twl6030-gpadc.c 24 KB

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