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>
50 * struct twl4030_madc_data - a container for madc info
51 * @dev - pointer to device structure for madc
52 * @lock - mutex protecting this data structure
53 * @requests - Array of request struct corresponding to SW1, SW2 and RT
54 * @imr - Interrupt mask register of MADC
55 * @isr - Interrupt status register of MADC
57 struct twl4030_madc_data {
59 struct mutex lock; /* mutex protecting this data structure */
60 struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
65 static struct twl4030_madc_data *twl4030_madc;
67 struct twl4030_prescale_divider_ratios {
72 static const struct twl4030_prescale_divider_ratios
73 twl4030_divider_ratios[16] = {
74 {1, 1}, /* CHANNEL 0 No Prescaler */
75 {1, 1}, /* CHANNEL 1 No Prescaler */
76 {6, 10}, /* CHANNEL 2 */
77 {6, 10}, /* CHANNEL 3 */
78 {6, 10}, /* CHANNEL 4 */
79 {6, 10}, /* CHANNEL 5 */
80 {6, 10}, /* CHANNEL 6 */
81 {6, 10}, /* CHANNEL 7 */
82 {3, 14}, /* CHANNEL 8 */
83 {1, 3}, /* CHANNEL 9 */
84 {1, 1}, /* CHANNEL 10 No Prescaler */
85 {15, 100}, /* CHANNEL 11 */
86 {1, 4}, /* CHANNEL 12 */
87 {1, 1}, /* CHANNEL 13 Reserved channels */
88 {1, 1}, /* CHANNEL 14 Reseved channels */
89 {5, 11}, /* CHANNEL 15 */
94 * Conversion table from -3 to 55 degree Celcius
96 static int therm_tbl[] = {
97 30800, 29500, 28300, 27100,
98 26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900,
99 17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100,
100 11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310,
101 8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830,
102 5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170,
103 4040, 3910, 3790, 3670, 3550
107 * Structure containing the registers
108 * of different conversion methods supported by MADC.
109 * Hardware or RT real time conversion request initiated by external host
110 * processor for RT Signal conversions.
111 * External host processors can also request for non RT conversions
112 * SW1 and SW2 software conversions also called asynchronous or GPC request.
115 const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
116 [TWL4030_MADC_RT] = {
117 .sel = TWL4030_MADC_RTSELECT_LSB,
118 .avg = TWL4030_MADC_RTAVERAGE_LSB,
119 .rbase = TWL4030_MADC_RTCH0_LSB,
121 [TWL4030_MADC_SW1] = {
122 .sel = TWL4030_MADC_SW1SELECT_LSB,
123 .avg = TWL4030_MADC_SW1AVERAGE_LSB,
124 .rbase = TWL4030_MADC_GPCH0_LSB,
125 .ctrl = TWL4030_MADC_CTRL_SW1,
127 [TWL4030_MADC_SW2] = {
128 .sel = TWL4030_MADC_SW2SELECT_LSB,
129 .avg = TWL4030_MADC_SW2AVERAGE_LSB,
130 .rbase = TWL4030_MADC_GPCH0_LSB,
131 .ctrl = TWL4030_MADC_CTRL_SW2,
136 * Function to read a particular channel value.
137 * @madc - pointer to struct twl4030_madc_data
138 * @reg - lsb of ADC Channel
139 * If the i2c read fails it returns an error else returns 0.
141 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
146 * For each ADC channel, we have MSB and LSB register pair. MSB address
147 * is always LSB address+1. reg parameter is the address of LSB register
149 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
151 dev_err(madc->dev, "unable to read MSB register 0x%X\n",
155 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
157 dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
161 return (int)(((msb << 8) | lsb) >> 6);
165 * Return battery temperature
168 static int twl4030battery_temperature(int raw_volt)
171 int temp, curr, volt, res, ret;
173 volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
174 /* Getting and calculating the supply current in micro ampers */
175 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
179 curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
180 /* Getting and calculating the thermistor resistance in ohms */
181 res = volt * 1000 / curr;
182 /* calculating temperature */
183 for (temp = 58; temp >= 0; temp--) {
184 int actual = therm_tbl[temp];
186 if ((actual - res) >= 0)
193 static int twl4030battery_current(int raw_volt)
198 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
199 TWL4030_BCI_BCICTL1);
202 if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
203 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
204 else /* slope of 0.88 mV/mA */
205 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
208 * Function to read channel values
209 * @madc - pointer to twl4030_madc_data struct
210 * @reg_base - Base address of the first channel
211 * @Channels - 16 bit bitmap. If the bit is set, channel value is read
212 * @buf - The channel values are stored here. if read fails error
213 * @raw - Return raw values without conversion
215 * Returns the number of successfully read channels.
217 static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
218 u8 reg_base, unsigned
219 long channels, int *buf,
222 int count = 0, count_req = 0, i;
225 for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
226 reg = reg_base + 2 * i;
227 buf[i] = twl4030_madc_channel_raw_read(madc, reg);
230 "Unable to read register 0x%X\n", reg);
240 buf[i] = twl4030battery_current(buf[i]);
242 dev_err(madc->dev, "err reading current\n");
246 buf[i] = buf[i] - 750;
250 buf[i] = twl4030battery_temperature(buf[i]);
252 dev_err(madc->dev, "err reading temperature\n");
261 /* Analog Input (V) = conv_result * step_size / R
262 * conv_result = decimal value of 10-bit conversion
264 * step size = 1.5 / (2 ^ 10 -1)
265 * R = Prescaler ratio for input channels.
266 * Result given in mV hence multiplied by 1000.
268 buf[i] = (buf[i] * 3 * 1000 *
269 twl4030_divider_ratios[i].denominator)
271 twl4030_divider_ratios[i].numerator);
275 dev_err(madc->dev, "%d channel conversion failed\n", count_req);
282 * @madc - pointer to twl4030_madc_data struct
283 * @id - irq number to be enabled
284 * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
285 * corresponding to RT, SW1, SW2 conversion requests.
286 * If the i2c read fails it returns an error else returns 0.
288 static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
293 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
295 dev_err(madc->dev, "unable to read imr register 0x%X\n",
300 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
303 "unable to write imr register 0x%X\n", madc->imr);
313 * @madc - pointer to twl4030_madc_data struct
314 * @id - irq number to be disabled
315 * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
316 * corresponding to RT, SW1, SW2 conversion requests.
317 * Returns error if i2c read/write fails.
319 static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
324 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
326 dev_err(madc->dev, "unable to read imr register 0x%X\n",
331 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
334 "unable to write imr register 0x%X\n", madc->imr);
341 static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
343 struct twl4030_madc_data *madc = _madc;
344 const struct twl4030_madc_conversion_method *method;
347 struct twl4030_madc_request *r;
349 mutex_lock(&madc->lock);
350 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
352 dev_err(madc->dev, "unable to read isr register 0x%X\n",
356 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
358 dev_err(madc->dev, "unable to read imr register 0x%X\n",
363 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
364 if (!(isr_val & (1 << i)))
366 ret = twl4030_madc_disable_irq(madc, i);
368 dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
369 madc->requests[i].result_pending = 1;
371 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
372 r = &madc->requests[i];
373 /* No pending results for this method, move to next one */
374 if (!r->result_pending)
376 method = &twl4030_conversion_methods[r->method];
378 len = twl4030_madc_read_channels(madc, method->rbase,
379 r->channels, r->rbuf, r->raw);
380 /* Return results to caller */
381 if (r->func_cb != NULL) {
382 r->func_cb(len, r->channels, r->rbuf);
386 r->result_pending = 0;
389 mutex_unlock(&madc->lock);
395 * In case of error check whichever request is active
396 * and service the same.
398 for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
399 r = &madc->requests[i];
402 method = &twl4030_conversion_methods[r->method];
404 len = twl4030_madc_read_channels(madc, method->rbase,
405 r->channels, r->rbuf, r->raw);
406 /* Return results to caller */
407 if (r->func_cb != NULL) {
408 r->func_cb(len, r->channels, r->rbuf);
412 r->result_pending = 0;
415 mutex_unlock(&madc->lock);
420 static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
421 struct twl4030_madc_request *req)
423 struct twl4030_madc_request *p;
426 p = &madc->requests[req->method];
427 memcpy(p, req, sizeof(*req));
428 ret = twl4030_madc_enable_irq(madc, req->method);
430 dev_err(madc->dev, "enable irq failed!!\n");
438 * Function which enables the madc conversion
439 * by writing to the control register.
440 * @madc - pointer to twl4030_madc_data struct
441 * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
442 * corresponding to RT SW1 or SW2 conversion methods.
443 * Returns 0 if succeeds else a negative error value
445 static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
448 const struct twl4030_madc_conversion_method *method;
450 method = &twl4030_conversion_methods[conv_method];
451 switch (conv_method) {
452 case TWL4030_MADC_SW1:
453 case TWL4030_MADC_SW2:
454 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
455 TWL4030_MADC_SW_START, method->ctrl);
458 "unable to write ctrl register 0x%X\n",
471 * Function that waits for conversion to be ready
472 * @madc - pointer to twl4030_madc_data struct
473 * @timeout_ms - timeout value in milliseconds
474 * @status_reg - ctrl register
475 * returns 0 if succeeds else a negative error value
477 static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
478 unsigned int timeout_ms,
481 unsigned long timeout;
484 timeout = jiffies + msecs_to_jiffies(timeout_ms);
488 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg);
491 "unable to read status register 0x%X\n",
495 if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
497 usleep_range(500, 2000);
498 } while (!time_after(jiffies, timeout));
499 dev_err(madc->dev, "conversion timeout!\n");
505 * An exported function which can be called from other kernel drivers.
506 * @req twl4030_madc_request structure
507 * req->rbuf will be filled with read values of channels based on the
508 * channel index. If a particular channel reading fails there will
509 * be a negative error value in the corresponding array element.
510 * returns 0 if succeeds else error value
512 int twl4030_madc_conversion(struct twl4030_madc_request *req)
514 const struct twl4030_madc_conversion_method *method;
518 if (!req || !twl4030_madc)
521 mutex_lock(&twl4030_madc->lock);
522 if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
526 /* Do we have a conversion request ongoing */
527 if (twl4030_madc->requests[req->method].active) {
531 ch_msb = (req->channels >> 8) & 0xff;
532 ch_lsb = req->channels & 0xff;
533 method = &twl4030_conversion_methods[req->method];
534 /* Select channels to be converted */
535 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
537 dev_err(twl4030_madc->dev,
538 "unable to write sel register 0x%X\n", method->sel + 1);
541 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
543 dev_err(twl4030_madc->dev,
544 "unable to write sel register 0x%X\n", method->sel + 1);
547 /* Select averaging for all channels if do_avg is set */
549 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
550 ch_msb, method->avg + 1);
552 dev_err(twl4030_madc->dev,
553 "unable to write avg register 0x%X\n",
557 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
558 ch_lsb, method->avg);
560 dev_err(twl4030_madc->dev,
561 "unable to write sel reg 0x%X\n",
566 if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
567 ret = twl4030_madc_set_irq(twl4030_madc, req);
570 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
573 twl4030_madc->requests[req->method].active = 1;
577 /* With RT method we should not be here anymore */
578 if (req->method == TWL4030_MADC_RT) {
582 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
585 twl4030_madc->requests[req->method].active = 1;
586 /* Wait until conversion is ready (ctrl register returns EOC) */
587 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
589 twl4030_madc->requests[req->method].active = 0;
592 ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
593 req->channels, req->rbuf, req->raw);
594 twl4030_madc->requests[req->method].active = 0;
597 mutex_unlock(&twl4030_madc->lock);
601 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
604 * Return channel value
607 int twl4030_get_madc_conversion(int channel_no)
609 struct twl4030_madc_request req;
613 req.channels = (1 << channel_no);
614 req.method = TWL4030_MADC_SW2;
617 ret = twl4030_madc_conversion(&req);
620 if (req.rbuf[channel_no] > 0)
621 temp = req.rbuf[channel_no];
625 EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
628 * Function to enable or disable bias current for
629 * main battery type reading or temperature sensing
630 * @madc - pointer to twl4030_madc_data struct
631 * @chan - can be one of the two values
632 * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
633 * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature
635 * @on - enable or disable chan.
637 static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
643 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
644 ®val, TWL4030_BCI_BCICTL1);
646 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
647 TWL4030_BCI_BCICTL1);
651 regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
653 regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
654 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
655 regval, TWL4030_BCI_BCICTL1);
657 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
658 TWL4030_BCI_BCICTL1);
666 * Function that sets MADC software power on bit to enable MADC
667 * @madc - pointer to twl4030_madc_data struct
668 * @on - Enable or disable MADC software powen on bit.
669 * returns error if i2c read/write fails else 0
671 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
676 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
677 ®val, TWL4030_MADC_CTRL1);
679 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
684 regval |= TWL4030_MADC_MADCON;
686 regval &= ~TWL4030_MADC_MADCON;
687 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
689 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
698 * Initialize MADC and request for threaded irq
700 static int twl4030_madc_probe(struct platform_device *pdev)
702 struct twl4030_madc_data *madc;
703 struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
708 dev_err(&pdev->dev, "platform_data not available\n");
711 madc = kzalloc(sizeof(*madc), GFP_KERNEL);
715 madc->dev = &pdev->dev;
718 * Phoenix provides 2 interrupt lines. The first one is connected to
719 * the OMAP. The other one can be connected to the other processor such
720 * as modem. Hence two separate ISR and IMR registers.
722 madc->imr = (pdata->irq_line == 1) ?
723 TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
724 madc->isr = (pdata->irq_line == 1) ?
725 TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
726 ret = twl4030_madc_set_power(madc, 1);
729 ret = twl4030_madc_set_current_generator(madc, 0, 1);
731 goto err_current_generator;
733 ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
734 ®val, TWL4030_BCI_BCICTL1);
736 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
737 TWL4030_BCI_BCICTL1);
740 regval |= TWL4030_BCI_MESBAT;
741 ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
742 regval, TWL4030_BCI_BCICTL1);
744 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
745 TWL4030_BCI_BCICTL1);
749 /* Check that MADC clock is on */
750 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1);
752 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
757 /* If MADC clk is not on, turn it on */
758 if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
759 dev_info(&pdev->dev, "clk disabled, enabling\n");
760 regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
761 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
764 dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
770 platform_set_drvdata(pdev, madc);
771 mutex_init(&madc->lock);
772 ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
773 twl4030_madc_threaded_irq_handler,
774 IRQF_TRIGGER_RISING, "twl4030_madc", madc);
776 dev_dbg(&pdev->dev, "could not request irq\n");
782 twl4030_madc_set_current_generator(madc, 0, 0);
783 err_current_generator:
784 twl4030_madc_set_power(madc, 0);
791 static int twl4030_madc_remove(struct platform_device *pdev)
793 struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
795 free_irq(platform_get_irq(pdev, 0), madc);
796 twl4030_madc_set_current_generator(madc, 0, 0);
797 twl4030_madc_set_power(madc, 0);
803 static struct platform_driver twl4030_madc_driver = {
804 .probe = twl4030_madc_probe,
805 .remove = twl4030_madc_remove,
807 .name = "twl4030_madc",
808 .owner = THIS_MODULE,
812 module_platform_driver(twl4030_madc_driver);
814 MODULE_DESCRIPTION("TWL4030 ADC driver");
815 MODULE_LICENSE("GPL");
816 MODULE_AUTHOR("J Keerthy");
817 MODULE_ALIAS("platform:twl4030_madc");