2 * OMAP SmartReflex Voltage Control
4 * Author: Thara Gopinath <thara@ti.com>
6 * Copyright (C) 2010 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>
29 #include <plat/common.h>
32 #include "smartreflex.h"
34 #define SMARTREFLEX_NAME_LEN 16
35 #define NVALUE_NAME_LEN 40
36 #define SR_DISABLE_TIMEOUT 200
54 struct platform_device *pdev;
55 struct list_head node;
56 struct omap_sr_nvalue_table *nvalue_table;
57 struct voltagedomain *voltdm;
58 struct dentry *dbg_dir;
61 /* sr_list contains all the instances of smartreflex module */
62 static LIST_HEAD(sr_list);
64 static struct omap_sr_class_data *sr_class;
65 static struct omap_sr_pmic_data *sr_pmic_data;
66 static struct dentry *sr_dbg_dir;
68 static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
70 __raw_writel(value, (sr->base + offset));
73 static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
77 u32 errconfig_offs = 0, errconfig_mask = 0;
79 reg_val = __raw_readl(sr->base + offset);
83 * Smartreflex error config register is special as it contains
84 * certain status bits which if written a 1 into means a clear
85 * of those bits. So in order to make sure no accidental write of
86 * 1 happens to those status bits, do a clear of them in the read
87 * value. This mean this API doesn't rewrite values in these bits
88 * if they are currently set, but does allow the caller to write
91 if (sr->ip_type == SR_TYPE_V1) {
92 errconfig_offs = ERRCONFIG_V1;
93 errconfig_mask = ERRCONFIG_STATUS_V1_MASK;
94 } else if (sr->ip_type == SR_TYPE_V2) {
95 errconfig_offs = ERRCONFIG_V2;
96 errconfig_mask = ERRCONFIG_VPBOUNDINTST_V2;
99 if (offset == errconfig_offs)
100 reg_val &= ~errconfig_mask;
104 __raw_writel(reg_val, (sr->base + offset));
107 static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
109 return __raw_readl(sr->base + offset);
112 static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
114 struct omap_sr *sr_info;
117 pr_err("%s: Null voltage domain passed!\n", __func__);
118 return ERR_PTR(-EINVAL);
121 list_for_each_entry(sr_info, &sr_list, node) {
122 if (voltdm == sr_info->voltdm)
126 return ERR_PTR(-ENODATA);
129 static irqreturn_t sr_interrupt(int irq, void *data)
131 struct omap_sr *sr_info = (struct omap_sr *)data;
134 if (sr_info->ip_type == SR_TYPE_V1) {
135 /* Read the status bits */
136 status = sr_read_reg(sr_info, ERRCONFIG_V1);
138 /* Clear them by writing back */
139 sr_write_reg(sr_info, ERRCONFIG_V1, status);
140 } else if (sr_info->ip_type == SR_TYPE_V2) {
141 /* Read the status bits */
142 status = sr_read_reg(sr_info, IRQSTATUS);
144 /* Clear them by writing back */
145 sr_write_reg(sr_info, IRQSTATUS, status);
148 if (sr_class->notify)
149 sr_class->notify(sr_info->voltdm, status);
154 static void sr_set_clk_length(struct omap_sr *sr)
159 if (cpu_is_omap34xx())
160 sys_ck = clk_get(NULL, "sys_ck");
162 sys_ck = clk_get(NULL, "sys_clkin_ck");
164 if (IS_ERR(sys_ck)) {
165 dev_err(&sr->pdev->dev, "%s: unable to get sys clk\n",
169 sys_clk_speed = clk_get_rate(sys_ck);
172 switch (sys_clk_speed) {
174 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
177 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
180 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
183 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
186 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
189 dev_err(&sr->pdev->dev, "%s: Invalid sysclk value: %d\n",
190 __func__, sys_clk_speed);
195 static void sr_set_regfields(struct omap_sr *sr)
198 * For time being these values are defined in smartreflex.h
199 * and populated during init. May be they can be moved to board
200 * file or pmic specific data structure. In that case these structure
201 * fields will have to be populated using the pdata or pmic structure.
203 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
204 sr->err_weight = OMAP3430_SR_ERRWEIGHT;
205 sr->err_maxlimit = OMAP3430_SR_ERRMAXLIMIT;
206 sr->accum_data = OMAP3430_SR_ACCUMDATA;
207 if (!(strcmp(sr->voltdm->name, "mpu"))) {
208 sr->senn_avgweight = OMAP3430_SR1_SENNAVGWEIGHT;
209 sr->senp_avgweight = OMAP3430_SR1_SENPAVGWEIGHT;
211 sr->senn_avgweight = OMAP3430_SR2_SENNAVGWEIGHT;
212 sr->senp_avgweight = OMAP3430_SR2_SENPAVGWEIGHT;
217 static void sr_start_vddautocomp(struct omap_sr *sr)
219 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
220 dev_warn(&sr->pdev->dev,
221 "%s: smartreflex class driver not registered\n",
226 if (!sr_class->enable(sr->voltdm))
227 sr->autocomp_active = true;
230 static void sr_stop_vddautocomp(struct omap_sr *sr)
232 if (!sr_class || !(sr_class->disable)) {
233 dev_warn(&sr->pdev->dev,
234 "%s: smartreflex class driver not registered\n",
239 if (sr->autocomp_active) {
240 sr_class->disable(sr->voltdm, 1);
241 sr->autocomp_active = false;
246 * This function handles the intializations which have to be done
247 * only when both sr device and class driver regiter has
248 * completed. This will be attempted to be called from both sr class
249 * driver register and sr device intializtion API's. Only one call
250 * will ultimately succeed.
252 * Currently this function registers interrupt handler for a particular SR
253 * if smartreflex class driver is already registered and has
254 * requested for interrupts and the SR interrupt line in present.
256 static int sr_late_init(struct omap_sr *sr_info)
259 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
260 struct resource *mem;
263 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
264 name = kasprintf(GFP_KERNEL, "sr_%s", sr_info->voltdm->name);
269 ret = request_irq(sr_info->irq, sr_interrupt,
270 0, name, (void *)sr_info);
273 disable_irq(sr_info->irq);
276 if (pdata && pdata->enable_on_init)
277 sr_start_vddautocomp(sr_info);
282 iounmap(sr_info->base);
283 mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
284 release_mem_region(mem->start, resource_size(mem));
285 list_del(&sr_info->node);
286 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
287 "interrupt handler. Smartreflex will"
288 "not function as desired\n", __func__);
294 static void sr_v1_disable(struct omap_sr *sr)
298 /* Enable MCUDisableAcknowledge interrupt */
299 sr_modify_reg(sr, ERRCONFIG_V1,
300 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
302 /* SRCONFIG - disable SR */
303 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
305 /* Disable all other SR interrupts and clear the status */
306 sr_modify_reg(sr, ERRCONFIG_V1,
307 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
308 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
309 (ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
310 ERRCONFIG_MCUBOUNDINTST |
311 ERRCONFIG_VPBOUNDINTST_V1));
314 * Wait for SR to be disabled.
315 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
317 omap_test_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
318 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
321 if (timeout >= SR_DISABLE_TIMEOUT)
322 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
325 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
326 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
327 ERRCONFIG_MCUDISACKINTST);
330 static void sr_v2_disable(struct omap_sr *sr)
334 /* Enable MCUDisableAcknowledge interrupt */
335 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
337 /* SRCONFIG - disable SR */
338 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
340 /* Disable all other SR interrupts and clear the status */
341 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
342 ERRCONFIG_VPBOUNDINTST_V2);
343 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
344 IRQENABLE_MCUVALIDINT |
345 IRQENABLE_MCUBOUNDSINT));
346 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
347 IRQSTATUS_MCVALIDINT |
348 IRQSTATUS_MCBOUNDSINT));
351 * Wait for SR to be disabled.
352 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
354 omap_test_timeout((sr_read_reg(sr, IRQSTATUS) &
355 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
358 if (timeout >= SR_DISABLE_TIMEOUT)
359 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
362 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
363 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
364 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
367 static u32 sr_retrieve_nvalue(struct omap_sr *sr, u32 efuse_offs)
371 if (!sr->nvalue_table) {
372 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
377 for (i = 0; i < sr->nvalue_count; i++) {
378 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
379 return sr->nvalue_table[i].nvalue;
385 /* Public Functions */
388 * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
389 * error generator module.
390 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
392 * This API is to be called from the smartreflex class driver to
393 * configure the error generator module inside the smartreflex module.
394 * SR settings if using the ERROR module inside Smartreflex.
395 * SR CLASS 3 by default uses only the ERROR module where as
396 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
397 * module. Returns 0 on success and error value in case of failure.
399 int sr_configure_errgen(struct voltagedomain *voltdm)
401 u32 sr_config, sr_errconfig, errconfig_offs, vpboundint_en;
402 u32 vpboundint_st, senp_en = 0, senn_en = 0;
403 u8 senp_shift, senn_shift;
404 struct omap_sr *sr = _sr_lookup(voltdm);
407 pr_warning("%s: omap_sr struct for sr_%s not found\n",
408 __func__, voltdm->name);
413 sr_set_clk_length(sr);
415 senp_en = sr->senp_mod;
416 senn_en = sr->senn_mod;
418 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
419 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
421 if (sr->ip_type == SR_TYPE_V1) {
422 sr_config |= SRCONFIG_DELAYCTRL;
423 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
424 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
425 errconfig_offs = ERRCONFIG_V1;
426 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
427 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
428 } else if (sr->ip_type == SR_TYPE_V2) {
429 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
430 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
431 errconfig_offs = ERRCONFIG_V2;
432 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
433 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
435 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
436 "module without specifying the ip\n", __func__);
440 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
441 sr_write_reg(sr, SRCONFIG, sr_config);
442 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
443 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
444 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
445 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
446 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
449 /* Enabling the interrupts if the ERROR module is used */
450 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
457 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
458 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
460 * This API is to be called from the smartreflex class driver to
461 * disable the error generator module inside the smartreflex module.
463 * Returns 0 on success and error value in case of failure.
465 int sr_disable_errgen(struct voltagedomain *voltdm)
467 u32 errconfig_offs, vpboundint_en;
469 struct omap_sr *sr = _sr_lookup(voltdm);
472 pr_warning("%s: omap_sr struct for sr_%s not found\n",
473 __func__, voltdm->name);
477 if (sr->ip_type == SR_TYPE_V1) {
478 errconfig_offs = ERRCONFIG_V1;
479 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
480 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
481 } else if (sr->ip_type == SR_TYPE_V2) {
482 errconfig_offs = ERRCONFIG_V2;
483 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
484 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
486 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
487 "module without specifying the ip\n", __func__);
491 /* Disable the interrupts of ERROR module */
492 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
494 /* Disable the Sensor and errorgen */
495 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
501 * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
503 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
505 * This API is to be called from the smartreflex class driver to
506 * configure the minmaxavg module inside the smartreflex module.
507 * SR settings if using the ERROR module inside Smartreflex.
508 * SR CLASS 3 by default uses only the ERROR module where as
509 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
510 * module. Returns 0 on success and error value in case of failure.
512 int sr_configure_minmax(struct voltagedomain *voltdm)
514 u32 sr_config, sr_avgwt;
515 u32 senp_en = 0, senn_en = 0;
516 u8 senp_shift, senn_shift;
517 struct omap_sr *sr = _sr_lookup(voltdm);
520 pr_warning("%s: omap_sr struct for sr_%s not found\n",
521 __func__, voltdm->name);
526 sr_set_clk_length(sr);
528 senp_en = sr->senp_mod;
529 senn_en = sr->senn_mod;
531 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
533 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
535 if (sr->ip_type == SR_TYPE_V1) {
536 sr_config |= SRCONFIG_DELAYCTRL;
537 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
538 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
539 } else if (sr->ip_type == SR_TYPE_V2) {
540 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
541 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
543 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
544 "module without specifying the ip\n", __func__);
548 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
549 sr_write_reg(sr, SRCONFIG, sr_config);
550 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
551 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
552 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
555 * Enabling the interrupts if MINMAXAVG module is used.
556 * TODO: check if all the interrupts are mandatory
558 if (sr->ip_type == SR_TYPE_V1) {
559 sr_modify_reg(sr, ERRCONFIG_V1,
560 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
561 ERRCONFIG_MCUBOUNDINTEN),
562 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
563 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
564 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
565 } else if (sr->ip_type == SR_TYPE_V2) {
566 sr_write_reg(sr, IRQSTATUS,
567 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
568 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
569 sr_write_reg(sr, IRQENABLE_SET,
570 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
571 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
578 * sr_enable() - Enables the smartreflex module.
579 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
580 * @volt: The voltage at which the Voltage domain associated with
581 * the smartreflex module is operating at.
582 * This is required only to program the correct Ntarget value.
584 * This API is to be called from the smartreflex class driver to
585 * enable a smartreflex module. Returns 0 on success. Returns error
586 * value if the voltage passed is wrong or if ntarget value is wrong.
588 int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
590 u32 nvalue_reciprocal;
591 struct omap_volt_data *volt_data;
592 struct omap_sr *sr = _sr_lookup(voltdm);
596 pr_warning("%s: omap_sr struct for sr_%s not found\n",
597 __func__, voltdm->name);
601 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
603 if (IS_ERR(volt_data)) {
604 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
605 "for nominal voltage %ld\n", __func__, volt);
609 nvalue_reciprocal = sr_retrieve_nvalue(sr, volt_data->sr_efuse_offs);
611 if (!nvalue_reciprocal) {
612 dev_warn(&sr->pdev->dev, "%s: NVALUE = 0 at voltage %ld\n",
617 /* errminlimit is opp dependent and hence linked to voltage */
618 sr->err_minlimit = volt_data->sr_errminlimit;
620 pm_runtime_get_sync(&sr->pdev->dev);
622 /* Check if SR is already enabled. If yes do nothing */
623 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
627 ret = sr_class->configure(voltdm);
631 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_reciprocal);
633 /* SRCONFIG - enable SR */
634 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
639 * sr_disable() - Disables the smartreflex module.
640 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
642 * This API is to be called from the smartreflex class driver to
643 * disable a smartreflex module.
645 void sr_disable(struct voltagedomain *voltdm)
647 struct omap_sr *sr = _sr_lookup(voltdm);
650 pr_warning("%s: omap_sr struct for sr_%s not found\n",
651 __func__, voltdm->name);
655 /* Check if SR clocks are already disabled. If yes do nothing */
656 if (pm_runtime_suspended(&sr->pdev->dev))
660 * Disable SR if only it is indeed enabled. Else just
661 * disable the clocks.
663 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
664 if (sr->ip_type == SR_TYPE_V1)
666 else if (sr->ip_type == SR_TYPE_V2)
670 pm_runtime_put_sync_suspend(&sr->pdev->dev);
674 * sr_register_class() - API to register a smartreflex class parameters.
675 * @class_data: The structure containing various sr class specific data.
677 * This API is to be called by the smartreflex class driver to register itself
678 * with the smartreflex driver during init. Returns 0 on success else the
681 int sr_register_class(struct omap_sr_class_data *class_data)
683 struct omap_sr *sr_info;
686 pr_warning("%s:, Smartreflex class data passed is NULL\n",
692 pr_warning("%s: Smartreflex class driver already registered\n",
697 sr_class = class_data;
700 * Call into late init to do intializations that require
701 * both sr driver and sr class driver to be initiallized.
703 list_for_each_entry(sr_info, &sr_list, node)
704 sr_late_init(sr_info);
710 * omap_sr_enable() - API to enable SR clocks and to call into the
711 * registered smartreflex class enable API.
712 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
714 * This API is to be called from the kernel in order to enable
715 * a particular smartreflex module. This API will do the initial
716 * configurations to turn on the smartreflex module and in turn call
717 * into the registered smartreflex class enable API.
719 void omap_sr_enable(struct voltagedomain *voltdm)
721 struct omap_sr *sr = _sr_lookup(voltdm);
724 pr_warning("%s: omap_sr struct for sr_%s not found\n",
725 __func__, voltdm->name);
729 if (!sr->autocomp_active)
732 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
733 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
734 "registered\n", __func__);
738 sr_class->enable(voltdm);
742 * omap_sr_disable() - API to disable SR without resetting the voltage
744 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
746 * This API is to be called from the kernel in order to disable
747 * a particular smartreflex module. This API will in turn call
748 * into the registered smartreflex class disable API. This API will tell
749 * the smartreflex class disable not to reset the VP voltage after
750 * disabling smartreflex.
752 void omap_sr_disable(struct voltagedomain *voltdm)
754 struct omap_sr *sr = _sr_lookup(voltdm);
757 pr_warning("%s: omap_sr struct for sr_%s not found\n",
758 __func__, voltdm->name);
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(voltdm, 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 sr_%s not found\n",
791 __func__, voltdm->name);
795 if (!sr->autocomp_active)
798 if (!sr_class || !(sr_class->disable)) {
799 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
800 "registered\n", __func__);
804 sr_class->disable(voltdm, 1);
808 * omap_sr_register_pmic() - API to register pmic specific info.
809 * @pmic_data: The structure containing pmic specific data.
811 * This API is to be called from the PMIC specific code to register with
812 * smartreflex driver pmic specific info. Currently the only info required
813 * is the smartreflex init on the PMIC side.
815 void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
818 pr_warning("%s: Trying to register NULL PMIC data structure"
819 "with smartreflex\n", __func__);
823 sr_pmic_data = pmic_data;
826 /* PM Debug Fs enteries to enable disable smartreflex. */
827 static int omap_sr_autocomp_show(void *data, u64 *val)
829 struct omap_sr *sr_info = (struct omap_sr *) data;
832 pr_warning("%s: omap_sr struct not found\n", __func__);
836 *val = sr_info->autocomp_active;
841 static int omap_sr_autocomp_store(void *data, u64 val)
843 struct omap_sr *sr_info = (struct omap_sr *) data;
846 pr_warning("%s: omap_sr struct not found\n", __func__);
851 if (val && (val != 1)) {
852 pr_warning("%s: Invalid argument %lld\n", __func__, val);
856 /* control enable/disable only if there is a delta in value */
857 if (sr_info->autocomp_active != val) {
859 sr_stop_vddautocomp(sr_info);
861 sr_start_vddautocomp(sr_info);
867 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
868 omap_sr_autocomp_store, "%llu\n");
870 static int __init omap_sr_probe(struct platform_device *pdev)
872 struct omap_sr *sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
873 struct omap_sr_data *pdata = pdev->dev.platform_data;
874 struct resource *mem, *irq;
875 struct dentry *nvalue_dir;
876 struct omap_volt_data *volt_data;
881 dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
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->pdev = pdev;
913 sr_info->srid = pdev->id;
914 sr_info->voltdm = pdata->voltdm;
915 sr_info->nvalue_table = pdata->nvalue_table;
916 sr_info->nvalue_count = pdata->nvalue_count;
917 sr_info->senn_mod = pdata->senn_mod;
918 sr_info->senp_mod = pdata->senp_mod;
919 sr_info->autocomp_active = false;
920 sr_info->ip_type = pdata->ip_type;
921 sr_info->base = ioremap(mem->start, resource_size(mem));
922 if (!sr_info->base) {
923 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
925 goto err_release_region;
929 sr_info->irq = irq->start;
931 sr_set_clk_length(sr_info);
932 sr_set_regfields(sr_info);
934 list_add(&sr_info->node, &sr_list);
937 * Call into late init to do intializations that require
938 * both sr driver and sr class driver to be initiallized.
941 ret = sr_late_init(sr_info);
943 pr_warning("%s: Error in SR late init\n", __func__);
948 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
950 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
952 ret = PTR_ERR(sr_dbg_dir);
953 pr_err("%s:sr debugfs dir creation failed(%d)\n",
959 name = kasprintf(GFP_KERNEL, "sr_%s", sr_info->voltdm->name);
961 dev_err(&pdev->dev, "%s: Unable to alloc debugfs name\n",
966 sr_info->dbg_dir = debugfs_create_dir(name, sr_dbg_dir);
968 if (IS_ERR(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);
981 (void) debugfs_create_x32("errminlimit", S_IRUGO, sr_info->dbg_dir,
982 &sr_info->err_minlimit);
984 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
985 if (IS_ERR(nvalue_dir)) {
986 dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
987 "for n-values\n", __func__);
988 ret = PTR_ERR(nvalue_dir);
992 omap_voltage_get_volttable(sr_info->voltdm, &volt_data);
994 dev_warn(&pdev->dev, "%s: No Voltage table for the"
995 " corresponding vdd vdd_%s. Cannot create debugfs"
996 "entries for n-values\n",
997 __func__, sr_info->voltdm->name);
1002 for (i = 0; i < sr_info->nvalue_count; i++) {
1003 char name[NVALUE_NAME_LEN + 1];
1005 snprintf(name, sizeof(name), "volt_%d",
1006 volt_data[i].volt_nominal);
1007 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
1008 &(sr_info->nvalue_table[i].nvalue));
1014 debugfs_remove_recursive(sr_info->dbg_dir);
1016 list_del(&sr_info->node);
1017 iounmap(sr_info->base);
1019 release_mem_region(mem->start, resource_size(mem));
1026 static int __devexit omap_sr_remove(struct platform_device *pdev)
1028 struct omap_sr_data *pdata = pdev->dev.platform_data;
1029 struct omap_sr *sr_info;
1030 struct resource *mem;
1033 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1037 sr_info = _sr_lookup(pdata->voltdm);
1038 if (IS_ERR(sr_info)) {
1039 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1044 if (sr_info->autocomp_active)
1045 sr_stop_vddautocomp(sr_info);
1046 if (sr_info->dbg_dir)
1047 debugfs_remove_recursive(sr_info->dbg_dir);
1049 list_del(&sr_info->node);
1050 iounmap(sr_info->base);
1052 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1053 release_mem_region(mem->start, resource_size(mem));
1058 static void __devexit omap_sr_shutdown(struct platform_device *pdev)
1060 struct omap_sr_data *pdata = pdev->dev.platform_data;
1061 struct omap_sr *sr_info;
1064 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1068 sr_info = _sr_lookup(pdata->voltdm);
1069 if (IS_ERR(sr_info)) {
1070 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1075 if (sr_info->autocomp_active)
1076 sr_stop_vddautocomp(sr_info);
1081 static struct platform_driver smartreflex_driver = {
1082 .remove = __devexit_p(omap_sr_remove),
1083 .shutdown = __devexit_p(omap_sr_shutdown),
1085 .name = "smartreflex",
1089 static int __init sr_init(void)
1094 * sr_init is a late init. If by then a pmic specific API is not
1095 * registered either there is no need for anything to be done on
1096 * the PMIC side or somebody has forgotten to register a PMIC
1097 * handler. Warn for the second condition.
1099 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1100 sr_pmic_data->sr_pmic_init();
1102 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
1104 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1106 pr_err("%s: platform driver register failed for SR\n",
1114 static void __exit sr_exit(void)
1116 platform_driver_unregister(&smartreflex_driver);
1118 late_initcall(sr_init);
1119 module_exit(sr_exit);
1121 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1122 MODULE_LICENSE("GPL");
1123 MODULE_ALIAS("platform:" DRIVER_NAME);
1124 MODULE_AUTHOR("Texas Instruments Inc");