1 // SPDX-License-Identifier: GPL-2.0-only
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.
8 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
9 * J Keerthy <j-keerthy@ti.com>
11 * Based on twl4030-madc.c
12 * Copyright (C) 2008 Nokia Corporation
13 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
15 * Amit Kucheria <amit.kucheria@canonical.com>
18 #include <linux/device.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/mfd/twl.h>
25 #include <linux/module.h>
26 #include <linux/stddef.h>
27 #include <linux/mutex.h>
28 #include <linux/bitops.h>
29 #include <linux/jiffies.h>
30 #include <linux/types.h>
31 #include <linux/gfp.h>
32 #include <linux/err.h>
34 #include <linux/regulator/consumer.h>
36 #include <linux/iio/iio.h>
38 #define TWL4030_MADC_MAX_CHANNELS 16
40 #define TWL4030_MADC_CTRL1 0x00
41 #define TWL4030_MADC_CTRL2 0x01
43 #define TWL4030_MADC_RTSELECT_LSB 0x02
44 #define TWL4030_MADC_SW1SELECT_LSB 0x06
45 #define TWL4030_MADC_SW2SELECT_LSB 0x0A
47 #define TWL4030_MADC_RTAVERAGE_LSB 0x04
48 #define TWL4030_MADC_SW1AVERAGE_LSB 0x08
49 #define TWL4030_MADC_SW2AVERAGE_LSB 0x0C
51 #define TWL4030_MADC_CTRL_SW1 0x12
52 #define TWL4030_MADC_CTRL_SW2 0x13
54 #define TWL4030_MADC_RTCH0_LSB 0x17
55 #define TWL4030_MADC_GPCH0_LSB 0x37
57 #define TWL4030_MADC_MADCON (1 << 0) /* MADC power on */
58 #define TWL4030_MADC_BUSY (1 << 0) /* MADC busy */
59 /* MADC conversion completion */
60 #define TWL4030_MADC_EOC_SW (1 << 1)
61 /* MADC SWx start conversion */
62 #define TWL4030_MADC_SW_START (1 << 5)
63 #define TWL4030_MADC_ADCIN0 (1 << 0)
64 #define TWL4030_MADC_ADCIN1 (1 << 1)
65 #define TWL4030_MADC_ADCIN2 (1 << 2)
66 #define TWL4030_MADC_ADCIN3 (1 << 3)
67 #define TWL4030_MADC_ADCIN4 (1 << 4)
68 #define TWL4030_MADC_ADCIN5 (1 << 5)
69 #define TWL4030_MADC_ADCIN6 (1 << 6)
70 #define TWL4030_MADC_ADCIN7 (1 << 7)
71 #define TWL4030_MADC_ADCIN8 (1 << 8)
72 #define TWL4030_MADC_ADCIN9 (1 << 9)
73 #define TWL4030_MADC_ADCIN10 (1 << 10)
74 #define TWL4030_MADC_ADCIN11 (1 << 11)
75 #define TWL4030_MADC_ADCIN12 (1 << 12)
76 #define TWL4030_MADC_ADCIN13 (1 << 13)
77 #define TWL4030_MADC_ADCIN14 (1 << 14)
78 #define TWL4030_MADC_ADCIN15 (1 << 15)
81 #define TWL4030_MADC_BTEMP TWL4030_MADC_ADCIN1
82 #define TWL4030_MADC_VBUS TWL4030_MADC_ADCIN8
83 #define TWL4030_MADC_VBKB TWL4030_MADC_ADCIN9
84 #define TWL4030_MADC_ICHG TWL4030_MADC_ADCIN10
85 #define TWL4030_MADC_VCHG TWL4030_MADC_ADCIN11
86 #define TWL4030_MADC_VBAT TWL4030_MADC_ADCIN12
88 /* Step size and prescaler ratio */
89 #define TEMP_STEP_SIZE 147
90 #define TEMP_PSR_R 100
91 #define CURR_STEP_SIZE 147
92 #define CURR_PSR_R1 44
93 #define CURR_PSR_R2 88
95 #define TWL4030_BCI_BCICTL1 0x23
96 #define TWL4030_BCI_CGAIN 0x020
97 #define TWL4030_BCI_MESBAT (1 << 1)
98 #define TWL4030_BCI_TYPEN (1 << 4)
99 #define TWL4030_BCI_ITHEN (1 << 3)
101 #define REG_BCICTL2 0x024
102 #define TWL4030_BCI_ITHSENS 0x007
104 /* Register and bits for GPBR1 register */
105 #define TWL4030_REG_GPBR1 0x0c
106 #define TWL4030_GPBR1_MADC_HFCLK_EN (1 << 7)
108 #define TWL4030_USB_SEL_MADC_MCPC (1<<3)
109 #define TWL4030_USB_CARKIT_ANA_CTRL 0xBB
111 struct twl4030_madc_conversion_method {
119 * struct twl4030_madc_request - madc request packet for channel conversion
120 * @channels: 16 bit bitmap for individual channels
121 * @do_avg: sample the input channel for 4 consecutive cycles
122 * @method: RT, SW1, SW2
123 * @type: Polling or interrupt based method
124 * @active: Flag if request is active
125 * @result_pending: Flag from irq handler, that result is ready
126 * @raw: Return raw value, do not convert it
127 * @rbuf: Result buffer
129 struct twl4030_madc_request {
130 unsigned long channels;
137 int rbuf[TWL4030_MADC_MAX_CHANNELS];
140 enum conversion_methods {
144 TWL4030_MADC_NUM_METHODS
149 TWL4030_MADC_IRQ_ONESHOT,
150 TWL4030_MADC_IRQ_REARM
154 * struct twl4030_madc_data - a container for madc info
155 * @dev: Pointer to device structure for madc
156 * @lock: Mutex protecting this data structure
157 * @usb3v1: Pointer to bias regulator for madc
158 * @requests: Array of request struct corresponding to SW1, SW2 and RT
159 * @use_second_irq: IRQ selection (main or co-processor)
160 * @imr: Interrupt mask register of MADC
161 * @isr: Interrupt status register of MADC
163 struct twl4030_madc_data {
166 struct regulator *usb3v1;
167 struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
173 static int twl4030_madc_conversion(struct twl4030_madc_request *req);
175 static int twl4030_madc_read(struct iio_dev *iio_dev,
176 const struct iio_chan_spec *chan,
177 int *val, int *val2, long mask)
179 struct twl4030_madc_data *madc = iio_priv(iio_dev);
180 struct twl4030_madc_request req;
183 req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
185 req.channels = BIT(chan->channel);
187 req.type = TWL4030_MADC_WAIT;
188 req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
189 req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
191 ret = twl4030_madc_conversion(&req);
195 *val = req.rbuf[chan->channel];
200 static const struct iio_info twl4030_madc_iio_info = {
201 .read_raw = &twl4030_madc_read,
204 #define TWL4030_ADC_CHANNEL(_channel, _type, _name) { \
206 .channel = _channel, \
207 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
208 BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
209 BIT(IIO_CHAN_INFO_PROCESSED), \
210 .datasheet_name = _name, \
214 static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
215 TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0"),
216 TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1"),
217 TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2"),
218 TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3"),
219 TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4"),
220 TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5"),
221 TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6"),
222 TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7"),
223 TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8"),
224 TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9"),
225 TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10"),
226 TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11"),
227 TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12"),
228 TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13"),
229 TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14"),
230 TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15"),
233 static struct twl4030_madc_data *twl4030_madc;
235 static const struct s16_fract twl4030_divider_ratios[16] = {
236 {1, 1}, /* CHANNEL 0 No Prescaler */
237 {1, 1}, /* CHANNEL 1 No Prescaler */
238 {6, 10}, /* CHANNEL 2 */
239 {6, 10}, /* CHANNEL 3 */
240 {6, 10}, /* CHANNEL 4 */
241 {6, 10}, /* CHANNEL 5 */
242 {6, 10}, /* CHANNEL 6 */
243 {6, 10}, /* CHANNEL 7 */
244 {3, 14}, /* CHANNEL 8 */
245 {1, 3}, /* CHANNEL 9 */
246 {1, 1}, /* CHANNEL 10 No Prescaler */
247 {15, 100}, /* CHANNEL 11 */
248 {1, 4}, /* CHANNEL 12 */
249 {1, 1}, /* CHANNEL 13 Reserved channels */
250 {1, 1}, /* CHANNEL 14 Reseved channels */
251 {5, 11}, /* CHANNEL 15 */
254 /* Conversion table from -3 to 55 degrees Celcius */
255 static int twl4030_therm_tbl[] = {
256 30800, 29500, 28300, 27100,
257 26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700,
258 17900, 17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100,
259 12600, 12100, 11600, 11200, 10800, 10400, 10000, 9630, 9280,
260 8950, 8620, 8310, 8020, 7730, 7460, 7200, 6950, 6710,
261 6470, 6250, 6040, 5830, 5640, 5450, 5260, 5090, 4920,
262 4760, 4600, 4450, 4310, 4170, 4040, 3910, 3790, 3670,
267 * Structure containing the registers
268 * of different conversion methods supported by MADC.
269 * Hardware or RT real time conversion request initiated by external host
270 * processor for RT Signal conversions.
271 * External host processors can also request for non RT conversions
272 * SW1 and SW2 software conversions also called asynchronous or GPC request.
275 const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
276 [TWL4030_MADC_RT] = {
277 .sel = TWL4030_MADC_RTSELECT_LSB,
278 .avg = TWL4030_MADC_RTAVERAGE_LSB,
279 .rbase = TWL4030_MADC_RTCH0_LSB,
281 [TWL4030_MADC_SW1] = {
282 .sel = TWL4030_MADC_SW1SELECT_LSB,
283 .avg = TWL4030_MADC_SW1AVERAGE_LSB,
284 .rbase = TWL4030_MADC_GPCH0_LSB,
285 .ctrl = TWL4030_MADC_CTRL_SW1,
287 [TWL4030_MADC_SW2] = {
288 .sel = TWL4030_MADC_SW2SELECT_LSB,
289 .avg = TWL4030_MADC_SW2AVERAGE_LSB,
290 .rbase = TWL4030_MADC_GPCH0_LSB,
291 .ctrl = TWL4030_MADC_CTRL_SW2,
296 * twl4030_madc_channel_raw_read() - Function to read a particular channel value
297 * @madc: pointer to struct twl4030_madc_data
298 * @reg: lsb of ADC Channel
300 * Return: 0 on success, an error code otherwise.
302 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
307 * For each ADC channel, we have MSB and LSB register pair. MSB address
308 * is always LSB address+1. reg parameter is the address of LSB register
310 ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
312 dev_err(madc->dev, "unable to read register 0x%X\n", reg);
316 return (int)(val >> 6);
320 * Return battery temperature in degrees Celsius
323 static int twl4030battery_temperature(int raw_volt)
326 int temp, curr, volt, res, ret;
328 volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
329 /* Getting and calculating the supply current in micro amperes */
330 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
335 curr = ((val & TWL4030_BCI_ITHSENS) + 1) * 10;
336 /* Getting and calculating the thermistor resistance in ohms */
337 res = volt * 1000 / curr;
338 /* calculating temperature */
339 for (temp = 58; temp >= 0; temp--) {
340 int actual = twl4030_therm_tbl[temp];
341 if ((actual - res) >= 0)
348 static int twl4030battery_current(int raw_volt)
353 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
354 TWL4030_BCI_BCICTL1);
357 if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
358 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
359 else /* slope of 0.88 mV/mA */
360 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
364 * Function to read channel values
365 * @madc - pointer to twl4030_madc_data struct
366 * @reg_base - Base address of the first channel
367 * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
368 * @buf - The channel values are stored here. if read fails error
369 * @raw - Return raw values without conversion
371 * Returns the number of successfully read channels.
373 static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
374 u8 reg_base, unsigned
375 long channels, int *buf,
382 for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
383 reg = reg_base + (2 * i);
384 buf[i] = twl4030_madc_channel_raw_read(madc, reg);
386 dev_err(madc->dev, "Unable to read register 0x%X\n",
396 buf[i] = twl4030battery_current(buf[i]);
398 dev_err(madc->dev, "err reading current\n");
402 buf[i] = buf[i] - 750;
406 buf[i] = twl4030battery_temperature(buf[i]);
408 dev_err(madc->dev, "err reading temperature\n");
417 /* Analog Input (V) = conv_result * step_size / R
418 * conv_result = decimal value of 10-bit conversion
420 * step size = 1.5 / (2 ^ 10 -1)
421 * R = Prescaler ratio for input channels.
422 * Result given in mV hence multiplied by 1000.
424 buf[i] = (buf[i] * 3 * 1000 *
425 twl4030_divider_ratios[i].denominator)
427 twl4030_divider_ratios[i].numerator);
436 * @madc - pointer to twl4030_madc_data struct
437 * @id - irq number to be disabled
438 * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
439 * corresponding to RT, SW1, SW2 conversion requests.
440 * Returns error if i2c read/write fails.
442 static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
447 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
449 dev_err(madc->dev, "unable to read imr register 0x%X\n",
454 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
457 "unable to write imr register 0x%X\n", madc->imr);
464 static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
466 struct twl4030_madc_data *madc = _madc;
467 const struct twl4030_madc_conversion_method *method;
470 struct twl4030_madc_request *r;
472 mutex_lock(&madc->lock);
473 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
475 dev_err(madc->dev, "unable to read isr register 0x%X\n",
479 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
481 dev_err(madc->dev, "unable to read imr register 0x%X\n",
486 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
487 if (!(isr_val & (1 << i)))
489 ret = twl4030_madc_disable_irq(madc, i);
491 dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
492 madc->requests[i].result_pending = true;
494 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
495 r = &madc->requests[i];
496 /* No pending results for this method, move to next one */
497 if (!r->result_pending)
499 method = &twl4030_conversion_methods[r->method];
501 twl4030_madc_read_channels(madc, method->rbase,
502 r->channels, r->rbuf, r->raw);
504 r->result_pending = false;
507 mutex_unlock(&madc->lock);
513 * In case of error check whichever request is active
514 * and service the same.
516 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
517 r = &madc->requests[i];
520 method = &twl4030_conversion_methods[r->method];
522 twl4030_madc_read_channels(madc, method->rbase,
523 r->channels, r->rbuf, r->raw);
525 r->result_pending = false;
528 mutex_unlock(&madc->lock);
534 * Function which enables the madc conversion
535 * by writing to the control register.
536 * @madc - pointer to twl4030_madc_data struct
537 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
538 * corresponding to RT SW1 or SW2 conversion methods.
539 * Returns 0 if succeeds else a negative error value
541 static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
544 const struct twl4030_madc_conversion_method *method;
547 if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
550 method = &twl4030_conversion_methods[conv_method];
551 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
554 dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
563 * Function that waits for conversion to be ready
564 * @madc - pointer to twl4030_madc_data struct
565 * @timeout_ms - timeout value in milliseconds
566 * @status_reg - ctrl register
567 * returns 0 if succeeds else a negative error value
569 static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
570 unsigned int timeout_ms,
573 unsigned long timeout;
576 timeout = jiffies + msecs_to_jiffies(timeout_ms);
580 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg);
583 "unable to read status register 0x%X\n",
587 if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
589 usleep_range(500, 2000);
590 } while (!time_after(jiffies, timeout));
591 dev_err(madc->dev, "conversion timeout!\n");
597 * An exported function which can be called from other kernel drivers.
598 * @req twl4030_madc_request structure
599 * req->rbuf will be filled with read values of channels based on the
600 * channel index. If a particular channel reading fails there will
601 * be a negative error value in the corresponding array element.
602 * returns 0 if succeeds else error value
604 static int twl4030_madc_conversion(struct twl4030_madc_request *req)
606 const struct twl4030_madc_conversion_method *method;
609 if (!req || !twl4030_madc)
612 mutex_lock(&twl4030_madc->lock);
613 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
617 /* Do we have a conversion request ongoing */
618 if (twl4030_madc->requests[req->method].active) {
622 method = &twl4030_conversion_methods[req->method];
623 /* Select channels to be converted */
624 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
626 dev_err(twl4030_madc->dev,
627 "unable to write sel register 0x%X\n", method->sel);
630 /* Select averaging for all channels if do_avg is set */
632 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
635 dev_err(twl4030_madc->dev,
636 "unable to write avg register 0x%X\n",
641 /* With RT method we should not be here anymore */
642 if (req->method == TWL4030_MADC_RT) {
646 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
649 twl4030_madc->requests[req->method].active = true;
650 /* Wait until conversion is ready (ctrl register returns EOC) */
651 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
653 twl4030_madc->requests[req->method].active = false;
656 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
657 req->channels, req->rbuf, req->raw);
658 twl4030_madc->requests[req->method].active = false;
661 mutex_unlock(&twl4030_madc->lock);
667 * twl4030_madc_set_current_generator() - setup bias current
669 * @madc: pointer to twl4030_madc_data struct
670 * @chan: can be one of the two values:
671 * 0 - Enables bias current for main battery type reading
672 * 1 - Enables bias current for main battery temperature sensing
673 * @on: enable or disable chan.
675 * Function to enable or disable bias current for
676 * main battery type reading or temperature sensing
678 static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
685 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
686 ®val, TWL4030_BCI_BCICTL1);
688 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
689 TWL4030_BCI_BCICTL1);
693 regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
699 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
700 regval, TWL4030_BCI_BCICTL1);
702 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
703 TWL4030_BCI_BCICTL1);
711 * Function that sets MADC software power on bit to enable MADC
712 * @madc - pointer to twl4030_madc_data struct
713 * @on - Enable or disable MADC software power on bit.
714 * returns error if i2c read/write fails else 0
716 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
721 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
722 ®val, TWL4030_MADC_CTRL1);
724 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
729 regval |= TWL4030_MADC_MADCON;
731 regval &= ~TWL4030_MADC_MADCON;
732 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
734 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
743 * Initialize MADC and request for threaded irq
745 static int twl4030_madc_probe(struct platform_device *pdev)
747 struct twl4030_madc_data *madc;
748 struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
749 struct device_node *np = pdev->dev.of_node;
752 struct iio_dev *iio_dev = NULL;
755 dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n");
759 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc));
761 dev_err(&pdev->dev, "failed allocating iio device\n");
765 madc = iio_priv(iio_dev);
766 madc->dev = &pdev->dev;
768 iio_dev->name = dev_name(&pdev->dev);
769 iio_dev->info = &twl4030_madc_iio_info;
770 iio_dev->modes = INDIO_DIRECT_MODE;
771 iio_dev->channels = twl4030_madc_iio_channels;
772 iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
775 * Phoenix provides 2 interrupt lines. The first one is connected to
776 * the OMAP. The other one can be connected to the other processor such
777 * as modem. Hence two separate ISR and IMR registers.
780 madc->use_second_irq = (pdata->irq_line != 1);
782 madc->use_second_irq = of_property_read_bool(np,
783 "ti,system-uses-second-madc-irq");
785 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
787 madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
790 ret = twl4030_madc_set_power(madc, 1);
793 ret = twl4030_madc_set_current_generator(madc, 0, 1);
795 goto err_current_generator;
797 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
798 ®val, TWL4030_BCI_BCICTL1);
800 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
801 TWL4030_BCI_BCICTL1);
804 regval |= TWL4030_BCI_MESBAT;
805 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
806 regval, TWL4030_BCI_BCICTL1);
808 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
809 TWL4030_BCI_BCICTL1);
813 /* Check that MADC clock is on */
814 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1);
816 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
821 /* If MADC clk is not on, turn it on */
822 if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
823 dev_info(&pdev->dev, "clk disabled, enabling\n");
824 regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
825 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
828 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
834 platform_set_drvdata(pdev, iio_dev);
835 mutex_init(&madc->lock);
837 irq = platform_get_irq(pdev, 0);
838 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
839 twl4030_madc_threaded_irq_handler,
840 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
841 "twl4030_madc", madc);
843 dev_err(&pdev->dev, "could not request irq\n");
848 /* Configure MADC[3:6] */
849 ret = twl_i2c_read_u8(TWL_MODULE_USB, ®val,
850 TWL4030_USB_CARKIT_ANA_CTRL);
852 dev_err(&pdev->dev, "unable to read reg CARKIT_ANA_CTRL 0x%X\n",
853 TWL4030_USB_CARKIT_ANA_CTRL);
856 regval |= TWL4030_USB_SEL_MADC_MCPC;
857 ret = twl_i2c_write_u8(TWL_MODULE_USB, regval,
858 TWL4030_USB_CARKIT_ANA_CTRL);
860 dev_err(&pdev->dev, "unable to write reg CARKIT_ANA_CTRL 0x%X\n",
861 TWL4030_USB_CARKIT_ANA_CTRL);
865 /* Enable 3v1 bias regulator for MADC[3:6] */
866 madc->usb3v1 = devm_regulator_get(madc->dev, "vusb3v1");
867 if (IS_ERR(madc->usb3v1)) {
872 ret = regulator_enable(madc->usb3v1);
874 dev_err(madc->dev, "could not enable 3v1 bias regulator\n");
878 ret = iio_device_register(iio_dev);
880 dev_err(&pdev->dev, "could not register iio device\n");
887 regulator_disable(madc->usb3v1);
889 twl4030_madc_set_current_generator(madc, 0, 0);
890 err_current_generator:
891 twl4030_madc_set_power(madc, 0);
895 static void twl4030_madc_remove(struct platform_device *pdev)
897 struct iio_dev *iio_dev = platform_get_drvdata(pdev);
898 struct twl4030_madc_data *madc = iio_priv(iio_dev);
900 iio_device_unregister(iio_dev);
902 twl4030_madc_set_current_generator(madc, 0, 0);
903 twl4030_madc_set_power(madc, 0);
905 regulator_disable(madc->usb3v1);
909 static const struct of_device_id twl_madc_of_match[] = {
910 { .compatible = "ti,twl4030-madc", },
913 MODULE_DEVICE_TABLE(of, twl_madc_of_match);
916 static struct platform_driver twl4030_madc_driver = {
917 .probe = twl4030_madc_probe,
918 .remove_new = twl4030_madc_remove,
920 .name = "twl4030_madc",
921 .of_match_table = of_match_ptr(twl_madc_of_match),
925 module_platform_driver(twl4030_madc_driver);
927 MODULE_DESCRIPTION("TWL4030 ADC driver");
928 MODULE_LICENSE("GPL");
929 MODULE_AUTHOR("J Keerthy");
930 MODULE_ALIAS("platform:twl4030_madc");