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>
51
* struct twl4030_madc_data - a container for madc info
52
* @dev - pointer to device structure for madc
53
* @lock - mutex protecting this data structure
54
* @requests - Array of request struct corresponding to SW1, SW2 and RT
55
* @imr - Interrupt mask register of MADC
56
* @isr - Interrupt status register of MADC
58
struct twl4030_madc_data {
60
struct mutex lock; /* mutex protecting this data structure */
61
struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
66
static struct twl4030_madc_data *twl4030_madc;
68
struct twl4030_prescale_divider_ratios {
73
static const struct twl4030_prescale_divider_ratios
74
twl4030_divider_ratios[16] = {
75
{1, 1}, /* CHANNEL 0 No Prescaler */
76
{1, 1}, /* CHANNEL 1 No Prescaler */
77
{6, 10}, /* CHANNEL 2 */
78
{6, 10}, /* CHANNEL 3 */
79
{6, 10}, /* CHANNEL 4 */
80
{6, 10}, /* CHANNEL 5 */
81
{6, 10}, /* CHANNEL 6 */
82
{6, 10}, /* CHANNEL 7 */
83
{3, 14}, /* CHANNEL 8 */
84
{1, 3}, /* CHANNEL 9 */
85
{1, 1}, /* CHANNEL 10 No Prescaler */
86
{15, 100}, /* CHANNEL 11 */
87
{1, 4}, /* CHANNEL 12 */
88
{1, 1}, /* CHANNEL 13 Reserved channels */
89
{1, 1}, /* CHANNEL 14 Reseved channels */
90
{5, 11}, /* CHANNEL 15 */
95
* Conversion table from -3 to 55 degree Celcius
97
static int therm_tbl[] = {
98
30800, 29500, 28300, 27100,
99
26000, 24900, 23900, 22900, 22000, 21100, 20300, 19400, 18700, 17900,
100
17200, 16500, 15900, 15300, 14700, 14100, 13600, 13100, 12600, 12100,
101
11600, 11200, 10800, 10400, 10000, 9630, 9280, 8950, 8620, 8310,
102
8020, 7730, 7460, 7200, 6950, 6710, 6470, 6250, 6040, 5830,
103
5640, 5450, 5260, 5090, 4920, 4760, 4600, 4450, 4310, 4170,
104
4040, 3910, 3790, 3670, 3550
108
* Structure containing the registers
109
* of different conversion methods supported by MADC.
110
* Hardware or RT real time conversion request initiated by external host
111
* processor for RT Signal conversions.
112
* External host processors can also request for non RT conversions
113
* SW1 and SW2 software conversions also called asynchronous or GPC request.
116
const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
117
[TWL4030_MADC_RT] = {
118
.sel = TWL4030_MADC_RTSELECT_LSB,
119
.avg = TWL4030_MADC_RTAVERAGE_LSB,
120
.rbase = TWL4030_MADC_RTCH0_LSB,
122
[TWL4030_MADC_SW1] = {
123
.sel = TWL4030_MADC_SW1SELECT_LSB,
124
.avg = TWL4030_MADC_SW1AVERAGE_LSB,
125
.rbase = TWL4030_MADC_GPCH0_LSB,
126
.ctrl = TWL4030_MADC_CTRL_SW1,
128
[TWL4030_MADC_SW2] = {
129
.sel = TWL4030_MADC_SW2SELECT_LSB,
130
.avg = TWL4030_MADC_SW2AVERAGE_LSB,
131
.rbase = TWL4030_MADC_GPCH0_LSB,
132
.ctrl = TWL4030_MADC_CTRL_SW2,
137
* Function to read a particular channel value.
138
* @madc - pointer to struct twl4030_madc_data
139
* @reg - lsb of ADC Channel
140
* If the i2c read fails it returns an error else returns 0.
142
static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
147
* For each ADC channel, we have MSB and LSB register pair. MSB address
148
* is always LSB address+1. reg parameter is the address of LSB register
150
ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
152
dev_err(madc->dev, "unable to read MSB register 0x%X\n",
156
ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
158
dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
162
return (int)(((msb << 8) | lsb) >> 6);
166
* Return battery temperature
169
static int twl4030battery_temperature(int raw_volt)
172
int temp, curr, volt, res, ret;
174
volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
175
/* Getting and calculating the supply current in micro ampers */
176
ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
180
curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
181
/* Getting and calculating the thermistor resistance in ohms */
182
res = volt * 1000 / curr;
183
/* calculating temperature */
184
for (temp = 58; temp >= 0; temp--) {
185
int actual = therm_tbl[temp];
187
if ((actual - res) >= 0)
194
static int twl4030battery_current(int raw_volt)
199
ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
200
TWL4030_BCI_BCICTL1);
203
if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
204
return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
205
else /* slope of 0.88 mV/mA */
206
return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
209
* Function to read channel values
210
* @madc - pointer to twl4030_madc_data struct
211
* @reg_base - Base address of the first channel
212
* @Channels - 16 bit bitmap. If the bit is set, channel value is read
213
* @buf - The channel values are stored here. if read fails error
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)
221
int count = 0, count_req = 0, i;
224
for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
225
reg = reg_base + 2 * i;
226
buf[i] = twl4030_madc_channel_raw_read(madc, reg);
229
"Unable to read register 0x%X\n", reg);
235
buf[i] = twl4030battery_current(buf[i]);
237
dev_err(madc->dev, "err reading current\n");
241
buf[i] = buf[i] - 750;
245
buf[i] = twl4030battery_temperature(buf[i]);
247
dev_err(madc->dev, "err reading temperature\n");
256
/* Analog Input (V) = conv_result * step_size / R
257
* conv_result = decimal value of 10-bit conversion
259
* step size = 1.5 / (2 ^ 10 -1)
260
* R = Prescaler ratio for input channels.
261
* Result given in mV hence multiplied by 1000.
263
buf[i] = (buf[i] * 3 * 1000 *
264
twl4030_divider_ratios[i].denominator)
266
twl4030_divider_ratios[i].numerator);
270
dev_err(madc->dev, "%d channel conversion failed\n", count_req);
277
* @madc - pointer to twl4030_madc_data struct
278
* @id - irq number to be enabled
279
* can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
280
* corresponding to RT, SW1, SW2 conversion requests.
281
* If the i2c read fails it returns an error else returns 0.
283
static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
288
ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
290
dev_err(madc->dev, "unable to read imr register 0x%X\n",
295
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
298
"unable to write imr register 0x%X\n", madc->imr);
308
* @madc - pointer to twl4030_madc_data struct
309
* @id - irq number to be disabled
310
* can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
311
* corresponding to RT, SW1, SW2 conversion requests.
312
* Returns error if i2c read/write fails.
314
static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
319
ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
321
dev_err(madc->dev, "unable to read imr register 0x%X\n",
326
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
329
"unable to write imr register 0x%X\n", madc->imr);
336
static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
338
struct twl4030_madc_data *madc = _madc;
339
const struct twl4030_madc_conversion_method *method;
342
struct twl4030_madc_request *r;
344
mutex_lock(&madc->lock);
345
ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
347
dev_err(madc->dev, "unable to read isr register 0x%X\n",
351
ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
353
dev_err(madc->dev, "unable to read imr register 0x%X\n",
358
for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
359
if (!(isr_val & (1 << i)))
361
ret = twl4030_madc_disable_irq(madc, i);
363
dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
364
madc->requests[i].result_pending = 1;
366
for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
367
r = &madc->requests[i];
368
/* No pending results for this method, move to next one */
369
if (!r->result_pending)
371
method = &twl4030_conversion_methods[r->method];
373
len = twl4030_madc_read_channels(madc, method->rbase,
374
r->channels, r->rbuf);
375
/* Return results to caller */
376
if (r->func_cb != NULL) {
377
r->func_cb(len, r->channels, r->rbuf);
381
r->result_pending = 0;
384
mutex_unlock(&madc->lock);
390
* In case of error check whichever request is active
391
* and service the same.
393
for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
394
r = &madc->requests[i];
397
method = &twl4030_conversion_methods[r->method];
399
len = twl4030_madc_read_channels(madc, method->rbase,
400
r->channels, r->rbuf);
401
/* Return results to caller */
402
if (r->func_cb != NULL) {
403
r->func_cb(len, r->channels, r->rbuf);
407
r->result_pending = 0;
410
mutex_unlock(&madc->lock);
415
static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
416
struct twl4030_madc_request *req)
418
struct twl4030_madc_request *p;
421
p = &madc->requests[req->method];
422
memcpy(p, req, sizeof(*req));
423
ret = twl4030_madc_enable_irq(madc, req->method);
425
dev_err(madc->dev, "enable irq failed!!\n");
433
* Function which enables the madc conversion
434
* by writing to the control register.
435
* @madc - pointer to twl4030_madc_data struct
436
* @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
437
* corresponding to RT SW1 or SW2 conversion methods.
438
* Returns 0 if succeeds else a negative error value
440
static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
443
const struct twl4030_madc_conversion_method *method;
445
method = &twl4030_conversion_methods[conv_method];
446
switch (conv_method) {
447
case TWL4030_MADC_SW1:
448
case TWL4030_MADC_SW2:
449
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
450
TWL4030_MADC_SW_START, method->ctrl);
453
"unable to write ctrl register 0x%X\n",
466
* Function that waits for conversion to be ready
467
* @madc - pointer to twl4030_madc_data struct
468
* @timeout_ms - timeout value in milliseconds
469
* @status_reg - ctrl register
470
* returns 0 if succeeds else a negative error value
472
static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
473
unsigned int timeout_ms,
476
unsigned long timeout;
479
timeout = jiffies + msecs_to_jiffies(timeout_ms);
483
ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, ®, status_reg);
486
"unable to read status register 0x%X\n",
490
if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
492
usleep_range(500, 2000);
493
} while (!time_after(jiffies, timeout));
494
dev_err(madc->dev, "conversion timeout!\n");
500
* An exported function which can be called from other kernel drivers.
501
* @req twl4030_madc_request structure
502
* req->rbuf will be filled with read values of channels based on the
503
* channel index. If a particular channel reading fails there will
504
* be a negative error value in the corresponding array element.
505
* returns 0 if succeeds else error value
507
int twl4030_madc_conversion(struct twl4030_madc_request *req)
509
const struct twl4030_madc_conversion_method *method;
513
if (!req || !twl4030_madc)
516
mutex_lock(&twl4030_madc->lock);
517
if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
521
/* Do we have a conversion request ongoing */
522
if (twl4030_madc->requests[req->method].active) {
526
ch_msb = (req->channels >> 8) & 0xff;
527
ch_lsb = req->channels & 0xff;
528
method = &twl4030_conversion_methods[req->method];
529
/* Select channels to be converted */
530
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
532
dev_err(twl4030_madc->dev,
533
"unable to write sel register 0x%X\n", method->sel + 1);
536
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
538
dev_err(twl4030_madc->dev,
539
"unable to write sel register 0x%X\n", method->sel + 1);
542
/* Select averaging for all channels if do_avg is set */
544
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
545
ch_msb, method->avg + 1);
547
dev_err(twl4030_madc->dev,
548
"unable to write avg register 0x%X\n",
552
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
553
ch_lsb, method->avg);
555
dev_err(twl4030_madc->dev,
556
"unable to write sel reg 0x%X\n",
561
if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
562
ret = twl4030_madc_set_irq(twl4030_madc, req);
565
ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
568
twl4030_madc->requests[req->method].active = 1;
572
/* With RT method we should not be here anymore */
573
if (req->method == TWL4030_MADC_RT) {
577
ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
580
twl4030_madc->requests[req->method].active = 1;
581
/* Wait until conversion is ready (ctrl register returns EOC) */
582
ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
584
twl4030_madc->requests[req->method].active = 0;
587
ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
588
req->channels, req->rbuf);
589
twl4030_madc->requests[req->method].active = 0;
592
mutex_unlock(&twl4030_madc->lock);
596
EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
599
* Return channel value
602
int twl4030_get_madc_conversion(int channel_no)
604
struct twl4030_madc_request req;
608
req.channels = (1 << channel_no);
609
req.method = TWL4030_MADC_SW2;
612
ret = twl4030_madc_conversion(&req);
615
if (req.rbuf[channel_no] > 0)
616
temp = req.rbuf[channel_no];
620
EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
623
* Function to enable or disable bias current for
624
* main battery type reading or temperature sensing
625
* @madc - pointer to twl4030_madc_data struct
626
* @chan - can be one of the two values
627
* TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
628
* TWL4030_BCI_TYPEN - Enables bias current for main battery temperature
630
* @on - enable or disable chan.
632
static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
638
ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
639
®val, TWL4030_BCI_BCICTL1);
641
dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
642
TWL4030_BCI_BCICTL1);
646
regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
648
regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
649
ret = twl_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE,
650
regval, TWL4030_BCI_BCICTL1);
652
dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
653
TWL4030_BCI_BCICTL1);
661
* Function that sets MADC software power on bit to enable MADC
662
* @madc - pointer to twl4030_madc_data struct
663
* @on - Enable or disable MADC software powen on bit.
664
* returns error if i2c read/write fails else 0
666
static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
671
ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
672
®val, TWL4030_MADC_CTRL1);
674
dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
679
regval |= TWL4030_MADC_MADCON;
681
regval &= ~TWL4030_MADC_MADCON;
682
ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
684
dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
693
* Initialize MADC and request for threaded irq
695
static int __devinit twl4030_madc_probe(struct platform_device *pdev)
697
struct twl4030_madc_data *madc;
698
struct twl4030_madc_platform_data *pdata = pdev->dev.platform_data;
703
dev_err(&pdev->dev, "platform_data not available\n");
706
madc = kzalloc(sizeof(*madc), GFP_KERNEL);
710
madc->dev = &pdev->dev;
713
* Phoenix provides 2 interrupt lines. The first one is connected to
714
* the OMAP. The other one can be connected to the other processor such
715
* as modem. Hence two separate ISR and IMR registers.
717
madc->imr = (pdata->irq_line == 1) ?
718
TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
719
madc->isr = (pdata->irq_line == 1) ?
720
TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
721
ret = twl4030_madc_set_power(madc, 1);
724
ret = twl4030_madc_set_current_generator(madc, 0, 1);
726
goto err_current_generator;
728
ret = twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
729
®val, TWL4030_BCI_BCICTL1);
731
dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
732
TWL4030_BCI_BCICTL1);
735
regval |= TWL4030_BCI_MESBAT;
736
ret = twl_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE,
737
regval, TWL4030_BCI_BCICTL1);
739
dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
740
TWL4030_BCI_BCICTL1);
744
/* Check that MADC clock is on */
745
ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, ®val, TWL4030_REG_GPBR1);
747
dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
752
/* If MADC clk is not on, turn it on */
753
if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
754
dev_info(&pdev->dev, "clk disabled, enabling\n");
755
regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
756
ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
759
dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
765
platform_set_drvdata(pdev, madc);
766
mutex_init(&madc->lock);
767
ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
768
twl4030_madc_threaded_irq_handler,
769
IRQF_TRIGGER_RISING, "twl4030_madc", madc);
771
dev_dbg(&pdev->dev, "could not request irq\n");
777
platform_set_drvdata(pdev, NULL);
779
twl4030_madc_set_current_generator(madc, 0, 0);
780
err_current_generator:
781
twl4030_madc_set_power(madc, 0);
788
static int __devexit twl4030_madc_remove(struct platform_device *pdev)
790
struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
792
free_irq(platform_get_irq(pdev, 0), madc);
793
platform_set_drvdata(pdev, NULL);
794
twl4030_madc_set_current_generator(madc, 0, 0);
795
twl4030_madc_set_power(madc, 0);
801
static struct platform_driver twl4030_madc_driver = {
802
.probe = twl4030_madc_probe,
803
.remove = __exit_p(twl4030_madc_remove),
805
.name = "twl4030_madc",
806
.owner = THIS_MODULE,
810
static int __init twl4030_madc_init(void)
812
return platform_driver_register(&twl4030_madc_driver);
815
module_init(twl4030_madc_init);
817
static void __exit twl4030_madc_exit(void)
819
platform_driver_unregister(&twl4030_madc_driver);
822
module_exit(twl4030_madc_exit);
824
MODULE_DESCRIPTION("TWL4030 ADC driver");
825
MODULE_LICENSE("GPL");
826
MODULE_AUTHOR("J Keerthy");
827
MODULE_ALIAS("platform:twl4030_madc");