blob: 3ee7261432143c8842ea5834cb3c7ab55cf72e28 [file] [log] [blame]
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301/*
2 * OMAP SmartReflex Voltage Control
3 *
4 * Author: Thara Gopinath <thara@ti.com>
5 *
6 * Copyright (C) 2010 Texas Instruments, Inc.
7 * Thara Gopinath <thara@ti.com>
8 *
9 * Copyright (C) 2008 Nokia Corporation
10 * Kalle Jokiniemi
11 *
12 * Copyright (C) 2007 Texas Instruments, Inc.
13 * Lesly A M <x0080970@ti.com>
14 *
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.
18 */
19
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>
27
28#include <plat/common.h>
Thara Gopinath984aa6d2010-05-29 22:02:22 +053029
30#include "pm.h"
Paul Walmsley7328ff42011-02-25 15:54:33 -070031#include "smartreflex.h"
Thara Gopinath984aa6d2010-05-29 22:02:22 +053032
33#define SMARTREFLEX_NAME_LEN 16
Thara Gopinath077fcec2010-10-27 20:29:37 +053034#define NVALUE_NAME_LEN 40
Thara Gopinath984aa6d2010-05-29 22:02:22 +053035#define SR_DISABLE_TIMEOUT 200
36
37struct 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;
Anand S Sawantb1ace382011-02-17 21:27:30 +053057 struct dentry *dbg_dir;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053058};
59
60/* sr_list contains all the instances of smartreflex module */
61static LIST_HEAD(sr_list);
62
63static struct omap_sr_class_data *sr_class;
64static struct omap_sr_pmic_data *sr_pmic_data;
65
66static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
67{
68 __raw_writel(value, (sr->base + offset));
69}
70
71static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
72 u32 value)
73{
74 u32 reg_val;
75 u32 errconfig_offs = 0, errconfig_mask = 0;
76
77 reg_val = __raw_readl(sr->base + offset);
78 reg_val &= ~mask;
79
80 /*
81 * Smartreflex error config register is special as it contains
82 * certain status bits which if written a 1 into means a clear
83 * of those bits. So in order to make sure no accidental write of
84 * 1 happens to those status bits, do a clear of them in the read
85 * value. This mean this API doesn't rewrite values in these bits
86 * if they are currently set, but does allow the caller to write
87 * those bits.
88 */
89 if (sr->ip_type == SR_TYPE_V1) {
90 errconfig_offs = ERRCONFIG_V1;
91 errconfig_mask = ERRCONFIG_STATUS_V1_MASK;
92 } else if (sr->ip_type == SR_TYPE_V2) {
93 errconfig_offs = ERRCONFIG_V2;
94 errconfig_mask = ERRCONFIG_VPBOUNDINTST_V2;
95 }
96
97 if (offset == errconfig_offs)
98 reg_val &= ~errconfig_mask;
99
100 reg_val |= value;
101
102 __raw_writel(reg_val, (sr->base + offset));
103}
104
105static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
106{
107 return __raw_readl(sr->base + offset);
108}
109
110static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
111{
112 struct omap_sr *sr_info;
113
114 if (!voltdm) {
115 pr_err("%s: Null voltage domain passed!\n", __func__);
116 return ERR_PTR(-EINVAL);
117 }
118
119 list_for_each_entry(sr_info, &sr_list, node) {
120 if (voltdm == sr_info->voltdm)
121 return sr_info;
122 }
123
124 return ERR_PTR(-ENODATA);
125}
126
127static irqreturn_t sr_interrupt(int irq, void *data)
128{
129 struct omap_sr *sr_info = (struct omap_sr *)data;
130 u32 status = 0;
131
132 if (sr_info->ip_type == SR_TYPE_V1) {
133 /* Read the status bits */
134 status = sr_read_reg(sr_info, ERRCONFIG_V1);
135
136 /* Clear them by writing back */
137 sr_write_reg(sr_info, ERRCONFIG_V1, status);
138 } else if (sr_info->ip_type == SR_TYPE_V2) {
139 /* Read the status bits */
140 sr_read_reg(sr_info, IRQSTATUS);
141
142 /* Clear them by writing back */
143 sr_write_reg(sr_info, IRQSTATUS, status);
144 }
145
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530146 if (sr_class->notify)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530147 sr_class->notify(sr_info->voltdm, status);
148
149 return IRQ_HANDLED;
150}
151
152static void sr_set_clk_length(struct omap_sr *sr)
153{
154 struct clk *sys_ck;
155 u32 sys_clk_speed;
156
Thara Gopinathb35cecf2010-08-18 12:23:12 +0530157 if (cpu_is_omap34xx())
158 sys_ck = clk_get(NULL, "sys_ck");
159 else
160 sys_ck = clk_get(NULL, "sys_clkin_ck");
161
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530162 if (IS_ERR(sys_ck)) {
163 dev_err(&sr->pdev->dev, "%s: unable to get sys clk\n",
164 __func__);
165 return;
166 }
167 sys_clk_speed = clk_get_rate(sys_ck);
168 clk_put(sys_ck);
169
170 switch (sys_clk_speed) {
171 case 12000000:
172 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
173 break;
174 case 13000000:
175 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
176 break;
177 case 19200000:
178 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
179 break;
180 case 26000000:
181 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
182 break;
183 case 38400000:
184 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
185 break;
186 default:
187 dev_err(&sr->pdev->dev, "%s: Invalid sysclk value: %d\n",
188 __func__, sys_clk_speed);
189 break;
190 }
191}
192
193static void sr_set_regfields(struct omap_sr *sr)
194{
195 /*
196 * For time being these values are defined in smartreflex.h
197 * and populated during init. May be they can be moved to board
198 * file or pmic specific data structure. In that case these structure
199 * fields will have to be populated using the pdata or pmic structure.
200 */
Thara Gopinathb35cecf2010-08-18 12:23:12 +0530201 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530202 sr->err_weight = OMAP3430_SR_ERRWEIGHT;
203 sr->err_maxlimit = OMAP3430_SR_ERRMAXLIMIT;
204 sr->accum_data = OMAP3430_SR_ACCUMDATA;
205 if (!(strcmp(sr->voltdm->name, "mpu"))) {
206 sr->senn_avgweight = OMAP3430_SR1_SENNAVGWEIGHT;
207 sr->senp_avgweight = OMAP3430_SR1_SENPAVGWEIGHT;
208 } else {
209 sr->senn_avgweight = OMAP3430_SR2_SENNAVGWEIGHT;
210 sr->senp_avgweight = OMAP3430_SR2_SENPAVGWEIGHT;
211 }
212 }
213}
214
215static void sr_start_vddautocomp(struct omap_sr *sr)
216{
217 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
218 dev_warn(&sr->pdev->dev,
219 "%s: smartreflex class driver not registered\n",
220 __func__);
221 return;
222 }
223
224 if (!sr_class->enable(sr->voltdm))
225 sr->autocomp_active = true;
226}
227
228static void sr_stop_vddautocomp(struct omap_sr *sr)
229{
230 if (!sr_class || !(sr_class->disable)) {
231 dev_warn(&sr->pdev->dev,
232 "%s: smartreflex class driver not registered\n",
233 __func__);
234 return;
235 }
236
237 if (sr->autocomp_active) {
238 sr_class->disable(sr->voltdm, 1);
239 sr->autocomp_active = false;
240 }
241}
242
243/*
244 * This function handles the intializations which have to be done
245 * only when both sr device and class driver regiter has
246 * completed. This will be attempted to be called from both sr class
247 * driver register and sr device intializtion API's. Only one call
248 * will ultimately succeed.
249 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300250 * Currently this function registers interrrupt handler for a particular SR
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530251 * if smartreflex class driver is already registered and has
252 * requested for interrupts and the SR interrupt line in present.
253 */
254static int sr_late_init(struct omap_sr *sr_info)
255{
256 char *name;
257 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
258 struct resource *mem;
259 int ret = 0;
260
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530261 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
Vasiliy Kulikov5c56f322011-01-19 15:57:22 +0300262 name = kasprintf(GFP_KERNEL, "sr_%s", sr_info->voltdm->name);
263 if (name == NULL) {
264 ret = -ENOMEM;
265 goto error;
266 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530267 ret = request_irq(sr_info->irq, sr_interrupt,
268 0, name, (void *)sr_info);
269 if (ret)
270 goto error;
271 }
272
273 if (pdata && pdata->enable_on_init)
274 sr_start_vddautocomp(sr_info);
275
276 return ret;
277
278error:
279 iounmap(sr_info->base);
280 mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
281 release_mem_region(mem->start, resource_size(mem));
282 list_del(&sr_info->node);
283 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
284 "interrupt handler. Smartreflex will"
285 "not function as desired\n", __func__);
Shweta Gulatib3329a32011-02-15 13:40:30 +0530286 kfree(name);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530287 kfree(sr_info);
288 return ret;
289}
290
291static void sr_v1_disable(struct omap_sr *sr)
292{
293 int timeout = 0;
294
295 /* Enable MCUDisableAcknowledge interrupt */
296 sr_modify_reg(sr, ERRCONFIG_V1,
297 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
298
299 /* SRCONFIG - disable SR */
300 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
301
302 /* Disable all other SR interrupts and clear the status */
303 sr_modify_reg(sr, ERRCONFIG_V1,
304 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
305 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
306 (ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
307 ERRCONFIG_MCUBOUNDINTST |
308 ERRCONFIG_VPBOUNDINTST_V1));
309
310 /*
311 * Wait for SR to be disabled.
312 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
313 */
314 omap_test_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
315 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
316 timeout);
317
318 if (timeout >= SR_DISABLE_TIMEOUT)
319 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
320 __func__);
321
322 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
323 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
324 ERRCONFIG_MCUDISACKINTST);
325}
326
327static void sr_v2_disable(struct omap_sr *sr)
328{
329 int timeout = 0;
330
331 /* Enable MCUDisableAcknowledge interrupt */
332 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
333
334 /* SRCONFIG - disable SR */
335 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
336
337 /* Disable all other SR interrupts and clear the status */
338 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
339 ERRCONFIG_VPBOUNDINTST_V2);
340 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
341 IRQENABLE_MCUVALIDINT |
342 IRQENABLE_MCUBOUNDSINT));
343 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
344 IRQSTATUS_MCVALIDINT |
345 IRQSTATUS_MCBOUNDSINT));
346
347 /*
348 * Wait for SR to be disabled.
349 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
350 */
351 omap_test_timeout((sr_read_reg(sr, IRQSTATUS) &
352 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
353 timeout);
354
355 if (timeout >= SR_DISABLE_TIMEOUT)
356 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
357 __func__);
358
359 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
360 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
361 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
362}
363
364static u32 sr_retrieve_nvalue(struct omap_sr *sr, u32 efuse_offs)
365{
366 int i;
367
368 if (!sr->nvalue_table) {
369 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
370 __func__);
371 return 0;
372 }
373
374 for (i = 0; i < sr->nvalue_count; i++) {
375 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
376 return sr->nvalue_table[i].nvalue;
377 }
378
379 return 0;
380}
381
382/* Public Functions */
383
384/**
385 * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
386 * error generator module.
387 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
388 *
389 * This API is to be called from the smartreflex class driver to
390 * configure the error generator module inside the smartreflex module.
391 * SR settings if using the ERROR module inside Smartreflex.
392 * SR CLASS 3 by default uses only the ERROR module where as
393 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
394 * module. Returns 0 on success and error value in case of failure.
395 */
396int sr_configure_errgen(struct voltagedomain *voltdm)
397{
398 u32 sr_config, sr_errconfig, errconfig_offs, vpboundint_en;
399 u32 vpboundint_st, senp_en = 0, senn_en = 0;
400 u8 senp_shift, senn_shift;
401 struct omap_sr *sr = _sr_lookup(voltdm);
402
403 if (IS_ERR(sr)) {
404 pr_warning("%s: omap_sr struct for sr_%s not found\n",
405 __func__, voltdm->name);
406 return -EINVAL;
407 }
408
409 if (!sr->clk_length)
410 sr_set_clk_length(sr);
411
412 senp_en = sr->senp_mod;
413 senn_en = sr->senn_mod;
414
415 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
416 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
417
418 if (sr->ip_type == SR_TYPE_V1) {
419 sr_config |= SRCONFIG_DELAYCTRL;
420 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
421 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
422 errconfig_offs = ERRCONFIG_V1;
423 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
424 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
425 } else if (sr->ip_type == SR_TYPE_V2) {
426 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
427 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
428 errconfig_offs = ERRCONFIG_V2;
429 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
430 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
431 } else {
432 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
433 "module without specifying the ip\n", __func__);
434 return -EINVAL;
435 }
436
437 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
438 sr_write_reg(sr, SRCONFIG, sr_config);
439 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
440 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
441 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
442 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
443 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
444 sr_errconfig);
445
446 /* Enabling the interrupts if the ERROR module is used */
447 sr_modify_reg(sr, errconfig_offs,
448 vpboundint_en, (vpboundint_en | vpboundint_st));
449
450 return 0;
451}
452
453/**
454 * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
455 * minmaxavg module.
456 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
457 *
458 * This API is to be called from the smartreflex class driver to
459 * configure the minmaxavg module inside the smartreflex module.
460 * SR settings if using the ERROR module inside Smartreflex.
461 * SR CLASS 3 by default uses only the ERROR module where as
462 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
463 * module. Returns 0 on success and error value in case of failure.
464 */
465int sr_configure_minmax(struct voltagedomain *voltdm)
466{
467 u32 sr_config, sr_avgwt;
468 u32 senp_en = 0, senn_en = 0;
469 u8 senp_shift, senn_shift;
470 struct omap_sr *sr = _sr_lookup(voltdm);
471
472 if (IS_ERR(sr)) {
473 pr_warning("%s: omap_sr struct for sr_%s not found\n",
474 __func__, voltdm->name);
475 return -EINVAL;
476 }
477
478 if (!sr->clk_length)
479 sr_set_clk_length(sr);
480
481 senp_en = sr->senp_mod;
482 senn_en = sr->senn_mod;
483
484 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
485 SRCONFIG_SENENABLE |
486 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
487
488 if (sr->ip_type == SR_TYPE_V1) {
489 sr_config |= SRCONFIG_DELAYCTRL;
490 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
491 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
492 } else if (sr->ip_type == SR_TYPE_V2) {
493 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
494 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
495 } else {
496 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
497 "module without specifying the ip\n", __func__);
498 return -EINVAL;
499 }
500
501 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
502 sr_write_reg(sr, SRCONFIG, sr_config);
503 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
504 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
505 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
506
507 /*
508 * Enabling the interrupts if MINMAXAVG module is used.
509 * TODO: check if all the interrupts are mandatory
510 */
511 if (sr->ip_type == SR_TYPE_V1) {
512 sr_modify_reg(sr, ERRCONFIG_V1,
513 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
514 ERRCONFIG_MCUBOUNDINTEN),
515 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
516 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
517 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
518 } else if (sr->ip_type == SR_TYPE_V2) {
519 sr_write_reg(sr, IRQSTATUS,
520 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
521 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
522 sr_write_reg(sr, IRQENABLE_SET,
523 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
524 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
525 }
526
527 return 0;
528}
529
530/**
531 * sr_enable() - Enables the smartreflex module.
532 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
533 * @volt: The voltage at which the Voltage domain associated with
534 * the smartreflex module is operating at.
535 * This is required only to program the correct Ntarget value.
536 *
537 * This API is to be called from the smartreflex class driver to
538 * enable a smartreflex module. Returns 0 on success. Returns error
539 * value if the voltage passed is wrong or if ntarget value is wrong.
540 */
541int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
542{
543 u32 nvalue_reciprocal;
544 struct omap_volt_data *volt_data;
545 struct omap_sr *sr = _sr_lookup(voltdm);
546 int ret;
547
548 if (IS_ERR(sr)) {
549 pr_warning("%s: omap_sr struct for sr_%s not found\n",
550 __func__, voltdm->name);
551 return -EINVAL;
552 }
553
554 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
555
556 if (IS_ERR(volt_data)) {
557 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
558 "for nominal voltage %ld\n", __func__, volt);
559 return -ENODATA;
560 }
561
562 nvalue_reciprocal = sr_retrieve_nvalue(sr, volt_data->sr_efuse_offs);
563
564 if (!nvalue_reciprocal) {
565 dev_warn(&sr->pdev->dev, "%s: NVALUE = 0 at voltage %ld\n",
566 __func__, volt);
567 return -ENODATA;
568 }
569
570 /* errminlimit is opp dependent and hence linked to voltage */
571 sr->err_minlimit = volt_data->sr_errminlimit;
572
573 pm_runtime_get_sync(&sr->pdev->dev);
574
575 /* Check if SR is already enabled. If yes do nothing */
576 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
577 return 0;
578
579 /* Configure SR */
580 ret = sr_class->configure(voltdm);
581 if (ret)
582 return ret;
583
584 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_reciprocal);
585
586 /* SRCONFIG - enable SR */
587 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
588 return 0;
589}
590
591/**
592 * sr_disable() - Disables the smartreflex module.
593 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
594 *
595 * This API is to be called from the smartreflex class driver to
596 * disable a smartreflex module.
597 */
598void sr_disable(struct voltagedomain *voltdm)
599{
600 struct omap_sr *sr = _sr_lookup(voltdm);
601
602 if (IS_ERR(sr)) {
603 pr_warning("%s: omap_sr struct for sr_%s not found\n",
604 __func__, voltdm->name);
605 return;
606 }
607
608 /* Check if SR clocks are already disabled. If yes do nothing */
609 if (pm_runtime_suspended(&sr->pdev->dev))
610 return;
611
612 /*
613 * Disable SR if only it is indeed enabled. Else just
614 * disable the clocks.
615 */
616 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
617 if (sr->ip_type == SR_TYPE_V1)
618 sr_v1_disable(sr);
619 else if (sr->ip_type == SR_TYPE_V2)
620 sr_v2_disable(sr);
621 }
622
623 pm_runtime_put_sync(&sr->pdev->dev);
624}
625
626/**
627 * sr_register_class() - API to register a smartreflex class parameters.
628 * @class_data: The structure containing various sr class specific data.
629 *
630 * This API is to be called by the smartreflex class driver to register itself
631 * with the smartreflex driver during init. Returns 0 on success else the
632 * error value.
633 */
634int sr_register_class(struct omap_sr_class_data *class_data)
635{
636 struct omap_sr *sr_info;
637
638 if (!class_data) {
639 pr_warning("%s:, Smartreflex class data passed is NULL\n",
640 __func__);
641 return -EINVAL;
642 }
643
644 if (sr_class) {
645 pr_warning("%s: Smartreflex class driver already registered\n",
646 __func__);
647 return -EBUSY;
648 }
649
650 sr_class = class_data;
651
652 /*
653 * Call into late init to do intializations that require
654 * both sr driver and sr class driver to be initiallized.
655 */
656 list_for_each_entry(sr_info, &sr_list, node)
657 sr_late_init(sr_info);
658
659 return 0;
660}
661
662/**
663 * omap_sr_enable() - API to enable SR clocks and to call into the
664 * registered smartreflex class enable API.
665 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
666 *
667 * This API is to be called from the kernel in order to enable
668 * a particular smartreflex module. This API will do the initial
669 * configurations to turn on the smartreflex module and in turn call
670 * into the registered smartreflex class enable API.
671 */
672void omap_sr_enable(struct voltagedomain *voltdm)
673{
674 struct omap_sr *sr = _sr_lookup(voltdm);
675
676 if (IS_ERR(sr)) {
677 pr_warning("%s: omap_sr struct for sr_%s not found\n",
678 __func__, voltdm->name);
679 return;
680 }
681
682 if (!sr->autocomp_active)
683 return;
684
685 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
686 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
687 "registered\n", __func__);
688 return;
689 }
690
691 sr_class->enable(voltdm);
692}
693
694/**
695 * omap_sr_disable() - API to disable SR without resetting the voltage
696 * processor voltage
697 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
698 *
699 * This API is to be called from the kernel in order to disable
700 * a particular smartreflex module. This API will in turn call
701 * into the registered smartreflex class disable API. This API will tell
702 * the smartreflex class disable not to reset the VP voltage after
703 * disabling smartreflex.
704 */
705void omap_sr_disable(struct voltagedomain *voltdm)
706{
707 struct omap_sr *sr = _sr_lookup(voltdm);
708
709 if (IS_ERR(sr)) {
710 pr_warning("%s: omap_sr struct for sr_%s not found\n",
711 __func__, voltdm->name);
712 return;
713 }
714
715 if (!sr->autocomp_active)
716 return;
717
718 if (!sr_class || !(sr_class->disable)) {
719 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
720 "registered\n", __func__);
721 return;
722 }
723
724 sr_class->disable(voltdm, 0);
725}
726
727/**
728 * omap_sr_disable_reset_volt() - API to disable SR and reset the
729 * voltage processor voltage
730 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
731 *
732 * This API is to be called from the kernel in order to disable
733 * a particular smartreflex module. This API will in turn call
734 * into the registered smartreflex class disable API. This API will tell
735 * the smartreflex class disable to reset the VP voltage after
736 * disabling smartreflex.
737 */
738void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
739{
740 struct omap_sr *sr = _sr_lookup(voltdm);
741
742 if (IS_ERR(sr)) {
743 pr_warning("%s: omap_sr struct for sr_%s not found\n",
744 __func__, voltdm->name);
745 return;
746 }
747
748 if (!sr->autocomp_active)
749 return;
750
751 if (!sr_class || !(sr_class->disable)) {
752 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
753 "registered\n", __func__);
754 return;
755 }
756
757 sr_class->disable(voltdm, 1);
758}
759
760/**
761 * omap_sr_register_pmic() - API to register pmic specific info.
762 * @pmic_data: The structure containing pmic specific data.
763 *
764 * This API is to be called from the PMIC specific code to register with
765 * smartreflex driver pmic specific info. Currently the only info required
766 * is the smartreflex init on the PMIC side.
767 */
768void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
769{
770 if (!pmic_data) {
771 pr_warning("%s: Trying to register NULL PMIC data structure"
772 "with smartreflex\n", __func__);
773 return;
774 }
775
776 sr_pmic_data = pmic_data;
777}
778
779/* PM Debug Fs enteries to enable disable smartreflex. */
780static int omap_sr_autocomp_show(void *data, u64 *val)
781{
782 struct omap_sr *sr_info = (struct omap_sr *) data;
783
784 if (!sr_info) {
Stefan Weil83535842011-01-30 20:29:38 +0100785 pr_warning("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530786 return -EINVAL;
787 }
788
789 *val = sr_info->autocomp_active;
790
791 return 0;
792}
793
794static int omap_sr_autocomp_store(void *data, u64 val)
795{
796 struct omap_sr *sr_info = (struct omap_sr *) data;
797
798 if (!sr_info) {
Stefan Weil83535842011-01-30 20:29:38 +0100799 pr_warning("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530800 return -EINVAL;
801 }
802
803 /* Sanity check */
804 if (val && (val != 1)) {
805 pr_warning("%s: Invalid argument %lld\n", __func__, val);
806 return -EINVAL;
807 }
808
809 if (!val)
810 sr_stop_vddautocomp(sr_info);
811 else
812 sr_start_vddautocomp(sr_info);
813
814 return 0;
815}
816
817DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
818 omap_sr_autocomp_store, "%llu\n");
819
820static int __init omap_sr_probe(struct platform_device *pdev)
821{
822 struct omap_sr *sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
823 struct omap_sr_data *pdata = pdev->dev.platform_data;
824 struct resource *mem, *irq;
Anand S Sawantb1ace382011-02-17 21:27:30 +0530825 struct dentry *vdd_dbg_dir, *nvalue_dir;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530826 struct omap_volt_data *volt_data;
827 int i, ret = 0;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530828
829 if (!sr_info) {
830 dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
831 __func__);
832 return -ENOMEM;
833 }
834
835 if (!pdata) {
836 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
Stefan Weil720bc782011-01-30 20:26:27 +0100837 ret = -EINVAL;
838 goto err_free_devinfo;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530839 }
840
841 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
842 if (!mem) {
843 dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
844 ret = -ENODEV;
845 goto err_free_devinfo;
846 }
847
Aaro Koskinenda9e7392011-04-26 02:25:16 -0700848 mem = request_mem_region(mem->start, resource_size(mem),
849 dev_name(&pdev->dev));
850 if (!mem) {
851 dev_err(&pdev->dev, "%s: no mem region\n", __func__);
852 ret = -EBUSY;
853 goto err_free_devinfo;
854 }
855
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530856 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
857
858 pm_runtime_enable(&pdev->dev);
859
860 sr_info->pdev = pdev;
861 sr_info->srid = pdev->id;
862 sr_info->voltdm = pdata->voltdm;
863 sr_info->nvalue_table = pdata->nvalue_table;
864 sr_info->nvalue_count = pdata->nvalue_count;
865 sr_info->senn_mod = pdata->senn_mod;
866 sr_info->senp_mod = pdata->senp_mod;
867 sr_info->autocomp_active = false;
868 sr_info->ip_type = pdata->ip_type;
869 sr_info->base = ioremap(mem->start, resource_size(mem));
870 if (!sr_info->base) {
871 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
872 ret = -ENOMEM;
873 goto err_release_region;
874 }
875
876 if (irq)
877 sr_info->irq = irq->start;
878
879 sr_set_clk_length(sr_info);
880 sr_set_regfields(sr_info);
881
882 list_add(&sr_info->node, &sr_list);
883
884 /*
885 * Call into late init to do intializations that require
886 * both sr driver and sr class driver to be initiallized.
887 */
888 if (sr_class) {
889 ret = sr_late_init(sr_info);
890 if (ret) {
891 pr_warning("%s: Error in SR late init\n", __func__);
Aaro Koskinen0bf6e2e2011-04-26 02:25:10 -0700892 return ret;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530893 }
894 }
895
896 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
897
898 /*
899 * If the voltage domain debugfs directory is not created, do
900 * not try to create rest of the debugfs entries.
901 */
902 vdd_dbg_dir = omap_voltage_get_dbgdir(sr_info->voltdm);
Shweta Gulatib3329a32011-02-15 13:40:30 +0530903 if (!vdd_dbg_dir) {
904 ret = -EINVAL;
Aaro Koskinen0c49cc12011-04-26 02:25:21 -0700905 goto err_iounmap;
Shweta Gulatib3329a32011-02-15 13:40:30 +0530906 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530907
Anand S Sawantb1ace382011-02-17 21:27:30 +0530908 sr_info->dbg_dir = debugfs_create_dir("smartreflex", vdd_dbg_dir);
909 if (IS_ERR(sr_info->dbg_dir)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530910 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
911 __func__);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530912 ret = PTR_ERR(sr_info->dbg_dir);
Aaro Koskinen0c49cc12011-04-26 02:25:21 -0700913 goto err_iounmap;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530914 }
915
Anand S Sawantb1ace382011-02-17 21:27:30 +0530916 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
917 sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
918 (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530919 &sr_info->err_weight);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530920 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530921 &sr_info->err_maxlimit);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530922 (void) debugfs_create_x32("errminlimit", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530923 &sr_info->err_minlimit);
924
Anand S Sawantb1ace382011-02-17 21:27:30 +0530925 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530926 if (IS_ERR(nvalue_dir)) {
927 dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
928 "for n-values\n", __func__);
Shweta Gulatib3329a32011-02-15 13:40:30 +0530929 ret = PTR_ERR(nvalue_dir);
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700930 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530931 }
932
933 omap_voltage_get_volttable(sr_info->voltdm, &volt_data);
934 if (!volt_data) {
935 dev_warn(&pdev->dev, "%s: No Voltage table for the"
936 " corresponding vdd vdd_%s. Cannot create debugfs"
937 "entries for n-values\n",
938 __func__, sr_info->voltdm->name);
Shweta Gulatib3329a32011-02-15 13:40:30 +0530939 ret = -ENODATA;
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700940 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530941 }
942
943 for (i = 0; i < sr_info->nvalue_count; i++) {
Aaro Koskinen865212a2011-02-07 16:08:04 +0200944 char name[NVALUE_NAME_LEN + 1];
Thara Gopinath077fcec2010-10-27 20:29:37 +0530945
Aaro Koskinen865212a2011-02-07 16:08:04 +0200946 snprintf(name, sizeof(name), "volt_%d",
947 volt_data[i].volt_nominal);
Vasiliy Kulikov1232a182011-02-04 12:23:20 +0000948 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530949 &(sr_info->nvalue_table[i].nvalue));
950 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530951
952 return ret;
953
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700954err_debugfs:
955 debugfs_remove_recursive(sr_info->dbg_dir);
Aaro Koskinen0c49cc12011-04-26 02:25:21 -0700956err_iounmap:
Aaro Koskinen833d78f2011-04-26 02:25:27 -0700957 list_del(&sr_info->node);
Aaro Koskinen0c49cc12011-04-26 02:25:21 -0700958 iounmap(sr_info->base);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530959err_release_region:
960 release_mem_region(mem->start, resource_size(mem));
961err_free_devinfo:
962 kfree(sr_info);
963
964 return ret;
965}
966
967static int __devexit omap_sr_remove(struct platform_device *pdev)
968{
969 struct omap_sr_data *pdata = pdev->dev.platform_data;
970 struct omap_sr *sr_info;
971 struct resource *mem;
972
973 if (!pdata) {
974 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
975 return -EINVAL;
976 }
977
978 sr_info = _sr_lookup(pdata->voltdm);
Julia Lawall28693ec2011-01-24 20:55:22 +0100979 if (IS_ERR(sr_info)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530980 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
981 __func__);
982 return -EINVAL;
983 }
984
985 if (sr_info->autocomp_active)
986 sr_stop_vddautocomp(sr_info);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530987 if (sr_info->dbg_dir)
988 debugfs_remove_recursive(sr_info->dbg_dir);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530989
990 list_del(&sr_info->node);
991 iounmap(sr_info->base);
992 kfree(sr_info);
993 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
994 release_mem_region(mem->start, resource_size(mem));
995
996 return 0;
997}
998
999static struct platform_driver smartreflex_driver = {
1000 .remove = omap_sr_remove,
1001 .driver = {
1002 .name = "smartreflex",
1003 },
1004};
1005
1006static int __init sr_init(void)
1007{
1008 int ret = 0;
1009
1010 /*
1011 * sr_init is a late init. If by then a pmic specific API is not
1012 * registered either there is no need for anything to be done on
1013 * the PMIC side or somebody has forgotten to register a PMIC
1014 * handler. Warn for the second condition.
1015 */
1016 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1017 sr_pmic_data->sr_pmic_init();
1018 else
1019 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
1020
1021 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1022 if (ret) {
1023 pr_err("%s: platform driver register failed for SR\n",
1024 __func__);
1025 return ret;
1026 }
1027
1028 return 0;
1029}
1030
1031static void __exit sr_exit(void)
1032{
1033 platform_driver_unregister(&smartreflex_driver);
1034}
1035late_initcall(sr_init);
1036module_exit(sr_exit);
1037
1038MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1039MODULE_LICENSE("GPL");
1040MODULE_ALIAS("platform:" DRIVER_NAME);
1041MODULE_AUTHOR("Texas Instruments Inc");