ab8500-gpadc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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/pm_runtime.h>
  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>
  21. #include <linux/list.h>
  22. #include <linux/mfd/abx500.h>
  23. #include <linux/mfd/abx500/ab8500.h>
  24. #include <linux/mfd/abx500/ab8500-gpadc.h>
  25. /*
  26. * GPADC register offsets
  27. * Bank : 0x0A
  28. */
  29. #define AB8500_GPADC_CTRL1_REG 0x00
  30. #define AB8500_GPADC_CTRL2_REG 0x01
  31. #define AB8500_GPADC_CTRL3_REG 0x02
  32. #define AB8500_GPADC_AUTO_TIMER_REG 0x03
  33. #define AB8500_GPADC_STAT_REG 0x04
  34. #define AB8500_GPADC_MANDATAL_REG 0x05
  35. #define AB8500_GPADC_MANDATAH_REG 0x06
  36. #define AB8500_GPADC_AUTODATAL_REG 0x07
  37. #define AB8500_GPADC_AUTODATAH_REG 0x08
  38. #define AB8500_GPADC_MUX_CTRL_REG 0x09
  39. #define AB8540_GPADC_MANDATA2L_REG 0x09
  40. #define AB8540_GPADC_MANDATA2H_REG 0x0A
  41. #define AB8540_GPADC_APEAAX_REG 0x10
  42. #define AB8540_GPADC_APEAAT_REG 0x11
  43. #define AB8540_GPADC_APEAAM_REG 0x12
  44. #define AB8540_GPADC_APEAAH_REG 0x13
  45. #define AB8540_GPADC_APEAAL_REG 0x14
  46. /*
  47. * OTP register offsets
  48. * Bank : 0x15
  49. */
  50. #define AB8500_GPADC_CAL_1 0x0F
  51. #define AB8500_GPADC_CAL_2 0x10
  52. #define AB8500_GPADC_CAL_3 0x11
  53. #define AB8500_GPADC_CAL_4 0x12
  54. #define AB8500_GPADC_CAL_5 0x13
  55. #define AB8500_GPADC_CAL_6 0x14
  56. #define AB8500_GPADC_CAL_7 0x15
  57. /* New calibration for 8540 */
  58. #define AB8540_GPADC_OTP4_REG_7 0x38
  59. #define AB8540_GPADC_OTP4_REG_6 0x39
  60. #define AB8540_GPADC_OTP4_REG_5 0x3A
  61. /* gpadc constants */
  62. #define EN_VINTCORE12 0x04
  63. #define EN_VTVOUT 0x02
  64. #define EN_GPADC 0x01
  65. #define DIS_GPADC 0x00
  66. #define AVG_1 0x00
  67. #define AVG_4 0x20
  68. #define AVG_8 0x40
  69. #define AVG_16 0x60
  70. #define ADC_SW_CONV 0x04
  71. #define EN_ICHAR 0x80
  72. #define BTEMP_PULL_UP 0x08
  73. #define EN_BUF 0x40
  74. #define DIS_ZERO 0x00
  75. #define GPADC_BUSY 0x01
  76. #define EN_FALLING 0x10
  77. #define EN_TRIG_EDGE 0x02
  78. #define EN_VBIAS_XTAL_TEMP 0x02
  79. /* GPADC constants from AB8500 spec, UM0836 */
  80. #define ADC_RESOLUTION 1024
  81. #define ADC_CH_BTEMP_MIN 0
  82. #define ADC_CH_BTEMP_MAX 1350
  83. #define ADC_CH_DIETEMP_MIN 0
  84. #define ADC_CH_DIETEMP_MAX 1350
  85. #define ADC_CH_CHG_V_MIN 0
  86. #define ADC_CH_CHG_V_MAX 20030
  87. #define ADC_CH_ACCDET2_MIN 0
  88. #define ADC_CH_ACCDET2_MAX 2500
  89. #define ADC_CH_VBAT_MIN 2300
  90. #define ADC_CH_VBAT_MAX 4800
  91. #define ADC_CH_CHG_I_MIN 0
  92. #define ADC_CH_CHG_I_MAX 1500
  93. #define ADC_CH_BKBAT_MIN 0
  94. #define ADC_CH_BKBAT_MAX 3200
  95. /* GPADC constants from AB8540 spec */
  96. #define ADC_CH_IBAT_MIN (-6000) /* mA range measured by ADC for ibat*/
  97. #define ADC_CH_IBAT_MAX 6000
  98. #define ADC_CH_IBAT_MIN_V (-60) /* mV range measured by ADC for ibat*/
  99. #define ADC_CH_IBAT_MAX_V 60
  100. #define IBAT_VDROP_L (-56) /* mV */
  101. #define IBAT_VDROP_H 56
  102. /* This is used to not lose precision when dividing to get gain and offset */
  103. #define CALIB_SCALE 1000
  104. /*
  105. * Number of bits shift used to not lose precision
  106. * when dividing to get ibat gain.
  107. */
  108. #define CALIB_SHIFT_IBAT 20
  109. /* Time in ms before disabling regulator */
  110. #define GPADC_AUDOSUSPEND_DELAY 1
  111. #define CONVERSION_TIME 500 /* ms */
  112. enum cal_channels {
  113. ADC_INPUT_VMAIN = 0,
  114. ADC_INPUT_BTEMP,
  115. ADC_INPUT_VBAT,
  116. ADC_INPUT_IBAT,
  117. NBR_CAL_INPUTS,
  118. };
  119. /**
  120. * struct adc_cal_data - Table for storing gain and offset for the calibrated
  121. * ADC channels
  122. * @gain: Gain of the ADC channel
  123. * @offset: Offset of the ADC channel
  124. */
  125. struct adc_cal_data {
  126. s64 gain;
  127. s64 offset;
  128. };
  129. /**
  130. * struct ab8500_gpadc - AB8500 GPADC device information
  131. * @dev: pointer to the struct device
  132. * @node: a list of AB8500 GPADCs, hence prepared for
  133. reentrance
  134. * @parent: pointer to the struct ab8500
  135. * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
  136. * the completion of gpadc conversion
  137. * @ab8500_gpadc_lock: structure of type mutex
  138. * @regu: pointer to the struct regulator
  139. * @irq_sw: interrupt number that is used by gpadc for Sw
  140. * conversion
  141. * @irq_hw: interrupt number that is used by gpadc for Hw
  142. * conversion
  143. * @cal_data array of ADC calibration data structs
  144. */
  145. struct ab8500_gpadc {
  146. struct device *dev;
  147. struct list_head node;
  148. struct ab8500 *parent;
  149. struct completion ab8500_gpadc_complete;
  150. struct mutex ab8500_gpadc_lock;
  151. struct regulator *regu;
  152. int irq_sw;
  153. int irq_hw;
  154. struct adc_cal_data cal_data[NBR_CAL_INPUTS];
  155. };
  156. static LIST_HEAD(ab8500_gpadc_list);
  157. /**
  158. * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
  159. * (i.e. the first GPADC in the instance list)
  160. */
  161. struct ab8500_gpadc *ab8500_gpadc_get(char *name)
  162. {
  163. struct ab8500_gpadc *gpadc;
  164. list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
  165. if (!strcmp(name, dev_name(gpadc->dev)))
  166. return gpadc;
  167. }
  168. return ERR_PTR(-ENOENT);
  169. }
  170. EXPORT_SYMBOL(ab8500_gpadc_get);
  171. /**
  172. * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
  173. */
  174. int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel,
  175. int ad_value)
  176. {
  177. int res;
  178. switch (channel) {
  179. case MAIN_CHARGER_V:
  180. /* For some reason we don't have calibrated data */
  181. if (!gpadc->cal_data[ADC_INPUT_VMAIN].gain) {
  182. res = ADC_CH_CHG_V_MIN + (ADC_CH_CHG_V_MAX -
  183. ADC_CH_CHG_V_MIN) * ad_value /
  184. ADC_RESOLUTION;
  185. break;
  186. }
  187. /* Here we can use the calibrated data */
  188. res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VMAIN].gain +
  189. gpadc->cal_data[ADC_INPUT_VMAIN].offset) / CALIB_SCALE;
  190. break;
  191. case XTAL_TEMP:
  192. case BAT_CTRL:
  193. case BTEMP_BALL:
  194. case ACC_DETECT1:
  195. case ADC_AUX1:
  196. case ADC_AUX2:
  197. /* For some reason we don't have calibrated data */
  198. if (!gpadc->cal_data[ADC_INPUT_BTEMP].gain) {
  199. res = ADC_CH_BTEMP_MIN + (ADC_CH_BTEMP_MAX -
  200. ADC_CH_BTEMP_MIN) * ad_value /
  201. ADC_RESOLUTION;
  202. break;
  203. }
  204. /* Here we can use the calibrated data */
  205. res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_BTEMP].gain +
  206. gpadc->cal_data[ADC_INPUT_BTEMP].offset) / CALIB_SCALE;
  207. break;
  208. case MAIN_BAT_V:
  209. case VBAT_TRUE_MEAS:
  210. /* For some reason we don't have calibrated data */
  211. if (!gpadc->cal_data[ADC_INPUT_VBAT].gain) {
  212. res = ADC_CH_VBAT_MIN + (ADC_CH_VBAT_MAX -
  213. ADC_CH_VBAT_MIN) * ad_value /
  214. ADC_RESOLUTION;
  215. break;
  216. }
  217. /* Here we can use the calibrated data */
  218. res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VBAT].gain +
  219. gpadc->cal_data[ADC_INPUT_VBAT].offset) / CALIB_SCALE;
  220. break;
  221. case DIE_TEMP:
  222. res = ADC_CH_DIETEMP_MIN +
  223. (ADC_CH_DIETEMP_MAX - ADC_CH_DIETEMP_MIN) * ad_value /
  224. ADC_RESOLUTION;
  225. break;
  226. case ACC_DETECT2:
  227. res = ADC_CH_ACCDET2_MIN +
  228. (ADC_CH_ACCDET2_MAX - ADC_CH_ACCDET2_MIN) * ad_value /
  229. ADC_RESOLUTION;
  230. break;
  231. case VBUS_V:
  232. res = ADC_CH_CHG_V_MIN +
  233. (ADC_CH_CHG_V_MAX - ADC_CH_CHG_V_MIN) * ad_value /
  234. ADC_RESOLUTION;
  235. break;
  236. case MAIN_CHARGER_C:
  237. case USB_CHARGER_C:
  238. res = ADC_CH_CHG_I_MIN +
  239. (ADC_CH_CHG_I_MAX - ADC_CH_CHG_I_MIN) * ad_value /
  240. ADC_RESOLUTION;
  241. break;
  242. case BK_BAT_V:
  243. res = ADC_CH_BKBAT_MIN +
  244. (ADC_CH_BKBAT_MAX - ADC_CH_BKBAT_MIN) * ad_value /
  245. ADC_RESOLUTION;
  246. break;
  247. case IBAT_VIRTUAL_CHANNEL:
  248. /* For some reason we don't have calibrated data */
  249. if (!gpadc->cal_data[ADC_INPUT_IBAT].gain) {
  250. res = ADC_CH_IBAT_MIN + (ADC_CH_IBAT_MAX -
  251. ADC_CH_IBAT_MIN) * ad_value /
  252. ADC_RESOLUTION;
  253. break;
  254. }
  255. /* Here we can use the calibrated data */
  256. res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_IBAT].gain +
  257. gpadc->cal_data[ADC_INPUT_IBAT].offset)
  258. >> CALIB_SHIFT_IBAT;
  259. break;
  260. default:
  261. dev_err(gpadc->dev,
  262. "unknown channel, not possible to convert\n");
  263. res = -EINVAL;
  264. break;
  265. }
  266. return res;
  267. }
  268. EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage);
  269. /**
  270. * ab8500_gpadc_sw_hw_convert() - gpadc conversion
  271. * @channel: analog channel to be converted to digital data
  272. * @avg_sample: number of ADC sample to average
  273. * @trig_egde: selected ADC trig edge
  274. * @trig_timer: selected ADC trigger delay timer
  275. * @conv_type: selected conversion type (HW or SW conversion)
  276. *
  277. * This function converts the selected analog i/p to digital
  278. * data.
  279. */
  280. int ab8500_gpadc_sw_hw_convert(struct ab8500_gpadc *gpadc, u8 channel,
  281. u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type)
  282. {
  283. int ad_value;
  284. int voltage;
  285. ad_value = ab8500_gpadc_read_raw(gpadc, channel, avg_sample,
  286. trig_edge, trig_timer, conv_type);
  287. /* On failure retry a second time */
  288. if (ad_value < 0)
  289. ad_value = ab8500_gpadc_read_raw(gpadc, channel, avg_sample,
  290. trig_edge, trig_timer, conv_type);
  291. if (ad_value < 0) {
  292. dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n",
  293. channel);
  294. return ad_value;
  295. }
  296. voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value);
  297. if (voltage < 0)
  298. dev_err(gpadc->dev, "GPADC to voltage conversion failed ch:"
  299. " %d AD: 0x%x\n", channel, ad_value);
  300. return voltage;
  301. }
  302. EXPORT_SYMBOL(ab8500_gpadc_convert);
  303. /**
  304. * ab8500_gpadc_read_raw() - gpadc read
  305. * @channel: analog channel to be read
  306. * @avg_sample: number of ADC sample to average
  307. * @trig_edge: selected trig edge
  308. * @trig_timer: selected ADC trigger delay timer
  309. * @conv_type: selected conversion type (HW or SW conversion)
  310. *
  311. * This function obtains the raw ADC value for an hardware conversion,
  312. * this then needs to be converted by calling ab8500_gpadc_ad_to_voltage()
  313. */
  314. int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel,
  315. u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type)
  316. {
  317. int raw_data;
  318. raw_data = ab8500_gpadc_double_read_raw(gpadc, channel,
  319. avg_sample, trig_edge, trig_timer, conv_type, NULL);
  320. return raw_data;
  321. }
  322. int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel,
  323. u8 avg_sample, u8 trig_edge, u8 trig_timer, u8 conv_type,
  324. int *ibat)
  325. {
  326. int ret;
  327. int looplimit = 0;
  328. u8 val, low_data, high_data, low_data2, high_data2;
  329. if (!gpadc)
  330. return -ENODEV;
  331. /* check if convertion is supported */
  332. if ((gpadc->irq_sw < 0) && (conv_type == ADC_SW))
  333. return -ENOTSUPP;
  334. if ((gpadc->irq_hw < 0) && (conv_type == ADC_HW))
  335. return -ENOTSUPP;
  336. mutex_lock(&gpadc->ab8500_gpadc_lock);
  337. /* Enable VTVout LDO this is required for GPADC */
  338. pm_runtime_get_sync(gpadc->dev);
  339. /* Check if ADC is not busy, lock and proceed */
  340. do {
  341. ret = abx500_get_register_interruptible(gpadc->dev,
  342. AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
  343. if (ret < 0)
  344. goto out;
  345. if (!(val & GPADC_BUSY))
  346. break;
  347. msleep(10);
  348. } while (++looplimit < 10);
  349. if (looplimit >= 10 && (val & GPADC_BUSY)) {
  350. dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
  351. ret = -EINVAL;
  352. goto out;
  353. }
  354. /* Enable GPADC */
  355. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  356. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
  357. if (ret < 0) {
  358. dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
  359. goto out;
  360. }
  361. /* Select the channel source and set average samples */
  362. switch (avg_sample) {
  363. case SAMPLE_1:
  364. val = channel | AVG_1;
  365. break;
  366. case SAMPLE_4:
  367. val = channel | AVG_4;
  368. break;
  369. case SAMPLE_8:
  370. val = channel | AVG_8;
  371. break;
  372. default:
  373. val = channel | AVG_16;
  374. break;
  375. }
  376. if (conv_type == ADC_HW)
  377. ret = abx500_set_register_interruptible(gpadc->dev,
  378. AB8500_GPADC, AB8500_GPADC_CTRL3_REG, val);
  379. else
  380. ret = abx500_set_register_interruptible(gpadc->dev,
  381. AB8500_GPADC, AB8500_GPADC_CTRL2_REG, val);
  382. if (ret < 0) {
  383. dev_err(gpadc->dev,
  384. "gpadc_conversion: set avg samples failed\n");
  385. goto out;
  386. }
  387. /*
  388. * Enable ADC, buffering, select rising edge and enable ADC path
  389. * charging current sense if it needed, ABB 3.0 needs some special
  390. * treatment too.
  391. */
  392. if ((conv_type == ADC_HW) && (trig_edge)) {
  393. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  394. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  395. EN_FALLING, EN_FALLING);
  396. }
  397. switch (channel) {
  398. case MAIN_CHARGER_C:
  399. case USB_CHARGER_C:
  400. if (conv_type == ADC_HW)
  401. ret = abx500_mask_and_set_register_interruptible(
  402. gpadc->dev,
  403. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  404. EN_BUF | EN_ICHAR | EN_TRIG_EDGE,
  405. EN_BUF | EN_ICHAR | EN_TRIG_EDGE);
  406. else
  407. ret = abx500_mask_and_set_register_interruptible(
  408. gpadc->dev,
  409. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  410. EN_BUF | EN_ICHAR,
  411. EN_BUF | EN_ICHAR);
  412. break;
  413. case XTAL_TEMP:
  414. if (conv_type == ADC_HW)
  415. ret = abx500_mask_and_set_register_interruptible(
  416. gpadc->dev,
  417. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  418. EN_BUF | EN_TRIG_EDGE,
  419. EN_BUF | EN_TRIG_EDGE);
  420. else
  421. ret = abx500_mask_and_set_register_interruptible(
  422. gpadc->dev,
  423. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  424. EN_BUF ,
  425. EN_BUF);
  426. break;
  427. case VBAT_TRUE_MEAS:
  428. if (conv_type == ADC_HW)
  429. ret = abx500_mask_and_set_register_interruptible(
  430. gpadc->dev,
  431. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  432. EN_BUF | EN_TRIG_EDGE,
  433. EN_BUF | EN_TRIG_EDGE);
  434. else
  435. ret = abx500_mask_and_set_register_interruptible(
  436. gpadc->dev,
  437. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  438. EN_BUF ,
  439. EN_BUF);
  440. break;
  441. case BAT_CTRL_AND_IBAT:
  442. case VBAT_MEAS_AND_IBAT:
  443. case VBAT_TRUE_MEAS_AND_IBAT:
  444. case BAT_TEMP_AND_IBAT:
  445. if (conv_type == ADC_HW)
  446. ret = abx500_mask_and_set_register_interruptible(
  447. gpadc->dev,
  448. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  449. EN_TRIG_EDGE,
  450. EN_TRIG_EDGE);
  451. else
  452. ret = abx500_mask_and_set_register_interruptible(
  453. gpadc->dev,
  454. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  455. EN_BUF,
  456. 0);
  457. break;
  458. case BTEMP_BALL:
  459. if (!is_ab8500_2p0_or_earlier(gpadc->parent)) {
  460. if (conv_type == ADC_HW)
  461. /* Turn on btemp pull-up on ABB 3.0 */
  462. ret = abx500_mask_and_set_register_interruptible
  463. (gpadc->dev,
  464. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  465. EN_BUF | BTEMP_PULL_UP | EN_TRIG_EDGE,
  466. EN_BUF | BTEMP_PULL_UP | EN_TRIG_EDGE);
  467. else
  468. ret = abx500_mask_and_set_register_interruptible
  469. (gpadc->dev,
  470. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  471. EN_BUF | BTEMP_PULL_UP,
  472. EN_BUF | BTEMP_PULL_UP);
  473. /*
  474. * Delay might be needed for ABB8500 cut 3.0, if not, remove
  475. * when hardware will be available
  476. */
  477. usleep_range(1000, 1000);
  478. break;
  479. }
  480. /* Intentional fallthrough */
  481. default:
  482. if (conv_type == ADC_HW)
  483. ret = abx500_mask_and_set_register_interruptible(
  484. gpadc->dev,
  485. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  486. EN_BUF | EN_TRIG_EDGE,
  487. EN_BUF | EN_TRIG_EDGE);
  488. else
  489. ret = abx500_mask_and_set_register_interruptible(
  490. gpadc->dev,
  491. AB8500_GPADC,
  492. AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
  493. break;
  494. }
  495. if (ret < 0) {
  496. dev_err(gpadc->dev,
  497. "gpadc_conversion: select falling edge failed\n");
  498. goto out;
  499. }
  500. /* Set trigger delay timer */
  501. if (conv_type == ADC_HW) {
  502. ret = abx500_set_register_interruptible(gpadc->dev,
  503. AB8500_GPADC, AB8500_GPADC_AUTO_TIMER_REG, trig_timer);
  504. if (ret < 0) {
  505. dev_err(gpadc->dev,
  506. "gpadc_conversion: trig timer failed\n");
  507. goto out;
  508. }
  509. }
  510. /* Start SW conversion */
  511. if (conv_type == ADC_SW) {
  512. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  513. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  514. ADC_SW_CONV, ADC_SW_CONV);
  515. if (ret < 0) {
  516. dev_err(gpadc->dev,
  517. "gpadc_conversion: start s/w conv failed\n");
  518. goto out;
  519. }
  520. }
  521. /* wait for completion of conversion */
  522. if (conv_type == ADC_HW) {
  523. if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete,
  524. 2 * HZ)) {
  525. dev_err(gpadc->dev,
  526. "timeout didn't receive hw GPADC conv interrupt\n");
  527. ret = -EINVAL;
  528. goto out;
  529. }
  530. } else {
  531. if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete,
  532. msecs_to_jiffies(CONVERSION_TIME))) {
  533. dev_err(gpadc->dev,
  534. "timeout didn't receive sw GPADC conv interrupt\n");
  535. ret = -EINVAL;
  536. goto out;
  537. }
  538. }
  539. /* Read the converted RAW data */
  540. if (conv_type == ADC_HW) {
  541. ret = abx500_get_register_interruptible(gpadc->dev,
  542. AB8500_GPADC, AB8500_GPADC_AUTODATAL_REG, &low_data);
  543. if (ret < 0) {
  544. dev_err(gpadc->dev,
  545. "gpadc_conversion: read hw low data failed\n");
  546. goto out;
  547. }
  548. ret = abx500_get_register_interruptible(gpadc->dev,
  549. AB8500_GPADC, AB8500_GPADC_AUTODATAH_REG, &high_data);
  550. if (ret < 0) {
  551. dev_err(gpadc->dev,
  552. "gpadc_conversion: read hw high data failed\n");
  553. goto out;
  554. }
  555. } else {
  556. ret = abx500_get_register_interruptible(gpadc->dev,
  557. AB8500_GPADC, AB8500_GPADC_MANDATAL_REG, &low_data);
  558. if (ret < 0) {
  559. dev_err(gpadc->dev,
  560. "gpadc_conversion: read sw low data failed\n");
  561. goto out;
  562. }
  563. ret = abx500_get_register_interruptible(gpadc->dev,
  564. AB8500_GPADC, AB8500_GPADC_MANDATAH_REG, &high_data);
  565. if (ret < 0) {
  566. dev_err(gpadc->dev,
  567. "gpadc_conversion: read sw high data failed\n");
  568. goto out;
  569. }
  570. }
  571. /* Check if double convertion is required */
  572. if ((channel == BAT_CTRL_AND_IBAT) ||
  573. (channel == VBAT_MEAS_AND_IBAT) ||
  574. (channel == VBAT_TRUE_MEAS_AND_IBAT) ||
  575. (channel == BAT_TEMP_AND_IBAT)) {
  576. if (conv_type == ADC_HW) {
  577. /* not supported */
  578. ret = -ENOTSUPP;
  579. dev_err(gpadc->dev,
  580. "gpadc_conversion: only SW double conversion supported\n");
  581. goto out;
  582. } else {
  583. /* Read the converted RAW data 2 */
  584. ret = abx500_get_register_interruptible(gpadc->dev,
  585. AB8500_GPADC, AB8540_GPADC_MANDATA2L_REG,
  586. &low_data2);
  587. if (ret < 0) {
  588. dev_err(gpadc->dev,
  589. "gpadc_conversion: read sw low data 2 failed\n");
  590. goto out;
  591. }
  592. ret = abx500_get_register_interruptible(gpadc->dev,
  593. AB8500_GPADC, AB8540_GPADC_MANDATA2H_REG,
  594. &high_data2);
  595. if (ret < 0) {
  596. dev_err(gpadc->dev,
  597. "gpadc_conversion: read sw high data 2 failed\n");
  598. goto out;
  599. }
  600. if (ibat != NULL) {
  601. *ibat = (high_data2 << 8) | low_data2;
  602. } else {
  603. dev_warn(gpadc->dev,
  604. "gpadc_conversion: ibat not stored\n");
  605. }
  606. }
  607. }
  608. /* Disable GPADC */
  609. ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  610. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  611. if (ret < 0) {
  612. dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
  613. goto out;
  614. }
  615. /* Disable VTVout LDO this is required for GPADC */
  616. pm_runtime_mark_last_busy(gpadc->dev);
  617. pm_runtime_put_autosuspend(gpadc->dev);
  618. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  619. return (high_data << 8) | low_data;
  620. out:
  621. /*
  622. * It has shown to be needed to turn off the GPADC if an error occurs,
  623. * otherwise we might have problem when waiting for the busy bit in the
  624. * GPADC status register to go low. In V1.1 there wait_for_completion
  625. * seems to timeout when waiting for an interrupt.. Not seen in V2.0
  626. */
  627. (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  628. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  629. pm_runtime_put(gpadc->dev);
  630. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  631. dev_err(gpadc->dev,
  632. "gpadc_conversion: Failed to AD convert channel %d\n", channel);
  633. return ret;
  634. }
  635. EXPORT_SYMBOL(ab8500_gpadc_read_raw);
  636. /**
  637. * ab8500_bm_gpadcconvend_handler() - isr for gpadc conversion completion
  638. * @irq: irq number
  639. * @data: pointer to the data passed during request irq
  640. *
  641. * This is a interrupt service routine for gpadc conversion completion.
  642. * Notifies the gpadc completion is completed and the converted raw value
  643. * can be read from the registers.
  644. * Returns IRQ status(IRQ_HANDLED)
  645. */
  646. static irqreturn_t ab8500_bm_gpadcconvend_handler(int irq, void *_gpadc)
  647. {
  648. struct ab8500_gpadc *gpadc = _gpadc;
  649. complete(&gpadc->ab8500_gpadc_complete);
  650. return IRQ_HANDLED;
  651. }
  652. static int otp_cal_regs[] = {
  653. AB8500_GPADC_CAL_1,
  654. AB8500_GPADC_CAL_2,
  655. AB8500_GPADC_CAL_3,
  656. AB8500_GPADC_CAL_4,
  657. AB8500_GPADC_CAL_5,
  658. AB8500_GPADC_CAL_6,
  659. AB8500_GPADC_CAL_7,
  660. };
  661. static int otp4_cal_regs[] = {
  662. AB8540_GPADC_OTP4_REG_7,
  663. AB8540_GPADC_OTP4_REG_6,
  664. AB8540_GPADC_OTP4_REG_5,
  665. };
  666. static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
  667. {
  668. int i;
  669. int ret[ARRAY_SIZE(otp_cal_regs)];
  670. u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
  671. int ret_otp4[ARRAY_SIZE(otp4_cal_regs)];
  672. u8 gpadc_otp4[ARRAY_SIZE(otp4_cal_regs)];
  673. int vmain_high, vmain_low;
  674. int btemp_high, btemp_low;
  675. int vbat_high, vbat_low;
  676. int ibat_high, ibat_low;
  677. s64 V_gain, V_offset, V2A_gain, V2A_offset;
  678. struct ab8500 *ab8500;
  679. ab8500 = gpadc->parent;
  680. /* First we read all OTP registers and store the error code */
  681. for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
  682. ret[i] = abx500_get_register_interruptible(gpadc->dev,
  683. AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]);
  684. if (ret[i] < 0)
  685. dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
  686. __func__, otp_cal_regs[i]);
  687. }
  688. /*
  689. * The ADC calibration data is stored in OTP registers.
  690. * The layout of the calibration data is outlined below and a more
  691. * detailed description can be found in UM0836
  692. *
  693. * vm_h/l = vmain_high/low
  694. * bt_h/l = btemp_high/low
  695. * vb_h/l = vbat_high/low
  696. *
  697. * Data bits 8500/9540:
  698. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  699. * |.......|.......|.......|.......|.......|.......|.......|.......
  700. * | | vm_h9 | vm_h8
  701. * |.......|.......|.......|.......|.......|.......|.......|.......
  702. * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
  703. * |.......|.......|.......|.......|.......|.......|.......|.......
  704. * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
  705. * |.......|.......|.......|.......|.......|.......|.......|.......
  706. * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
  707. * |.......|.......|.......|.......|.......|.......|.......|.......
  708. * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
  709. * |.......|.......|.......|.......|.......|.......|.......|.......
  710. * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
  711. * |.......|.......|.......|.......|.......|.......|.......|.......
  712. * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
  713. * |.......|.......|.......|.......|.......|.......|.......|.......
  714. *
  715. * Data bits 8540:
  716. * OTP2
  717. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  718. * |.......|.......|.......|.......|.......|.......|.......|.......
  719. * |
  720. * |.......|.......|.......|.......|.......|.......|.......|.......
  721. * | vm_h9 | vm_h8 | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
  722. * |.......|.......|.......|.......|.......|.......|.......|.......
  723. * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
  724. * |.......|.......|.......|.......|.......|.......|.......|.......
  725. * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
  726. * |.......|.......|.......|.......|.......|.......|.......|.......
  727. * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
  728. * |.......|.......|.......|.......|.......|.......|.......|.......
  729. * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
  730. * |.......|.......|.......|.......|.......|.......|.......|.......
  731. * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
  732. * |.......|.......|.......|.......|.......|.......|.......|.......
  733. *
  734. * Data bits 8540:
  735. * OTP4
  736. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  737. * |.......|.......|.......|.......|.......|.......|.......|.......
  738. * | | ib_h9 | ib_h8 | ib_h7
  739. * |.......|.......|.......|.......|.......|.......|.......|.......
  740. * | ib_h6 | ib_h5 | ib_h4 | ib_h3 | ib_h2 | ib_h1 | ib_h0 | ib_l5
  741. * |.......|.......|.......|.......|.......|.......|.......|.......
  742. * | ib_l4 | ib_l3 | ib_l2 | ib_l1 | ib_l0 |
  743. *
  744. *
  745. * Ideal output ADC codes corresponding to injected input voltages
  746. * during manufacturing is:
  747. *
  748. * vmain_high: Vin = 19500mV / ADC ideal code = 997
  749. * vmain_low: Vin = 315mV / ADC ideal code = 16
  750. * btemp_high: Vin = 1300mV / ADC ideal code = 985
  751. * btemp_low: Vin = 21mV / ADC ideal code = 16
  752. * vbat_high: Vin = 4700mV / ADC ideal code = 982
  753. * vbat_low: Vin = 2380mV / ADC ideal code = 33
  754. */
  755. if (is_ab8540(ab8500)) {
  756. /* Calculate gain and offset for VMAIN if all reads succeeded*/
  757. if (!(ret[1] < 0 || ret[2] < 0)) {
  758. vmain_high = (((gpadc_cal[1] & 0xFF) << 2) |
  759. ((gpadc_cal[2] & 0xC0) >> 6));
  760. vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
  761. gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
  762. (19500 - 315) / (vmain_high - vmain_low);
  763. gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE *
  764. 19500 - (CALIB_SCALE * (19500 - 315) /
  765. (vmain_high - vmain_low)) * vmain_high;
  766. } else {
  767. gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
  768. }
  769. /* Read IBAT calibration Data */
  770. for (i = 0; i < ARRAY_SIZE(otp4_cal_regs); i++) {
  771. ret_otp4[i] = abx500_get_register_interruptible(
  772. gpadc->dev, AB8500_OTP_EMUL,
  773. otp4_cal_regs[i], &gpadc_otp4[i]);
  774. if (ret_otp4[i] < 0)
  775. dev_err(gpadc->dev,
  776. "%s: read otp4 reg 0x%02x failed\n",
  777. __func__, otp4_cal_regs[i]);
  778. }
  779. /* Calculate gain and offset for IBAT if all reads succeeded */
  780. if (!(ret_otp4[0] < 0 || ret_otp4[1] < 0 || ret_otp4[2] < 0)) {
  781. ibat_high = (((gpadc_otp4[0] & 0x07) << 7) |
  782. ((gpadc_otp4[1] & 0xFE) >> 1));
  783. ibat_low = (((gpadc_otp4[1] & 0x01) << 5) |
  784. ((gpadc_otp4[2] & 0xF8) >> 3));
  785. V_gain = ((IBAT_VDROP_H - IBAT_VDROP_L)
  786. << CALIB_SHIFT_IBAT) / (ibat_high - ibat_low);
  787. V_offset = (IBAT_VDROP_H << CALIB_SHIFT_IBAT) -
  788. (((IBAT_VDROP_H - IBAT_VDROP_L) <<
  789. CALIB_SHIFT_IBAT) / (ibat_high - ibat_low))
  790. * ibat_high;
  791. /*
  792. * Result obtained is in mV (at a scale factor),
  793. * we need to calculate gain and offset to get mA
  794. */
  795. V2A_gain = (ADC_CH_IBAT_MAX - ADC_CH_IBAT_MIN)/
  796. (ADC_CH_IBAT_MAX_V - ADC_CH_IBAT_MIN_V);
  797. V2A_offset = ((ADC_CH_IBAT_MAX_V * ADC_CH_IBAT_MIN -
  798. ADC_CH_IBAT_MAX * ADC_CH_IBAT_MIN_V)
  799. << CALIB_SHIFT_IBAT)
  800. / (ADC_CH_IBAT_MAX_V - ADC_CH_IBAT_MIN_V);
  801. gpadc->cal_data[ADC_INPUT_IBAT].gain = V_gain * V2A_gain;
  802. gpadc->cal_data[ADC_INPUT_IBAT].offset = V_offset *
  803. V2A_gain + V2A_offset;
  804. } else {
  805. gpadc->cal_data[ADC_INPUT_IBAT].gain = 0;
  806. }
  807. dev_dbg(gpadc->dev, "IBAT gain %llu offset %llu\n",
  808. gpadc->cal_data[ADC_INPUT_IBAT].gain,
  809. gpadc->cal_data[ADC_INPUT_IBAT].offset);
  810. } else {
  811. /* Calculate gain and offset for VMAIN if all reads succeeded */
  812. if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
  813. vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
  814. ((gpadc_cal[1] & 0x3F) << 2) |
  815. ((gpadc_cal[2] & 0xC0) >> 6));
  816. vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
  817. gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
  818. (19500 - 315) / (vmain_high - vmain_low);
  819. gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE *
  820. 19500 - (CALIB_SCALE * (19500 - 315) /
  821. (vmain_high - vmain_low)) * vmain_high;
  822. } else {
  823. gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
  824. }
  825. }
  826. /* Calculate gain and offset for BTEMP if all reads succeeded */
  827. if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
  828. btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
  829. (gpadc_cal[3] << 1) | ((gpadc_cal[4] & 0x80) >> 7));
  830. btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
  831. gpadc->cal_data[ADC_INPUT_BTEMP].gain =
  832. CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
  833. gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 -
  834. (CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low))
  835. * btemp_high;
  836. } else {
  837. gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0;
  838. }
  839. /* Calculate gain and offset for VBAT if all reads succeeded */
  840. if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
  841. vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
  842. vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
  843. gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE *
  844. (4700 - 2380) / (vbat_high - vbat_low);
  845. gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 -
  846. (CALIB_SCALE * (4700 - 2380) /
  847. (vbat_high - vbat_low)) * vbat_high;
  848. } else {
  849. gpadc->cal_data[ADC_INPUT_VBAT].gain = 0;
  850. }
  851. dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n",
  852. gpadc->cal_data[ADC_INPUT_VMAIN].gain,
  853. gpadc->cal_data[ADC_INPUT_VMAIN].offset);
  854. dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n",
  855. gpadc->cal_data[ADC_INPUT_BTEMP].gain,
  856. gpadc->cal_data[ADC_INPUT_BTEMP].offset);
  857. dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n",
  858. gpadc->cal_data[ADC_INPUT_VBAT].gain,
  859. gpadc->cal_data[ADC_INPUT_VBAT].offset);
  860. }
  861. static int ab8500_gpadc_runtime_suspend(struct device *dev)
  862. {
  863. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  864. regulator_disable(gpadc->regu);
  865. return 0;
  866. }
  867. static int ab8500_gpadc_runtime_resume(struct device *dev)
  868. {
  869. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  870. regulator_enable(gpadc->regu);
  871. return 0;
  872. }
  873. static int ab8500_gpadc_runtime_idle(struct device *dev)
  874. {
  875. pm_runtime_suspend(dev);
  876. return 0;
  877. }
  878. static int ab8500_gpadc_suspend(struct device *dev)
  879. {
  880. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  881. mutex_lock(&gpadc->ab8500_gpadc_lock);
  882. pm_runtime_get_sync(dev);
  883. regulator_disable(gpadc->regu);
  884. return 0;
  885. }
  886. static int ab8500_gpadc_resume(struct device *dev)
  887. {
  888. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  889. regulator_enable(gpadc->regu);
  890. pm_runtime_mark_last_busy(gpadc->dev);
  891. pm_runtime_put_autosuspend(gpadc->dev);
  892. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  893. return 0;
  894. }
  895. static int ab8500_gpadc_probe(struct platform_device *pdev)
  896. {
  897. int ret = 0;
  898. struct ab8500_gpadc *gpadc;
  899. gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
  900. if (!gpadc) {
  901. dev_err(&pdev->dev, "Error: No memory\n");
  902. return -ENOMEM;
  903. }
  904. gpadc->irq_sw = platform_get_irq_byname(pdev, "SW_CONV_END");
  905. if (gpadc->irq_sw < 0)
  906. dev_err(gpadc->dev, "failed to get platform sw_conv_end irq\n");
  907. gpadc->irq_hw = platform_get_irq_byname(pdev, "HW_CONV_END");
  908. if (gpadc->irq_hw < 0)
  909. dev_err(gpadc->dev, "failed to get platform hw_conv_end irq\n");
  910. gpadc->dev = &pdev->dev;
  911. gpadc->parent = dev_get_drvdata(pdev->dev.parent);
  912. mutex_init(&gpadc->ab8500_gpadc_lock);
  913. /* Initialize completion used to notify completion of conversion */
  914. init_completion(&gpadc->ab8500_gpadc_complete);
  915. /* Register interrupts */
  916. if (gpadc->irq_sw >= 0) {
  917. ret = request_threaded_irq(gpadc->irq_sw, NULL,
  918. ab8500_bm_gpadcconvend_handler,
  919. IRQF_NO_SUSPEND | IRQF_SHARED, "ab8500-gpadc-sw",
  920. gpadc);
  921. if (ret < 0) {
  922. dev_err(gpadc->dev,
  923. "Failed to register interrupt irq: %d\n",
  924. gpadc->irq_sw);
  925. goto fail;
  926. }
  927. }
  928. if (gpadc->irq_hw >= 0) {
  929. ret = request_threaded_irq(gpadc->irq_hw, NULL,
  930. ab8500_bm_gpadcconvend_handler,
  931. IRQF_NO_SUSPEND | IRQF_SHARED, "ab8500-gpadc-hw",
  932. gpadc);
  933. if (ret < 0) {
  934. dev_err(gpadc->dev,
  935. "Failed to register interrupt irq: %d\n",
  936. gpadc->irq_hw);
  937. goto fail_irq;
  938. }
  939. }
  940. /* VTVout LDO used to power up ab8500-GPADC */
  941. gpadc->regu = regulator_get(&pdev->dev, "vddadc");
  942. if (IS_ERR(gpadc->regu)) {
  943. ret = PTR_ERR(gpadc->regu);
  944. dev_err(gpadc->dev, "failed to get vtvout LDO\n");
  945. goto fail_irq;
  946. }
  947. platform_set_drvdata(pdev, gpadc);
  948. regulator_enable(gpadc->regu);
  949. pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY);
  950. pm_runtime_use_autosuspend(gpadc->dev);
  951. pm_runtime_set_active(gpadc->dev);
  952. pm_runtime_enable(gpadc->dev);
  953. ab8500_gpadc_read_calibration_data(gpadc);
  954. list_add_tail(&gpadc->node, &ab8500_gpadc_list);
  955. dev_dbg(gpadc->dev, "probe success\n");
  956. return 0;
  957. fail_irq:
  958. free_irq(gpadc->irq_sw, gpadc);
  959. free_irq(gpadc->irq_hw, gpadc);
  960. fail:
  961. kfree(gpadc);
  962. gpadc = NULL;
  963. return ret;
  964. }
  965. static int ab8500_gpadc_remove(struct platform_device *pdev)
  966. {
  967. struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
  968. /* remove this gpadc entry from the list */
  969. list_del(&gpadc->node);
  970. /* remove interrupt - completion of Sw ADC conversion */
  971. if (gpadc->irq_sw >= 0)
  972. free_irq(gpadc->irq_sw, gpadc);
  973. if (gpadc->irq_hw >= 0)
  974. free_irq(gpadc->irq_hw, gpadc);
  975. pm_runtime_get_sync(gpadc->dev);
  976. pm_runtime_disable(gpadc->dev);
  977. regulator_disable(gpadc->regu);
  978. pm_runtime_set_suspended(gpadc->dev);
  979. pm_runtime_put_noidle(gpadc->dev);
  980. kfree(gpadc);
  981. gpadc = NULL;
  982. return 0;
  983. }
  984. static const struct dev_pm_ops ab8500_gpadc_pm_ops = {
  985. SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend,
  986. ab8500_gpadc_runtime_resume,
  987. ab8500_gpadc_runtime_idle)
  988. SET_SYSTEM_SLEEP_PM_OPS(ab8500_gpadc_suspend,
  989. ab8500_gpadc_resume)
  990. };
  991. static struct platform_driver ab8500_gpadc_driver = {
  992. .probe = ab8500_gpadc_probe,
  993. .remove = ab8500_gpadc_remove,
  994. .driver = {
  995. .name = "ab8500-gpadc",
  996. .owner = THIS_MODULE,
  997. .pm = &ab8500_gpadc_pm_ops,
  998. },
  999. };
  1000. static int __init ab8500_gpadc_init(void)
  1001. {
  1002. return platform_driver_register(&ab8500_gpadc_driver);
  1003. }
  1004. static void __exit ab8500_gpadc_exit(void)
  1005. {
  1006. platform_driver_unregister(&ab8500_gpadc_driver);
  1007. }
  1008. subsys_initcall_sync(ab8500_gpadc_init);
  1009. module_exit(ab8500_gpadc_exit);
  1010. MODULE_LICENSE("GPL v2");
  1011. MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson,"
  1012. "M'boumba Cedric Madianga");
  1013. MODULE_ALIAS("platform:ab8500_gpadc");
  1014. MODULE_DESCRIPTION("AB8500 GPADC driver");