blob: 4c4519e59be40541408346b2b3c41e603be22da8 [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
Tony Lindgrenec2c0822012-08-27 17:43:01 -070030#include <plat/cpu.h>
31
Thara Gopinath984aa6d2010-05-29 22:02:22 +053032#define SMARTREFLEX_NAME_LEN 16
Thara Gopinath077fcec2010-10-27 20:29:37 +053033#define NVALUE_NAME_LEN 40
Thara Gopinath984aa6d2010-05-29 22:02:22 +053034#define SR_DISABLE_TIMEOUT 200
35
Thara Gopinath984aa6d2010-05-29 22:02:22 +053036/* sr_list contains all the instances of smartreflex module */
37static LIST_HEAD(sr_list);
38
39static struct omap_sr_class_data *sr_class;
40static struct omap_sr_pmic_data *sr_pmic_data;
Kevin Hilman633ef8b2011-04-05 14:39:11 -070041static struct dentry *sr_dbg_dir;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053042
43static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
44{
45 __raw_writel(value, (sr->base + offset));
46}
47
48static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
49 u32 value)
50{
51 u32 reg_val;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053052
53 /*
54 * Smartreflex error config register is special as it contains
55 * certain status bits which if written a 1 into means a clear
56 * of those bits. So in order to make sure no accidental write of
57 * 1 happens to those status bits, do a clear of them in the read
58 * value. This mean this API doesn't rewrite values in these bits
59 * if they are currently set, but does allow the caller to write
60 * those bits.
61 */
Nishanth Menonade6ec02012-02-29 23:33:41 +010062 if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
63 mask |= ERRCONFIG_STATUS_V1_MASK;
64 else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
65 mask |= ERRCONFIG_VPBOUNDINTST_V2;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053066
Nishanth Menonade6ec02012-02-29 23:33:41 +010067 reg_val = __raw_readl(sr->base + offset);
68 reg_val &= ~mask;
69
70 value &= mask;
Thara Gopinath984aa6d2010-05-29 22:02:22 +053071
72 reg_val |= value;
73
74 __raw_writel(reg_val, (sr->base + offset));
75}
76
77static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
78{
79 return __raw_readl(sr->base + offset);
80}
81
82static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
83{
84 struct omap_sr *sr_info;
85
86 if (!voltdm) {
87 pr_err("%s: Null voltage domain passed!\n", __func__);
88 return ERR_PTR(-EINVAL);
89 }
90
91 list_for_each_entry(sr_info, &sr_list, node) {
92 if (voltdm == sr_info->voltdm)
93 return sr_info;
94 }
95
96 return ERR_PTR(-ENODATA);
97}
98
99static irqreturn_t sr_interrupt(int irq, void *data)
100{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100101 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530102 u32 status = 0;
103
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100104 switch (sr_info->ip_type) {
105 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530106 /* Read the status bits */
107 status = sr_read_reg(sr_info, ERRCONFIG_V1);
108
109 /* Clear them by writing back */
110 sr_write_reg(sr_info, ERRCONFIG_V1, status);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100111 break;
112 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530113 /* Read the status bits */
Felipe Balbi5a4f1842011-11-23 14:43:37 -0800114 status = sr_read_reg(sr_info, IRQSTATUS);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530115
116 /* Clear them by writing back */
117 sr_write_reg(sr_info, IRQSTATUS, status);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100118 break;
119 default:
120 dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
121 sr_info->ip_type);
122 return IRQ_NONE;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530123 }
124
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530125 if (sr_class->notify)
Jean Pihet80821c92012-04-24 10:22:12 +0530126 sr_class->notify(sr_info, status);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530127
128 return IRQ_HANDLED;
129}
130
131static void sr_set_clk_length(struct omap_sr *sr)
132{
Jean Pihet98aed082012-10-04 18:47:11 +0200133 struct clk *fck;
134 u32 fclk_speed;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530135
Jean Pihet98aed082012-10-04 18:47:11 +0200136 fck = clk_get(&sr->pdev->dev, "fck");
Thara Gopinathb35cecf2010-08-18 12:23:12 +0530137
Jean Pihet98aed082012-10-04 18:47:11 +0200138 if (IS_ERR(fck)) {
139 dev_err(&sr->pdev->dev, "%s: unable to get fck for device %s\n",
140 __func__, dev_name(&sr->pdev->dev));
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530141 return;
142 }
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100143
Jean Pihet98aed082012-10-04 18:47:11 +0200144 fclk_speed = clk_get_rate(fck);
145 clk_put(fck);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530146
Jean Pihet98aed082012-10-04 18:47:11 +0200147 switch (fclk_speed) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530148 case 12000000:
149 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
150 break;
151 case 13000000:
152 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
153 break;
154 case 19200000:
155 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
156 break;
157 case 26000000:
158 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
159 break;
160 case 38400000:
161 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
162 break;
163 default:
Jean Pihet98aed082012-10-04 18:47:11 +0200164 dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
165 __func__, fclk_speed);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530166 break;
167 }
168}
169
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530170static void sr_start_vddautocomp(struct omap_sr *sr)
171{
172 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
173 dev_warn(&sr->pdev->dev,
174 "%s: smartreflex class driver not registered\n",
175 __func__);
176 return;
177 }
178
Jean Pihet80821c92012-04-24 10:22:12 +0530179 if (!sr_class->enable(sr))
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530180 sr->autocomp_active = true;
181}
182
183static void sr_stop_vddautocomp(struct omap_sr *sr)
184{
185 if (!sr_class || !(sr_class->disable)) {
186 dev_warn(&sr->pdev->dev,
187 "%s: smartreflex class driver not registered\n",
188 __func__);
189 return;
190 }
191
192 if (sr->autocomp_active) {
Jean Pihet80821c92012-04-24 10:22:12 +0530193 sr_class->disable(sr, 1);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530194 sr->autocomp_active = false;
195 }
196}
197
198/*
199 * This function handles the intializations which have to be done
200 * only when both sr device and class driver regiter has
201 * completed. This will be attempted to be called from both sr class
202 * driver register and sr device intializtion API's. Only one call
203 * will ultimately succeed.
204 *
Vitaliy Ivanovfb914eb2011-06-23 20:01:55 +0300205 * Currently this function registers interrupt handler for a particular SR
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530206 * if smartreflex class driver is already registered and has
207 * requested for interrupts and the SR interrupt line in present.
208 */
209static int sr_late_init(struct omap_sr *sr_info)
210{
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530211 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
212 struct resource *mem;
213 int ret = 0;
214
Nishanth Menon7a89afa2011-02-14 12:16:36 +0530215 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530216 ret = request_irq(sr_info->irq, sr_interrupt,
Jean Pihet8b765d72012-04-24 10:41:27 +0530217 0, sr_info->name, sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530218 if (ret)
219 goto error;
Nishanth Menon1279ba52011-02-14 12:41:10 +0530220 disable_irq(sr_info->irq);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530221 }
222
223 if (pdata && pdata->enable_on_init)
224 sr_start_vddautocomp(sr_info);
225
226 return ret;
227
228error:
Nishanth Menon442155a2011-02-14 12:33:13 +0530229 iounmap(sr_info->base);
230 mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
231 release_mem_region(mem->start, resource_size(mem));
232 list_del(&sr_info->node);
233 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
234 "interrupt handler. Smartreflex will"
235 "not function as desired\n", __func__);
Nishanth Menon442155a2011-02-14 12:33:13 +0530236 kfree(sr_info);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100237
Nishanth Menon442155a2011-02-14 12:33:13 +0530238 return ret;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530239}
240
241static void sr_v1_disable(struct omap_sr *sr)
242{
243 int timeout = 0;
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100244 int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
245 ERRCONFIG_MCUBOUNDINTST;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530246
247 /* Enable MCUDisableAcknowledge interrupt */
248 sr_modify_reg(sr, ERRCONFIG_V1,
249 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
250
251 /* SRCONFIG - disable SR */
252 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
253
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100254 /* Disable all other SR interrupts and clear the status as needed */
255 if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
256 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530257 sr_modify_reg(sr, ERRCONFIG_V1,
258 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
259 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100260 errconf_val);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530261
262 /*
263 * Wait for SR to be disabled.
264 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
265 */
Jean Pihet50e4a7d2012-04-24 10:56:40 +0530266 sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
267 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
268 timeout);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530269
270 if (timeout >= SR_DISABLE_TIMEOUT)
271 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
272 __func__);
273
274 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
275 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
276 ERRCONFIG_MCUDISACKINTST);
277}
278
279static void sr_v2_disable(struct omap_sr *sr)
280{
281 int timeout = 0;
282
283 /* Enable MCUDisableAcknowledge interrupt */
284 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
285
286 /* SRCONFIG - disable SR */
287 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
288
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100289 /*
290 * Disable all other SR interrupts and clear the status
291 * write to status register ONLY on need basis - only if status
292 * is set.
293 */
294 if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
295 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530296 ERRCONFIG_VPBOUNDINTST_V2);
Nishanth Menoncfec9c52012-02-29 23:33:42 +0100297 else
298 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
299 0x0);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530300 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
301 IRQENABLE_MCUVALIDINT |
302 IRQENABLE_MCUBOUNDSINT));
303 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
304 IRQSTATUS_MCVALIDINT |
305 IRQSTATUS_MCBOUNDSINT));
306
307 /*
308 * Wait for SR to be disabled.
309 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
310 */
Jean Pihet50e4a7d2012-04-24 10:56:40 +0530311 sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
312 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
313 timeout);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530314
315 if (timeout >= SR_DISABLE_TIMEOUT)
316 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
317 __func__);
318
319 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
320 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
321 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
322}
323
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530324static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
325 struct omap_sr *sr, u32 efuse_offs)
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530326{
327 int i;
328
329 if (!sr->nvalue_table) {
330 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
331 __func__);
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530332 return NULL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530333 }
334
335 for (i = 0; i < sr->nvalue_count; i++) {
336 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530337 return &sr->nvalue_table[i];
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530338 }
339
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530340 return NULL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530341}
342
343/* Public Functions */
344
345/**
346 * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
347 * error generator module.
348 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
349 *
350 * This API is to be called from the smartreflex class driver to
351 * configure the error generator module inside the smartreflex module.
352 * SR settings if using the ERROR module inside Smartreflex.
353 * SR CLASS 3 by default uses only the ERROR module where as
354 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
355 * module. Returns 0 on success and error value in case of failure.
356 */
357int sr_configure_errgen(struct voltagedomain *voltdm)
358{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100359 u32 sr_config, sr_errconfig, errconfig_offs;
360 u32 vpboundint_en, vpboundint_st;
361 u32 senp_en = 0, senn_en = 0;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530362 u8 senp_shift, senn_shift;
363 struct omap_sr *sr = _sr_lookup(voltdm);
364
365 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530366 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100367 return PTR_ERR(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530368 }
369
370 if (!sr->clk_length)
371 sr_set_clk_length(sr);
372
373 senp_en = sr->senp_mod;
374 senn_en = sr->senn_mod;
375
376 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
377 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
378
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100379 switch (sr->ip_type) {
380 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530381 sr_config |= SRCONFIG_DELAYCTRL;
382 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
383 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
384 errconfig_offs = ERRCONFIG_V1;
385 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
386 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100387 break;
388 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530389 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
390 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
391 errconfig_offs = ERRCONFIG_V2;
392 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
393 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100394 break;
395 default:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530396 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
397 "module without specifying the ip\n", __func__);
398 return -EINVAL;
399 }
400
401 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
402 sr_write_reg(sr, SRCONFIG, sr_config);
403 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
404 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
405 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
406 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
407 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
408 sr_errconfig);
409
410 /* Enabling the interrupts if the ERROR module is used */
Nishanth Menon74754cc2012-02-29 23:33:38 +0100411 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
412 vpboundint_en);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530413
414 return 0;
415}
416
417/**
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100418 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
419 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
420 *
421 * This API is to be called from the smartreflex class driver to
422 * disable the error generator module inside the smartreflex module.
423 *
424 * Returns 0 on success and error value in case of failure.
425 */
426int sr_disable_errgen(struct voltagedomain *voltdm)
427{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100428 u32 errconfig_offs;
429 u32 vpboundint_en, vpboundint_st;
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100430 struct omap_sr *sr = _sr_lookup(voltdm);
431
432 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530433 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100434 return PTR_ERR(sr);
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100435 }
436
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100437 switch (sr->ip_type) {
438 case SR_TYPE_V1:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100439 errconfig_offs = ERRCONFIG_V1;
440 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
441 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100442 break;
443 case SR_TYPE_V2:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100444 errconfig_offs = ERRCONFIG_V2;
445 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
446 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100447 break;
448 default:
Nishanth Menonad54c3d2012-02-29 23:33:39 +0100449 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
450 "module without specifying the ip\n", __func__);
451 return -EINVAL;
452 }
453
454 /* Disable the interrupts of ERROR module */
455 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
456
457 /* Disable the Sensor and errorgen */
458 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
459
460 return 0;
461}
462
463/**
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530464 * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
465 * minmaxavg module.
466 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
467 *
468 * This API is to be called from the smartreflex class driver to
469 * configure the minmaxavg module inside the smartreflex module.
470 * SR settings if using the ERROR module inside Smartreflex.
471 * SR CLASS 3 by default uses only the ERROR module where as
472 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
473 * module. Returns 0 on success and error value in case of failure.
474 */
475int sr_configure_minmax(struct voltagedomain *voltdm)
476{
477 u32 sr_config, sr_avgwt;
478 u32 senp_en = 0, senn_en = 0;
479 u8 senp_shift, senn_shift;
480 struct omap_sr *sr = _sr_lookup(voltdm);
481
482 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530483 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100484 return PTR_ERR(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530485 }
486
487 if (!sr->clk_length)
488 sr_set_clk_length(sr);
489
490 senp_en = sr->senp_mod;
491 senn_en = sr->senn_mod;
492
493 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
494 SRCONFIG_SENENABLE |
495 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
496
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100497 switch (sr->ip_type) {
498 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530499 sr_config |= SRCONFIG_DELAYCTRL;
500 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
501 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100502 break;
503 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530504 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
505 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100506 break;
507 default:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530508 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
509 "module without specifying the ip\n", __func__);
510 return -EINVAL;
511 }
512
513 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
514 sr_write_reg(sr, SRCONFIG, sr_config);
515 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
516 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
517 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
518
519 /*
520 * Enabling the interrupts if MINMAXAVG module is used.
521 * TODO: check if all the interrupts are mandatory
522 */
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100523 switch (sr->ip_type) {
524 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530525 sr_modify_reg(sr, ERRCONFIG_V1,
526 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
527 ERRCONFIG_MCUBOUNDINTEN),
528 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
529 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
530 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100531 break;
532 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530533 sr_write_reg(sr, IRQSTATUS,
534 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
535 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
536 sr_write_reg(sr, IRQENABLE_SET,
537 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
538 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100539 break;
540 default:
541 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
542 "module without specifying the ip\n", __func__);
543 return -EINVAL;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530544 }
545
546 return 0;
547}
548
549/**
550 * sr_enable() - Enables the smartreflex module.
551 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
552 * @volt: The voltage at which the Voltage domain associated with
553 * the smartreflex module is operating at.
554 * This is required only to program the correct Ntarget value.
555 *
556 * This API is to be called from the smartreflex class driver to
557 * enable a smartreflex module. Returns 0 on success. Returns error
558 * value if the voltage passed is wrong or if ntarget value is wrong.
559 */
560int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
561{
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530562 struct omap_volt_data *volt_data;
563 struct omap_sr *sr = _sr_lookup(voltdm);
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530564 struct omap_sr_nvalue_table *nvalue_row;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530565 int ret;
566
567 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530568 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +0100569 return PTR_ERR(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530570 }
571
572 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
573
574 if (IS_ERR(volt_data)) {
575 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
576 "for nominal voltage %ld\n", __func__, volt);
Jean Pihet63371fa2012-02-29 23:33:49 +0100577 return PTR_ERR(volt_data);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530578 }
579
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530580 nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530581
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530582 if (!nvalue_row) {
583 dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
584 __func__, volt);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530585 return -ENODATA;
586 }
587
588 /* errminlimit is opp dependent and hence linked to voltage */
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530589 sr->err_minlimit = nvalue_row->errminlimit;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530590
591 pm_runtime_get_sync(&sr->pdev->dev);
592
593 /* Check if SR is already enabled. If yes do nothing */
594 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
595 return 0;
596
597 /* Configure SR */
Jean Pihet80821c92012-04-24 10:22:12 +0530598 ret = sr_class->configure(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530599 if (ret)
600 return ret;
601
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530602 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530603
604 /* SRCONFIG - enable SR */
605 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
606 return 0;
607}
608
609/**
610 * sr_disable() - Disables the smartreflex module.
611 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
612 *
613 * This API is to be called from the smartreflex class driver to
614 * disable a smartreflex module.
615 */
616void sr_disable(struct voltagedomain *voltdm)
617{
618 struct omap_sr *sr = _sr_lookup(voltdm);
619
620 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530621 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530622 return;
623 }
624
625 /* Check if SR clocks are already disabled. If yes do nothing */
626 if (pm_runtime_suspended(&sr->pdev->dev))
627 return;
628
629 /*
630 * Disable SR if only it is indeed enabled. Else just
631 * disable the clocks.
632 */
633 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100634 switch (sr->ip_type) {
635 case SR_TYPE_V1:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530636 sr_v1_disable(sr);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100637 break;
638 case SR_TYPE_V2:
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530639 sr_v2_disable(sr);
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100640 break;
641 default:
642 dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
643 sr->ip_type);
644 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530645 }
646
Colin Cross98333b32011-07-22 00:55:52 -0500647 pm_runtime_put_sync_suspend(&sr->pdev->dev);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530648}
649
650/**
651 * sr_register_class() - API to register a smartreflex class parameters.
652 * @class_data: The structure containing various sr class specific data.
653 *
654 * This API is to be called by the smartreflex class driver to register itself
655 * with the smartreflex driver during init. Returns 0 on success else the
656 * error value.
657 */
658int sr_register_class(struct omap_sr_class_data *class_data)
659{
660 struct omap_sr *sr_info;
661
662 if (!class_data) {
663 pr_warning("%s:, Smartreflex class data passed is NULL\n",
664 __func__);
665 return -EINVAL;
666 }
667
668 if (sr_class) {
669 pr_warning("%s: Smartreflex class driver already registered\n",
670 __func__);
671 return -EBUSY;
672 }
673
674 sr_class = class_data;
675
676 /*
677 * Call into late init to do intializations that require
678 * both sr driver and sr class driver to be initiallized.
679 */
680 list_for_each_entry(sr_info, &sr_list, node)
681 sr_late_init(sr_info);
682
683 return 0;
684}
685
686/**
687 * omap_sr_enable() - API to enable SR clocks and to call into the
688 * registered smartreflex class enable API.
689 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
690 *
691 * This API is to be called from the kernel in order to enable
692 * a particular smartreflex module. This API will do the initial
693 * configurations to turn on the smartreflex module and in turn call
694 * into the registered smartreflex class enable API.
695 */
696void omap_sr_enable(struct voltagedomain *voltdm)
697{
698 struct omap_sr *sr = _sr_lookup(voltdm);
699
700 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530701 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530702 return;
703 }
704
705 if (!sr->autocomp_active)
706 return;
707
708 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
709 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
710 "registered\n", __func__);
711 return;
712 }
713
Jean Pihet80821c92012-04-24 10:22:12 +0530714 sr_class->enable(sr);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530715}
716
717/**
718 * omap_sr_disable() - API to disable SR without resetting the voltage
719 * processor voltage
720 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
721 *
722 * This API is to be called from the kernel in order to disable
723 * a particular smartreflex module. This API will in turn call
724 * into the registered smartreflex class disable API. This API will tell
725 * the smartreflex class disable not to reset the VP voltage after
726 * disabling smartreflex.
727 */
728void omap_sr_disable(struct voltagedomain *voltdm)
729{
730 struct omap_sr *sr = _sr_lookup(voltdm);
731
732 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530733 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530734 return;
735 }
736
737 if (!sr->autocomp_active)
738 return;
739
740 if (!sr_class || !(sr_class->disable)) {
741 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
742 "registered\n", __func__);
743 return;
744 }
745
Jean Pihet80821c92012-04-24 10:22:12 +0530746 sr_class->disable(sr, 0);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530747}
748
749/**
750 * omap_sr_disable_reset_volt() - API to disable SR and reset the
751 * voltage processor voltage
752 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
753 *
754 * This API is to be called from the kernel in order to disable
755 * a particular smartreflex module. This API will in turn call
756 * into the registered smartreflex class disable API. This API will tell
757 * the smartreflex class disable to reset the VP voltage after
758 * disabling smartreflex.
759 */
760void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
761{
762 struct omap_sr *sr = _sr_lookup(voltdm);
763
764 if (IS_ERR(sr)) {
Jean Pihet8b765d72012-04-24 10:41:27 +0530765 pr_warning("%s: omap_sr struct for voltdm not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530766 return;
767 }
768
769 if (!sr->autocomp_active)
770 return;
771
772 if (!sr_class || !(sr_class->disable)) {
773 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
774 "registered\n", __func__);
775 return;
776 }
777
Jean Pihet80821c92012-04-24 10:22:12 +0530778 sr_class->disable(sr, 1);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530779}
780
781/**
782 * omap_sr_register_pmic() - API to register pmic specific info.
783 * @pmic_data: The structure containing pmic specific data.
784 *
785 * This API is to be called from the PMIC specific code to register with
786 * smartreflex driver pmic specific info. Currently the only info required
787 * is the smartreflex init on the PMIC side.
788 */
789void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
790{
791 if (!pmic_data) {
792 pr_warning("%s: Trying to register NULL PMIC data structure"
793 "with smartreflex\n", __func__);
794 return;
795 }
796
797 sr_pmic_data = pmic_data;
798}
799
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100800/* PM Debug FS entries to enable and disable smartreflex. */
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530801static int omap_sr_autocomp_show(void *data, u64 *val)
802{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100803 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530804
805 if (!sr_info) {
Stefan Weil83535842011-01-30 20:29:38 +0100806 pr_warning("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530807 return -EINVAL;
808 }
809
810 *val = sr_info->autocomp_active;
811
812 return 0;
813}
814
815static int omap_sr_autocomp_store(void *data, u64 val)
816{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100817 struct omap_sr *sr_info = data;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530818
819 if (!sr_info) {
Stefan Weil83535842011-01-30 20:29:38 +0100820 pr_warning("%s: omap_sr struct not found\n", __func__);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530821 return -EINVAL;
822 }
823
824 /* Sanity check */
Felipe Balbid6173692012-02-29 23:33:47 +0100825 if (val > 1) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530826 pr_warning("%s: Invalid argument %lld\n", __func__, val);
827 return -EINVAL;
828 }
829
Nishanth Menonac77a6f2011-02-14 21:14:17 +0530830 /* control enable/disable only if there is a delta in value */
831 if (sr_info->autocomp_active != val) {
832 if (!val)
833 sr_stop_vddautocomp(sr_info);
834 else
835 sr_start_vddautocomp(sr_info);
836 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530837
838 return 0;
839}
840
841DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100842 omap_sr_autocomp_store, "%llu\n");
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530843
844static int __init omap_sr_probe(struct platform_device *pdev)
845{
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100846 struct omap_sr *sr_info;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530847 struct omap_sr_data *pdata = pdev->dev.platform_data;
848 struct resource *mem, *irq;
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700849 struct dentry *nvalue_dir;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530850 int i, ret = 0;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530851
Felipe Balbi4018bfe2012-02-29 23:33:46 +0100852 sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530853 if (!sr_info) {
854 dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
855 __func__);
856 return -ENOMEM;
857 }
858
Felipe Balbi1079a8b2012-02-29 23:33:44 +0100859 platform_set_drvdata(pdev, sr_info);
860
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530861 if (!pdata) {
862 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
Stefan Weil720bc7822011-01-30 20:26:27 +0100863 ret = -EINVAL;
864 goto err_free_devinfo;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530865 }
866
867 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
868 if (!mem) {
869 dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
870 ret = -ENODEV;
871 goto err_free_devinfo;
872 }
873
Aaro Koskinenda9e73922011-04-26 02:25:16 -0700874 mem = request_mem_region(mem->start, resource_size(mem),
875 dev_name(&pdev->dev));
876 if (!mem) {
877 dev_err(&pdev->dev, "%s: no mem region\n", __func__);
878 ret = -EBUSY;
879 goto err_free_devinfo;
880 }
881
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530882 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
883
884 pm_runtime_enable(&pdev->dev);
Nishanth Menone13d8f32011-07-09 14:37:21 -0700885 pm_runtime_irq_safe(&pdev->dev);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530886
Jean Pihet8b765d72012-04-24 10:41:27 +0530887 sr_info->name = kasprintf(GFP_KERNEL, "%s", pdata->name);
888 if (!sr_info->name) {
889 dev_err(&pdev->dev, "%s: Unable to alloc SR instance name\n",
890 __func__);
891 ret = -ENOMEM;
892 goto err_release_region;
893 }
894
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530895 sr_info->pdev = pdev;
896 sr_info->srid = pdev->id;
897 sr_info->voltdm = pdata->voltdm;
898 sr_info->nvalue_table = pdata->nvalue_table;
899 sr_info->nvalue_count = pdata->nvalue_count;
900 sr_info->senn_mod = pdata->senn_mod;
901 sr_info->senp_mod = pdata->senp_mod;
Jean Pihet98aed082012-10-04 18:47:11 +0200902 sr_info->err_weight = pdata->err_weight;
903 sr_info->err_maxlimit = pdata->err_maxlimit;
904 sr_info->accum_data = pdata->accum_data;
905 sr_info->senn_avgweight = pdata->senn_avgweight;
906 sr_info->senp_avgweight = pdata->senp_avgweight;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530907 sr_info->autocomp_active = false;
908 sr_info->ip_type = pdata->ip_type;
Jean Pihet98aed082012-10-04 18:47:11 +0200909
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530910 sr_info->base = ioremap(mem->start, resource_size(mem));
911 if (!sr_info->base) {
912 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
913 ret = -ENOMEM;
Jean Pihetce3810c2012-09-24 16:16:40 +0200914 goto err_free_name;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530915 }
916
917 if (irq)
918 sr_info->irq = irq->start;
919
920 sr_set_clk_length(sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530921
922 list_add(&sr_info->node, &sr_list);
923
924 /*
925 * Call into late init to do intializations that require
926 * both sr driver and sr class driver to be initiallized.
927 */
928 if (sr_class) {
929 ret = sr_late_init(sr_info);
930 if (ret) {
931 pr_warning("%s: Error in SR late init\n", __func__);
Julia Lawall14ea9602012-01-12 10:55:17 +0100932 goto err_iounmap;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530933 }
934 }
935
936 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700937 if (!sr_dbg_dir) {
938 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100939 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
Kevin Hilman633ef8b2011-04-05 14:39:11 -0700940 ret = PTR_ERR(sr_dbg_dir);
941 pr_err("%s:sr debugfs dir creation failed(%d)\n",
942 __func__, ret);
943 goto err_iounmap;
944 }
Shweta Gulatib3329a32011-02-15 13:40:30 +0530945 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530946
Jean Pihet8b765d72012-04-24 10:41:27 +0530947 sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100948 if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530949 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
950 __func__);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530951 ret = PTR_ERR(sr_info->dbg_dir);
Jean Pihetce3810c2012-09-24 16:16:40 +0200952 goto err_debugfs;
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530953 }
954
Anand S Sawantb1ace382011-02-17 21:27:30 +0530955 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
956 sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
957 (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530958 &sr_info->err_weight);
Anand S Sawantb1ace382011-02-17 21:27:30 +0530959 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530960 &sr_info->err_maxlimit);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530961
Anand S Sawantb1ace382011-02-17 21:27:30 +0530962 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
Jean Pihet54b28cd2012-02-29 23:33:48 +0100963 if (IS_ERR_OR_NULL(nvalue_dir)) {
Thara Gopinath077fcec2010-10-27 20:29:37 +0530964 dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
965 "for n-values\n", __func__);
Shweta Gulatib3329a32011-02-15 13:40:30 +0530966 ret = PTR_ERR(nvalue_dir);
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700967 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530968 }
969
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530970 if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
971 dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
972 __func__, sr_info->name);
973
Shweta Gulatib3329a32011-02-15 13:40:30 +0530974 ret = -ENODATA;
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700975 goto err_debugfs;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530976 }
977
978 for (i = 0; i < sr_info->nvalue_count; i++) {
Aaro Koskinen865212ab2011-02-07 16:08:04 +0200979 char name[NVALUE_NAME_LEN + 1];
Thara Gopinath077fcec2010-10-27 20:29:37 +0530980
Jean Pihet5e7f2e12012-04-25 11:19:44 +0530981 snprintf(name, sizeof(name), "volt_%lu",
982 sr_info->nvalue_table[i].volt_nominal);
Vasiliy Kulikov1232a182011-02-04 12:23:20 +0000983 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530984 &(sr_info->nvalue_table[i].nvalue));
Jean Pihet308d1bd2012-04-24 23:41:54 +0530985 snprintf(name, sizeof(name), "errminlimit_%lu",
986 sr_info->nvalue_table[i].volt_nominal);
987 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
988 &(sr_info->nvalue_table[i].errminlimit));
989
Thara Gopinath077fcec2010-10-27 20:29:37 +0530990 }
Thara Gopinath984aa6d2010-05-29 22:02:22 +0530991
992 return ret;
993
Aaro Koskinen283a1c12011-04-26 02:25:32 -0700994err_debugfs:
995 debugfs_remove_recursive(sr_info->dbg_dir);
Aaro Koskinen0c49cc12011-04-26 02:25:21 -0700996err_iounmap:
Aaro Koskinen833d78f2011-04-26 02:25:27 -0700997 list_del(&sr_info->node);
Aaro Koskinen0c49cc12011-04-26 02:25:21 -0700998 iounmap(sr_info->base);
Jean Pihetce3810c2012-09-24 16:16:40 +0200999err_free_name:
1000 kfree(sr_info->name);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301001err_release_region:
1002 release_mem_region(mem->start, resource_size(mem));
1003err_free_devinfo:
1004 kfree(sr_info);
1005
1006 return ret;
1007}
1008
1009static int __devexit omap_sr_remove(struct platform_device *pdev)
1010{
1011 struct omap_sr_data *pdata = pdev->dev.platform_data;
1012 struct omap_sr *sr_info;
1013 struct resource *mem;
1014
1015 if (!pdata) {
1016 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1017 return -EINVAL;
1018 }
1019
1020 sr_info = _sr_lookup(pdata->voltdm);
Julia Lawall28693ec2011-01-24 20:55:22 +01001021 if (IS_ERR(sr_info)) {
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301022 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1023 __func__);
Jean Pihet63371fa2012-02-29 23:33:49 +01001024 return PTR_ERR(sr_info);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301025 }
1026
1027 if (sr_info->autocomp_active)
1028 sr_stop_vddautocomp(sr_info);
Anand S Sawantb1ace382011-02-17 21:27:30 +05301029 if (sr_info->dbg_dir)
1030 debugfs_remove_recursive(sr_info->dbg_dir);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301031
1032 list_del(&sr_info->node);
1033 iounmap(sr_info->base);
Jean Pihet8b765d72012-04-24 10:41:27 +05301034 kfree(sr_info->name);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301035 kfree(sr_info);
1036 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1037 release_mem_region(mem->start, resource_size(mem));
1038
1039 return 0;
1040}
1041
Nishanth Menon1f55bc1852012-03-01 00:29:44 +01001042static void __devexit omap_sr_shutdown(struct platform_device *pdev)
1043{
1044 struct omap_sr_data *pdata = pdev->dev.platform_data;
1045 struct omap_sr *sr_info;
1046
1047 if (!pdata) {
1048 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1049 return;
1050 }
1051
1052 sr_info = _sr_lookup(pdata->voltdm);
1053 if (IS_ERR(sr_info)) {
1054 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1055 __func__);
1056 return;
1057 }
1058
1059 if (sr_info->autocomp_active)
1060 sr_stop_vddautocomp(sr_info);
1061
1062 return;
1063}
1064
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301065static struct platform_driver smartreflex_driver = {
Tony Lindgren149f1d52012-02-23 14:55:40 -08001066 .remove = __devexit_p(omap_sr_remove),
Nishanth Menon1f55bc1852012-03-01 00:29:44 +01001067 .shutdown = __devexit_p(omap_sr_shutdown),
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301068 .driver = {
1069 .name = "smartreflex",
1070 },
1071};
1072
1073static int __init sr_init(void)
1074{
1075 int ret = 0;
1076
1077 /*
1078 * sr_init is a late init. If by then a pmic specific API is not
1079 * registered either there is no need for anything to be done on
1080 * the PMIC side or somebody has forgotten to register a PMIC
1081 * handler. Warn for the second condition.
1082 */
1083 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1084 sr_pmic_data->sr_pmic_init();
1085 else
1086 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
1087
1088 ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1089 if (ret) {
1090 pr_err("%s: platform driver register failed for SR\n",
1091 __func__);
1092 return ret;
1093 }
1094
1095 return 0;
1096}
Felipe Balbi1a21a682012-02-29 23:33:45 +01001097late_initcall(sr_init);
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301098
1099static void __exit sr_exit(void)
1100{
1101 platform_driver_unregister(&smartreflex_driver);
1102}
Thara Gopinath984aa6d2010-05-29 22:02:22 +05301103module_exit(sr_exit);
1104
1105MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1106MODULE_LICENSE("GPL");
1107MODULE_ALIAS("platform:" DRIVER_NAME);
1108MODULE_AUTHOR("Texas Instruments Inc");