twl6030-gpadc.c 25 KB

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