ab8500-gpadc.c 31 KB

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