ab8500-gpadc.c 33 KB

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