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 if (cpu_is_omap34xx())
137 sys_ck = clk_get(NULL, "sys_ck");
139 sys_ck = clk_get(NULL, "sys_clkin_ck");
141 if (IS_ERR(sys_ck)) {
142 dev_err(&sr->pdev->dev, "%s: unable to get sys clk\n",
147 sys_clk_speed = clk_get_rate(sys_ck);
150 switch (sys_clk_speed) {
152 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
155 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
158 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
161 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
164 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
167 dev_err(&sr->pdev->dev, "%s: Invalid sysclk value: %d\n",
168 __func__, sys_clk_speed);
173 static void sr_set_regfields(struct omap_sr *sr)
176 * For time being these values are defined in smartreflex.h
177 * and populated during init. May be they can be moved to board
178 * file or pmic specific data structure. In that case these structure
179 * fields will have to be populated using the pdata or pmic structure.
181 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
182 sr->err_weight = OMAP3430_SR_ERRWEIGHT;
183 sr->err_maxlimit = OMAP3430_SR_ERRMAXLIMIT;
184 sr->accum_data = OMAP3430_SR_ACCUMDATA;
185 if (!(strcmp(sr->name, "smartreflex_mpu_iva"))) {
186 sr->senn_avgweight = OMAP3430_SR1_SENNAVGWEIGHT;
187 sr->senp_avgweight = OMAP3430_SR1_SENPAVGWEIGHT;
189 sr->senn_avgweight = OMAP3430_SR2_SENNAVGWEIGHT;
190 sr->senp_avgweight = OMAP3430_SR2_SENPAVGWEIGHT;
195 static void sr_start_vddautocomp(struct omap_sr *sr)
197 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
198 dev_warn(&sr->pdev->dev,
199 "%s: smartreflex class driver not registered\n",
204 if (!sr_class->enable(sr))
205 sr->autocomp_active = true;
208 static void sr_stop_vddautocomp(struct omap_sr *sr)
210 if (!sr_class || !(sr_class->disable)) {
211 dev_warn(&sr->pdev->dev,
212 "%s: smartreflex class driver not registered\n",
217 if (sr->autocomp_active) {
218 sr_class->disable(sr, 1);
219 sr->autocomp_active = false;
224 * This function handles the intializations which have to be done
225 * only when both sr device and class driver regiter has
226 * completed. This will be attempted to be called from both sr class
227 * driver register and sr device intializtion API's. Only one call
228 * will ultimately succeed.
230 * Currently this function registers interrupt handler for a particular SR
231 * if smartreflex class driver is already registered and has
232 * requested for interrupts and the SR interrupt line in present.
234 static int sr_late_init(struct omap_sr *sr_info)
236 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
237 struct resource *mem;
240 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
241 ret = request_irq(sr_info->irq, sr_interrupt,
242 0, sr_info->name, sr_info);
245 disable_irq(sr_info->irq);
248 if (pdata && pdata->enable_on_init)
249 sr_start_vddautocomp(sr_info);
254 iounmap(sr_info->base);
255 mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
256 release_mem_region(mem->start, resource_size(mem));
257 list_del(&sr_info->node);
258 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
259 "interrupt handler. Smartreflex will"
260 "not function as desired\n", __func__);
266 static void sr_v1_disable(struct omap_sr *sr)
269 int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
270 ERRCONFIG_MCUBOUNDINTST;
272 /* Enable MCUDisableAcknowledge interrupt */
273 sr_modify_reg(sr, ERRCONFIG_V1,
274 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
276 /* SRCONFIG - disable SR */
277 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
279 /* Disable all other SR interrupts and clear the status as needed */
280 if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
281 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
282 sr_modify_reg(sr, ERRCONFIG_V1,
283 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
284 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
288 * Wait for SR to be disabled.
289 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
291 sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
292 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
295 if (timeout >= SR_DISABLE_TIMEOUT)
296 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
299 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
300 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
301 ERRCONFIG_MCUDISACKINTST);
304 static void sr_v2_disable(struct omap_sr *sr)
308 /* Enable MCUDisableAcknowledge interrupt */
309 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
311 /* SRCONFIG - disable SR */
312 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
315 * Disable all other SR interrupts and clear the status
316 * write to status register ONLY on need basis - only if status
319 if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
320 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
321 ERRCONFIG_VPBOUNDINTST_V2);
323 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
325 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
326 IRQENABLE_MCUVALIDINT |
327 IRQENABLE_MCUBOUNDSINT));
328 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
329 IRQSTATUS_MCVALIDINT |
330 IRQSTATUS_MCBOUNDSINT));
333 * Wait for SR to be disabled.
334 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
336 sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
337 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
340 if (timeout >= SR_DISABLE_TIMEOUT)
341 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
344 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
345 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
346 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
349 static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
350 struct omap_sr *sr, u32 efuse_offs)
354 if (!sr->nvalue_table) {
355 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
360 for (i = 0; i < sr->nvalue_count; i++) {
361 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
362 return &sr->nvalue_table[i];
368 /* Public Functions */
371 * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
372 * error generator module.
373 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
375 * This API is to be called from the smartreflex class driver to
376 * configure the error generator module inside the smartreflex module.
377 * SR settings if using the ERROR module inside Smartreflex.
378 * SR CLASS 3 by default uses only the ERROR module where as
379 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
380 * module. Returns 0 on success and error value in case of failure.
382 int sr_configure_errgen(struct voltagedomain *voltdm)
384 u32 sr_config, sr_errconfig, errconfig_offs;
385 u32 vpboundint_en, vpboundint_st;
386 u32 senp_en = 0, senn_en = 0;
387 u8 senp_shift, senn_shift;
388 struct omap_sr *sr = _sr_lookup(voltdm);
391 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
396 sr_set_clk_length(sr);
398 senp_en = sr->senp_mod;
399 senn_en = sr->senn_mod;
401 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
402 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
404 switch (sr->ip_type) {
406 sr_config |= SRCONFIG_DELAYCTRL;
407 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
408 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
409 errconfig_offs = ERRCONFIG_V1;
410 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
411 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
414 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
415 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
416 errconfig_offs = ERRCONFIG_V2;
417 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
418 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
421 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
422 "module without specifying the ip\n", __func__);
426 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
427 sr_write_reg(sr, SRCONFIG, sr_config);
428 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
429 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
430 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
431 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
432 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
435 /* Enabling the interrupts if the ERROR module is used */
436 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
443 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
444 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
446 * This API is to be called from the smartreflex class driver to
447 * disable the error generator module inside the smartreflex module.
449 * Returns 0 on success and error value in case of failure.
451 int sr_disable_errgen(struct voltagedomain *voltdm)
454 u32 vpboundint_en, vpboundint_st;
455 struct omap_sr *sr = _sr_lookup(voltdm);
458 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
462 switch (sr->ip_type) {
464 errconfig_offs = ERRCONFIG_V1;
465 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
466 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
469 errconfig_offs = ERRCONFIG_V2;
470 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
471 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
474 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
475 "module without specifying the ip\n", __func__);
479 /* Disable the interrupts of ERROR module */
480 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
482 /* Disable the Sensor and errorgen */
483 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
489 * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
491 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
493 * This API is to be called from the smartreflex class driver to
494 * configure the minmaxavg module inside the smartreflex module.
495 * SR settings if using the ERROR module inside Smartreflex.
496 * SR CLASS 3 by default uses only the ERROR module where as
497 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
498 * module. Returns 0 on success and error value in case of failure.
500 int sr_configure_minmax(struct voltagedomain *voltdm)
502 u32 sr_config, sr_avgwt;
503 u32 senp_en = 0, senn_en = 0;
504 u8 senp_shift, senn_shift;
505 struct omap_sr *sr = _sr_lookup(voltdm);
508 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
513 sr_set_clk_length(sr);
515 senp_en = sr->senp_mod;
516 senn_en = sr->senn_mod;
518 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
520 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
522 switch (sr->ip_type) {
524 sr_config |= SRCONFIG_DELAYCTRL;
525 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
526 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
529 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
530 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
533 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
534 "module without specifying the ip\n", __func__);
538 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
539 sr_write_reg(sr, SRCONFIG, sr_config);
540 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
541 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
542 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
545 * Enabling the interrupts if MINMAXAVG module is used.
546 * TODO: check if all the interrupts are mandatory
548 switch (sr->ip_type) {
550 sr_modify_reg(sr, ERRCONFIG_V1,
551 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
552 ERRCONFIG_MCUBOUNDINTEN),
553 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
554 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
555 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
558 sr_write_reg(sr, IRQSTATUS,
559 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
560 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
561 sr_write_reg(sr, IRQENABLE_SET,
562 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
563 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
566 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
567 "module without specifying the ip\n", __func__);
575 * sr_enable() - Enables the smartreflex module.
576 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
577 * @volt: The voltage at which the Voltage domain associated with
578 * the smartreflex module is operating at.
579 * This is required only to program the correct Ntarget value.
581 * This API is to be called from the smartreflex class driver to
582 * enable a smartreflex module. Returns 0 on success. Returns error
583 * value if the voltage passed is wrong or if ntarget value is wrong.
585 int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
587 struct omap_volt_data *volt_data;
588 struct omap_sr *sr = _sr_lookup(voltdm);
589 struct omap_sr_nvalue_table *nvalue_row;
593 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
597 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
599 if (IS_ERR(volt_data)) {
600 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
601 "for nominal voltage %ld\n", __func__, volt);
602 return PTR_ERR(volt_data);
605 nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
608 dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
613 /* errminlimit is opp dependent and hence linked to voltage */
614 sr->err_minlimit = nvalue_row->errminlimit;
616 pm_runtime_get_sync(&sr->pdev->dev);
618 /* Check if SR is already enabled. If yes do nothing */
619 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
623 ret = sr_class->configure(sr);
627 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
629 /* SRCONFIG - enable SR */
630 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
635 * sr_disable() - Disables the smartreflex module.
636 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
638 * This API is to be called from the smartreflex class driver to
639 * disable a smartreflex module.
641 void sr_disable(struct voltagedomain *voltdm)
643 struct omap_sr *sr = _sr_lookup(voltdm);
646 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
650 /* Check if SR clocks are already disabled. If yes do nothing */
651 if (pm_runtime_suspended(&sr->pdev->dev))
655 * Disable SR if only it is indeed enabled. Else just
656 * disable the clocks.
658 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
659 switch (sr->ip_type) {
667 dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
672 pm_runtime_put_sync_suspend(&sr->pdev->dev);
676 * sr_register_class() - API to register a smartreflex class parameters.
677 * @class_data: The structure containing various sr class specific data.
679 * This API is to be called by the smartreflex class driver to register itself
680 * with the smartreflex driver during init. Returns 0 on success else the
683 int sr_register_class(struct omap_sr_class_data *class_data)
685 struct omap_sr *sr_info;
688 pr_warning("%s:, Smartreflex class data passed is NULL\n",
694 pr_warning("%s: Smartreflex class driver already registered\n",
699 sr_class = class_data;
702 * Call into late init to do intializations that require
703 * both sr driver and sr class driver to be initiallized.
705 list_for_each_entry(sr_info, &sr_list, node)
706 sr_late_init(sr_info);
712 * omap_sr_enable() - API to enable SR clocks and to call into the
713 * registered smartreflex class enable API.
714 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
716 * This API is to be called from the kernel in order to enable
717 * a particular smartreflex module. This API will do the initial
718 * configurations to turn on the smartreflex module and in turn call
719 * into the registered smartreflex class enable API.
721 void omap_sr_enable(struct voltagedomain *voltdm)
723 struct omap_sr *sr = _sr_lookup(voltdm);
726 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
730 if (!sr->autocomp_active)
733 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
734 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
735 "registered\n", __func__);
739 sr_class->enable(sr);
743 * omap_sr_disable() - API to disable SR without resetting the voltage
745 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
747 * This API is to be called from the kernel in order to disable
748 * a particular smartreflex module. This API will in turn call
749 * into the registered smartreflex class disable API. This API will tell
750 * the smartreflex class disable not to reset the VP voltage after
751 * disabling smartreflex.
753 void omap_sr_disable(struct voltagedomain *voltdm)
755 struct omap_sr *sr = _sr_lookup(voltdm);
758 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
762 if (!sr->autocomp_active)
765 if (!sr_class || !(sr_class->disable)) {
766 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
767 "registered\n", __func__);
771 sr_class->disable(sr, 0);
775 * omap_sr_disable_reset_volt() - API to disable SR and reset the
776 * voltage processor voltage
777 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
779 * This API is to be called from the kernel in order to disable
780 * a particular smartreflex module. This API will in turn call
781 * into the registered smartreflex class disable API. This API will tell
782 * the smartreflex class disable to reset the VP voltage after
783 * disabling smartreflex.
785 void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
787 struct omap_sr *sr = _sr_lookup(voltdm);
790 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
794 if (!sr->autocomp_active)
797 if (!sr_class || !(sr_class->disable)) {
798 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
799 "registered\n", __func__);
803 sr_class->disable(sr, 1);
807 * omap_sr_register_pmic() - API to register pmic specific info.
808 * @pmic_data: The structure containing pmic specific data.
810 * This API is to be called from the PMIC specific code to register with
811 * smartreflex driver pmic specific info. Currently the only info required
812 * is the smartreflex init on the PMIC side.
814 void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
817 pr_warning("%s: Trying to register NULL PMIC data structure"
818 "with smartreflex\n", __func__);
822 sr_pmic_data = pmic_data;
825 /* PM Debug FS entries to enable and disable smartreflex. */
826 static int omap_sr_autocomp_show(void *data, u64 *val)
828 struct omap_sr *sr_info = data;
831 pr_warning("%s: omap_sr struct not found\n", __func__);
835 *val = sr_info->autocomp_active;
840 static int omap_sr_autocomp_store(void *data, u64 val)
842 struct omap_sr *sr_info = data;
845 pr_warning("%s: omap_sr struct not found\n", __func__);
851 pr_warning("%s: Invalid argument %lld\n", __func__, val);
855 /* control enable/disable only if there is a delta in value */
856 if (sr_info->autocomp_active != val) {
858 sr_stop_vddautocomp(sr_info);
860 sr_start_vddautocomp(sr_info);
866 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
867 omap_sr_autocomp_store, "%llu\n");
869 static int __init omap_sr_probe(struct platform_device *pdev)
871 struct omap_sr *sr_info;
872 struct omap_sr_data *pdata = pdev->dev.platform_data;
873 struct resource *mem, *irq;
874 struct dentry *nvalue_dir;
877 sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
879 dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
884 platform_set_drvdata(pdev, sr_info);
887 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
889 goto err_free_devinfo;
892 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
894 dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
896 goto err_free_devinfo;
899 mem = request_mem_region(mem->start, resource_size(mem),
900 dev_name(&pdev->dev));
902 dev_err(&pdev->dev, "%s: no mem region\n", __func__);
904 goto err_free_devinfo;
907 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
909 pm_runtime_enable(&pdev->dev);
910 pm_runtime_irq_safe(&pdev->dev);
912 sr_info->name = kasprintf(GFP_KERNEL, "%s", pdata->name);
913 if (!sr_info->name) {
914 dev_err(&pdev->dev, "%s: Unable to alloc SR instance name\n",
917 goto err_release_region;
920 sr_info->pdev = pdev;
921 sr_info->srid = pdev->id;
922 sr_info->voltdm = pdata->voltdm;
923 sr_info->nvalue_table = pdata->nvalue_table;
924 sr_info->nvalue_count = pdata->nvalue_count;
925 sr_info->senn_mod = pdata->senn_mod;
926 sr_info->senp_mod = pdata->senp_mod;
927 sr_info->autocomp_active = false;
928 sr_info->ip_type = pdata->ip_type;
929 sr_info->base = ioremap(mem->start, resource_size(mem));
930 if (!sr_info->base) {
931 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
933 goto err_release_region;
937 sr_info->irq = irq->start;
939 sr_set_clk_length(sr_info);
940 sr_set_regfields(sr_info);
942 list_add(&sr_info->node, &sr_list);
945 * Call into late init to do intializations that require
946 * both sr driver and sr class driver to be initiallized.
949 ret = sr_late_init(sr_info);
951 pr_warning("%s: Error in SR late init\n", __func__);
956 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
958 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
959 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
960 ret = PTR_ERR(sr_dbg_dir);
961 pr_err("%s:sr debugfs dir creation failed(%d)\n",
967 sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
968 if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
969 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
971 ret = PTR_ERR(sr_info->dbg_dir);
975 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
976 sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
977 (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
978 &sr_info->err_weight);
979 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
980 &sr_info->err_maxlimit);
982 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
983 if (IS_ERR_OR_NULL(nvalue_dir)) {
984 dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
985 "for n-values\n", __func__);
986 ret = PTR_ERR(nvalue_dir);
990 if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
991 dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
992 __func__, sr_info->name);
998 for (i = 0; i < sr_info->nvalue_count; i++) {
999 char name[NVALUE_NAME_LEN + 1];
1001 snprintf(name, sizeof(name), "volt_%lu",
1002 sr_info->nvalue_table[i].volt_nominal);
1003 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
1004 &(sr_info->nvalue_table[i].nvalue));
1005 snprintf(name, sizeof(name), "errminlimit_%lu",
1006 sr_info->nvalue_table[i].volt_nominal);
1007 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
1008 &(sr_info->nvalue_table[i].errminlimit));
1015 debugfs_remove_recursive(sr_info->dbg_dir);
1017 kfree(sr_info->name);
1019 list_del(&sr_info->node);
1020 iounmap(sr_info->base);
1022 release_mem_region(mem->start, resource_size(mem));
1029 static int __devexit omap_sr_remove(struct platform_device *pdev)
1031 struct omap_sr_data *pdata = pdev->dev.platform_data;
1032 struct omap_sr *sr_info;
1033 struct resource *mem;
1036 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1040 sr_info = _sr_lookup(pdata->voltdm);
1041 if (IS_ERR(sr_info)) {
1042 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1044 return PTR_ERR(sr_info);
1047 if (sr_info->autocomp_active)
1048 sr_stop_vddautocomp(sr_info);
1049 if (sr_info->dbg_dir)
1050 debugfs_remove_recursive(sr_info->dbg_dir);
1052 list_del(&sr_info->node);
1053 iounmap(sr_info->base);
1054 kfree(sr_info->name);
1056 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1057 release_mem_region(mem->start, resource_size(mem));
1062 static void __devexit omap_sr_shutdown(struct platform_device *pdev)
1064 struct omap_sr_data *pdata = pdev->dev.platform_data;
1065 struct omap_sr *sr_info;
1068 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1072 sr_info = _sr_lookup(pdata->voltdm);
1073 if (IS_ERR(sr_info)) {
1074 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1079 if (sr_info->autocomp_active)
1080 sr_stop_vddautocomp(sr_info);
1085 static struct platform_driver smartreflex_driver = {
1086 .remove = __devexit_p(omap_sr_remove),
1087 .shutdown = __devexit_p(omap_sr_shutdown),
1089 .name = "smartreflex",
1093 static int __init sr_init(void)
1098 * sr_init is a late init. If by then a pmic specific API is not
1099 * registered either there is no need for anything to be done on
1100 * the PMIC side or somebody has forgotten to register a PMIC
1101 * handler. Warn for the second condition.
1103 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1104 sr_pmic_data->sr_pmic_init();
1106 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
1108 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1110 pr_err("%s: platform driver register failed for SR\n",
1117 late_initcall(sr_init);
1119 static void __exit sr_exit(void)
1121 platform_driver_unregister(&smartreflex_driver);
1123 module_exit(sr_exit);
1125 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1126 MODULE_LICENSE("GPL");
1127 MODULE_ALIAS("platform:" DRIVER_NAME);
1128 MODULE_AUTHOR("Texas Instruments Inc");