twl4030-madc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. *
  3. * TWL4030 MADC module driver-This driver monitors the real time
  4. * conversion of analog signals like battery temperature,
  5. * battery type, battery level etc.
  6. *
  7. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  8. * J Keerthy <j-keerthy@ti.com>
  9. *
  10. * Based on twl4030-madc.c
  11. * Copyright (C) 2008 Nokia Corporation
  12. * Mikko Ylinen <mikko.k.ylinen@nokia.com>
  13. *
  14. * Amit Kucheria <amit.kucheria@canonical.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/device.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kernel.h>
  34. #include <linux/delay.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/slab.h>
  37. #include <linux/i2c/twl.h>
  38. #include <linux/i2c/twl4030-madc.h>
  39. #include <linux/module.h>
  40. #include <linux/stddef.h>
  41. #include <linux/mutex.h>
  42. #include <linux/bitops.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/types.h>
  45. #include <linux/gfp.h>
  46. #include <linux/err.h>
  47. #include <linux/iio/iio.h>
  48. /**
  49. * struct twl4030_madc_data - a container for madc info
  50. * @dev: Pointer to device structure for madc
  51. * @lock: Mutex protecting this data structure
  52. * @requests: Array of request struct corresponding to SW1, SW2 and RT
  53. * @use_second_irq: IRQ selection (main or co-processor)
  54. * @imr: Interrupt mask register of MADC
  55. * @isr: Interrupt status register of MADC
  56. */
  57. struct twl4030_madc_data {
  58. struct device *dev;
  59. struct mutex lock; /* mutex protecting this data structure */
  60. struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
  61. bool use_second_irq;
  62. u8 imr;
  63. u8 isr;
  64. };
  65. static int twl4030_madc_read(struct iio_dev *iio_dev,
  66. const struct iio_chan_spec *chan,
  67. int *val, int *val2, long mask)
  68. {
  69. struct twl4030_madc_data *madc = iio_priv(iio_dev);
  70. struct twl4030_madc_request req;
  71. int ret;
  72. req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
  73. req.channels = BIT(chan->channel);
  74. req.active = false;
  75. req.func_cb = NULL;
  76. req.type = TWL4030_MADC_WAIT;
  77. req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
  78. req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
  79. ret = twl4030_madc_conversion(&req);
  80. if (ret < 0)
  81. return ret;
  82. *val = req.rbuf[chan->channel];
  83. return IIO_VAL_INT;
  84. }
  85. static const struct iio_info twl4030_madc_iio_info = {
  86. .read_raw = &twl4030_madc_read,
  87. .driver_module = THIS_MODULE,
  88. };
  89. #define TWL4030_ADC_CHANNEL(_channel, _type, _name) { \
  90. .type = _type, \
  91. .channel = _channel, \
  92. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  93. BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
  94. BIT(IIO_CHAN_INFO_PROCESSED), \
  95. .datasheet_name = _name, \
  96. .indexed = 1, \
  97. }
  98. static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
  99. TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0"),
  100. TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1"),
  101. TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2"),
  102. TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3"),
  103. TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4"),
  104. TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5"),
  105. TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6"),
  106. TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7"),
  107. TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8"),
  108. TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9"),
  109. TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10"),
  110. TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11"),
  111. TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12"),
  112. TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13"),
  113. TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14"),
  114. TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15"),
  115. };
  116. static struct twl4030_madc_data *twl4030_madc;
  117. struct twl4030_prescale_divider_ratios {
  118. s16 numerator;
  119. s16 denominator;
  120. };
  121. static const struct twl4030_prescale_divider_ratios
  122. twl4030_divider_ratios[16] = {
  123. {1, 1}, /* CHANNEL 0 No Prescaler */
  124. {1, 1}, /* CHANNEL 1 No Prescaler */
  125. {6, 10}, /* CHANNEL 2 */
  126. {6, 10}, /* CHANNEL 3 */
  127. {6, 10}, /* CHANNEL 4 */
  128. {6, 10}, /* CHANNEL 5 */
  129. {6, 10}, /* CHANNEL 6 */
  130. {6, 10}, /* CHANNEL 7 */
  131. {3, 14}, /* CHANNEL 8 */
  132. {1, 3}, /* CHANNEL 9 */
  133. {1, 1}, /* CHANNEL 10 No Prescaler */
  134. {15, 100}, /* CHANNEL 11 */
  135. {1, 4}, /* CHANNEL 12 */
  136. {1, 1}, /* CHANNEL 13 Reserved channels */
  137. {1, 1}, /* CHANNEL 14 Reseved channels */
  138. {5, 11}, /* CHANNEL 15 */
  139. };
  140. /* Conversion table from -3 to 55 degrees Celcius */
  141. static int twl4030_therm_tbl[] = {
  142. 30800, 29500, 28300, 27100,
  143. 26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700,
  144. 17900, 17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100,
  145. 12600, 12100, 11600, 11200, 10800, 10400, 10000, 9630, 9280,
  146. 8950, 8620, 8310, 8020, 7730, 7460, 7200, 6950, 6710,
  147. 6470, 6250, 6040, 5830, 5640, 5450, 5260, 5090, 4920,
  148. 4760, 4600, 4450, 4310, 4170, 4040, 3910, 3790, 3670,
  149. 3550
  150. };
  151. /*
  152. * Structure containing the registers
  153. * of different conversion methods supported by MADC.
  154. * Hardware or RT real time conversion request initiated by external host
  155. * processor for RT Signal conversions.
  156. * External host processors can also request for non RT conversions
  157. * SW1 and SW2 software conversions also called asynchronous or GPC request.
  158. */
  159. static
  160. const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
  161. [TWL4030_MADC_RT] = {
  162. .sel = TWL4030_MADC_RTSELECT_LSB,
  163. .avg = TWL4030_MADC_RTAVERAGE_LSB,
  164. .rbase = TWL4030_MADC_RTCH0_LSB,
  165. },
  166. [TWL4030_MADC_SW1] = {
  167. .sel = TWL4030_MADC_SW1SELECT_LSB,
  168. .avg = TWL4030_MADC_SW1AVERAGE_LSB,
  169. .rbase = TWL4030_MADC_GPCH0_LSB,
  170. .ctrl = TWL4030_MADC_CTRL_SW1,
  171. },
  172. [TWL4030_MADC_SW2] = {
  173. .sel = TWL4030_MADC_SW2SELECT_LSB,
  174. .avg = TWL4030_MADC_SW2AVERAGE_LSB,
  175. .rbase = TWL4030_MADC_GPCH0_LSB,
  176. .ctrl = TWL4030_MADC_CTRL_SW2,
  177. },
  178. };
  179. /**
  180. * twl4030_madc_channel_raw_read() - Function to read a particular channel value
  181. * @madc: pointer to struct twl4030_madc_data
  182. * @reg: lsb of ADC Channel
  183. *
  184. * Return: 0 on success, an error code otherwise.
  185. */
  186. static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
  187. {
  188. u16 val;
  189. int ret;
  190. /*
  191. * For each ADC channel, we have MSB and LSB register pair. MSB address
  192. * is always LSB address+1. reg parameter is the address of LSB register
  193. */
  194. ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
  195. if (ret) {
  196. dev_err(madc->dev, "unable to read register 0x%X\n", reg);
  197. return ret;
  198. }
  199. return (int)(val >> 6);
  200. }
  201. /*
  202. * Return battery temperature in degrees Celsius
  203. * Or < 0 on failure.
  204. */
  205. static int twl4030battery_temperature(int raw_volt)
  206. {
  207. u8 val;
  208. int temp, curr, volt, res, ret;
  209. volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
  210. /* Getting and calculating the supply current in micro amperes */
  211. ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
  212. REG_BCICTL2);
  213. if (ret < 0)
  214. return ret;
  215. curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
  216. /* Getting and calculating the thermistor resistance in ohms */
  217. res = volt * 1000 / curr;
  218. /* calculating temperature */
  219. for (temp = 58; temp >= 0; temp--) {
  220. int actual = twl4030_therm_tbl[temp];
  221. if ((actual - res) >= 0)
  222. break;
  223. }
  224. return temp + 1;
  225. }
  226. static int twl4030battery_current(int raw_volt)
  227. {
  228. int ret;
  229. u8 val;
  230. ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
  231. TWL4030_BCI_BCICTL1);
  232. if (ret)
  233. return ret;
  234. if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
  235. return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
  236. else /* slope of 0.88 mV/mA */
  237. return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
  238. }
  239. /*
  240. * Function to read channel values
  241. * @madc - pointer to twl4030_madc_data struct
  242. * @reg_base - Base address of the first channel
  243. * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
  244. * @buf - The channel values are stored here. if read fails error
  245. * @raw - Return raw values without conversion
  246. * value is stored
  247. * Returns the number of successfully read channels.
  248. */
  249. static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  250. u8 reg_base, unsigned
  251. long channels, int *buf,
  252. bool raw)
  253. {
  254. int count = 0;
  255. int i;
  256. u8 reg;
  257. for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
  258. reg = reg_base + (2 * i);
  259. buf[i] = twl4030_madc_channel_raw_read(madc, reg);
  260. if (buf[i] < 0) {
  261. dev_err(madc->dev, "Unable to read register 0x%X\n",
  262. reg);
  263. return buf[i];
  264. }
  265. if (raw) {
  266. count++;
  267. continue;
  268. }
  269. switch (i) {
  270. case 10:
  271. buf[i] = twl4030battery_current(buf[i]);
  272. if (buf[i] < 0) {
  273. dev_err(madc->dev, "err reading current\n");
  274. return buf[i];
  275. } else {
  276. count++;
  277. buf[i] = buf[i] - 750;
  278. }
  279. break;
  280. case 1:
  281. buf[i] = twl4030battery_temperature(buf[i]);
  282. if (buf[i] < 0) {
  283. dev_err(madc->dev, "err reading temperature\n");
  284. return buf[i];
  285. } else {
  286. buf[i] -= 3;
  287. count++;
  288. }
  289. break;
  290. default:
  291. count++;
  292. /* Analog Input (V) = conv_result * step_size / R
  293. * conv_result = decimal value of 10-bit conversion
  294. * result
  295. * step size = 1.5 / (2 ^ 10 -1)
  296. * R = Prescaler ratio for input channels.
  297. * Result given in mV hence multiplied by 1000.
  298. */
  299. buf[i] = (buf[i] * 3 * 1000 *
  300. twl4030_divider_ratios[i].denominator)
  301. / (2 * 1023 *
  302. twl4030_divider_ratios[i].numerator);
  303. }
  304. }
  305. return count;
  306. }
  307. /*
  308. * Enables irq.
  309. * @madc - pointer to twl4030_madc_data struct
  310. * @id - irq number to be enabled
  311. * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
  312. * corresponding to RT, SW1, SW2 conversion requests.
  313. * If the i2c read fails it returns an error else returns 0.
  314. */
  315. static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
  316. {
  317. u8 val;
  318. int ret;
  319. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
  320. if (ret) {
  321. dev_err(madc->dev, "unable to read imr register 0x%X\n",
  322. madc->imr);
  323. return ret;
  324. }
  325. val &= ~(1 << id);
  326. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
  327. if (ret) {
  328. dev_err(madc->dev,
  329. "unable to write imr register 0x%X\n", madc->imr);
  330. return ret;
  331. }
  332. return 0;
  333. }
  334. /*
  335. * Disables irq.
  336. * @madc - pointer to twl4030_madc_data struct
  337. * @id - irq number to be disabled
  338. * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
  339. * corresponding to RT, SW1, SW2 conversion requests.
  340. * Returns error if i2c read/write fails.
  341. */
  342. static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
  343. {
  344. u8 val;
  345. int ret;
  346. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
  347. if (ret) {
  348. dev_err(madc->dev, "unable to read imr register 0x%X\n",
  349. madc->imr);
  350. return ret;
  351. }
  352. val |= (1 << id);
  353. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
  354. if (ret) {
  355. dev_err(madc->dev,
  356. "unable to write imr register 0x%X\n", madc->imr);
  357. return ret;
  358. }
  359. return 0;
  360. }
  361. static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
  362. {
  363. struct twl4030_madc_data *madc = _madc;
  364. const struct twl4030_madc_conversion_method *method;
  365. u8 isr_val, imr_val;
  366. int i, len, ret;
  367. struct twl4030_madc_request *r;
  368. mutex_lock(&madc->lock);
  369. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
  370. if (ret) {
  371. dev_err(madc->dev, "unable to read isr register 0x%X\n",
  372. madc->isr);
  373. goto err_i2c;
  374. }
  375. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
  376. if (ret) {
  377. dev_err(madc->dev, "unable to read imr register 0x%X\n",
  378. madc->imr);
  379. goto err_i2c;
  380. }
  381. isr_val &= ~imr_val;
  382. for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
  383. if (!(isr_val & (1 << i)))
  384. continue;
  385. ret = twl4030_madc_disable_irq(madc, i);
  386. if (ret < 0)
  387. dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
  388. madc->requests[i].result_pending = 1;
  389. }
  390. for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
  391. r = &madc->requests[i];
  392. /* No pending results for this method, move to next one */
  393. if (!r->result_pending)
  394. continue;
  395. method = &twl4030_conversion_methods[r->method];
  396. /* Read results */
  397. len = twl4030_madc_read_channels(madc, method->rbase,
  398. r->channels, r->rbuf, r->raw);
  399. /* Return results to caller */
  400. if (r->func_cb != NULL) {
  401. r->func_cb(len, r->channels, r->rbuf);
  402. r->func_cb = NULL;
  403. }
  404. /* Free request */
  405. r->result_pending = 0;
  406. r->active = 0;
  407. }
  408. mutex_unlock(&madc->lock);
  409. return IRQ_HANDLED;
  410. err_i2c:
  411. /*
  412. * In case of error check whichever request is active
  413. * and service the same.
  414. */
  415. for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
  416. r = &madc->requests[i];
  417. if (r->active == 0)
  418. continue;
  419. method = &twl4030_conversion_methods[r->method];
  420. /* Read results */
  421. len = twl4030_madc_read_channels(madc, method->rbase,
  422. r->channels, r->rbuf, r->raw);
  423. /* Return results to caller */
  424. if (r->func_cb != NULL) {
  425. r->func_cb(len, r->channels, r->rbuf);
  426. r->func_cb = NULL;
  427. }
  428. /* Free request */
  429. r->result_pending = 0;
  430. r->active = 0;
  431. }
  432. mutex_unlock(&madc->lock);
  433. return IRQ_HANDLED;
  434. }
  435. static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
  436. struct twl4030_madc_request *req)
  437. {
  438. struct twl4030_madc_request *p;
  439. int ret;
  440. p = &madc->requests[req->method];
  441. memcpy(p, req, sizeof(*req));
  442. ret = twl4030_madc_enable_irq(madc, req->method);
  443. if (ret < 0) {
  444. dev_err(madc->dev, "enable irq failed!!\n");
  445. return ret;
  446. }
  447. return 0;
  448. }
  449. /*
  450. * Function which enables the madc conversion
  451. * by writing to the control register.
  452. * @madc - pointer to twl4030_madc_data struct
  453. * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
  454. * corresponding to RT SW1 or SW2 conversion methods.
  455. * Returns 0 if succeeds else a negative error value
  456. */
  457. static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
  458. int conv_method)
  459. {
  460. const struct twl4030_madc_conversion_method *method;
  461. int ret = 0;
  462. if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
  463. return -ENOTSUPP;
  464. method = &twl4030_conversion_methods[conv_method];
  465. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
  466. method->ctrl);
  467. if (ret) {
  468. dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
  469. method->ctrl);
  470. return ret;
  471. }
  472. return 0;
  473. }
  474. /*
  475. * Function that waits for conversion to be ready
  476. * @madc - pointer to twl4030_madc_data struct
  477. * @timeout_ms - timeout value in milliseconds
  478. * @status_reg - ctrl register
  479. * returns 0 if succeeds else a negative error value
  480. */
  481. static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
  482. unsigned int timeout_ms,
  483. u8 status_reg)
  484. {
  485. unsigned long timeout;
  486. int ret;
  487. timeout = jiffies + msecs_to_jiffies(timeout_ms);
  488. do {
  489. u8 reg;
  490. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
  491. if (ret) {
  492. dev_err(madc->dev,
  493. "unable to read status register 0x%X\n",
  494. status_reg);
  495. return ret;
  496. }
  497. if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
  498. return 0;
  499. usleep_range(500, 2000);
  500. } while (!time_after(jiffies, timeout));
  501. dev_err(madc->dev, "conversion timeout!\n");
  502. return -EAGAIN;
  503. }
  504. /*
  505. * An exported function which can be called from other kernel drivers.
  506. * @req twl4030_madc_request structure
  507. * req->rbuf will be filled with read values of channels based on the
  508. * channel index. If a particular channel reading fails there will
  509. * be a negative error value in the corresponding array element.
  510. * returns 0 if succeeds else error value
  511. */
  512. int twl4030_madc_conversion(struct twl4030_madc_request *req)
  513. {
  514. const struct twl4030_madc_conversion_method *method;
  515. int ret;
  516. if (!req || !twl4030_madc)
  517. return -EINVAL;
  518. mutex_lock(&twl4030_madc->lock);
  519. if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
  520. ret = -EINVAL;
  521. goto out;
  522. }
  523. /* Do we have a conversion request ongoing */
  524. if (twl4030_madc->requests[req->method].active) {
  525. ret = -EBUSY;
  526. goto out;
  527. }
  528. method = &twl4030_conversion_methods[req->method];
  529. /* Select channels to be converted */
  530. ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
  531. if (ret) {
  532. dev_err(twl4030_madc->dev,
  533. "unable to write sel register 0x%X\n", method->sel);
  534. goto out;
  535. }
  536. /* Select averaging for all channels if do_avg is set */
  537. if (req->do_avg) {
  538. ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
  539. method->avg);
  540. if (ret) {
  541. dev_err(twl4030_madc->dev,
  542. "unable to write avg register 0x%X\n",
  543. method->avg);
  544. goto out;
  545. }
  546. }
  547. if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
  548. ret = twl4030_madc_set_irq(twl4030_madc, req);
  549. if (ret < 0)
  550. goto out;
  551. ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
  552. if (ret < 0)
  553. goto out;
  554. twl4030_madc->requests[req->method].active = 1;
  555. ret = 0;
  556. goto out;
  557. }
  558. /* With RT method we should not be here anymore */
  559. if (req->method == TWL4030_MADC_RT) {
  560. ret = -EINVAL;
  561. goto out;
  562. }
  563. ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
  564. if (ret < 0)
  565. goto out;
  566. twl4030_madc->requests[req->method].active = 1;
  567. /* Wait until conversion is ready (ctrl register returns EOC) */
  568. ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
  569. if (ret) {
  570. twl4030_madc->requests[req->method].active = 0;
  571. goto out;
  572. }
  573. ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
  574. req->channels, req->rbuf, req->raw);
  575. twl4030_madc->requests[req->method].active = 0;
  576. out:
  577. mutex_unlock(&twl4030_madc->lock);
  578. return ret;
  579. }
  580. EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
  581. int twl4030_get_madc_conversion(int channel_no)
  582. {
  583. struct twl4030_madc_request req;
  584. int temp = 0;
  585. int ret;
  586. req.channels = (1 << channel_no);
  587. req.method = TWL4030_MADC_SW2;
  588. req.active = 0;
  589. req.raw = 0;
  590. req.func_cb = NULL;
  591. ret = twl4030_madc_conversion(&req);
  592. if (ret < 0)
  593. return ret;
  594. if (req.rbuf[channel_no] > 0)
  595. temp = req.rbuf[channel_no];
  596. return temp;
  597. }
  598. EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
  599. /**
  600. * twl4030_madc_set_current_generator() - setup bias current
  601. *
  602. * @madc: pointer to twl4030_madc_data struct
  603. * @chan: can be one of the two values:
  604. * TWL4030_BCI_ITHEN
  605. * Enables bias current for main battery type reading
  606. * TWL4030_BCI_TYPEN
  607. * Enables bias current for main battery temperature sensing
  608. * @on: enable or disable chan.
  609. *
  610. * Function to enable or disable bias current for
  611. * main battery type reading or temperature sensing
  612. */
  613. static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  614. int chan, int on)
  615. {
  616. int ret;
  617. int regmask;
  618. u8 regval;
  619. ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
  620. &regval, TWL4030_BCI_BCICTL1);
  621. if (ret) {
  622. dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
  623. TWL4030_BCI_BCICTL1);
  624. return ret;
  625. }
  626. regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
  627. if (on)
  628. regval |= regmask;
  629. else
  630. regval &= ~regmask;
  631. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  632. regval, TWL4030_BCI_BCICTL1);
  633. if (ret) {
  634. dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
  635. TWL4030_BCI_BCICTL1);
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. /*
  641. * Function that sets MADC software power on bit to enable MADC
  642. * @madc - pointer to twl4030_madc_data struct
  643. * @on - Enable or disable MADC software power on bit.
  644. * returns error if i2c read/write fails else 0
  645. */
  646. static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
  647. {
  648. u8 regval;
  649. int ret;
  650. ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
  651. &regval, TWL4030_MADC_CTRL1);
  652. if (ret) {
  653. dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
  654. TWL4030_MADC_CTRL1);
  655. return ret;
  656. }
  657. if (on)
  658. regval |= TWL4030_MADC_MADCON;
  659. else
  660. regval &= ~TWL4030_MADC_MADCON;
  661. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
  662. if (ret) {
  663. dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
  664. TWL4030_MADC_CTRL1);
  665. return ret;
  666. }
  667. return 0;
  668. }
  669. /*
  670. * Initialize MADC and request for threaded irq
  671. */
  672. static int twl4030_madc_probe(struct platform_device *pdev)
  673. {
  674. struct twl4030_madc_data *madc;
  675. struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
  676. struct device_node *np = pdev->dev.of_node;
  677. int irq, ret;
  678. u8 regval;
  679. struct iio_dev *iio_dev = NULL;
  680. if (!pdata && !np) {
  681. dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n");
  682. return -EINVAL;
  683. }
  684. iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc));
  685. if (!iio_dev) {
  686. dev_err(&pdev->dev, "failed allocating iio device\n");
  687. return -ENOMEM;
  688. }
  689. madc = iio_priv(iio_dev);
  690. madc->dev = &pdev->dev;
  691. iio_dev->name = dev_name(&pdev->dev);
  692. iio_dev->dev.parent = &pdev->dev;
  693. iio_dev->dev.of_node = pdev->dev.of_node;
  694. iio_dev->info = &twl4030_madc_iio_info;
  695. iio_dev->modes = INDIO_DIRECT_MODE;
  696. iio_dev->channels = twl4030_madc_iio_channels;
  697. iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
  698. /*
  699. * Phoenix provides 2 interrupt lines. The first one is connected to
  700. * the OMAP. The other one can be connected to the other processor such
  701. * as modem. Hence two separate ISR and IMR registers.
  702. */
  703. if (pdata)
  704. madc->use_second_irq = (pdata->irq_line != 1);
  705. else
  706. madc->use_second_irq = of_property_read_bool(np,
  707. "ti,system-uses-second-madc-irq");
  708. madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
  709. TWL4030_MADC_IMR1;
  710. madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
  711. TWL4030_MADC_ISR1;
  712. ret = twl4030_madc_set_power(madc, 1);
  713. if (ret < 0)
  714. return ret;
  715. ret = twl4030_madc_set_current_generator(madc, 0, 1);
  716. if (ret < 0)
  717. goto err_current_generator;
  718. ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
  719. &regval, TWL4030_BCI_BCICTL1);
  720. if (ret) {
  721. dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
  722. TWL4030_BCI_BCICTL1);
  723. goto err_i2c;
  724. }
  725. regval |= TWL4030_BCI_MESBAT;
  726. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  727. regval, TWL4030_BCI_BCICTL1);
  728. if (ret) {
  729. dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
  730. TWL4030_BCI_BCICTL1);
  731. goto err_i2c;
  732. }
  733. /* Check that MADC clock is on */
  734. ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
  735. if (ret) {
  736. dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
  737. TWL4030_REG_GPBR1);
  738. goto err_i2c;
  739. }
  740. /* If MADC clk is not on, turn it on */
  741. if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
  742. dev_info(&pdev->dev, "clk disabled, enabling\n");
  743. regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
  744. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
  745. TWL4030_REG_GPBR1);
  746. if (ret) {
  747. dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
  748. TWL4030_REG_GPBR1);
  749. goto err_i2c;
  750. }
  751. }
  752. platform_set_drvdata(pdev, iio_dev);
  753. mutex_init(&madc->lock);
  754. irq = platform_get_irq(pdev, 0);
  755. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  756. twl4030_madc_threaded_irq_handler,
  757. IRQF_TRIGGER_RISING, "twl4030_madc", madc);
  758. if (ret) {
  759. dev_err(&pdev->dev, "could not request irq\n");
  760. goto err_i2c;
  761. }
  762. twl4030_madc = madc;
  763. ret = iio_device_register(iio_dev);
  764. if (ret) {
  765. dev_err(&pdev->dev, "could not register iio device\n");
  766. goto err_i2c;
  767. }
  768. return 0;
  769. err_i2c:
  770. twl4030_madc_set_current_generator(madc, 0, 0);
  771. err_current_generator:
  772. twl4030_madc_set_power(madc, 0);
  773. return ret;
  774. }
  775. static int twl4030_madc_remove(struct platform_device *pdev)
  776. {
  777. struct iio_dev *iio_dev = platform_get_drvdata(pdev);
  778. struct twl4030_madc_data *madc = iio_priv(iio_dev);
  779. iio_device_unregister(iio_dev);
  780. twl4030_madc_set_current_generator(madc, 0, 0);
  781. twl4030_madc_set_power(madc, 0);
  782. return 0;
  783. }
  784. #ifdef CONFIG_OF
  785. static const struct of_device_id twl_madc_of_match[] = {
  786. { .compatible = "ti,twl4030-madc", },
  787. { },
  788. };
  789. MODULE_DEVICE_TABLE(of, twl_madc_of_match);
  790. #endif
  791. static struct platform_driver twl4030_madc_driver = {
  792. .probe = twl4030_madc_probe,
  793. .remove = twl4030_madc_remove,
  794. .driver = {
  795. .name = "twl4030_madc",
  796. .of_match_table = of_match_ptr(twl_madc_of_match),
  797. },
  798. };
  799. module_platform_driver(twl4030_madc_driver);
  800. MODULE_DESCRIPTION("TWL4030 ADC driver");
  801. MODULE_LICENSE("GPL");
  802. MODULE_AUTHOR("J Keerthy");
  803. MODULE_ALIAS("platform:twl4030_madc");