blob: f34d34d46fc147ffb048301bacd33031ee482d77 [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
Thara Gopinath984aa6d2010-05-29 22:02:22 +053030#define SMARTREFLEX_NAME_LEN 16
Thara Gopinath077fcec2010-10-27 20:29:37 +053031#define NVALUE_NAME_LEN 40
Thara Gopinath984aa6d2010-05-29 22:02:22 +053032#define SR_DISABLE_TIMEOUT 200
33
Thara Gopinath984aa6d2010-05-29 22:02:22 +053034/* sr_list contains all the instances of smartreflex module */
35static LIST_HEAD(sr_list);
36
37static struct omap_sr_class_data *sr_class;
38static struct omap_sr_pmic_data *sr_pmic_data;
Kevin Hilman633ef8b2011-04-05 14:39:11 -070039static struct dentry *sr_dbg_dir;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053040
41static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
42{
43 __raw_writel(value, (sr->base + offset));
44}
45
46static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
47 u32 value)
48{
49 u32 reg_val;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053050
51 /*
52 * Smartreflex error config register is special as it contains
53 * certain status bits which if written a 1 into means a clear
54 * of those bits. So in order to make sure no accidental write of
55 * 1 happens to those status bits, do a clear of them in the read
56 * value. This mean this API doesn't rewrite values in these bits
57 * if they are currently set, but does allow the caller to write
58 * those bits.
59 */
Nishanth Menonade6ec02012-02-29 23:33:41 +010060 if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
61 mask |= ERRCONFIG_STATUS_V1_MASK;
62 else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
63 mask |= ERRCONFIG_VPBOUNDINTST_V2;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053064
Nishanth Menonade6ec02012-02-29 23:33:41 +010065 reg_val = __raw_readl(sr->base + offset);
66 reg_val &= ~mask;
67
68 value &= mask;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053069
70 reg_val |= value;
71
72 __raw_writel(reg_val, (sr->base + offset));
73}
74
75static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
76{
77 return __raw_readl(sr->base + offset);
78}
79
80static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
81{
82 struct omap_sr *sr_info;
83
84 if (!voltdm) {
85 pr_err("%s: Null voltage domain passed!\n", __func__);
86 return ERR_PTR(-EINVAL);
87 }
88
89 list_for_each_entry(sr_info, &sr_list, node) {
90 if (voltdm == sr_info->voltdm)
91 return sr_info;
92 }
93
94 return ERR_PTR(-ENODATA);
95}
96
97static irqreturn_t sr_interrupt(int irq, void *data)
98{
Felipe Balbi4018bfe2012-02-29 23:33:46 +010099 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530100 u32 status = 0;
101
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100102 switch (sr_info->ip_type) {
103 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530104 /* Read the status bits */
105 status = sr_read_reg(sr_info, ERRCONFIG_V1);
106
107 /* Clear them by writing back */
108 sr_write_reg(sr_info, ERRCONFIG_V1, status);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100109 break;
110 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530111 /* Read the status bits */
Felipe Balbi5a4f1842011-11-23 14:43:37 -0800112 status = sr_read_reg(sr_info, IRQSTATUS);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530113
114 /* Clear them by writing back */
115 sr_write_reg(sr_info, IRQSTATUS, status);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100116 break;
117 default:
118 dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
119 sr_info->ip_type);
120 return IRQ_NONE;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530121 }
122
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530123 if (sr_class->notify)
Jean Pihet80821c92012-04-24 10:22:12 +0530124 sr_class->notify(sr_info, status);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530125
126 return IRQ_HANDLED;
127}
128
129static void sr_set_clk_length(struct omap_sr *sr)
130{
Jean Pihet98aed082012-10-04 18:47:11 +0200131 struct clk *fck;
132 u32 fclk_speed;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530133
Jean Pihet98aed082012-10-04 18:47:11 +0200134 fck = clk_get(&sr->pdev->dev, "fck");
Thara Gopinathb35cecf2010-08-18 12:23:12 +0530135
Jean Pihet98aed082012-10-04 18:47:11 +0200136 if (IS_ERR(fck)) {
137 dev_err(&sr->pdev->dev, "%s: unable to get fck for device %s\n",
138 __func__, dev_name(&sr->pdev->dev));
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530139 return;
140 }
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100141
Jean Pihet98aed082012-10-04 18:47:11 +0200142 fclk_speed = clk_get_rate(fck);
143 clk_put(fck);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530144
Jean Pihet98aed082012-10-04 18:47:11 +0200145 switch (fclk_speed) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530146 case 12000000:
147 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
148 break;
149 case 13000000:
150 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
151 break;
152 case 19200000:
153 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
154 break;
155 case 26000000:
156 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
157 break;
158 case 38400000:
159 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
160 break;
161 default:
Jean Pihet98aed082012-10-04 18:47:11 +0200162 dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
163 __func__, fclk_speed);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530164 break;
165 }
166}
167
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530168static void sr_start_vddautocomp(struct omap_sr *sr)
169{
170 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
171 dev_warn(&sr->pdev->dev,
172 "%s: smartreflex class driver not registered\n",
173 __func__);
174 return;
175 }
176
Jean Pihet80821c92012-04-24 10:22:12 +0530177 if (!sr_class->enable(sr))
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530178 sr->autocomp_active = true;
179}
180
181static void sr_stop_vddautocomp(struct omap_sr *sr)
182{
183 if (!sr_class || !(sr_class->disable)) {
184 dev_warn(&sr->pdev->dev,
185 "%s: smartreflex class driver not registered\n",
186 __func__);
187 return;
188 }
189
190 if (sr->autocomp_active) {
Jean Pihet80821c92012-04-24 10:22:12 +0530191 sr_class->disable(sr, 1);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530192 sr->autocomp_active = false;
193 }
194}
195
196/*
197 * This function handles the intializations which have to be done
198 * only when both sr device and class driver regiter has
199 * completed. This will be attempted to be called from both sr class
200 * driver register and sr device intializtion API's. Only one call
201 * will ultimately succeed.
202 *
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +0300203 * Currently this function registers interrupt handler for a particular SR
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530204 * if smartreflex class driver is already registered and has
205 * requested for interrupts and the SR interrupt line in present.
206 */
207static int sr_late_init(struct omap_sr *sr_info)
208{
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530209 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
210 struct resource *mem;
211 int ret = 0;
212
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530213 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530214 ret = request_irq(sr_info->irq, sr_interrupt,
Jean Pihet8b765d72012-04-24 10:41:27 +0530215 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 iounmap(sr_info->base);
228 mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
229 release_mem_region(mem->start, resource_size(mem));
230 list_del(&sr_info->node);
231 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
232 "interrupt handler. Smartreflex will"
233 "not function as desired\n", __func__);
Nishanth Menon442155a2011-02-14 12:33:13 +0530234 kfree(sr_info);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100235
Nishanth Menon442155a2011-02-14 12:33:13 +0530236 return ret;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530237}
238
239static void sr_v1_disable(struct omap_sr *sr)
240{
241 int timeout = 0;
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100242 int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
243 ERRCONFIG_MCUBOUNDINTST;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530244
245 /* Enable MCUDisableAcknowledge interrupt */
246 sr_modify_reg(sr, ERRCONFIG_V1,
247 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
248
249 /* SRCONFIG - disable SR */
250 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
251
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100252 /* Disable all other SR interrupts and clear the status as needed */
253 if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
254 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530255 sr_modify_reg(sr, ERRCONFIG_V1,
256 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
257 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100258 errconf_val);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530259
260 /*
261 * Wait for SR to be disabled.
262 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
263 */
Jean Pihet50e4a7d2012-04-24 10:56:40 +0530264 sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
265 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
266 timeout);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530267
268 if (timeout >= SR_DISABLE_TIMEOUT)
269 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
270 __func__);
271
272 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
273 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
274 ERRCONFIG_MCUDISACKINTST);
275}
276
277static void sr_v2_disable(struct omap_sr *sr)
278{
279 int timeout = 0;
280
281 /* Enable MCUDisableAcknowledge interrupt */
282 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
283
284 /* SRCONFIG - disable SR */
285 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
286
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100287 /*
288 * Disable all other SR interrupts and clear the status
289 * write to status register ONLY on need basis - only if status
290 * is set.
291 */
292 if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
293 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530294 ERRCONFIG_VPBOUNDINTST_V2);
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100295 else
296 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
297 0x0);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530298 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
299 IRQENABLE_MCUVALIDINT |
300 IRQENABLE_MCUBOUNDSINT));
301 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
302 IRQSTATUS_MCVALIDINT |
303 IRQSTATUS_MCBOUNDSINT));
304
305 /*
306 * Wait for SR to be disabled.
307 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
308 */
Jean Pihet50e4a7d2012-04-24 10:56:40 +0530309 sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
310 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
311 timeout);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530312
313 if (timeout >= SR_DISABLE_TIMEOUT)
314 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
315 __func__);
316
317 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
318 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
319 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
320}
321
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530322static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
323 struct omap_sr *sr, u32 efuse_offs)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530324{
325 int i;
326
327 if (!sr->nvalue_table) {
328 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
329 __func__);
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530330 return NULL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530331 }
332
333 for (i = 0; i < sr->nvalue_count; i++) {
334 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530335 return &sr->nvalue_table[i];
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530336 }
337
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530338 return NULL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530339}
340
341/* Public Functions */
342
343/**
344 * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
345 * error generator module.
346 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
347 *
348 * This API is to be called from the smartreflex class driver to
349 * configure the error generator module inside the smartreflex module.
350 * SR settings if using the ERROR module inside Smartreflex.
351 * SR CLASS 3 by default uses only the ERROR module where as
352 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
353 * module. Returns 0 on success and error value in case of failure.
354 */
355int sr_configure_errgen(struct voltagedomain *voltdm)
356{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100357 u32 sr_config, sr_errconfig, errconfig_offs;
358 u32 vpboundint_en, vpboundint_st;
359 u32 senp_en = 0, senn_en = 0;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530360 u8 senp_shift, senn_shift;
361 struct omap_sr *sr = _sr_lookup(voltdm);
362
363 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530364 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100365 return PTR_ERR(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530366 }
367
368 if (!sr->clk_length)
369 sr_set_clk_length(sr);
370
371 senp_en = sr->senp_mod;
372 senn_en = sr->senn_mod;
373
374 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
375 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
376
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100377 switch (sr->ip_type) {
378 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530379 sr_config |= SRCONFIG_DELAYCTRL;
380 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
381 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
382 errconfig_offs = ERRCONFIG_V1;
383 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
384 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100385 break;
386 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530387 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
388 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
389 errconfig_offs = ERRCONFIG_V2;
390 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
391 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100392 break;
393 default:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530394 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
395 "module without specifying the ip\n", __func__);
396 return -EINVAL;
397 }
398
399 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
400 sr_write_reg(sr, SRCONFIG, sr_config);
401 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
402 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
403 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
404 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
405 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
406 sr_errconfig);
407
408 /* Enabling the interrupts if the ERROR module is used */
Nishanth Menon74754cc2012-02-29 23:33:38 +0100409 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
410 vpboundint_en);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530411
412 return 0;
413}
414
415/**
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100416 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
417 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
418 *
419 * This API is to be called from the smartreflex class driver to
420 * disable the error generator module inside the smartreflex module.
421 *
422 * Returns 0 on success and error value in case of failure.
423 */
424int sr_disable_errgen(struct voltagedomain *voltdm)
425{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100426 u32 errconfig_offs;
427 u32 vpboundint_en, vpboundint_st;
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100428 struct omap_sr *sr = _sr_lookup(voltdm);
429
430 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530431 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100432 return PTR_ERR(sr);
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100433 }
434
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100435 switch (sr->ip_type) {
436 case SR_TYPE_V1:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100437 errconfig_offs = ERRCONFIG_V1;
438 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
439 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100440 break;
441 case SR_TYPE_V2:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100442 errconfig_offs = ERRCONFIG_V2;
443 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
444 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100445 break;
446 default:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100447 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
448 "module without specifying the ip\n", __func__);
449 return -EINVAL;
450 }
451
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100452 /* Disable the Sensor and errorgen */
453 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
454
Nishanth Menonefe4e062013-05-30 13:08:34 +0300455 /*
456 * Disable the interrupts of ERROR module
457 * NOTE: modify is a read, modify,write - an implicit OCP barrier
458 * which is required is present here - sequencing is critical
459 * at this point (after errgen is disabled, vpboundint disable)
460 */
461 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
462
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100463 return 0;
464}
465
466/**
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530467 * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
468 * minmaxavg module.
469 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
470 *
471 * This API is to be called from the smartreflex class driver to
472 * configure the minmaxavg module inside the smartreflex module.
473 * SR settings if using the ERROR module inside Smartreflex.
474 * SR CLASS 3 by default uses only the ERROR module where as
475 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
476 * module. Returns 0 on success and error value in case of failure.
477 */
478int sr_configure_minmax(struct voltagedomain *voltdm)
479{
480 u32 sr_config, sr_avgwt;
481 u32 senp_en = 0, senn_en = 0;
482 u8 senp_shift, senn_shift;
483 struct omap_sr *sr = _sr_lookup(voltdm);
484
485 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530486 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100487 return PTR_ERR(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530488 }
489
490 if (!sr->clk_length)
491 sr_set_clk_length(sr);
492
493 senp_en = sr->senp_mod;
494 senn_en = sr->senn_mod;
495
496 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
497 SRCONFIG_SENENABLE |
498 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
499
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100500 switch (sr->ip_type) {
501 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530502 sr_config |= SRCONFIG_DELAYCTRL;
503 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
504 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100505 break;
506 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530507 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
508 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100509 break;
510 default:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530511 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
512 "module without specifying the ip\n", __func__);
513 return -EINVAL;
514 }
515
516 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
517 sr_write_reg(sr, SRCONFIG, sr_config);
518 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
519 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
520 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
521
522 /*
523 * Enabling the interrupts if MINMAXAVG module is used.
524 * TODO: check if all the interrupts are mandatory
525 */
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100526 switch (sr->ip_type) {
527 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530528 sr_modify_reg(sr, ERRCONFIG_V1,
529 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
530 ERRCONFIG_MCUBOUNDINTEN),
531 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
532 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
533 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100534 break;
535 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530536 sr_write_reg(sr, IRQSTATUS,
537 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
538 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
539 sr_write_reg(sr, IRQENABLE_SET,
540 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
541 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100542 break;
543 default:
544 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
545 "module without specifying the ip\n", __func__);
546 return -EINVAL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530547 }
548
549 return 0;
550}
551
552/**
553 * sr_enable() - Enables the smartreflex module.
554 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
555 * @volt: The voltage at which the Voltage domain associated with
556 * the smartreflex module is operating at.
557 * This is required only to program the correct Ntarget value.
558 *
559 * This API is to be called from the smartreflex class driver to
560 * enable a smartreflex module. Returns 0 on success. Returns error
561 * value if the voltage passed is wrong or if ntarget value is wrong.
562 */
563int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
564{
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530565 struct omap_volt_data *volt_data;
566 struct omap_sr *sr = _sr_lookup(voltdm);
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530567 struct omap_sr_nvalue_table *nvalue_row;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530568 int ret;
569
570 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530571 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100572 return PTR_ERR(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530573 }
574
575 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
576
577 if (IS_ERR(volt_data)) {
578 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
579 "for nominal voltage %ld\n", __func__, volt);
Jean Pihet63371fa2012-02-29 23:33:49 +0100580 return PTR_ERR(volt_data);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530581 }
582
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530583 nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530584
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530585 if (!nvalue_row) {
586 dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
587 __func__, volt);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530588 return -ENODATA;
589 }
590
591 /* errminlimit is opp dependent and hence linked to voltage */
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530592 sr->err_minlimit = nvalue_row->errminlimit;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530593
594 pm_runtime_get_sync(&sr->pdev->dev);
595
596 /* Check if SR is already enabled. If yes do nothing */
597 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
598 return 0;
599
600 /* Configure SR */
Jean Pihet80821c92012-04-24 10:22:12 +0530601 ret = sr_class->configure(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530602 if (ret)
603 return ret;
604
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530605 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530606
607 /* SRCONFIG - enable SR */
608 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
609 return 0;
610}
611
612/**
613 * sr_disable() - Disables the smartreflex module.
614 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
615 *
616 * This API is to be called from the smartreflex class driver to
617 * disable a smartreflex module.
618 */
619void sr_disable(struct voltagedomain *voltdm)
620{
621 struct omap_sr *sr = _sr_lookup(voltdm);
622
623 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530624 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530625 return;
626 }
627
628 /* Check if SR clocks are already disabled. If yes do nothing */
629 if (pm_runtime_suspended(&sr->pdev->dev))
630 return;
631
632 /*
633 * Disable SR if only it is indeed enabled. Else just
634 * disable the clocks.
635 */
636 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100637 switch (sr->ip_type) {
638 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530639 sr_v1_disable(sr);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100640 break;
641 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530642 sr_v2_disable(sr);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100643 break;
644 default:
645 dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
646 sr->ip_type);
647 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530648 }
649
Colin Cross98333b32011-07-22 00:55:52 -0500650 pm_runtime_put_sync_suspend(&sr->pdev->dev);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530651}
652
653/**
654 * sr_register_class() - API to register a smartreflex class parameters.
655 * @class_data: The structure containing various sr class specific data.
656 *
657 * This API is to be called by the smartreflex class driver to register itself
658 * with the smartreflex driver during init. Returns 0 on success else the
659 * error value.
660 */
661int sr_register_class(struct omap_sr_class_data *class_data)
662{
663 struct omap_sr *sr_info;
664
665 if (!class_data) {
666 pr_warning("%s:, Smartreflex class data passed is NULL\n",
667 __func__);
668 return -EINVAL;
669 }
670
671 if (sr_class) {
672 pr_warning("%s: Smartreflex class driver already registered\n",
673 __func__);
674 return -EBUSY;
675 }
676
677 sr_class = class_data;
678
679 /*
680 * Call into late init to do intializations that require
681 * both sr driver and sr class driver to be initiallized.
682 */
683 list_for_each_entry(sr_info, &sr_list, node)
684 sr_late_init(sr_info);
685
686 return 0;
687}
688
689/**
690 * omap_sr_enable() - API to enable SR clocks and to call into the
691 * registered smartreflex class enable API.
692 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
693 *
694 * This API is to be called from the kernel in order to enable
695 * a particular smartreflex module. This API will do the initial
696 * configurations to turn on the smartreflex module and in turn call
697 * into the registered smartreflex class enable API.
698 */
699void omap_sr_enable(struct voltagedomain *voltdm)
700{
701 struct omap_sr *sr = _sr_lookup(voltdm);
702
703 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530704 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530705 return;
706 }
707
708 if (!sr->autocomp_active)
709 return;
710
711 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
712 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
713 "registered\n", __func__);
714 return;
715 }
716
Jean Pihet80821c92012-04-24 10:22:12 +0530717 sr_class->enable(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530718}
719
720/**
721 * omap_sr_disable() - API to disable SR without resetting the voltage
722 * processor voltage
723 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
724 *
725 * This API is to be called from the kernel in order to disable
726 * a particular smartreflex module. This API will in turn call
727 * into the registered smartreflex class disable API. This API will tell
728 * the smartreflex class disable not to reset the VP voltage after
729 * disabling smartreflex.
730 */
731void omap_sr_disable(struct voltagedomain *voltdm)
732{
733 struct omap_sr *sr = _sr_lookup(voltdm);
734
735 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530736 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530737 return;
738 }
739
740 if (!sr->autocomp_active)
741 return;
742
743 if (!sr_class || !(sr_class->disable)) {
744 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
745 "registered\n", __func__);
746 return;
747 }
748
Jean Pihet80821c92012-04-24 10:22:12 +0530749 sr_class->disable(sr, 0);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530750}
751
752/**
753 * omap_sr_disable_reset_volt() - API to disable SR and reset the
754 * voltage processor voltage
755 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
756 *
757 * This API is to be called from the kernel in order to disable
758 * a particular smartreflex module. This API will in turn call
759 * into the registered smartreflex class disable API. This API will tell
760 * the smartreflex class disable to reset the VP voltage after
761 * disabling smartreflex.
762 */
763void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
764{
765 struct omap_sr *sr = _sr_lookup(voltdm);
766
767 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530768 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530769 return;
770 }
771
772 if (!sr->autocomp_active)
773 return;
774
775 if (!sr_class || !(sr_class->disable)) {
776 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
777 "registered\n", __func__);
778 return;
779 }
780
Jean Pihet80821c92012-04-24 10:22:12 +0530781 sr_class->disable(sr, 1);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530782}
783
784/**
785 * omap_sr_register_pmic() - API to register pmic specific info.
786 * @pmic_data: The structure containing pmic specific data.
787 *
788 * This API is to be called from the PMIC specific code to register with
789 * smartreflex driver pmic specific info. Currently the only info required
790 * is the smartreflex init on the PMIC side.
791 */
792void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
793{
794 if (!pmic_data) {
795 pr_warning("%s: Trying to register NULL PMIC data structure"
796 "with smartreflex\n", __func__);
797 return;
798 }
799
800 sr_pmic_data = pmic_data;
801}
802
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100803/* PM Debug FS entries to enable and disable smartreflex. */
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530804static int omap_sr_autocomp_show(void *data, u64 *val)
805{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100806 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530807
808 if (!sr_info) {
Stefan Weil83535842011-01-30 20:29:38 +0100809 pr_warning("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530810 return -EINVAL;
811 }
812
813 *val = sr_info->autocomp_active;
814
815 return 0;
816}
817
818static int omap_sr_autocomp_store(void *data, u64 val)
819{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100820 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530821
822 if (!sr_info) {
Stefan Weil83535842011-01-30 20:29:38 +0100823 pr_warning("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530824 return -EINVAL;
825 }
826
827 /* Sanity check */
Felipe Balbid6173692012-02-29 23:33:47 +0100828 if (val > 1) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530829 pr_warning("%s: Invalid argument %lld\n", __func__, val);
830 return -EINVAL;
831 }
832
Nishanth Menonac77a6f2011-02-14 21:14:17 +0530833 /* control enable/disable only if there is a delta in value */
834 if (sr_info->autocomp_active != val) {
835 if (!val)
836 sr_stop_vddautocomp(sr_info);
837 else
838 sr_start_vddautocomp(sr_info);
839 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530840
841 return 0;
842}
843
844DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100845 omap_sr_autocomp_store, "%llu\n");
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530846
847static int __init omap_sr_probe(struct platform_device *pdev)
848{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100849 struct omap_sr *sr_info;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530850 struct omap_sr_data *pdata = pdev->dev.platform_data;
851 struct resource *mem, *irq;
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700852 struct dentry *nvalue_dir;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530853 int i, ret = 0;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530854
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100855 sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530856 if (!sr_info) {
857 dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
858 __func__);
859 return -ENOMEM;
860 }
861
Felipe Balbi1079a8b2012-02-29 23:33:44 +0100862 platform_set_drvdata(pdev, sr_info);
863
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530864 if (!pdata) {
865 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
Stefan Weil720bc7822011-01-30 20:26:27 +0100866 ret = -EINVAL;
867 goto err_free_devinfo;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530868 }
869
870 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
871 if (!mem) {
872 dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
873 ret = -ENODEV;
874 goto err_free_devinfo;
875 }
876
Aaro Koskinenda9e73922011-04-26 02:25:16 -0700877 mem = request_mem_region(mem->start, resource_size(mem),
878 dev_name(&pdev->dev));
879 if (!mem) {
880 dev_err(&pdev->dev, "%s: no mem region\n", __func__);
881 ret = -EBUSY;
882 goto err_free_devinfo;
883 }
884
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530885 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
886
887 pm_runtime_enable(&pdev->dev);
Nishanth Menone13d8f32011-07-09 14:37:21 -0700888 pm_runtime_irq_safe(&pdev->dev);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530889
Jean Pihet8b765d72012-04-24 10:41:27 +0530890 sr_info->name = kasprintf(GFP_KERNEL, "%s", pdata->name);
891 if (!sr_info->name) {
892 dev_err(&pdev->dev, "%s: Unable to alloc SR instance name\n",
893 __func__);
894 ret = -ENOMEM;
895 goto err_release_region;
896 }
897
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530898 sr_info->pdev = pdev;
899 sr_info->srid = pdev->id;
900 sr_info->voltdm = pdata->voltdm;
901 sr_info->nvalue_table = pdata->nvalue_table;
902 sr_info->nvalue_count = pdata->nvalue_count;
903 sr_info->senn_mod = pdata->senn_mod;
904 sr_info->senp_mod = pdata->senp_mod;
Jean Pihet98aed082012-10-04 18:47:11 +0200905 sr_info->err_weight = pdata->err_weight;
906 sr_info->err_maxlimit = pdata->err_maxlimit;
907 sr_info->accum_data = pdata->accum_data;
908 sr_info->senn_avgweight = pdata->senn_avgweight;
909 sr_info->senp_avgweight = pdata->senp_avgweight;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530910 sr_info->autocomp_active = false;
911 sr_info->ip_type = pdata->ip_type;
Jean Pihet98aed082012-10-04 18:47:11 +0200912
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530913 sr_info->base = ioremap(mem->start, resource_size(mem));
914 if (!sr_info->base) {
915 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
916 ret = -ENOMEM;
Jean Pihetce3810c2012-09-24 16:16:40 +0200917 goto err_free_name;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530918 }
919
920 if (irq)
921 sr_info->irq = irq->start;
922
923 sr_set_clk_length(sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530924
925 list_add(&sr_info->node, &sr_list);
926
927 /*
928 * Call into late init to do intializations that require
929 * both sr driver and sr class driver to be initiallized.
930 */
931 if (sr_class) {
932 ret = sr_late_init(sr_info);
933 if (ret) {
934 pr_warning("%s: Error in SR late init\n", __func__);
Julia Lawall14ea9602012-01-12 10:55:17 +0100935 goto err_iounmap;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530936 }
937 }
938
939 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700940 if (!sr_dbg_dir) {
941 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100942 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700943 ret = PTR_ERR(sr_dbg_dir);
944 pr_err("%s:sr debugfs dir creation failed(%d)\n",
945 __func__, ret);
946 goto err_iounmap;
947 }
Shweta Gulatib3329a32011-02-15 13:40:30 +0530948 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530949
Jean Pihet8b765d72012-04-24 10:41:27 +0530950 sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100951 if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530952 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
953 __func__);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530954 ret = PTR_ERR(sr_info->dbg_dir);
Jean Pihetce3810c2012-09-24 16:16:40 +0200955 goto err_debugfs;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530956 }
957
Anand S Sawantb1ace382011-02-17 21:27:30 +0530958 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
959 sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
960 (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530961 &sr_info->err_weight);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530962 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530963 &sr_info->err_maxlimit);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530964
Anand S Sawantb1ace382011-02-17 21:27:30 +0530965 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100966 if (IS_ERR_OR_NULL(nvalue_dir)) {
Thara Gopinath077fcec2010-10-27 20:29:37 +0530967 dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
968 "for n-values\n", __func__);
Shweta Gulatib3329a32011-02-15 13:40:30 +0530969 ret = PTR_ERR(nvalue_dir);
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700970 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530971 }
972
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530973 if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
974 dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
975 __func__, sr_info->name);
976
Shweta Gulatib3329a32011-02-15 13:40:30 +0530977 ret = -ENODATA;
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700978 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530979 }
980
981 for (i = 0; i < sr_info->nvalue_count; i++) {
Aaro Koskinen865212ab2011-02-07 16:08:04 +0200982 char name[NVALUE_NAME_LEN + 1];
Thara Gopinath077fcec2010-10-27 20:29:37 +0530983
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530984 snprintf(name, sizeof(name), "volt_%lu",
985 sr_info->nvalue_table[i].volt_nominal);
Vasiliy Kulikov1232a182011-02-04 12:23:20 +0000986 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530987 &(sr_info->nvalue_table[i].nvalue));
Jean Pihet308d1bd2012-04-24 23:41:54 +0530988 snprintf(name, sizeof(name), "errminlimit_%lu",
989 sr_info->nvalue_table[i].volt_nominal);
990 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
991 &(sr_info->nvalue_table[i].errminlimit));
992
Thara Gopinath077fcec2010-10-27 20:29:37 +0530993 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530994
995 return ret;
996
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700997err_debugfs:
998 debugfs_remove_recursive(sr_info->dbg_dir);
Aaro Koskinen0c49cc12011-04-26 02:25:21 -0700999err_iounmap:
Aaro Koskinen833d78f2011-04-26 02:25:27 -07001000 list_del(&sr_info->node);
Aaro Koskinen0c49cc12011-04-26 02:25:21 -07001001 iounmap(sr_info->base);
Jean Pihetce3810c2012-09-24 16:16:40 +02001002err_free_name:
1003 kfree(sr_info->name);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301004err_release_region:
1005 release_mem_region(mem->start, resource_size(mem));
1006err_free_devinfo:
1007 kfree(sr_info);
1008
1009 return ret;
1010}
1011
Bill Pemberton415ec692012-11-19 13:26:07 -05001012static int omap_sr_remove(struct platform_device *pdev)
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301013{
1014 struct omap_sr_data *pdata = pdev->dev.platform_data;
1015 struct omap_sr *sr_info;
1016 struct resource *mem;
1017
1018 if (!pdata) {
1019 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1020 return -EINVAL;
1021 }
1022
1023 sr_info = _sr_lookup(pdata->voltdm);
Julia Lawall28693ec2011-01-24 20:55:22 +01001024 if (IS_ERR(sr_info)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301025 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1026 __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +01001027 return PTR_ERR(sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301028 }
1029
1030 if (sr_info->autocomp_active)
1031 sr_stop_vddautocomp(sr_info);
Anand S Sawantb1ace382011-02-17 21:27:30 +05301032 if (sr_info->dbg_dir)
1033 debugfs_remove_recursive(sr_info->dbg_dir);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301034
1035 list_del(&sr_info->node);
1036 iounmap(sr_info->base);
Jean Pihet8b765d72012-04-24 10:41:27 +05301037 kfree(sr_info->name);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301038 kfree(sr_info);
1039 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1040 release_mem_region(mem->start, resource_size(mem));
1041
1042 return 0;
1043}
1044
Bill Pemberton415ec692012-11-19 13:26:07 -05001045static void omap_sr_shutdown(struct platform_device *pdev)
Nishanth Menon1f55bc1852012-03-01 00:29:44 +01001046{
1047 struct omap_sr_data *pdata = pdev->dev.platform_data;
1048 struct omap_sr *sr_info;
1049
1050 if (!pdata) {
1051 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1052 return;
1053 }
1054
1055 sr_info = _sr_lookup(pdata->voltdm);
1056 if (IS_ERR(sr_info)) {
1057 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1058 __func__);
1059 return;
1060 }
1061
1062 if (sr_info->autocomp_active)
1063 sr_stop_vddautocomp(sr_info);
1064
1065 return;
1066}
1067
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301068static struct platform_driver smartreflex_driver = {
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001069 .remove = omap_sr_remove,
1070 .shutdown = omap_sr_shutdown,
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301071 .driver = {
1072 .name = "smartreflex",
1073 },
1074};
1075
1076static int __init sr_init(void)
1077{
1078 int ret = 0;
1079
1080 /*
1081 * sr_init is a late init. If by then a pmic specific API is not
1082 * registered either there is no need for anything to be done on
1083 * the PMIC side or somebody has forgotten to register a PMIC
1084 * handler. Warn for the second condition.
1085 */
1086 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1087 sr_pmic_data->sr_pmic_init();
1088 else
1089 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
1090
1091 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1092 if (ret) {
1093 pr_err("%s: platform driver register failed for SR\n",
1094 __func__);
1095 return ret;
1096 }
1097
1098 return 0;
1099}
Felipe Balbi1a21a682012-02-29 23:33:45 +01001100late_initcall(sr_init);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301101
1102static void __exit sr_exit(void)
1103{
1104 platform_driver_unregister(&smartreflex_driver);
1105}
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301106module_exit(sr_exit);
1107
1108MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1109MODULE_LICENSE("GPL");
1110MODULE_ALIAS("platform:" DRIVER_NAME);
1111MODULE_AUTHOR("Texas Instruments Inc");