blob: 5f341a50ee5a5783e6e84118124e9efcfe177bd3 [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
Lee Jonesf825ebe2013-01-28 09:20:45 +000089#define CONVERSION_TIME 500 /* ms */
90
Johan Palsson586f3312011-03-05 11:46:37 +010091enum cal_channels {
92 ADC_INPUT_VMAIN = 0,
93 ADC_INPUT_BTEMP,
94 ADC_INPUT_VBAT,
95 NBR_CAL_INPUTS,
96};
97
Arun Murthydae2db32011-02-22 10:11:13 +010098/**
Johan Palsson586f3312011-03-05 11:46:37 +010099 * struct adc_cal_data - Table for storing gain and offset for the calibrated
100 * ADC channels
101 * @gain: Gain of the ADC channel
102 * @offset: Offset of the ADC channel
103 */
104struct adc_cal_data {
105 u64 gain;
106 u64 offset;
107};
108
109/**
110 * struct ab8500_gpadc - AB8500 GPADC device information
Arun Murthydae2db32011-02-22 10:11:13 +0100111 * @dev: pointer to the struct device
Daniel Willerud63219922011-03-05 11:46:13 +0100112 * @node: a list of AB8500 GPADCs, hence prepared for
113 reentrance
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100114 * @parent: pointer to the struct ab8500
Arun Murthydae2db32011-02-22 10:11:13 +0100115 * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
116 * the completion of gpadc conversion
117 * @ab8500_gpadc_lock: structure of type mutex
118 * @regu: pointer to the struct regulator
119 * @irq: interrupt number that is used by gpadc
Johan Palsson586f3312011-03-05 11:46:37 +0100120 * @cal_data array of ADC calibration data structs
Arun Murthydae2db32011-02-22 10:11:13 +0100121 */
Daniel Willerud63219922011-03-05 11:46:13 +0100122struct ab8500_gpadc {
Arun Murthydae2db32011-02-22 10:11:13 +0100123 struct device *dev;
Daniel Willerud63219922011-03-05 11:46:13 +0100124 struct list_head node;
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100125 struct ab8500 *parent;
Arun Murthydae2db32011-02-22 10:11:13 +0100126 struct completion ab8500_gpadc_complete;
127 struct mutex ab8500_gpadc_lock;
128 struct regulator *regu;
129 int irq;
Johan Palsson586f3312011-03-05 11:46:37 +0100130 struct adc_cal_data cal_data[NBR_CAL_INPUTS];
Daniel Willerud63219922011-03-05 11:46:13 +0100131};
132
133static LIST_HEAD(ab8500_gpadc_list);
134
135/**
136 * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
137 * (i.e. the first GPADC in the instance list)
138 */
139struct ab8500_gpadc *ab8500_gpadc_get(char *name)
140{
141 struct ab8500_gpadc *gpadc;
142
143 list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
144 if (!strcmp(name, dev_name(gpadc->dev)))
145 return gpadc;
146 }
147
148 return ERR_PTR(-ENOENT);
149}
150EXPORT_SYMBOL(ab8500_gpadc_get);
Arun Murthydae2db32011-02-22 10:11:13 +0100151
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200152/**
153 * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
154 */
155int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel,
Johan Palsson586f3312011-03-05 11:46:37 +0100156 int ad_value)
157{
158 int res;
159
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200160 switch (channel) {
Johan Palsson586f3312011-03-05 11:46:37 +0100161 case MAIN_CHARGER_V:
162 /* For some reason we don't have calibrated data */
163 if (!gpadc->cal_data[ADC_INPUT_VMAIN].gain) {
164 res = ADC_CH_CHG_V_MIN + (ADC_CH_CHG_V_MAX -
165 ADC_CH_CHG_V_MIN) * ad_value /
166 ADC_RESOLUTION;
167 break;
168 }
169 /* Here we can use the calibrated data */
170 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VMAIN].gain +
171 gpadc->cal_data[ADC_INPUT_VMAIN].offset) / CALIB_SCALE;
172 break;
173
174 case BAT_CTRL:
175 case BTEMP_BALL:
176 case ACC_DETECT1:
177 case ADC_AUX1:
178 case ADC_AUX2:
179 /* For some reason we don't have calibrated data */
180 if (!gpadc->cal_data[ADC_INPUT_BTEMP].gain) {
181 res = ADC_CH_BTEMP_MIN + (ADC_CH_BTEMP_MAX -
182 ADC_CH_BTEMP_MIN) * ad_value /
183 ADC_RESOLUTION;
184 break;
185 }
186 /* Here we can use the calibrated data */
187 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_BTEMP].gain +
188 gpadc->cal_data[ADC_INPUT_BTEMP].offset) / CALIB_SCALE;
189 break;
190
191 case MAIN_BAT_V:
192 /* For some reason we don't have calibrated data */
193 if (!gpadc->cal_data[ADC_INPUT_VBAT].gain) {
194 res = ADC_CH_VBAT_MIN + (ADC_CH_VBAT_MAX -
195 ADC_CH_VBAT_MIN) * ad_value /
196 ADC_RESOLUTION;
197 break;
198 }
199 /* Here we can use the calibrated data */
200 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VBAT].gain +
201 gpadc->cal_data[ADC_INPUT_VBAT].offset) / CALIB_SCALE;
202 break;
203
204 case DIE_TEMP:
205 res = ADC_CH_DIETEMP_MIN +
206 (ADC_CH_DIETEMP_MAX - ADC_CH_DIETEMP_MIN) * ad_value /
207 ADC_RESOLUTION;
208 break;
209
210 case ACC_DETECT2:
211 res = ADC_CH_ACCDET2_MIN +
212 (ADC_CH_ACCDET2_MAX - ADC_CH_ACCDET2_MIN) * ad_value /
213 ADC_RESOLUTION;
214 break;
215
216 case VBUS_V:
217 res = ADC_CH_CHG_V_MIN +
218 (ADC_CH_CHG_V_MAX - ADC_CH_CHG_V_MIN) * ad_value /
219 ADC_RESOLUTION;
220 break;
221
222 case MAIN_CHARGER_C:
223 case USB_CHARGER_C:
224 res = ADC_CH_CHG_I_MIN +
225 (ADC_CH_CHG_I_MAX - ADC_CH_CHG_I_MIN) * ad_value /
226 ADC_RESOLUTION;
227 break;
228
229 case BK_BAT_V:
230 res = ADC_CH_BKBAT_MIN +
231 (ADC_CH_BKBAT_MAX - ADC_CH_BKBAT_MIN) * ad_value /
232 ADC_RESOLUTION;
233 break;
234
235 default:
236 dev_err(gpadc->dev,
237 "unknown channel, not possible to convert\n");
238 res = -EINVAL;
239 break;
240
241 }
242 return res;
243}
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200244EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage);
Johan Palsson586f3312011-03-05 11:46:37 +0100245
Arun Murthydae2db32011-02-22 10:11:13 +0100246/**
247 * ab8500_gpadc_convert() - gpadc conversion
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200248 * @channel: analog channel to be converted to digital data
Arun Murthydae2db32011-02-22 10:11:13 +0100249 *
250 * This function converts the selected analog i/p to digital
Johan Palsson586f3312011-03-05 11:46:37 +0100251 * data.
Arun Murthydae2db32011-02-22 10:11:13 +0100252 */
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200253int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 channel)
254{
255 int ad_value;
256 int voltage;
257
258 ad_value = ab8500_gpadc_read_raw(gpadc, channel);
259 if (ad_value < 0) {
260 dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n", channel);
261 return ad_value;
262 }
263
264 voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value);
265
266 if (voltage < 0)
267 dev_err(gpadc->dev, "GPADC to voltage conversion failed ch:"
268 " %d AD: 0x%x\n", channel, ad_value);
269
270 return voltage;
271}
272EXPORT_SYMBOL(ab8500_gpadc_convert);
273
274/**
275 * ab8500_gpadc_read_raw() - gpadc read
276 * @channel: analog channel to be read
277 *
278 * This function obtains the raw ADC value, this then needs
279 * to be converted by calling ab8500_gpadc_ad_to_voltage()
280 */
281int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel)
Arun Murthydae2db32011-02-22 10:11:13 +0100282{
283 int ret;
Arun Murthydae2db32011-02-22 10:11:13 +0100284 int looplimit = 0;
285 u8 val, low_data, high_data;
286
Daniel Willerud63219922011-03-05 11:46:13 +0100287 if (!gpadc)
Arun Murthydae2db32011-02-22 10:11:13 +0100288 return -ENODEV;
289
Daniel Willerud63219922011-03-05 11:46:13 +0100290 mutex_lock(&gpadc->ab8500_gpadc_lock);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000291
Arun Murthydae2db32011-02-22 10:11:13 +0100292 /* Enable VTVout LDO this is required for GPADC */
Lee Jones5f8aaef2013-02-04 08:33:13 +0000293 pm_runtime_get_sync(gpadc->dev);
Arun Murthydae2db32011-02-22 10:11:13 +0100294
295 /* Check if ADC is not busy, lock and proceed */
296 do {
Daniel Willerud63219922011-03-05 11:46:13 +0100297 ret = abx500_get_register_interruptible(gpadc->dev,
298 AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
Arun Murthydae2db32011-02-22 10:11:13 +0100299 if (ret < 0)
300 goto out;
301 if (!(val & GPADC_BUSY))
302 break;
303 msleep(10);
304 } while (++looplimit < 10);
305 if (looplimit >= 10 && (val & GPADC_BUSY)) {
Daniel Willerud63219922011-03-05 11:46:13 +0100306 dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
Arun Murthydae2db32011-02-22 10:11:13 +0100307 ret = -EINVAL;
308 goto out;
309 }
310
311 /* Enable GPADC */
Daniel Willerud63219922011-03-05 11:46:13 +0100312 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
313 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
Arun Murthydae2db32011-02-22 10:11:13 +0100314 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100315 dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100316 goto out;
317 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200318
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200319 /* Select the channel source and set average samples to 16 */
Daniel Willerud63219922011-03-05 11:46:13 +0100320 ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200321 AB8500_GPADC_CTRL2_REG, (channel | SW_AVG_16));
Arun Murthydae2db32011-02-22 10:11:13 +0100322 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100323 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100324 "gpadc_conversion: set avg samples failed\n");
325 goto out;
326 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200327
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100328 /*
329 * Enable ADC, buffering, select rising edge and enable ADC path
Karl Komierowskic9c95132011-05-08 00:55:31 +0200330 * charging current sense if it needed, ABB 3.0 needs some special
331 * treatment too.
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100332 */
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200333 switch (channel) {
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100334 case MAIN_CHARGER_C:
335 case USB_CHARGER_C:
336 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
337 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
338 EN_BUF | EN_ICHAR,
339 EN_BUF | EN_ICHAR);
340 break;
Karl Komierowskic9c95132011-05-08 00:55:31 +0200341 case BTEMP_BALL:
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100342 if (!is_ab8500_2p0_or_earlier(gpadc->parent)) {
Karl Komierowskic9c95132011-05-08 00:55:31 +0200343 /* Turn on btemp pull-up on ABB 3.0 */
344 ret = abx500_mask_and_set_register_interruptible(
345 gpadc->dev,
346 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
Johan Palssoned139412011-05-08 00:55:43 +0200347 EN_BUF | BTEMP_PULL_UP,
348 EN_BUF | BTEMP_PULL_UP);
Karl Komierowskic9c95132011-05-08 00:55:31 +0200349
350 /*
351 * Delay might be needed for ABB8500 cut 3.0, if not, remove
Masanari Iida5a4432b2012-08-13 21:00:25 +0900352 * when hardware will be available
Karl Komierowskic9c95132011-05-08 00:55:31 +0200353 */
Lee Jonesd0b32fa2011-08-29 08:32:36 +0200354 usleep_range(1000, 1000);
Karl Komierowskic9c95132011-05-08 00:55:31 +0200355 break;
356 }
357 /* Intentional fallthrough */
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100358 default:
359 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
360 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
361 break;
362 }
Arun Murthydae2db32011-02-22 10:11:13 +0100363 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100364 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100365 "gpadc_conversion: select falling edge failed\n");
366 goto out;
367 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200368
Daniel Willerud63219922011-03-05 11:46:13 +0100369 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
370 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV);
Arun Murthydae2db32011-02-22 10:11:13 +0100371 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100372 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100373 "gpadc_conversion: start s/w conversion failed\n");
374 goto out;
375 }
376 /* wait for completion of conversion */
Lee Jonesf825ebe2013-01-28 09:20:45 +0000377 if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete,
378 msecs_to_jiffies(CONVERSION_TIME))) {
Daniel Willerud63219922011-03-05 11:46:13 +0100379 dev_err(gpadc->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300380 "timeout: didn't receive GPADC conversion interrupt\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100381 ret = -EINVAL;
382 goto out;
383 }
384
385 /* Read the converted RAW data */
Daniel Willerud63219922011-03-05 11:46:13 +0100386 ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100387 AB8500_GPADC_MANDATAL_REG, &low_data);
388 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100389 dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100390 goto out;
391 }
392
Daniel Willerud63219922011-03-05 11:46:13 +0100393 ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100394 AB8500_GPADC_MANDATAH_REG, &high_data);
395 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100396 dev_err(gpadc->dev,
397 "gpadc_conversion: read high data failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100398 goto out;
399 }
400
Arun Murthydae2db32011-02-22 10:11:13 +0100401 /* Disable GPADC */
Daniel Willerud63219922011-03-05 11:46:13 +0100402 ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100403 AB8500_GPADC_CTRL1_REG, DIS_GPADC);
404 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100405 dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100406 goto out;
407 }
Lee Jones5f8aaef2013-02-04 08:33:13 +0000408
409 pm_runtime_mark_last_busy(gpadc->dev);
410 pm_runtime_put_autosuspend(gpadc->dev);
411
Daniel Willerud63219922011-03-05 11:46:13 +0100412 mutex_unlock(&gpadc->ab8500_gpadc_lock);
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200413
414 return (high_data << 8) | low_data;
Arun Murthydae2db32011-02-22 10:11:13 +0100415
416out:
417 /*
418 * It has shown to be needed to turn off the GPADC if an error occurs,
419 * otherwise we might have problem when waiting for the busy bit in the
420 * GPADC status register to go low. In V1.1 there wait_for_completion
421 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
422 */
Daniel Willerud63219922011-03-05 11:46:13 +0100423 (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100424 AB8500_GPADC_CTRL1_REG, DIS_GPADC);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000425
426 pm_runtime_put(gpadc->dev);
427
Daniel Willerud63219922011-03-05 11:46:13 +0100428 mutex_unlock(&gpadc->ab8500_gpadc_lock);
429 dev_err(gpadc->dev,
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200430 "gpadc_conversion: Failed to AD convert channel %d\n", channel);
Arun Murthydae2db32011-02-22 10:11:13 +0100431 return ret;
432}
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200433EXPORT_SYMBOL(ab8500_gpadc_read_raw);
Arun Murthydae2db32011-02-22 10:11:13 +0100434
435/**
436 * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion
437 * @irq: irq number
438 * @data: pointer to the data passed during request irq
439 *
440 * This is a interrupt service routine for s/w gpadc conversion completion.
441 * Notifies the gpadc completion is completed and the converted raw value
442 * can be read from the registers.
443 * Returns IRQ status(IRQ_HANDLED)
444 */
Daniel Willerud63219922011-03-05 11:46:13 +0100445static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_gpadc)
Arun Murthydae2db32011-02-22 10:11:13 +0100446{
Daniel Willerud63219922011-03-05 11:46:13 +0100447 struct ab8500_gpadc *gpadc = _gpadc;
Arun Murthydae2db32011-02-22 10:11:13 +0100448
449 complete(&gpadc->ab8500_gpadc_complete);
450
451 return IRQ_HANDLED;
452}
453
Johan Palsson586f3312011-03-05 11:46:37 +0100454static int otp_cal_regs[] = {
455 AB8500_GPADC_CAL_1,
456 AB8500_GPADC_CAL_2,
457 AB8500_GPADC_CAL_3,
458 AB8500_GPADC_CAL_4,
459 AB8500_GPADC_CAL_5,
460 AB8500_GPADC_CAL_6,
461 AB8500_GPADC_CAL_7,
462};
463
464static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
465{
466 int i;
467 int ret[ARRAY_SIZE(otp_cal_regs)];
468 u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
469
470 int vmain_high, vmain_low;
471 int btemp_high, btemp_low;
472 int vbat_high, vbat_low;
473
474 /* First we read all OTP registers and store the error code */
475 for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
476 ret[i] = abx500_get_register_interruptible(gpadc->dev,
477 AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]);
478 if (ret[i] < 0)
479 dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
480 __func__, otp_cal_regs[i]);
481 }
482
483 /*
484 * The ADC calibration data is stored in OTP registers.
485 * The layout of the calibration data is outlined below and a more
486 * detailed description can be found in UM0836
487 *
488 * vm_h/l = vmain_high/low
489 * bt_h/l = btemp_high/low
490 * vb_h/l = vbat_high/low
491 *
492 * Data bits:
493 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
494 * |.......|.......|.......|.......|.......|.......|.......|.......
495 * | | vm_h9 | vm_h8
496 * |.......|.......|.......|.......|.......|.......|.......|.......
497 * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
498 * |.......|.......|.......|.......|.......|.......|.......|.......
499 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
500 * |.......|.......|.......|.......|.......|.......|.......|.......
501 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
502 * |.......|.......|.......|.......|.......|.......|.......|.......
503 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
504 * |.......|.......|.......|.......|.......|.......|.......|.......
505 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
506 * |.......|.......|.......|.......|.......|.......|.......|.......
507 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
508 * |.......|.......|.......|.......|.......|.......|.......|.......
509 *
510 *
511 * Ideal output ADC codes corresponding to injected input voltages
512 * during manufacturing is:
513 *
514 * vmain_high: Vin = 19500mV / ADC ideal code = 997
515 * vmain_low: Vin = 315mV / ADC ideal code = 16
516 * btemp_high: Vin = 1300mV / ADC ideal code = 985
517 * btemp_low: Vin = 21mV / ADC ideal code = 16
518 * vbat_high: Vin = 4700mV / ADC ideal code = 982
519 * vbat_low: Vin = 2380mV / ADC ideal code = 33
520 */
521
522 /* Calculate gain and offset for VMAIN if all reads succeeded */
523 if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
524 vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
525 ((gpadc_cal[1] & 0x3F) << 2) |
526 ((gpadc_cal[2] & 0xC0) >> 6));
527
528 vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
529
530 gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
531 (19500 - 315) / (vmain_high - vmain_low);
532
533 gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 19500 -
534 (CALIB_SCALE * (19500 - 315) /
535 (vmain_high - vmain_low)) * vmain_high;
536 } else {
537 gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
538 }
539
540 /* Calculate gain and offset for BTEMP if all reads succeeded */
541 if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
542 btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
543 (gpadc_cal[3] << 1) |
544 ((gpadc_cal[4] & 0x80) >> 7));
545
546 btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
547
548 gpadc->cal_data[ADC_INPUT_BTEMP].gain =
549 CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
550
551 gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 -
552 (CALIB_SCALE * (1300 - 21) /
553 (btemp_high - btemp_low)) * btemp_high;
554 } else {
555 gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0;
556 }
557
558 /* Calculate gain and offset for VBAT if all reads succeeded */
559 if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
560 vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
561 vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
562
563 gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE *
564 (4700 - 2380) / (vbat_high - vbat_low);
565
566 gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 -
567 (CALIB_SCALE * (4700 - 2380) /
568 (vbat_high - vbat_low)) * vbat_high;
569 } else {
570 gpadc->cal_data[ADC_INPUT_VBAT].gain = 0;
571 }
572
573 dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n",
574 gpadc->cal_data[ADC_INPUT_VMAIN].gain,
575 gpadc->cal_data[ADC_INPUT_VMAIN].offset);
576
577 dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n",
578 gpadc->cal_data[ADC_INPUT_BTEMP].gain,
579 gpadc->cal_data[ADC_INPUT_BTEMP].offset);
580
581 dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n",
582 gpadc->cal_data[ADC_INPUT_VBAT].gain,
583 gpadc->cal_data[ADC_INPUT_VBAT].offset);
584}
585
Lee Jones5f8aaef2013-02-04 08:33:13 +0000586static int ab8500_gpadc_runtime_suspend(struct device *dev)
587{
588 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
589
590 regulator_disable(gpadc->regu);
591 return 0;
592}
593
594static int ab8500_gpadc_runtime_resume(struct device *dev)
595{
596 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
Axel Lin54fc4032013-03-09 17:46:45 +0800597 int ret;
Lee Jones5f8aaef2013-02-04 08:33:13 +0000598
Axel Lin54fc4032013-03-09 17:46:45 +0800599 ret = regulator_enable(gpadc->regu);
600 if (ret)
601 dev_err(dev, "Failed to enable vtvout LDO: %d\n", ret);
602 return ret;
Lee Jones5f8aaef2013-02-04 08:33:13 +0000603}
604
605static int ab8500_gpadc_runtime_idle(struct device *dev)
606{
Lee Jones5f8aaef2013-02-04 08:33:13 +0000607 pm_runtime_suspend(dev);
608 return 0;
609}
610
Bill Pembertonf791be42012-11-19 13:23:04 -0500611static int ab8500_gpadc_probe(struct platform_device *pdev)
Arun Murthydae2db32011-02-22 10:11:13 +0100612{
613 int ret = 0;
614 struct ab8500_gpadc *gpadc;
615
616 gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
617 if (!gpadc) {
618 dev_err(&pdev->dev, "Error: No memory\n");
619 return -ENOMEM;
620 }
621
Arun Murthydae2db32011-02-22 10:11:13 +0100622 gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END");
623 if (gpadc->irq < 0) {
Lee Jones6dff11e2012-05-17 14:45:20 +0100624 dev_err(&pdev->dev, "failed to get platform irq-%d\n",
Daniel Willerud63219922011-03-05 11:46:13 +0100625 gpadc->irq);
Arun Murthydae2db32011-02-22 10:11:13 +0100626 ret = gpadc->irq;
627 goto fail;
628 }
629
630 gpadc->dev = &pdev->dev;
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100631 gpadc->parent = dev_get_drvdata(pdev->dev.parent);
Daniel Willerud63219922011-03-05 11:46:13 +0100632 mutex_init(&gpadc->ab8500_gpadc_lock);
Arun Murthydae2db32011-02-22 10:11:13 +0100633
634 /* Initialize completion used to notify completion of conversion */
635 init_completion(&gpadc->ab8500_gpadc_complete);
636
637 /* Register interrupt - SwAdcComplete */
638 ret = request_threaded_irq(gpadc->irq, NULL,
639 ab8500_bm_gpswadcconvend_handler,
Lee Jones6e19e832012-05-30 12:47:34 +0800640 IRQF_ONESHOT | IRQF_NO_SUSPEND | IRQF_SHARED,
641 "ab8500-gpadc", gpadc);
Arun Murthydae2db32011-02-22 10:11:13 +0100642 if (ret < 0) {
643 dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
644 gpadc->irq);
645 goto fail;
646 }
647
648 /* VTVout LDO used to power up ab8500-GPADC */
Axel Lin54fc4032013-03-09 17:46:45 +0800649 gpadc->regu = devm_regulator_get(&pdev->dev, "vddadc");
Arun Murthydae2db32011-02-22 10:11:13 +0100650 if (IS_ERR(gpadc->regu)) {
651 ret = PTR_ERR(gpadc->regu);
652 dev_err(gpadc->dev, "failed to get vtvout LDO\n");
Daniel Willerud633e0fa2011-03-05 11:46:27 +0100653 goto fail_irq;
Arun Murthydae2db32011-02-22 10:11:13 +0100654 }
Lee Jones5f8aaef2013-02-04 08:33:13 +0000655
656 platform_set_drvdata(pdev, gpadc);
657
Axel Lin54fc4032013-03-09 17:46:45 +0800658 ret = regulator_enable(gpadc->regu);
659 if (ret) {
660 dev_err(gpadc->dev, "Failed to enable vtvout LDO: %d\n", ret);
661 goto fail_enable;
662 }
Lee Jones5f8aaef2013-02-04 08:33:13 +0000663
664 pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY);
665 pm_runtime_use_autosuspend(gpadc->dev);
666 pm_runtime_set_active(gpadc->dev);
667 pm_runtime_enable(gpadc->dev);
668
Johan Palsson586f3312011-03-05 11:46:37 +0100669 ab8500_gpadc_read_calibration_data(gpadc);
Daniel Willerud63219922011-03-05 11:46:13 +0100670 list_add_tail(&gpadc->node, &ab8500_gpadc_list);
Arun Murthydae2db32011-02-22 10:11:13 +0100671 dev_dbg(gpadc->dev, "probe success\n");
672 return 0;
Axel Lin54fc4032013-03-09 17:46:45 +0800673
674fail_enable:
Daniel Willerud633e0fa2011-03-05 11:46:27 +0100675fail_irq:
676 free_irq(gpadc->irq, gpadc);
Arun Murthydae2db32011-02-22 10:11:13 +0100677fail:
678 kfree(gpadc);
679 gpadc = NULL;
680 return ret;
681}
682
Bill Pemberton4740f732012-11-19 13:26:01 -0500683static int ab8500_gpadc_remove(struct platform_device *pdev)
Arun Murthydae2db32011-02-22 10:11:13 +0100684{
685 struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
686
Daniel Willerud63219922011-03-05 11:46:13 +0100687 /* remove this gpadc entry from the list */
688 list_del(&gpadc->node);
Arun Murthydae2db32011-02-22 10:11:13 +0100689 /* remove interrupt - completion of Sw ADC conversion */
Daniel Willerud63219922011-03-05 11:46:13 +0100690 free_irq(gpadc->irq, gpadc);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000691
692 pm_runtime_get_sync(gpadc->dev);
693 pm_runtime_disable(gpadc->dev);
694
695 regulator_disable(gpadc->regu);
696
697 pm_runtime_set_suspended(gpadc->dev);
698
699 pm_runtime_put_noidle(gpadc->dev);
700
Arun Murthydae2db32011-02-22 10:11:13 +0100701 kfree(gpadc);
702 gpadc = NULL;
703 return 0;
704}
705
Lee Jones5f8aaef2013-02-04 08:33:13 +0000706static const struct dev_pm_ops ab8500_gpadc_pm_ops = {
707 SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend,
708 ab8500_gpadc_runtime_resume,
709 ab8500_gpadc_runtime_idle)
710};
711
Arun Murthydae2db32011-02-22 10:11:13 +0100712static struct platform_driver ab8500_gpadc_driver = {
713 .probe = ab8500_gpadc_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500714 .remove = ab8500_gpadc_remove,
Arun Murthydae2db32011-02-22 10:11:13 +0100715 .driver = {
716 .name = "ab8500-gpadc",
717 .owner = THIS_MODULE,
Lee Jones5f8aaef2013-02-04 08:33:13 +0000718 .pm = &ab8500_gpadc_pm_ops,
Arun Murthydae2db32011-02-22 10:11:13 +0100719 },
720};
721
722static int __init ab8500_gpadc_init(void)
723{
724 return platform_driver_register(&ab8500_gpadc_driver);
725}
726
727static void __exit ab8500_gpadc_exit(void)
728{
729 platform_driver_unregister(&ab8500_gpadc_driver);
730}
731
732subsys_initcall_sync(ab8500_gpadc_init);
733module_exit(ab8500_gpadc_exit);
734
735MODULE_LICENSE("GPL v2");
Johan Palsson586f3312011-03-05 11:46:37 +0100736MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson");
Arun Murthydae2db32011-02-22 10:11:13 +0100737MODULE_ALIAS("platform:ab8500_gpadc");
738MODULE_DESCRIPTION("AB8500 GPADC driver");