OMAP3: PM: Adding debug support to Voltage and Smartreflex drivers
[linux-2.6/x86.git] / arch / arm / mach-omap2 / smartreflex.c
blob52a05b336c0827ef544a4451b75b86be3abe0834
1 /*
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
10 * Kalle Jokiniemi
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/interrupt.h>
21 #include <linux/clk.h>
22 #include <linux/io.h>
23 #include <linux/debugfs.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pm_runtime.h>
28 #include <plat/common.h>
29 #include <plat/smartreflex.h>
31 #include "pm.h"
33 #define SMARTREFLEX_NAME_LEN 16
34 #define NVALUE_NAME_LEN 40
35 #define SR_DISABLE_TIMEOUT 200
37 struct omap_sr {
38 int srid;
39 int ip_type;
40 int nvalue_count;
41 bool autocomp_active;
42 u32 clk_length;
43 u32 err_weight;
44 u32 err_minlimit;
45 u32 err_maxlimit;
46 u32 accum_data;
47 u32 senn_avgweight;
48 u32 senp_avgweight;
49 u32 senp_mod;
50 u32 senn_mod;
51 unsigned int irq;
52 void __iomem *base;
53 struct platform_device *pdev;
54 struct list_head node;
55 struct omap_sr_nvalue_table *nvalue_table;
56 struct voltagedomain *voltdm;
59 /* sr_list contains all the instances of smartreflex module */
60 static LIST_HEAD(sr_list);
62 static struct omap_sr_class_data *sr_class;
63 static struct omap_sr_pmic_data *sr_pmic_data;
65 static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
67 __raw_writel(value, (sr->base + offset));
70 static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
71 u32 value)
73 u32 reg_val;
74 u32 errconfig_offs = 0, errconfig_mask = 0;
76 reg_val = __raw_readl(sr->base + offset);
77 reg_val &= ~mask;
80 * Smartreflex error config register is special as it contains
81 * certain status bits which if written a 1 into means a clear
82 * of those bits. So in order to make sure no accidental write of
83 * 1 happens to those status bits, do a clear of them in the read
84 * value. This mean this API doesn't rewrite values in these bits
85 * if they are currently set, but does allow the caller to write
86 * those bits.
88 if (sr->ip_type == SR_TYPE_V1) {
89 errconfig_offs = ERRCONFIG_V1;
90 errconfig_mask = ERRCONFIG_STATUS_V1_MASK;
91 } else if (sr->ip_type == SR_TYPE_V2) {
92 errconfig_offs = ERRCONFIG_V2;
93 errconfig_mask = ERRCONFIG_VPBOUNDINTST_V2;
96 if (offset == errconfig_offs)
97 reg_val &= ~errconfig_mask;
99 reg_val |= value;
101 __raw_writel(reg_val, (sr->base + offset));
104 static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
106 return __raw_readl(sr->base + offset);
109 static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
111 struct omap_sr *sr_info;
113 if (!voltdm) {
114 pr_err("%s: Null voltage domain passed!\n", __func__);
115 return ERR_PTR(-EINVAL);
118 list_for_each_entry(sr_info, &sr_list, node) {
119 if (voltdm == sr_info->voltdm)
120 return sr_info;
123 return ERR_PTR(-ENODATA);
126 static irqreturn_t sr_interrupt(int irq, void *data)
128 struct omap_sr *sr_info = (struct omap_sr *)data;
129 u32 status = 0;
131 if (sr_info->ip_type == SR_TYPE_V1) {
132 /* Read the status bits */
133 status = sr_read_reg(sr_info, ERRCONFIG_V1);
135 /* Clear them by writing back */
136 sr_write_reg(sr_info, ERRCONFIG_V1, status);
137 } else if (sr_info->ip_type == SR_TYPE_V2) {
138 /* Read the status bits */
139 sr_read_reg(sr_info, IRQSTATUS);
141 /* Clear them by writing back */
142 sr_write_reg(sr_info, IRQSTATUS, status);
145 if (sr_class->class_type == SR_CLASS2 && sr_class->notify)
146 sr_class->notify(sr_info->voltdm, status);
148 return IRQ_HANDLED;
151 static void sr_set_clk_length(struct omap_sr *sr)
153 struct clk *sys_ck;
154 u32 sys_clk_speed;
156 sys_ck = clk_get(NULL, "sys_ck");
157 if (IS_ERR(sys_ck)) {
158 dev_err(&sr->pdev->dev, "%s: unable to get sys clk\n",
159 __func__);
160 return;
162 sys_clk_speed = clk_get_rate(sys_ck);
163 clk_put(sys_ck);
165 switch (sys_clk_speed) {
166 case 12000000:
167 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
168 break;
169 case 13000000:
170 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
171 break;
172 case 19200000:
173 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
174 break;
175 case 26000000:
176 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
177 break;
178 case 38400000:
179 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
180 break;
181 default:
182 dev_err(&sr->pdev->dev, "%s: Invalid sysclk value: %d\n",
183 __func__, sys_clk_speed);
184 break;
188 static void sr_set_regfields(struct omap_sr *sr)
191 * For time being these values are defined in smartreflex.h
192 * and populated during init. May be they can be moved to board
193 * file or pmic specific data structure. In that case these structure
194 * fields will have to be populated using the pdata or pmic structure.
196 if (cpu_is_omap34xx()) {
197 sr->err_weight = OMAP3430_SR_ERRWEIGHT;
198 sr->err_maxlimit = OMAP3430_SR_ERRMAXLIMIT;
199 sr->accum_data = OMAP3430_SR_ACCUMDATA;
200 if (!(strcmp(sr->voltdm->name, "mpu"))) {
201 sr->senn_avgweight = OMAP3430_SR1_SENNAVGWEIGHT;
202 sr->senp_avgweight = OMAP3430_SR1_SENPAVGWEIGHT;
203 } else {
204 sr->senn_avgweight = OMAP3430_SR2_SENNAVGWEIGHT;
205 sr->senp_avgweight = OMAP3430_SR2_SENPAVGWEIGHT;
210 static void sr_start_vddautocomp(struct omap_sr *sr)
212 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
213 dev_warn(&sr->pdev->dev,
214 "%s: smartreflex class driver not registered\n",
215 __func__);
216 return;
219 if (!sr_class->enable(sr->voltdm))
220 sr->autocomp_active = true;
223 static void sr_stop_vddautocomp(struct omap_sr *sr)
225 if (!sr_class || !(sr_class->disable)) {
226 dev_warn(&sr->pdev->dev,
227 "%s: smartreflex class driver not registered\n",
228 __func__);
229 return;
232 if (sr->autocomp_active) {
233 sr_class->disable(sr->voltdm, 1);
234 sr->autocomp_active = false;
239 * This function handles the intializations which have to be done
240 * only when both sr device and class driver regiter has
241 * completed. This will be attempted to be called from both sr class
242 * driver register and sr device intializtion API's. Only one call
243 * will ultimately succeed.
245 * Currenly this function registers interrrupt handler for a particular SR
246 * if smartreflex class driver is already registered and has
247 * requested for interrupts and the SR interrupt line in present.
249 static int sr_late_init(struct omap_sr *sr_info)
251 char *name;
252 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
253 struct resource *mem;
254 int ret = 0;
256 if (sr_class->class_type == SR_CLASS2 &&
257 sr_class->notify_flags && sr_info->irq) {
259 name = kzalloc(SMARTREFLEX_NAME_LEN + 1, GFP_KERNEL);
260 strcpy(name, "sr_");
261 strcat(name, sr_info->voltdm->name);
262 ret = request_irq(sr_info->irq, sr_interrupt,
263 0, name, (void *)sr_info);
264 if (ret)
265 goto error;
268 if (pdata && pdata->enable_on_init)
269 sr_start_vddautocomp(sr_info);
271 return ret;
273 error:
274 iounmap(sr_info->base);
275 mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
276 release_mem_region(mem->start, resource_size(mem));
277 list_del(&sr_info->node);
278 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
279 "interrupt handler. Smartreflex will"
280 "not function as desired\n", __func__);
281 kfree(sr_info);
282 return ret;
285 static void sr_v1_disable(struct omap_sr *sr)
287 int timeout = 0;
289 /* Enable MCUDisableAcknowledge interrupt */
290 sr_modify_reg(sr, ERRCONFIG_V1,
291 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
293 /* SRCONFIG - disable SR */
294 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
296 /* Disable all other SR interrupts and clear the status */
297 sr_modify_reg(sr, ERRCONFIG_V1,
298 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
299 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
300 (ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
301 ERRCONFIG_MCUBOUNDINTST |
302 ERRCONFIG_VPBOUNDINTST_V1));
305 * Wait for SR to be disabled.
306 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
308 omap_test_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
309 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
310 timeout);
312 if (timeout >= SR_DISABLE_TIMEOUT)
313 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
314 __func__);
316 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
317 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
318 ERRCONFIG_MCUDISACKINTST);
321 static void sr_v2_disable(struct omap_sr *sr)
323 int timeout = 0;
325 /* Enable MCUDisableAcknowledge interrupt */
326 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
328 /* SRCONFIG - disable SR */
329 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
331 /* Disable all other SR interrupts and clear the status */
332 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
333 ERRCONFIG_VPBOUNDINTST_V2);
334 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
335 IRQENABLE_MCUVALIDINT |
336 IRQENABLE_MCUBOUNDSINT));
337 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
338 IRQSTATUS_MCVALIDINT |
339 IRQSTATUS_MCBOUNDSINT));
342 * Wait for SR to be disabled.
343 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
345 omap_test_timeout((sr_read_reg(sr, IRQSTATUS) &
346 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
347 timeout);
349 if (timeout >= SR_DISABLE_TIMEOUT)
350 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
351 __func__);
353 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
354 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
355 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
358 static u32 sr_retrieve_nvalue(struct omap_sr *sr, u32 efuse_offs)
360 int i;
362 if (!sr->nvalue_table) {
363 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
364 __func__);
365 return 0;
368 for (i = 0; i < sr->nvalue_count; i++) {
369 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
370 return sr->nvalue_table[i].nvalue;
373 return 0;
376 /* Public Functions */
379 * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
380 * error generator module.
381 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
383 * This API is to be called from the smartreflex class driver to
384 * configure the error generator module inside the smartreflex module.
385 * SR settings if using the ERROR module inside Smartreflex.
386 * SR CLASS 3 by default uses only the ERROR module where as
387 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
388 * module. Returns 0 on success and error value in case of failure.
390 int sr_configure_errgen(struct voltagedomain *voltdm)
392 u32 sr_config, sr_errconfig, errconfig_offs, vpboundint_en;
393 u32 vpboundint_st, senp_en = 0, senn_en = 0;
394 u8 senp_shift, senn_shift;
395 struct omap_sr *sr = _sr_lookup(voltdm);
397 if (IS_ERR(sr)) {
398 pr_warning("%s: omap_sr struct for sr_%s not found\n",
399 __func__, voltdm->name);
400 return -EINVAL;
403 if (!sr->clk_length)
404 sr_set_clk_length(sr);
406 senp_en = sr->senp_mod;
407 senn_en = sr->senn_mod;
409 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
410 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
412 if (sr->ip_type == SR_TYPE_V1) {
413 sr_config |= SRCONFIG_DELAYCTRL;
414 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
415 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
416 errconfig_offs = ERRCONFIG_V1;
417 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
418 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
419 } else if (sr->ip_type == SR_TYPE_V2) {
420 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
421 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
422 errconfig_offs = ERRCONFIG_V2;
423 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
424 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
425 } else {
426 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
427 "module without specifying the ip\n", __func__);
428 return -EINVAL;
431 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
432 sr_write_reg(sr, SRCONFIG, sr_config);
433 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
434 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
435 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
436 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
437 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
438 sr_errconfig);
440 /* Enabling the interrupts if the ERROR module is used */
441 sr_modify_reg(sr, errconfig_offs,
442 vpboundint_en, (vpboundint_en | vpboundint_st));
444 return 0;
448 * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
449 * minmaxavg module.
450 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
452 * This API is to be called from the smartreflex class driver to
453 * configure the minmaxavg module inside the smartreflex module.
454 * SR settings if using the ERROR module inside Smartreflex.
455 * SR CLASS 3 by default uses only the ERROR module where as
456 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
457 * module. Returns 0 on success and error value in case of failure.
459 int sr_configure_minmax(struct voltagedomain *voltdm)
461 u32 sr_config, sr_avgwt;
462 u32 senp_en = 0, senn_en = 0;
463 u8 senp_shift, senn_shift;
464 struct omap_sr *sr = _sr_lookup(voltdm);
466 if (IS_ERR(sr)) {
467 pr_warning("%s: omap_sr struct for sr_%s not found\n",
468 __func__, voltdm->name);
469 return -EINVAL;
472 if (!sr->clk_length)
473 sr_set_clk_length(sr);
475 senp_en = sr->senp_mod;
476 senn_en = sr->senn_mod;
478 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
479 SRCONFIG_SENENABLE |
480 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
482 if (sr->ip_type == SR_TYPE_V1) {
483 sr_config |= SRCONFIG_DELAYCTRL;
484 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
485 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
486 } else if (sr->ip_type == SR_TYPE_V2) {
487 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
488 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
489 } else {
490 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
491 "module without specifying the ip\n", __func__);
492 return -EINVAL;
495 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
496 sr_write_reg(sr, SRCONFIG, sr_config);
497 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
498 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
499 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
502 * Enabling the interrupts if MINMAXAVG module is used.
503 * TODO: check if all the interrupts are mandatory
505 if (sr->ip_type == SR_TYPE_V1) {
506 sr_modify_reg(sr, ERRCONFIG_V1,
507 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
508 ERRCONFIG_MCUBOUNDINTEN),
509 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
510 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
511 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
512 } else if (sr->ip_type == SR_TYPE_V2) {
513 sr_write_reg(sr, IRQSTATUS,
514 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
515 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
516 sr_write_reg(sr, IRQENABLE_SET,
517 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
518 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
521 return 0;
525 * sr_enable() - Enables the smartreflex module.
526 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
527 * @volt: The voltage at which the Voltage domain associated with
528 * the smartreflex module is operating at.
529 * This is required only to program the correct Ntarget value.
531 * This API is to be called from the smartreflex class driver to
532 * enable a smartreflex module. Returns 0 on success. Returns error
533 * value if the voltage passed is wrong or if ntarget value is wrong.
535 int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
537 u32 nvalue_reciprocal;
538 struct omap_volt_data *volt_data;
539 struct omap_sr *sr = _sr_lookup(voltdm);
540 int ret;
542 if (IS_ERR(sr)) {
543 pr_warning("%s: omap_sr struct for sr_%s not found\n",
544 __func__, voltdm->name);
545 return -EINVAL;
548 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
550 if (IS_ERR(volt_data)) {
551 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
552 "for nominal voltage %ld\n", __func__, volt);
553 return -ENODATA;
556 nvalue_reciprocal = sr_retrieve_nvalue(sr, volt_data->sr_efuse_offs);
558 if (!nvalue_reciprocal) {
559 dev_warn(&sr->pdev->dev, "%s: NVALUE = 0 at voltage %ld\n",
560 __func__, volt);
561 return -ENODATA;
564 /* errminlimit is opp dependent and hence linked to voltage */
565 sr->err_minlimit = volt_data->sr_errminlimit;
567 pm_runtime_get_sync(&sr->pdev->dev);
569 /* Check if SR is already enabled. If yes do nothing */
570 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
571 return 0;
573 /* Configure SR */
574 ret = sr_class->configure(voltdm);
575 if (ret)
576 return ret;
578 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_reciprocal);
580 /* SRCONFIG - enable SR */
581 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
582 return 0;
586 * sr_disable() - Disables the smartreflex module.
587 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
589 * This API is to be called from the smartreflex class driver to
590 * disable a smartreflex module.
592 void sr_disable(struct voltagedomain *voltdm)
594 struct omap_sr *sr = _sr_lookup(voltdm);
596 if (IS_ERR(sr)) {
597 pr_warning("%s: omap_sr struct for sr_%s not found\n",
598 __func__, voltdm->name);
599 return;
602 /* Check if SR clocks are already disabled. If yes do nothing */
603 if (pm_runtime_suspended(&sr->pdev->dev))
604 return;
607 * Disable SR if only it is indeed enabled. Else just
608 * disable the clocks.
610 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
611 if (sr->ip_type == SR_TYPE_V1)
612 sr_v1_disable(sr);
613 else if (sr->ip_type == SR_TYPE_V2)
614 sr_v2_disable(sr);
617 pm_runtime_put_sync(&sr->pdev->dev);
621 * sr_register_class() - API to register a smartreflex class parameters.
622 * @class_data: The structure containing various sr class specific data.
624 * This API is to be called by the smartreflex class driver to register itself
625 * with the smartreflex driver during init. Returns 0 on success else the
626 * error value.
628 int sr_register_class(struct omap_sr_class_data *class_data)
630 struct omap_sr *sr_info;
632 if (!class_data) {
633 pr_warning("%s:, Smartreflex class data passed is NULL\n",
634 __func__);
635 return -EINVAL;
638 if (sr_class) {
639 pr_warning("%s: Smartreflex class driver already registered\n",
640 __func__);
641 return -EBUSY;
644 sr_class = class_data;
647 * Call into late init to do intializations that require
648 * both sr driver and sr class driver to be initiallized.
650 list_for_each_entry(sr_info, &sr_list, node)
651 sr_late_init(sr_info);
653 return 0;
657 * omap_sr_enable() - API to enable SR clocks and to call into the
658 * registered smartreflex class enable API.
659 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
661 * This API is to be called from the kernel in order to enable
662 * a particular smartreflex module. This API will do the initial
663 * configurations to turn on the smartreflex module and in turn call
664 * into the registered smartreflex class enable API.
666 void omap_sr_enable(struct voltagedomain *voltdm)
668 struct omap_sr *sr = _sr_lookup(voltdm);
670 if (IS_ERR(sr)) {
671 pr_warning("%s: omap_sr struct for sr_%s not found\n",
672 __func__, voltdm->name);
673 return;
676 if (!sr->autocomp_active)
677 return;
679 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
680 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
681 "registered\n", __func__);
682 return;
685 sr_class->enable(voltdm);
689 * omap_sr_disable() - API to disable SR without resetting the voltage
690 * processor voltage
691 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
693 * This API is to be called from the kernel in order to disable
694 * a particular smartreflex module. This API will in turn call
695 * into the registered smartreflex class disable API. This API will tell
696 * the smartreflex class disable not to reset the VP voltage after
697 * disabling smartreflex.
699 void omap_sr_disable(struct voltagedomain *voltdm)
701 struct omap_sr *sr = _sr_lookup(voltdm);
703 if (IS_ERR(sr)) {
704 pr_warning("%s: omap_sr struct for sr_%s not found\n",
705 __func__, voltdm->name);
706 return;
709 if (!sr->autocomp_active)
710 return;
712 if (!sr_class || !(sr_class->disable)) {
713 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
714 "registered\n", __func__);
715 return;
718 sr_class->disable(voltdm, 0);
722 * omap_sr_disable_reset_volt() - API to disable SR and reset the
723 * voltage processor voltage
724 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
726 * This API is to be called from the kernel in order to disable
727 * a particular smartreflex module. This API will in turn call
728 * into the registered smartreflex class disable API. This API will tell
729 * the smartreflex class disable to reset the VP voltage after
730 * disabling smartreflex.
732 void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
734 struct omap_sr *sr = _sr_lookup(voltdm);
736 if (IS_ERR(sr)) {
737 pr_warning("%s: omap_sr struct for sr_%s not found\n",
738 __func__, voltdm->name);
739 return;
742 if (!sr->autocomp_active)
743 return;
745 if (!sr_class || !(sr_class->disable)) {
746 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
747 "registered\n", __func__);
748 return;
751 sr_class->disable(voltdm, 1);
755 * omap_sr_register_pmic() - API to register pmic specific info.
756 * @pmic_data: The structure containing pmic specific data.
758 * This API is to be called from the PMIC specific code to register with
759 * smartreflex driver pmic specific info. Currently the only info required
760 * is the smartreflex init on the PMIC side.
762 void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
764 if (!pmic_data) {
765 pr_warning("%s: Trying to register NULL PMIC data structure"
766 "with smartreflex\n", __func__);
767 return;
770 sr_pmic_data = pmic_data;
773 /* PM Debug Fs enteries to enable disable smartreflex. */
774 static int omap_sr_autocomp_show(void *data, u64 *val)
776 struct omap_sr *sr_info = (struct omap_sr *) data;
778 if (!sr_info) {
779 pr_warning("%s: omap_sr struct for sr_%s not found\n",
780 __func__, sr_info->voltdm->name);
781 return -EINVAL;
784 *val = sr_info->autocomp_active;
786 return 0;
789 static int omap_sr_autocomp_store(void *data, u64 val)
791 struct omap_sr *sr_info = (struct omap_sr *) data;
793 if (!sr_info) {
794 pr_warning("%s: omap_sr struct for sr_%s not found\n",
795 __func__, sr_info->voltdm->name);
796 return -EINVAL;
799 /* Sanity check */
800 if (val && (val != 1)) {
801 pr_warning("%s: Invalid argument %lld\n", __func__, val);
802 return -EINVAL;
805 if (!val)
806 sr_stop_vddautocomp(sr_info);
807 else
808 sr_start_vddautocomp(sr_info);
810 return 0;
813 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
814 omap_sr_autocomp_store, "%llu\n");
816 static int __init omap_sr_probe(struct platform_device *pdev)
818 struct omap_sr *sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
819 struct omap_sr_data *pdata = pdev->dev.platform_data;
820 struct resource *mem, *irq;
821 struct dentry *vdd_dbg_dir, *dbg_dir, *nvalue_dir;
822 struct omap_volt_data *volt_data;
823 int i, ret = 0;
825 if (!sr_info) {
826 dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
827 __func__);
828 return -ENOMEM;
831 if (!pdata) {
832 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
833 return -EINVAL;
836 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
837 if (!mem) {
838 dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
839 ret = -ENODEV;
840 goto err_free_devinfo;
843 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
845 pm_runtime_enable(&pdev->dev);
847 sr_info->pdev = pdev;
848 sr_info->srid = pdev->id;
849 sr_info->voltdm = pdata->voltdm;
850 sr_info->nvalue_table = pdata->nvalue_table;
851 sr_info->nvalue_count = pdata->nvalue_count;
852 sr_info->senn_mod = pdata->senn_mod;
853 sr_info->senp_mod = pdata->senp_mod;
854 sr_info->autocomp_active = false;
855 sr_info->ip_type = pdata->ip_type;
856 sr_info->base = ioremap(mem->start, resource_size(mem));
857 if (!sr_info->base) {
858 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
859 ret = -ENOMEM;
860 goto err_release_region;
863 if (irq)
864 sr_info->irq = irq->start;
866 sr_set_clk_length(sr_info);
867 sr_set_regfields(sr_info);
869 list_add(&sr_info->node, &sr_list);
872 * Call into late init to do intializations that require
873 * both sr driver and sr class driver to be initiallized.
875 if (sr_class) {
876 ret = sr_late_init(sr_info);
877 if (ret) {
878 pr_warning("%s: Error in SR late init\n", __func__);
879 return ret;
883 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
886 * If the voltage domain debugfs directory is not created, do
887 * not try to create rest of the debugfs entries.
889 vdd_dbg_dir = omap_voltage_get_dbgdir(sr_info->voltdm);
890 if (!vdd_dbg_dir)
891 return -EINVAL;
893 dbg_dir = debugfs_create_dir("smartreflex", vdd_dbg_dir);
894 if (IS_ERR(dbg_dir)) {
895 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
896 __func__);
897 return PTR_ERR(dbg_dir);
900 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUGO, dbg_dir,
901 (void *)sr_info, &pm_sr_fops);
902 (void) debugfs_create_x32("errweight", S_IRUGO, dbg_dir,
903 &sr_info->err_weight);
904 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, dbg_dir,
905 &sr_info->err_maxlimit);
906 (void) debugfs_create_x32("errminlimit", S_IRUGO, dbg_dir,
907 &sr_info->err_minlimit);
909 nvalue_dir = debugfs_create_dir("nvalue", dbg_dir);
910 if (IS_ERR(nvalue_dir)) {
911 dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
912 "for n-values\n", __func__);
913 return PTR_ERR(nvalue_dir);
916 omap_voltage_get_volttable(sr_info->voltdm, &volt_data);
917 if (!volt_data) {
918 dev_warn(&pdev->dev, "%s: No Voltage table for the"
919 " corresponding vdd vdd_%s. Cannot create debugfs"
920 "entries for n-values\n",
921 __func__, sr_info->voltdm->name);
922 return -ENODATA;
925 for (i = 0; i < sr_info->nvalue_count; i++) {
926 char *name;
927 char volt_name[32];
929 name = kzalloc(NVALUE_NAME_LEN + 1, GFP_KERNEL);
930 if (!name) {
931 dev_err(&pdev->dev, "%s: Unable to allocate memory"
932 " for n-value directory name\n", __func__);
933 return -ENOMEM;
936 strcpy(name, "volt_");
937 sprintf(volt_name, "%d", volt_data[i].volt_nominal);
938 strcat(name, volt_name);
939 (void) debugfs_create_x32(name, S_IRUGO | S_IWUGO, nvalue_dir,
940 &(sr_info->nvalue_table[i].nvalue));
943 return ret;
945 err_release_region:
946 release_mem_region(mem->start, resource_size(mem));
947 err_free_devinfo:
948 kfree(sr_info);
950 return ret;
953 static int __devexit omap_sr_remove(struct platform_device *pdev)
955 struct omap_sr_data *pdata = pdev->dev.platform_data;
956 struct omap_sr *sr_info;
957 struct resource *mem;
959 if (!pdata) {
960 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
961 return -EINVAL;
964 sr_info = _sr_lookup(pdata->voltdm);
965 if (!sr_info) {
966 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
967 __func__);
968 return -EINVAL;
971 if (sr_info->autocomp_active)
972 sr_stop_vddautocomp(sr_info);
974 list_del(&sr_info->node);
975 iounmap(sr_info->base);
976 kfree(sr_info);
977 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
978 release_mem_region(mem->start, resource_size(mem));
980 return 0;
983 static struct platform_driver smartreflex_driver = {
984 .remove = omap_sr_remove,
985 .driver = {
986 .name = "smartreflex",
990 static int __init sr_init(void)
992 int ret = 0;
995 * sr_init is a late init. If by then a pmic specific API is not
996 * registered either there is no need for anything to be done on
997 * the PMIC side or somebody has forgotten to register a PMIC
998 * handler. Warn for the second condition.
1000 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1001 sr_pmic_data->sr_pmic_init();
1002 else
1003 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
1005 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1006 if (ret) {
1007 pr_err("%s: platform driver register failed for SR\n",
1008 __func__);
1009 return ret;
1012 return 0;
1015 static void __exit sr_exit(void)
1017 platform_driver_unregister(&smartreflex_driver);
1019 late_initcall(sr_init);
1020 module_exit(sr_exit);
1022 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1023 MODULE_LICENSE("GPL");
1024 MODULE_ALIAS("platform:" DRIVER_NAME);
1025 MODULE_AUTHOR("Texas Instruments Inc");