twl4030-madc.c 25 KB

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