twl6030-gpadc.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TWL6030 GPADC module driver
  4. *
  5. * Copyright (C) 2009-2013 Texas Instruments Inc.
  6. * Nishant Kamat <[email protected]>
  7. * Balaji T K <[email protected]>
  8. * Graeme Gregory <[email protected]>
  9. * Girish S Ghongdemath <[email protected]>
  10. * Ambresh K <[email protected]>
  11. * Oleksandr Kozaruk <[email protected]
  12. *
  13. * Based on twl4030-madc.c
  14. * Copyright (C) 2008 Nokia Corporation
  15. * Mikko Ylinen <[email protected]>
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/mfd/twl.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #define DRIVER_NAME "twl6030_gpadc"
  26. /*
  27. * twl6030 per TRM has 17 channels, and twl6032 has 19 channels
  28. * 2 test network channels are not used,
  29. * 2 die temperature channels are not used either, as it is not
  30. * defined how to convert ADC value to temperature
  31. */
  32. #define TWL6030_GPADC_USED_CHANNELS 13
  33. #define TWL6030_GPADC_MAX_CHANNELS 15
  34. #define TWL6032_GPADC_USED_CHANNELS 15
  35. #define TWL6032_GPADC_MAX_CHANNELS 19
  36. #define TWL6030_GPADC_NUM_TRIM_REGS 16
  37. #define TWL6030_GPADC_CTRL_P1 0x05
  38. #define TWL6032_GPADC_GPSELECT_ISB 0x07
  39. #define TWL6032_GPADC_CTRL_P1 0x08
  40. #define TWL6032_GPADC_GPCH0_LSB 0x0d
  41. #define TWL6032_GPADC_GPCH0_MSB 0x0e
  42. #define TWL6030_GPADC_CTRL_P1_SP1 BIT(3)
  43. #define TWL6030_GPADC_GPCH0_LSB (0x29)
  44. #define TWL6030_GPADC_RT_SW1_EOC_MASK BIT(5)
  45. #define TWL6030_GPADC_TRIM1 0xCD
  46. #define TWL6030_REG_TOGGLE1 0x90
  47. #define TWL6030_GPADCS BIT(1)
  48. #define TWL6030_GPADCR BIT(0)
  49. #define USB_VBUS_CTRL_SET 0x04
  50. #define USB_ID_CTRL_SET 0x06
  51. #define TWL6030_MISC1 0xE4
  52. #define VBUS_MEAS 0x01
  53. #define ID_MEAS 0x01
  54. #define VAC_MEAS 0x04
  55. #define VBAT_MEAS 0x02
  56. #define BB_MEAS 0x01
  57. /**
  58. * struct twl6030_chnl_calib - channel calibration
  59. * @gain: slope coefficient for ideal curve
  60. * @gain_error: gain error
  61. * @offset_error: offset of the real curve
  62. */
  63. struct twl6030_chnl_calib {
  64. s32 gain;
  65. s32 gain_error;
  66. s32 offset_error;
  67. };
  68. /**
  69. * struct twl6030_ideal_code - GPADC calibration parameters
  70. * GPADC is calibrated in two points: close to the beginning and
  71. * to the and of the measurable input range
  72. *
  73. * @channel: channel number
  74. * @code1: ideal code for the input at the beginning
  75. * @code2: ideal code for at the end of the range
  76. * @volt1: voltage input at the beginning(low voltage)
  77. * @volt2: voltage input at the end(high voltage)
  78. */
  79. struct twl6030_ideal_code {
  80. int channel;
  81. u16 code1;
  82. u16 code2;
  83. u16 volt1;
  84. u16 volt2;
  85. };
  86. struct twl6030_gpadc_data;
  87. /**
  88. * struct twl6030_gpadc_platform_data - platform specific data
  89. * @nchannels: number of GPADC channels
  90. * @iio_channels: iio channels
  91. * @ideal: pointer to calibration parameters
  92. * @start_conversion: pointer to ADC start conversion function
  93. * @channel_to_reg: pointer to ADC function to convert channel to
  94. * register address for reading conversion result
  95. * @calibrate: pointer to calibration function
  96. */
  97. struct twl6030_gpadc_platform_data {
  98. const int nchannels;
  99. const struct iio_chan_spec *iio_channels;
  100. const struct twl6030_ideal_code *ideal;
  101. int (*start_conversion)(int channel);
  102. u8 (*channel_to_reg)(int channel);
  103. int (*calibrate)(struct twl6030_gpadc_data *gpadc);
  104. };
  105. /**
  106. * struct twl6030_gpadc_data - GPADC data
  107. * @dev: device pointer
  108. * @lock: mutual exclusion lock for the structure
  109. * @irq_complete: completion to signal end of conversion
  110. * @twl6030_cal_tbl: pointer to calibration data for each
  111. * channel with gain error and offset
  112. * @pdata: pointer to device specific data
  113. */
  114. struct twl6030_gpadc_data {
  115. struct device *dev;
  116. struct mutex lock;
  117. struct completion irq_complete;
  118. struct twl6030_chnl_calib *twl6030_cal_tbl;
  119. const struct twl6030_gpadc_platform_data *pdata;
  120. };
  121. /*
  122. * channels 11, 12, 13, 15 and 16 have no calibration data
  123. * calibration offset is same for channels 1, 3, 4, 5
  124. *
  125. * The data is taken from GPADC_TRIM registers description.
  126. * GPADC_TRIM registers keep difference between the code measured
  127. * at volt1 and volt2 input voltages and corresponding code1 and code2
  128. */
  129. static const struct twl6030_ideal_code
  130. twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
  131. [0] = { /* ch 0, external, battery type, resistor value */
  132. .channel = 0,
  133. .code1 = 116,
  134. .code2 = 745,
  135. .volt1 = 141,
  136. .volt2 = 910,
  137. },
  138. [1] = { /* ch 1, external, battery temperature, NTC resistor value */
  139. .channel = 1,
  140. .code1 = 82,
  141. .code2 = 900,
  142. .volt1 = 100,
  143. .volt2 = 1100,
  144. },
  145. [2] = { /* ch 2, external, audio accessory/general purpose */
  146. .channel = 2,
  147. .code1 = 55,
  148. .code2 = 818,
  149. .volt1 = 101,
  150. .volt2 = 1499,
  151. },
  152. [3] = { /* ch 3, external, general purpose */
  153. .channel = 3,
  154. .code1 = 82,
  155. .code2 = 900,
  156. .volt1 = 100,
  157. .volt2 = 1100,
  158. },
  159. [4] = { /* ch 4, external, temperature measurement/general purpose */
  160. .channel = 4,
  161. .code1 = 82,
  162. .code2 = 900,
  163. .volt1 = 100,
  164. .volt2 = 1100,
  165. },
  166. [5] = { /* ch 5, external, general purpose */
  167. .channel = 5,
  168. .code1 = 82,
  169. .code2 = 900,
  170. .volt1 = 100,
  171. .volt2 = 1100,
  172. },
  173. [6] = { /* ch 6, external, general purpose */
  174. .channel = 6,
  175. .code1 = 82,
  176. .code2 = 900,
  177. .volt1 = 100,
  178. .volt2 = 1100,
  179. },
  180. [7] = { /* ch 7, internal, main battery */
  181. .channel = 7,
  182. .code1 = 614,
  183. .code2 = 941,
  184. .volt1 = 3001,
  185. .volt2 = 4599,
  186. },
  187. [8] = { /* ch 8, internal, backup battery */
  188. .channel = 8,
  189. .code1 = 82,
  190. .code2 = 688,
  191. .volt1 = 501,
  192. .volt2 = 4203,
  193. },
  194. [9] = { /* ch 9, internal, external charger input */
  195. .channel = 9,
  196. .code1 = 182,
  197. .code2 = 818,
  198. .volt1 = 2001,
  199. .volt2 = 8996,
  200. },
  201. [10] = { /* ch 10, internal, VBUS */
  202. .channel = 10,
  203. .code1 = 149,
  204. .code2 = 818,
  205. .volt1 = 1001,
  206. .volt2 = 5497,
  207. },
  208. [11] = { /* ch 11, internal, VBUS charging current */
  209. .channel = 11,
  210. },
  211. /* ch 12, internal, Die temperature */
  212. /* ch 13, internal, Die temperature */
  213. [12] = { /* ch 14, internal, USB ID line */
  214. .channel = 14,
  215. .code1 = 48,
  216. .code2 = 714,
  217. .volt1 = 323,
  218. .volt2 = 4800,
  219. },
  220. };
  221. static const struct twl6030_ideal_code
  222. twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
  223. [0] = { /* ch 0, external, battery type, resistor value */
  224. .channel = 0,
  225. .code1 = 1441,
  226. .code2 = 3276,
  227. .volt1 = 440,
  228. .volt2 = 1000,
  229. },
  230. [1] = { /* ch 1, external, battery temperature, NTC resistor value */
  231. .channel = 1,
  232. .code1 = 1441,
  233. .code2 = 3276,
  234. .volt1 = 440,
  235. .volt2 = 1000,
  236. },
  237. [2] = { /* ch 2, external, audio accessory/general purpose */
  238. .channel = 2,
  239. .code1 = 1441,
  240. .code2 = 3276,
  241. .volt1 = 660,
  242. .volt2 = 1500,
  243. },
  244. [3] = { /* ch 3, external, temperature with external diode/general
  245. purpose */
  246. .channel = 3,
  247. .code1 = 1441,
  248. .code2 = 3276,
  249. .volt1 = 440,
  250. .volt2 = 1000,
  251. },
  252. [4] = { /* ch 4, external, temperature measurement/general purpose */
  253. .channel = 4,
  254. .code1 = 1441,
  255. .code2 = 3276,
  256. .volt1 = 440,
  257. .volt2 = 1000,
  258. },
  259. [5] = { /* ch 5, external, general purpose */
  260. .channel = 5,
  261. .code1 = 1441,
  262. .code2 = 3276,
  263. .volt1 = 440,
  264. .volt2 = 1000,
  265. },
  266. [6] = { /* ch 6, external, general purpose */
  267. .channel = 6,
  268. .code1 = 1441,
  269. .code2 = 3276,
  270. .volt1 = 440,
  271. .volt2 = 1000,
  272. },
  273. [7] = { /* ch7, internal, system supply */
  274. .channel = 7,
  275. .code1 = 1441,
  276. .code2 = 3276,
  277. .volt1 = 2200,
  278. .volt2 = 5000,
  279. },
  280. [8] = { /* ch8, internal, backup battery */
  281. .channel = 8,
  282. .code1 = 1441,
  283. .code2 = 3276,
  284. .volt1 = 2200,
  285. .volt2 = 5000,
  286. },
  287. [9] = { /* ch 9, internal, external charger input */
  288. .channel = 9,
  289. .code1 = 1441,
  290. .code2 = 3276,
  291. .volt1 = 3960,
  292. .volt2 = 9000,
  293. },
  294. [10] = { /* ch10, internal, VBUS */
  295. .channel = 10,
  296. .code1 = 150,
  297. .code2 = 751,
  298. .volt1 = 1000,
  299. .volt2 = 5000,
  300. },
  301. [11] = { /* ch 11, internal, VBUS DC-DC output current */
  302. .channel = 11,
  303. .code1 = 1441,
  304. .code2 = 3276,
  305. .volt1 = 660,
  306. .volt2 = 1500,
  307. },
  308. /* ch 12, internal, Die temperature */
  309. /* ch 13, internal, Die temperature */
  310. [12] = { /* ch 14, internal, USB ID line */
  311. .channel = 14,
  312. .code1 = 1441,
  313. .code2 = 3276,
  314. .volt1 = 2420,
  315. .volt2 = 5500,
  316. },
  317. /* ch 15, internal, test network */
  318. /* ch 16, internal, test network */
  319. [13] = { /* ch 17, internal, battery charging current */
  320. .channel = 17,
  321. },
  322. [14] = { /* ch 18, internal, battery voltage */
  323. .channel = 18,
  324. .code1 = 1441,
  325. .code2 = 3276,
  326. .volt1 = 2200,
  327. .volt2 = 5000,
  328. },
  329. };
  330. static inline int twl6030_gpadc_write(u8 reg, u8 val)
  331. {
  332. return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
  333. }
  334. static inline int twl6030_gpadc_read(u8 reg, u8 *val)
  335. {
  336. return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
  337. }
  338. static int twl6030_gpadc_enable_irq(u8 mask)
  339. {
  340. int ret;
  341. ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
  342. if (ret < 0)
  343. return ret;
  344. ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
  345. return ret;
  346. }
  347. static void twl6030_gpadc_disable_irq(u8 mask)
  348. {
  349. twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
  350. twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
  351. }
  352. static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
  353. {
  354. struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
  355. complete(&gpadc->irq_complete);
  356. return IRQ_HANDLED;
  357. }
  358. static int twl6030_start_conversion(int channel)
  359. {
  360. return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
  361. TWL6030_GPADC_CTRL_P1_SP1);
  362. }
  363. static int twl6032_start_conversion(int channel)
  364. {
  365. int ret;
  366. ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
  367. if (ret)
  368. return ret;
  369. return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
  370. TWL6030_GPADC_CTRL_P1_SP1);
  371. }
  372. static u8 twl6030_channel_to_reg(int channel)
  373. {
  374. return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
  375. }
  376. static u8 twl6032_channel_to_reg(int channel)
  377. {
  378. /*
  379. * for any prior chosen channel, when the conversion is ready
  380. * the result is avalable in GPCH0_LSB, GPCH0_MSB.
  381. */
  382. return TWL6032_GPADC_GPCH0_LSB;
  383. }
  384. static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
  385. int channel, int size)
  386. {
  387. int i;
  388. for (i = 0; i < size; i++)
  389. if (ideal[i].channel == channel)
  390. break;
  391. return i;
  392. }
  393. static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
  394. *pdata, int channel)
  395. {
  396. const struct twl6030_ideal_code *ideal = pdata->ideal;
  397. int i;
  398. i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
  399. /* not calibrated channels have 0 in all structure members */
  400. return pdata->ideal[i].code2;
  401. }
  402. static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
  403. int channel, int raw_code)
  404. {
  405. const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
  406. int corrected_code;
  407. int i;
  408. i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
  409. corrected_code = ((raw_code * 1000) -
  410. gpadc->twl6030_cal_tbl[i].offset_error) /
  411. gpadc->twl6030_cal_tbl[i].gain_error;
  412. return corrected_code;
  413. }
  414. static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
  415. int channel, int *res)
  416. {
  417. u8 reg = gpadc->pdata->channel_to_reg(channel);
  418. __le16 val;
  419. int raw_code;
  420. int ret;
  421. ret = twl6030_gpadc_read(reg, (u8 *)&val);
  422. if (ret) {
  423. dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
  424. return ret;
  425. }
  426. raw_code = le16_to_cpu(val);
  427. dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
  428. if (twl6030_channel_calibrated(gpadc->pdata, channel))
  429. *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
  430. else
  431. *res = raw_code;
  432. return ret;
  433. }
  434. static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
  435. int channel, int *val)
  436. {
  437. const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
  438. int corrected_code;
  439. int channel_value;
  440. int i;
  441. int ret;
  442. ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
  443. if (ret)
  444. return ret;
  445. i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
  446. channel_value = corrected_code *
  447. gpadc->twl6030_cal_tbl[i].gain;
  448. /* Shift back into mV range */
  449. channel_value /= 1000;
  450. dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
  451. dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
  452. *val = channel_value;
  453. return ret;
  454. }
  455. static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
  456. const struct iio_chan_spec *chan,
  457. int *val, int *val2, long mask)
  458. {
  459. struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
  460. int ret;
  461. long timeout;
  462. mutex_lock(&gpadc->lock);
  463. ret = gpadc->pdata->start_conversion(chan->channel);
  464. if (ret) {
  465. dev_err(gpadc->dev, "failed to start conversion\n");
  466. goto err;
  467. }
  468. /* wait for conversion to complete */
  469. timeout = wait_for_completion_interruptible_timeout(
  470. &gpadc->irq_complete, msecs_to_jiffies(5000));
  471. if (timeout == 0) {
  472. ret = -ETIMEDOUT;
  473. goto err;
  474. } else if (timeout < 0) {
  475. ret = -EINTR;
  476. goto err;
  477. }
  478. switch (mask) {
  479. case IIO_CHAN_INFO_RAW:
  480. ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
  481. ret = ret ? -EIO : IIO_VAL_INT;
  482. break;
  483. case IIO_CHAN_INFO_PROCESSED:
  484. ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
  485. ret = ret ? -EIO : IIO_VAL_INT;
  486. break;
  487. default:
  488. break;
  489. }
  490. err:
  491. mutex_unlock(&gpadc->lock);
  492. return ret;
  493. }
  494. /*
  495. * The GPADC channels are calibrated using a two point calibration method.
  496. * The channels measured with two known values: volt1 and volt2, and
  497. * ideal corresponding output codes are known: code1, code2.
  498. * The difference(d1, d2) between ideal and measured codes stored in trim
  499. * registers.
  500. * The goal is to find offset and gain of the real curve for each calibrated
  501. * channel.
  502. * gain: k = 1 + ((d2 - d1) / (x2 - x1))
  503. * offset: b = d1 + (k - 1) * x1
  504. */
  505. static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
  506. int channel, int d1, int d2)
  507. {
  508. int b, k, gain, x1, x2, i;
  509. const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
  510. i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
  511. /* Gain */
  512. gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
  513. (ideal[i].code2 - ideal[i].code1);
  514. x1 = ideal[i].code1;
  515. x2 = ideal[i].code2;
  516. /* k - real curve gain */
  517. k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
  518. /* b - offset of the real curve gain */
  519. b = (d1 * 1000) - (k - 1000) * x1;
  520. gpadc->twl6030_cal_tbl[i].gain = gain;
  521. gpadc->twl6030_cal_tbl[i].gain_error = k;
  522. gpadc->twl6030_cal_tbl[i].offset_error = b;
  523. dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1);
  524. dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2);
  525. dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1);
  526. dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2);
  527. dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
  528. dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k);
  529. dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b);
  530. }
  531. static inline int twl6030_gpadc_get_trim_offset(s8 d)
  532. {
  533. /*
  534. * XXX NOTE!
  535. * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
  536. * though, the documentation states that trim value
  537. * is absolute value, the correct conversion results are
  538. * obtained if the value is interpreted as 2's complement.
  539. */
  540. __u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
  541. return sign_extend32(temp, 6);
  542. }
  543. static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
  544. {
  545. int ret;
  546. int chn;
  547. u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
  548. s8 d1, d2;
  549. /*
  550. * for calibration two measurements have been performed at
  551. * factory, for some channels, during the production test and
  552. * have been stored in registers. This two stored values are
  553. * used to correct the measurements. The values represent
  554. * offsets for the given input from the output on ideal curve.
  555. */
  556. ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
  557. TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
  558. if (ret < 0) {
  559. dev_err(gpadc->dev, "calibration failed\n");
  560. return ret;
  561. }
  562. for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
  563. switch (chn) {
  564. case 0:
  565. d1 = trim_regs[0];
  566. d2 = trim_regs[1];
  567. break;
  568. case 1:
  569. case 3:
  570. case 4:
  571. case 5:
  572. case 6:
  573. d1 = trim_regs[4];
  574. d2 = trim_regs[5];
  575. break;
  576. case 2:
  577. d1 = trim_regs[12];
  578. d2 = trim_regs[13];
  579. break;
  580. case 7:
  581. d1 = trim_regs[6];
  582. d2 = trim_regs[7];
  583. break;
  584. case 8:
  585. d1 = trim_regs[2];
  586. d2 = trim_regs[3];
  587. break;
  588. case 9:
  589. d1 = trim_regs[8];
  590. d2 = trim_regs[9];
  591. break;
  592. case 10:
  593. d1 = trim_regs[10];
  594. d2 = trim_regs[11];
  595. break;
  596. case 14:
  597. d1 = trim_regs[14];
  598. d2 = trim_regs[15];
  599. break;
  600. default:
  601. continue;
  602. }
  603. d1 = twl6030_gpadc_get_trim_offset(d1);
  604. d2 = twl6030_gpadc_get_trim_offset(d2);
  605. twl6030_calibrate_channel(gpadc, chn, d1, d2);
  606. }
  607. return 0;
  608. }
  609. static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
  610. unsigned int reg1, unsigned int mask0, unsigned int mask1,
  611. unsigned int shift0)
  612. {
  613. int val;
  614. val = (trim_regs[reg0] & mask0) << shift0;
  615. val |= (trim_regs[reg1] & mask1) >> 1;
  616. if (trim_regs[reg1] & 0x01)
  617. val = -val;
  618. return val;
  619. }
  620. static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
  621. {
  622. int chn, d1 = 0, d2 = 0, temp;
  623. u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
  624. int ret;
  625. ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
  626. TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
  627. if (ret < 0) {
  628. dev_err(gpadc->dev, "calibration failed\n");
  629. return ret;
  630. }
  631. /*
  632. * Loop to calculate the value needed for returning voltages from
  633. * GPADC not values.
  634. *
  635. * gain is calculated to 3 decimal places fixed point.
  636. */
  637. for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
  638. switch (chn) {
  639. case 0:
  640. case 1:
  641. case 2:
  642. case 3:
  643. case 4:
  644. case 5:
  645. case 6:
  646. case 11:
  647. case 14:
  648. d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
  649. 0x06, 2);
  650. d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
  651. 0x06, 2);
  652. break;
  653. case 8:
  654. temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
  655. 0x06, 2);
  656. d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
  657. 0x18, 0x1E, 1);
  658. temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
  659. 0x06, 2);
  660. d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
  661. 0x1F, 0x06, 2);
  662. break;
  663. case 9:
  664. temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
  665. 0x06, 2);
  666. d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
  667. 0x18, 0x1E, 1);
  668. temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
  669. 0x06, 2);
  670. d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
  671. 0x1F, 0x06, 1);
  672. break;
  673. case 10:
  674. d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
  675. 0x0E, 3);
  676. d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
  677. 0x0E, 3);
  678. break;
  679. case 7:
  680. case 18:
  681. temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
  682. 0x06, 2);
  683. d1 = (trim_regs[4] & 0x7E) >> 1;
  684. if (trim_regs[4] & 0x01)
  685. d1 = -d1;
  686. d1 += temp;
  687. temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
  688. 0x06, 2);
  689. d2 = (trim_regs[5] & 0xFE) >> 1;
  690. if (trim_regs[5] & 0x01)
  691. d2 = -d2;
  692. d2 += temp;
  693. break;
  694. default:
  695. /* No data for other channels */
  696. continue;
  697. }
  698. twl6030_calibrate_channel(gpadc, chn, d1, d2);
  699. }
  700. return 0;
  701. }
  702. #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
  703. .type = _type, \
  704. .channel = chn, \
  705. .info_mask_separate = BIT(chan_info), \
  706. .indexed = 1, \
  707. }
  708. static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
  709. TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  710. TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
  711. TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  712. TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  713. TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
  714. TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  715. TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  716. TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  717. TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  718. TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  719. TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  720. TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
  721. TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  722. };
  723. static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
  724. TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  725. TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
  726. TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  727. TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  728. TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
  729. TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  730. TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  731. TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  732. TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  733. TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  734. TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  735. TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  736. TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  737. TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
  738. TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
  739. };
  740. static const struct iio_info twl6030_gpadc_iio_info = {
  741. .read_raw = &twl6030_gpadc_read_raw,
  742. };
  743. static const struct twl6030_gpadc_platform_data twl6030_pdata = {
  744. .iio_channels = twl6030_gpadc_iio_channels,
  745. .nchannels = TWL6030_GPADC_USED_CHANNELS,
  746. .ideal = twl6030_ideal,
  747. .start_conversion = twl6030_start_conversion,
  748. .channel_to_reg = twl6030_channel_to_reg,
  749. .calibrate = twl6030_calibration,
  750. };
  751. static const struct twl6030_gpadc_platform_data twl6032_pdata = {
  752. .iio_channels = twl6032_gpadc_iio_channels,
  753. .nchannels = TWL6032_GPADC_USED_CHANNELS,
  754. .ideal = twl6032_ideal,
  755. .start_conversion = twl6032_start_conversion,
  756. .channel_to_reg = twl6032_channel_to_reg,
  757. .calibrate = twl6032_calibration,
  758. };
  759. static const struct of_device_id of_twl6030_match_tbl[] = {
  760. {
  761. .compatible = "ti,twl6030-gpadc",
  762. .data = &twl6030_pdata,
  763. },
  764. {
  765. .compatible = "ti,twl6032-gpadc",
  766. .data = &twl6032_pdata,
  767. },
  768. { /* end */ }
  769. };
  770. MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
  771. static int twl6030_gpadc_probe(struct platform_device *pdev)
  772. {
  773. struct device *dev = &pdev->dev;
  774. struct twl6030_gpadc_data *gpadc;
  775. const struct twl6030_gpadc_platform_data *pdata;
  776. const struct of_device_id *match;
  777. struct iio_dev *indio_dev;
  778. int irq;
  779. int ret;
  780. match = of_match_device(of_twl6030_match_tbl, dev);
  781. if (!match)
  782. return -EINVAL;
  783. pdata = match->data;
  784. indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
  785. if (!indio_dev)
  786. return -ENOMEM;
  787. gpadc = iio_priv(indio_dev);
  788. gpadc->twl6030_cal_tbl = devm_kcalloc(dev,
  789. pdata->nchannels,
  790. sizeof(*gpadc->twl6030_cal_tbl),
  791. GFP_KERNEL);
  792. if (!gpadc->twl6030_cal_tbl)
  793. return -ENOMEM;
  794. gpadc->dev = dev;
  795. gpadc->pdata = pdata;
  796. platform_set_drvdata(pdev, indio_dev);
  797. mutex_init(&gpadc->lock);
  798. init_completion(&gpadc->irq_complete);
  799. ret = pdata->calibrate(gpadc);
  800. if (ret < 0) {
  801. dev_err(dev, "failed to read calibration registers\n");
  802. return ret;
  803. }
  804. irq = platform_get_irq(pdev, 0);
  805. if (irq < 0)
  806. return irq;
  807. ret = devm_request_threaded_irq(dev, irq, NULL,
  808. twl6030_gpadc_irq_handler,
  809. IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
  810. if (ret)
  811. return ret;
  812. ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
  813. if (ret < 0) {
  814. dev_err(dev, "failed to enable GPADC interrupt\n");
  815. return ret;
  816. }
  817. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
  818. TWL6030_REG_TOGGLE1);
  819. if (ret < 0) {
  820. dev_err(dev, "failed to enable GPADC module\n");
  821. return ret;
  822. }
  823. ret = twl_i2c_write_u8(TWL_MODULE_USB, VBUS_MEAS, USB_VBUS_CTRL_SET);
  824. if (ret < 0) {
  825. dev_err(dev, "failed to wire up inputs\n");
  826. return ret;
  827. }
  828. ret = twl_i2c_write_u8(TWL_MODULE_USB, ID_MEAS, USB_ID_CTRL_SET);
  829. if (ret < 0) {
  830. dev_err(dev, "failed to wire up inputs\n");
  831. return ret;
  832. }
  833. ret = twl_i2c_write_u8(TWL6030_MODULE_ID0,
  834. VBAT_MEAS | BB_MEAS | VAC_MEAS,
  835. TWL6030_MISC1);
  836. if (ret < 0) {
  837. dev_err(dev, "failed to wire up inputs\n");
  838. return ret;
  839. }
  840. indio_dev->name = DRIVER_NAME;
  841. indio_dev->info = &twl6030_gpadc_iio_info;
  842. indio_dev->modes = INDIO_DIRECT_MODE;
  843. indio_dev->channels = pdata->iio_channels;
  844. indio_dev->num_channels = pdata->nchannels;
  845. return iio_device_register(indio_dev);
  846. }
  847. static int twl6030_gpadc_remove(struct platform_device *pdev)
  848. {
  849. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  850. twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
  851. iio_device_unregister(indio_dev);
  852. return 0;
  853. }
  854. static int twl6030_gpadc_suspend(struct device *pdev)
  855. {
  856. int ret;
  857. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
  858. TWL6030_REG_TOGGLE1);
  859. if (ret)
  860. dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
  861. return 0;
  862. };
  863. static int twl6030_gpadc_resume(struct device *pdev)
  864. {
  865. int ret;
  866. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
  867. TWL6030_REG_TOGGLE1);
  868. if (ret)
  869. dev_err(pdev, "error setting GPADC (%d)!\n", ret);
  870. return 0;
  871. };
  872. static DEFINE_SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
  873. twl6030_gpadc_resume);
  874. static struct platform_driver twl6030_gpadc_driver = {
  875. .probe = twl6030_gpadc_probe,
  876. .remove = twl6030_gpadc_remove,
  877. .driver = {
  878. .name = DRIVER_NAME,
  879. .pm = pm_sleep_ptr(&twl6030_gpadc_pm_ops),
  880. .of_match_table = of_twl6030_match_tbl,
  881. },
  882. };
  883. module_platform_driver(twl6030_gpadc_driver);
  884. MODULE_ALIAS("platform:" DRIVER_NAME);
  885. MODULE_AUTHOR("Balaji T K <[email protected]>");
  886. MODULE_AUTHOR("Graeme Gregory <[email protected]>");
  887. MODULE_AUTHOR("Oleksandr Kozaruk <[email protected]");
  888. MODULE_DESCRIPTION("twl6030 ADC driver");
  889. MODULE_LICENSE("GPL");