twl4030-madc.c 25 KB

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