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