twl4030-madc.c 26 KB

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