blob: fa0f19b975a6a59cd5532e7091052ec194dd14b2 [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 *
Jean Pihet21ff63a2012-04-25 16:43:17 +05306 * Copyright (C) 2012 Texas Instruments, Inc.
Thara Gopinath984aa6d2010-05-29 22:02:22 +05307 * 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
Tony Lindgrena1bcc1d2011-11-07 12:27:10 -080020#include <linux/module.h>
Thara Gopinath984aa6d2010-05-29 22:02:22 +053021#include <linux/interrupt.h>
22#include <linux/clk.h>
23#include <linux/io.h>
24#include <linux/debugfs.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/pm_runtime.h>
Jean Pihetb86aeaf2012-04-25 16:06:20 +053028#include <linux/power/smartreflex.h>
Thara Gopinath984aa6d2010-05-29 22:02:22 +053029
Andrii Tseglytskyi33da2822013-05-30 13:08:36 +030030#define DRIVER_NAME "smartreflex"
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +030031#define SMARTREFLEX_NAME_LEN 32
Thara Gopinath077fcec2010-10-27 20:29:37 +053032#define NVALUE_NAME_LEN 40
Thara Gopinath984aa6d2010-05-29 22:02:22 +053033#define SR_DISABLE_TIMEOUT 200
34
Thara Gopinath984aa6d2010-05-29 22:02:22 +053035/* sr_list contains all the instances of smartreflex module */
36static LIST_HEAD(sr_list);
37
38static struct omap_sr_class_data *sr_class;
39static struct omap_sr_pmic_data *sr_pmic_data;
Kevin Hilman633ef8b2011-04-05 14:39:11 -070040static struct dentry *sr_dbg_dir;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053041
42static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
43{
44 __raw_writel(value, (sr->base + offset));
45}
46
47static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
48 u32 value)
49{
50 u32 reg_val;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053051
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 */
Nishanth Menonade6ec02012-02-29 23:33:41 +010061 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;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053065
Nishanth Menonade6ec02012-02-29 23:33:41 +010066 reg_val = __raw_readl(sr->base + offset);
67 reg_val &= ~mask;
68
69 value &= mask;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053070
71 reg_val |= value;
72
73 __raw_writel(reg_val, (sr->base + offset));
74}
75
76static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
77{
78 return __raw_readl(sr->base + offset);
79}
80
81static 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
98static irqreturn_t sr_interrupt(int irq, void *data)
99{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100100 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530101 u32 status = 0;
102
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100103 switch (sr_info->ip_type) {
104 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530105 /* 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);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100110 break;
111 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530112 /* Read the status bits */
Felipe Balbi5a4f1842011-11-23 14:43:37 -0800113 status = sr_read_reg(sr_info, IRQSTATUS);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530114
115 /* Clear them by writing back */
116 sr_write_reg(sr_info, IRQSTATUS, status);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100117 break;
118 default:
119 dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
120 sr_info->ip_type);
121 return IRQ_NONE;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530122 }
123
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530124 if (sr_class->notify)
Jean Pihet80821c92012-04-24 10:22:12 +0530125 sr_class->notify(sr_info, status);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530126
127 return IRQ_HANDLED;
128}
129
130static void sr_set_clk_length(struct omap_sr *sr)
131{
Jean Pihet98aed082012-10-04 18:47:11 +0200132 struct clk *fck;
133 u32 fclk_speed;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530134
Jean Pihet98aed082012-10-04 18:47:11 +0200135 fck = clk_get(&sr->pdev->dev, "fck");
Thara Gopinathb35cecf2010-08-18 12:23:12 +0530136
Jean Pihet98aed082012-10-04 18:47:11 +0200137 if (IS_ERR(fck)) {
138 dev_err(&sr->pdev->dev, "%s: unable to get fck for device %s\n",
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700139 __func__, dev_name(&sr->pdev->dev));
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530140 return;
141 }
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100142
Jean Pihet98aed082012-10-04 18:47:11 +0200143 fclk_speed = clk_get_rate(fck);
144 clk_put(fck);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530145
Jean Pihet98aed082012-10-04 18:47:11 +0200146 switch (fclk_speed) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530147 case 12000000:
148 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
149 break;
150 case 13000000:
151 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
152 break;
153 case 19200000:
154 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
155 break;
156 case 26000000:
157 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
158 break;
159 case 38400000:
160 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
161 break;
162 default:
Jean Pihet98aed082012-10-04 18:47:11 +0200163 dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
164 __func__, fclk_speed);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530165 break;
166 }
167}
168
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530169static void sr_start_vddautocomp(struct omap_sr *sr)
170{
171 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
172 dev_warn(&sr->pdev->dev,
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700173 "%s: smartreflex class driver not registered\n",
174 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530175 return;
176 }
177
Jean Pihet80821c92012-04-24 10:22:12 +0530178 if (!sr_class->enable(sr))
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530179 sr->autocomp_active = true;
180}
181
182static void sr_stop_vddautocomp(struct omap_sr *sr)
183{
184 if (!sr_class || !(sr_class->disable)) {
185 dev_warn(&sr->pdev->dev,
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700186 "%s: smartreflex class driver not registered\n",
187 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530188 return;
189 }
190
191 if (sr->autocomp_active) {
Jean Pihet80821c92012-04-24 10:22:12 +0530192 sr_class->disable(sr, 1);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530193 sr->autocomp_active = false;
194 }
195}
196
197/*
198 * This function handles the intializations which have to be done
199 * only when both sr device and class driver regiter has
200 * completed. This will be attempted to be called from both sr class
201 * driver register and sr device intializtion API's. Only one call
202 * will ultimately succeed.
203 *
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +0300204 * Currently this function registers interrupt handler for a particular SR
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530205 * if smartreflex class driver is already registered and has
206 * requested for interrupts and the SR interrupt line in present.
207 */
208static int sr_late_init(struct omap_sr *sr_info)
209{
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530210 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530211 int ret = 0;
212
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530213 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300214 ret = devm_request_irq(&sr_info->pdev->dev, sr_info->irq,
215 sr_interrupt, 0, sr_info->name, sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530216 if (ret)
217 goto error;
Nishanth Menon1279ba52011-02-14 12:41:10 +0530218 disable_irq(sr_info->irq);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530219 }
220
221 if (pdata && pdata->enable_on_init)
222 sr_start_vddautocomp(sr_info);
223
224 return ret;
225
226error:
Nishanth Menon442155a2011-02-14 12:33:13 +0530227 list_del(&sr_info->node);
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700228 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering interrupt handler. Smartreflex will not function as desired\n",
229 __func__);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100230
Nishanth Menon442155a2011-02-14 12:33:13 +0530231 return ret;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530232}
233
234static void sr_v1_disable(struct omap_sr *sr)
235{
236 int timeout = 0;
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100237 int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
238 ERRCONFIG_MCUBOUNDINTST;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530239
240 /* Enable MCUDisableAcknowledge interrupt */
241 sr_modify_reg(sr, ERRCONFIG_V1,
242 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
243
244 /* SRCONFIG - disable SR */
245 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
246
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100247 /* Disable all other SR interrupts and clear the status as needed */
248 if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
249 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530250 sr_modify_reg(sr, ERRCONFIG_V1,
251 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
252 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100253 errconf_val);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530254
255 /*
256 * Wait for SR to be disabled.
257 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
258 */
Jean Pihet50e4a7d2012-04-24 10:56:40 +0530259 sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
260 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
261 timeout);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530262
263 if (timeout >= SR_DISABLE_TIMEOUT)
264 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700265 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530266
267 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
268 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
269 ERRCONFIG_MCUDISACKINTST);
270}
271
272static void sr_v2_disable(struct omap_sr *sr)
273{
274 int timeout = 0;
275
276 /* Enable MCUDisableAcknowledge interrupt */
277 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
278
279 /* SRCONFIG - disable SR */
280 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
281
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100282 /*
283 * Disable all other SR interrupts and clear the status
284 * write to status register ONLY on need basis - only if status
285 * is set.
286 */
287 if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
288 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530289 ERRCONFIG_VPBOUNDINTST_V2);
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100290 else
291 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
292 0x0);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530293 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
294 IRQENABLE_MCUVALIDINT |
295 IRQENABLE_MCUBOUNDSINT));
296 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
297 IRQSTATUS_MCVALIDINT |
298 IRQSTATUS_MCBOUNDSINT));
299
300 /*
301 * Wait for SR to be disabled.
302 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
303 */
Jean Pihet50e4a7d2012-04-24 10:56:40 +0530304 sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
305 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
306 timeout);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530307
308 if (timeout >= SR_DISABLE_TIMEOUT)
309 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700310 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530311
312 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
313 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
314 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
315}
316
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530317static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
318 struct omap_sr *sr, u32 efuse_offs)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530319{
320 int i;
321
322 if (!sr->nvalue_table) {
323 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700324 __func__);
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530325 return NULL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530326 }
327
328 for (i = 0; i < sr->nvalue_count; i++) {
329 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530330 return &sr->nvalue_table[i];
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530331 }
332
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530333 return NULL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530334}
335
336/* Public Functions */
337
338/**
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300339 * sr_configure_errgen() - Configures the SmartReflex to perform AVS using the
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530340 * error generator module.
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300341 * @sr: SR module to be configured.
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530342 *
343 * This API is to be called from the smartreflex class driver to
344 * configure the error generator module inside the smartreflex module.
345 * SR settings if using the ERROR module inside Smartreflex.
346 * SR CLASS 3 by default uses only the ERROR module where as
347 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
348 * module. Returns 0 on success and error value in case of failure.
349 */
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300350int sr_configure_errgen(struct omap_sr *sr)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530351{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100352 u32 sr_config, sr_errconfig, errconfig_offs;
353 u32 vpboundint_en, vpboundint_st;
354 u32 senp_en = 0, senn_en = 0;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530355 u8 senp_shift, senn_shift;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530356
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300357 if (!sr) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700358 pr_warn("%s: NULL omap_sr from %pF\n",
359 __func__, (void *)_RET_IP_);
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300360 return -EINVAL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530361 }
362
363 if (!sr->clk_length)
364 sr_set_clk_length(sr);
365
366 senp_en = sr->senp_mod;
367 senn_en = sr->senn_mod;
368
369 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
370 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
371
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100372 switch (sr->ip_type) {
373 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530374 sr_config |= SRCONFIG_DELAYCTRL;
375 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
376 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
377 errconfig_offs = ERRCONFIG_V1;
378 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
379 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100380 break;
381 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530382 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
383 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
384 errconfig_offs = ERRCONFIG_V2;
385 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
386 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100387 break;
388 default:
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700389 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
390 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530391 return -EINVAL;
392 }
393
394 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
395 sr_write_reg(sr, SRCONFIG, sr_config);
396 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
397 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
398 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
399 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
400 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
401 sr_errconfig);
402
403 /* Enabling the interrupts if the ERROR module is used */
Nishanth Menon74754cc2012-02-29 23:33:38 +0100404 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
405 vpboundint_en);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530406
407 return 0;
408}
409
410/**
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100411 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300412 * @sr: SR module to be configured.
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100413 *
414 * This API is to be called from the smartreflex class driver to
415 * disable the error generator module inside the smartreflex module.
416 *
417 * Returns 0 on success and error value in case of failure.
418 */
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300419int sr_disable_errgen(struct omap_sr *sr)
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100420{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100421 u32 errconfig_offs;
422 u32 vpboundint_en, vpboundint_st;
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100423
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300424 if (!sr) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700425 pr_warn("%s: NULL omap_sr from %pF\n",
426 __func__, (void *)_RET_IP_);
Andrii Tseglytskyi3dfc35f2013-05-27 14:09:22 +0300427 return -EINVAL;
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100428 }
429
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100430 switch (sr->ip_type) {
431 case SR_TYPE_V1:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100432 errconfig_offs = ERRCONFIG_V1;
433 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
434 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100435 break;
436 case SR_TYPE_V2:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100437 errconfig_offs = ERRCONFIG_V2;
438 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
439 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100440 break;
441 default:
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700442 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
443 __func__);
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100444 return -EINVAL;
445 }
446
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100447 /* Disable the Sensor and errorgen */
448 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
449
Nishanth Menonefe4e062013-05-30 13:08:34 +0300450 /*
451 * Disable the interrupts of ERROR module
452 * NOTE: modify is a read, modify,write - an implicit OCP barrier
453 * which is required is present here - sequencing is critical
454 * at this point (after errgen is disabled, vpboundint disable)
455 */
456 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
457
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100458 return 0;
459}
460
461/**
Andrii Tseglytskyi6c805732013-05-27 14:09:23 +0300462 * sr_configure_minmax() - Configures the SmartReflex to perform AVS using the
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530463 * minmaxavg module.
Andrii Tseglytskyi6c805732013-05-27 14:09:23 +0300464 * @sr: SR module to be configured.
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530465 *
466 * This API is to be called from the smartreflex class driver to
467 * configure the minmaxavg module inside the smartreflex module.
468 * SR settings if using the ERROR module inside Smartreflex.
469 * SR CLASS 3 by default uses only the ERROR module where as
470 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
471 * module. Returns 0 on success and error value in case of failure.
472 */
Andrii Tseglytskyi6c805732013-05-27 14:09:23 +0300473int sr_configure_minmax(struct omap_sr *sr)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530474{
475 u32 sr_config, sr_avgwt;
476 u32 senp_en = 0, senn_en = 0;
477 u8 senp_shift, senn_shift;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530478
Andrii Tseglytskyi6c805732013-05-27 14:09:23 +0300479 if (!sr) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700480 pr_warn("%s: NULL omap_sr from %pF\n",
481 __func__, (void *)_RET_IP_);
Andrii Tseglytskyi6c805732013-05-27 14:09:23 +0300482 return -EINVAL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530483 }
484
485 if (!sr->clk_length)
486 sr_set_clk_length(sr);
487
488 senp_en = sr->senp_mod;
489 senn_en = sr->senn_mod;
490
491 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
492 SRCONFIG_SENENABLE |
493 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
494
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100495 switch (sr->ip_type) {
496 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530497 sr_config |= SRCONFIG_DELAYCTRL;
498 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
499 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100500 break;
501 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530502 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
503 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100504 break;
505 default:
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700506 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
507 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530508 return -EINVAL;
509 }
510
511 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
512 sr_write_reg(sr, SRCONFIG, sr_config);
513 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
514 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
515 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
516
517 /*
518 * Enabling the interrupts if MINMAXAVG module is used.
519 * TODO: check if all the interrupts are mandatory
520 */
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100521 switch (sr->ip_type) {
522 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530523 sr_modify_reg(sr, ERRCONFIG_V1,
524 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
525 ERRCONFIG_MCUBOUNDINTEN),
526 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
527 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
528 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100529 break;
530 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530531 sr_write_reg(sr, IRQSTATUS,
532 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
533 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
534 sr_write_reg(sr, IRQENABLE_SET,
535 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
536 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100537 break;
538 default:
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700539 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
540 __func__);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100541 return -EINVAL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530542 }
543
544 return 0;
545}
546
547/**
548 * sr_enable() - Enables the smartreflex module.
Andrii Tseglytskyi299066b2013-05-27 14:09:24 +0300549 * @sr: pointer to which the SR module to be configured belongs to.
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530550 * @volt: The voltage at which the Voltage domain associated with
551 * the smartreflex module is operating at.
552 * This is required only to program the correct Ntarget value.
553 *
554 * This API is to be called from the smartreflex class driver to
555 * enable a smartreflex module. Returns 0 on success. Returns error
556 * value if the voltage passed is wrong or if ntarget value is wrong.
557 */
Andrii Tseglytskyi299066b2013-05-27 14:09:24 +0300558int sr_enable(struct omap_sr *sr, unsigned long volt)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530559{
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530560 struct omap_volt_data *volt_data;
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530561 struct omap_sr_nvalue_table *nvalue_row;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530562 int ret;
563
Andrii Tseglytskyi299066b2013-05-27 14:09:24 +0300564 if (!sr) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700565 pr_warn("%s: NULL omap_sr from %pF\n",
566 __func__, (void *)_RET_IP_);
Andrii Tseglytskyi299066b2013-05-27 14:09:24 +0300567 return -EINVAL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530568 }
569
570 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
571
572 if (IS_ERR(volt_data)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700573 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table for nominal voltage %ld\n",
574 __func__, volt);
Jean Pihet63371fa2012-02-29 23:33:49 +0100575 return PTR_ERR(volt_data);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530576 }
577
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530578 nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530579
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530580 if (!nvalue_row) {
581 dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
582 __func__, volt);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530583 return -ENODATA;
584 }
585
586 /* errminlimit is opp dependent and hence linked to voltage */
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530587 sr->err_minlimit = nvalue_row->errminlimit;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530588
589 pm_runtime_get_sync(&sr->pdev->dev);
590
591 /* Check if SR is already enabled. If yes do nothing */
592 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
593 return 0;
594
595 /* Configure SR */
Jean Pihet80821c92012-04-24 10:22:12 +0530596 ret = sr_class->configure(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530597 if (ret)
598 return ret;
599
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530600 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530601
602 /* SRCONFIG - enable SR */
603 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
604 return 0;
605}
606
607/**
608 * sr_disable() - Disables the smartreflex module.
Andrii Tseglytskyi299066b2013-05-27 14:09:24 +0300609 * @sr: pointer to which the SR module to be configured belongs to.
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530610 *
611 * This API is to be called from the smartreflex class driver to
612 * disable a smartreflex module.
613 */
Andrii Tseglytskyi299066b2013-05-27 14:09:24 +0300614void sr_disable(struct omap_sr *sr)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530615{
Andrii Tseglytskyi299066b2013-05-27 14:09:24 +0300616 if (!sr) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700617 pr_warn("%s: NULL omap_sr from %pF\n",
618 __func__, (void *)_RET_IP_);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530619 return;
620 }
621
622 /* Check if SR clocks are already disabled. If yes do nothing */
623 if (pm_runtime_suspended(&sr->pdev->dev))
624 return;
625
626 /*
627 * Disable SR if only it is indeed enabled. Else just
628 * disable the clocks.
629 */
630 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100631 switch (sr->ip_type) {
632 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530633 sr_v1_disable(sr);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100634 break;
635 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530636 sr_v2_disable(sr);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100637 break;
638 default:
639 dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
640 sr->ip_type);
641 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530642 }
643
Colin Cross98333b32011-07-22 00:55:52 -0500644 pm_runtime_put_sync_suspend(&sr->pdev->dev);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530645}
646
647/**
648 * sr_register_class() - API to register a smartreflex class parameters.
649 * @class_data: The structure containing various sr class specific data.
650 *
651 * This API is to be called by the smartreflex class driver to register itself
652 * with the smartreflex driver during init. Returns 0 on success else the
653 * error value.
654 */
655int sr_register_class(struct omap_sr_class_data *class_data)
656{
657 struct omap_sr *sr_info;
658
659 if (!class_data) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700660 pr_warn("%s:, Smartreflex class data passed is NULL\n",
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530661 __func__);
662 return -EINVAL;
663 }
664
665 if (sr_class) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700666 pr_warn("%s: Smartreflex class driver already registered\n",
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530667 __func__);
668 return -EBUSY;
669 }
670
671 sr_class = class_data;
672
673 /*
674 * Call into late init to do intializations that require
675 * both sr driver and sr class driver to be initiallized.
676 */
677 list_for_each_entry(sr_info, &sr_list, node)
678 sr_late_init(sr_info);
679
680 return 0;
681}
682
683/**
684 * omap_sr_enable() - API to enable SR clocks and to call into the
685 * registered smartreflex class enable API.
686 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
687 *
688 * This API is to be called from the kernel in order to enable
689 * a particular smartreflex module. This API will do the initial
690 * configurations to turn on the smartreflex module and in turn call
691 * into the registered smartreflex class enable API.
692 */
693void omap_sr_enable(struct voltagedomain *voltdm)
694{
695 struct omap_sr *sr = _sr_lookup(voltdm);
696
697 if (IS_ERR(sr)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700698 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530699 return;
700 }
701
702 if (!sr->autocomp_active)
703 return;
704
705 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700706 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
707 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530708 return;
709 }
710
Jean Pihet80821c92012-04-24 10:22:12 +0530711 sr_class->enable(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530712}
713
714/**
715 * omap_sr_disable() - API to disable SR without resetting the voltage
716 * processor voltage
717 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
718 *
719 * This API is to be called from the kernel in order to disable
720 * a particular smartreflex module. This API will in turn call
721 * into the registered smartreflex class disable API. This API will tell
722 * the smartreflex class disable not to reset the VP voltage after
723 * disabling smartreflex.
724 */
725void omap_sr_disable(struct voltagedomain *voltdm)
726{
727 struct omap_sr *sr = _sr_lookup(voltdm);
728
729 if (IS_ERR(sr)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700730 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530731 return;
732 }
733
734 if (!sr->autocomp_active)
735 return;
736
737 if (!sr_class || !(sr_class->disable)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700738 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
739 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530740 return;
741 }
742
Jean Pihet80821c92012-04-24 10:22:12 +0530743 sr_class->disable(sr, 0);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530744}
745
746/**
747 * omap_sr_disable_reset_volt() - API to disable SR and reset the
748 * voltage processor voltage
749 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
750 *
751 * This API is to be called from the kernel in order to disable
752 * a particular smartreflex module. This API will in turn call
753 * into the registered smartreflex class disable API. This API will tell
754 * the smartreflex class disable to reset the VP voltage after
755 * disabling smartreflex.
756 */
757void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
758{
759 struct omap_sr *sr = _sr_lookup(voltdm);
760
761 if (IS_ERR(sr)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700762 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530763 return;
764 }
765
766 if (!sr->autocomp_active)
767 return;
768
769 if (!sr_class || !(sr_class->disable)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700770 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
771 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530772 return;
773 }
774
Jean Pihet80821c92012-04-24 10:22:12 +0530775 sr_class->disable(sr, 1);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530776}
777
778/**
779 * omap_sr_register_pmic() - API to register pmic specific info.
780 * @pmic_data: The structure containing pmic specific data.
781 *
782 * This API is to be called from the PMIC specific code to register with
783 * smartreflex driver pmic specific info. Currently the only info required
784 * is the smartreflex init on the PMIC side.
785 */
786void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
787{
788 if (!pmic_data) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700789 pr_warn("%s: Trying to register NULL PMIC data structure with smartreflex\n",
790 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530791 return;
792 }
793
794 sr_pmic_data = pmic_data;
795}
796
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100797/* PM Debug FS entries to enable and disable smartreflex. */
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530798static int omap_sr_autocomp_show(void *data, u64 *val)
799{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100800 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530801
802 if (!sr_info) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700803 pr_warn("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530804 return -EINVAL;
805 }
806
807 *val = sr_info->autocomp_active;
808
809 return 0;
810}
811
812static int omap_sr_autocomp_store(void *data, u64 val)
813{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100814 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530815
816 if (!sr_info) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700817 pr_warn("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530818 return -EINVAL;
819 }
820
821 /* Sanity check */
Felipe Balbid6173692012-02-29 23:33:47 +0100822 if (val > 1) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700823 pr_warn("%s: Invalid argument %lld\n", __func__, val);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530824 return -EINVAL;
825 }
826
Nishanth Menonac77a6f2011-02-14 21:14:17 +0530827 /* control enable/disable only if there is a delta in value */
828 if (sr_info->autocomp_active != val) {
829 if (!val)
830 sr_stop_vddautocomp(sr_info);
831 else
832 sr_start_vddautocomp(sr_info);
833 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530834
835 return 0;
836}
837
838DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100839 omap_sr_autocomp_store, "%llu\n");
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530840
841static int __init omap_sr_probe(struct platform_device *pdev)
842{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100843 struct omap_sr *sr_info;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530844 struct omap_sr_data *pdata = pdev->dev.platform_data;
845 struct resource *mem, *irq;
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700846 struct dentry *nvalue_dir;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530847 int i, ret = 0;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530848
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300849 sr_info = devm_kzalloc(&pdev->dev, sizeof(struct omap_sr), GFP_KERNEL);
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700850 if (!sr_info)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530851 return -ENOMEM;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530852
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300853 sr_info->name = devm_kzalloc(&pdev->dev,
854 SMARTREFLEX_NAME_LEN, GFP_KERNEL);
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700855 if (!sr_info->name)
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300856 return -ENOMEM;
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300857
Felipe Balbi1079a8b2012-02-29 23:33:44 +0100858 platform_set_drvdata(pdev, sr_info);
859
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530860 if (!pdata) {
861 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300862 return -EINVAL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530863 }
864
865 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300866 sr_info->base = devm_ioremap_resource(&pdev->dev, mem);
867 if (IS_ERR(sr_info->base)) {
868 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
869 return PTR_ERR(sr_info->base);
Aaro Koskinenda9e73922011-04-26 02:25:16 -0700870 }
871
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530872 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
873
874 pm_runtime_enable(&pdev->dev);
Nishanth Menone13d8f32011-07-09 14:37:21 -0700875 pm_runtime_irq_safe(&pdev->dev);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530876
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300877 snprintf(sr_info->name, SMARTREFLEX_NAME_LEN, "%s", pdata->name);
Jean Pihet8b765d72012-04-24 10:41:27 +0530878
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530879 sr_info->pdev = pdev;
880 sr_info->srid = pdev->id;
881 sr_info->voltdm = pdata->voltdm;
882 sr_info->nvalue_table = pdata->nvalue_table;
883 sr_info->nvalue_count = pdata->nvalue_count;
884 sr_info->senn_mod = pdata->senn_mod;
885 sr_info->senp_mod = pdata->senp_mod;
Jean Pihet98aed082012-10-04 18:47:11 +0200886 sr_info->err_weight = pdata->err_weight;
887 sr_info->err_maxlimit = pdata->err_maxlimit;
888 sr_info->accum_data = pdata->accum_data;
889 sr_info->senn_avgweight = pdata->senn_avgweight;
890 sr_info->senp_avgweight = pdata->senp_avgweight;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530891 sr_info->autocomp_active = false;
892 sr_info->ip_type = pdata->ip_type;
Jean Pihet98aed082012-10-04 18:47:11 +0200893
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530894 if (irq)
895 sr_info->irq = irq->start;
896
897 sr_set_clk_length(sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530898
899 list_add(&sr_info->node, &sr_list);
900
901 /*
902 * Call into late init to do intializations that require
903 * both sr driver and sr class driver to be initiallized.
904 */
905 if (sr_class) {
906 ret = sr_late_init(sr_info);
907 if (ret) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700908 pr_warn("%s: Error in SR late init\n", __func__);
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300909 goto err_list_del;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530910 }
911 }
912
913 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700914 if (!sr_dbg_dir) {
915 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100916 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700917 ret = PTR_ERR(sr_dbg_dir);
918 pr_err("%s:sr debugfs dir creation failed(%d)\n",
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700919 __func__, ret);
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300920 goto err_list_del;
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700921 }
Shweta Gulatib3329a32011-02-15 13:40:30 +0530922 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530923
Jean Pihet8b765d72012-04-24 10:41:27 +0530924 sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100925 if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530926 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
927 __func__);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530928 ret = PTR_ERR(sr_info->dbg_dir);
Jean Pihetce3810c2012-09-24 16:16:40 +0200929 goto err_debugfs;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530930 }
931
Anand S Sawantb1ace382011-02-17 21:27:30 +0530932 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
933 sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
934 (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530935 &sr_info->err_weight);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530936 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530937 &sr_info->err_maxlimit);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530938
Anand S Sawantb1ace382011-02-17 21:27:30 +0530939 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100940 if (IS_ERR_OR_NULL(nvalue_dir)) {
Joe Perchese2cdeaa2016-08-19 09:15:50 -0700941 dev_err(&pdev->dev, "%s: Unable to create debugfs directory for n-values\n",
942 __func__);
Shweta Gulatib3329a32011-02-15 13:40:30 +0530943 ret = PTR_ERR(nvalue_dir);
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700944 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530945 }
946
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530947 if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
948 dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
949 __func__, sr_info->name);
950
Shweta Gulatib3329a32011-02-15 13:40:30 +0530951 ret = -ENODATA;
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700952 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530953 }
954
955 for (i = 0; i < sr_info->nvalue_count; i++) {
Aaro Koskinen865212ab2011-02-07 16:08:04 +0200956 char name[NVALUE_NAME_LEN + 1];
Thara Gopinath077fcec2010-10-27 20:29:37 +0530957
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530958 snprintf(name, sizeof(name), "volt_%lu",
959 sr_info->nvalue_table[i].volt_nominal);
Vasiliy Kulikov1232a182011-02-04 12:23:20 +0000960 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530961 &(sr_info->nvalue_table[i].nvalue));
Jean Pihet308d1bd2012-04-24 23:41:54 +0530962 snprintf(name, sizeof(name), "errminlimit_%lu",
963 sr_info->nvalue_table[i].volt_nominal);
964 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
965 &(sr_info->nvalue_table[i].errminlimit));
966
Thara Gopinath077fcec2010-10-27 20:29:37 +0530967 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530968
969 return ret;
970
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700971err_debugfs:
972 debugfs_remove_recursive(sr_info->dbg_dir);
Andrii Tseglytskyiefca4062013-05-30 13:43:56 +0300973err_list_del:
Aaro Koskinen833d78f2011-04-26 02:25:27 -0700974 list_del(&sr_info->node);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530975 return ret;
976}
977
Bill Pemberton415ec692012-11-19 13:26:07 -0500978static int omap_sr_remove(struct platform_device *pdev)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530979{
980 struct omap_sr_data *pdata = pdev->dev.platform_data;
981 struct omap_sr *sr_info;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530982
983 if (!pdata) {
984 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
985 return -EINVAL;
986 }
987
988 sr_info = _sr_lookup(pdata->voltdm);
Julia Lawall28693ec2011-01-24 20:55:22 +0100989 if (IS_ERR(sr_info)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530990 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
991 __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100992 return PTR_ERR(sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530993 }
994
995 if (sr_info->autocomp_active)
996 sr_stop_vddautocomp(sr_info);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530997 if (sr_info->dbg_dir)
998 debugfs_remove_recursive(sr_info->dbg_dir);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530999
Andrii Tseglytskyibd4a36b2013-05-30 13:08:35 +03001000 pm_runtime_disable(&pdev->dev);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301001 list_del(&sr_info->node);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301002 return 0;
1003}
1004
Bill Pemberton415ec692012-11-19 13:26:07 -05001005static void omap_sr_shutdown(struct platform_device *pdev)
Nishanth Menon1f55bc1852012-03-01 00:29:44 +01001006{
1007 struct omap_sr_data *pdata = pdev->dev.platform_data;
1008 struct omap_sr *sr_info;
1009
1010 if (!pdata) {
1011 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1012 return;
1013 }
1014
1015 sr_info = _sr_lookup(pdata->voltdm);
1016 if (IS_ERR(sr_info)) {
1017 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1018 __func__);
1019 return;
1020 }
1021
1022 if (sr_info->autocomp_active)
1023 sr_stop_vddautocomp(sr_info);
1024
1025 return;
1026}
1027
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301028static struct platform_driver smartreflex_driver = {
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001029 .remove = omap_sr_remove,
1030 .shutdown = omap_sr_shutdown,
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301031 .driver = {
Andrii Tseglytskyi33da2822013-05-30 13:08:36 +03001032 .name = DRIVER_NAME,
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301033 },
1034};
1035
1036static int __init sr_init(void)
1037{
1038 int ret = 0;
1039
1040 /*
1041 * sr_init is a late init. If by then a pmic specific API is not
1042 * registered either there is no need for anything to be done on
1043 * the PMIC side or somebody has forgotten to register a PMIC
1044 * handler. Warn for the second condition.
1045 */
1046 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1047 sr_pmic_data->sr_pmic_init();
1048 else
Joe Perchese2cdeaa2016-08-19 09:15:50 -07001049 pr_warn("%s: No PMIC hook to init smartreflex\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301050
1051 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1052 if (ret) {
1053 pr_err("%s: platform driver register failed for SR\n",
Joe Perchese2cdeaa2016-08-19 09:15:50 -07001054 __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301055 return ret;
1056 }
1057
1058 return 0;
1059}
Felipe Balbi1a21a682012-02-29 23:33:45 +01001060late_initcall(sr_init);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301061
1062static void __exit sr_exit(void)
1063{
1064 platform_driver_unregister(&smartreflex_driver);
1065}
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301066module_exit(sr_exit);
1067
1068MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1069MODULE_LICENSE("GPL");
1070MODULE_ALIAS("platform:" DRIVER_NAME);
1071MODULE_AUTHOR("Texas Instruments Inc");