]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/power/avs/smartreflex.c
536d99dc0008fd45474e25c7b38d942a9824ba1a
[linux.git] / drivers / power / avs / smartreflex.c
1 /*
2  * OMAP SmartReflex Voltage Control
3  *
4  * Author: Thara Gopinath       <thara@ti.com>
5  *
6  * Copyright (C) 2012 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/module.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/interrupt.h>
23 #include <linux/clk.h>
24 #include <linux/io.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/power/smartreflex.h>
30
31 #define DRIVER_NAME     "smartreflex"
32 #define SMARTREFLEX_NAME_LEN    32
33 #define NVALUE_NAME_LEN         40
34 #define SR_DISABLE_TIMEOUT      200
35
36 /* sr_list contains all the instances of smartreflex module */
37 static LIST_HEAD(sr_list);
38
39 static struct omap_sr_class_data *sr_class;
40 static struct dentry            *sr_dbg_dir;
41
42 static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
43 {
44         __raw_writel(value, (sr->base + offset));
45 }
46
47 static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
48                                         u32 value)
49 {
50         u32 reg_val;
51
52         /*
53          * Smartreflex error config register is special as it contains
54          * certain status bits which if written a 1 into means a clear
55          * of those bits. So in order to make sure no accidental write of
56          * 1 happens to those status bits, do a clear of them in the read
57          * value. This mean this API doesn't rewrite values in these bits
58          * if they are currently set, but does allow the caller to write
59          * those bits.
60          */
61         if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
62                 mask |= ERRCONFIG_STATUS_V1_MASK;
63         else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
64                 mask |= ERRCONFIG_VPBOUNDINTST_V2;
65
66         reg_val = __raw_readl(sr->base + offset);
67         reg_val &= ~mask;
68
69         value &= mask;
70
71         reg_val |= value;
72
73         __raw_writel(reg_val, (sr->base + offset));
74 }
75
76 static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
77 {
78         return __raw_readl(sr->base + offset);
79 }
80
81 static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
82 {
83         struct omap_sr *sr_info;
84
85         if (!voltdm) {
86                 pr_err("%s: Null voltage domain passed!\n", __func__);
87                 return ERR_PTR(-EINVAL);
88         }
89
90         list_for_each_entry(sr_info, &sr_list, node) {
91                 if (voltdm == sr_info->voltdm)
92                         return sr_info;
93         }
94
95         return ERR_PTR(-ENODATA);
96 }
97
98 static irqreturn_t sr_interrupt(int irq, void *data)
99 {
100         struct omap_sr *sr_info = data;
101         u32 status = 0;
102
103         switch (sr_info->ip_type) {
104         case SR_TYPE_V1:
105                 /* Read the status bits */
106                 status = sr_read_reg(sr_info, ERRCONFIG_V1);
107
108                 /* Clear them by writing back */
109                 sr_write_reg(sr_info, ERRCONFIG_V1, status);
110                 break;
111         case SR_TYPE_V2:
112                 /* Read the status bits */
113                 status = sr_read_reg(sr_info, IRQSTATUS);
114
115                 /* Clear them by writing back */
116                 sr_write_reg(sr_info, IRQSTATUS, status);
117                 break;
118         default:
119                 dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
120                         sr_info->ip_type);
121                 return IRQ_NONE;
122         }
123
124         if (sr_class->notify)
125                 sr_class->notify(sr_info, status);
126
127         return IRQ_HANDLED;
128 }
129
130 static void sr_set_clk_length(struct omap_sr *sr)
131 {
132         struct clk *fck;
133         u32 fclk_speed;
134
135         /* Try interconnect target module fck first if it already exists */
136         fck = clk_get(sr->pdev->dev.parent, "fck");
137         if (IS_ERR(fck)) {
138                 fck = clk_get(&sr->pdev->dev, "fck");
139                 if (IS_ERR(fck)) {
140                         dev_err(&sr->pdev->dev,
141                                 "%s: unable to get fck for device %s\n",
142                                 __func__, dev_name(&sr->pdev->dev));
143                         return;
144                 }
145         }
146
147         fclk_speed = clk_get_rate(fck);
148         clk_put(fck);
149
150         switch (fclk_speed) {
151         case 12000000:
152                 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
153                 break;
154         case 13000000:
155                 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
156                 break;
157         case 19200000:
158                 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
159                 break;
160         case 26000000:
161                 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
162                 break;
163         case 38400000:
164                 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
165                 break;
166         default:
167                 dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
168                         __func__, fclk_speed);
169                 break;
170         }
171 }
172
173 static void sr_start_vddautocomp(struct omap_sr *sr)
174 {
175         if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
176                 dev_warn(&sr->pdev->dev,
177                          "%s: smartreflex class driver not registered\n",
178                          __func__);
179                 return;
180         }
181
182         if (!sr_class->enable(sr))
183                 sr->autocomp_active = true;
184 }
185
186 static void sr_stop_vddautocomp(struct omap_sr *sr)
187 {
188         if (!sr_class || !(sr_class->disable)) {
189                 dev_warn(&sr->pdev->dev,
190                          "%s: smartreflex class driver not registered\n",
191                          __func__);
192                 return;
193         }
194
195         if (sr->autocomp_active) {
196                 sr_class->disable(sr, 1);
197                 sr->autocomp_active = false;
198         }
199 }
200
201 /*
202  * This function handles the initializations which have to be done
203  * only when both sr device and class driver regiter has
204  * completed. This will be attempted to be called from both sr class
205  * driver register and sr device intializtion API's. Only one call
206  * will ultimately succeed.
207  *
208  * Currently this function registers interrupt handler for a particular SR
209  * if smartreflex class driver is already registered and has
210  * requested for interrupts and the SR interrupt line in present.
211  */
212 static int sr_late_init(struct omap_sr *sr_info)
213 {
214         struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
215         int ret = 0;
216
217         if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
218                 ret = devm_request_irq(&sr_info->pdev->dev, sr_info->irq,
219                                        sr_interrupt, 0, sr_info->name, sr_info);
220                 if (ret)
221                         goto error;
222                 disable_irq(sr_info->irq);
223         }
224
225         if (pdata && pdata->enable_on_init)
226                 sr_start_vddautocomp(sr_info);
227
228         return ret;
229
230 error:
231         list_del(&sr_info->node);
232         dev_err(&sr_info->pdev->dev, "%s: ERROR in registering interrupt handler. Smartreflex will not function as desired\n",
233                 __func__);
234
235         return ret;
236 }
237
238 static void sr_v1_disable(struct omap_sr *sr)
239 {
240         int timeout = 0;
241         int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
242                         ERRCONFIG_MCUBOUNDINTST;
243
244         /* Enable MCUDisableAcknowledge interrupt */
245         sr_modify_reg(sr, ERRCONFIG_V1,
246                         ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
247
248         /* SRCONFIG - disable SR */
249         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
250
251         /* Disable all other SR interrupts and clear the status as needed */
252         if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
253                 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
254         sr_modify_reg(sr, ERRCONFIG_V1,
255                         (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
256                         ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
257                         errconf_val);
258
259         /*
260          * Wait for SR to be disabled.
261          * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
262          */
263         sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
264                              ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
265                              timeout);
266
267         if (timeout >= SR_DISABLE_TIMEOUT)
268                 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
269                          __func__);
270
271         /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
272         sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
273                         ERRCONFIG_MCUDISACKINTST);
274 }
275
276 static void sr_v2_disable(struct omap_sr *sr)
277 {
278         int timeout = 0;
279
280         /* Enable MCUDisableAcknowledge interrupt */
281         sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
282
283         /* SRCONFIG - disable SR */
284         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
285
286         /*
287          * Disable all other SR interrupts and clear the status
288          * write to status register ONLY on need basis - only if status
289          * is set.
290          */
291         if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
292                 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
293                         ERRCONFIG_VPBOUNDINTST_V2);
294         else
295                 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
296                                 0x0);
297         sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
298                         IRQENABLE_MCUVALIDINT |
299                         IRQENABLE_MCUBOUNDSINT));
300         sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
301                         IRQSTATUS_MCVALIDINT |
302                         IRQSTATUS_MCBOUNDSINT));
303
304         /*
305          * Wait for SR to be disabled.
306          * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
307          */
308         sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
309                              IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
310                              timeout);
311
312         if (timeout >= SR_DISABLE_TIMEOUT)
313                 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
314                          __func__);
315
316         /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
317         sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
318         sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
319 }
320
321 static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
322                                 struct omap_sr *sr, u32 efuse_offs)
323 {
324         int i;
325
326         if (!sr->nvalue_table) {
327                 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
328                          __func__);
329                 return NULL;
330         }
331
332         for (i = 0; i < sr->nvalue_count; i++) {
333                 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
334                         return &sr->nvalue_table[i];
335         }
336
337         return NULL;
338 }
339
340 /* Public Functions */
341
342 /**
343  * sr_configure_errgen() - Configures the SmartReflex to perform AVS using the
344  *                       error generator module.
345  * @sr:                 SR module to be configured.
346  *
347  * This API is to be called from the smartreflex class driver to
348  * configure the error generator module inside the smartreflex module.
349  * SR settings if using the ERROR module inside Smartreflex.
350  * SR CLASS 3 by default uses only the ERROR module where as
351  * SR CLASS 2 can choose between ERROR module and MINMAXAVG
352  * module. Returns 0 on success and error value in case of failure.
353  */
354 int sr_configure_errgen(struct omap_sr *sr)
355 {
356         u32 sr_config, sr_errconfig, errconfig_offs;
357         u32 vpboundint_en, vpboundint_st;
358         u32 senp_en = 0, senn_en = 0;
359         u8 senp_shift, senn_shift;
360
361         if (!sr) {
362                 pr_warn("%s: NULL omap_sr from %pS\n",
363                         __func__, (void *)_RET_IP_);
364                 return -EINVAL;
365         }
366
367         if (!sr->clk_length)
368                 sr_set_clk_length(sr);
369
370         senp_en = sr->senp_mod;
371         senn_en = sr->senn_mod;
372
373         sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
374                 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
375
376         switch (sr->ip_type) {
377         case SR_TYPE_V1:
378                 sr_config |= SRCONFIG_DELAYCTRL;
379                 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
380                 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
381                 errconfig_offs = ERRCONFIG_V1;
382                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
383                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
384                 break;
385         case SR_TYPE_V2:
386                 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
387                 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
388                 errconfig_offs = ERRCONFIG_V2;
389                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
390                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
391                 break;
392         default:
393                 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
394                         __func__);
395                 return -EINVAL;
396         }
397
398         sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
399         sr_write_reg(sr, SRCONFIG, sr_config);
400         sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
401                 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
402                 (sr->err_minlimit <<  ERRCONFIG_ERRMINLIMIT_SHIFT);
403         sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
404                 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
405                 sr_errconfig);
406
407         /* Enabling the interrupts if the ERROR module is used */
408         sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
409                       vpboundint_en);
410
411         return 0;
412 }
413
414 /**
415  * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
416  * @sr:                 SR module to be configured.
417  *
418  * This API is to be called from the smartreflex class driver to
419  * disable the error generator module inside the smartreflex module.
420  *
421  * Returns 0 on success and error value in case of failure.
422  */
423 int sr_disable_errgen(struct omap_sr *sr)
424 {
425         u32 errconfig_offs;
426         u32 vpboundint_en, vpboundint_st;
427
428         if (!sr) {
429                 pr_warn("%s: NULL omap_sr from %pS\n",
430                         __func__, (void *)_RET_IP_);
431                 return -EINVAL;
432         }
433
434         switch (sr->ip_type) {
435         case SR_TYPE_V1:
436                 errconfig_offs = ERRCONFIG_V1;
437                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
438                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
439                 break;
440         case SR_TYPE_V2:
441                 errconfig_offs = ERRCONFIG_V2;
442                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
443                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
444                 break;
445         default:
446                 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
447                         __func__);
448                 return -EINVAL;
449         }
450
451         /* Disable the Sensor and errorgen */
452         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
453
454         /*
455          * Disable the interrupts of ERROR module
456          * NOTE: modify is a read, modify,write - an implicit OCP barrier
457          * which is required is present here - sequencing is critical
458          * at this point (after errgen is disabled, vpboundint disable)
459          */
460         sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
461
462         return 0;
463 }
464
465 /**
466  * sr_configure_minmax() - Configures the SmartReflex to perform AVS using the
467  *                       minmaxavg module.
468  * @sr:                 SR module to be configured.
469  *
470  * This API is to be called from the smartreflex class driver to
471  * configure the minmaxavg module inside the smartreflex module.
472  * SR settings if using the ERROR module inside Smartreflex.
473  * SR CLASS 3 by default uses only the ERROR module where as
474  * SR CLASS 2 can choose between ERROR module and MINMAXAVG
475  * module. Returns 0 on success and error value in case of failure.
476  */
477 int sr_configure_minmax(struct omap_sr *sr)
478 {
479         u32 sr_config, sr_avgwt;
480         u32 senp_en = 0, senn_en = 0;
481         u8 senp_shift, senn_shift;
482
483         if (!sr) {
484                 pr_warn("%s: NULL omap_sr from %pS\n",
485                         __func__, (void *)_RET_IP_);
486                 return -EINVAL;
487         }
488
489         if (!sr->clk_length)
490                 sr_set_clk_length(sr);
491
492         senp_en = sr->senp_mod;
493         senn_en = sr->senn_mod;
494
495         sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
496                 SRCONFIG_SENENABLE |
497                 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
498
499         switch (sr->ip_type) {
500         case SR_TYPE_V1:
501                 sr_config |= SRCONFIG_DELAYCTRL;
502                 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
503                 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
504                 break;
505         case SR_TYPE_V2:
506                 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
507                 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
508                 break;
509         default:
510                 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
511                         __func__);
512                 return -EINVAL;
513         }
514
515         sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
516         sr_write_reg(sr, SRCONFIG, sr_config);
517         sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
518                 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
519         sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
520
521         /*
522          * Enabling the interrupts if MINMAXAVG module is used.
523          * TODO: check if all the interrupts are mandatory
524          */
525         switch (sr->ip_type) {
526         case SR_TYPE_V1:
527                 sr_modify_reg(sr, ERRCONFIG_V1,
528                         (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
529                         ERRCONFIG_MCUBOUNDINTEN),
530                         (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
531                          ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
532                          ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
533                 break;
534         case SR_TYPE_V2:
535                 sr_write_reg(sr, IRQSTATUS,
536                         IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
537                         IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
538                 sr_write_reg(sr, IRQENABLE_SET,
539                         IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
540                         IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
541                 break;
542         default:
543                 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
544                         __func__);
545                 return -EINVAL;
546         }
547
548         return 0;
549 }
550
551 /**
552  * sr_enable() - Enables the smartreflex module.
553  * @sr:         pointer to which the SR module to be configured belongs to.
554  * @volt:       The voltage at which the Voltage domain associated with
555  *              the smartreflex module is operating at.
556  *              This is required only to program the correct Ntarget value.
557  *
558  * This API is to be called from the smartreflex class driver to
559  * enable a smartreflex module. Returns 0 on success. Returns error
560  * value if the voltage passed is wrong or if ntarget value is wrong.
561  */
562 int sr_enable(struct omap_sr *sr, unsigned long volt)
563 {
564         struct omap_volt_data *volt_data;
565         struct omap_sr_nvalue_table *nvalue_row;
566         int ret;
567
568         if (!sr) {
569                 pr_warn("%s: NULL omap_sr from %pS\n",
570                         __func__, (void *)_RET_IP_);
571                 return -EINVAL;
572         }
573
574         volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
575
576         if (IS_ERR(volt_data)) {
577                 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table for nominal voltage %ld\n",
578                          __func__, volt);
579                 return PTR_ERR(volt_data);
580         }
581
582         nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
583
584         if (!nvalue_row) {
585                 dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
586                          __func__, volt);
587                 return -ENODATA;
588         }
589
590         /* errminlimit is opp dependent and hence linked to voltage */
591         sr->err_minlimit = nvalue_row->errminlimit;
592
593         pm_runtime_get_sync(&sr->pdev->dev);
594
595         /* Check if SR is already enabled. If yes do nothing */
596         if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
597                 return 0;
598
599         /* Configure SR */
600         ret = sr_class->configure(sr);
601         if (ret)
602                 return ret;
603
604         sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
605
606         /* SRCONFIG - enable SR */
607         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
608         return 0;
609 }
610
611 /**
612  * sr_disable() - Disables the smartreflex module.
613  * @sr:         pointer to which the SR module to be configured belongs to.
614  *
615  * This API is to be called from the smartreflex class driver to
616  * disable a smartreflex module.
617  */
618 void sr_disable(struct omap_sr *sr)
619 {
620         if (!sr) {
621                 pr_warn("%s: NULL omap_sr from %pS\n",
622                         __func__, (void *)_RET_IP_);
623                 return;
624         }
625
626         /* Check if SR clocks are already disabled. If yes do nothing */
627         if (pm_runtime_suspended(&sr->pdev->dev))
628                 return;
629
630         /*
631          * Disable SR if only it is indeed enabled. Else just
632          * disable the clocks.
633          */
634         if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
635                 switch (sr->ip_type) {
636                 case SR_TYPE_V1:
637                         sr_v1_disable(sr);
638                         break;
639                 case SR_TYPE_V2:
640                         sr_v2_disable(sr);
641                         break;
642                 default:
643                         dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
644                                 sr->ip_type);
645                 }
646         }
647
648         pm_runtime_put_sync_suspend(&sr->pdev->dev);
649 }
650
651 /**
652  * sr_register_class() - API to register a smartreflex class parameters.
653  * @class_data: The structure containing various sr class specific data.
654  *
655  * This API is to be called by the smartreflex class driver to register itself
656  * with the smartreflex driver during init. Returns 0 on success else the
657  * error value.
658  */
659 int sr_register_class(struct omap_sr_class_data *class_data)
660 {
661         struct omap_sr *sr_info;
662
663         if (!class_data) {
664                 pr_warn("%s:, Smartreflex class data passed is NULL\n",
665                         __func__);
666                 return -EINVAL;
667         }
668
669         if (sr_class) {
670                 pr_warn("%s: Smartreflex class driver already registered\n",
671                         __func__);
672                 return -EBUSY;
673         }
674
675         sr_class = class_data;
676
677         /*
678          * Call into late init to do initializations that require
679          * both sr driver and sr class driver to be initiallized.
680          */
681         list_for_each_entry(sr_info, &sr_list, node)
682                 sr_late_init(sr_info);
683
684         return 0;
685 }
686
687 /**
688  * omap_sr_enable() -  API to enable SR clocks and to call into the
689  *                      registered smartreflex class enable API.
690  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
691  *
692  * This API is to be called from the kernel in order to enable
693  * a particular smartreflex module. This API will do the initial
694  * configurations to turn on the smartreflex module and in turn call
695  * into the registered smartreflex class enable API.
696  */
697 void omap_sr_enable(struct voltagedomain *voltdm)
698 {
699         struct omap_sr *sr = _sr_lookup(voltdm);
700
701         if (IS_ERR(sr)) {
702                 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
703                 return;
704         }
705
706         if (!sr->autocomp_active)
707                 return;
708
709         if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
710                 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
711                          __func__);
712                 return;
713         }
714
715         sr_class->enable(sr);
716 }
717
718 /**
719  * omap_sr_disable() - API to disable SR without resetting the voltage
720  *                      processor voltage
721  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
722  *
723  * This API is to be called from the kernel in order to disable
724  * a particular smartreflex module. This API will in turn call
725  * into the registered smartreflex class disable API. This API will tell
726  * the smartreflex class disable not to reset the VP voltage after
727  * disabling smartreflex.
728  */
729 void omap_sr_disable(struct voltagedomain *voltdm)
730 {
731         struct omap_sr *sr = _sr_lookup(voltdm);
732
733         if (IS_ERR(sr)) {
734                 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
735                 return;
736         }
737
738         if (!sr->autocomp_active)
739                 return;
740
741         if (!sr_class || !(sr_class->disable)) {
742                 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
743                          __func__);
744                 return;
745         }
746
747         sr_class->disable(sr, 0);
748 }
749
750 /**
751  * omap_sr_disable_reset_volt() - API to disable SR and reset the
752  *                              voltage processor voltage
753  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
754  *
755  * This API is to be called from the kernel in order to disable
756  * a particular smartreflex module. This API will in turn call
757  * into the registered smartreflex class disable API. This API will tell
758  * the smartreflex class disable to reset the VP voltage after
759  * disabling smartreflex.
760  */
761 void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
762 {
763         struct omap_sr *sr = _sr_lookup(voltdm);
764
765         if (IS_ERR(sr)) {
766                 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
767                 return;
768         }
769
770         if (!sr->autocomp_active)
771                 return;
772
773         if (!sr_class || !(sr_class->disable)) {
774                 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
775                          __func__);
776                 return;
777         }
778
779         sr_class->disable(sr, 1);
780 }
781
782 /* PM Debug FS entries to enable and disable smartreflex. */
783 static int omap_sr_autocomp_show(void *data, u64 *val)
784 {
785         struct omap_sr *sr_info = data;
786
787         if (!sr_info) {
788                 pr_warn("%s: omap_sr struct not found\n", __func__);
789                 return -EINVAL;
790         }
791
792         *val = sr_info->autocomp_active;
793
794         return 0;
795 }
796
797 static int omap_sr_autocomp_store(void *data, u64 val)
798 {
799         struct omap_sr *sr_info = data;
800
801         if (!sr_info) {
802                 pr_warn("%s: omap_sr struct not found\n", __func__);
803                 return -EINVAL;
804         }
805
806         /* Sanity check */
807         if (val > 1) {
808                 pr_warn("%s: Invalid argument %lld\n", __func__, val);
809                 return -EINVAL;
810         }
811
812         /* control enable/disable only if there is a delta in value */
813         if (sr_info->autocomp_active != val) {
814                 if (!val)
815                         sr_stop_vddautocomp(sr_info);
816                 else
817                         sr_start_vddautocomp(sr_info);
818         }
819
820         return 0;
821 }
822
823 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
824                         omap_sr_autocomp_store, "%llu\n");
825
826 static int omap_sr_probe(struct platform_device *pdev)
827 {
828         struct omap_sr *sr_info;
829         struct omap_sr_data *pdata = pdev->dev.platform_data;
830         struct resource *mem, *irq;
831         struct dentry *nvalue_dir;
832         int i, ret = 0;
833
834         sr_info = devm_kzalloc(&pdev->dev, sizeof(struct omap_sr), GFP_KERNEL);
835         if (!sr_info)
836                 return -ENOMEM;
837
838         sr_info->name = devm_kzalloc(&pdev->dev,
839                                      SMARTREFLEX_NAME_LEN, GFP_KERNEL);
840         if (!sr_info->name)
841                 return -ENOMEM;
842
843         platform_set_drvdata(pdev, sr_info);
844
845         if (!pdata) {
846                 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
847                 return -EINVAL;
848         }
849
850         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
851         sr_info->base = devm_ioremap_resource(&pdev->dev, mem);
852         if (IS_ERR(sr_info->base)) {
853                 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
854                 return PTR_ERR(sr_info->base);
855         }
856
857         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
858
859         pm_runtime_enable(&pdev->dev);
860         pm_runtime_irq_safe(&pdev->dev);
861
862         snprintf(sr_info->name, SMARTREFLEX_NAME_LEN, "%s", pdata->name);
863
864         sr_info->pdev = pdev;
865         sr_info->srid = pdev->id;
866         sr_info->voltdm = pdata->voltdm;
867         sr_info->nvalue_table = pdata->nvalue_table;
868         sr_info->nvalue_count = pdata->nvalue_count;
869         sr_info->senn_mod = pdata->senn_mod;
870         sr_info->senp_mod = pdata->senp_mod;
871         sr_info->err_weight = pdata->err_weight;
872         sr_info->err_maxlimit = pdata->err_maxlimit;
873         sr_info->accum_data = pdata->accum_data;
874         sr_info->senn_avgweight = pdata->senn_avgweight;
875         sr_info->senp_avgweight = pdata->senp_avgweight;
876         sr_info->autocomp_active = false;
877         sr_info->ip_type = pdata->ip_type;
878
879         if (irq)
880                 sr_info->irq = irq->start;
881
882         sr_set_clk_length(sr_info);
883
884         list_add(&sr_info->node, &sr_list);
885
886         ret = pm_runtime_get_sync(&pdev->dev);
887         if (ret < 0) {
888                 pm_runtime_put_noidle(&pdev->dev);
889                 goto err_list_del;
890         }
891
892         /*
893          * Call into late init to do initializations that require
894          * both sr driver and sr class driver to be initiallized.
895          */
896         if (sr_class) {
897                 ret = sr_late_init(sr_info);
898                 if (ret) {
899                         pr_warn("%s: Error in SR late init\n", __func__);
900                         goto err_list_del;
901                 }
902         }
903
904         dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
905         if (!sr_dbg_dir) {
906                 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
907                 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
908                         ret = PTR_ERR(sr_dbg_dir);
909                         pr_err("%s:sr debugfs dir creation failed(%d)\n",
910                                __func__, ret);
911                         goto err_list_del;
912                 }
913         }
914
915         sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
916         if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
917                 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
918                         __func__);
919                 ret = PTR_ERR(sr_info->dbg_dir);
920                 goto err_debugfs;
921         }
922
923         (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
924                         sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
925         (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
926                         &sr_info->err_weight);
927         (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
928                         &sr_info->err_maxlimit);
929
930         nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
931         if (IS_ERR_OR_NULL(nvalue_dir)) {
932                 dev_err(&pdev->dev, "%s: Unable to create debugfs directory for n-values\n",
933                         __func__);
934                 ret = PTR_ERR(nvalue_dir);
935                 goto err_debugfs;
936         }
937
938         if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
939                 dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
940                          __func__, sr_info->name);
941
942                 ret = -ENODATA;
943                 goto err_debugfs;
944         }
945
946         for (i = 0; i < sr_info->nvalue_count; i++) {
947                 char name[NVALUE_NAME_LEN + 1];
948
949                 snprintf(name, sizeof(name), "volt_%lu",
950                                 sr_info->nvalue_table[i].volt_nominal);
951                 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
952                                 &(sr_info->nvalue_table[i].nvalue));
953                 snprintf(name, sizeof(name), "errminlimit_%lu",
954                          sr_info->nvalue_table[i].volt_nominal);
955                 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
956                                 &(sr_info->nvalue_table[i].errminlimit));
957
958         }
959
960         pm_runtime_put_sync(&pdev->dev);
961
962         return ret;
963
964 err_debugfs:
965         debugfs_remove_recursive(sr_info->dbg_dir);
966 err_list_del:
967         list_del(&sr_info->node);
968
969         pm_runtime_put_sync(&pdev->dev);
970
971         return ret;
972 }
973
974 static int omap_sr_remove(struct platform_device *pdev)
975 {
976         struct omap_sr_data *pdata = pdev->dev.platform_data;
977         struct omap_sr *sr_info;
978
979         if (!pdata) {
980                 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
981                 return -EINVAL;
982         }
983
984         sr_info = _sr_lookup(pdata->voltdm);
985         if (IS_ERR(sr_info)) {
986                 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
987                         __func__);
988                 return PTR_ERR(sr_info);
989         }
990
991         if (sr_info->autocomp_active)
992                 sr_stop_vddautocomp(sr_info);
993         if (sr_info->dbg_dir)
994                 debugfs_remove_recursive(sr_info->dbg_dir);
995
996         pm_runtime_disable(&pdev->dev);
997         list_del(&sr_info->node);
998         return 0;
999 }
1000
1001 static void omap_sr_shutdown(struct platform_device *pdev)
1002 {
1003         struct omap_sr_data *pdata = pdev->dev.platform_data;
1004         struct omap_sr *sr_info;
1005
1006         if (!pdata) {
1007                 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1008                 return;
1009         }
1010
1011         sr_info = _sr_lookup(pdata->voltdm);
1012         if (IS_ERR(sr_info)) {
1013                 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1014                         __func__);
1015                 return;
1016         }
1017
1018         if (sr_info->autocomp_active)
1019                 sr_stop_vddautocomp(sr_info);
1020
1021         return;
1022 }
1023
1024 static const struct of_device_id omap_sr_match[] = {
1025         { .compatible = "ti,omap3-smartreflex-core", },
1026         { .compatible = "ti,omap3-smartreflex-mpu-iva", },
1027         { .compatible = "ti,omap4-smartreflex-core", },
1028         { .compatible = "ti,omap4-smartreflex-mpu", },
1029         { .compatible = "ti,omap4-smartreflex-iva", },
1030         {  },
1031 };
1032 MODULE_DEVICE_TABLE(of, omap_sr_match);
1033
1034 static struct platform_driver smartreflex_driver = {
1035         .probe          = omap_sr_probe,
1036         .remove         = omap_sr_remove,
1037         .shutdown       = omap_sr_shutdown,
1038         .driver         = {
1039                 .name   = DRIVER_NAME,
1040                 .of_match_table = omap_sr_match,
1041         },
1042 };
1043
1044 static int __init sr_init(void)
1045 {
1046         int ret = 0;
1047
1048         ret = platform_driver_register(&smartreflex_driver);
1049         if (ret) {
1050                 pr_err("%s: platform driver register failed for SR\n",
1051                        __func__);
1052                 return ret;
1053         }
1054
1055         return 0;
1056 }
1057 late_initcall(sr_init);
1058
1059 static void __exit sr_exit(void)
1060 {
1061         platform_driver_unregister(&smartreflex_driver);
1062 }
1063 module_exit(sr_exit);
1064
1065 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1066 MODULE_LICENSE("GPL");
1067 MODULE_ALIAS("platform:" DRIVER_NAME);
1068 MODULE_AUTHOR("Texas Instruments Inc");