blob: 580f1008d67a1f7ced469fe32a1aea3f25bcb9aa [file] [log] [blame]
Arun Murthydae2db32011-02-22 10:11:13 +01001/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License v2
5 * Author: Arun R Murthy <arun.murthy@stericsson.com>
Daniel Willerud63219922011-03-05 11:46:13 +01006 * Author: Daniel Willerud <daniel.willerud@stericsson.com>
Johan Palsson586f3312011-03-05 11:46:37 +01007 * Author: Johan Palsson <johan.palsson@stericsson.com>
Arun Murthydae2db32011-02-22 10:11:13 +01008 */
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/interrupt.h>
13#include <linux/spinlock.h>
14#include <linux/delay.h>
Lee Jones5f8aaef2013-02-04 08:33:13 +000015#include <linux/pm_runtime.h>
Arun Murthydae2db32011-02-22 10:11:13 +010016#include <linux/platform_device.h>
17#include <linux/completion.h>
18#include <linux/regulator/consumer.h>
19#include <linux/err.h>
20#include <linux/slab.h>
Daniel Willerud63219922011-03-05 11:46:13 +010021#include <linux/list.h>
Arun Murthydae2db32011-02-22 10:11:13 +010022#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010023#include <linux/mfd/abx500/ab8500.h>
24#include <linux/mfd/abx500/ab8500-gpadc.h>
Arun Murthydae2db32011-02-22 10:11:13 +010025
26/*
27 * GPADC register offsets
28 * Bank : 0x0A
29 */
30#define AB8500_GPADC_CTRL1_REG 0x00
31#define AB8500_GPADC_CTRL2_REG 0x01
32#define AB8500_GPADC_CTRL3_REG 0x02
33#define AB8500_GPADC_AUTO_TIMER_REG 0x03
34#define AB8500_GPADC_STAT_REG 0x04
35#define AB8500_GPADC_MANDATAL_REG 0x05
36#define AB8500_GPADC_MANDATAH_REG 0x06
37#define AB8500_GPADC_AUTODATAL_REG 0x07
38#define AB8500_GPADC_AUTODATAH_REG 0x08
39#define AB8500_GPADC_MUX_CTRL_REG 0x09
40
Johan Palsson586f3312011-03-05 11:46:37 +010041/*
42 * OTP register offsets
43 * Bank : 0x15
44 */
45#define AB8500_GPADC_CAL_1 0x0F
46#define AB8500_GPADC_CAL_2 0x10
47#define AB8500_GPADC_CAL_3 0x11
48#define AB8500_GPADC_CAL_4 0x12
49#define AB8500_GPADC_CAL_5 0x13
50#define AB8500_GPADC_CAL_6 0x14
51#define AB8500_GPADC_CAL_7 0x15
52
Arun Murthydae2db32011-02-22 10:11:13 +010053/* gpadc constants */
54#define EN_VINTCORE12 0x04
55#define EN_VTVOUT 0x02
56#define EN_GPADC 0x01
57#define DIS_GPADC 0x00
58#define SW_AVG_16 0x60
59#define ADC_SW_CONV 0x04
Karl Komierowski4aad5a92011-03-05 11:46:45 +010060#define EN_ICHAR 0x80
Johan Palssoned139412011-05-08 00:55:43 +020061#define BTEMP_PULL_UP 0x08
Arun Murthydae2db32011-02-22 10:11:13 +010062#define EN_BUF 0x40
63#define DIS_ZERO 0x00
64#define GPADC_BUSY 0x01
65
Johan Palsson586f3312011-03-05 11:46:37 +010066/* GPADC constants from AB8500 spec, UM0836 */
67#define ADC_RESOLUTION 1024
68#define ADC_CH_BTEMP_MIN 0
69#define ADC_CH_BTEMP_MAX 1350
70#define ADC_CH_DIETEMP_MIN 0
71#define ADC_CH_DIETEMP_MAX 1350
72#define ADC_CH_CHG_V_MIN 0
73#define ADC_CH_CHG_V_MAX 20030
74#define ADC_CH_ACCDET2_MIN 0
75#define ADC_CH_ACCDET2_MAX 2500
76#define ADC_CH_VBAT_MIN 2300
77#define ADC_CH_VBAT_MAX 4800
78#define ADC_CH_CHG_I_MIN 0
79#define ADC_CH_CHG_I_MAX 1500
80#define ADC_CH_BKBAT_MIN 0
81#define ADC_CH_BKBAT_MAX 3200
82
83/* This is used to not lose precision when dividing to get gain and offset */
84#define CALIB_SCALE 1000
85
Lee Jones5f8aaef2013-02-04 08:33:13 +000086/* Time in ms before disabling regulator */
87#define GPADC_AUDOSUSPEND_DELAY 1
88
Johan Palsson586f3312011-03-05 11:46:37 +010089enum cal_channels {
90 ADC_INPUT_VMAIN = 0,
91 ADC_INPUT_BTEMP,
92 ADC_INPUT_VBAT,
93 NBR_CAL_INPUTS,
94};
95
Arun Murthydae2db32011-02-22 10:11:13 +010096/**
Johan Palsson586f3312011-03-05 11:46:37 +010097 * struct adc_cal_data - Table for storing gain and offset for the calibrated
98 * ADC channels
99 * @gain: Gain of the ADC channel
100 * @offset: Offset of the ADC channel
101 */
102struct adc_cal_data {
103 u64 gain;
104 u64 offset;
105};
106
107/**
108 * struct ab8500_gpadc - AB8500 GPADC device information
Arun Murthydae2db32011-02-22 10:11:13 +0100109 * @dev: pointer to the struct device
Daniel Willerud63219922011-03-05 11:46:13 +0100110 * @node: a list of AB8500 GPADCs, hence prepared for
111 reentrance
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100112 * @parent: pointer to the struct ab8500
Arun Murthydae2db32011-02-22 10:11:13 +0100113 * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
114 * the completion of gpadc conversion
115 * @ab8500_gpadc_lock: structure of type mutex
116 * @regu: pointer to the struct regulator
117 * @irq: interrupt number that is used by gpadc
Johan Palsson586f3312011-03-05 11:46:37 +0100118 * @cal_data array of ADC calibration data structs
Arun Murthydae2db32011-02-22 10:11:13 +0100119 */
Daniel Willerud63219922011-03-05 11:46:13 +0100120struct ab8500_gpadc {
Arun Murthydae2db32011-02-22 10:11:13 +0100121 struct device *dev;
Daniel Willerud63219922011-03-05 11:46:13 +0100122 struct list_head node;
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100123 struct ab8500 *parent;
Arun Murthydae2db32011-02-22 10:11:13 +0100124 struct completion ab8500_gpadc_complete;
125 struct mutex ab8500_gpadc_lock;
126 struct regulator *regu;
127 int irq;
Johan Palsson586f3312011-03-05 11:46:37 +0100128 struct adc_cal_data cal_data[NBR_CAL_INPUTS];
Daniel Willerud63219922011-03-05 11:46:13 +0100129};
130
131static LIST_HEAD(ab8500_gpadc_list);
132
133/**
134 * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
135 * (i.e. the first GPADC in the instance list)
136 */
137struct ab8500_gpadc *ab8500_gpadc_get(char *name)
138{
139 struct ab8500_gpadc *gpadc;
140
141 list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
142 if (!strcmp(name, dev_name(gpadc->dev)))
143 return gpadc;
144 }
145
146 return ERR_PTR(-ENOENT);
147}
148EXPORT_SYMBOL(ab8500_gpadc_get);
Arun Murthydae2db32011-02-22 10:11:13 +0100149
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200150/**
151 * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
152 */
153int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel,
Johan Palsson586f3312011-03-05 11:46:37 +0100154 int ad_value)
155{
156 int res;
157
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200158 switch (channel) {
Johan Palsson586f3312011-03-05 11:46:37 +0100159 case MAIN_CHARGER_V:
160 /* For some reason we don't have calibrated data */
161 if (!gpadc->cal_data[ADC_INPUT_VMAIN].gain) {
162 res = ADC_CH_CHG_V_MIN + (ADC_CH_CHG_V_MAX -
163 ADC_CH_CHG_V_MIN) * ad_value /
164 ADC_RESOLUTION;
165 break;
166 }
167 /* Here we can use the calibrated data */
168 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VMAIN].gain +
169 gpadc->cal_data[ADC_INPUT_VMAIN].offset) / CALIB_SCALE;
170 break;
171
172 case BAT_CTRL:
173 case BTEMP_BALL:
174 case ACC_DETECT1:
175 case ADC_AUX1:
176 case ADC_AUX2:
177 /* For some reason we don't have calibrated data */
178 if (!gpadc->cal_data[ADC_INPUT_BTEMP].gain) {
179 res = ADC_CH_BTEMP_MIN + (ADC_CH_BTEMP_MAX -
180 ADC_CH_BTEMP_MIN) * ad_value /
181 ADC_RESOLUTION;
182 break;
183 }
184 /* Here we can use the calibrated data */
185 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_BTEMP].gain +
186 gpadc->cal_data[ADC_INPUT_BTEMP].offset) / CALIB_SCALE;
187 break;
188
189 case MAIN_BAT_V:
190 /* For some reason we don't have calibrated data */
191 if (!gpadc->cal_data[ADC_INPUT_VBAT].gain) {
192 res = ADC_CH_VBAT_MIN + (ADC_CH_VBAT_MAX -
193 ADC_CH_VBAT_MIN) * ad_value /
194 ADC_RESOLUTION;
195 break;
196 }
197 /* Here we can use the calibrated data */
198 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VBAT].gain +
199 gpadc->cal_data[ADC_INPUT_VBAT].offset) / CALIB_SCALE;
200 break;
201
202 case DIE_TEMP:
203 res = ADC_CH_DIETEMP_MIN +
204 (ADC_CH_DIETEMP_MAX - ADC_CH_DIETEMP_MIN) * ad_value /
205 ADC_RESOLUTION;
206 break;
207
208 case ACC_DETECT2:
209 res = ADC_CH_ACCDET2_MIN +
210 (ADC_CH_ACCDET2_MAX - ADC_CH_ACCDET2_MIN) * ad_value /
211 ADC_RESOLUTION;
212 break;
213
214 case VBUS_V:
215 res = ADC_CH_CHG_V_MIN +
216 (ADC_CH_CHG_V_MAX - ADC_CH_CHG_V_MIN) * ad_value /
217 ADC_RESOLUTION;
218 break;
219
220 case MAIN_CHARGER_C:
221 case USB_CHARGER_C:
222 res = ADC_CH_CHG_I_MIN +
223 (ADC_CH_CHG_I_MAX - ADC_CH_CHG_I_MIN) * ad_value /
224 ADC_RESOLUTION;
225 break;
226
227 case BK_BAT_V:
228 res = ADC_CH_BKBAT_MIN +
229 (ADC_CH_BKBAT_MAX - ADC_CH_BKBAT_MIN) * ad_value /
230 ADC_RESOLUTION;
231 break;
232
233 default:
234 dev_err(gpadc->dev,
235 "unknown channel, not possible to convert\n");
236 res = -EINVAL;
237 break;
238
239 }
240 return res;
241}
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200242EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage);
Johan Palsson586f3312011-03-05 11:46:37 +0100243
Arun Murthydae2db32011-02-22 10:11:13 +0100244/**
245 * ab8500_gpadc_convert() - gpadc conversion
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200246 * @channel: analog channel to be converted to digital data
Arun Murthydae2db32011-02-22 10:11:13 +0100247 *
248 * This function converts the selected analog i/p to digital
Johan Palsson586f3312011-03-05 11:46:37 +0100249 * data.
Arun Murthydae2db32011-02-22 10:11:13 +0100250 */
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200251int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 channel)
252{
253 int ad_value;
254 int voltage;
255
256 ad_value = ab8500_gpadc_read_raw(gpadc, channel);
257 if (ad_value < 0) {
258 dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n", channel);
259 return ad_value;
260 }
261
262 voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value);
263
264 if (voltage < 0)
265 dev_err(gpadc->dev, "GPADC to voltage conversion failed ch:"
266 " %d AD: 0x%x\n", channel, ad_value);
267
268 return voltage;
269}
270EXPORT_SYMBOL(ab8500_gpadc_convert);
271
272/**
273 * ab8500_gpadc_read_raw() - gpadc read
274 * @channel: analog channel to be read
275 *
276 * This function obtains the raw ADC value, this then needs
277 * to be converted by calling ab8500_gpadc_ad_to_voltage()
278 */
279int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel)
Arun Murthydae2db32011-02-22 10:11:13 +0100280{
281 int ret;
Arun Murthydae2db32011-02-22 10:11:13 +0100282 int looplimit = 0;
283 u8 val, low_data, high_data;
284
Daniel Willerud63219922011-03-05 11:46:13 +0100285 if (!gpadc)
Arun Murthydae2db32011-02-22 10:11:13 +0100286 return -ENODEV;
287
Daniel Willerud63219922011-03-05 11:46:13 +0100288 mutex_lock(&gpadc->ab8500_gpadc_lock);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000289
Arun Murthydae2db32011-02-22 10:11:13 +0100290 /* Enable VTVout LDO this is required for GPADC */
Lee Jones5f8aaef2013-02-04 08:33:13 +0000291 pm_runtime_get_sync(gpadc->dev);
Arun Murthydae2db32011-02-22 10:11:13 +0100292
293 /* Check if ADC is not busy, lock and proceed */
294 do {
Daniel Willerud63219922011-03-05 11:46:13 +0100295 ret = abx500_get_register_interruptible(gpadc->dev,
296 AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
Arun Murthydae2db32011-02-22 10:11:13 +0100297 if (ret < 0)
298 goto out;
299 if (!(val & GPADC_BUSY))
300 break;
301 msleep(10);
302 } while (++looplimit < 10);
303 if (looplimit >= 10 && (val & GPADC_BUSY)) {
Daniel Willerud63219922011-03-05 11:46:13 +0100304 dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
Arun Murthydae2db32011-02-22 10:11:13 +0100305 ret = -EINVAL;
306 goto out;
307 }
308
309 /* Enable GPADC */
Daniel Willerud63219922011-03-05 11:46:13 +0100310 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
311 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
Arun Murthydae2db32011-02-22 10:11:13 +0100312 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100313 dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100314 goto out;
315 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200316
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200317 /* Select the channel source and set average samples to 16 */
Daniel Willerud63219922011-03-05 11:46:13 +0100318 ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200319 AB8500_GPADC_CTRL2_REG, (channel | SW_AVG_16));
Arun Murthydae2db32011-02-22 10:11:13 +0100320 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100321 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100322 "gpadc_conversion: set avg samples failed\n");
323 goto out;
324 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200325
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100326 /*
327 * Enable ADC, buffering, select rising edge and enable ADC path
Karl Komierowskic9c95132011-05-08 00:55:31 +0200328 * charging current sense if it needed, ABB 3.0 needs some special
329 * treatment too.
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100330 */
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200331 switch (channel) {
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100332 case MAIN_CHARGER_C:
333 case USB_CHARGER_C:
334 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
335 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
336 EN_BUF | EN_ICHAR,
337 EN_BUF | EN_ICHAR);
338 break;
Karl Komierowskic9c95132011-05-08 00:55:31 +0200339 case BTEMP_BALL:
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100340 if (!is_ab8500_2p0_or_earlier(gpadc->parent)) {
Karl Komierowskic9c95132011-05-08 00:55:31 +0200341 /* Turn on btemp pull-up on ABB 3.0 */
342 ret = abx500_mask_and_set_register_interruptible(
343 gpadc->dev,
344 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
Johan Palssoned139412011-05-08 00:55:43 +0200345 EN_BUF | BTEMP_PULL_UP,
346 EN_BUF | BTEMP_PULL_UP);
Karl Komierowskic9c95132011-05-08 00:55:31 +0200347
348 /*
349 * Delay might be needed for ABB8500 cut 3.0, if not, remove
Masanari Iida5a4432b2012-08-13 21:00:25 +0900350 * when hardware will be available
Karl Komierowskic9c95132011-05-08 00:55:31 +0200351 */
Lee Jonesd0b32fa2011-08-29 08:32:36 +0200352 usleep_range(1000, 1000);
Karl Komierowskic9c95132011-05-08 00:55:31 +0200353 break;
354 }
355 /* Intentional fallthrough */
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100356 default:
357 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
358 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
359 break;
360 }
Arun Murthydae2db32011-02-22 10:11:13 +0100361 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100362 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100363 "gpadc_conversion: select falling edge failed\n");
364 goto out;
365 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200366
Daniel Willerud63219922011-03-05 11:46:13 +0100367 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
368 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV);
Arun Murthydae2db32011-02-22 10:11:13 +0100369 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100370 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100371 "gpadc_conversion: start s/w conversion failed\n");
372 goto out;
373 }
374 /* wait for completion of conversion */
Daniel Willerud63219922011-03-05 11:46:13 +0100375 if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, 2*HZ)) {
376 dev_err(gpadc->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300377 "timeout: didn't receive GPADC conversion interrupt\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100378 ret = -EINVAL;
379 goto out;
380 }
381
382 /* Read the converted RAW data */
Daniel Willerud63219922011-03-05 11:46:13 +0100383 ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100384 AB8500_GPADC_MANDATAL_REG, &low_data);
385 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100386 dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100387 goto out;
388 }
389
Daniel Willerud63219922011-03-05 11:46:13 +0100390 ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100391 AB8500_GPADC_MANDATAH_REG, &high_data);
392 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100393 dev_err(gpadc->dev,
394 "gpadc_conversion: read high data failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100395 goto out;
396 }
397
Arun Murthydae2db32011-02-22 10:11:13 +0100398 /* Disable GPADC */
Daniel Willerud63219922011-03-05 11:46:13 +0100399 ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100400 AB8500_GPADC_CTRL1_REG, DIS_GPADC);
401 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100402 dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100403 goto out;
404 }
Lee Jones5f8aaef2013-02-04 08:33:13 +0000405
406 pm_runtime_mark_last_busy(gpadc->dev);
407 pm_runtime_put_autosuspend(gpadc->dev);
408
Daniel Willerud63219922011-03-05 11:46:13 +0100409 mutex_unlock(&gpadc->ab8500_gpadc_lock);
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200410
411 return (high_data << 8) | low_data;
Arun Murthydae2db32011-02-22 10:11:13 +0100412
413out:
414 /*
415 * It has shown to be needed to turn off the GPADC if an error occurs,
416 * otherwise we might have problem when waiting for the busy bit in the
417 * GPADC status register to go low. In V1.1 there wait_for_completion
418 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
419 */
Daniel Willerud63219922011-03-05 11:46:13 +0100420 (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100421 AB8500_GPADC_CTRL1_REG, DIS_GPADC);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000422
423 pm_runtime_put(gpadc->dev);
424
Daniel Willerud63219922011-03-05 11:46:13 +0100425 mutex_unlock(&gpadc->ab8500_gpadc_lock);
426 dev_err(gpadc->dev,
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200427 "gpadc_conversion: Failed to AD convert channel %d\n", channel);
Arun Murthydae2db32011-02-22 10:11:13 +0100428 return ret;
429}
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200430EXPORT_SYMBOL(ab8500_gpadc_read_raw);
Arun Murthydae2db32011-02-22 10:11:13 +0100431
432/**
433 * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion
434 * @irq: irq number
435 * @data: pointer to the data passed during request irq
436 *
437 * This is a interrupt service routine for s/w gpadc conversion completion.
438 * Notifies the gpadc completion is completed and the converted raw value
439 * can be read from the registers.
440 * Returns IRQ status(IRQ_HANDLED)
441 */
Daniel Willerud63219922011-03-05 11:46:13 +0100442static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_gpadc)
Arun Murthydae2db32011-02-22 10:11:13 +0100443{
Daniel Willerud63219922011-03-05 11:46:13 +0100444 struct ab8500_gpadc *gpadc = _gpadc;
Arun Murthydae2db32011-02-22 10:11:13 +0100445
446 complete(&gpadc->ab8500_gpadc_complete);
447
448 return IRQ_HANDLED;
449}
450
Johan Palsson586f3312011-03-05 11:46:37 +0100451static int otp_cal_regs[] = {
452 AB8500_GPADC_CAL_1,
453 AB8500_GPADC_CAL_2,
454 AB8500_GPADC_CAL_3,
455 AB8500_GPADC_CAL_4,
456 AB8500_GPADC_CAL_5,
457 AB8500_GPADC_CAL_6,
458 AB8500_GPADC_CAL_7,
459};
460
461static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
462{
463 int i;
464 int ret[ARRAY_SIZE(otp_cal_regs)];
465 u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
466
467 int vmain_high, vmain_low;
468 int btemp_high, btemp_low;
469 int vbat_high, vbat_low;
470
471 /* First we read all OTP registers and store the error code */
472 for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
473 ret[i] = abx500_get_register_interruptible(gpadc->dev,
474 AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]);
475 if (ret[i] < 0)
476 dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
477 __func__, otp_cal_regs[i]);
478 }
479
480 /*
481 * The ADC calibration data is stored in OTP registers.
482 * The layout of the calibration data is outlined below and a more
483 * detailed description can be found in UM0836
484 *
485 * vm_h/l = vmain_high/low
486 * bt_h/l = btemp_high/low
487 * vb_h/l = vbat_high/low
488 *
489 * Data bits:
490 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
491 * |.......|.......|.......|.......|.......|.......|.......|.......
492 * | | vm_h9 | vm_h8
493 * |.......|.......|.......|.......|.......|.......|.......|.......
494 * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
495 * |.......|.......|.......|.......|.......|.......|.......|.......
496 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
497 * |.......|.......|.......|.......|.......|.......|.......|.......
498 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
499 * |.......|.......|.......|.......|.......|.......|.......|.......
500 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
501 * |.......|.......|.......|.......|.......|.......|.......|.......
502 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
503 * |.......|.......|.......|.......|.......|.......|.......|.......
504 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
505 * |.......|.......|.......|.......|.......|.......|.......|.......
506 *
507 *
508 * Ideal output ADC codes corresponding to injected input voltages
509 * during manufacturing is:
510 *
511 * vmain_high: Vin = 19500mV / ADC ideal code = 997
512 * vmain_low: Vin = 315mV / ADC ideal code = 16
513 * btemp_high: Vin = 1300mV / ADC ideal code = 985
514 * btemp_low: Vin = 21mV / ADC ideal code = 16
515 * vbat_high: Vin = 4700mV / ADC ideal code = 982
516 * vbat_low: Vin = 2380mV / ADC ideal code = 33
517 */
518
519 /* Calculate gain and offset for VMAIN if all reads succeeded */
520 if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
521 vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
522 ((gpadc_cal[1] & 0x3F) << 2) |
523 ((gpadc_cal[2] & 0xC0) >> 6));
524
525 vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
526
527 gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
528 (19500 - 315) / (vmain_high - vmain_low);
529
530 gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 19500 -
531 (CALIB_SCALE * (19500 - 315) /
532 (vmain_high - vmain_low)) * vmain_high;
533 } else {
534 gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
535 }
536
537 /* Calculate gain and offset for BTEMP if all reads succeeded */
538 if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
539 btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
540 (gpadc_cal[3] << 1) |
541 ((gpadc_cal[4] & 0x80) >> 7));
542
543 btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
544
545 gpadc->cal_data[ADC_INPUT_BTEMP].gain =
546 CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
547
548 gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 -
549 (CALIB_SCALE * (1300 - 21) /
550 (btemp_high - btemp_low)) * btemp_high;
551 } else {
552 gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0;
553 }
554
555 /* Calculate gain and offset for VBAT if all reads succeeded */
556 if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
557 vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
558 vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
559
560 gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE *
561 (4700 - 2380) / (vbat_high - vbat_low);
562
563 gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 -
564 (CALIB_SCALE * (4700 - 2380) /
565 (vbat_high - vbat_low)) * vbat_high;
566 } else {
567 gpadc->cal_data[ADC_INPUT_VBAT].gain = 0;
568 }
569
570 dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n",
571 gpadc->cal_data[ADC_INPUT_VMAIN].gain,
572 gpadc->cal_data[ADC_INPUT_VMAIN].offset);
573
574 dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n",
575 gpadc->cal_data[ADC_INPUT_BTEMP].gain,
576 gpadc->cal_data[ADC_INPUT_BTEMP].offset);
577
578 dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n",
579 gpadc->cal_data[ADC_INPUT_VBAT].gain,
580 gpadc->cal_data[ADC_INPUT_VBAT].offset);
581}
582
Lee Jones5f8aaef2013-02-04 08:33:13 +0000583static int ab8500_gpadc_runtime_suspend(struct device *dev)
584{
585 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
586
587 regulator_disable(gpadc->regu);
588 return 0;
589}
590
591static int ab8500_gpadc_runtime_resume(struct device *dev)
592{
593 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
594
595 regulator_enable(gpadc->regu);
596 return 0;
597}
598
599static int ab8500_gpadc_runtime_idle(struct device *dev)
600{
601 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
602
603 pm_runtime_suspend(dev);
604 return 0;
605}
606
Bill Pembertonf791be42012-11-19 13:23:04 -0500607static int ab8500_gpadc_probe(struct platform_device *pdev)
Arun Murthydae2db32011-02-22 10:11:13 +0100608{
609 int ret = 0;
610 struct ab8500_gpadc *gpadc;
611
612 gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
613 if (!gpadc) {
614 dev_err(&pdev->dev, "Error: No memory\n");
615 return -ENOMEM;
616 }
617
Arun Murthydae2db32011-02-22 10:11:13 +0100618 gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END");
619 if (gpadc->irq < 0) {
Lee Jones6dff11e2012-05-17 14:45:20 +0100620 dev_err(&pdev->dev, "failed to get platform irq-%d\n",
Daniel Willerud63219922011-03-05 11:46:13 +0100621 gpadc->irq);
Arun Murthydae2db32011-02-22 10:11:13 +0100622 ret = gpadc->irq;
623 goto fail;
624 }
625
626 gpadc->dev = &pdev->dev;
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100627 gpadc->parent = dev_get_drvdata(pdev->dev.parent);
Daniel Willerud63219922011-03-05 11:46:13 +0100628 mutex_init(&gpadc->ab8500_gpadc_lock);
Arun Murthydae2db32011-02-22 10:11:13 +0100629
630 /* Initialize completion used to notify completion of conversion */
631 init_completion(&gpadc->ab8500_gpadc_complete);
632
633 /* Register interrupt - SwAdcComplete */
634 ret = request_threaded_irq(gpadc->irq, NULL,
635 ab8500_bm_gpswadcconvend_handler,
Lee Jones6e19e832012-05-30 12:47:34 +0800636 IRQF_ONESHOT | IRQF_NO_SUSPEND | IRQF_SHARED,
637 "ab8500-gpadc", gpadc);
Arun Murthydae2db32011-02-22 10:11:13 +0100638 if (ret < 0) {
639 dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
640 gpadc->irq);
641 goto fail;
642 }
643
644 /* VTVout LDO used to power up ab8500-GPADC */
645 gpadc->regu = regulator_get(&pdev->dev, "vddadc");
646 if (IS_ERR(gpadc->regu)) {
647 ret = PTR_ERR(gpadc->regu);
648 dev_err(gpadc->dev, "failed to get vtvout LDO\n");
Daniel Willerud633e0fa2011-03-05 11:46:27 +0100649 goto fail_irq;
Arun Murthydae2db32011-02-22 10:11:13 +0100650 }
Lee Jones5f8aaef2013-02-04 08:33:13 +0000651
652 platform_set_drvdata(pdev, gpadc);
653
654 regulator_enable(gpadc->regu);
655
656 pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY);
657 pm_runtime_use_autosuspend(gpadc->dev);
658 pm_runtime_set_active(gpadc->dev);
659 pm_runtime_enable(gpadc->dev);
660
Johan Palsson586f3312011-03-05 11:46:37 +0100661 ab8500_gpadc_read_calibration_data(gpadc);
Daniel Willerud63219922011-03-05 11:46:13 +0100662 list_add_tail(&gpadc->node, &ab8500_gpadc_list);
Arun Murthydae2db32011-02-22 10:11:13 +0100663 dev_dbg(gpadc->dev, "probe success\n");
664 return 0;
Daniel Willerud633e0fa2011-03-05 11:46:27 +0100665fail_irq:
666 free_irq(gpadc->irq, gpadc);
Arun Murthydae2db32011-02-22 10:11:13 +0100667fail:
668 kfree(gpadc);
669 gpadc = NULL;
670 return ret;
671}
672
Bill Pemberton4740f732012-11-19 13:26:01 -0500673static int ab8500_gpadc_remove(struct platform_device *pdev)
Arun Murthydae2db32011-02-22 10:11:13 +0100674{
675 struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
676
Daniel Willerud63219922011-03-05 11:46:13 +0100677 /* remove this gpadc entry from the list */
678 list_del(&gpadc->node);
Arun Murthydae2db32011-02-22 10:11:13 +0100679 /* remove interrupt - completion of Sw ADC conversion */
Daniel Willerud63219922011-03-05 11:46:13 +0100680 free_irq(gpadc->irq, gpadc);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000681
682 pm_runtime_get_sync(gpadc->dev);
683 pm_runtime_disable(gpadc->dev);
684
685 regulator_disable(gpadc->regu);
686
687 pm_runtime_set_suspended(gpadc->dev);
688
689 pm_runtime_put_noidle(gpadc->dev);
690
Arun Murthydae2db32011-02-22 10:11:13 +0100691 kfree(gpadc);
692 gpadc = NULL;
693 return 0;
694}
695
Lee Jones5f8aaef2013-02-04 08:33:13 +0000696static const struct dev_pm_ops ab8500_gpadc_pm_ops = {
697 SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend,
698 ab8500_gpadc_runtime_resume,
699 ab8500_gpadc_runtime_idle)
700};
701
Arun Murthydae2db32011-02-22 10:11:13 +0100702static struct platform_driver ab8500_gpadc_driver = {
703 .probe = ab8500_gpadc_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500704 .remove = ab8500_gpadc_remove,
Arun Murthydae2db32011-02-22 10:11:13 +0100705 .driver = {
706 .name = "ab8500-gpadc",
707 .owner = THIS_MODULE,
Lee Jones5f8aaef2013-02-04 08:33:13 +0000708 .pm = &ab8500_gpadc_pm_ops,
Arun Murthydae2db32011-02-22 10:11:13 +0100709 },
710};
711
712static int __init ab8500_gpadc_init(void)
713{
714 return platform_driver_register(&ab8500_gpadc_driver);
715}
716
717static void __exit ab8500_gpadc_exit(void)
718{
719 platform_driver_unregister(&ab8500_gpadc_driver);
720}
721
722subsys_initcall_sync(ab8500_gpadc_init);
723module_exit(ab8500_gpadc_exit);
724
725MODULE_LICENSE("GPL v2");
Johan Palsson586f3312011-03-05 11:46:37 +0100726MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson");
Arun Murthydae2db32011-02-22 10:11:13 +0100727MODULE_ALIAS("platform:ab8500_gpadc");
728MODULE_DESCRIPTION("AB8500 GPADC driver");