2 * drivers/i2c/chips/twl6030-gpadc.c
4 * TWL6030 GPADC module driver
6 * Copyright (C) 2009 Texas Instruments Inc.
7 * Nishant Kamat <nskamat@ti.com>
9 * Based on twl4030-madc.c
10 * Copyright (C) 2008 Nokia Corporation
11 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * version 2 as published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/module.h>
34 #include <linux/delay.h>
36 #include <linux/platform_device.h>
37 #include <linux/miscdevice.h>
38 #include <linux/slab.h>
39 #include <linux/hwmon-sysfs.h>
40 #include <linux/i2c/twl.h>
41 #include <linux/i2c/twl6030-gpadc.h>
43 #include <linux/uaccess.h>
45 #define TWL6030_GPADC_PFX "twl6030-gpadc: "
46 #define ENABLE_GPADC 0x02
47 #define REG_TOGGLE1 0x90
48 #define GPADCS (1 << 1)
49 #define GPADCR (1 << 0)
51 #define SCALE (1 << 15)
53 struct twl6030_chnl_calib {
58 struct twl6030_ideal_code {
63 struct twl6032_chnl_calib {
69 struct twl6032_ideal_code {
76 static struct twl6030_chnl_calib
77 twl6030_calib_tbl[GPADC_MAX_CHANNELS];
78 static const u32 calibration_bit_map = 0x47FF;
80 /* Trim address where measured offset from ideal code is stored */
81 static const u8 twl6030_trim_addr[GPADC_MAX_CHANNELS] = {
92 0xD7, /* CHANNEL 10 */
93 0x00, /* CHANNEL 11 */
94 0x00, /* CHANNEL 12 */
95 0x00, /* CHANNEL 13 */
96 0xDB, /* CHANNEL 14 */
97 0x00, /* CHANNEL 15 */
98 0x00, /* CHANNEL 16 */
101 #define TWL6032_GPADC_TRIM1 0xCD
102 #define TWL6032_GPADC_TRIM2 0xCE
103 #define TWL6032_GPADC_TRIM3 0xCF
104 #define TWL6032_GPADC_TRIM4 0xD0
105 #define TWL6032_GPADC_TRIM5 0xD1
106 #define TWL6032_GPADC_TRIM6 0xD2
107 #define TWL6032_GPADC_TRIM7 0xD3
108 #define TWL6032_GPADC_TRIM8 0xD4
109 #define TWL6032_GPADC_TRIM9 0xD5
110 #define TWL6032_GPADC_TRIM10 0xD6
111 #define TWL6032_GPADC_TRIM11 0xD7
112 #define TWL6032_GPADC_TRIM12 0xD8
113 #define TWL6032_GPADC_TRIM13 0xD9
114 #define TWL6032_GPADC_TRIM14 0xDA
115 #define TWL6032_GPADC_TRIM15 0xDB
116 #define TWL6032_GPADC_TRIM16 0xDC
117 #define TWL6032_GPADC_TRIM19 0xFD
120 * actual scaler gain is multiplied by 8 for fixed point operation
122 * For channels 0, 1, 3, 4, 5, 6, 12, 13
124 * is used, as scaler is Vref * divider
127 static const u16 twl6030_gain[TWL6030_GPADC_MAX_CHANNELS] = {
168 * calibration not needed for channel 11, 12, 13, 15 and 16
169 * calibration offset is same for channel 1, 3, 4, 5
171 static const struct twl6030_ideal_code
172 twl6030_ideal[GPADC_MAX_CHANNELS] = {
173 {116, 745}, /* CHANNEL 0 */
174 {82, 900}, /* CHANNEL 1 */
175 {55, 818}, /* CHANNEL 2 */
176 {82, 900}, /* CHANNEL 3 */
177 {82, 900}, /* CHANNEL 4 */
178 {82, 900}, /* CHANNEL 5 */
179 {82, 900}, /* CHANNEL 6 */
180 {614, 941}, /* CHANNEL 7 */
181 {82, 688}, /* CHANNEL 8 */
182 {182, 818}, /* CHANNEL 9 */
183 {149, 818}, /* CHANNEL 10 */
184 {0, 0}, /* CHANNEL 11 */
185 {0, 0}, /* CHANNEL 12 */
186 {0, 0}, /* CHANNEL 13 */
187 {48, 714}, /* CHANNEL 14 */
188 {0, 0}, /* CHANNEL 15 */
189 {0, 0}, /* CHANNEL 16 */
192 /* PhoenixLite has a different calibration sysem to the Phoenix */
193 static const struct twl6032_ideal_code
194 twl6032_ideal[GPADC_MAX_CHANNELS] = {
285 {}, /* CHANNEL 15 - UNUSED */
286 {}, /* CHANNEL 16 - UNUSED */
287 {}, /* CHANNEL 17 - UNUSED */
297 struct twl6030_gpadc_data {
300 struct work_struct ws;
301 struct twl6030_gpadc_request requests[TWL6030_GPADC_NUM_METHODS];
303 struct twl6032_chnl_calib *twl6032_cal_tbl;
304 unsigned long features;
307 static struct twl6030_gpadc_data *the_gpadc;
310 struct twl6030_gpadc_conversion_method twl6030_conversion_methods_table[] = {
311 [TWL6030_GPADC_RT] = {
312 .sel = TWL6030_GPADC_RTSELECT_LSB,
313 .rbase = TWL6030_GPADC_RTCH0_LSB,
314 .mask = TWL6030_GPADC_RT_SW1_EOC_MASK,
317 * TWL6030_GPADC_SW1 is not supported as
318 * interrupt from RT and SW1 cannot be differentiated
320 [TWL6030_GPADC_SW2] = {
321 .rbase = TWL6030_GPADC_GPCH0_LSB,
322 .ctrl = TWL6030_GPADC_CTRL_P2,
323 .enable = TWL6030_GPADC_CTRL_P2_SP2,
324 .mask = TWL6030_GPADC_SW2_EOC_MASK,
329 struct twl6030_gpadc_conversion_method twl6032_conversion_methods_table[] = {
330 [TWL6030_GPADC_RT] = {
331 .sel = TWL6032_GPADC_RTSELECT_LSB,
332 .rbase = TWL6032_RTCH0_LSB,
333 .mask = TWL6032_GPADC_RT_EOC_MASK,
335 [TWL6030_GPADC_SW2] = {
336 .sel = TWL6032_GPADC_GPSELECT_ISB,
337 .rbase = TWL6032_GPCH0_LSB,
338 .ctrl = TWL6032_GPADC_CTRL_P1,
339 .enable = TWL6030_GPADC_CTRL_P1_SP1,
340 .mask = TWL6032_GPADC_SW_EOC_MASK,
345 struct twl6030_gpadc_conversion_method *twl6030_conversion_methods;
347 static ssize_t show_gain(struct device *dev,
348 struct device_attribute *devattr, char *buf)
350 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
354 value = twl6030_calib_tbl[attr->index].gain_error;
356 status = sprintf(buf, "%d\n", value);
360 static ssize_t set_gain(struct device *dev,
361 struct device_attribute *devattr, const char *buf, size_t count)
365 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
367 if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
371 twl6030_calib_tbl[attr->index].gain_error = val;
376 static ssize_t show_offset(struct device *dev,
377 struct device_attribute *devattr, char *buf)
379 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
383 value = twl6030_calib_tbl[attr->index].offset_error;
385 status = sprintf(buf, "%d\n", value);
389 static ssize_t set_offset(struct device *dev,
390 struct device_attribute *devattr, const char *buf, size_t count)
394 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
396 if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
400 twl6030_calib_tbl[attr->index].offset_error = val;
405 static int twl6030_gpadc_read(struct twl6030_gpadc_data *gpadc, u8 reg)
410 ret = twl_i2c_read_u8(TWL_MODULE_MADC, &val, reg);
412 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
419 static void twl6030_gpadc_write(struct twl6030_gpadc_data *gpadc,
424 ret = twl_i2c_write_u8(TWL_MODULE_MADC, val, reg);
426 dev_err(gpadc->dev, "unable to write register 0x%X\n", reg);
429 static int twl6030_gpadc_channel_raw_read(struct twl6030_gpadc_data *gpadc,
434 /* For each ADC channel, we have MSB and LSB register pair.
435 * MSB address is always LSB address+1. reg parameter is the
436 * addr of LSB register
438 msb = twl6030_gpadc_read(gpadc, reg + 1);
439 lsb = twl6030_gpadc_read(gpadc, reg);
440 return (int)((msb << 8) | lsb);
443 static int twl6030_gpadc_read_channels(struct twl6030_gpadc_data *gpadc,
444 u8 reg_base, u32 channels, struct twl6030_gpadc_request *req)
452 s32 raw_channel_value;
454 channels = ~channels;
455 if (gpadc->features & TWL6032_SUBCLASS) {
456 for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++) {
457 if (channels & BIT(i))
460 reg = reg_base + 2 * count;
462 dev_dbg(gpadc->dev, "GPADC chn: %d\n", i);
463 raw_code = twl6030_gpadc_channel_raw_read(gpadc, reg);
464 dev_dbg(gpadc->dev, "GPADC raw: %d\n", raw_code);
466 req->buf[i].raw_code = raw_code;
468 /* No correction for channels 15-17 */
469 if (unlikely((i >= 15) && (i <= 17))) {
470 raw_channel_value = raw_code;
471 req->buf[i].code = raw_code;
472 req->rbuf[i] = raw_code;
474 raw_channel_value = (raw_code *
475 gpadc->twl6032_cal_tbl[i].gain);
477 /* Shift back into mV range */
478 raw_channel_value /= 1000;
480 req->buf[i].code = corrected_code =
482 gpadc->twl6032_cal_tbl[i].offset_error) /
483 gpadc->twl6032_cal_tbl[i].gain_error;
485 dev_dbg(gpadc->dev, "GPADC cor: %d\n",
488 req->rbuf[i] = corrected_code *
489 gpadc->twl6032_cal_tbl[i].gain;
491 /* Shift back into mV range */
492 req->rbuf[i] /= 1000;
494 req->buf[i].raw_channel_value = raw_channel_value;
495 dev_dbg(gpadc->dev, "GPADC val: %d\n", req->rbuf[i]);
498 for (i = 0; i < TWL6030_GPADC_MAX_CHANNELS; i++) {
499 if (channels & BIT(i))
501 reg = reg_base + 2 * i;
502 raw_code = twl6030_gpadc_channel_raw_read(gpadc, reg);
503 req->buf[i].raw_code = raw_code;
506 * multiply by 1000 to convert the unit to milli
507 * division by 1024 (>> 10) for 10 bit ADC
508 * division by 8 (>> 3) for actual scaler gain
510 raw_channel_value = (raw_code * twl6030_gain[i]
512 req->buf[i].raw_channel_value = raw_channel_value;
514 if (~calibration_bit_map & BIT(i)) {
515 req->buf[i].code = raw_code;
516 req->rbuf[i] = raw_channel_value;
518 gain_error = twl6030_calib_tbl[i].gain_error;
519 offset_error = twl6030_calib_tbl[i].offset_error;
520 req->buf[i].code = corrected_code =
521 (raw_code * SCALE - offset_error) /
523 req->rbuf[i] = (corrected_code * twl6030_gain[i]
526 dev_dbg(gpadc->dev, "GPADC val: %d", req->rbuf[i]);
532 static void twl6030_gpadc_enable_irq(u16 method)
534 twl6030_interrupt_unmask(twl6030_conversion_methods[method].mask,
536 twl6030_interrupt_unmask(twl6030_conversion_methods[method].mask,
540 static void twl6030_gpadc_disable_irq(u16 method)
542 twl6030_interrupt_mask(twl6030_conversion_methods[method].mask,
544 twl6030_interrupt_mask(twl6030_conversion_methods[method].mask,
548 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *_req)
550 struct twl6030_gpadc_request *req = _req;
552 #ifdef CONFIG_LOCKDEP
553 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
554 * we don't want and can't tolerate. Although it might be
555 * friendlier not to borrow this thread context...
560 /* Find the cause of the interrupt and enable the pending
561 bit for the corresponding method */
562 twl6030_gpadc_disable_irq(req->method);
563 req->result_pending = 1;
565 schedule_work(&the_gpadc->ws);
570 static void twl6030_gpadc_work(struct work_struct *ws)
572 const struct twl6030_gpadc_conversion_method *method;
573 struct twl6030_gpadc_data *gpadc;
574 struct twl6030_gpadc_request *r;
577 gpadc = container_of(ws, struct twl6030_gpadc_data, ws);
578 mutex_lock(&gpadc->lock);
580 for (i = 0; i < TWL6030_GPADC_NUM_METHODS; i++) {
582 r = &gpadc->requests[i];
584 /* No pending results for this method, move to next one */
585 if (!r->result_pending)
588 method = &twl6030_conversion_methods[r->method];
591 len = twl6030_gpadc_read_channels(gpadc, method->rbase,
594 /* Return results to caller */
595 if (r->func_cb != NULL) {
601 r->result_pending = 0;
605 mutex_unlock(&gpadc->lock);
608 static int twl6030_gpadc_set_irq(struct twl6030_gpadc_data *gpadc,
609 struct twl6030_gpadc_request *req)
611 struct twl6030_gpadc_request *p;
613 p = &gpadc->requests[req->method];
614 p->channels = req->channels;
615 p->method = req->method;
616 p->func_cb = req->func_cb;
619 twl6030_gpadc_enable_irq(req->method);
625 twl6030_gpadc_start_conversion(struct twl6030_gpadc_data *gpadc,
628 const struct twl6030_gpadc_conversion_method *method;
630 method = &twl6030_conversion_methods[conv_method];
631 twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCS, REG_TOGGLE1);
633 switch (conv_method) {
634 case TWL6030_GPADC_SW2:
635 twl6030_gpadc_write(gpadc, method->ctrl, method->enable);
637 case TWL6030_GPADC_RT:
643 static int twl6030_gpadc_is_conversion_ready(
644 struct twl6030_gpadc_data *gpadc, u8 status_reg)
646 u8 reg = twl6030_gpadc_read(gpadc, status_reg);
647 return !(reg & TWL6030_GPADC_BUSY) && (reg & TWL6030_GPADC_EOC_SW);
650 static int twl6030_gpadc_wait_conversion_ready(
651 struct twl6030_gpadc_data *gpadc,
652 unsigned int timeout_ms, u8 status_reg)
654 unsigned long timeout;
656 timeout = jiffies + msecs_to_jiffies(timeout_ms);
658 if (twl6030_gpadc_is_conversion_ready(gpadc, status_reg))
660 msleep_interruptible(1);
661 } while (!time_after(jiffies, timeout));
663 /* one more checking against scheduler-caused timeout */
664 if (twl6030_gpadc_is_conversion_ready(gpadc, status_reg))
670 /* locks held by caller */
671 static int _twl6030_gpadc_conversion(struct twl6030_gpadc_request *req,
672 const struct twl6030_gpadc_conversion_method *method)
674 u8 ch_msb, ch_lsb, ch_isb;
677 if (req->method == TWL6030_GPADC_RT) {
678 ch_msb = (req->channels >> 16) & 0x01;
679 ch_isb = (req->channels >> 8) & 0xff;
680 ch_lsb = req->channels & 0xff;
681 twl6030_gpadc_write(the_gpadc, method->sel + 2, ch_msb);
682 twl6030_gpadc_write(the_gpadc, method->sel + 1, ch_isb);
683 twl6030_gpadc_write(the_gpadc, method->sel, ch_lsb);
686 if ((req->type == TWL6030_GPADC_IRQ_ONESHOT) &&
687 (req->func_cb != NULL)) {
688 twl6030_gpadc_set_irq(the_gpadc, req);
689 twl6030_gpadc_start_conversion(the_gpadc, req->method);
690 the_gpadc->requests[req->method].active = 1;
695 /* With RT method we should not be here anymore */
696 if (req->method == TWL6030_GPADC_RT) {
701 twl6030_gpadc_start_conversion(the_gpadc, req->method);
702 the_gpadc->requests[req->method].active = 1;
704 /* Wait until conversion is ready (ctrl register returns EOC) */
705 ret = twl6030_gpadc_wait_conversion_ready(the_gpadc, 5, method->ctrl);
707 dev_dbg(the_gpadc->dev, "conversion timeout!\n");
708 the_gpadc->requests[req->method].active = 0;
712 ret = twl6030_gpadc_read_channels(the_gpadc, method->rbase,
714 the_gpadc->requests[req->method].active = 0;
719 /* locks held by caller */
720 static int _twl6032_gpadc_conversion(struct twl6030_gpadc_request *req,
721 const struct twl6030_gpadc_conversion_method *method)
723 int i, ret, count = 0, channelcnt = 0;
724 u8 ch_msb, ch_lsb, ch_isb;
726 if ((req->type == TWL6030_GPADC_IRQ_ONESHOT) &&
727 (req->func_cb == NULL)) {
732 for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++)
733 if (req->channels & BIT(i))
736 if (req->method == TWL6030_GPADC_RT) {
738 * For the TWL6032 real time conversion
739 * maximum channels count is 2
741 if ((req->type != TWL6030_GPADC_IRQ_ONESHOT) ||
747 ch_msb = (req->channels >> 16) & 0x07;
748 ch_isb = (req->channels >> 8) & 0xff;
749 ch_lsb = req->channels & 0xff;
750 twl6030_gpadc_write(the_gpadc, method->sel + 2, ch_msb);
751 twl6030_gpadc_write(the_gpadc, method->sel + 1, ch_isb);
752 twl6030_gpadc_write(the_gpadc, method->sel, ch_lsb);
756 * For the TWL6032 Asynchronous Conversion
757 * maximum channels count is 1
759 if ((req->method == TWL6030_GPADC_SW2) &&
760 (req->type == TWL6030_GPADC_IRQ_ONESHOT)) {
761 if (channelcnt > 1) {
766 for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++) {
767 if (!(req->channels & BIT(i)))
770 /* select the ADC channel to be read */
771 twl6030_gpadc_write(the_gpadc, method->sel, i);
775 if (req->type == TWL6030_GPADC_IRQ_ONESHOT) {
776 twl6030_gpadc_set_irq(the_gpadc, req);
777 twl6030_gpadc_start_conversion(the_gpadc, req->method);
778 the_gpadc->requests[req->method].active = 1;
783 for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++) {
784 if (!(req->channels & BIT(i)))
787 /* select the ADC channel to be read */
788 twl6030_gpadc_write(the_gpadc, method->sel, i);
790 twl6030_gpadc_start_conversion(the_gpadc, req->method);
791 the_gpadc->requests[req->method].active = 1;
793 /* Wait until conversion is ready (ctrl register is EOC) */
794 ret = twl6030_gpadc_wait_conversion_ready(the_gpadc, 5,
797 dev_dbg(the_gpadc->dev, "conversion timeout!\n");
798 the_gpadc->requests[req->method].active = 0;
802 ret = twl6030_gpadc_read_channels(the_gpadc, method->rbase,
805 dev_err(the_gpadc->dev, "%s: channel error %d\n",
809 the_gpadc->requests[req->method].active = 0;
816 int twl6030_gpadc_conversion(struct twl6030_gpadc_request *req)
818 const struct twl6030_gpadc_conversion_method *method;
827 mutex_lock(&the_gpadc->lock);
829 if (req->method >= TWL6030_GPADC_NUM_METHODS) {
830 dev_err(the_gpadc->dev, "unsupported conversion method\n");
835 /* Do we have a conversion request ongoing */
836 if (the_gpadc->requests[req->method].active) {
841 method = &twl6030_conversion_methods[req->method];
843 if (the_gpadc->features & TWL6032_SUBCLASS)
844 ret = _twl6032_gpadc_conversion(req, method);
846 ret = _twl6030_gpadc_conversion(req, method);
849 mutex_unlock(&the_gpadc->lock);
853 EXPORT_SYMBOL(twl6030_gpadc_conversion);
855 static ssize_t show_channel(struct device *dev,
856 struct device_attribute *devattr, char *buf)
858 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
859 struct twl6030_gpadc_request req;
863 req.channels = (1 << attr->index);
864 req.method = TWL6030_GPADC_SW2;
867 ret = twl6030_gpadc_conversion(&req);
871 if (req.rbuf[attr->index] > 0)
872 temp = req.rbuf[attr->index];
874 ret = sprintf(buf, "%d\n", temp);
879 static ssize_t show_raw_code(struct device *dev,
880 struct device_attribute *devattr, char *buf)
882 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
883 struct twl6030_gpadc_request req;
887 req.channels = (1 << attr->index);
888 req.method = TWL6030_GPADC_SW2;
891 ret = twl6030_gpadc_conversion(&req);
895 if (req.buf[attr->index].raw_channel_value > 0)
896 temp = req.buf[attr->index].raw_code;
898 ret = sprintf(buf, "%d\n", temp);
903 #define in_gain(index) \
904 static SENSOR_DEVICE_ATTR(in##index##_gain, S_IRUGO|S_IWUSR, show_gain, \
906 static SENSOR_DEVICE_ATTR(in##index##_offset, S_IRUGO|S_IWUSR, show_offset, \
927 #define in_channel(index) \
928 static SENSOR_DEVICE_ATTR(in##index##_channel, S_IRUGO, show_channel, \
930 static SENSOR_DEVICE_ATTR(in##index##_raw_code, S_IRUGO, show_raw_code, \
954 &sensor_dev_attr_in##X##_gain.dev_attr.attr, \
955 &sensor_dev_attr_in##X##_offset.dev_attr.attr \
957 #define IN_ATTRS_CHANNEL(X)\
958 &sensor_dev_attr_in##X##_channel.dev_attr.attr, \
959 &sensor_dev_attr_in##X##_raw_code.dev_attr.attr \
961 static struct attribute *twl6030_gpadc_attributes[] = {
989 IN_ATTRS_CHANNEL(10),
990 IN_ATTRS_CHANNEL(11),
991 IN_ATTRS_CHANNEL(12),
992 IN_ATTRS_CHANNEL(13),
993 IN_ATTRS_CHANNEL(14),
994 IN_ATTRS_CHANNEL(15),
995 IN_ATTRS_CHANNEL(16),
996 IN_ATTRS_CHANNEL(17),
997 IN_ATTRS_CHANNEL(18),
1001 static const struct attribute_group twl6030_gpadc_group = {
1002 .attrs = twl6030_gpadc_attributes,
1005 static long twl6030_gpadc_ioctl(struct file *filp, unsigned int cmd,
1008 struct twl6030_gpadc_user_parms par;
1011 ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
1013 dev_dbg(the_gpadc->dev, "copy_from_user: %d\n", ret);
1018 case TWL6030_GPADC_IOCX_ADC_READ:
1019 case TWL6030_GPADC_IOCX_ADC_RAW_READ: {
1020 struct twl6030_gpadc_request req;
1021 if (the_gpadc->features & TWL6032_SUBCLASS) {
1022 if (par.channel >= TWL6032_GPADC_MAX_CHANNELS)
1025 if (par.channel >= TWL6030_GPADC_MAX_CHANNELS)
1029 req.channels = (1 << par.channel);
1030 req.method = TWL6030_GPADC_SW2;
1033 val = twl6030_gpadc_conversion(&req);
1034 if (likely(val > 0)) {
1036 if (cmd == TWL6030_GPADC_IOCX_ADC_READ)
1037 par.result = (u16)req.rbuf[par.channel];
1039 par.result = (u16)req.buf[par.channel].raw_code;
1041 } else if (val == 0) {
1042 par.status = -ENODATA;
1052 ret = copy_to_user((void __user *) arg, &par, sizeof(par));
1054 dev_dbg(the_gpadc->dev, "copy_to_user: %d\n", ret);
1061 static const struct file_operations twl6030_gpadc_fileops = {
1062 .owner = THIS_MODULE,
1063 .unlocked_ioctl = twl6030_gpadc_ioctl
1066 static struct miscdevice twl6030_gpadc_device = {
1067 .minor = MISC_DYNAMIC_MINOR,
1068 .name = "twl6030-gpadc",
1069 .fops = &twl6030_gpadc_fileops
1072 static int twl6030_calibration(void)
1074 s8 delta_error1 = 0, delta_error2 = 0;
1075 s16 ideal_code1, ideal_code2;
1081 for (index = 0; index < TWL6030_GPADC_MAX_CHANNELS; index++) {
1082 if (~calibration_bit_map & (1 << index))
1085 ret = twl_i2c_read_u8(TWL6030_MODULE_ID2, &delta_error1,
1086 twl6030_trim_addr[index]);
1090 twl_i2c_read_u8(TWL6030_MODULE_ID2, &delta_error2,
1091 (twl6030_trim_addr[index] + 1));
1095 /* convert 7 bit to 8 bit signed number */
1096 delta_error1 = ((s8)(delta_error1 << 1) >> 1);
1097 delta_error2 = ((s8)(delta_error2 << 1) >> 1);
1098 ideal_code1 = twl6030_ideal[index].code1;
1099 ideal_code2 = twl6030_ideal[index].code2;
1101 gain_error_1 = (delta_error2 - delta_error1) * SCALE
1102 / (ideal_code2 - ideal_code1);
1103 offset_error = delta_error1 * SCALE - gain_error_1
1105 twl6030_calib_tbl[index].gain_error = gain_error_1 + SCALE;
1106 twl6030_calib_tbl[index].offset_error = offset_error;
1112 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
1114 int chn, d1 = 0, d2 = 0, b, k, gain, x1, x2, temp;
1118 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs + 1,
1119 TWL6032_GPADC_TRIM1, 16);
1123 /* Loop to calculate the value needed for returning voltages from
1126 * gain is calculated to 3 decimal places fixed point.
1128 for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
1143 d1 = (trim_regs[3] & 0x1F) << 2;
1144 d1 |= (trim_regs[1] & 0x06) >> 1;
1145 if (trim_regs[1] & 0x01)
1149 d2 = (trim_regs[4] & 0x3F) << 2;
1150 d2 |= (trim_regs[2] & 0x06) >> 1;
1151 if (trim_regs[2] & 0x01)
1156 temp = (trim_regs[3] & 0x1F) << 2;
1157 temp |= (trim_regs[1] & 0x06) >> 1;
1158 if (trim_regs[1] & 0x01)
1161 d1 = (trim_regs[8] & 0x18) << 1;
1162 d1 |= (trim_regs[7] & 0x1E) >> 1;
1163 if (trim_regs[7] & 0x01)
1169 temp = (trim_regs[4] & 0x3F) << 2;
1170 temp |= (trim_regs[2] & 0x06) >> 1;
1171 if (trim_regs[2] & 0x01)
1174 d2 = (trim_regs[10] & 0x1F) << 2;
1175 d2 |= (trim_regs[8] & 0x06) >> 1;
1176 if (trim_regs[8] & 0x01)
1183 temp = (trim_regs[3] & 0x1F) << 2;
1184 temp |= (trim_regs[1] & 0x06) >> 1;
1185 if (trim_regs[1] & 0x01)
1188 d1 = (trim_regs[14] & 0x18) << 1;
1189 d1 |= (trim_regs[12] & 0x1E) >> 1;
1190 if (trim_regs[12] & 0x01)
1196 temp = (trim_regs[4] & 0x3F) << 2;
1197 temp |= (trim_regs[2] & 0x06) >> 1;
1198 if (trim_regs[2] & 0x01)
1201 d2 = (trim_regs[16] & 0x1F) << 2;
1202 d2 |= (trim_regs[14] & 0x06) >> 1;
1203 if (trim_regs[14] & 0x01)
1209 d1 = (trim_regs[11] & 0x0F) << 3;
1210 d1 |= (trim_regs[9] & 0x0E) >> 1;
1211 if (trim_regs[9] & 0x01)
1215 d2 = (trim_regs[15] & 0x0F) << 3;
1216 d2 |= (trim_regs[13] & 0x0E) >> 1;
1217 if (trim_regs[13] & 0x01)
1223 temp = (trim_regs[3] & 0x1F) << 2;
1224 temp |= (trim_regs[1] & 0x06) >> 1;
1225 if (trim_regs[1] & 0x01)
1228 d1 = (trim_regs[5] & 0x7E) >> 1;
1229 if (trim_regs[5] & 0x01)
1235 temp = (trim_regs[4] & 0x3F) << 2;
1236 temp |= (trim_regs[2] & 0x06) >> 1;
1237 if (trim_regs[2] & 0x01)
1240 d2 = (trim_regs[6] & 0xFF) >> 1;
1241 if (trim_regs[6] & 0x01)
1247 /* No data for other channels */
1251 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", chn, d1);
1252 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", chn, d2);
1255 gain = ((twl6032_ideal[chn].v2 -
1256 twl6032_ideal[chn].v1) * 1000) /
1257 ((twl6032_ideal[chn].code2 -
1258 twl6032_ideal[chn].code1));
1260 x1 = twl6032_ideal[chn].code1;
1261 x2 = twl6032_ideal[chn].code2;
1264 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
1267 b = (d1 * 1000) - (k - 1000) * x1;
1269 gpadc->twl6032_cal_tbl[chn].gain = gain;
1270 gpadc->twl6032_cal_tbl[chn].gain_error = k;
1271 gpadc->twl6032_cal_tbl[chn].offset_error = b;
1273 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", chn, x1);
1274 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", chn, x2);
1275 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", chn, gain);
1276 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", chn, k);
1277 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", chn, b);
1284 static int __devinit twl6030_gpadc_probe(struct platform_device *pdev)
1286 struct twl6030_gpadc_data *gpadc;
1287 struct twl4030_madc_platform_data *pdata = pdev->dev.platform_data;
1292 gpadc = kzalloc(sizeof *gpadc, GFP_KERNEL);
1297 dev_dbg(&pdev->dev, "platform_data not available\n");
1302 if (pdata->features & TWL6032_SUBCLASS) {
1303 gpadc->twl6032_cal_tbl = kzalloc(
1304 sizeof(struct twl6032_chnl_calib) *
1305 TWL6032_GPADC_MAX_CHANNELS,
1307 if (!gpadc->twl6032_cal_tbl) {
1313 gpadc->dev = &pdev->dev;
1315 gpadc->features = pdata->features;
1317 twl6030_conversion_methods = twl6030_conversion_methods_table;
1319 if (gpadc->features & TWL6032_SUBCLASS)
1320 twl6030_conversion_methods = twl6032_conversion_methods_table;
1322 ret = misc_register(&twl6030_gpadc_device);
1324 dev_dbg(&pdev->dev, "could not register misc_device\n");
1328 irq_rt = platform_get_irq(pdev, 0);
1330 dev_err(&pdev->dev, "failed to get irq\n");
1334 ret = request_threaded_irq(irq_rt, NULL, twl6030_gpadc_irq_handler,
1335 0, "twl6030_gpadc", &gpadc->requests[TWL6030_GPADC_RT]);
1337 dev_dbg(&pdev->dev, "could not request irq\n");
1341 irq = platform_get_irq(pdev, 1);
1343 dev_err(&pdev->dev, "failed to get irq\n");
1347 ret = request_threaded_irq(irq, NULL, twl6030_gpadc_irq_handler,
1348 0, "twl6030_gpadc", &gpadc->requests[TWL6030_GPADC_SW2]);
1350 dev_dbg(&pdev->dev, "could not request irq\n");
1354 platform_set_drvdata(pdev, gpadc);
1355 mutex_init(&gpadc->lock);
1356 INIT_WORK(&gpadc->ws, twl6030_gpadc_work);
1358 if (gpadc->features & TWL6032_SUBCLASS)
1359 ret = twl6032_calibration(gpadc);
1361 ret = twl6030_calibration();
1363 dev_err(&pdev->dev, "Failed to read calibration registers\n");
1368 ret = sysfs_create_group(&pdev->dev.kobj, &twl6030_gpadc_group);
1370 dev_err(&pdev->dev, "could not create sysfs files\n");
1375 free_irq(irq, &gpadc->requests[TWL6030_GPADC_SW2]);
1377 free_irq(irq_rt, &gpadc->requests[TWL6030_GPADC_RT]);
1379 misc_deregister(&twl6030_gpadc_device);
1382 if (pdata->features & TWL6032_SUBCLASS)
1383 kfree(gpadc->twl6032_cal_tbl);
1390 static int __devexit twl6030_gpadc_remove(struct platform_device *pdev)
1392 struct twl6030_gpadc_data *gpadc = platform_get_drvdata(pdev);
1394 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT);
1395 twl6030_gpadc_disable_irq(TWL6030_GPADC_SW2);
1396 free_irq(platform_get_irq(pdev, 0), gpadc);
1397 sysfs_remove_group(&pdev->dev.kobj, &twl6030_gpadc_group);
1398 cancel_work_sync(&gpadc->ws);
1399 misc_deregister(&twl6030_gpadc_device);
1404 static int twl6030_gpadc_suspend(struct device *pdev)
1408 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCR, REG_TOGGLE1);
1410 pr_err("%s: Error reseting GPADC (%d)!\n", __func__, ret);
1415 static int twl6030_gpadc_resume(struct device *pdev)
1419 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCS, REG_TOGGLE1);
1421 pr_err("%s: Error setting GPADC (%d)!\n", __func__, ret);
1425 static const struct dev_pm_ops twl6030_gpadc_pm_ops = {
1426 .suspend = twl6030_gpadc_suspend,
1427 .resume = twl6030_gpadc_resume,
1430 static struct platform_driver twl6030_gpadc_driver = {
1431 .probe = twl6030_gpadc_probe,
1432 .remove = __devexit_p(twl6030_gpadc_remove),
1434 .name = "twl6030_gpadc",
1435 .owner = THIS_MODULE,
1436 .pm = &twl6030_gpadc_pm_ops,
1440 static int __init twl6030_gpadc_init(void)
1442 return platform_driver_register(&twl6030_gpadc_driver);
1444 module_init(twl6030_gpadc_init);
1446 static void __exit twl6030_gpadc_exit(void)
1448 platform_driver_unregister(&twl6030_gpadc_driver);
1450 module_exit(twl6030_gpadc_exit);
1452 MODULE_ALIAS("platform:twl6030-gpadc");
1453 MODULE_AUTHOR("Texas Instruments Inc.");
1454 MODULE_DESCRIPTION("twl6030 ADC driver");
1455 MODULE_LICENSE("GPL");