2 * OMAP SmartReflex Voltage Control
4 * Author: Thara Gopinath <thara@ti.com>
6 * Copyright (C) 2012 Texas Instruments, Inc.
7 * Thara Gopinath <thara@ti.com>
9 * Copyright (C) 2008 Nokia Corporation
12 * Copyright (C) 2007 Texas Instruments, Inc.
13 * Lesly A M <x0080970@ti.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/clk.h>
24 #include <linux/debugfs.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/power/smartreflex.h>
30 #define DRIVER_NAME "smartreflex"
31 #define SMARTREFLEX_NAME_LEN 32
32 #define NVALUE_NAME_LEN 40
33 #define SR_DISABLE_TIMEOUT 200
35 /* sr_list contains all the instances of smartreflex module */
36 static LIST_HEAD(sr_list);
38 static struct omap_sr_class_data *sr_class;
39 static struct omap_sr_pmic_data *sr_pmic_data;
40 static struct dentry *sr_dbg_dir;
42 static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
44 __raw_writel(value, (sr->base + offset));
47 static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
53 * Smartreflex error config register is special as it contains
54 * certain status bits which if written a 1 into means a clear
55 * of those bits. So in order to make sure no accidental write of
56 * 1 happens to those status bits, do a clear of them in the read
57 * value. This mean this API doesn't rewrite values in these bits
58 * if they are currently set, but does allow the caller to write
61 if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
62 mask |= ERRCONFIG_STATUS_V1_MASK;
63 else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
64 mask |= ERRCONFIG_VPBOUNDINTST_V2;
66 reg_val = __raw_readl(sr->base + offset);
73 __raw_writel(reg_val, (sr->base + offset));
76 static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
78 return __raw_readl(sr->base + offset);
81 static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
83 struct omap_sr *sr_info;
86 pr_err("%s: Null voltage domain passed!\n", __func__);
87 return ERR_PTR(-EINVAL);
90 list_for_each_entry(sr_info, &sr_list, node) {
91 if (voltdm == sr_info->voltdm)
95 return ERR_PTR(-ENODATA);
98 static irqreturn_t sr_interrupt(int irq, void *data)
100 struct omap_sr *sr_info = data;
103 switch (sr_info->ip_type) {
105 /* Read the status bits */
106 status = sr_read_reg(sr_info, ERRCONFIG_V1);
108 /* Clear them by writing back */
109 sr_write_reg(sr_info, ERRCONFIG_V1, status);
112 /* Read the status bits */
113 status = sr_read_reg(sr_info, IRQSTATUS);
115 /* Clear them by writing back */
116 sr_write_reg(sr_info, IRQSTATUS, status);
119 dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
124 if (sr_class->notify)
125 sr_class->notify(sr_info, status);
130 static void sr_set_clk_length(struct omap_sr *sr)
135 fck = clk_get(&sr->pdev->dev, "fck");
138 dev_err(&sr->pdev->dev, "%s: unable to get fck for device %s\n",
139 __func__, dev_name(&sr->pdev->dev));
143 fclk_speed = clk_get_rate(fck);
146 switch (fclk_speed) {
148 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
151 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
154 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
157 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
160 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
163 dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
164 __func__, fclk_speed);
169 static void sr_start_vddautocomp(struct omap_sr *sr)
171 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
172 dev_warn(&sr->pdev->dev,
173 "%s: smartreflex class driver not registered\n",
178 if (!sr_class->enable(sr))
179 sr->autocomp_active = true;
182 static void sr_stop_vddautocomp(struct omap_sr *sr)
184 if (!sr_class || !(sr_class->disable)) {
185 dev_warn(&sr->pdev->dev,
186 "%s: smartreflex class driver not registered\n",
191 if (sr->autocomp_active) {
192 sr_class->disable(sr, 1);
193 sr->autocomp_active = false;
198 * This function handles the intializations which have to be done
199 * only when both sr device and class driver regiter has
200 * completed. This will be attempted to be called from both sr class
201 * driver register and sr device intializtion API's. Only one call
202 * will ultimately succeed.
204 * Currently this function registers interrupt handler for a particular SR
205 * if smartreflex class driver is already registered and has
206 * requested for interrupts and the SR interrupt line in present.
208 static int sr_late_init(struct omap_sr *sr_info)
210 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
213 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
214 ret = devm_request_irq(&sr_info->pdev->dev, sr_info->irq,
215 sr_interrupt, 0, sr_info->name, sr_info);
218 disable_irq(sr_info->irq);
221 if (pdata && pdata->enable_on_init)
222 sr_start_vddautocomp(sr_info);
227 list_del(&sr_info->node);
228 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering interrupt handler. Smartreflex will not function as desired\n",
234 static void sr_v1_disable(struct omap_sr *sr)
237 int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
238 ERRCONFIG_MCUBOUNDINTST;
240 /* Enable MCUDisableAcknowledge interrupt */
241 sr_modify_reg(sr, ERRCONFIG_V1,
242 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
244 /* SRCONFIG - disable SR */
245 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
247 /* Disable all other SR interrupts and clear the status as needed */
248 if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
249 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
250 sr_modify_reg(sr, ERRCONFIG_V1,
251 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
252 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
256 * Wait for SR to be disabled.
257 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
259 sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
260 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
263 if (timeout >= SR_DISABLE_TIMEOUT)
264 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
267 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
268 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
269 ERRCONFIG_MCUDISACKINTST);
272 static void sr_v2_disable(struct omap_sr *sr)
276 /* Enable MCUDisableAcknowledge interrupt */
277 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
279 /* SRCONFIG - disable SR */
280 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
283 * Disable all other SR interrupts and clear the status
284 * write to status register ONLY on need basis - only if status
287 if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
288 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
289 ERRCONFIG_VPBOUNDINTST_V2);
291 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
293 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
294 IRQENABLE_MCUVALIDINT |
295 IRQENABLE_MCUBOUNDSINT));
296 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
297 IRQSTATUS_MCVALIDINT |
298 IRQSTATUS_MCBOUNDSINT));
301 * Wait for SR to be disabled.
302 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
304 sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
305 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
308 if (timeout >= SR_DISABLE_TIMEOUT)
309 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
312 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
313 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
314 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
317 static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
318 struct omap_sr *sr, u32 efuse_offs)
322 if (!sr->nvalue_table) {
323 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
328 for (i = 0; i < sr->nvalue_count; i++) {
329 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
330 return &sr->nvalue_table[i];
336 /* Public Functions */
339 * sr_configure_errgen() - Configures the SmartReflex to perform AVS using the
340 * error generator module.
341 * @sr: SR module to be configured.
343 * This API is to be called from the smartreflex class driver to
344 * configure the error generator module inside the smartreflex module.
345 * SR settings if using the ERROR module inside Smartreflex.
346 * SR CLASS 3 by default uses only the ERROR module where as
347 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
348 * module. Returns 0 on success and error value in case of failure.
350 int sr_configure_errgen(struct omap_sr *sr)
352 u32 sr_config, sr_errconfig, errconfig_offs;
353 u32 vpboundint_en, vpboundint_st;
354 u32 senp_en = 0, senn_en = 0;
355 u8 senp_shift, senn_shift;
358 pr_warn("%s: NULL omap_sr from %pF\n",
359 __func__, (void *)_RET_IP_);
364 sr_set_clk_length(sr);
366 senp_en = sr->senp_mod;
367 senn_en = sr->senn_mod;
369 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
370 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
372 switch (sr->ip_type) {
374 sr_config |= SRCONFIG_DELAYCTRL;
375 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
376 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
377 errconfig_offs = ERRCONFIG_V1;
378 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
379 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
382 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
383 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
384 errconfig_offs = ERRCONFIG_V2;
385 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
386 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
389 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
394 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
395 sr_write_reg(sr, SRCONFIG, sr_config);
396 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
397 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
398 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
399 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
400 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
403 /* Enabling the interrupts if the ERROR module is used */
404 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
411 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
412 * @sr: SR module to be configured.
414 * This API is to be called from the smartreflex class driver to
415 * disable the error generator module inside the smartreflex module.
417 * Returns 0 on success and error value in case of failure.
419 int sr_disable_errgen(struct omap_sr *sr)
422 u32 vpboundint_en, vpboundint_st;
425 pr_warn("%s: NULL omap_sr from %pF\n",
426 __func__, (void *)_RET_IP_);
430 switch (sr->ip_type) {
432 errconfig_offs = ERRCONFIG_V1;
433 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
434 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
437 errconfig_offs = ERRCONFIG_V2;
438 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
439 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
442 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
447 /* Disable the Sensor and errorgen */
448 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
451 * Disable the interrupts of ERROR module
452 * NOTE: modify is a read, modify,write - an implicit OCP barrier
453 * which is required is present here - sequencing is critical
454 * at this point (after errgen is disabled, vpboundint disable)
456 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
462 * sr_configure_minmax() - Configures the SmartReflex to perform AVS using the
464 * @sr: SR module to be configured.
466 * This API is to be called from the smartreflex class driver to
467 * configure the minmaxavg module inside the smartreflex module.
468 * SR settings if using the ERROR module inside Smartreflex.
469 * SR CLASS 3 by default uses only the ERROR module where as
470 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
471 * module. Returns 0 on success and error value in case of failure.
473 int sr_configure_minmax(struct omap_sr *sr)
475 u32 sr_config, sr_avgwt;
476 u32 senp_en = 0, senn_en = 0;
477 u8 senp_shift, senn_shift;
480 pr_warn("%s: NULL omap_sr from %pF\n",
481 __func__, (void *)_RET_IP_);
486 sr_set_clk_length(sr);
488 senp_en = sr->senp_mod;
489 senn_en = sr->senn_mod;
491 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
493 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
495 switch (sr->ip_type) {
497 sr_config |= SRCONFIG_DELAYCTRL;
498 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
499 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
502 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
503 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
506 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
511 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
512 sr_write_reg(sr, SRCONFIG, sr_config);
513 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
514 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
515 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
518 * Enabling the interrupts if MINMAXAVG module is used.
519 * TODO: check if all the interrupts are mandatory
521 switch (sr->ip_type) {
523 sr_modify_reg(sr, ERRCONFIG_V1,
524 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
525 ERRCONFIG_MCUBOUNDINTEN),
526 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
527 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
528 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
531 sr_write_reg(sr, IRQSTATUS,
532 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
533 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
534 sr_write_reg(sr, IRQENABLE_SET,
535 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
536 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
539 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
548 * sr_enable() - Enables the smartreflex module.
549 * @sr: pointer to which the SR module to be configured belongs to.
550 * @volt: The voltage at which the Voltage domain associated with
551 * the smartreflex module is operating at.
552 * This is required only to program the correct Ntarget value.
554 * This API is to be called from the smartreflex class driver to
555 * enable a smartreflex module. Returns 0 on success. Returns error
556 * value if the voltage passed is wrong or if ntarget value is wrong.
558 int sr_enable(struct omap_sr *sr, unsigned long volt)
560 struct omap_volt_data *volt_data;
561 struct omap_sr_nvalue_table *nvalue_row;
565 pr_warn("%s: NULL omap_sr from %pF\n",
566 __func__, (void *)_RET_IP_);
570 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
572 if (IS_ERR(volt_data)) {
573 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table for nominal voltage %ld\n",
575 return PTR_ERR(volt_data);
578 nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
581 dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
586 /* errminlimit is opp dependent and hence linked to voltage */
587 sr->err_minlimit = nvalue_row->errminlimit;
589 pm_runtime_get_sync(&sr->pdev->dev);
591 /* Check if SR is already enabled. If yes do nothing */
592 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
596 ret = sr_class->configure(sr);
600 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
602 /* SRCONFIG - enable SR */
603 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
608 * sr_disable() - Disables the smartreflex module.
609 * @sr: pointer to which the SR module to be configured belongs to.
611 * This API is to be called from the smartreflex class driver to
612 * disable a smartreflex module.
614 void sr_disable(struct omap_sr *sr)
617 pr_warn("%s: NULL omap_sr from %pF\n",
618 __func__, (void *)_RET_IP_);
622 /* Check if SR clocks are already disabled. If yes do nothing */
623 if (pm_runtime_suspended(&sr->pdev->dev))
627 * Disable SR if only it is indeed enabled. Else just
628 * disable the clocks.
630 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
631 switch (sr->ip_type) {
639 dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
644 pm_runtime_put_sync_suspend(&sr->pdev->dev);
648 * sr_register_class() - API to register a smartreflex class parameters.
649 * @class_data: The structure containing various sr class specific data.
651 * This API is to be called by the smartreflex class driver to register itself
652 * with the smartreflex driver during init. Returns 0 on success else the
655 int sr_register_class(struct omap_sr_class_data *class_data)
657 struct omap_sr *sr_info;
660 pr_warn("%s:, Smartreflex class data passed is NULL\n",
666 pr_warn("%s: Smartreflex class driver already registered\n",
671 sr_class = class_data;
674 * Call into late init to do intializations that require
675 * both sr driver and sr class driver to be initiallized.
677 list_for_each_entry(sr_info, &sr_list, node)
678 sr_late_init(sr_info);
684 * omap_sr_enable() - API to enable SR clocks and to call into the
685 * registered smartreflex class enable API.
686 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
688 * This API is to be called from the kernel in order to enable
689 * a particular smartreflex module. This API will do the initial
690 * configurations to turn on the smartreflex module and in turn call
691 * into the registered smartreflex class enable API.
693 void omap_sr_enable(struct voltagedomain *voltdm)
695 struct omap_sr *sr = _sr_lookup(voltdm);
698 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
702 if (!sr->autocomp_active)
705 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
706 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
711 sr_class->enable(sr);
715 * omap_sr_disable() - API to disable SR without resetting the voltage
717 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
719 * This API is to be called from the kernel in order to disable
720 * a particular smartreflex module. This API will in turn call
721 * into the registered smartreflex class disable API. This API will tell
722 * the smartreflex class disable not to reset the VP voltage after
723 * disabling smartreflex.
725 void omap_sr_disable(struct voltagedomain *voltdm)
727 struct omap_sr *sr = _sr_lookup(voltdm);
730 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
734 if (!sr->autocomp_active)
737 if (!sr_class || !(sr_class->disable)) {
738 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
743 sr_class->disable(sr, 0);
747 * omap_sr_disable_reset_volt() - API to disable SR and reset the
748 * voltage processor voltage
749 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
751 * This API is to be called from the kernel in order to disable
752 * a particular smartreflex module. This API will in turn call
753 * into the registered smartreflex class disable API. This API will tell
754 * the smartreflex class disable to reset the VP voltage after
755 * disabling smartreflex.
757 void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
759 struct omap_sr *sr = _sr_lookup(voltdm);
762 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
766 if (!sr->autocomp_active)
769 if (!sr_class || !(sr_class->disable)) {
770 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
775 sr_class->disable(sr, 1);
779 * omap_sr_register_pmic() - API to register pmic specific info.
780 * @pmic_data: The structure containing pmic specific data.
782 * This API is to be called from the PMIC specific code to register with
783 * smartreflex driver pmic specific info. Currently the only info required
784 * is the smartreflex init on the PMIC side.
786 void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
789 pr_warn("%s: Trying to register NULL PMIC data structure with smartreflex\n",
794 sr_pmic_data = pmic_data;
797 /* PM Debug FS entries to enable and disable smartreflex. */
798 static int omap_sr_autocomp_show(void *data, u64 *val)
800 struct omap_sr *sr_info = data;
803 pr_warn("%s: omap_sr struct not found\n", __func__);
807 *val = sr_info->autocomp_active;
812 static int omap_sr_autocomp_store(void *data, u64 val)
814 struct omap_sr *sr_info = data;
817 pr_warn("%s: omap_sr struct not found\n", __func__);
823 pr_warn("%s: Invalid argument %lld\n", __func__, val);
827 /* control enable/disable only if there is a delta in value */
828 if (sr_info->autocomp_active != val) {
830 sr_stop_vddautocomp(sr_info);
832 sr_start_vddautocomp(sr_info);
838 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
839 omap_sr_autocomp_store, "%llu\n");
841 static int __init omap_sr_probe(struct platform_device *pdev)
843 struct omap_sr *sr_info;
844 struct omap_sr_data *pdata = pdev->dev.platform_data;
845 struct resource *mem, *irq;
846 struct dentry *nvalue_dir;
849 sr_info = devm_kzalloc(&pdev->dev, sizeof(struct omap_sr), GFP_KERNEL);
853 sr_info->name = devm_kzalloc(&pdev->dev,
854 SMARTREFLEX_NAME_LEN, GFP_KERNEL);
858 platform_set_drvdata(pdev, sr_info);
861 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
865 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
866 sr_info->base = devm_ioremap_resource(&pdev->dev, mem);
867 if (IS_ERR(sr_info->base)) {
868 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
869 return PTR_ERR(sr_info->base);
872 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
874 pm_runtime_enable(&pdev->dev);
875 pm_runtime_irq_safe(&pdev->dev);
877 snprintf(sr_info->name, SMARTREFLEX_NAME_LEN, "%s", pdata->name);
879 sr_info->pdev = pdev;
880 sr_info->srid = pdev->id;
881 sr_info->voltdm = pdata->voltdm;
882 sr_info->nvalue_table = pdata->nvalue_table;
883 sr_info->nvalue_count = pdata->nvalue_count;
884 sr_info->senn_mod = pdata->senn_mod;
885 sr_info->senp_mod = pdata->senp_mod;
886 sr_info->err_weight = pdata->err_weight;
887 sr_info->err_maxlimit = pdata->err_maxlimit;
888 sr_info->accum_data = pdata->accum_data;
889 sr_info->senn_avgweight = pdata->senn_avgweight;
890 sr_info->senp_avgweight = pdata->senp_avgweight;
891 sr_info->autocomp_active = false;
892 sr_info->ip_type = pdata->ip_type;
895 sr_info->irq = irq->start;
897 sr_set_clk_length(sr_info);
899 list_add(&sr_info->node, &sr_list);
902 * Call into late init to do intializations that require
903 * both sr driver and sr class driver to be initiallized.
906 ret = sr_late_init(sr_info);
908 pr_warn("%s: Error in SR late init\n", __func__);
913 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
915 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
916 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
917 ret = PTR_ERR(sr_dbg_dir);
918 pr_err("%s:sr debugfs dir creation failed(%d)\n",
924 sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
925 if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
926 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
928 ret = PTR_ERR(sr_info->dbg_dir);
932 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
933 sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
934 (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
935 &sr_info->err_weight);
936 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
937 &sr_info->err_maxlimit);
939 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
940 if (IS_ERR_OR_NULL(nvalue_dir)) {
941 dev_err(&pdev->dev, "%s: Unable to create debugfs directory for n-values\n",
943 ret = PTR_ERR(nvalue_dir);
947 if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
948 dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
949 __func__, sr_info->name);
955 for (i = 0; i < sr_info->nvalue_count; i++) {
956 char name[NVALUE_NAME_LEN + 1];
958 snprintf(name, sizeof(name), "volt_%lu",
959 sr_info->nvalue_table[i].volt_nominal);
960 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
961 &(sr_info->nvalue_table[i].nvalue));
962 snprintf(name, sizeof(name), "errminlimit_%lu",
963 sr_info->nvalue_table[i].volt_nominal);
964 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
965 &(sr_info->nvalue_table[i].errminlimit));
972 debugfs_remove_recursive(sr_info->dbg_dir);
974 list_del(&sr_info->node);
978 static int omap_sr_remove(struct platform_device *pdev)
980 struct omap_sr_data *pdata = pdev->dev.platform_data;
981 struct omap_sr *sr_info;
984 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
988 sr_info = _sr_lookup(pdata->voltdm);
989 if (IS_ERR(sr_info)) {
990 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
992 return PTR_ERR(sr_info);
995 if (sr_info->autocomp_active)
996 sr_stop_vddautocomp(sr_info);
997 debugfs_remove_recursive(sr_info->dbg_dir);
999 pm_runtime_disable(&pdev->dev);
1000 list_del(&sr_info->node);
1004 static void omap_sr_shutdown(struct platform_device *pdev)
1006 struct omap_sr_data *pdata = pdev->dev.platform_data;
1007 struct omap_sr *sr_info;
1010 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1014 sr_info = _sr_lookup(pdata->voltdm);
1015 if (IS_ERR(sr_info)) {
1016 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1021 if (sr_info->autocomp_active)
1022 sr_stop_vddautocomp(sr_info);
1027 static struct platform_driver smartreflex_driver = {
1028 .remove = omap_sr_remove,
1029 .shutdown = omap_sr_shutdown,
1031 .name = DRIVER_NAME,
1035 static int __init sr_init(void)
1040 * sr_init is a late init. If by then a pmic specific API is not
1041 * registered either there is no need for anything to be done on
1042 * the PMIC side or somebody has forgotten to register a PMIC
1043 * handler. Warn for the second condition.
1045 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1046 sr_pmic_data->sr_pmic_init();
1048 pr_warn("%s: No PMIC hook to init smartreflex\n", __func__);
1050 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1052 pr_err("%s: platform driver register failed for SR\n",
1059 late_initcall(sr_init);
1061 static void __exit sr_exit(void)
1063 platform_driver_unregister(&smartreflex_driver);
1065 module_exit(sr_exit);
1067 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1068 MODULE_LICENSE("GPL");
1069 MODULE_ALIAS("platform:" DRIVER_NAME);
1070 MODULE_AUTHOR("Texas Instruments Inc");