Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 1 | /* |
| 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 Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 6 | * Author: Daniel Willerud <daniel.willerud@stericsson.com> |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 7 | * Author: Johan Palsson <johan.palsson@stericsson.com> |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 8 | */ |
| 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 Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 15 | #include <linux/pm_runtime.h> |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 16 | #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 Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 21 | #include <linux/list.h> |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 22 | #include <linux/mfd/abx500.h> |
Linus Walleij | ee66e65 | 2011-12-02 14:16:33 +0100 | [diff] [blame] | 23 | #include <linux/mfd/abx500/ab8500.h> |
| 24 | #include <linux/mfd/abx500/ab8500-gpadc.h> |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 25 | |
| 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 Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 41 | /* |
| 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 Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 53 | /* 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 Komierowski | 4aad5a9 | 2011-03-05 11:46:45 +0100 | [diff] [blame] | 60 | #define EN_ICHAR 0x80 |
Johan Palsson | ed13941 | 2011-05-08 00:55:43 +0200 | [diff] [blame] | 61 | #define BTEMP_PULL_UP 0x08 |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 62 | #define EN_BUF 0x40 |
| 63 | #define DIS_ZERO 0x00 |
| 64 | #define GPADC_BUSY 0x01 |
| 65 | |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 66 | /* 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 Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 86 | /* Time in ms before disabling regulator */ |
| 87 | #define GPADC_AUDOSUSPEND_DELAY 1 |
| 88 | |
Lee Jones | f825ebe | 2013-01-28 09:20:45 +0000 | [diff] [blame] | 89 | #define CONVERSION_TIME 500 /* ms */ |
| 90 | |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 91 | enum cal_channels { |
| 92 | ADC_INPUT_VMAIN = 0, |
| 93 | ADC_INPUT_BTEMP, |
| 94 | ADC_INPUT_VBAT, |
| 95 | NBR_CAL_INPUTS, |
| 96 | }; |
| 97 | |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 98 | /** |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 99 | * 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 | */ |
| 104 | struct adc_cal_data { |
| 105 | u64 gain; |
| 106 | u64 offset; |
| 107 | }; |
| 108 | |
| 109 | /** |
| 110 | * struct ab8500_gpadc - AB8500 GPADC device information |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 111 | * @dev: pointer to the struct device |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 112 | * @node: a list of AB8500 GPADCs, hence prepared for |
| 113 | reentrance |
Michel JAOUEN | 20bf428 | 2012-02-09 12:06:47 +0100 | [diff] [blame] | 114 | * @parent: pointer to the struct ab8500 |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 115 | * @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 Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 120 | * @cal_data array of ADC calibration data structs |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 121 | */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 122 | struct ab8500_gpadc { |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 123 | struct device *dev; |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 124 | struct list_head node; |
Michel JAOUEN | 20bf428 | 2012-02-09 12:06:47 +0100 | [diff] [blame] | 125 | struct ab8500 *parent; |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 126 | struct completion ab8500_gpadc_complete; |
| 127 | struct mutex ab8500_gpadc_lock; |
| 128 | struct regulator *regu; |
| 129 | int irq; |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 130 | struct adc_cal_data cal_data[NBR_CAL_INPUTS]; |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static 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 | */ |
| 139 | struct 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 | } |
| 150 | EXPORT_SYMBOL(ab8500_gpadc_get); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 151 | |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 152 | /** |
| 153 | * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage |
| 154 | */ |
| 155 | int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel, |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 156 | int ad_value) |
| 157 | { |
| 158 | int res; |
| 159 | |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 160 | switch (channel) { |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 161 | 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 Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 244 | EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage); |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 245 | |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 246 | /** |
| 247 | * ab8500_gpadc_convert() - gpadc conversion |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 248 | * @channel: analog channel to be converted to digital data |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 249 | * |
| 250 | * This function converts the selected analog i/p to digital |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 251 | * data. |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 252 | */ |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 253 | int 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); |
Jonas Aaberg | d89cc5a | 2012-04-17 16:10:46 +0200 | [diff] [blame^] | 259 | |
| 260 | /* On failure retry a second time */ |
| 261 | if (ad_value < 0) |
| 262 | ad_value = ab8500_gpadc_read_raw(gpadc, channel); |
| 263 | |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 264 | if (ad_value < 0) { |
| 265 | dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n", channel); |
| 266 | return ad_value; |
| 267 | } |
| 268 | |
| 269 | voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value); |
| 270 | |
| 271 | if (voltage < 0) |
| 272 | dev_err(gpadc->dev, "GPADC to voltage conversion failed ch:" |
| 273 | " %d AD: 0x%x\n", channel, ad_value); |
| 274 | |
| 275 | return voltage; |
| 276 | } |
| 277 | EXPORT_SYMBOL(ab8500_gpadc_convert); |
| 278 | |
| 279 | /** |
| 280 | * ab8500_gpadc_read_raw() - gpadc read |
| 281 | * @channel: analog channel to be read |
| 282 | * |
| 283 | * This function obtains the raw ADC value, this then needs |
| 284 | * to be converted by calling ab8500_gpadc_ad_to_voltage() |
| 285 | */ |
| 286 | int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel) |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 287 | { |
| 288 | int ret; |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 289 | int looplimit = 0; |
| 290 | u8 val, low_data, high_data; |
| 291 | |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 292 | if (!gpadc) |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 293 | return -ENODEV; |
| 294 | |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 295 | mutex_lock(&gpadc->ab8500_gpadc_lock); |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 296 | |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 297 | /* Enable VTVout LDO this is required for GPADC */ |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 298 | pm_runtime_get_sync(gpadc->dev); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 299 | |
| 300 | /* Check if ADC is not busy, lock and proceed */ |
| 301 | do { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 302 | ret = abx500_get_register_interruptible(gpadc->dev, |
| 303 | AB8500_GPADC, AB8500_GPADC_STAT_REG, &val); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 304 | if (ret < 0) |
| 305 | goto out; |
| 306 | if (!(val & GPADC_BUSY)) |
| 307 | break; |
| 308 | msleep(10); |
| 309 | } while (++looplimit < 10); |
| 310 | if (looplimit >= 10 && (val & GPADC_BUSY)) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 311 | dev_err(gpadc->dev, "gpadc_conversion: GPADC busy"); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 312 | ret = -EINVAL; |
| 313 | goto out; |
| 314 | } |
| 315 | |
| 316 | /* Enable GPADC */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 317 | ret = abx500_mask_and_set_register_interruptible(gpadc->dev, |
| 318 | AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 319 | if (ret < 0) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 320 | dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n"); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 321 | goto out; |
| 322 | } |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 323 | |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 324 | /* Select the channel source and set average samples to 16 */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 325 | ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 326 | AB8500_GPADC_CTRL2_REG, (channel | SW_AVG_16)); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 327 | if (ret < 0) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 328 | dev_err(gpadc->dev, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 329 | "gpadc_conversion: set avg samples failed\n"); |
| 330 | goto out; |
| 331 | } |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 332 | |
Karl Komierowski | 4aad5a9 | 2011-03-05 11:46:45 +0100 | [diff] [blame] | 333 | /* |
| 334 | * Enable ADC, buffering, select rising edge and enable ADC path |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 335 | * charging current sense if it needed, ABB 3.0 needs some special |
| 336 | * treatment too. |
Karl Komierowski | 4aad5a9 | 2011-03-05 11:46:45 +0100 | [diff] [blame] | 337 | */ |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 338 | switch (channel) { |
Karl Komierowski | 4aad5a9 | 2011-03-05 11:46:45 +0100 | [diff] [blame] | 339 | case MAIN_CHARGER_C: |
| 340 | case USB_CHARGER_C: |
| 341 | ret = abx500_mask_and_set_register_interruptible(gpadc->dev, |
| 342 | AB8500_GPADC, AB8500_GPADC_CTRL1_REG, |
| 343 | EN_BUF | EN_ICHAR, |
| 344 | EN_BUF | EN_ICHAR); |
| 345 | break; |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 346 | case BTEMP_BALL: |
Michel JAOUEN | 20bf428 | 2012-02-09 12:06:47 +0100 | [diff] [blame] | 347 | if (!is_ab8500_2p0_or_earlier(gpadc->parent)) { |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 348 | /* Turn on btemp pull-up on ABB 3.0 */ |
| 349 | ret = abx500_mask_and_set_register_interruptible( |
| 350 | gpadc->dev, |
| 351 | AB8500_GPADC, AB8500_GPADC_CTRL1_REG, |
Johan Palsson | ed13941 | 2011-05-08 00:55:43 +0200 | [diff] [blame] | 352 | EN_BUF | BTEMP_PULL_UP, |
| 353 | EN_BUF | BTEMP_PULL_UP); |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * Delay might be needed for ABB8500 cut 3.0, if not, remove |
Masanari Iida | 5a4432b | 2012-08-13 21:00:25 +0900 | [diff] [blame] | 357 | * when hardware will be available |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 358 | */ |
Lee Jones | d0b32fa | 2011-08-29 08:32:36 +0200 | [diff] [blame] | 359 | usleep_range(1000, 1000); |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | /* Intentional fallthrough */ |
Karl Komierowski | 4aad5a9 | 2011-03-05 11:46:45 +0100 | [diff] [blame] | 363 | default: |
| 364 | ret = abx500_mask_and_set_register_interruptible(gpadc->dev, |
| 365 | AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF); |
| 366 | break; |
| 367 | } |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 368 | if (ret < 0) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 369 | dev_err(gpadc->dev, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 370 | "gpadc_conversion: select falling edge failed\n"); |
| 371 | goto out; |
| 372 | } |
Karl Komierowski | c9c9513 | 2011-05-08 00:55:31 +0200 | [diff] [blame] | 373 | |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 374 | ret = abx500_mask_and_set_register_interruptible(gpadc->dev, |
| 375 | AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 376 | if (ret < 0) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 377 | dev_err(gpadc->dev, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 378 | "gpadc_conversion: start s/w conversion failed\n"); |
| 379 | goto out; |
| 380 | } |
| 381 | /* wait for completion of conversion */ |
Lee Jones | f825ebe | 2013-01-28 09:20:45 +0000 | [diff] [blame] | 382 | if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, |
| 383 | msecs_to_jiffies(CONVERSION_TIME))) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 384 | dev_err(gpadc->dev, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 385 | "timeout: didn't receive GPADC conversion interrupt\n"); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 386 | ret = -EINVAL; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | /* Read the converted RAW data */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 391 | ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 392 | AB8500_GPADC_MANDATAL_REG, &low_data); |
| 393 | if (ret < 0) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 394 | dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n"); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 395 | goto out; |
| 396 | } |
| 397 | |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 398 | ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 399 | AB8500_GPADC_MANDATAH_REG, &high_data); |
| 400 | if (ret < 0) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 401 | dev_err(gpadc->dev, |
| 402 | "gpadc_conversion: read high data failed\n"); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 403 | goto out; |
| 404 | } |
| 405 | |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 406 | /* Disable GPADC */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 407 | ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 408 | AB8500_GPADC_CTRL1_REG, DIS_GPADC); |
| 409 | if (ret < 0) { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 410 | dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n"); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 411 | goto out; |
| 412 | } |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 413 | |
| 414 | pm_runtime_mark_last_busy(gpadc->dev); |
| 415 | pm_runtime_put_autosuspend(gpadc->dev); |
| 416 | |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 417 | mutex_unlock(&gpadc->ab8500_gpadc_lock); |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 418 | |
| 419 | return (high_data << 8) | low_data; |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 420 | |
| 421 | out: |
| 422 | /* |
| 423 | * It has shown to be needed to turn off the GPADC if an error occurs, |
| 424 | * otherwise we might have problem when waiting for the busy bit in the |
| 425 | * GPADC status register to go low. In V1.1 there wait_for_completion |
| 426 | * seems to timeout when waiting for an interrupt.. Not seen in V2.0 |
| 427 | */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 428 | (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 429 | AB8500_GPADC_CTRL1_REG, DIS_GPADC); |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 430 | |
| 431 | pm_runtime_put(gpadc->dev); |
| 432 | |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 433 | mutex_unlock(&gpadc->ab8500_gpadc_lock); |
| 434 | dev_err(gpadc->dev, |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 435 | "gpadc_conversion: Failed to AD convert channel %d\n", channel); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 436 | return ret; |
| 437 | } |
Karl Komierowski | bd4a40b | 2011-08-10 15:09:43 +0200 | [diff] [blame] | 438 | EXPORT_SYMBOL(ab8500_gpadc_read_raw); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 439 | |
| 440 | /** |
| 441 | * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion |
| 442 | * @irq: irq number |
| 443 | * @data: pointer to the data passed during request irq |
| 444 | * |
| 445 | * This is a interrupt service routine for s/w gpadc conversion completion. |
| 446 | * Notifies the gpadc completion is completed and the converted raw value |
| 447 | * can be read from the registers. |
| 448 | * Returns IRQ status(IRQ_HANDLED) |
| 449 | */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 450 | static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_gpadc) |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 451 | { |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 452 | struct ab8500_gpadc *gpadc = _gpadc; |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 453 | |
| 454 | complete(&gpadc->ab8500_gpadc_complete); |
| 455 | |
| 456 | return IRQ_HANDLED; |
| 457 | } |
| 458 | |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 459 | static int otp_cal_regs[] = { |
| 460 | AB8500_GPADC_CAL_1, |
| 461 | AB8500_GPADC_CAL_2, |
| 462 | AB8500_GPADC_CAL_3, |
| 463 | AB8500_GPADC_CAL_4, |
| 464 | AB8500_GPADC_CAL_5, |
| 465 | AB8500_GPADC_CAL_6, |
| 466 | AB8500_GPADC_CAL_7, |
| 467 | }; |
| 468 | |
| 469 | static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc) |
| 470 | { |
| 471 | int i; |
| 472 | int ret[ARRAY_SIZE(otp_cal_regs)]; |
| 473 | u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)]; |
| 474 | |
| 475 | int vmain_high, vmain_low; |
| 476 | int btemp_high, btemp_low; |
| 477 | int vbat_high, vbat_low; |
| 478 | |
| 479 | /* First we read all OTP registers and store the error code */ |
| 480 | for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) { |
| 481 | ret[i] = abx500_get_register_interruptible(gpadc->dev, |
| 482 | AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]); |
| 483 | if (ret[i] < 0) |
| 484 | dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n", |
| 485 | __func__, otp_cal_regs[i]); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * The ADC calibration data is stored in OTP registers. |
| 490 | * The layout of the calibration data is outlined below and a more |
| 491 | * detailed description can be found in UM0836 |
| 492 | * |
| 493 | * vm_h/l = vmain_high/low |
| 494 | * bt_h/l = btemp_high/low |
| 495 | * vb_h/l = vbat_high/low |
| 496 | * |
| 497 | * Data bits: |
| 498 | * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 499 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 500 | * | | vm_h9 | vm_h8 |
| 501 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 502 | * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2 |
| 503 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 504 | * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9 |
| 505 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 506 | * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1 |
| 507 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 508 | * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8 |
| 509 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 510 | * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0 |
| 511 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 512 | * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 | |
| 513 | * |.......|.......|.......|.......|.......|.......|.......|....... |
| 514 | * |
| 515 | * |
| 516 | * Ideal output ADC codes corresponding to injected input voltages |
| 517 | * during manufacturing is: |
| 518 | * |
| 519 | * vmain_high: Vin = 19500mV / ADC ideal code = 997 |
| 520 | * vmain_low: Vin = 315mV / ADC ideal code = 16 |
| 521 | * btemp_high: Vin = 1300mV / ADC ideal code = 985 |
| 522 | * btemp_low: Vin = 21mV / ADC ideal code = 16 |
| 523 | * vbat_high: Vin = 4700mV / ADC ideal code = 982 |
| 524 | * vbat_low: Vin = 2380mV / ADC ideal code = 33 |
| 525 | */ |
| 526 | |
| 527 | /* Calculate gain and offset for VMAIN if all reads succeeded */ |
| 528 | if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) { |
| 529 | vmain_high = (((gpadc_cal[0] & 0x03) << 8) | |
| 530 | ((gpadc_cal[1] & 0x3F) << 2) | |
| 531 | ((gpadc_cal[2] & 0xC0) >> 6)); |
| 532 | |
| 533 | vmain_low = ((gpadc_cal[2] & 0x3E) >> 1); |
| 534 | |
| 535 | gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE * |
| 536 | (19500 - 315) / (vmain_high - vmain_low); |
| 537 | |
| 538 | gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 19500 - |
| 539 | (CALIB_SCALE * (19500 - 315) / |
| 540 | (vmain_high - vmain_low)) * vmain_high; |
| 541 | } else { |
| 542 | gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0; |
| 543 | } |
| 544 | |
| 545 | /* Calculate gain and offset for BTEMP if all reads succeeded */ |
| 546 | if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) { |
| 547 | btemp_high = (((gpadc_cal[2] & 0x01) << 9) | |
| 548 | (gpadc_cal[3] << 1) | |
| 549 | ((gpadc_cal[4] & 0x80) >> 7)); |
| 550 | |
| 551 | btemp_low = ((gpadc_cal[4] & 0x7C) >> 2); |
| 552 | |
| 553 | gpadc->cal_data[ADC_INPUT_BTEMP].gain = |
| 554 | CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low); |
| 555 | |
| 556 | gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 - |
| 557 | (CALIB_SCALE * (1300 - 21) / |
| 558 | (btemp_high - btemp_low)) * btemp_high; |
| 559 | } else { |
| 560 | gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0; |
| 561 | } |
| 562 | |
| 563 | /* Calculate gain and offset for VBAT if all reads succeeded */ |
| 564 | if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) { |
| 565 | vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]); |
| 566 | vbat_low = ((gpadc_cal[6] & 0xFC) >> 2); |
| 567 | |
| 568 | gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE * |
| 569 | (4700 - 2380) / (vbat_high - vbat_low); |
| 570 | |
| 571 | gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 - |
| 572 | (CALIB_SCALE * (4700 - 2380) / |
| 573 | (vbat_high - vbat_low)) * vbat_high; |
| 574 | } else { |
| 575 | gpadc->cal_data[ADC_INPUT_VBAT].gain = 0; |
| 576 | } |
| 577 | |
| 578 | dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n", |
| 579 | gpadc->cal_data[ADC_INPUT_VMAIN].gain, |
| 580 | gpadc->cal_data[ADC_INPUT_VMAIN].offset); |
| 581 | |
| 582 | dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n", |
| 583 | gpadc->cal_data[ADC_INPUT_BTEMP].gain, |
| 584 | gpadc->cal_data[ADC_INPUT_BTEMP].offset); |
| 585 | |
| 586 | dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n", |
| 587 | gpadc->cal_data[ADC_INPUT_VBAT].gain, |
| 588 | gpadc->cal_data[ADC_INPUT_VBAT].offset); |
| 589 | } |
| 590 | |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 591 | static int ab8500_gpadc_runtime_suspend(struct device *dev) |
| 592 | { |
| 593 | struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); |
| 594 | |
| 595 | regulator_disable(gpadc->regu); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int ab8500_gpadc_runtime_resume(struct device *dev) |
| 600 | { |
| 601 | struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); |
| 602 | |
| 603 | regulator_enable(gpadc->regu); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int ab8500_gpadc_runtime_idle(struct device *dev) |
| 608 | { |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 609 | pm_runtime_suspend(dev); |
| 610 | return 0; |
| 611 | } |
| 612 | |
Daniel WILLERUD | 774c50a | 2012-04-12 08:15:05 +0200 | [diff] [blame] | 613 | static int ab8500_gpadc_suspend(struct device *dev) |
| 614 | { |
| 615 | struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); |
| 616 | |
| 617 | mutex_lock(&gpadc->ab8500_gpadc_lock); |
| 618 | |
| 619 | pm_runtime_get_sync(dev); |
| 620 | |
| 621 | regulator_disable(gpadc->regu); |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int ab8500_gpadc_resume(struct device *dev) |
| 626 | { |
| 627 | struct ab8500_gpadc *gpadc = dev_get_drvdata(dev); |
| 628 | |
| 629 | regulator_enable(gpadc->regu); |
| 630 | |
| 631 | pm_runtime_mark_last_busy(gpadc->dev); |
| 632 | pm_runtime_put_autosuspend(gpadc->dev); |
| 633 | |
| 634 | mutex_unlock(&gpadc->ab8500_gpadc_lock); |
| 635 | return 0; |
| 636 | } |
| 637 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 638 | static int ab8500_gpadc_probe(struct platform_device *pdev) |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 639 | { |
| 640 | int ret = 0; |
| 641 | struct ab8500_gpadc *gpadc; |
| 642 | |
| 643 | gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL); |
| 644 | if (!gpadc) { |
| 645 | dev_err(&pdev->dev, "Error: No memory\n"); |
| 646 | return -ENOMEM; |
| 647 | } |
| 648 | |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 649 | gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END"); |
| 650 | if (gpadc->irq < 0) { |
Lee Jones | 6dff11e | 2012-05-17 14:45:20 +0100 | [diff] [blame] | 651 | dev_err(&pdev->dev, "failed to get platform irq-%d\n", |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 652 | gpadc->irq); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 653 | ret = gpadc->irq; |
| 654 | goto fail; |
| 655 | } |
| 656 | |
| 657 | gpadc->dev = &pdev->dev; |
Michel JAOUEN | 20bf428 | 2012-02-09 12:06:47 +0100 | [diff] [blame] | 658 | gpadc->parent = dev_get_drvdata(pdev->dev.parent); |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 659 | mutex_init(&gpadc->ab8500_gpadc_lock); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 660 | |
| 661 | /* Initialize completion used to notify completion of conversion */ |
| 662 | init_completion(&gpadc->ab8500_gpadc_complete); |
| 663 | |
| 664 | /* Register interrupt - SwAdcComplete */ |
| 665 | ret = request_threaded_irq(gpadc->irq, NULL, |
| 666 | ab8500_bm_gpswadcconvend_handler, |
Lee Jones | 6e19e83 | 2012-05-30 12:47:34 +0800 | [diff] [blame] | 667 | IRQF_ONESHOT | IRQF_NO_SUSPEND | IRQF_SHARED, |
| 668 | "ab8500-gpadc", gpadc); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 669 | if (ret < 0) { |
| 670 | dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n", |
| 671 | gpadc->irq); |
| 672 | goto fail; |
| 673 | } |
| 674 | |
| 675 | /* VTVout LDO used to power up ab8500-GPADC */ |
| 676 | gpadc->regu = regulator_get(&pdev->dev, "vddadc"); |
| 677 | if (IS_ERR(gpadc->regu)) { |
| 678 | ret = PTR_ERR(gpadc->regu); |
| 679 | dev_err(gpadc->dev, "failed to get vtvout LDO\n"); |
Daniel Willerud | 633e0fa | 2011-03-05 11:46:27 +0100 | [diff] [blame] | 680 | goto fail_irq; |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 681 | } |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 682 | |
| 683 | platform_set_drvdata(pdev, gpadc); |
| 684 | |
| 685 | regulator_enable(gpadc->regu); |
| 686 | |
| 687 | pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY); |
| 688 | pm_runtime_use_autosuspend(gpadc->dev); |
| 689 | pm_runtime_set_active(gpadc->dev); |
| 690 | pm_runtime_enable(gpadc->dev); |
| 691 | |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 692 | ab8500_gpadc_read_calibration_data(gpadc); |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 693 | list_add_tail(&gpadc->node, &ab8500_gpadc_list); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 694 | dev_dbg(gpadc->dev, "probe success\n"); |
| 695 | return 0; |
Daniel Willerud | 633e0fa | 2011-03-05 11:46:27 +0100 | [diff] [blame] | 696 | fail_irq: |
| 697 | free_irq(gpadc->irq, gpadc); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 698 | fail: |
| 699 | kfree(gpadc); |
| 700 | gpadc = NULL; |
| 701 | return ret; |
| 702 | } |
| 703 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 704 | static int ab8500_gpadc_remove(struct platform_device *pdev) |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 705 | { |
| 706 | struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev); |
| 707 | |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 708 | /* remove this gpadc entry from the list */ |
| 709 | list_del(&gpadc->node); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 710 | /* remove interrupt - completion of Sw ADC conversion */ |
Daniel Willerud | 6321992 | 2011-03-05 11:46:13 +0100 | [diff] [blame] | 711 | free_irq(gpadc->irq, gpadc); |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 712 | |
| 713 | pm_runtime_get_sync(gpadc->dev); |
| 714 | pm_runtime_disable(gpadc->dev); |
| 715 | |
| 716 | regulator_disable(gpadc->regu); |
| 717 | |
| 718 | pm_runtime_set_suspended(gpadc->dev); |
| 719 | |
| 720 | pm_runtime_put_noidle(gpadc->dev); |
| 721 | |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 722 | kfree(gpadc); |
| 723 | gpadc = NULL; |
| 724 | return 0; |
| 725 | } |
| 726 | |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 727 | static const struct dev_pm_ops ab8500_gpadc_pm_ops = { |
| 728 | SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend, |
| 729 | ab8500_gpadc_runtime_resume, |
| 730 | ab8500_gpadc_runtime_idle) |
Daniel WILLERUD | 774c50a | 2012-04-12 08:15:05 +0200 | [diff] [blame] | 731 | SET_SYSTEM_SLEEP_PM_OPS(ab8500_gpadc_suspend, |
| 732 | ab8500_gpadc_resume) |
| 733 | |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 736 | static struct platform_driver ab8500_gpadc_driver = { |
| 737 | .probe = ab8500_gpadc_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 738 | .remove = ab8500_gpadc_remove, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 739 | .driver = { |
| 740 | .name = "ab8500-gpadc", |
| 741 | .owner = THIS_MODULE, |
Lee Jones | 5f8aaef | 2013-02-04 08:33:13 +0000 | [diff] [blame] | 742 | .pm = &ab8500_gpadc_pm_ops, |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 743 | }, |
| 744 | }; |
| 745 | |
| 746 | static int __init ab8500_gpadc_init(void) |
| 747 | { |
| 748 | return platform_driver_register(&ab8500_gpadc_driver); |
| 749 | } |
| 750 | |
| 751 | static void __exit ab8500_gpadc_exit(void) |
| 752 | { |
| 753 | platform_driver_unregister(&ab8500_gpadc_driver); |
| 754 | } |
| 755 | |
| 756 | subsys_initcall_sync(ab8500_gpadc_init); |
| 757 | module_exit(ab8500_gpadc_exit); |
| 758 | |
| 759 | MODULE_LICENSE("GPL v2"); |
Johan Palsson | 586f331 | 2011-03-05 11:46:37 +0100 | [diff] [blame] | 760 | MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson"); |
Arun Murthy | dae2db3 | 2011-02-22 10:11:13 +0100 | [diff] [blame] | 761 | MODULE_ALIAS("platform:ab8500_gpadc"); |
| 762 | MODULE_DESCRIPTION("AB8500 GPADC driver"); |