blob: 8673bf66f7d7e0e8dcd20c0d2d868796372d11b1 [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
Lee Jones73482342013-02-26 10:06:55 +000058#define AVG_1 0x00
59#define AVG_4 0x20
60#define AVG_8 0x40
61#define AVG_16 0x60
Arun Murthydae2db32011-02-22 10:11:13 +010062#define ADC_SW_CONV 0x04
Karl Komierowski4aad5a92011-03-05 11:46:45 +010063#define EN_ICHAR 0x80
Johan Palssoned139412011-05-08 00:55:43 +020064#define BTEMP_PULL_UP 0x08
Arun Murthydae2db32011-02-22 10:11:13 +010065#define EN_BUF 0x40
66#define DIS_ZERO 0x00
67#define GPADC_BUSY 0x01
Lee Jones73482342013-02-26 10:06:55 +000068#define EN_FALLING 0x10
69#define EN_TRIG_EDGE 0x02
Arun Murthydae2db32011-02-22 10:11:13 +010070
Johan Palsson586f3312011-03-05 11:46:37 +010071/* GPADC constants from AB8500 spec, UM0836 */
72#define ADC_RESOLUTION 1024
73#define ADC_CH_BTEMP_MIN 0
74#define ADC_CH_BTEMP_MAX 1350
75#define ADC_CH_DIETEMP_MIN 0
76#define ADC_CH_DIETEMP_MAX 1350
77#define ADC_CH_CHG_V_MIN 0
78#define ADC_CH_CHG_V_MAX 20030
79#define ADC_CH_ACCDET2_MIN 0
80#define ADC_CH_ACCDET2_MAX 2500
81#define ADC_CH_VBAT_MIN 2300
82#define ADC_CH_VBAT_MAX 4800
83#define ADC_CH_CHG_I_MIN 0
84#define ADC_CH_CHG_I_MAX 1500
85#define ADC_CH_BKBAT_MIN 0
86#define ADC_CH_BKBAT_MAX 3200
87
88/* This is used to not lose precision when dividing to get gain and offset */
89#define CALIB_SCALE 1000
90
Lee Jones5f8aaef2013-02-04 08:33:13 +000091/* Time in ms before disabling regulator */
92#define GPADC_AUDOSUSPEND_DELAY 1
93
Lee Jonesf825ebe2013-01-28 09:20:45 +000094#define CONVERSION_TIME 500 /* ms */
95
Johan Palsson586f3312011-03-05 11:46:37 +010096enum cal_channels {
97 ADC_INPUT_VMAIN = 0,
98 ADC_INPUT_BTEMP,
99 ADC_INPUT_VBAT,
100 NBR_CAL_INPUTS,
101};
102
Arun Murthydae2db32011-02-22 10:11:13 +0100103/**
Johan Palsson586f3312011-03-05 11:46:37 +0100104 * struct adc_cal_data - Table for storing gain and offset for the calibrated
105 * ADC channels
106 * @gain: Gain of the ADC channel
107 * @offset: Offset of the ADC channel
108 */
109struct adc_cal_data {
110 u64 gain;
111 u64 offset;
112};
113
114/**
115 * struct ab8500_gpadc - AB8500 GPADC device information
Arun Murthydae2db32011-02-22 10:11:13 +0100116 * @dev: pointer to the struct device
Daniel Willerud63219922011-03-05 11:46:13 +0100117 * @node: a list of AB8500 GPADCs, hence prepared for
118 reentrance
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100119 * @parent: pointer to the struct ab8500
Arun Murthydae2db32011-02-22 10:11:13 +0100120 * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
121 * the completion of gpadc conversion
122 * @ab8500_gpadc_lock: structure of type mutex
123 * @regu: pointer to the struct regulator
Lee Jones73482342013-02-26 10:06:55 +0000124 * @irq_sw: interrupt number that is used by gpadc for Sw
125 * conversion
126 * @irq_hw: interrupt number that is used by gpadc for Hw
127 * conversion
Johan Palsson586f3312011-03-05 11:46:37 +0100128 * @cal_data array of ADC calibration data structs
Arun Murthydae2db32011-02-22 10:11:13 +0100129 */
Daniel Willerud63219922011-03-05 11:46:13 +0100130struct ab8500_gpadc {
Arun Murthydae2db32011-02-22 10:11:13 +0100131 struct device *dev;
Daniel Willerud63219922011-03-05 11:46:13 +0100132 struct list_head node;
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100133 struct ab8500 *parent;
Arun Murthydae2db32011-02-22 10:11:13 +0100134 struct completion ab8500_gpadc_complete;
135 struct mutex ab8500_gpadc_lock;
136 struct regulator *regu;
Lee Jones73482342013-02-26 10:06:55 +0000137 int irq_sw;
138 int irq_hw;
Johan Palsson586f3312011-03-05 11:46:37 +0100139 struct adc_cal_data cal_data[NBR_CAL_INPUTS];
Daniel Willerud63219922011-03-05 11:46:13 +0100140};
141
142static LIST_HEAD(ab8500_gpadc_list);
143
144/**
145 * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
146 * (i.e. the first GPADC in the instance list)
147 */
148struct ab8500_gpadc *ab8500_gpadc_get(char *name)
149{
150 struct ab8500_gpadc *gpadc;
151
152 list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
153 if (!strcmp(name, dev_name(gpadc->dev)))
154 return gpadc;
155 }
156
157 return ERR_PTR(-ENOENT);
158}
159EXPORT_SYMBOL(ab8500_gpadc_get);
Arun Murthydae2db32011-02-22 10:11:13 +0100160
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200161/**
162 * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
163 */
164int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel,
Johan Palsson586f3312011-03-05 11:46:37 +0100165 int ad_value)
166{
167 int res;
168
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200169 switch (channel) {
Johan Palsson586f3312011-03-05 11:46:37 +0100170 case MAIN_CHARGER_V:
171 /* For some reason we don't have calibrated data */
172 if (!gpadc->cal_data[ADC_INPUT_VMAIN].gain) {
173 res = ADC_CH_CHG_V_MIN + (ADC_CH_CHG_V_MAX -
174 ADC_CH_CHG_V_MIN) * ad_value /
175 ADC_RESOLUTION;
176 break;
177 }
178 /* Here we can use the calibrated data */
179 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VMAIN].gain +
180 gpadc->cal_data[ADC_INPUT_VMAIN].offset) / CALIB_SCALE;
181 break;
182
183 case BAT_CTRL:
184 case BTEMP_BALL:
185 case ACC_DETECT1:
186 case ADC_AUX1:
187 case ADC_AUX2:
188 /* For some reason we don't have calibrated data */
189 if (!gpadc->cal_data[ADC_INPUT_BTEMP].gain) {
190 res = ADC_CH_BTEMP_MIN + (ADC_CH_BTEMP_MAX -
191 ADC_CH_BTEMP_MIN) * ad_value /
192 ADC_RESOLUTION;
193 break;
194 }
195 /* Here we can use the calibrated data */
196 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_BTEMP].gain +
197 gpadc->cal_data[ADC_INPUT_BTEMP].offset) / CALIB_SCALE;
198 break;
199
200 case MAIN_BAT_V:
201 /* For some reason we don't have calibrated data */
202 if (!gpadc->cal_data[ADC_INPUT_VBAT].gain) {
203 res = ADC_CH_VBAT_MIN + (ADC_CH_VBAT_MAX -
204 ADC_CH_VBAT_MIN) * ad_value /
205 ADC_RESOLUTION;
206 break;
207 }
208 /* Here we can use the calibrated data */
209 res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VBAT].gain +
210 gpadc->cal_data[ADC_INPUT_VBAT].offset) / CALIB_SCALE;
211 break;
212
213 case DIE_TEMP:
214 res = ADC_CH_DIETEMP_MIN +
215 (ADC_CH_DIETEMP_MAX - ADC_CH_DIETEMP_MIN) * ad_value /
216 ADC_RESOLUTION;
217 break;
218
219 case ACC_DETECT2:
220 res = ADC_CH_ACCDET2_MIN +
221 (ADC_CH_ACCDET2_MAX - ADC_CH_ACCDET2_MIN) * ad_value /
222 ADC_RESOLUTION;
223 break;
224
225 case VBUS_V:
226 res = ADC_CH_CHG_V_MIN +
227 (ADC_CH_CHG_V_MAX - ADC_CH_CHG_V_MIN) * ad_value /
228 ADC_RESOLUTION;
229 break;
230
231 case MAIN_CHARGER_C:
232 case USB_CHARGER_C:
233 res = ADC_CH_CHG_I_MIN +
234 (ADC_CH_CHG_I_MAX - ADC_CH_CHG_I_MIN) * ad_value /
235 ADC_RESOLUTION;
236 break;
237
238 case BK_BAT_V:
239 res = ADC_CH_BKBAT_MIN +
240 (ADC_CH_BKBAT_MAX - ADC_CH_BKBAT_MIN) * ad_value /
241 ADC_RESOLUTION;
242 break;
243
244 default:
245 dev_err(gpadc->dev,
246 "unknown channel, not possible to convert\n");
247 res = -EINVAL;
248 break;
249
250 }
251 return res;
252}
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200253EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage);
Johan Palsson586f3312011-03-05 11:46:37 +0100254
Arun Murthydae2db32011-02-22 10:11:13 +0100255/**
Lee Jones73482342013-02-26 10:06:55 +0000256 * ab8500_gpadc_sw_hw_convert() - gpadc conversion
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200257 * @channel: analog channel to be converted to digital data
Lee Jones73482342013-02-26 10:06:55 +0000258 * @avg_sample: number of ADC sample to average
259 * @trig_egde: selected ADC trig edge
260 * @trig_timer: selected ADC trigger delay timer
261 * @conv_type: selected conversion type (HW or SW conversion)
Arun Murthydae2db32011-02-22 10:11:13 +0100262 *
263 * This function converts the selected analog i/p to digital
Johan Palsson586f3312011-03-05 11:46:37 +0100264 * data.
Arun Murthydae2db32011-02-22 10:11:13 +0100265 */
Lee Jones73482342013-02-26 10:06:55 +0000266int ab8500_gpadc_sw_hw_convert(struct ab8500_gpadc *gpadc, u8 channel,
267 u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type)
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200268{
269 int ad_value;
270 int voltage;
271
Lee Jones73482342013-02-26 10:06:55 +0000272 ad_value = ab8500_gpadc_read_raw(gpadc, channel, avg_sample,
273 trig_edge, trig_timer, conv_type);
274/* On failure retry a second time */
Jonas Aabergd89cc5a2012-04-17 16:10:46 +0200275 if (ad_value < 0)
Lee Jones73482342013-02-26 10:06:55 +0000276 ad_value = ab8500_gpadc_read_raw(gpadc, channel, avg_sample,
277 trig_edge, trig_timer, conv_type);
278if (ad_value < 0) {
279 dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n",
280 channel);
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200281 return ad_value;
282 }
283
284 voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value);
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200285 if (voltage < 0)
286 dev_err(gpadc->dev, "GPADC to voltage conversion failed ch:"
287 " %d AD: 0x%x\n", channel, ad_value);
288
289 return voltage;
290}
291EXPORT_SYMBOL(ab8500_gpadc_convert);
292
293/**
294 * ab8500_gpadc_read_raw() - gpadc read
295 * @channel: analog channel to be read
Lee Jones73482342013-02-26 10:06:55 +0000296 * @avg_sample: number of ADC sample to average
297 * @trig_edge: selected trig edge
298 * @trig_timer: selected ADC trigger delay timer
299 * @conv_type: selected conversion type (HW or SW conversion)
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200300 *
Lee Jones73482342013-02-26 10:06:55 +0000301 * This function obtains the raw ADC value for an hardware conversion,
302 * this then needs to be converted by calling ab8500_gpadc_ad_to_voltage()
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200303 */
Lee Jones73482342013-02-26 10:06:55 +0000304int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel,
305 u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type)
Arun Murthydae2db32011-02-22 10:11:13 +0100306{
307 int ret;
Arun Murthydae2db32011-02-22 10:11:13 +0100308 int looplimit = 0;
309 u8 val, low_data, high_data;
310
Daniel Willerud63219922011-03-05 11:46:13 +0100311 if (!gpadc)
Arun Murthydae2db32011-02-22 10:11:13 +0100312 return -ENODEV;
313
Daniel Willerud63219922011-03-05 11:46:13 +0100314 mutex_lock(&gpadc->ab8500_gpadc_lock);
Arun Murthydae2db32011-02-22 10:11:13 +0100315 /* Enable VTVout LDO this is required for GPADC */
Lee Jones5f8aaef2013-02-04 08:33:13 +0000316 pm_runtime_get_sync(gpadc->dev);
Arun Murthydae2db32011-02-22 10:11:13 +0100317
318 /* Check if ADC is not busy, lock and proceed */
319 do {
Daniel Willerud63219922011-03-05 11:46:13 +0100320 ret = abx500_get_register_interruptible(gpadc->dev,
321 AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
Arun Murthydae2db32011-02-22 10:11:13 +0100322 if (ret < 0)
323 goto out;
324 if (!(val & GPADC_BUSY))
325 break;
326 msleep(10);
327 } while (++looplimit < 10);
328 if (looplimit >= 10 && (val & GPADC_BUSY)) {
Daniel Willerud63219922011-03-05 11:46:13 +0100329 dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
Arun Murthydae2db32011-02-22 10:11:13 +0100330 ret = -EINVAL;
331 goto out;
332 }
333
334 /* Enable GPADC */
Daniel Willerud63219922011-03-05 11:46:13 +0100335 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
336 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
Arun Murthydae2db32011-02-22 10:11:13 +0100337 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100338 dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100339 goto out;
340 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200341
Lee Jones73482342013-02-26 10:06:55 +0000342 /* Select the channel source and set average samples */
343 switch (avg_sample) {
344 case SAMPLE_1:
345 val = channel | AVG_1;
346 break;
347 case SAMPLE_4:
348 val = channel | AVG_4;
349 break;
350 case SAMPLE_8:
351 val = channel | AVG_8;
352 break;
353 default:
354 val = channel | AVG_16;
355 break;
356
357 }
358
359 if (conv_type == ADC_HW)
360 ret = abx500_set_register_interruptible(gpadc->dev,
361 AB8500_GPADC, AB8500_GPADC_CTRL3_REG, val);
362 else
363 ret = abx500_set_register_interruptible(gpadc->dev,
364 AB8500_GPADC, AB8500_GPADC_CTRL2_REG, val);
Arun Murthydae2db32011-02-22 10:11:13 +0100365 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100366 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100367 "gpadc_conversion: set avg samples failed\n");
368 goto out;
369 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200370
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100371 /*
372 * Enable ADC, buffering, select rising edge and enable ADC path
Karl Komierowskic9c95132011-05-08 00:55:31 +0200373 * charging current sense if it needed, ABB 3.0 needs some special
374 * treatment too.
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100375 */
Lee Jones73482342013-02-26 10:06:55 +0000376 if ((conv_type == ADC_HW) && (trig_edge)) {
377 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
378 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
379 EN_FALLING, EN_FALLING);
380
381 }
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200382 switch (channel) {
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100383 case MAIN_CHARGER_C:
384 case USB_CHARGER_C:
Lee Jones73482342013-02-26 10:06:55 +0000385 if (conv_type == ADC_HW)
Karl Komierowskic9c95132011-05-08 00:55:31 +0200386 ret = abx500_mask_and_set_register_interruptible(
387 gpadc->dev,
388 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
Lee Jones73482342013-02-26 10:06:55 +0000389 EN_BUF | EN_ICHAR | EN_TRIG_EDGE,
390 EN_BUF | EN_ICHAR | EN_TRIG_EDGE);
391 else
392 ret = abx500_mask_and_set_register_interruptible(
393 gpadc->dev,
394 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
395 EN_BUF | EN_ICHAR,
396 EN_BUF | EN_ICHAR);
397 break;
398 case BTEMP_BALL:
399 if (!is_ab8500_2p0_or_earlier(gpadc->parent)) {
400 if (conv_type == ADC_HW)
401 /* Turn on btemp pull-up on ABB 3.0 */
402 ret = abx500_mask_and_set_register_interruptible
403 (gpadc->dev,
404 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
405 EN_BUF | BTEMP_PULL_UP | EN_TRIG_EDGE,
406 EN_BUF | BTEMP_PULL_UP | EN_TRIG_EDGE);
407 else
408 ret = abx500_mask_and_set_register_interruptible
409 (gpadc->dev,
410 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
411 EN_BUF | BTEMP_PULL_UP,
412 EN_BUF | BTEMP_PULL_UP);
Karl Komierowskic9c95132011-05-08 00:55:31 +0200413
414 /*
415 * Delay might be needed for ABB8500 cut 3.0, if not, remove
Masanari Iida5a4432b2012-08-13 21:00:25 +0900416 * when hardware will be available
Karl Komierowskic9c95132011-05-08 00:55:31 +0200417 */
Lee Jonesd0b32fa2011-08-29 08:32:36 +0200418 usleep_range(1000, 1000);
Karl Komierowskic9c95132011-05-08 00:55:31 +0200419 break;
420 }
421 /* Intentional fallthrough */
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100422 default:
Lee Jones73482342013-02-26 10:06:55 +0000423 if (conv_type == ADC_HW)
424 ret = abx500_mask_and_set_register_interruptible(
425 gpadc->dev,
426 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
427 EN_BUF | EN_TRIG_EDGE,
428 EN_BUF | EN_TRIG_EDGE);
429 else
430 ret = abx500_mask_and_set_register_interruptible(
431 gpadc->dev,
432 AB8500_GPADC,
433 AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
Karl Komierowski4aad5a92011-03-05 11:46:45 +0100434 break;
435 }
Arun Murthydae2db32011-02-22 10:11:13 +0100436 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100437 dev_err(gpadc->dev,
Arun Murthydae2db32011-02-22 10:11:13 +0100438 "gpadc_conversion: select falling edge failed\n");
439 goto out;
440 }
Karl Komierowskic9c95132011-05-08 00:55:31 +0200441
Lee Jones73482342013-02-26 10:06:55 +0000442 /* Set trigger delay timer */
443 if (conv_type == ADC_HW) {
444 ret = abx500_set_register_interruptible(gpadc->dev,
445 AB8500_GPADC, AB8500_GPADC_AUTO_TIMER_REG, trig_timer);
446 if (ret < 0) {
447 dev_err(gpadc->dev,
448 "gpadc_conversion: trig timer failed\n");
449 goto out;
450 }
Arun Murthydae2db32011-02-22 10:11:13 +0100451 }
Lee Jones73482342013-02-26 10:06:55 +0000452
453 /* Start SW conversion */
454 if (conv_type == ADC_SW) {
455 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
456 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
457 ADC_SW_CONV, ADC_SW_CONV);
458 if (ret < 0) {
459 dev_err(gpadc->dev,
460 "gpadc_conversion: start s/w conv failed\n");
461 goto out;
462 }
463 }
464
Arun Murthydae2db32011-02-22 10:11:13 +0100465 /* wait for completion of conversion */
Lee Jones73482342013-02-26 10:06:55 +0000466 if (conv_type == ADC_HW) {
467 if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete,
468 2*HZ)) {
469 dev_err(gpadc->dev,
470 "timeout didn't receive"
471 " hw GPADC conv interrupt\n");
472 ret = -EINVAL;
473 goto out;
474 }
475 } else {
476 if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete,
477 msecs_to_jiffies(CONVERSION_TIME))) {
478 dev_err(gpadc->dev,
479 "timeout didn't receive"
480 " sw GPADC conv interrupt\n");
481 ret = -EINVAL;
482 goto out;
483 }
Arun Murthydae2db32011-02-22 10:11:13 +0100484 }
485
486 /* Read the converted RAW data */
Lee Jones73482342013-02-26 10:06:55 +0000487 if (conv_type == ADC_HW) {
488 ret = abx500_get_register_interruptible(gpadc->dev,
489 AB8500_GPADC, AB8500_GPADC_AUTODATAL_REG, &low_data);
490 if (ret < 0) {
491 dev_err(gpadc->dev,
492 "gpadc_conversion: read hw low data failed\n");
493 goto out;
494 }
Arun Murthydae2db32011-02-22 10:11:13 +0100495
Lee Jones73482342013-02-26 10:06:55 +0000496 ret = abx500_get_register_interruptible(gpadc->dev,
497 AB8500_GPADC, AB8500_GPADC_AUTODATAH_REG, &high_data);
498 if (ret < 0) {
499 dev_err(gpadc->dev,
500 "gpadc_conversion: read hw high data failed\n");
501 goto out;
502 }
503 } else {
504 ret = abx500_get_register_interruptible(gpadc->dev,
505 AB8500_GPADC, AB8500_GPADC_MANDATAL_REG, &low_data);
506 if (ret < 0) {
507 dev_err(gpadc->dev,
508 "gpadc_conversion: read sw low data failed\n");
509 goto out;
510 }
511
512 ret = abx500_get_register_interruptible(gpadc->dev,
513 AB8500_GPADC, AB8500_GPADC_MANDATAH_REG, &high_data);
514 if (ret < 0) {
515 dev_err(gpadc->dev,
516 "gpadc_conversion: read sw high data failed\n");
517 goto out;
518 }
Arun Murthydae2db32011-02-22 10:11:13 +0100519 }
520
Arun Murthydae2db32011-02-22 10:11:13 +0100521 /* Disable GPADC */
Daniel Willerud63219922011-03-05 11:46:13 +0100522 ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100523 AB8500_GPADC_CTRL1_REG, DIS_GPADC);
524 if (ret < 0) {
Daniel Willerud63219922011-03-05 11:46:13 +0100525 dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
Arun Murthydae2db32011-02-22 10:11:13 +0100526 goto out;
527 }
Lee Jones5f8aaef2013-02-04 08:33:13 +0000528
Lee Jones73482342013-02-26 10:06:55 +0000529 /* Disable VTVout LDO this is required for GPADC */
Lee Jones5f8aaef2013-02-04 08:33:13 +0000530 pm_runtime_mark_last_busy(gpadc->dev);
531 pm_runtime_put_autosuspend(gpadc->dev);
532
Daniel Willerud63219922011-03-05 11:46:13 +0100533 mutex_unlock(&gpadc->ab8500_gpadc_lock);
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200534
535 return (high_data << 8) | low_data;
Arun Murthydae2db32011-02-22 10:11:13 +0100536
537out:
538 /*
539 * It has shown to be needed to turn off the GPADC if an error occurs,
540 * otherwise we might have problem when waiting for the busy bit in the
541 * GPADC status register to go low. In V1.1 there wait_for_completion
542 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
543 */
Daniel Willerud63219922011-03-05 11:46:13 +0100544 (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
Arun Murthydae2db32011-02-22 10:11:13 +0100545 AB8500_GPADC_CTRL1_REG, DIS_GPADC);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000546 pm_runtime_put(gpadc->dev);
Daniel Willerud63219922011-03-05 11:46:13 +0100547 mutex_unlock(&gpadc->ab8500_gpadc_lock);
548 dev_err(gpadc->dev,
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200549 "gpadc_conversion: Failed to AD convert channel %d\n", channel);
Arun Murthydae2db32011-02-22 10:11:13 +0100550 return ret;
551}
Karl Komierowskibd4a40b2011-08-10 15:09:43 +0200552EXPORT_SYMBOL(ab8500_gpadc_read_raw);
Arun Murthydae2db32011-02-22 10:11:13 +0100553
554/**
Lee Jones73482342013-02-26 10:06:55 +0000555 * ab8500_bm_gpadcconvend_handler() - isr for gpadc conversion completion
Arun Murthydae2db32011-02-22 10:11:13 +0100556 * @irq: irq number
557 * @data: pointer to the data passed during request irq
558 *
Lee Jones73482342013-02-26 10:06:55 +0000559 * This is a interrupt service routine for gpadc conversion completion.
Arun Murthydae2db32011-02-22 10:11:13 +0100560 * Notifies the gpadc completion is completed and the converted raw value
561 * can be read from the registers.
562 * Returns IRQ status(IRQ_HANDLED)
563 */
Lee Jones73482342013-02-26 10:06:55 +0000564static irqreturn_t ab8500_bm_gpadcconvend_handler(int irq, void *_gpadc)
Arun Murthydae2db32011-02-22 10:11:13 +0100565{
Daniel Willerud63219922011-03-05 11:46:13 +0100566 struct ab8500_gpadc *gpadc = _gpadc;
Arun Murthydae2db32011-02-22 10:11:13 +0100567
568 complete(&gpadc->ab8500_gpadc_complete);
569
570 return IRQ_HANDLED;
571}
572
Johan Palsson586f3312011-03-05 11:46:37 +0100573static int otp_cal_regs[] = {
574 AB8500_GPADC_CAL_1,
575 AB8500_GPADC_CAL_2,
576 AB8500_GPADC_CAL_3,
577 AB8500_GPADC_CAL_4,
578 AB8500_GPADC_CAL_5,
579 AB8500_GPADC_CAL_6,
580 AB8500_GPADC_CAL_7,
581};
582
583static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
584{
585 int i;
586 int ret[ARRAY_SIZE(otp_cal_regs)];
587 u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
588
589 int vmain_high, vmain_low;
590 int btemp_high, btemp_low;
591 int vbat_high, vbat_low;
592
593 /* First we read all OTP registers and store the error code */
594 for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
595 ret[i] = abx500_get_register_interruptible(gpadc->dev,
596 AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]);
597 if (ret[i] < 0)
598 dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
599 __func__, otp_cal_regs[i]);
600 }
601
602 /*
603 * The ADC calibration data is stored in OTP registers.
604 * The layout of the calibration data is outlined below and a more
605 * detailed description can be found in UM0836
606 *
607 * vm_h/l = vmain_high/low
608 * bt_h/l = btemp_high/low
609 * vb_h/l = vbat_high/low
610 *
611 * Data bits:
612 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
613 * |.......|.......|.......|.......|.......|.......|.......|.......
614 * | | vm_h9 | vm_h8
615 * |.......|.......|.......|.......|.......|.......|.......|.......
616 * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
617 * |.......|.......|.......|.......|.......|.......|.......|.......
618 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
619 * |.......|.......|.......|.......|.......|.......|.......|.......
620 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
621 * |.......|.......|.......|.......|.......|.......|.......|.......
622 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
623 * |.......|.......|.......|.......|.......|.......|.......|.......
624 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
625 * |.......|.......|.......|.......|.......|.......|.......|.......
626 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
627 * |.......|.......|.......|.......|.......|.......|.......|.......
628 *
629 *
630 * Ideal output ADC codes corresponding to injected input voltages
631 * during manufacturing is:
632 *
633 * vmain_high: Vin = 19500mV / ADC ideal code = 997
634 * vmain_low: Vin = 315mV / ADC ideal code = 16
635 * btemp_high: Vin = 1300mV / ADC ideal code = 985
636 * btemp_low: Vin = 21mV / ADC ideal code = 16
637 * vbat_high: Vin = 4700mV / ADC ideal code = 982
638 * vbat_low: Vin = 2380mV / ADC ideal code = 33
639 */
640
641 /* Calculate gain and offset for VMAIN if all reads succeeded */
642 if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
643 vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
644 ((gpadc_cal[1] & 0x3F) << 2) |
645 ((gpadc_cal[2] & 0xC0) >> 6));
646
647 vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
648
649 gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
650 (19500 - 315) / (vmain_high - vmain_low);
651
652 gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 19500 -
653 (CALIB_SCALE * (19500 - 315) /
654 (vmain_high - vmain_low)) * vmain_high;
655 } else {
656 gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
657 }
658
659 /* Calculate gain and offset for BTEMP if all reads succeeded */
660 if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
661 btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
662 (gpadc_cal[3] << 1) |
663 ((gpadc_cal[4] & 0x80) >> 7));
664
665 btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
666
667 gpadc->cal_data[ADC_INPUT_BTEMP].gain =
668 CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
669
670 gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 -
671 (CALIB_SCALE * (1300 - 21) /
672 (btemp_high - btemp_low)) * btemp_high;
673 } else {
674 gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0;
675 }
676
677 /* Calculate gain and offset for VBAT if all reads succeeded */
678 if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
679 vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
680 vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
681
682 gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE *
683 (4700 - 2380) / (vbat_high - vbat_low);
684
685 gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 -
686 (CALIB_SCALE * (4700 - 2380) /
687 (vbat_high - vbat_low)) * vbat_high;
688 } else {
689 gpadc->cal_data[ADC_INPUT_VBAT].gain = 0;
690 }
691
692 dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n",
693 gpadc->cal_data[ADC_INPUT_VMAIN].gain,
694 gpadc->cal_data[ADC_INPUT_VMAIN].offset);
695
696 dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n",
697 gpadc->cal_data[ADC_INPUT_BTEMP].gain,
698 gpadc->cal_data[ADC_INPUT_BTEMP].offset);
699
700 dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n",
701 gpadc->cal_data[ADC_INPUT_VBAT].gain,
702 gpadc->cal_data[ADC_INPUT_VBAT].offset);
703}
704
Lee Jones5f8aaef2013-02-04 08:33:13 +0000705static int ab8500_gpadc_runtime_suspend(struct device *dev)
706{
707 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
708
709 regulator_disable(gpadc->regu);
710 return 0;
711}
712
713static int ab8500_gpadc_runtime_resume(struct device *dev)
714{
715 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
716
717 regulator_enable(gpadc->regu);
718 return 0;
719}
720
721static int ab8500_gpadc_runtime_idle(struct device *dev)
722{
Lee Jones5f8aaef2013-02-04 08:33:13 +0000723 pm_runtime_suspend(dev);
724 return 0;
725}
726
Daniel WILLERUD774c50a2012-04-12 08:15:05 +0200727static int ab8500_gpadc_suspend(struct device *dev)
728{
729 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
730
731 mutex_lock(&gpadc->ab8500_gpadc_lock);
732
733 pm_runtime_get_sync(dev);
734
735 regulator_disable(gpadc->regu);
736 return 0;
737}
738
739static int ab8500_gpadc_resume(struct device *dev)
740{
741 struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
742
743 regulator_enable(gpadc->regu);
744
745 pm_runtime_mark_last_busy(gpadc->dev);
746 pm_runtime_put_autosuspend(gpadc->dev);
747
748 mutex_unlock(&gpadc->ab8500_gpadc_lock);
749 return 0;
750}
751
Bill Pembertonf791be42012-11-19 13:23:04 -0500752static int ab8500_gpadc_probe(struct platform_device *pdev)
Arun Murthydae2db32011-02-22 10:11:13 +0100753{
754 int ret = 0;
755 struct ab8500_gpadc *gpadc;
756
757 gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
758 if (!gpadc) {
759 dev_err(&pdev->dev, "Error: No memory\n");
760 return -ENOMEM;
761 }
762
Lee Jones73482342013-02-26 10:06:55 +0000763 gpadc->irq_sw = platform_get_irq_byname(pdev, "SW_CONV_END");
764 if (gpadc->irq_sw < 0) {
765 dev_err(gpadc->dev, "failed to get platform irq-%d\n",
766 gpadc->irq_sw);
767 ret = gpadc->irq_sw;
768 goto fail;
769 }
770
771 gpadc->irq_hw = platform_get_irq_byname(pdev, "HW_CONV_END");
772 if (gpadc->irq_hw < 0) {
773 dev_err(gpadc->dev, "failed to get platform irq-%d\n",
774 gpadc->irq_hw);
775 ret = gpadc->irq_hw;
Arun Murthydae2db32011-02-22 10:11:13 +0100776 goto fail;
777 }
778
779 gpadc->dev = &pdev->dev;
Michel JAOUEN20bf4282012-02-09 12:06:47 +0100780 gpadc->parent = dev_get_drvdata(pdev->dev.parent);
Daniel Willerud63219922011-03-05 11:46:13 +0100781 mutex_init(&gpadc->ab8500_gpadc_lock);
Arun Murthydae2db32011-02-22 10:11:13 +0100782
783 /* Initialize completion used to notify completion of conversion */
784 init_completion(&gpadc->ab8500_gpadc_complete);
785
Lee Jones73482342013-02-26 10:06:55 +0000786 /* Register interrupts */
787 ret = request_threaded_irq(gpadc->irq_sw, NULL,
788 ab8500_bm_gpadcconvend_handler,
789 IRQF_NO_SUSPEND | IRQF_SHARED, "ab8500-gpadc-sw", gpadc);
Arun Murthydae2db32011-02-22 10:11:13 +0100790 if (ret < 0) {
791 dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
Lee Jones73482342013-02-26 10:06:55 +0000792 gpadc->irq_sw);
793 goto fail;
794 }
795 ret = request_threaded_irq(gpadc->irq_hw, NULL,
796 ab8500_bm_gpadcconvend_handler,
797 IRQF_NO_SUSPEND | IRQF_SHARED, "ab8500-gpadc-hw", gpadc);
798 if (ret < 0) {
799 dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
800 gpadc->irq_hw);
Arun Murthydae2db32011-02-22 10:11:13 +0100801 goto fail;
802 }
803
804 /* VTVout LDO used to power up ab8500-GPADC */
805 gpadc->regu = regulator_get(&pdev->dev, "vddadc");
806 if (IS_ERR(gpadc->regu)) {
807 ret = PTR_ERR(gpadc->regu);
808 dev_err(gpadc->dev, "failed to get vtvout LDO\n");
Daniel Willerud633e0fa2011-03-05 11:46:27 +0100809 goto fail_irq;
Arun Murthydae2db32011-02-22 10:11:13 +0100810 }
Lee Jones5f8aaef2013-02-04 08:33:13 +0000811
812 platform_set_drvdata(pdev, gpadc);
813
814 regulator_enable(gpadc->regu);
815
816 pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY);
817 pm_runtime_use_autosuspend(gpadc->dev);
818 pm_runtime_set_active(gpadc->dev);
819 pm_runtime_enable(gpadc->dev);
820
Johan Palsson586f3312011-03-05 11:46:37 +0100821 ab8500_gpadc_read_calibration_data(gpadc);
Daniel Willerud63219922011-03-05 11:46:13 +0100822 list_add_tail(&gpadc->node, &ab8500_gpadc_list);
Arun Murthydae2db32011-02-22 10:11:13 +0100823 dev_dbg(gpadc->dev, "probe success\n");
824 return 0;
Daniel Willerud633e0fa2011-03-05 11:46:27 +0100825fail_irq:
Lee Jones73482342013-02-26 10:06:55 +0000826 free_irq(gpadc->irq_sw, gpadc);
827 free_irq(gpadc->irq_hw, gpadc);
Arun Murthydae2db32011-02-22 10:11:13 +0100828fail:
829 kfree(gpadc);
830 gpadc = NULL;
831 return ret;
832}
833
Bill Pemberton4740f732012-11-19 13:26:01 -0500834static int ab8500_gpadc_remove(struct platform_device *pdev)
Arun Murthydae2db32011-02-22 10:11:13 +0100835{
836 struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
837
Daniel Willerud63219922011-03-05 11:46:13 +0100838 /* remove this gpadc entry from the list */
839 list_del(&gpadc->node);
Arun Murthydae2db32011-02-22 10:11:13 +0100840 /* remove interrupt - completion of Sw ADC conversion */
Lee Jones73482342013-02-26 10:06:55 +0000841 free_irq(gpadc->irq_sw, gpadc);
842 free_irq(gpadc->irq_hw, gpadc);
Lee Jones5f8aaef2013-02-04 08:33:13 +0000843
844 pm_runtime_get_sync(gpadc->dev);
845 pm_runtime_disable(gpadc->dev);
846
847 regulator_disable(gpadc->regu);
848
849 pm_runtime_set_suspended(gpadc->dev);
850
851 pm_runtime_put_noidle(gpadc->dev);
852
Arun Murthydae2db32011-02-22 10:11:13 +0100853 kfree(gpadc);
854 gpadc = NULL;
855 return 0;
856}
857
Lee Jones5f8aaef2013-02-04 08:33:13 +0000858static const struct dev_pm_ops ab8500_gpadc_pm_ops = {
859 SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend,
860 ab8500_gpadc_runtime_resume,
861 ab8500_gpadc_runtime_idle)
Daniel WILLERUD774c50a2012-04-12 08:15:05 +0200862 SET_SYSTEM_SLEEP_PM_OPS(ab8500_gpadc_suspend,
863 ab8500_gpadc_resume)
864
Lee Jones5f8aaef2013-02-04 08:33:13 +0000865};
866
Arun Murthydae2db32011-02-22 10:11:13 +0100867static struct platform_driver ab8500_gpadc_driver = {
868 .probe = ab8500_gpadc_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500869 .remove = ab8500_gpadc_remove,
Arun Murthydae2db32011-02-22 10:11:13 +0100870 .driver = {
871 .name = "ab8500-gpadc",
872 .owner = THIS_MODULE,
Lee Jones5f8aaef2013-02-04 08:33:13 +0000873 .pm = &ab8500_gpadc_pm_ops,
Arun Murthydae2db32011-02-22 10:11:13 +0100874 },
875};
876
877static int __init ab8500_gpadc_init(void)
878{
879 return platform_driver_register(&ab8500_gpadc_driver);
880}
881
882static void __exit ab8500_gpadc_exit(void)
883{
884 platform_driver_unregister(&ab8500_gpadc_driver);
885}
886
887subsys_initcall_sync(ab8500_gpadc_init);
888module_exit(ab8500_gpadc_exit);
889
890MODULE_LICENSE("GPL v2");
Lee Jones73482342013-02-26 10:06:55 +0000891MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson,"
892 "M'boumba Cedric Madianga");
Arun Murthydae2db32011-02-22 10:11:13 +0100893MODULE_ALIAS("platform:ab8500_gpadc");
894MODULE_DESCRIPTION("AB8500 GPADC driver");