ab8500-gpadc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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>
  6. * Author: Daniel Willerud <daniel.willerud@stericsson.com>
  7. * Author: Johan Palsson <johan.palsson@stericsson.com>
  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>
  15. #include <linux/platform_device.h>
  16. #include <linux/completion.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/list.h>
  21. #include <linux/mfd/abx500.h>
  22. #include <linux/mfd/abx500/ab8500.h>
  23. #include <linux/mfd/abx500/ab8500-gpadc.h>
  24. /*
  25. * GPADC register offsets
  26. * Bank : 0x0A
  27. */
  28. #define AB8500_GPADC_CTRL1_REG 0x00
  29. #define AB8500_GPADC_CTRL2_REG 0x01
  30. #define AB8500_GPADC_CTRL3_REG 0x02
  31. #define AB8500_GPADC_AUTO_TIMER_REG 0x03
  32. #define AB8500_GPADC_STAT_REG 0x04
  33. #define AB8500_GPADC_MANDATAL_REG 0x05
  34. #define AB8500_GPADC_MANDATAH_REG 0x06
  35. #define AB8500_GPADC_AUTODATAL_REG 0x07
  36. #define AB8500_GPADC_AUTODATAH_REG 0x08
  37. #define AB8500_GPADC_MUX_CTRL_REG 0x09
  38. /*
  39. * OTP register offsets
  40. * Bank : 0x15
  41. */
  42. #define AB8500_GPADC_CAL_1 0x0F
  43. #define AB8500_GPADC_CAL_2 0x10
  44. #define AB8500_GPADC_CAL_3 0x11
  45. #define AB8500_GPADC_CAL_4 0x12
  46. #define AB8500_GPADC_CAL_5 0x13
  47. #define AB8500_GPADC_CAL_6 0x14
  48. #define AB8500_GPADC_CAL_7 0x15
  49. /* gpadc constants */
  50. #define EN_VINTCORE12 0x04
  51. #define EN_VTVOUT 0x02
  52. #define EN_GPADC 0x01
  53. #define DIS_GPADC 0x00
  54. #define SW_AVG_16 0x60
  55. #define ADC_SW_CONV 0x04
  56. #define EN_ICHAR 0x80
  57. #define BTEMP_PULL_UP 0x08
  58. #define EN_BUF 0x40
  59. #define DIS_ZERO 0x00
  60. #define GPADC_BUSY 0x01
  61. /* GPADC constants from AB8500 spec, UM0836 */
  62. #define ADC_RESOLUTION 1024
  63. #define ADC_CH_BTEMP_MIN 0
  64. #define ADC_CH_BTEMP_MAX 1350
  65. #define ADC_CH_DIETEMP_MIN 0
  66. #define ADC_CH_DIETEMP_MAX 1350
  67. #define ADC_CH_CHG_V_MIN 0
  68. #define ADC_CH_CHG_V_MAX 20030
  69. #define ADC_CH_ACCDET2_MIN 0
  70. #define ADC_CH_ACCDET2_MAX 2500
  71. #define ADC_CH_VBAT_MIN 2300
  72. #define ADC_CH_VBAT_MAX 4800
  73. #define ADC_CH_CHG_I_MIN 0
  74. #define ADC_CH_CHG_I_MAX 1500
  75. #define ADC_CH_BKBAT_MIN 0
  76. #define ADC_CH_BKBAT_MAX 3200
  77. /* This is used to not lose precision when dividing to get gain and offset */
  78. #define CALIB_SCALE 1000
  79. enum cal_channels {
  80. ADC_INPUT_VMAIN = 0,
  81. ADC_INPUT_BTEMP,
  82. ADC_INPUT_VBAT,
  83. NBR_CAL_INPUTS,
  84. };
  85. /**
  86. * struct adc_cal_data - Table for storing gain and offset for the calibrated
  87. * ADC channels
  88. * @gain: Gain of the ADC channel
  89. * @offset: Offset of the ADC channel
  90. */
  91. struct adc_cal_data {
  92. u64 gain;
  93. u64 offset;
  94. };
  95. /**
  96. * struct ab8500_gpadc - AB8500 GPADC device information
  97. * @chip_id ABB chip id
  98. * @dev: pointer to the struct device
  99. * @node: a list of AB8500 GPADCs, hence prepared for
  100. reentrance
  101. * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
  102. * the completion of gpadc conversion
  103. * @ab8500_gpadc_lock: structure of type mutex
  104. * @regu: pointer to the struct regulator
  105. * @irq: interrupt number that is used by gpadc
  106. * @cal_data array of ADC calibration data structs
  107. */
  108. struct ab8500_gpadc {
  109. u8 chip_id;
  110. struct device *dev;
  111. struct list_head node;
  112. struct completion ab8500_gpadc_complete;
  113. struct mutex ab8500_gpadc_lock;
  114. struct regulator *regu;
  115. int irq;
  116. struct adc_cal_data cal_data[NBR_CAL_INPUTS];
  117. };
  118. static LIST_HEAD(ab8500_gpadc_list);
  119. /**
  120. * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
  121. * (i.e. the first GPADC in the instance list)
  122. */
  123. struct ab8500_gpadc *ab8500_gpadc_get(char *name)
  124. {
  125. struct ab8500_gpadc *gpadc;
  126. list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
  127. if (!strcmp(name, dev_name(gpadc->dev)))
  128. return gpadc;
  129. }
  130. return ERR_PTR(-ENOENT);
  131. }
  132. EXPORT_SYMBOL(ab8500_gpadc_get);
  133. /**
  134. * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
  135. */
  136. int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel,
  137. int ad_value)
  138. {
  139. int res;
  140. switch (channel) {
  141. case MAIN_CHARGER_V:
  142. /* For some reason we don't have calibrated data */
  143. if (!gpadc->cal_data[ADC_INPUT_VMAIN].gain) {
  144. res = ADC_CH_CHG_V_MIN + (ADC_CH_CHG_V_MAX -
  145. ADC_CH_CHG_V_MIN) * ad_value /
  146. ADC_RESOLUTION;
  147. break;
  148. }
  149. /* Here we can use the calibrated data */
  150. res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VMAIN].gain +
  151. gpadc->cal_data[ADC_INPUT_VMAIN].offset) / CALIB_SCALE;
  152. break;
  153. case BAT_CTRL:
  154. case BTEMP_BALL:
  155. case ACC_DETECT1:
  156. case ADC_AUX1:
  157. case ADC_AUX2:
  158. /* For some reason we don't have calibrated data */
  159. if (!gpadc->cal_data[ADC_INPUT_BTEMP].gain) {
  160. res = ADC_CH_BTEMP_MIN + (ADC_CH_BTEMP_MAX -
  161. ADC_CH_BTEMP_MIN) * ad_value /
  162. ADC_RESOLUTION;
  163. break;
  164. }
  165. /* Here we can use the calibrated data */
  166. res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_BTEMP].gain +
  167. gpadc->cal_data[ADC_INPUT_BTEMP].offset) / CALIB_SCALE;
  168. break;
  169. case MAIN_BAT_V:
  170. /* For some reason we don't have calibrated data */
  171. if (!gpadc->cal_data[ADC_INPUT_VBAT].gain) {
  172. res = ADC_CH_VBAT_MIN + (ADC_CH_VBAT_MAX -
  173. ADC_CH_VBAT_MIN) * ad_value /
  174. ADC_RESOLUTION;
  175. break;
  176. }
  177. /* Here we can use the calibrated data */
  178. res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VBAT].gain +
  179. gpadc->cal_data[ADC_INPUT_VBAT].offset) / CALIB_SCALE;
  180. break;
  181. case DIE_TEMP:
  182. res = ADC_CH_DIETEMP_MIN +
  183. (ADC_CH_DIETEMP_MAX - ADC_CH_DIETEMP_MIN) * ad_value /
  184. ADC_RESOLUTION;
  185. break;
  186. case ACC_DETECT2:
  187. res = ADC_CH_ACCDET2_MIN +
  188. (ADC_CH_ACCDET2_MAX - ADC_CH_ACCDET2_MIN) * ad_value /
  189. ADC_RESOLUTION;
  190. break;
  191. case VBUS_V:
  192. res = ADC_CH_CHG_V_MIN +
  193. (ADC_CH_CHG_V_MAX - ADC_CH_CHG_V_MIN) * ad_value /
  194. ADC_RESOLUTION;
  195. break;
  196. case MAIN_CHARGER_C:
  197. case USB_CHARGER_C:
  198. res = ADC_CH_CHG_I_MIN +
  199. (ADC_CH_CHG_I_MAX - ADC_CH_CHG_I_MIN) * ad_value /
  200. ADC_RESOLUTION;
  201. break;
  202. case BK_BAT_V:
  203. res = ADC_CH_BKBAT_MIN +
  204. (ADC_CH_BKBAT_MAX - ADC_CH_BKBAT_MIN) * ad_value /
  205. ADC_RESOLUTION;
  206. break;
  207. default:
  208. dev_err(gpadc->dev,
  209. "unknown channel, not possible to convert\n");
  210. res = -EINVAL;
  211. break;
  212. }
  213. return res;
  214. }
  215. EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage);
  216. /**
  217. * ab8500_gpadc_convert() - gpadc conversion
  218. * @channel: analog channel to be converted to digital data
  219. *
  220. * This function converts the selected analog i/p to digital
  221. * data.
  222. */
  223. int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 channel)
  224. {
  225. int ad_value;
  226. int voltage;
  227. ad_value = ab8500_gpadc_read_raw(gpadc, channel);
  228. if (ad_value < 0) {
  229. dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n", channel);
  230. return ad_value;
  231. }
  232. voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value);
  233. if (voltage < 0)
  234. dev_err(gpadc->dev, "GPADC to voltage conversion failed ch:"
  235. " %d AD: 0x%x\n", channel, ad_value);
  236. return voltage;
  237. }
  238. EXPORT_SYMBOL(ab8500_gpadc_convert);
  239. /**
  240. * ab8500_gpadc_read_raw() - gpadc read
  241. * @channel: analog channel to be read
  242. *
  243. * This function obtains the raw ADC value, this then needs
  244. * to be converted by calling ab8500_gpadc_ad_to_voltage()
  245. */
  246. int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel)
  247. {
  248. int ret;
  249. int looplimit = 0;
  250. u8 val, low_data, high_data;
  251. if (!gpadc)
  252. return -ENODEV;
  253. mutex_lock(&gpadc->ab8500_gpadc_lock);
  254. /* Enable VTVout LDO this is required for GPADC */
  255. regulator_enable(gpadc->regu);
  256. /* Check if ADC is not busy, lock and proceed */
  257. do {
  258. ret = abx500_get_register_interruptible(gpadc->dev,
  259. AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
  260. if (ret < 0)
  261. goto out;
  262. if (!(val & GPADC_BUSY))
  263. break;
  264. msleep(10);
  265. } while (++looplimit < 10);
  266. if (looplimit >= 10 && (val & GPADC_BUSY)) {
  267. dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
  268. ret = -EINVAL;
  269. goto out;
  270. }
  271. /* Enable GPADC */
  272. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  273. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
  274. if (ret < 0) {
  275. dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
  276. goto out;
  277. }
  278. /* Select the channel source and set average samples to 16 */
  279. ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  280. AB8500_GPADC_CTRL2_REG, (channel | SW_AVG_16));
  281. if (ret < 0) {
  282. dev_err(gpadc->dev,
  283. "gpadc_conversion: set avg samples failed\n");
  284. goto out;
  285. }
  286. /*
  287. * Enable ADC, buffering, select rising edge and enable ADC path
  288. * charging current sense if it needed, ABB 3.0 needs some special
  289. * treatment too.
  290. */
  291. switch (channel) {
  292. case MAIN_CHARGER_C:
  293. case USB_CHARGER_C:
  294. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  295. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  296. EN_BUF | EN_ICHAR,
  297. EN_BUF | EN_ICHAR);
  298. break;
  299. case BTEMP_BALL:
  300. if (gpadc->chip_id >= AB8500_CUT3P0) {
  301. /* Turn on btemp pull-up on ABB 3.0 */
  302. ret = abx500_mask_and_set_register_interruptible(
  303. gpadc->dev,
  304. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  305. EN_BUF | BTEMP_PULL_UP,
  306. EN_BUF | BTEMP_PULL_UP);
  307. /*
  308. * Delay might be needed for ABB8500 cut 3.0, if not, remove
  309. * when hardware will be availible
  310. */
  311. msleep(1);
  312. break;
  313. }
  314. /* Intentional fallthrough */
  315. default:
  316. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  317. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
  318. break;
  319. }
  320. if (ret < 0) {
  321. dev_err(gpadc->dev,
  322. "gpadc_conversion: select falling edge failed\n");
  323. goto out;
  324. }
  325. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  326. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV);
  327. if (ret < 0) {
  328. dev_err(gpadc->dev,
  329. "gpadc_conversion: start s/w conversion failed\n");
  330. goto out;
  331. }
  332. /* wait for completion of conversion */
  333. if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, 2*HZ)) {
  334. dev_err(gpadc->dev,
  335. "timeout: didn't receive GPADC conversion interrupt\n");
  336. ret = -EINVAL;
  337. goto out;
  338. }
  339. /* Read the converted RAW data */
  340. ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
  341. AB8500_GPADC_MANDATAL_REG, &low_data);
  342. if (ret < 0) {
  343. dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n");
  344. goto out;
  345. }
  346. ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
  347. AB8500_GPADC_MANDATAH_REG, &high_data);
  348. if (ret < 0) {
  349. dev_err(gpadc->dev,
  350. "gpadc_conversion: read high data failed\n");
  351. goto out;
  352. }
  353. /* Disable GPADC */
  354. ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  355. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  356. if (ret < 0) {
  357. dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
  358. goto out;
  359. }
  360. /* Disable VTVout LDO this is required for GPADC */
  361. regulator_disable(gpadc->regu);
  362. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  363. return (high_data << 8) | low_data;
  364. out:
  365. /*
  366. * It has shown to be needed to turn off the GPADC if an error occurs,
  367. * otherwise we might have problem when waiting for the busy bit in the
  368. * GPADC status register to go low. In V1.1 there wait_for_completion
  369. * seems to timeout when waiting for an interrupt.. Not seen in V2.0
  370. */
  371. (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  372. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  373. regulator_disable(gpadc->regu);
  374. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  375. dev_err(gpadc->dev,
  376. "gpadc_conversion: Failed to AD convert channel %d\n", channel);
  377. return ret;
  378. }
  379. EXPORT_SYMBOL(ab8500_gpadc_read_raw);
  380. /**
  381. * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion
  382. * @irq: irq number
  383. * @data: pointer to the data passed during request irq
  384. *
  385. * This is a interrupt service routine for s/w gpadc conversion completion.
  386. * Notifies the gpadc completion is completed and the converted raw value
  387. * can be read from the registers.
  388. * Returns IRQ status(IRQ_HANDLED)
  389. */
  390. static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_gpadc)
  391. {
  392. struct ab8500_gpadc *gpadc = _gpadc;
  393. complete(&gpadc->ab8500_gpadc_complete);
  394. return IRQ_HANDLED;
  395. }
  396. static int otp_cal_regs[] = {
  397. AB8500_GPADC_CAL_1,
  398. AB8500_GPADC_CAL_2,
  399. AB8500_GPADC_CAL_3,
  400. AB8500_GPADC_CAL_4,
  401. AB8500_GPADC_CAL_5,
  402. AB8500_GPADC_CAL_6,
  403. AB8500_GPADC_CAL_7,
  404. };
  405. static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
  406. {
  407. int i;
  408. int ret[ARRAY_SIZE(otp_cal_regs)];
  409. u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
  410. int vmain_high, vmain_low;
  411. int btemp_high, btemp_low;
  412. int vbat_high, vbat_low;
  413. /* First we read all OTP registers and store the error code */
  414. for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
  415. ret[i] = abx500_get_register_interruptible(gpadc->dev,
  416. AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]);
  417. if (ret[i] < 0)
  418. dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
  419. __func__, otp_cal_regs[i]);
  420. }
  421. /*
  422. * The ADC calibration data is stored in OTP registers.
  423. * The layout of the calibration data is outlined below and a more
  424. * detailed description can be found in UM0836
  425. *
  426. * vm_h/l = vmain_high/low
  427. * bt_h/l = btemp_high/low
  428. * vb_h/l = vbat_high/low
  429. *
  430. * Data bits:
  431. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  432. * |.......|.......|.......|.......|.......|.......|.......|.......
  433. * | | vm_h9 | vm_h8
  434. * |.......|.......|.......|.......|.......|.......|.......|.......
  435. * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
  436. * |.......|.......|.......|.......|.......|.......|.......|.......
  437. * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
  438. * |.......|.......|.......|.......|.......|.......|.......|.......
  439. * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
  440. * |.......|.......|.......|.......|.......|.......|.......|.......
  441. * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
  442. * |.......|.......|.......|.......|.......|.......|.......|.......
  443. * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
  444. * |.......|.......|.......|.......|.......|.......|.......|.......
  445. * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
  446. * |.......|.......|.......|.......|.......|.......|.......|.......
  447. *
  448. *
  449. * Ideal output ADC codes corresponding to injected input voltages
  450. * during manufacturing is:
  451. *
  452. * vmain_high: Vin = 19500mV / ADC ideal code = 997
  453. * vmain_low: Vin = 315mV / ADC ideal code = 16
  454. * btemp_high: Vin = 1300mV / ADC ideal code = 985
  455. * btemp_low: Vin = 21mV / ADC ideal code = 16
  456. * vbat_high: Vin = 4700mV / ADC ideal code = 982
  457. * vbat_low: Vin = 2380mV / ADC ideal code = 33
  458. */
  459. /* Calculate gain and offset for VMAIN if all reads succeeded */
  460. if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
  461. vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
  462. ((gpadc_cal[1] & 0x3F) << 2) |
  463. ((gpadc_cal[2] & 0xC0) >> 6));
  464. vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
  465. gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
  466. (19500 - 315) / (vmain_high - vmain_low);
  467. gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 19500 -
  468. (CALIB_SCALE * (19500 - 315) /
  469. (vmain_high - vmain_low)) * vmain_high;
  470. } else {
  471. gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
  472. }
  473. /* Calculate gain and offset for BTEMP if all reads succeeded */
  474. if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
  475. btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
  476. (gpadc_cal[3] << 1) |
  477. ((gpadc_cal[4] & 0x80) >> 7));
  478. btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
  479. gpadc->cal_data[ADC_INPUT_BTEMP].gain =
  480. CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
  481. gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 -
  482. (CALIB_SCALE * (1300 - 21) /
  483. (btemp_high - btemp_low)) * btemp_high;
  484. } else {
  485. gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0;
  486. }
  487. /* Calculate gain and offset for VBAT if all reads succeeded */
  488. if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
  489. vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
  490. vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
  491. gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE *
  492. (4700 - 2380) / (vbat_high - vbat_low);
  493. gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 -
  494. (CALIB_SCALE * (4700 - 2380) /
  495. (vbat_high - vbat_low)) * vbat_high;
  496. } else {
  497. gpadc->cal_data[ADC_INPUT_VBAT].gain = 0;
  498. }
  499. dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n",
  500. gpadc->cal_data[ADC_INPUT_VMAIN].gain,
  501. gpadc->cal_data[ADC_INPUT_VMAIN].offset);
  502. dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n",
  503. gpadc->cal_data[ADC_INPUT_BTEMP].gain,
  504. gpadc->cal_data[ADC_INPUT_BTEMP].offset);
  505. dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n",
  506. gpadc->cal_data[ADC_INPUT_VBAT].gain,
  507. gpadc->cal_data[ADC_INPUT_VBAT].offset);
  508. }
  509. static int __devinit ab8500_gpadc_probe(struct platform_device *pdev)
  510. {
  511. int ret = 0;
  512. struct ab8500_gpadc *gpadc;
  513. gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
  514. if (!gpadc) {
  515. dev_err(&pdev->dev, "Error: No memory\n");
  516. return -ENOMEM;
  517. }
  518. gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END");
  519. if (gpadc->irq < 0) {
  520. dev_err(gpadc->dev, "failed to get platform irq-%d\n",
  521. gpadc->irq);
  522. ret = gpadc->irq;
  523. goto fail;
  524. }
  525. gpadc->dev = &pdev->dev;
  526. mutex_init(&gpadc->ab8500_gpadc_lock);
  527. /* Initialize completion used to notify completion of conversion */
  528. init_completion(&gpadc->ab8500_gpadc_complete);
  529. /* Register interrupt - SwAdcComplete */
  530. ret = request_threaded_irq(gpadc->irq, NULL,
  531. ab8500_bm_gpswadcconvend_handler,
  532. IRQF_NO_SUSPEND | IRQF_SHARED, "ab8500-gpadc", gpadc);
  533. if (ret < 0) {
  534. dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
  535. gpadc->irq);
  536. goto fail;
  537. }
  538. /* Get Chip ID of the ABB ASIC */
  539. ret = abx500_get_chip_id(gpadc->dev);
  540. if (ret < 0) {
  541. dev_err(gpadc->dev, "failed to get chip ID\n");
  542. goto fail_irq;
  543. }
  544. gpadc->chip_id = (u8) ret;
  545. /* VTVout LDO used to power up ab8500-GPADC */
  546. gpadc->regu = regulator_get(&pdev->dev, "vddadc");
  547. if (IS_ERR(gpadc->regu)) {
  548. ret = PTR_ERR(gpadc->regu);
  549. dev_err(gpadc->dev, "failed to get vtvout LDO\n");
  550. goto fail_irq;
  551. }
  552. ab8500_gpadc_read_calibration_data(gpadc);
  553. list_add_tail(&gpadc->node, &ab8500_gpadc_list);
  554. dev_dbg(gpadc->dev, "probe success\n");
  555. return 0;
  556. fail_irq:
  557. free_irq(gpadc->irq, gpadc);
  558. fail:
  559. kfree(gpadc);
  560. gpadc = NULL;
  561. return ret;
  562. }
  563. static int __devexit ab8500_gpadc_remove(struct platform_device *pdev)
  564. {
  565. struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
  566. /* remove this gpadc entry from the list */
  567. list_del(&gpadc->node);
  568. /* remove interrupt - completion of Sw ADC conversion */
  569. free_irq(gpadc->irq, gpadc);
  570. /* disable VTVout LDO that is being used by GPADC */
  571. regulator_put(gpadc->regu);
  572. kfree(gpadc);
  573. gpadc = NULL;
  574. return 0;
  575. }
  576. static struct platform_driver ab8500_gpadc_driver = {
  577. .probe = ab8500_gpadc_probe,
  578. .remove = __devexit_p(ab8500_gpadc_remove),
  579. .driver = {
  580. .name = "ab8500-gpadc",
  581. .owner = THIS_MODULE,
  582. },
  583. };
  584. static int __init ab8500_gpadc_init(void)
  585. {
  586. return platform_driver_register(&ab8500_gpadc_driver);
  587. }
  588. static void __exit ab8500_gpadc_exit(void)
  589. {
  590. platform_driver_unregister(&ab8500_gpadc_driver);
  591. }
  592. subsys_initcall_sync(ab8500_gpadc_init);
  593. module_exit(ab8500_gpadc_exit);
  594. MODULE_LICENSE("GPL v2");
  595. MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson");
  596. MODULE_ALIAS("platform:ab8500_gpadc");
  597. MODULE_DESCRIPTION("AB8500 GPADC driver");