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>
32 #define SMARTREFLEX_NAME_LEN 16
33 #define NVALUE_NAME_LEN 40
34 #define SR_DISABLE_TIMEOUT 200
36 /* sr_list contains all the instances of smartreflex module */
37 static LIST_HEAD(sr_list
);
39 static struct omap_sr_class_data
*sr_class
;
40 static struct omap_sr_pmic_data
*sr_pmic_data
;
41 static struct dentry
*sr_dbg_dir
;
43 static inline void sr_write_reg(struct omap_sr
*sr
, unsigned offset
, u32 value
)
45 __raw_writel(value
, (sr
->base
+ offset
));
48 static inline void sr_modify_reg(struct omap_sr
*sr
, unsigned offset
, u32 mask
,
54 * Smartreflex error config register is special as it contains
55 * certain status bits which if written a 1 into means a clear
56 * of those bits. So in order to make sure no accidental write of
57 * 1 happens to those status bits, do a clear of them in the read
58 * value. This mean this API doesn't rewrite values in these bits
59 * if they are currently set, but does allow the caller to write
62 if (sr
->ip_type
== SR_TYPE_V1
&& offset
== ERRCONFIG_V1
)
63 mask
|= ERRCONFIG_STATUS_V1_MASK
;
64 else if (sr
->ip_type
== SR_TYPE_V2
&& offset
== ERRCONFIG_V2
)
65 mask
|= ERRCONFIG_VPBOUNDINTST_V2
;
67 reg_val
= __raw_readl(sr
->base
+ offset
);
74 __raw_writel(reg_val
, (sr
->base
+ offset
));
77 static inline u32
sr_read_reg(struct omap_sr
*sr
, unsigned offset
)
79 return __raw_readl(sr
->base
+ offset
);
82 static struct omap_sr
*_sr_lookup(struct voltagedomain
*voltdm
)
84 struct omap_sr
*sr_info
;
87 pr_err("%s: Null voltage domain passed!\n", __func__
);
88 return ERR_PTR(-EINVAL
);
91 list_for_each_entry(sr_info
, &sr_list
, node
) {
92 if (voltdm
== sr_info
->voltdm
)
96 return ERR_PTR(-ENODATA
);
99 static irqreturn_t
sr_interrupt(int irq
, void *data
)
101 struct omap_sr
*sr_info
= data
;
104 switch (sr_info
->ip_type
) {
106 /* Read the status bits */
107 status
= sr_read_reg(sr_info
, ERRCONFIG_V1
);
109 /* Clear them by writing back */
110 sr_write_reg(sr_info
, ERRCONFIG_V1
, status
);
113 /* Read the status bits */
114 status
= sr_read_reg(sr_info
, IRQSTATUS
);
116 /* Clear them by writing back */
117 sr_write_reg(sr_info
, IRQSTATUS
, status
);
120 dev_err(&sr_info
->pdev
->dev
, "UNKNOWN IP type %d\n",
125 if (sr_class
->notify
)
126 sr_class
->notify(sr_info
, status
);
131 static void sr_set_clk_length(struct omap_sr
*sr
)
136 fck
= clk_get(&sr
->pdev
->dev
, "fck");
139 dev_err(&sr
->pdev
->dev
, "%s: unable to get fck for device %s\n",
140 __func__
, dev_name(&sr
->pdev
->dev
));
144 fclk_speed
= clk_get_rate(fck
);
147 switch (fclk_speed
) {
149 sr
->clk_length
= SRCLKLENGTH_12MHZ_SYSCLK
;
152 sr
->clk_length
= SRCLKLENGTH_13MHZ_SYSCLK
;
155 sr
->clk_length
= SRCLKLENGTH_19MHZ_SYSCLK
;
158 sr
->clk_length
= SRCLKLENGTH_26MHZ_SYSCLK
;
161 sr
->clk_length
= SRCLKLENGTH_38MHZ_SYSCLK
;
164 dev_err(&sr
->pdev
->dev
, "%s: Invalid fclk rate: %d\n",
165 __func__
, fclk_speed
);
170 static void sr_start_vddautocomp(struct omap_sr
*sr
)
172 if (!sr_class
|| !(sr_class
->enable
) || !(sr_class
->configure
)) {
173 dev_warn(&sr
->pdev
->dev
,
174 "%s: smartreflex class driver not registered\n",
179 if (!sr_class
->enable(sr
))
180 sr
->autocomp_active
= true;
183 static void sr_stop_vddautocomp(struct omap_sr
*sr
)
185 if (!sr_class
|| !(sr_class
->disable
)) {
186 dev_warn(&sr
->pdev
->dev
,
187 "%s: smartreflex class driver not registered\n",
192 if (sr
->autocomp_active
) {
193 sr_class
->disable(sr
, 1);
194 sr
->autocomp_active
= false;
199 * This function handles the intializations which have to be done
200 * only when both sr device and class driver regiter has
201 * completed. This will be attempted to be called from both sr class
202 * driver register and sr device intializtion API's. Only one call
203 * will ultimately succeed.
205 * Currently this function registers interrupt handler for a particular SR
206 * if smartreflex class driver is already registered and has
207 * requested for interrupts and the SR interrupt line in present.
209 static int sr_late_init(struct omap_sr
*sr_info
)
211 struct omap_sr_data
*pdata
= sr_info
->pdev
->dev
.platform_data
;
212 struct resource
*mem
;
215 if (sr_class
->notify
&& sr_class
->notify_flags
&& sr_info
->irq
) {
216 ret
= request_irq(sr_info
->irq
, sr_interrupt
,
217 0, sr_info
->name
, sr_info
);
220 disable_irq(sr_info
->irq
);
223 if (pdata
&& pdata
->enable_on_init
)
224 sr_start_vddautocomp(sr_info
);
229 iounmap(sr_info
->base
);
230 mem
= platform_get_resource(sr_info
->pdev
, IORESOURCE_MEM
, 0);
231 release_mem_region(mem
->start
, resource_size(mem
));
232 list_del(&sr_info
->node
);
233 dev_err(&sr_info
->pdev
->dev
, "%s: ERROR in registering"
234 "interrupt handler. Smartreflex will"
235 "not function as desired\n", __func__
);
241 static void sr_v1_disable(struct omap_sr
*sr
)
244 int errconf_val
= ERRCONFIG_MCUACCUMINTST
| ERRCONFIG_MCUVALIDINTST
|
245 ERRCONFIG_MCUBOUNDINTST
;
247 /* Enable MCUDisableAcknowledge interrupt */
248 sr_modify_reg(sr
, ERRCONFIG_V1
,
249 ERRCONFIG_MCUDISACKINTEN
, ERRCONFIG_MCUDISACKINTEN
);
251 /* SRCONFIG - disable SR */
252 sr_modify_reg(sr
, SRCONFIG
, SRCONFIG_SRENABLE
, 0x0);
254 /* Disable all other SR interrupts and clear the status as needed */
255 if (sr_read_reg(sr
, ERRCONFIG_V1
) & ERRCONFIG_VPBOUNDINTST_V1
)
256 errconf_val
|= ERRCONFIG_VPBOUNDINTST_V1
;
257 sr_modify_reg(sr
, ERRCONFIG_V1
,
258 (ERRCONFIG_MCUACCUMINTEN
| ERRCONFIG_MCUVALIDINTEN
|
259 ERRCONFIG_MCUBOUNDINTEN
| ERRCONFIG_VPBOUNDINTEN_V1
),
263 * Wait for SR to be disabled.
264 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
266 sr_test_cond_timeout((sr_read_reg(sr
, ERRCONFIG_V1
) &
267 ERRCONFIG_MCUDISACKINTST
), SR_DISABLE_TIMEOUT
,
270 if (timeout
>= SR_DISABLE_TIMEOUT
)
271 dev_warn(&sr
->pdev
->dev
, "%s: Smartreflex disable timedout\n",
274 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
275 sr_modify_reg(sr
, ERRCONFIG_V1
, ERRCONFIG_MCUDISACKINTEN
,
276 ERRCONFIG_MCUDISACKINTST
);
279 static void sr_v2_disable(struct omap_sr
*sr
)
283 /* Enable MCUDisableAcknowledge interrupt */
284 sr_write_reg(sr
, IRQENABLE_SET
, IRQENABLE_MCUDISABLEACKINT
);
286 /* SRCONFIG - disable SR */
287 sr_modify_reg(sr
, SRCONFIG
, SRCONFIG_SRENABLE
, 0x0);
290 * Disable all other SR interrupts and clear the status
291 * write to status register ONLY on need basis - only if status
294 if (sr_read_reg(sr
, ERRCONFIG_V2
) & ERRCONFIG_VPBOUNDINTST_V2
)
295 sr_modify_reg(sr
, ERRCONFIG_V2
, ERRCONFIG_VPBOUNDINTEN_V2
,
296 ERRCONFIG_VPBOUNDINTST_V2
);
298 sr_modify_reg(sr
, ERRCONFIG_V2
, ERRCONFIG_VPBOUNDINTEN_V2
,
300 sr_write_reg(sr
, IRQENABLE_CLR
, (IRQENABLE_MCUACCUMINT
|
301 IRQENABLE_MCUVALIDINT
|
302 IRQENABLE_MCUBOUNDSINT
));
303 sr_write_reg(sr
, IRQSTATUS
, (IRQSTATUS_MCUACCUMINT
|
304 IRQSTATUS_MCVALIDINT
|
305 IRQSTATUS_MCBOUNDSINT
));
308 * Wait for SR to be disabled.
309 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
311 sr_test_cond_timeout((sr_read_reg(sr
, IRQSTATUS
) &
312 IRQSTATUS_MCUDISABLEACKINT
), SR_DISABLE_TIMEOUT
,
315 if (timeout
>= SR_DISABLE_TIMEOUT
)
316 dev_warn(&sr
->pdev
->dev
, "%s: Smartreflex disable timedout\n",
319 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
320 sr_write_reg(sr
, IRQENABLE_CLR
, IRQENABLE_MCUDISABLEACKINT
);
321 sr_write_reg(sr
, IRQSTATUS
, IRQSTATUS_MCUDISABLEACKINT
);
324 static struct omap_sr_nvalue_table
*sr_retrieve_nvalue_row(
325 struct omap_sr
*sr
, u32 efuse_offs
)
329 if (!sr
->nvalue_table
) {
330 dev_warn(&sr
->pdev
->dev
, "%s: Missing ntarget value table\n",
335 for (i
= 0; i
< sr
->nvalue_count
; i
++) {
336 if (sr
->nvalue_table
[i
].efuse_offs
== efuse_offs
)
337 return &sr
->nvalue_table
[i
];
343 /* Public Functions */
346 * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
347 * error generator module.
348 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
350 * This API is to be called from the smartreflex class driver to
351 * configure the error generator module inside the smartreflex module.
352 * SR settings if using the ERROR module inside Smartreflex.
353 * SR CLASS 3 by default uses only the ERROR module where as
354 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
355 * module. Returns 0 on success and error value in case of failure.
357 int sr_configure_errgen(struct voltagedomain
*voltdm
)
359 u32 sr_config
, sr_errconfig
, errconfig_offs
;
360 u32 vpboundint_en
, vpboundint_st
;
361 u32 senp_en
= 0, senn_en
= 0;
362 u8 senp_shift
, senn_shift
;
363 struct omap_sr
*sr
= _sr_lookup(voltdm
);
366 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
371 sr_set_clk_length(sr
);
373 senp_en
= sr
->senp_mod
;
374 senn_en
= sr
->senn_mod
;
376 sr_config
= (sr
->clk_length
<< SRCONFIG_SRCLKLENGTH_SHIFT
) |
377 SRCONFIG_SENENABLE
| SRCONFIG_ERRGEN_EN
;
379 switch (sr
->ip_type
) {
381 sr_config
|= SRCONFIG_DELAYCTRL
;
382 senn_shift
= SRCONFIG_SENNENABLE_V1_SHIFT
;
383 senp_shift
= SRCONFIG_SENPENABLE_V1_SHIFT
;
384 errconfig_offs
= ERRCONFIG_V1
;
385 vpboundint_en
= ERRCONFIG_VPBOUNDINTEN_V1
;
386 vpboundint_st
= ERRCONFIG_VPBOUNDINTST_V1
;
389 senn_shift
= SRCONFIG_SENNENABLE_V2_SHIFT
;
390 senp_shift
= SRCONFIG_SENPENABLE_V2_SHIFT
;
391 errconfig_offs
= ERRCONFIG_V2
;
392 vpboundint_en
= ERRCONFIG_VPBOUNDINTEN_V2
;
393 vpboundint_st
= ERRCONFIG_VPBOUNDINTST_V2
;
396 dev_err(&sr
->pdev
->dev
, "%s: Trying to Configure smartreflex"
397 "module without specifying the ip\n", __func__
);
401 sr_config
|= ((senn_en
<< senn_shift
) | (senp_en
<< senp_shift
));
402 sr_write_reg(sr
, SRCONFIG
, sr_config
);
403 sr_errconfig
= (sr
->err_weight
<< ERRCONFIG_ERRWEIGHT_SHIFT
) |
404 (sr
->err_maxlimit
<< ERRCONFIG_ERRMAXLIMIT_SHIFT
) |
405 (sr
->err_minlimit
<< ERRCONFIG_ERRMINLIMIT_SHIFT
);
406 sr_modify_reg(sr
, errconfig_offs
, (SR_ERRWEIGHT_MASK
|
407 SR_ERRMAXLIMIT_MASK
| SR_ERRMINLIMIT_MASK
),
410 /* Enabling the interrupts if the ERROR module is used */
411 sr_modify_reg(sr
, errconfig_offs
, (vpboundint_en
| vpboundint_st
),
418 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
419 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
421 * This API is to be called from the smartreflex class driver to
422 * disable the error generator module inside the smartreflex module.
424 * Returns 0 on success and error value in case of failure.
426 int sr_disable_errgen(struct voltagedomain
*voltdm
)
429 u32 vpboundint_en
, vpboundint_st
;
430 struct omap_sr
*sr
= _sr_lookup(voltdm
);
433 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
437 switch (sr
->ip_type
) {
439 errconfig_offs
= ERRCONFIG_V1
;
440 vpboundint_en
= ERRCONFIG_VPBOUNDINTEN_V1
;
441 vpboundint_st
= ERRCONFIG_VPBOUNDINTST_V1
;
444 errconfig_offs
= ERRCONFIG_V2
;
445 vpboundint_en
= ERRCONFIG_VPBOUNDINTEN_V2
;
446 vpboundint_st
= ERRCONFIG_VPBOUNDINTST_V2
;
449 dev_err(&sr
->pdev
->dev
, "%s: Trying to Configure smartreflex"
450 "module without specifying the ip\n", __func__
);
454 /* Disable the interrupts of ERROR module */
455 sr_modify_reg(sr
, errconfig_offs
, vpboundint_en
| vpboundint_st
, 0);
457 /* Disable the Sensor and errorgen */
458 sr_modify_reg(sr
, SRCONFIG
, SRCONFIG_SENENABLE
| SRCONFIG_ERRGEN_EN
, 0);
464 * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
466 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
468 * This API is to be called from the smartreflex class driver to
469 * configure the minmaxavg module inside the smartreflex module.
470 * SR settings if using the ERROR module inside Smartreflex.
471 * SR CLASS 3 by default uses only the ERROR module where as
472 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
473 * module. Returns 0 on success and error value in case of failure.
475 int sr_configure_minmax(struct voltagedomain
*voltdm
)
477 u32 sr_config
, sr_avgwt
;
478 u32 senp_en
= 0, senn_en
= 0;
479 u8 senp_shift
, senn_shift
;
480 struct omap_sr
*sr
= _sr_lookup(voltdm
);
483 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
488 sr_set_clk_length(sr
);
490 senp_en
= sr
->senp_mod
;
491 senn_en
= sr
->senn_mod
;
493 sr_config
= (sr
->clk_length
<< SRCONFIG_SRCLKLENGTH_SHIFT
) |
495 (sr
->accum_data
<< SRCONFIG_ACCUMDATA_SHIFT
);
497 switch (sr
->ip_type
) {
499 sr_config
|= SRCONFIG_DELAYCTRL
;
500 senn_shift
= SRCONFIG_SENNENABLE_V1_SHIFT
;
501 senp_shift
= SRCONFIG_SENPENABLE_V1_SHIFT
;
504 senn_shift
= SRCONFIG_SENNENABLE_V2_SHIFT
;
505 senp_shift
= SRCONFIG_SENPENABLE_V2_SHIFT
;
508 dev_err(&sr
->pdev
->dev
, "%s: Trying to Configure smartreflex"
509 "module without specifying the ip\n", __func__
);
513 sr_config
|= ((senn_en
<< senn_shift
) | (senp_en
<< senp_shift
));
514 sr_write_reg(sr
, SRCONFIG
, sr_config
);
515 sr_avgwt
= (sr
->senp_avgweight
<< AVGWEIGHT_SENPAVGWEIGHT_SHIFT
) |
516 (sr
->senn_avgweight
<< AVGWEIGHT_SENNAVGWEIGHT_SHIFT
);
517 sr_write_reg(sr
, AVGWEIGHT
, sr_avgwt
);
520 * Enabling the interrupts if MINMAXAVG module is used.
521 * TODO: check if all the interrupts are mandatory
523 switch (sr
->ip_type
) {
525 sr_modify_reg(sr
, ERRCONFIG_V1
,
526 (ERRCONFIG_MCUACCUMINTEN
| ERRCONFIG_MCUVALIDINTEN
|
527 ERRCONFIG_MCUBOUNDINTEN
),
528 (ERRCONFIG_MCUACCUMINTEN
| ERRCONFIG_MCUACCUMINTST
|
529 ERRCONFIG_MCUVALIDINTEN
| ERRCONFIG_MCUVALIDINTST
|
530 ERRCONFIG_MCUBOUNDINTEN
| ERRCONFIG_MCUBOUNDINTST
));
533 sr_write_reg(sr
, IRQSTATUS
,
534 IRQSTATUS_MCUACCUMINT
| IRQSTATUS_MCVALIDINT
|
535 IRQSTATUS_MCBOUNDSINT
| IRQSTATUS_MCUDISABLEACKINT
);
536 sr_write_reg(sr
, IRQENABLE_SET
,
537 IRQENABLE_MCUACCUMINT
| IRQENABLE_MCUVALIDINT
|
538 IRQENABLE_MCUBOUNDSINT
| IRQENABLE_MCUDISABLEACKINT
);
541 dev_err(&sr
->pdev
->dev
, "%s: Trying to Configure smartreflex"
542 "module without specifying the ip\n", __func__
);
550 * sr_enable() - Enables the smartreflex module.
551 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
552 * @volt: The voltage at which the Voltage domain associated with
553 * the smartreflex module is operating at.
554 * This is required only to program the correct Ntarget value.
556 * This API is to be called from the smartreflex class driver to
557 * enable a smartreflex module. Returns 0 on success. Returns error
558 * value if the voltage passed is wrong or if ntarget value is wrong.
560 int sr_enable(struct voltagedomain
*voltdm
, unsigned long volt
)
562 struct omap_volt_data
*volt_data
;
563 struct omap_sr
*sr
= _sr_lookup(voltdm
);
564 struct omap_sr_nvalue_table
*nvalue_row
;
568 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
572 volt_data
= omap_voltage_get_voltdata(sr
->voltdm
, volt
);
574 if (IS_ERR(volt_data
)) {
575 dev_warn(&sr
->pdev
->dev
, "%s: Unable to get voltage table"
576 "for nominal voltage %ld\n", __func__
, volt
);
577 return PTR_ERR(volt_data
);
580 nvalue_row
= sr_retrieve_nvalue_row(sr
, volt_data
->sr_efuse_offs
);
583 dev_warn(&sr
->pdev
->dev
, "%s: failure getting SR data for this voltage %ld\n",
588 /* errminlimit is opp dependent and hence linked to voltage */
589 sr
->err_minlimit
= nvalue_row
->errminlimit
;
591 pm_runtime_get_sync(&sr
->pdev
->dev
);
593 /* Check if SR is already enabled. If yes do nothing */
594 if (sr_read_reg(sr
, SRCONFIG
) & SRCONFIG_SRENABLE
)
598 ret
= sr_class
->configure(sr
);
602 sr_write_reg(sr
, NVALUERECIPROCAL
, nvalue_row
->nvalue
);
604 /* SRCONFIG - enable SR */
605 sr_modify_reg(sr
, SRCONFIG
, SRCONFIG_SRENABLE
, SRCONFIG_SRENABLE
);
610 * sr_disable() - Disables the smartreflex module.
611 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
613 * This API is to be called from the smartreflex class driver to
614 * disable a smartreflex module.
616 void sr_disable(struct voltagedomain
*voltdm
)
618 struct omap_sr
*sr
= _sr_lookup(voltdm
);
621 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
625 /* Check if SR clocks are already disabled. If yes do nothing */
626 if (pm_runtime_suspended(&sr
->pdev
->dev
))
630 * Disable SR if only it is indeed enabled. Else just
631 * disable the clocks.
633 if (sr_read_reg(sr
, SRCONFIG
) & SRCONFIG_SRENABLE
) {
634 switch (sr
->ip_type
) {
642 dev_err(&sr
->pdev
->dev
, "UNKNOWN IP type %d\n",
647 pm_runtime_put_sync_suspend(&sr
->pdev
->dev
);
651 * sr_register_class() - API to register a smartreflex class parameters.
652 * @class_data: The structure containing various sr class specific data.
654 * This API is to be called by the smartreflex class driver to register itself
655 * with the smartreflex driver during init. Returns 0 on success else the
658 int sr_register_class(struct omap_sr_class_data
*class_data
)
660 struct omap_sr
*sr_info
;
663 pr_warning("%s:, Smartreflex class data passed is NULL\n",
669 pr_warning("%s: Smartreflex class driver already registered\n",
674 sr_class
= class_data
;
677 * Call into late init to do intializations that require
678 * both sr driver and sr class driver to be initiallized.
680 list_for_each_entry(sr_info
, &sr_list
, node
)
681 sr_late_init(sr_info
);
687 * omap_sr_enable() - API to enable SR clocks and to call into the
688 * registered smartreflex class enable API.
689 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
691 * This API is to be called from the kernel in order to enable
692 * a particular smartreflex module. This API will do the initial
693 * configurations to turn on the smartreflex module and in turn call
694 * into the registered smartreflex class enable API.
696 void omap_sr_enable(struct voltagedomain
*voltdm
)
698 struct omap_sr
*sr
= _sr_lookup(voltdm
);
701 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
705 if (!sr
->autocomp_active
)
708 if (!sr_class
|| !(sr_class
->enable
) || !(sr_class
->configure
)) {
709 dev_warn(&sr
->pdev
->dev
, "%s: smartreflex class driver not"
710 "registered\n", __func__
);
714 sr_class
->enable(sr
);
718 * omap_sr_disable() - API to disable SR without resetting the voltage
720 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
722 * This API is to be called from the kernel in order to disable
723 * a particular smartreflex module. This API will in turn call
724 * into the registered smartreflex class disable API. This API will tell
725 * the smartreflex class disable not to reset the VP voltage after
726 * disabling smartreflex.
728 void omap_sr_disable(struct voltagedomain
*voltdm
)
730 struct omap_sr
*sr
= _sr_lookup(voltdm
);
733 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
737 if (!sr
->autocomp_active
)
740 if (!sr_class
|| !(sr_class
->disable
)) {
741 dev_warn(&sr
->pdev
->dev
, "%s: smartreflex class driver not"
742 "registered\n", __func__
);
746 sr_class
->disable(sr
, 0);
750 * omap_sr_disable_reset_volt() - API to disable SR and reset the
751 * voltage processor voltage
752 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
754 * This API is to be called from the kernel in order to disable
755 * a particular smartreflex module. This API will in turn call
756 * into the registered smartreflex class disable API. This API will tell
757 * the smartreflex class disable to reset the VP voltage after
758 * disabling smartreflex.
760 void omap_sr_disable_reset_volt(struct voltagedomain
*voltdm
)
762 struct omap_sr
*sr
= _sr_lookup(voltdm
);
765 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__
);
769 if (!sr
->autocomp_active
)
772 if (!sr_class
|| !(sr_class
->disable
)) {
773 dev_warn(&sr
->pdev
->dev
, "%s: smartreflex class driver not"
774 "registered\n", __func__
);
778 sr_class
->disable(sr
, 1);
782 * omap_sr_register_pmic() - API to register pmic specific info.
783 * @pmic_data: The structure containing pmic specific data.
785 * This API is to be called from the PMIC specific code to register with
786 * smartreflex driver pmic specific info. Currently the only info required
787 * is the smartreflex init on the PMIC side.
789 void omap_sr_register_pmic(struct omap_sr_pmic_data
*pmic_data
)
792 pr_warning("%s: Trying to register NULL PMIC data structure"
793 "with smartreflex\n", __func__
);
797 sr_pmic_data
= pmic_data
;
800 /* PM Debug FS entries to enable and disable smartreflex. */
801 static int omap_sr_autocomp_show(void *data
, u64
*val
)
803 struct omap_sr
*sr_info
= data
;
806 pr_warning("%s: omap_sr struct not found\n", __func__
);
810 *val
= sr_info
->autocomp_active
;
815 static int omap_sr_autocomp_store(void *data
, u64 val
)
817 struct omap_sr
*sr_info
= data
;
820 pr_warning("%s: omap_sr struct not found\n", __func__
);
826 pr_warning("%s: Invalid argument %lld\n", __func__
, val
);
830 /* control enable/disable only if there is a delta in value */
831 if (sr_info
->autocomp_active
!= val
) {
833 sr_stop_vddautocomp(sr_info
);
835 sr_start_vddautocomp(sr_info
);
841 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops
, omap_sr_autocomp_show
,
842 omap_sr_autocomp_store
, "%llu\n");
844 static int __init
omap_sr_probe(struct platform_device
*pdev
)
846 struct omap_sr
*sr_info
;
847 struct omap_sr_data
*pdata
= pdev
->dev
.platform_data
;
848 struct resource
*mem
, *irq
;
849 struct dentry
*nvalue_dir
;
852 sr_info
= kzalloc(sizeof(struct omap_sr
), GFP_KERNEL
);
854 dev_err(&pdev
->dev
, "%s: unable to allocate sr_info\n",
859 platform_set_drvdata(pdev
, sr_info
);
862 dev_err(&pdev
->dev
, "%s: platform data missing\n", __func__
);
864 goto err_free_devinfo
;
867 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
869 dev_err(&pdev
->dev
, "%s: no mem resource\n", __func__
);
871 goto err_free_devinfo
;
874 mem
= request_mem_region(mem
->start
, resource_size(mem
),
875 dev_name(&pdev
->dev
));
877 dev_err(&pdev
->dev
, "%s: no mem region\n", __func__
);
879 goto err_free_devinfo
;
882 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
884 pm_runtime_enable(&pdev
->dev
);
885 pm_runtime_irq_safe(&pdev
->dev
);
887 sr_info
->name
= kasprintf(GFP_KERNEL
, "%s", pdata
->name
);
888 if (!sr_info
->name
) {
889 dev_err(&pdev
->dev
, "%s: Unable to alloc SR instance name\n",
892 goto err_release_region
;
895 sr_info
->pdev
= pdev
;
896 sr_info
->srid
= pdev
->id
;
897 sr_info
->voltdm
= pdata
->voltdm
;
898 sr_info
->nvalue_table
= pdata
->nvalue_table
;
899 sr_info
->nvalue_count
= pdata
->nvalue_count
;
900 sr_info
->senn_mod
= pdata
->senn_mod
;
901 sr_info
->senp_mod
= pdata
->senp_mod
;
902 sr_info
->err_weight
= pdata
->err_weight
;
903 sr_info
->err_maxlimit
= pdata
->err_maxlimit
;
904 sr_info
->accum_data
= pdata
->accum_data
;
905 sr_info
->senn_avgweight
= pdata
->senn_avgweight
;
906 sr_info
->senp_avgweight
= pdata
->senp_avgweight
;
907 sr_info
->autocomp_active
= false;
908 sr_info
->ip_type
= pdata
->ip_type
;
910 sr_info
->base
= ioremap(mem
->start
, resource_size(mem
));
911 if (!sr_info
->base
) {
912 dev_err(&pdev
->dev
, "%s: ioremap fail\n", __func__
);
918 sr_info
->irq
= irq
->start
;
920 sr_set_clk_length(sr_info
);
922 list_add(&sr_info
->node
, &sr_list
);
925 * Call into late init to do intializations that require
926 * both sr driver and sr class driver to be initiallized.
929 ret
= sr_late_init(sr_info
);
931 pr_warning("%s: Error in SR late init\n", __func__
);
936 dev_info(&pdev
->dev
, "%s: SmartReflex driver initialized\n", __func__
);
938 sr_dbg_dir
= debugfs_create_dir("smartreflex", NULL
);
939 if (IS_ERR_OR_NULL(sr_dbg_dir
)) {
940 ret
= PTR_ERR(sr_dbg_dir
);
941 pr_err("%s:sr debugfs dir creation failed(%d)\n",
947 sr_info
->dbg_dir
= debugfs_create_dir(sr_info
->name
, sr_dbg_dir
);
948 if (IS_ERR_OR_NULL(sr_info
->dbg_dir
)) {
949 dev_err(&pdev
->dev
, "%s: Unable to create debugfs directory\n",
951 ret
= PTR_ERR(sr_info
->dbg_dir
);
955 (void) debugfs_create_file("autocomp", S_IRUGO
| S_IWUSR
,
956 sr_info
->dbg_dir
, (void *)sr_info
, &pm_sr_fops
);
957 (void) debugfs_create_x32("errweight", S_IRUGO
, sr_info
->dbg_dir
,
958 &sr_info
->err_weight
);
959 (void) debugfs_create_x32("errmaxlimit", S_IRUGO
, sr_info
->dbg_dir
,
960 &sr_info
->err_maxlimit
);
962 nvalue_dir
= debugfs_create_dir("nvalue", sr_info
->dbg_dir
);
963 if (IS_ERR_OR_NULL(nvalue_dir
)) {
964 dev_err(&pdev
->dev
, "%s: Unable to create debugfs directory"
965 "for n-values\n", __func__
);
966 ret
= PTR_ERR(nvalue_dir
);
970 if (sr_info
->nvalue_count
== 0 || !sr_info
->nvalue_table
) {
971 dev_warn(&pdev
->dev
, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
972 __func__
, sr_info
->name
);
978 for (i
= 0; i
< sr_info
->nvalue_count
; i
++) {
979 char name
[NVALUE_NAME_LEN
+ 1];
981 snprintf(name
, sizeof(name
), "volt_%lu",
982 sr_info
->nvalue_table
[i
].volt_nominal
);
983 (void) debugfs_create_x32(name
, S_IRUGO
| S_IWUSR
, nvalue_dir
,
984 &(sr_info
->nvalue_table
[i
].nvalue
));
985 snprintf(name
, sizeof(name
), "errminlimit_%lu",
986 sr_info
->nvalue_table
[i
].volt_nominal
);
987 (void) debugfs_create_x32(name
, S_IRUGO
| S_IWUSR
, nvalue_dir
,
988 &(sr_info
->nvalue_table
[i
].errminlimit
));
995 debugfs_remove_recursive(sr_info
->dbg_dir
);
997 list_del(&sr_info
->node
);
998 iounmap(sr_info
->base
);
1000 kfree(sr_info
->name
);
1002 release_mem_region(mem
->start
, resource_size(mem
));
1009 static int omap_sr_remove(struct platform_device
*pdev
)
1011 struct omap_sr_data
*pdata
= pdev
->dev
.platform_data
;
1012 struct omap_sr
*sr_info
;
1013 struct resource
*mem
;
1016 dev_err(&pdev
->dev
, "%s: platform data missing\n", __func__
);
1020 sr_info
= _sr_lookup(pdata
->voltdm
);
1021 if (IS_ERR(sr_info
)) {
1022 dev_warn(&pdev
->dev
, "%s: omap_sr struct not found\n",
1024 return PTR_ERR(sr_info
);
1027 if (sr_info
->autocomp_active
)
1028 sr_stop_vddautocomp(sr_info
);
1029 if (sr_info
->dbg_dir
)
1030 debugfs_remove_recursive(sr_info
->dbg_dir
);
1032 list_del(&sr_info
->node
);
1033 iounmap(sr_info
->base
);
1034 kfree(sr_info
->name
);
1036 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1037 release_mem_region(mem
->start
, resource_size(mem
));
1042 static void omap_sr_shutdown(struct platform_device
*pdev
)
1044 struct omap_sr_data
*pdata
= pdev
->dev
.platform_data
;
1045 struct omap_sr
*sr_info
;
1048 dev_err(&pdev
->dev
, "%s: platform data missing\n", __func__
);
1052 sr_info
= _sr_lookup(pdata
->voltdm
);
1053 if (IS_ERR(sr_info
)) {
1054 dev_warn(&pdev
->dev
, "%s: omap_sr struct not found\n",
1059 if (sr_info
->autocomp_active
)
1060 sr_stop_vddautocomp(sr_info
);
1065 static struct platform_driver smartreflex_driver
= {
1066 .remove
= omap_sr_remove
,
1067 .shutdown
= omap_sr_shutdown
,
1069 .name
= "smartreflex",
1073 static int __init
sr_init(void)
1078 * sr_init is a late init. If by then a pmic specific API is not
1079 * registered either there is no need for anything to be done on
1080 * the PMIC side or somebody has forgotten to register a PMIC
1081 * handler. Warn for the second condition.
1083 if (sr_pmic_data
&& sr_pmic_data
->sr_pmic_init
)
1084 sr_pmic_data
->sr_pmic_init();
1086 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__
);
1088 ret
= platform_driver_probe(&smartreflex_driver
, omap_sr_probe
);
1090 pr_err("%s: platform driver register failed for SR\n",
1097 late_initcall(sr_init
);
1099 static void __exit
sr_exit(void)
1101 platform_driver_unregister(&smartreflex_driver
);
1103 module_exit(sr_exit
);
1105 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1106 MODULE_LICENSE("GPL");
1107 MODULE_ALIAS("platform:" DRIVER_NAME
);
1108 MODULE_AUTHOR("Texas Instruments Inc");