33 #include <linux/device.h>
35 #include <linux/kernel.h>
38 #include <linux/slab.h>
41 #include <linux/module.h>
42 #include <linux/stddef.h>
44 #include <linux/bitops.h>
46 #include <linux/types.h>
74 twl4030_divider_ratios[16] = {
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
152 dev_err(madc->
dev,
"unable to read MSB register 0x%X\n",
158 dev_err(madc->
dev,
"unable to read LSB register 0x%X\n", reg);
169 static int twl4030battery_temperature(
int raw_volt)
182 res = volt * 1000 /
curr;
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)
218 u8 reg_base,
unsigned
221 int count = 0, count_req = 0,
i;
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]);
241 buf[
i] = buf[
i] - 750;
245 buf[
i] = twl4030battery_temperature(buf[i]);
247 dev_err(madc->
dev,
"err reading temperature\n");
263 buf[
i] = (buf[
i] * 3 * 1000 *
266 twl4030_divider_ratios[i].numerator);
270 dev_err(madc->
dev,
"%d channel conversion failed\n", count_req);
290 dev_err(madc->
dev,
"unable to read imr register 0x%X\n",
298 "unable to write imr register 0x%X\n", madc->
imr);
321 dev_err(madc->
dev,
"unable to read imr register 0x%X\n",
329 "unable to write imr register 0x%X\n", madc->
imr);
336 static irqreturn_t twl4030_madc_threaded_irq_handler(
int irq,
void *_madc)
347 dev_err(madc->
dev,
"unable to read isr register 0x%X\n",
353 dev_err(madc->
dev,
"unable to read imr register 0x%X\n",
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);
371 method = &twl4030_conversion_methods[r->
method];
373 len = twl4030_madc_read_channels(madc, method->
rbase,
397 method = &twl4030_conversion_methods[r->
method];
399 len = twl4030_madc_read_channels(madc, method->
rbase,
422 memcpy(p, req,
sizeof(*req));
423 ret = twl4030_madc_enable_irq(madc, req->
method);
445 method = &twl4030_conversion_methods[conv_method];
446 switch (conv_method) {
453 "unable to write ctrl register 0x%X\n",
486 "unable to read status register 0x%X\n",
513 if (!req || !twl4030_madc)
526 ch_msb = (req->
channels >> 8) & 0xff;
528 method = &twl4030_conversion_methods[req->
method];
533 "unable to write sel register 0x%X\n", method->
sel + 1);
539 "unable to write sel register 0x%X\n", method->
sel + 1);
545 ch_msb, method->
avg + 1);
548 "unable to write avg register 0x%X\n",
553 ch_lsb, method->
avg);
556 "unable to write sel reg 0x%X\n",
562 ret = twl4030_madc_set_irq(twl4030_madc, req);
565 ret = twl4030_madc_start_conversion(twl4030_madc, req->
method);
577 ret = twl4030_madc_start_conversion(twl4030_madc, req->
method);
582 ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->
ctrl);
587 ret = twl4030_madc_read_channels(twl4030_madc, method->
rbase,
615 if (req.
rbuf[channel_no] > 0)
616 temp = req.
rbuf[channel_no];
641 dev_err(madc->
dev,
"unable to read BCICTL1 reg 0x%X",
652 dev_err(madc->
dev,
"unable to write BCICTL1 reg 0x%X\n",
674 dev_err(madc->
dev,
"unable to read madc ctrl1 reg 0x%X\n",
684 dev_err(madc->
dev,
"unable to write madc ctrl1 reg 0x%X\n",
703 dev_err(&pdev->
dev,
"platform_data not available\n");
721 ret = twl4030_madc_set_power(madc, 1);
724 ret = twl4030_madc_set_current_generator(madc, 0, 1);
726 goto err_current_generator;
731 dev_err(&pdev->
dev,
"unable to read reg BCI CTL1 0x%X\n",
739 dev_err(&pdev->
dev,
"unable to write reg BCI Ctl1 0x%X\n",
747 dev_err(&pdev->
dev,
"unable to read reg GPBR1 0x%X\n",
759 dev_err(&pdev->
dev,
"unable to write reg GPBR1 0x%X\n",
765 platform_set_drvdata(pdev, madc);
768 twl4030_madc_threaded_irq_handler,
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);
793 platform_set_drvdata(pdev,
NULL);
794 twl4030_madc_set_current_generator(madc, 0, 0);
795 twl4030_madc_set_power(madc, 0);
802 .probe = twl4030_madc_probe,
803 .remove =
__exit_p(twl4030_madc_remove),
805 .name =
"twl4030_madc",