twl4030-madc.c 22 KB

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