twl6030-gpadc.c 25 KB

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