twl4030-madc.c 25 KB

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