twl6030-gpadc.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. ret = -EINTR;
  483. goto err;
  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 = 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. ret = iio_device_register(indio_dev);
  836. return ret;
  837. }
  838. static int twl6030_gpadc_remove(struct platform_device *pdev)
  839. {
  840. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  841. twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
  842. iio_device_unregister(indio_dev);
  843. return 0;
  844. }
  845. #ifdef CONFIG_PM_SLEEP
  846. static int twl6030_gpadc_suspend(struct device *pdev)
  847. {
  848. int ret;
  849. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
  850. TWL6030_REG_TOGGLE1);
  851. if (ret)
  852. dev_err(pdev, "error reseting GPADC (%d)!\n", ret);
  853. return 0;
  854. };
  855. static int twl6030_gpadc_resume(struct device *pdev)
  856. {
  857. int ret;
  858. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
  859. TWL6030_REG_TOGGLE1);
  860. if (ret)
  861. dev_err(pdev, "error setting GPADC (%d)!\n", ret);
  862. return 0;
  863. };
  864. #endif
  865. static SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
  866. twl6030_gpadc_resume);
  867. static struct platform_driver twl6030_gpadc_driver = {
  868. .probe = twl6030_gpadc_probe,
  869. .remove = twl6030_gpadc_remove,
  870. .driver = {
  871. .name = DRIVER_NAME,
  872. .owner = THIS_MODULE,
  873. .pm = &twl6030_gpadc_pm_ops,
  874. .of_match_table = of_twl6030_match_tbl,
  875. },
  876. };
  877. module_platform_driver(twl6030_gpadc_driver);
  878. MODULE_ALIAS("platform: " DRIVER_NAME);
  879. MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
  880. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  881. MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
  882. MODULE_DESCRIPTION("twl6030 ADC driver");
  883. MODULE_LICENSE("GPL");