ab8500-gpadc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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_sw_hw_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. unsigned long completion_timeout;
  331. u8 val, low_data, high_data, low_data2, high_data2;
  332. u8 val_reg1 = 0;
  333. unsigned int delay_min = 0;
  334. unsigned int delay_max = 0;
  335. u8 data_low_addr, data_high_addr;
  336. if (!gpadc)
  337. return -ENODEV;
  338. /* check if convertion is supported */
  339. if ((gpadc->irq_sw < 0) && (conv_type == ADC_SW))
  340. return -ENOTSUPP;
  341. if ((gpadc->irq_hw < 0) && (conv_type == ADC_HW))
  342. return -ENOTSUPP;
  343. mutex_lock(&gpadc->ab8500_gpadc_lock);
  344. /* Enable VTVout LDO this is required for GPADC */
  345. pm_runtime_get_sync(gpadc->dev);
  346. /* Check if ADC is not busy, lock and proceed */
  347. do {
  348. ret = abx500_get_register_interruptible(gpadc->dev,
  349. AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
  350. if (ret < 0)
  351. goto out;
  352. if (!(val & GPADC_BUSY))
  353. break;
  354. msleep(10);
  355. } while (++looplimit < 10);
  356. if (looplimit >= 10 && (val & GPADC_BUSY)) {
  357. dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
  358. ret = -EINVAL;
  359. goto out;
  360. }
  361. /* Enable GPADC */
  362. val_reg1 |= EN_GPADC;
  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. val_reg1 |= EN_TRIG_EDGE;
  382. if (trig_edge)
  383. val_reg1 |= EN_FALLING;
  384. }
  385. else
  386. ret = abx500_set_register_interruptible(gpadc->dev,
  387. AB8500_GPADC, AB8500_GPADC_CTRL2_REG, val);
  388. if (ret < 0) {
  389. dev_err(gpadc->dev,
  390. "gpadc_conversion: set avg samples failed\n");
  391. goto out;
  392. }
  393. /*
  394. * Enable ADC, buffering, select rising edge and enable ADC path
  395. * charging current sense if it needed, ABB 3.0 needs some special
  396. * treatment too.
  397. */
  398. switch (channel) {
  399. case MAIN_CHARGER_C:
  400. case USB_CHARGER_C:
  401. val_reg1 |= EN_BUF | EN_ICHAR;
  402. break;
  403. case BTEMP_BALL:
  404. if (!is_ab8500_2p0_or_earlier(gpadc->parent)) {
  405. val_reg1 |= EN_BUF | BTEMP_PULL_UP;
  406. /*
  407. * Delay might be needed for ABB8500 cut 3.0, if not,
  408. * remove when hardware will be availible
  409. */
  410. delay_min = 1000; /* Delay in micro seconds */
  411. delay_max = 10000; /* large range to optimise sleep mode */
  412. break;
  413. }
  414. /* Intentional fallthrough */
  415. default:
  416. val_reg1 |= EN_BUF;
  417. break;
  418. }
  419. /* Write configuration to register */
  420. ret = abx500_set_register_interruptible(gpadc->dev,
  421. AB8500_GPADC, AB8500_GPADC_CTRL1_REG, val_reg1);
  422. if (ret < 0) {
  423. dev_err(gpadc->dev,
  424. "gpadc_conversion: set Control register failed\n");
  425. goto out;
  426. }
  427. if (delay_min != 0)
  428. usleep_range(delay_min, delay_max);
  429. if (conv_type == ADC_HW) {
  430. /* Set trigger delay timer */
  431. ret = abx500_set_register_interruptible(gpadc->dev,
  432. AB8500_GPADC, AB8500_GPADC_AUTO_TIMER_REG, trig_timer);
  433. if (ret < 0) {
  434. dev_err(gpadc->dev,
  435. "gpadc_conversion: trig timer failed\n");
  436. goto out;
  437. }
  438. completion_timeout = 2 * HZ;
  439. data_low_addr = AB8500_GPADC_AUTODATAL_REG;
  440. data_high_addr = AB8500_GPADC_AUTODATAH_REG;
  441. } else {
  442. /* Start SW conversion */
  443. ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
  444. AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
  445. ADC_SW_CONV, ADC_SW_CONV);
  446. if (ret < 0) {
  447. dev_err(gpadc->dev,
  448. "gpadc_conversion: start s/w conv failed\n");
  449. goto out;
  450. }
  451. completion_timeout = msecs_to_jiffies(CONVERSION_TIME);
  452. data_low_addr = AB8500_GPADC_MANDATAL_REG;
  453. data_high_addr = AB8500_GPADC_MANDATAH_REG;
  454. }
  455. /* wait for completion of conversion */
  456. if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete,
  457. completion_timeout)) {
  458. dev_err(gpadc->dev,
  459. "timeout didn't receive GPADC conv interrupt\n");
  460. ret = -EINVAL;
  461. goto out;
  462. }
  463. /* Read the converted RAW data */
  464. ret = abx500_get_register_interruptible(gpadc->dev,
  465. AB8500_GPADC, data_low_addr, &low_data);
  466. if (ret < 0) {
  467. dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n");
  468. goto out;
  469. }
  470. ret = abx500_get_register_interruptible(gpadc->dev,
  471. AB8500_GPADC, data_high_addr, &high_data);
  472. if (ret < 0) {
  473. dev_err(gpadc->dev, "gpadc_conversion: read high data failed\n");
  474. goto out;
  475. }
  476. /* Check if double convertion is required */
  477. if ((channel == BAT_CTRL_AND_IBAT) ||
  478. (channel == VBAT_MEAS_AND_IBAT) ||
  479. (channel == VBAT_TRUE_MEAS_AND_IBAT) ||
  480. (channel == BAT_TEMP_AND_IBAT)) {
  481. if (conv_type == ADC_HW) {
  482. /* not supported */
  483. ret = -ENOTSUPP;
  484. dev_err(gpadc->dev,
  485. "gpadc_conversion: only SW double conversion supported\n");
  486. goto out;
  487. } else {
  488. /* Read the converted RAW data 2 */
  489. ret = abx500_get_register_interruptible(gpadc->dev,
  490. AB8500_GPADC, AB8540_GPADC_MANDATA2L_REG,
  491. &low_data2);
  492. if (ret < 0) {
  493. dev_err(gpadc->dev,
  494. "gpadc_conversion: read sw low data 2 failed\n");
  495. goto out;
  496. }
  497. ret = abx500_get_register_interruptible(gpadc->dev,
  498. AB8500_GPADC, AB8540_GPADC_MANDATA2H_REG,
  499. &high_data2);
  500. if (ret < 0) {
  501. dev_err(gpadc->dev,
  502. "gpadc_conversion: read sw high data 2 failed\n");
  503. goto out;
  504. }
  505. if (ibat != NULL) {
  506. *ibat = (high_data2 << 8) | low_data2;
  507. } else {
  508. dev_warn(gpadc->dev,
  509. "gpadc_conversion: ibat not stored\n");
  510. }
  511. }
  512. }
  513. /* Disable GPADC */
  514. ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  515. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  516. if (ret < 0) {
  517. dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
  518. goto out;
  519. }
  520. /* Disable VTVout LDO this is required for GPADC */
  521. pm_runtime_mark_last_busy(gpadc->dev);
  522. pm_runtime_put_autosuspend(gpadc->dev);
  523. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  524. return (high_data << 8) | low_data;
  525. out:
  526. /*
  527. * It has shown to be needed to turn off the GPADC if an error occurs,
  528. * otherwise we might have problem when waiting for the busy bit in the
  529. * GPADC status register to go low. In V1.1 there wait_for_completion
  530. * seems to timeout when waiting for an interrupt.. Not seen in V2.0
  531. */
  532. (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
  533. AB8500_GPADC_CTRL1_REG, DIS_GPADC);
  534. pm_runtime_put(gpadc->dev);
  535. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  536. dev_err(gpadc->dev,
  537. "gpadc_conversion: Failed to AD convert channel %d\n", channel);
  538. return ret;
  539. }
  540. EXPORT_SYMBOL(ab8500_gpadc_read_raw);
  541. /**
  542. * ab8500_bm_gpadcconvend_handler() - isr for gpadc conversion completion
  543. * @irq: irq number
  544. * @data: pointer to the data passed during request irq
  545. *
  546. * This is a interrupt service routine for gpadc conversion completion.
  547. * Notifies the gpadc completion is completed and the converted raw value
  548. * can be read from the registers.
  549. * Returns IRQ status(IRQ_HANDLED)
  550. */
  551. static irqreturn_t ab8500_bm_gpadcconvend_handler(int irq, void *_gpadc)
  552. {
  553. struct ab8500_gpadc *gpadc = _gpadc;
  554. complete(&gpadc->ab8500_gpadc_complete);
  555. return IRQ_HANDLED;
  556. }
  557. static int otp_cal_regs[] = {
  558. AB8500_GPADC_CAL_1,
  559. AB8500_GPADC_CAL_2,
  560. AB8500_GPADC_CAL_3,
  561. AB8500_GPADC_CAL_4,
  562. AB8500_GPADC_CAL_5,
  563. AB8500_GPADC_CAL_6,
  564. AB8500_GPADC_CAL_7,
  565. };
  566. static int otp4_cal_regs[] = {
  567. AB8540_GPADC_OTP4_REG_7,
  568. AB8540_GPADC_OTP4_REG_6,
  569. AB8540_GPADC_OTP4_REG_5,
  570. };
  571. static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
  572. {
  573. int i;
  574. int ret[ARRAY_SIZE(otp_cal_regs)];
  575. u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
  576. int ret_otp4[ARRAY_SIZE(otp4_cal_regs)];
  577. u8 gpadc_otp4[ARRAY_SIZE(otp4_cal_regs)];
  578. int vmain_high, vmain_low;
  579. int btemp_high, btemp_low;
  580. int vbat_high, vbat_low;
  581. int ibat_high, ibat_low;
  582. s64 V_gain, V_offset, V2A_gain, V2A_offset;
  583. struct ab8500 *ab8500;
  584. ab8500 = gpadc->parent;
  585. /* First we read all OTP registers and store the error code */
  586. for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
  587. ret[i] = abx500_get_register_interruptible(gpadc->dev,
  588. AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]);
  589. if (ret[i] < 0)
  590. dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
  591. __func__, otp_cal_regs[i]);
  592. }
  593. /*
  594. * The ADC calibration data is stored in OTP registers.
  595. * The layout of the calibration data is outlined below and a more
  596. * detailed description can be found in UM0836
  597. *
  598. * vm_h/l = vmain_high/low
  599. * bt_h/l = btemp_high/low
  600. * vb_h/l = vbat_high/low
  601. *
  602. * Data bits 8500/9540:
  603. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  604. * |.......|.......|.......|.......|.......|.......|.......|.......
  605. * | | vm_h9 | vm_h8
  606. * |.......|.......|.......|.......|.......|.......|.......|.......
  607. * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
  608. * |.......|.......|.......|.......|.......|.......|.......|.......
  609. * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
  610. * |.......|.......|.......|.......|.......|.......|.......|.......
  611. * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
  612. * |.......|.......|.......|.......|.......|.......|.......|.......
  613. * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
  614. * |.......|.......|.......|.......|.......|.......|.......|.......
  615. * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
  616. * |.......|.......|.......|.......|.......|.......|.......|.......
  617. * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
  618. * |.......|.......|.......|.......|.......|.......|.......|.......
  619. *
  620. * Data bits 8540:
  621. * OTP2
  622. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  623. * |.......|.......|.......|.......|.......|.......|.......|.......
  624. * |
  625. * |.......|.......|.......|.......|.......|.......|.......|.......
  626. * | vm_h9 | vm_h8 | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
  627. * |.......|.......|.......|.......|.......|.......|.......|.......
  628. * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
  629. * |.......|.......|.......|.......|.......|.......|.......|.......
  630. * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
  631. * |.......|.......|.......|.......|.......|.......|.......|.......
  632. * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
  633. * |.......|.......|.......|.......|.......|.......|.......|.......
  634. * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
  635. * |.......|.......|.......|.......|.......|.......|.......|.......
  636. * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
  637. * |.......|.......|.......|.......|.......|.......|.......|.......
  638. *
  639. * Data bits 8540:
  640. * OTP4
  641. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
  642. * |.......|.......|.......|.......|.......|.......|.......|.......
  643. * | | ib_h9 | ib_h8 | ib_h7
  644. * |.......|.......|.......|.......|.......|.......|.......|.......
  645. * | ib_h6 | ib_h5 | ib_h4 | ib_h3 | ib_h2 | ib_h1 | ib_h0 | ib_l5
  646. * |.......|.......|.......|.......|.......|.......|.......|.......
  647. * | ib_l4 | ib_l3 | ib_l2 | ib_l1 | ib_l0 |
  648. *
  649. *
  650. * Ideal output ADC codes corresponding to injected input voltages
  651. * during manufacturing is:
  652. *
  653. * vmain_high: Vin = 19500mV / ADC ideal code = 997
  654. * vmain_low: Vin = 315mV / ADC ideal code = 16
  655. * btemp_high: Vin = 1300mV / ADC ideal code = 985
  656. * btemp_low: Vin = 21mV / ADC ideal code = 16
  657. * vbat_high: Vin = 4700mV / ADC ideal code = 982
  658. * vbat_low: Vin = 2380mV / ADC ideal code = 33
  659. */
  660. if (is_ab8540(ab8500)) {
  661. /* Calculate gain and offset for VMAIN if all reads succeeded*/
  662. if (!(ret[1] < 0 || ret[2] < 0)) {
  663. vmain_high = (((gpadc_cal[1] & 0xFF) << 2) |
  664. ((gpadc_cal[2] & 0xC0) >> 6));
  665. vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
  666. gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_hi =
  667. (u16)vmain_high;
  668. gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_lo =
  669. (u16)vmain_low;
  670. gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
  671. (19500 - 315) / (vmain_high - vmain_low);
  672. gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE *
  673. 19500 - (CALIB_SCALE * (19500 - 315) /
  674. (vmain_high - vmain_low)) * vmain_high;
  675. } else {
  676. gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
  677. }
  678. /* Read IBAT calibration Data */
  679. for (i = 0; i < ARRAY_SIZE(otp4_cal_regs); i++) {
  680. ret_otp4[i] = abx500_get_register_interruptible(
  681. gpadc->dev, AB8500_OTP_EMUL,
  682. otp4_cal_regs[i], &gpadc_otp4[i]);
  683. if (ret_otp4[i] < 0)
  684. dev_err(gpadc->dev,
  685. "%s: read otp4 reg 0x%02x failed\n",
  686. __func__, otp4_cal_regs[i]);
  687. }
  688. /* Calculate gain and offset for IBAT if all reads succeeded */
  689. if (!(ret_otp4[0] < 0 || ret_otp4[1] < 0 || ret_otp4[2] < 0)) {
  690. ibat_high = (((gpadc_otp4[0] & 0x07) << 7) |
  691. ((gpadc_otp4[1] & 0xFE) >> 1));
  692. ibat_low = (((gpadc_otp4[1] & 0x01) << 5) |
  693. ((gpadc_otp4[2] & 0xF8) >> 3));
  694. gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_hi =
  695. (u16)ibat_high;
  696. gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_lo =
  697. (u16)ibat_low;
  698. V_gain = ((IBAT_VDROP_H - IBAT_VDROP_L)
  699. << CALIB_SHIFT_IBAT) / (ibat_high - ibat_low);
  700. V_offset = (IBAT_VDROP_H << CALIB_SHIFT_IBAT) -
  701. (((IBAT_VDROP_H - IBAT_VDROP_L) <<
  702. CALIB_SHIFT_IBAT) / (ibat_high - ibat_low))
  703. * ibat_high;
  704. /*
  705. * Result obtained is in mV (at a scale factor),
  706. * we need to calculate gain and offset to get mA
  707. */
  708. V2A_gain = (ADC_CH_IBAT_MAX - ADC_CH_IBAT_MIN)/
  709. (ADC_CH_IBAT_MAX_V - ADC_CH_IBAT_MIN_V);
  710. V2A_offset = ((ADC_CH_IBAT_MAX_V * ADC_CH_IBAT_MIN -
  711. ADC_CH_IBAT_MAX * ADC_CH_IBAT_MIN_V)
  712. << CALIB_SHIFT_IBAT)
  713. / (ADC_CH_IBAT_MAX_V - ADC_CH_IBAT_MIN_V);
  714. gpadc->cal_data[ADC_INPUT_IBAT].gain = V_gain * V2A_gain;
  715. gpadc->cal_data[ADC_INPUT_IBAT].offset = V_offset *
  716. V2A_gain + V2A_offset;
  717. } else {
  718. gpadc->cal_data[ADC_INPUT_IBAT].gain = 0;
  719. }
  720. dev_dbg(gpadc->dev, "IBAT gain %llu offset %llu\n",
  721. gpadc->cal_data[ADC_INPUT_IBAT].gain,
  722. gpadc->cal_data[ADC_INPUT_IBAT].offset);
  723. } else {
  724. /* Calculate gain and offset for VMAIN if all reads succeeded */
  725. if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
  726. vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
  727. ((gpadc_cal[1] & 0x3F) << 2) |
  728. ((gpadc_cal[2] & 0xC0) >> 6));
  729. vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
  730. gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_hi =
  731. (u16)vmain_high;
  732. gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_lo =
  733. (u16)vmain_low;
  734. gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
  735. (19500 - 315) / (vmain_high - vmain_low);
  736. gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE *
  737. 19500 - (CALIB_SCALE * (19500 - 315) /
  738. (vmain_high - vmain_low)) * vmain_high;
  739. } else {
  740. gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
  741. }
  742. }
  743. /* Calculate gain and offset for BTEMP if all reads succeeded */
  744. if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
  745. btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
  746. (gpadc_cal[3] << 1) | ((gpadc_cal[4] & 0x80) >> 7));
  747. btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
  748. gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_hi = (u16)btemp_high;
  749. gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_lo = (u16)btemp_low;
  750. gpadc->cal_data[ADC_INPUT_BTEMP].gain =
  751. CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
  752. gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 -
  753. (CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low))
  754. * btemp_high;
  755. } else {
  756. gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0;
  757. }
  758. /* Calculate gain and offset for VBAT if all reads succeeded */
  759. if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
  760. vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
  761. vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
  762. gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_hi = (u16)vbat_high;
  763. gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_lo = (u16)vbat_low;
  764. gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE *
  765. (4700 - 2380) / (vbat_high - vbat_low);
  766. gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 -
  767. (CALIB_SCALE * (4700 - 2380) /
  768. (vbat_high - vbat_low)) * vbat_high;
  769. } else {
  770. gpadc->cal_data[ADC_INPUT_VBAT].gain = 0;
  771. }
  772. dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n",
  773. gpadc->cal_data[ADC_INPUT_VMAIN].gain,
  774. gpadc->cal_data[ADC_INPUT_VMAIN].offset);
  775. dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n",
  776. gpadc->cal_data[ADC_INPUT_BTEMP].gain,
  777. gpadc->cal_data[ADC_INPUT_BTEMP].offset);
  778. dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n",
  779. gpadc->cal_data[ADC_INPUT_VBAT].gain,
  780. gpadc->cal_data[ADC_INPUT_VBAT].offset);
  781. }
  782. #ifdef CONFIG_PM
  783. static int ab8500_gpadc_runtime_suspend(struct device *dev)
  784. {
  785. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  786. regulator_disable(gpadc->regu);
  787. return 0;
  788. }
  789. static int ab8500_gpadc_runtime_resume(struct device *dev)
  790. {
  791. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  792. int ret;
  793. ret = regulator_enable(gpadc->regu);
  794. if (ret)
  795. dev_err(dev, "Failed to enable vtvout LDO: %d\n", ret);
  796. return ret;
  797. }
  798. #endif
  799. #ifdef CONFIG_PM_SLEEP
  800. static int ab8500_gpadc_suspend(struct device *dev)
  801. {
  802. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  803. mutex_lock(&gpadc->ab8500_gpadc_lock);
  804. pm_runtime_get_sync(dev);
  805. regulator_disable(gpadc->regu);
  806. return 0;
  807. }
  808. static int ab8500_gpadc_resume(struct device *dev)
  809. {
  810. struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);
  811. int ret;
  812. ret = regulator_enable(gpadc->regu);
  813. if (ret)
  814. dev_err(dev, "Failed to enable vtvout LDO: %d\n", ret);
  815. pm_runtime_mark_last_busy(gpadc->dev);
  816. pm_runtime_put_autosuspend(gpadc->dev);
  817. mutex_unlock(&gpadc->ab8500_gpadc_lock);
  818. return ret;
  819. }
  820. #endif
  821. static int ab8500_gpadc_probe(struct platform_device *pdev)
  822. {
  823. int ret = 0;
  824. struct ab8500_gpadc *gpadc;
  825. gpadc = devm_kzalloc(&pdev->dev, sizeof(struct ab8500_gpadc), GFP_KERNEL);
  826. if (!gpadc) {
  827. dev_err(&pdev->dev, "Error: No memory\n");
  828. return -ENOMEM;
  829. }
  830. gpadc->irq_sw = platform_get_irq_byname(pdev, "SW_CONV_END");
  831. if (gpadc->irq_sw < 0)
  832. dev_err(gpadc->dev, "failed to get platform sw_conv_end irq\n");
  833. gpadc->irq_hw = platform_get_irq_byname(pdev, "HW_CONV_END");
  834. if (gpadc->irq_hw < 0)
  835. dev_err(gpadc->dev, "failed to get platform hw_conv_end irq\n");
  836. gpadc->dev = &pdev->dev;
  837. gpadc->parent = dev_get_drvdata(pdev->dev.parent);
  838. mutex_init(&gpadc->ab8500_gpadc_lock);
  839. /* Initialize completion used to notify completion of conversion */
  840. init_completion(&gpadc->ab8500_gpadc_complete);
  841. /* Register interrupts */
  842. if (gpadc->irq_sw >= 0) {
  843. ret = request_threaded_irq(gpadc->irq_sw, NULL,
  844. ab8500_bm_gpadcconvend_handler,
  845. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  846. "ab8500-gpadc-sw",
  847. gpadc);
  848. if (ret < 0) {
  849. dev_err(gpadc->dev,
  850. "Failed to register interrupt irq: %d\n",
  851. gpadc->irq_sw);
  852. goto fail;
  853. }
  854. }
  855. if (gpadc->irq_hw >= 0) {
  856. ret = request_threaded_irq(gpadc->irq_hw, NULL,
  857. ab8500_bm_gpadcconvend_handler,
  858. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  859. "ab8500-gpadc-hw",
  860. gpadc);
  861. if (ret < 0) {
  862. dev_err(gpadc->dev,
  863. "Failed to register interrupt irq: %d\n",
  864. gpadc->irq_hw);
  865. goto fail_irq;
  866. }
  867. }
  868. /* VTVout LDO used to power up ab8500-GPADC */
  869. gpadc->regu = devm_regulator_get(&pdev->dev, "vddadc");
  870. if (IS_ERR(gpadc->regu)) {
  871. ret = PTR_ERR(gpadc->regu);
  872. dev_err(gpadc->dev, "failed to get vtvout LDO\n");
  873. goto fail_irq;
  874. }
  875. platform_set_drvdata(pdev, gpadc);
  876. ret = regulator_enable(gpadc->regu);
  877. if (ret) {
  878. dev_err(gpadc->dev, "Failed to enable vtvout LDO: %d\n", ret);
  879. goto fail_enable;
  880. }
  881. pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY);
  882. pm_runtime_use_autosuspend(gpadc->dev);
  883. pm_runtime_set_active(gpadc->dev);
  884. pm_runtime_enable(gpadc->dev);
  885. ab8500_gpadc_read_calibration_data(gpadc);
  886. list_add_tail(&gpadc->node, &ab8500_gpadc_list);
  887. dev_dbg(gpadc->dev, "probe success\n");
  888. return 0;
  889. fail_enable:
  890. fail_irq:
  891. free_irq(gpadc->irq_sw, gpadc);
  892. free_irq(gpadc->irq_hw, gpadc);
  893. fail:
  894. return ret;
  895. }
  896. static int ab8500_gpadc_remove(struct platform_device *pdev)
  897. {
  898. struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
  899. /* remove this gpadc entry from the list */
  900. list_del(&gpadc->node);
  901. /* remove interrupt - completion of Sw ADC conversion */
  902. if (gpadc->irq_sw >= 0)
  903. free_irq(gpadc->irq_sw, gpadc);
  904. if (gpadc->irq_hw >= 0)
  905. free_irq(gpadc->irq_hw, gpadc);
  906. pm_runtime_get_sync(gpadc->dev);
  907. pm_runtime_disable(gpadc->dev);
  908. regulator_disable(gpadc->regu);
  909. pm_runtime_set_suspended(gpadc->dev);
  910. pm_runtime_put_noidle(gpadc->dev);
  911. return 0;
  912. }
  913. static const struct dev_pm_ops ab8500_gpadc_pm_ops = {
  914. SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend,
  915. ab8500_gpadc_runtime_resume,
  916. NULL)
  917. SET_SYSTEM_SLEEP_PM_OPS(ab8500_gpadc_suspend,
  918. ab8500_gpadc_resume)
  919. };
  920. static struct platform_driver ab8500_gpadc_driver = {
  921. .probe = ab8500_gpadc_probe,
  922. .remove = ab8500_gpadc_remove,
  923. .driver = {
  924. .name = "ab8500-gpadc",
  925. .pm = &ab8500_gpadc_pm_ops,
  926. },
  927. };
  928. static int __init ab8500_gpadc_init(void)
  929. {
  930. return platform_driver_register(&ab8500_gpadc_driver);
  931. }
  932. static void __exit ab8500_gpadc_exit(void)
  933. {
  934. platform_driver_unregister(&ab8500_gpadc_driver);
  935. }
  936. /**
  937. * ab8540_gpadc_get_otp() - returns OTP values
  938. *
  939. */
  940. void ab8540_gpadc_get_otp(struct ab8500_gpadc *gpadc,
  941. u16 *vmain_l, u16 *vmain_h, u16 *btemp_l, u16 *btemp_h,
  942. u16 *vbat_l, u16 *vbat_h, u16 *ibat_l, u16 *ibat_h)
  943. {
  944. *vmain_l = gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_lo;
  945. *vmain_h = gpadc->cal_data[ADC_INPUT_VMAIN].otp_calib_hi;
  946. *btemp_l = gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_lo;
  947. *btemp_h = gpadc->cal_data[ADC_INPUT_BTEMP].otp_calib_hi;
  948. *vbat_l = gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_lo;
  949. *vbat_h = gpadc->cal_data[ADC_INPUT_VBAT].otp_calib_hi;
  950. *ibat_l = gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_lo;
  951. *ibat_h = gpadc->cal_data[ADC_INPUT_IBAT].otp_calib_hi;
  952. return ;
  953. }
  954. subsys_initcall_sync(ab8500_gpadc_init);
  955. module_exit(ab8500_gpadc_exit);
  956. MODULE_LICENSE("GPL v2");
  957. MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson,"
  958. "M'boumba Cedric Madianga");
  959. MODULE_ALIAS("platform:ab8500_gpadc");
  960. MODULE_DESCRIPTION("AB8500 GPADC driver");