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/init.h>
33 #include <linux/device.h>
34 #include <linux/interrupt.h>
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/platform_device.h>
38 #include <linux/slab.h>
39 #include <linux/i2c/twl.h>
40 #include <linux/i2c/twl4030-madc.h>
41 #include <linux/module.h>
42 #include <linux/stddef.h>
43 #include <linux/mutex.h>
44 #include <linux/bitops.h>
45 #include <linux/jiffies.h>
46 #include <linux/types.h>
47 #include <linux/gfp.h>
48 #include <linux/err.h>
50 #include <linux/iio/iio.h>
53 * struct twl4030_madc_data - a container for madc info
54 * @dev: Pointer to device structure for madc
55 * @lock: Mutex protecting this data structure
56 * @requests: Array of request struct corresponding to SW1, SW2 and RT
57 * @use_second_irq: IRQ selection (main or co-processor)
58 * @imr: Interrupt mask register of MADC
59 * @isr: Interrupt status register of MADC
61 struct twl4030_madc_data {
63 struct mutex lock; /* mutex protecting this data structure */
64 struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
70 static int twl4030_madc_read(struct iio_dev *iio_dev,
71 const struct iio_chan_spec *chan,
72 int *val, int *val2, long mask)
74 struct twl4030_madc_data *madc = iio_priv(iio_dev);
75 struct twl4030_madc_request req;
78 req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
80 req.channels = BIT(chan->channel);
83 req.type = TWL4030_MADC_WAIT;
84 req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
85 req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
87 ret = twl4030_madc_conversion(&req);
91 *val = req.rbuf[chan->channel];
96 static const struct iio_info twl4030_madc_iio_info = {
97 .read_raw = &twl4030_madc_read,
98 .driver_module = THIS_MODULE,
101 #define TWL4030_ADC_CHANNEL(_channel, _type, _name) { \
103 .channel = _channel, \
104 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
105 BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
106 BIT(IIO_CHAN_INFO_PROCESSED), \
107 .datasheet_name = _name, \
111 static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
112 TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0"),
113 TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1"),
114 TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2"),
115 TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3"),
116 TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4"),
117 TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5"),
118 TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6"),
119 TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7"),
120 TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8"),
121 TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9"),
122 TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10"),
123 TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11"),
124 TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12"),
125 TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13"),
126 TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14"),
127 TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15"),
130 static struct twl4030_madc_data *twl4030_madc;
132 struct twl4030_prescale_divider_ratios {
137 static const struct twl4030_prescale_divider_ratios
138 twl4030_divider_ratios[16] = {
139 {1, 1}, /* CHANNEL 0 No Prescaler */
140 {1, 1}, /* CHANNEL 1 No Prescaler */
141 {6, 10}, /* CHANNEL 2 */
142 {6, 10}, /* CHANNEL 3 */
143 {6, 10}, /* CHANNEL 4 */
144 {6, 10}, /* CHANNEL 5 */
145 {6, 10}, /* CHANNEL 6 */
146 {6, 10}, /* CHANNEL 7 */
147 {3, 14}, /* CHANNEL 8 */
148 {1, 3}, /* CHANNEL 9 */
149 {1, 1}, /* CHANNEL 10 No Prescaler */
150 {15, 100}, /* CHANNEL 11 */
151 {1, 4}, /* CHANNEL 12 */
152 {1, 1}, /* CHANNEL 13 Reserved channels */
153 {1, 1}, /* CHANNEL 14 Reseved channels */
154 {5, 11}, /* CHANNEL 15 */
158 /* Conversion table from -3 to 55 degrees Celcius */
159 static int twl4030_therm_tbl[] = {
160 30800, 29500, 28300, 27100,
161 26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700,
162 17900, 17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100,
163 12600, 12100, 11600, 11200, 10800, 10400, 10000, 9630, 9280,
164 8950, 8620, 8310, 8020, 7730, 7460, 7200, 6950, 6710,
165 6470, 6250, 6040, 5830, 5640, 5450, 5260, 5090, 4920,
166 4760, 4600, 4450, 4310, 4170, 4040, 3910, 3790, 3670,
171 * Structure containing the registers
172 * of different conversion methods supported by MADC.
173 * Hardware or RT real time conversion request initiated by external host
174 * processor for RT Signal conversions.
175 * External host processors can also request for non RT conversions
176 * SW1 and SW2 software conversions also called asynchronous or GPC request.
179 const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
180 [TWL4030_MADC_RT] = {
181 .sel = TWL4030_MADC_RTSELECT_LSB,
182 .avg = TWL4030_MADC_RTAVERAGE_LSB,
183 .rbase = TWL4030_MADC_RTCH0_LSB,
185 [TWL4030_MADC_SW1] = {
186 .sel = TWL4030_MADC_SW1SELECT_LSB,
187 .avg = TWL4030_MADC_SW1AVERAGE_LSB,
188 .rbase = TWL4030_MADC_GPCH0_LSB,
189 .ctrl = TWL4030_MADC_CTRL_SW1,
191 [TWL4030_MADC_SW2] = {
192 .sel = TWL4030_MADC_SW2SELECT_LSB,
193 .avg = TWL4030_MADC_SW2AVERAGE_LSB,
194 .rbase = TWL4030_MADC_GPCH0_LSB,
195 .ctrl = TWL4030_MADC_CTRL_SW2,
200 * twl4030_madc_channel_raw_read() - Function to read a particular channel value
201 * @madc: pointer to struct twl4030_madc_data
202 * @reg: lsb of ADC Channel
204 * Return: 0 on success, an error code otherwise.
206 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
211 * For each ADC channel, we have MSB and LSB register pair. MSB address
212 * is always LSB address+1. reg parameter is the address of LSB register
214 ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
216 dev_err(madc->dev, "unable to read register 0x%X\n", reg);
220 return (int)(val >> 6);
224 * Return battery temperature in degrees Celsius
227 static int twl4030battery_temperature(int raw_volt)
230 int temp, curr, volt, res, ret;
232 volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
233 /* Getting and calculating the supply current in micro amperes */
234 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
239 curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
240 /* Getting and calculating the thermistor resistance in ohms */
241 res = volt * 1000 / curr;
242 /* calculating temperature */
243 for (temp = 58; temp >= 0; temp--) {
244 int actual = twl4030_therm_tbl[temp];
245 if ((actual - res) >= 0)
252 static int twl4030battery_current(int raw_volt)
257 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
258 TWL4030_BCI_BCICTL1);
261 if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
262 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
263 else /* slope of 0.88 mV/mA */
264 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
268 * Function to read channel values
269 * @madc - pointer to twl4030_madc_data struct
270 * @reg_base - Base address of the first channel
271 * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
272 * @buf - The channel values are stored here. if read fails error
273 * @raw - Return raw values without conversion
275 * Returns the number of successfully read channels.
277 static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
278 u8 reg_base, unsigned
279 long channels, int *buf,
286 for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
287 reg = reg_base + (2 * i);
288 buf[i] = twl4030_madc_channel_raw_read(madc, reg);
290 dev_err(madc->dev, "Unable to read register 0x%X\n",
300 buf[i] = twl4030battery_current(buf[i]);
302 dev_err(madc->dev, "err reading current\n");
306 buf[i] = buf[i] - 750;
310 buf[i] = twl4030battery_temperature(buf[i]);
312 dev_err(madc->dev, "err reading temperature\n");
321 /* Analog Input (V) = conv_result * step_size / R
322 * conv_result = decimal value of 10-bit conversion
324 * step size = 1.5 / (2 ^ 10 -1)
325 * R = Prescaler ratio for input channels.
326 * Result given in mV hence multiplied by 1000.
328 buf[i] = (buf[i] * 3 * 1000 *
329 twl4030_divider_ratios[i].denominator)
331 twl4030_divider_ratios[i].numerator);
340 * @madc - pointer to twl4030_madc_data struct
341 * @id - irq number to be enabled
342 * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
343 * corresponding to RT, SW1, SW2 conversion requests.
344 * If the i2c read fails it returns an error else returns 0.
346 static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
351 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
353 dev_err(madc->dev, "unable to read imr register 0x%X\n",
359 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
362 "unable to write imr register 0x%X\n", madc->imr);
371 * @madc - pointer to twl4030_madc_data struct
372 * @id - irq number to be disabled
373 * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
374 * corresponding to RT, SW1, SW2 conversion requests.
375 * Returns error if i2c read/write fails.
377 static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
382 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
384 dev_err(madc->dev, "unable to read imr register 0x%X\n",
389 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
392 "unable to write imr register 0x%X\n", madc->imr);
399 static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
401 struct twl4030_madc_data *madc = _madc;
402 const struct twl4030_madc_conversion_method *method;
405 struct twl4030_madc_request *r;
407 mutex_lock(&madc->lock);
408 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
410 dev_err(madc->dev, "unable to read isr register 0x%X\n",
414 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
416 dev_err(madc->dev, "unable to read imr register 0x%X\n",
421 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
422 if (!(isr_val & (1 << i)))
424 ret = twl4030_madc_disable_irq(madc, i);
426 dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
427 madc->requests[i].result_pending = 1;
429 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
430 r = &madc->requests[i];
431 /* No pending results for this method, move to next one */
432 if (!r->result_pending)
434 method = &twl4030_conversion_methods[r->method];
436 len = twl4030_madc_read_channels(madc, method->rbase,
437 r->channels, r->rbuf, r->raw);
438 /* Return results to caller */
439 if (r->func_cb != NULL) {
440 r->func_cb(len, r->channels, r->rbuf);
444 r->result_pending = 0;
447 mutex_unlock(&madc->lock);
453 * In case of error check whichever request is active
454 * and service the same.
456 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
457 r = &madc->requests[i];
460 method = &twl4030_conversion_methods[r->method];
462 len = twl4030_madc_read_channels(madc, method->rbase,
463 r->channels, r->rbuf, r->raw);
464 /* Return results to caller */
465 if (r->func_cb != NULL) {
466 r->func_cb(len, r->channels, r->rbuf);
470 r->result_pending = 0;
473 mutex_unlock(&madc->lock);
478 static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
479 struct twl4030_madc_request *req)
481 struct twl4030_madc_request *p;
484 p = &madc->requests[req->method];
485 memcpy(p, req, sizeof(*req));
486 ret = twl4030_madc_enable_irq(madc, req->method);
488 dev_err(madc->dev, "enable irq failed!!\n");
496 * Function which enables the madc conversion
497 * by writing to the control register.
498 * @madc - pointer to twl4030_madc_data struct
499 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
500 * corresponding to RT SW1 or SW2 conversion methods.
501 * Returns 0 if succeeds else a negative error value
503 static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
506 const struct twl4030_madc_conversion_method *method;
509 if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
512 method = &twl4030_conversion_methods[conv_method];
513 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
516 dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
525 * Function that waits for conversion to be ready
526 * @madc - pointer to twl4030_madc_data struct
527 * @timeout_ms - timeout value in milliseconds
528 * @status_reg - ctrl register
529 * returns 0 if succeeds else a negative error value
531 static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
532 unsigned int timeout_ms,
535 unsigned long timeout;
538 timeout = jiffies + msecs_to_jiffies(timeout_ms);
542 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg);
545 "unable to read status register 0x%X\n",
549 if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
551 usleep_range(500, 2000);
552 } while (!time_after(jiffies, timeout));
553 dev_err(madc->dev, "conversion timeout!\n");
559 * An exported function which can be called from other kernel drivers.
560 * @req twl4030_madc_request structure
561 * req->rbuf will be filled with read values of channels based on the
562 * channel index. If a particular channel reading fails there will
563 * be a negative error value in the corresponding array element.
564 * returns 0 if succeeds else error value
566 int twl4030_madc_conversion(struct twl4030_madc_request *req)
568 const struct twl4030_madc_conversion_method *method;
571 if (!req || !twl4030_madc)
574 mutex_lock(&twl4030_madc->lock);
575 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
579 /* Do we have a conversion request ongoing */
580 if (twl4030_madc->requests[req->method].active) {
584 method = &twl4030_conversion_methods[req->method];
585 /* Select channels to be converted */
586 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
588 dev_err(twl4030_madc->dev,
589 "unable to write sel register 0x%X\n", method->sel);
592 /* Select averaging for all channels if do_avg is set */
594 ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
597 dev_err(twl4030_madc->dev,
598 "unable to write avg register 0x%X\n",
603 if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
604 ret = twl4030_madc_set_irq(twl4030_madc, req);
607 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
610 twl4030_madc->requests[req->method].active = 1;
614 /* With RT method we should not be here anymore */
615 if (req->method == TWL4030_MADC_RT) {
619 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
622 twl4030_madc->requests[req->method].active = 1;
623 /* Wait until conversion is ready (ctrl register returns EOC) */
624 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
626 twl4030_madc->requests[req->method].active = 0;
629 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
630 req->channels, req->rbuf, req->raw);
631 twl4030_madc->requests[req->method].active = 0;
634 mutex_unlock(&twl4030_madc->lock);
638 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
640 int twl4030_get_madc_conversion(int channel_no)
642 struct twl4030_madc_request req;
646 req.channels = (1 << channel_no);
647 req.method = TWL4030_MADC_SW2;
650 ret = twl4030_madc_conversion(&req);
653 if (req.rbuf[channel_no] > 0)
654 temp = req.rbuf[channel_no];
658 EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
661 * twl4030_madc_set_current_generator() - setup bias current
663 * @madc: pointer to twl4030_madc_data struct
664 * @chan: can be one of the two values:
666 * Enables bias current for main battery type reading
668 * Enables bias current for main battery temperature sensing
669 * @on: enable or disable chan.
671 * Function to enable or disable bias current for
672 * main battery type reading or temperature sensing
674 static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
681 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
682 ®val, TWL4030_BCI_BCICTL1);
684 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
685 TWL4030_BCI_BCICTL1);
689 regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
695 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
696 regval, TWL4030_BCI_BCICTL1);
698 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
699 TWL4030_BCI_BCICTL1);
707 * Function that sets MADC software power on bit to enable MADC
708 * @madc - pointer to twl4030_madc_data struct
709 * @on - Enable or disable MADC software power on bit.
710 * returns error if i2c read/write fails else 0
712 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
717 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
718 ®val, TWL4030_MADC_CTRL1);
720 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
725 regval |= TWL4030_MADC_MADCON;
727 regval &= ~TWL4030_MADC_MADCON;
728 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
730 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
739 * Initialize MADC and request for threaded irq
741 static int twl4030_madc_probe(struct platform_device *pdev)
743 struct twl4030_madc_data *madc;
744 struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
745 struct device_node *np = pdev->dev.of_node;
748 struct iio_dev *iio_dev = NULL;
751 dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n");
755 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc));
757 dev_err(&pdev->dev, "failed allocating iio device\n");
761 madc = iio_priv(iio_dev);
762 madc->dev = &pdev->dev;
764 iio_dev->name = dev_name(&pdev->dev);
765 iio_dev->dev.parent = &pdev->dev;
766 iio_dev->dev.of_node = pdev->dev.of_node;
767 iio_dev->info = &twl4030_madc_iio_info;
768 iio_dev->modes = INDIO_DIRECT_MODE;
769 iio_dev->channels = twl4030_madc_iio_channels;
770 iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
773 * Phoenix provides 2 interrupt lines. The first one is connected to
774 * the OMAP. The other one can be connected to the other processor such
775 * as modem. Hence two separate ISR and IMR registers.
778 madc->use_second_irq = (pdata->irq_line != 1);
780 madc->use_second_irq = of_property_read_bool(np,
781 "ti,system-uses-second-madc-irq");
783 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
785 madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
788 ret = twl4030_madc_set_power(madc, 1);
791 ret = twl4030_madc_set_current_generator(madc, 0, 1);
793 goto err_current_generator;
795 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
796 ®val, TWL4030_BCI_BCICTL1);
798 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
799 TWL4030_BCI_BCICTL1);
802 regval |= TWL4030_BCI_MESBAT;
803 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
804 regval, TWL4030_BCI_BCICTL1);
806 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
807 TWL4030_BCI_BCICTL1);
811 /* Check that MADC clock is on */
812 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1);
814 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
819 /* If MADC clk is not on, turn it on */
820 if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
821 dev_info(&pdev->dev, "clk disabled, enabling\n");
822 regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
823 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
826 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
832 platform_set_drvdata(pdev, iio_dev);
833 mutex_init(&madc->lock);
835 irq = platform_get_irq(pdev, 0);
836 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
837 twl4030_madc_threaded_irq_handler,
838 IRQF_TRIGGER_RISING, "twl4030_madc", madc);
840 dev_err(&pdev->dev, "could not request irq\n");
845 ret = iio_device_register(iio_dev);
847 dev_err(&pdev->dev, "could not register iio device\n");
854 twl4030_madc_set_current_generator(madc, 0, 0);
855 err_current_generator:
856 twl4030_madc_set_power(madc, 0);
860 static int twl4030_madc_remove(struct platform_device *pdev)
862 struct iio_dev *iio_dev = platform_get_drvdata(pdev);
863 struct twl4030_madc_data *madc = iio_priv(iio_dev);
865 iio_device_unregister(iio_dev);
867 twl4030_madc_set_current_generator(madc, 0, 0);
868 twl4030_madc_set_power(madc, 0);
874 static const struct of_device_id twl_madc_of_match[] = {
875 { .compatible = "ti,twl4030-madc", },
878 MODULE_DEVICE_TABLE(of, twl_madc_of_match);
881 static struct platform_driver twl4030_madc_driver = {
882 .probe = twl4030_madc_probe,
883 .remove = twl4030_madc_remove,
885 .name = "twl4030_madc",
886 .owner = THIS_MODULE,
887 .of_match_table = of_match_ptr(twl_madc_of_match),
891 module_platform_driver(twl4030_madc_driver);
893 MODULE_DESCRIPTION("TWL4030 ADC driver");
894 MODULE_LICENSE("GPL");
895 MODULE_AUTHOR("J Keerthy");
896 MODULE_ALIAS("platform:twl4030_madc");