twl4030-madc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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(TWL4030_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(TWL4030_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. * value is stored
  199. * Returns the number of successfully read channels.
  200. */
  201. static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  202. u8 reg_base, unsigned
  203. long channels, int *buf)
  204. {
  205. int count = 0, count_req = 0, i;
  206. u8 reg;
  207. for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
  208. reg = reg_base + 2 * i;
  209. buf[i] = twl4030_madc_channel_raw_read(madc, reg);
  210. if (buf[i] < 0) {
  211. dev_err(madc->dev,
  212. "Unable to read register 0x%X\n", reg);
  213. count_req++;
  214. continue;
  215. }
  216. switch (i) {
  217. case 10:
  218. buf[i] = twl4030battery_current(buf[i]);
  219. if (buf[i] < 0) {
  220. dev_err(madc->dev, "err reading current\n");
  221. count_req++;
  222. } else {
  223. count++;
  224. buf[i] = buf[i] - 750;
  225. }
  226. break;
  227. case 1:
  228. buf[i] = twl4030battery_temperature(buf[i]);
  229. if (buf[i] < 0) {
  230. dev_err(madc->dev, "err reading temperature\n");
  231. count_req++;
  232. } else {
  233. buf[i] -= 3;
  234. count++;
  235. }
  236. break;
  237. default:
  238. count++;
  239. /* Analog Input (V) = conv_result * step_size / R
  240. * conv_result = decimal value of 10-bit conversion
  241. * result
  242. * step size = 1.5 / (2 ^ 10 -1)
  243. * R = Prescaler ratio for input channels.
  244. * Result given in mV hence multiplied by 1000.
  245. */
  246. buf[i] = (buf[i] * 3 * 1000 *
  247. twl4030_divider_ratios[i].denominator)
  248. / (2 * 1023 *
  249. twl4030_divider_ratios[i].numerator);
  250. }
  251. }
  252. if (count_req)
  253. dev_err(madc->dev, "%d channel conversion failed\n", count_req);
  254. return count;
  255. }
  256. /*
  257. * Enables irq.
  258. * @madc - pointer to twl4030_madc_data struct
  259. * @id - irq number to be enabled
  260. * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
  261. * corresponding to RT, SW1, SW2 conversion requests.
  262. * If the i2c read fails it returns an error else returns 0.
  263. */
  264. static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
  265. {
  266. u8 val;
  267. int ret;
  268. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
  269. if (ret) {
  270. dev_err(madc->dev, "unable to read imr register 0x%X\n",
  271. madc->imr);
  272. return ret;
  273. }
  274. val &= ~(1 << id);
  275. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
  276. if (ret) {
  277. dev_err(madc->dev,
  278. "unable to write imr register 0x%X\n", madc->imr);
  279. return ret;
  280. }
  281. return 0;
  282. }
  283. /*
  284. * Disables irq.
  285. * @madc - pointer to twl4030_madc_data struct
  286. * @id - irq number to be disabled
  287. * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
  288. * corresponding to RT, SW1, SW2 conversion requests.
  289. * Returns error if i2c read/write fails.
  290. */
  291. static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
  292. {
  293. u8 val;
  294. int ret;
  295. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
  296. if (ret) {
  297. dev_err(madc->dev, "unable to read imr register 0x%X\n",
  298. madc->imr);
  299. return ret;
  300. }
  301. val |= (1 << id);
  302. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
  303. if (ret) {
  304. dev_err(madc->dev,
  305. "unable to write imr register 0x%X\n", madc->imr);
  306. return ret;
  307. }
  308. return 0;
  309. }
  310. static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
  311. {
  312. struct twl4030_madc_data *madc = _madc;
  313. const struct twl4030_madc_conversion_method *method;
  314. u8 isr_val, imr_val;
  315. int i, len, ret;
  316. struct twl4030_madc_request *r;
  317. mutex_lock(&madc->lock);
  318. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
  319. if (ret) {
  320. dev_err(madc->dev, "unable to read isr register 0x%X\n",
  321. madc->isr);
  322. goto err_i2c;
  323. }
  324. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
  325. if (ret) {
  326. dev_err(madc->dev, "unable to read imr register 0x%X\n",
  327. madc->imr);
  328. goto err_i2c;
  329. }
  330. isr_val &= ~imr_val;
  331. for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
  332. if (!(isr_val & (1 << i)))
  333. continue;
  334. ret = twl4030_madc_disable_irq(madc, i);
  335. if (ret < 0)
  336. dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
  337. madc->requests[i].result_pending = 1;
  338. }
  339. for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
  340. r = &madc->requests[i];
  341. /* No pending results for this method, move to next one */
  342. if (!r->result_pending)
  343. continue;
  344. method = &twl4030_conversion_methods[r->method];
  345. /* Read results */
  346. len = twl4030_madc_read_channels(madc, method->rbase,
  347. r->channels, r->rbuf);
  348. /* Return results to caller */
  349. if (r->func_cb != NULL) {
  350. r->func_cb(len, r->channels, r->rbuf);
  351. r->func_cb = NULL;
  352. }
  353. /* Free request */
  354. r->result_pending = 0;
  355. r->active = 0;
  356. }
  357. mutex_unlock(&madc->lock);
  358. return IRQ_HANDLED;
  359. err_i2c:
  360. /*
  361. * In case of error check whichever request is active
  362. * and service the same.
  363. */
  364. for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
  365. r = &madc->requests[i];
  366. if (r->active == 0)
  367. continue;
  368. method = &twl4030_conversion_methods[r->method];
  369. /* Read results */
  370. len = twl4030_madc_read_channels(madc, method->rbase,
  371. r->channels, r->rbuf);
  372. /* Return results to caller */
  373. if (r->func_cb != NULL) {
  374. r->func_cb(len, r->channels, r->rbuf);
  375. r->func_cb = NULL;
  376. }
  377. /* Free request */
  378. r->result_pending = 0;
  379. r->active = 0;
  380. }
  381. mutex_unlock(&madc->lock);
  382. return IRQ_HANDLED;
  383. }
  384. static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
  385. struct twl4030_madc_request *req)
  386. {
  387. struct twl4030_madc_request *p;
  388. int ret;
  389. p = &madc->requests[req->method];
  390. memcpy(p, req, sizeof(*req));
  391. ret = twl4030_madc_enable_irq(madc, req->method);
  392. if (ret < 0) {
  393. dev_err(madc->dev, "enable irq failed!!\n");
  394. return ret;
  395. }
  396. return 0;
  397. }
  398. /*
  399. * Function which enables the madc conversion
  400. * by writing to the control register.
  401. * @madc - pointer to twl4030_madc_data struct
  402. * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
  403. * corresponding to RT SW1 or SW2 conversion methods.
  404. * Returns 0 if succeeds else a negative error value
  405. */
  406. static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
  407. int conv_method)
  408. {
  409. const struct twl4030_madc_conversion_method *method;
  410. int ret = 0;
  411. method = &twl4030_conversion_methods[conv_method];
  412. switch (conv_method) {
  413. case TWL4030_MADC_SW1:
  414. case TWL4030_MADC_SW2:
  415. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
  416. TWL4030_MADC_SW_START, method->ctrl);
  417. if (ret) {
  418. dev_err(madc->dev,
  419. "unable to write ctrl register 0x%X\n",
  420. method->ctrl);
  421. return ret;
  422. }
  423. break;
  424. default:
  425. break;
  426. }
  427. return 0;
  428. }
  429. /*
  430. * Function that waits for conversion to be ready
  431. * @madc - pointer to twl4030_madc_data struct
  432. * @timeout_ms - timeout value in milliseconds
  433. * @status_reg - ctrl register
  434. * returns 0 if succeeds else a negative error value
  435. */
  436. static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
  437. unsigned int timeout_ms,
  438. u8 status_reg)
  439. {
  440. unsigned long timeout;
  441. int ret;
  442. timeout = jiffies + msecs_to_jiffies(timeout_ms);
  443. do {
  444. u8 reg;
  445. ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
  446. if (ret) {
  447. dev_err(madc->dev,
  448. "unable to read status register 0x%X\n",
  449. status_reg);
  450. return ret;
  451. }
  452. if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
  453. return 0;
  454. usleep_range(500, 2000);
  455. } while (!time_after(jiffies, timeout));
  456. dev_err(madc->dev, "conversion timeout!\n");
  457. return -EAGAIN;
  458. }
  459. /*
  460. * An exported function which can be called from other kernel drivers.
  461. * @req twl4030_madc_request structure
  462. * req->rbuf will be filled with read values of channels based on the
  463. * channel index. If a particular channel reading fails there will
  464. * be a negative error value in the corresponding array element.
  465. * returns 0 if succeeds else error value
  466. */
  467. int twl4030_madc_conversion(struct twl4030_madc_request *req)
  468. {
  469. const struct twl4030_madc_conversion_method *method;
  470. u8 ch_msb, ch_lsb;
  471. int ret;
  472. if (!req || !twl4030_madc)
  473. return -EINVAL;
  474. mutex_lock(&twl4030_madc->lock);
  475. if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
  476. ret = -EINVAL;
  477. goto out;
  478. }
  479. /* Do we have a conversion request ongoing */
  480. if (twl4030_madc->requests[req->method].active) {
  481. ret = -EBUSY;
  482. goto out;
  483. }
  484. ch_msb = (req->channels >> 8) & 0xff;
  485. ch_lsb = req->channels & 0xff;
  486. method = &twl4030_conversion_methods[req->method];
  487. /* Select channels to be converted */
  488. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
  489. if (ret) {
  490. dev_err(twl4030_madc->dev,
  491. "unable to write sel register 0x%X\n", method->sel + 1);
  492. goto out;
  493. }
  494. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
  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. /* Select averaging for all channels if do_avg is set */
  501. if (req->do_avg) {
  502. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
  503. ch_msb, method->avg + 1);
  504. if (ret) {
  505. dev_err(twl4030_madc->dev,
  506. "unable to write avg register 0x%X\n",
  507. method->avg + 1);
  508. goto out;
  509. }
  510. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
  511. ch_lsb, method->avg);
  512. if (ret) {
  513. dev_err(twl4030_madc->dev,
  514. "unable to write sel reg 0x%X\n",
  515. method->sel + 1);
  516. goto out;
  517. }
  518. }
  519. if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
  520. ret = twl4030_madc_set_irq(twl4030_madc, req);
  521. if (ret < 0)
  522. goto out;
  523. ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
  524. if (ret < 0)
  525. goto out;
  526. twl4030_madc->requests[req->method].active = 1;
  527. ret = 0;
  528. goto out;
  529. }
  530. /* With RT method we should not be here anymore */
  531. if (req->method == TWL4030_MADC_RT) {
  532. ret = -EINVAL;
  533. goto out;
  534. }
  535. ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
  536. if (ret < 0)
  537. goto out;
  538. twl4030_madc->requests[req->method].active = 1;
  539. /* Wait until conversion is ready (ctrl register returns EOC) */
  540. ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
  541. if (ret) {
  542. twl4030_madc->requests[req->method].active = 0;
  543. goto out;
  544. }
  545. ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
  546. req->channels, req->rbuf);
  547. twl4030_madc->requests[req->method].active = 0;
  548. out:
  549. mutex_unlock(&twl4030_madc->lock);
  550. return ret;
  551. }
  552. EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
  553. /*
  554. * Return channel value
  555. * Or < 0 on failure.
  556. */
  557. int twl4030_get_madc_conversion(int channel_no)
  558. {
  559. struct twl4030_madc_request req;
  560. int temp = 0;
  561. int ret;
  562. req.channels = (1 << channel_no);
  563. req.method = TWL4030_MADC_SW2;
  564. req.active = 0;
  565. req.func_cb = NULL;
  566. ret = twl4030_madc_conversion(&req);
  567. if (ret < 0)
  568. return ret;
  569. if (req.rbuf[channel_no] > 0)
  570. temp = req.rbuf[channel_no];
  571. return temp;
  572. }
  573. EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
  574. /*
  575. * Function to enable or disable bias current for
  576. * main battery type reading or temperature sensing
  577. * @madc - pointer to twl4030_madc_data struct
  578. * @chan - can be one of the two values
  579. * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
  580. * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature
  581. * sensing
  582. * @on - enable or disable chan.
  583. */
  584. static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  585. int chan, int on)
  586. {
  587. int ret;
  588. u8 regval;
  589. ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
  590. &regval, TWL4030_BCI_BCICTL1);
  591. if (ret) {
  592. dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
  593. TWL4030_BCI_BCICTL1);
  594. return ret;
  595. }
  596. if (on)
  597. regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
  598. else
  599. regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
  600. ret = twl_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE,
  601. regval, TWL4030_BCI_BCICTL1);
  602. if (ret) {
  603. dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
  604. TWL4030_BCI_BCICTL1);
  605. return ret;
  606. }
  607. return 0;
  608. }
  609. /*
  610. * Function that sets MADC software power on bit to enable MADC
  611. * @madc - pointer to twl4030_madc_data struct
  612. * @on - Enable or disable MADC software powen on bit.
  613. * returns error if i2c read/write fails else 0
  614. */
  615. static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
  616. {
  617. u8 regval;
  618. int ret;
  619. ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
  620. &regval, TWL4030_MADC_CTRL1);
  621. if (ret) {
  622. dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
  623. TWL4030_MADC_CTRL1);
  624. return ret;
  625. }
  626. if (on)
  627. regval |= TWL4030_MADC_MADCON;
  628. else
  629. regval &= ~TWL4030_MADC_MADCON;
  630. ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
  631. if (ret) {
  632. dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
  633. TWL4030_MADC_CTRL1);
  634. return ret;
  635. }
  636. return 0;
  637. }
  638. /*
  639. * Initialize MADC and request for threaded irq
  640. */
  641. static int __devinit twl4030_madc_probe(struct platform_device *pdev)
  642. {
  643. struct twl4030_madc_data *madc;
  644. struct twl4030_madc_platform_data *pdata = pdev->dev.platform_data;
  645. int ret;
  646. u8 regval;
  647. if (!pdata) {
  648. dev_err(&pdev->dev, "platform_data not available\n");
  649. return -EINVAL;
  650. }
  651. madc = kzalloc(sizeof(*madc), GFP_KERNEL);
  652. if (!madc)
  653. return -ENOMEM;
  654. madc->dev = &pdev->dev;
  655. /*
  656. * Phoenix provides 2 interrupt lines. The first one is connected to
  657. * the OMAP. The other one can be connected to the other processor such
  658. * as modem. Hence two separate ISR and IMR registers.
  659. */
  660. madc->imr = (pdata->irq_line == 1) ?
  661. TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
  662. madc->isr = (pdata->irq_line == 1) ?
  663. TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
  664. ret = twl4030_madc_set_power(madc, 1);
  665. if (ret < 0)
  666. goto err_power;
  667. ret = twl4030_madc_set_current_generator(madc, 0, 1);
  668. if (ret < 0)
  669. goto err_current_generator;
  670. ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
  671. &regval, TWL4030_BCI_BCICTL1);
  672. if (ret) {
  673. dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
  674. TWL4030_BCI_BCICTL1);
  675. goto err_i2c;
  676. }
  677. regval |= TWL4030_BCI_MESBAT;
  678. ret = twl_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE,
  679. regval, TWL4030_BCI_BCICTL1);
  680. if (ret) {
  681. dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
  682. TWL4030_BCI_BCICTL1);
  683. goto err_i2c;
  684. }
  685. /* Check that MADC clock is on */
  686. ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
  687. if (ret) {
  688. dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
  689. TWL4030_REG_GPBR1);
  690. goto err_i2c;
  691. }
  692. /* If MADC clk is not on, turn it on */
  693. if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
  694. dev_info(&pdev->dev, "clk disabled, enabling\n");
  695. regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
  696. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
  697. TWL4030_REG_GPBR1);
  698. if (ret) {
  699. dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
  700. TWL4030_REG_GPBR1);
  701. goto err_i2c;
  702. }
  703. }
  704. platform_set_drvdata(pdev, madc);
  705. mutex_init(&madc->lock);
  706. ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
  707. twl4030_madc_threaded_irq_handler,
  708. IRQF_TRIGGER_RISING, "twl4030_madc", madc);
  709. if (ret) {
  710. dev_dbg(&pdev->dev, "could not request irq\n");
  711. goto err_irq;
  712. }
  713. twl4030_madc = madc;
  714. return 0;
  715. err_irq:
  716. platform_set_drvdata(pdev, NULL);
  717. err_i2c:
  718. twl4030_madc_set_current_generator(madc, 0, 0);
  719. err_current_generator:
  720. twl4030_madc_set_power(madc, 0);
  721. err_power:
  722. kfree(madc);
  723. return ret;
  724. }
  725. static int __devexit twl4030_madc_remove(struct platform_device *pdev)
  726. {
  727. struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
  728. free_irq(platform_get_irq(pdev, 0), madc);
  729. platform_set_drvdata(pdev, NULL);
  730. twl4030_madc_set_current_generator(madc, 0, 0);
  731. twl4030_madc_set_power(madc, 0);
  732. kfree(madc);
  733. return 0;
  734. }
  735. static struct platform_driver twl4030_madc_driver = {
  736. .probe = twl4030_madc_probe,
  737. .remove = __exit_p(twl4030_madc_remove),
  738. .driver = {
  739. .name = "twl4030_madc",
  740. .owner = THIS_MODULE,
  741. },
  742. };
  743. module_platform_driver(twl4030_madc_driver);
  744. MODULE_DESCRIPTION("TWL4030 ADC driver");
  745. MODULE_LICENSE("GPL");
  746. MODULE_AUTHOR("J Keerthy");
  747. MODULE_ALIAS("platform:twl4030_madc");