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.
7 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
8 * J Keerthy <j-keerthy@ti.com>
10 * Based on twl4030-madc.c
11 * Copyright (C) 2008 Nokia Corporation
12 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
14 * Amit Kucheria <amit.kucheria@canonical.com>
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.
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.
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
32 #include <linux/device.h>
33 #include <linux/interrupt.h>
34 #include <linux/kernel.h>
35 #include <linux/delay.h>
36 #include <linux/platform_device.h>
37 #include <linux/slab.h>
38 #include <linux/i2c/twl.h>
39 #include <linux/i2c/twl4030-madc.h>
40 #include <linux/module.h>
41 #include <linux/stddef.h>
42 #include <linux/mutex.h>
43 #include <linux/bitops.h>
44 #include <linux/jiffies.h>
45 #include <linux/types.h>
46 #include <linux/gfp.h>
47 #include <linux/err.h>
48 #include <linux/regulator/consumer.h>
50 #include <linux/iio/iio.h>
52 #define TWL4030_USB_SEL_MADC_MCPC (1<<3)
53 #define TWL4030_USB_CARKIT_ANA_CTRL 0xBB
56 * struct twl4030_madc_data - a container for madc info
57 * @dev: Pointer to device structure for madc
58 * @lock: Mutex protecting this data structure
59 * @regulator: Pointer to bias regulator for madc
60 * @requests: Array of request struct corresponding to SW1, SW2 and RT
61 * @use_second_irq: IRQ selection (main or co-processor)
62 * @imr: Interrupt mask register of MADC
63 * @isr: Interrupt status register of MADC
65 struct twl4030_madc_data {
67 struct mutex lock; /* mutex protecting this data structure */
68 struct regulator *usb3v1;
69 struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
75 static int twl4030_madc_read(struct iio_dev *iio_dev,
76 const struct iio_chan_spec *chan,
77 int *val, int *val2, long mask)
79 struct twl4030_madc_data *madc = iio_priv(iio_dev);
80 struct twl4030_madc_request req;
83 req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
85 req.channels = BIT(chan->channel);
88 req.type = TWL4030_MADC_WAIT;
89 req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
90 req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
92 ret = twl4030_madc_conversion(&req);
96 *val = req.rbuf[chan->channel];
101 static const struct iio_info twl4030_madc_iio_info = {
102 .read_raw = &twl4030_madc_read,
103 .driver_module = THIS_MODULE,
106 #define TWL4030_ADC_CHANNEL(_channel, _type, _name) { \
108 .channel = _channel, \
109 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
110 BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
111 BIT(IIO_CHAN_INFO_PROCESSED), \
112 .datasheet_name = _name, \
116 static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
117 TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0"),
118 TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1"),
119 TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2"),
120 TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3"),
121 TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4"),
122 TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5"),
123 TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6"),
124 TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7"),
125 TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8"),
126 TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9"),
127 TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10"),
128 TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11"),
129 TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12"),
130 TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13"),
131 TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14"),
132 TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15"),
135 static struct twl4030_madc_data *twl4030_madc;
137 struct twl4030_prescale_divider_ratios {
142 static const struct twl4030_prescale_divider_ratios
143 twl4030_divider_ratios[16] = {
144 {1, 1}, /* CHANNEL 0 No Prescaler */
145 {1, 1}, /* CHANNEL 1 No Prescaler */
146 {6, 10}, /* CHANNEL 2 */
147 {6, 10}, /* CHANNEL 3 */
148 {6, 10}, /* CHANNEL 4 */
149 {6, 10}, /* CHANNEL 5 */
150 {6, 10}, /* CHANNEL 6 */
151 {6, 10}, /* CHANNEL 7 */
152 {3, 14}, /* CHANNEL 8 */
153 {1, 3}, /* CHANNEL 9 */
154 {1, 1}, /* CHANNEL 10 No Prescaler */
155 {15, 100}, /* CHANNEL 11 */
156 {1, 4}, /* CHANNEL 12 */
157 {1, 1}, /* CHANNEL 13 Reserved channels */
158 {1, 1}, /* CHANNEL 14 Reseved channels */
159 {5, 11}, /* CHANNEL 15 */
163 /* Conversion table from -3 to 55 degrees Celcius */
164 static int twl4030_therm_tbl[] = {
165 30800, 29500, 28300, 27100,
166 26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700,
167 17900, 17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100,
168 12600, 12100, 11600, 11200, 10800, 10400, 10000, 9630, 9280,
169 8950, 8620, 8310, 8020, 7730, 7460, 7200, 6950, 6710,
170 6470, 6250, 6040, 5830, 5640, 5450, 5260, 5090, 4920,
171 4760, 4600, 4450, 4310, 4170, 4040, 3910, 3790, 3670,
176 * Structure containing the registers
177 * of different conversion methods supported by MADC.
178 * Hardware or RT real time conversion request initiated by external host
179 * processor for RT Signal conversions.
180 * External host processors can also request for non RT conversions
181 * SW1 and SW2 software conversions also called asynchronous or GPC request.
184 const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
185 [TWL4030_MADC_RT] = {
186 .sel = TWL4030_MADC_RTSELECT_LSB,
187 .avg = TWL4030_MADC_RTAVERAGE_LSB,
188 .rbase = TWL4030_MADC_RTCH0_LSB,
190 [TWL4030_MADC_SW1] = {
191 .sel = TWL4030_MADC_SW1SELECT_LSB,
192 .avg = TWL4030_MADC_SW1AVERAGE_LSB,
193 .rbase = TWL4030_MADC_GPCH0_LSB,
194 .ctrl = TWL4030_MADC_CTRL_SW1,
196 [TWL4030_MADC_SW2] = {
197 .sel = TWL4030_MADC_SW2SELECT_LSB,
198 .avg = TWL4030_MADC_SW2AVERAGE_LSB,
199 .rbase = TWL4030_MADC_GPCH0_LSB,
200 .ctrl = TWL4030_MADC_CTRL_SW2,
205 * twl4030_madc_channel_raw_read() - Function to read a particular channel value
206 * @madc: pointer to struct twl4030_madc_data
207 * @reg: lsb of ADC Channel
209 * Return: 0 on success, an error code otherwise.
211 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
216 * For each ADC channel, we have MSB and LSB register pair. MSB address
217 * is always LSB address+1. reg parameter is the address of LSB register
219 ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
221 dev_err(madc->dev, "unable to read register 0x%X\n", reg);
225 return (int)(val >> 6);
229 * Return battery temperature in degrees Celsius
232 static int twl4030battery_temperature(int raw_volt)
235 int temp, curr, volt, res, ret;
237 volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
238 /* Getting and calculating the supply current in micro amperes */
239 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
244 curr = ((val & TWL4030_BCI_ITHSENS) + 1) * 10;
245 /* Getting and calculating the thermistor resistance in ohms */
246 res = volt * 1000 / curr;
247 /* calculating temperature */
248 for (temp = 58; temp >= 0; temp--) {
249 int actual = twl4030_therm_tbl[temp];
250 if ((actual - res) >= 0)
257 static int twl4030battery_current(int raw_volt)
262 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
263 TWL4030_BCI_BCICTL1);
266 if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
267 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
268 else /* slope of 0.88 mV/mA */
269 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
273 * Function to read channel values
274 * @madc - pointer to twl4030_madc_data struct
275 * @reg_base - Base address of the first channel
276 * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
277 * @buf - The channel values are stored here. if read fails error
278 * @raw - Return raw values without conversion
280 * Returns the number of successfully read channels.
282 static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
283 u8 reg_base, unsigned
284 long channels, int *buf,
291 for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
292 reg = reg_base + (2 * i);
293 buf[i] = twl4030_madc_channel_raw_read(madc, reg);
295 dev_err(madc->dev, "Unable to read register 0x%X\n",
305 buf[i] = twl4030battery_current(buf[i]);
307 dev_err(madc->dev, "err reading current\n");
311 buf[i] = buf[i] - 750;
315 buf[i] = twl4030battery_temperature(buf[i]);
317 dev_err(madc->dev, "err reading temperature\n");
326 /* Analog Input (V) = conv_result * step_size / R
327 * conv_result = decimal value of 10-bit conversion
329 * step size = 1.5 / (2 ^ 10 -1)
330 * R = Prescaler ratio for input channels.
331 * Result given in mV hence multiplied by 1000.
333 buf[i] = (buf[i] * 3 * 1000 *
334 twl4030_divider_ratios[i].denominator)
336 twl4030_divider_ratios[i].numerator);
345 * @madc - pointer to twl4030_madc_data struct
346 * @id - irq number to be enabled
347 * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
348 * corresponding to RT, SW1, SW2 conversion requests.
349 * If the i2c read fails it returns an error else returns 0.
351 static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
356 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
358 dev_err(madc->dev, "unable to read imr register 0x%X\n",
364 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
367 "unable to write imr register 0x%X\n", madc->imr);
376 * @madc - pointer to twl4030_madc_data struct
377 * @id - irq number to be disabled
378 * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
379 * corresponding to RT, SW1, SW2 conversion requests.
380 * Returns error if i2c read/write fails.
382 static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
387 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
389 dev_err(madc->dev, "unable to read imr register 0x%X\n",
394 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
397 "unable to write imr register 0x%X\n", madc->imr);
404 static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
406 struct twl4030_madc_data *madc = _madc;
407 const struct twl4030_madc_conversion_method *method;
410 struct twl4030_madc_request *r;
412 mutex_lock(&madc->lock);
413 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
415 dev_err(madc->dev, "unable to read isr register 0x%X\n",
419 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
421 dev_err(madc->dev, "unable to read imr register 0x%X\n",
426 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
427 if (!(isr_val & (1 << i)))
429 ret = twl4030_madc_disable_irq(madc, i);
431 dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
432 madc->requests[i].result_pending = 1;
434 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
435 r = &madc->requests[i];
436 /* No pending results for this method, move to next one */
437 if (!r->result_pending)
439 method = &twl4030_conversion_methods[r->method];
441 len = twl4030_madc_read_channels(madc, method->rbase,
442 r->channels, r->rbuf, r->raw);
443 /* Return results to caller */
444 if (r->func_cb != NULL) {
445 r->func_cb(len, r->channels, r->rbuf);
449 r->result_pending = 0;
452 mutex_unlock(&madc->lock);
458 * In case of error check whichever request is active
459 * and service the same.
461 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
462 r = &madc->requests[i];
465 method = &twl4030_conversion_methods[r->method];
467 len = twl4030_madc_read_channels(madc, method->rbase,
468 r->channels, r->rbuf, r->raw);
469 /* Return results to caller */
470 if (r->func_cb != NULL) {
471 r->func_cb(len, r->channels, r->rbuf);
475 r->result_pending = 0;
478 mutex_unlock(&madc->lock);
483 static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
484 struct twl4030_madc_request *req)
486 struct twl4030_madc_request *p;
489 p = &madc->requests[req->method];
490 memcpy(p, req, sizeof(*req));
491 ret = twl4030_madc_enable_irq(madc, req->method);
493 dev_err(madc->dev, "enable irq failed!!\n");
501 * Function which enables the madc conversion
502 * by writing to the control register.
503 * @madc - pointer to twl4030_madc_data struct
504 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
505 * corresponding to RT SW1 or SW2 conversion methods.
506 * Returns 0 if succeeds else a negative error value
508 static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
511 const struct twl4030_madc_conversion_method *method;
514 if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
517 method = &twl4030_conversion_methods[conv_method];
518 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
521 dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
530 * Function that waits for conversion to be ready
531 * @madc - pointer to twl4030_madc_data struct
532 * @timeout_ms - timeout value in milliseconds
533 * @status_reg - ctrl register
534 * returns 0 if succeeds else a negative error value
536 static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
537 unsigned int timeout_ms,
540 unsigned long timeout;
543 timeout = jiffies + msecs_to_jiffies(timeout_ms);
547 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg);
550 "unable to read status register 0x%X\n",
554 if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
556 usleep_range(500, 2000);
557 } while (!time_after(jiffies, timeout));
558 dev_err(madc->dev, "conversion timeout!\n");
564 * An exported function which can be called from other kernel drivers.
565 * @req twl4030_madc_request structure
566 * req->rbuf will be filled with read values of channels based on the
567 * channel index. If a particular channel reading fails there will
568 * be a negative error value in the corresponding array element.
569 * returns 0 if succeeds else error value
571 int twl4030_madc_conversion(struct twl4030_madc_request *req)
573 const struct twl4030_madc_conversion_method *method;
576 if (!req || !twl4030_madc)
579 mutex_lock(&twl4030_madc->lock);
580 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
584 /* Do we have a conversion request ongoing */
585 if (twl4030_madc->requests[req->method].active) {
589 method = &twl4030_conversion_methods[req->method];
590 /* Select channels to be converted */
591 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
593 dev_err(twl4030_madc->dev,
594 "unable to write sel register 0x%X\n", method->sel);
597 /* Select averaging for all channels if do_avg is set */
599 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
602 dev_err(twl4030_madc->dev,
603 "unable to write avg register 0x%X\n",
608 if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
609 ret = twl4030_madc_set_irq(twl4030_madc, req);
612 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
615 twl4030_madc->requests[req->method].active = 1;
619 /* With RT method we should not be here anymore */
620 if (req->method == TWL4030_MADC_RT) {
624 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
627 twl4030_madc->requests[req->method].active = 1;
628 /* Wait until conversion is ready (ctrl register returns EOC) */
629 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
631 twl4030_madc->requests[req->method].active = 0;
634 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
635 req->channels, req->rbuf, req->raw);
636 twl4030_madc->requests[req->method].active = 0;
639 mutex_unlock(&twl4030_madc->lock);
643 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
645 int twl4030_get_madc_conversion(int channel_no)
647 struct twl4030_madc_request req;
651 req.channels = (1 << channel_no);
652 req.method = TWL4030_MADC_SW2;
656 ret = twl4030_madc_conversion(&req);
659 if (req.rbuf[channel_no] > 0)
660 temp = req.rbuf[channel_no];
664 EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
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->dev.parent = &pdev->dev;
770 iio_dev->dev.of_node = pdev->dev.of_node;
771 iio_dev->info = &twl4030_madc_iio_info;
772 iio_dev->modes = INDIO_DIRECT_MODE;
773 iio_dev->channels = twl4030_madc_iio_channels;
774 iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
777 * Phoenix provides 2 interrupt lines. The first one is connected to
778 * the OMAP. The other one can be connected to the other processor such
779 * as modem. Hence two separate ISR and IMR registers.
782 madc->use_second_irq = (pdata->irq_line != 1);
784 madc->use_second_irq = of_property_read_bool(np,
785 "ti,system-uses-second-madc-irq");
787 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
789 madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
792 ret = twl4030_madc_set_power(madc, 1);
795 ret = twl4030_madc_set_current_generator(madc, 0, 1);
797 goto err_current_generator;
799 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
800 ®val, TWL4030_BCI_BCICTL1);
802 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
803 TWL4030_BCI_BCICTL1);
806 regval |= TWL4030_BCI_MESBAT;
807 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
808 regval, TWL4030_BCI_BCICTL1);
810 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
811 TWL4030_BCI_BCICTL1);
815 /* Check that MADC clock is on */
816 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1);
818 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
823 /* If MADC clk is not on, turn it on */
824 if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
825 dev_info(&pdev->dev, "clk disabled, enabling\n");
826 regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
827 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
830 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
836 platform_set_drvdata(pdev, iio_dev);
837 mutex_init(&madc->lock);
839 irq = platform_get_irq(pdev, 0);
840 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
841 twl4030_madc_threaded_irq_handler,
842 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
843 "twl4030_madc", madc);
845 dev_err(&pdev->dev, "could not request irq\n");
850 /* Configure MADC[3:6] */
851 ret = twl_i2c_read_u8(TWL_MODULE_USB, ®val,
852 TWL4030_USB_CARKIT_ANA_CTRL);
854 dev_err(&pdev->dev, "unable to read reg CARKIT_ANA_CTRL 0x%X\n",
855 TWL4030_USB_CARKIT_ANA_CTRL);
858 regval |= TWL4030_USB_SEL_MADC_MCPC;
859 ret = twl_i2c_write_u8(TWL_MODULE_USB, regval,
860 TWL4030_USB_CARKIT_ANA_CTRL);
862 dev_err(&pdev->dev, "unable to write reg CARKIT_ANA_CTRL 0x%X\n",
863 TWL4030_USB_CARKIT_ANA_CTRL);
867 /* Enable 3v1 bias regulator for MADC[3:6] */
868 madc->usb3v1 = devm_regulator_get(madc->dev, "vusb3v1");
869 if (IS_ERR(madc->usb3v1)) {
874 ret = regulator_enable(madc->usb3v1);
876 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 int 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);
911 static const struct of_device_id twl_madc_of_match[] = {
912 { .compatible = "ti,twl4030-madc", },
915 MODULE_DEVICE_TABLE(of, twl_madc_of_match);
918 static struct platform_driver twl4030_madc_driver = {
919 .probe = twl4030_madc_probe,
920 .remove = twl4030_madc_remove,
922 .name = "twl4030_madc",
923 .of_match_table = of_match_ptr(twl_madc_of_match),
927 module_platform_driver(twl4030_madc_driver);
929 MODULE_DESCRIPTION("TWL4030 ADC driver");
930 MODULE_LICENSE("GPL");
931 MODULE_AUTHOR("J Keerthy");
932 MODULE_ALIAS("platform:twl4030_madc");