serial: PL011: move interrupt clearing
[pandora-kernel.git] / drivers / hwmon / ads7871.c
1 /*
2  *  ads7871 - driver for TI ADS7871 A/D converter
3  *
4  *  Copyright (c) 2010 Paul Thomas <pthomas8589@gmail.com>
5  *
6  *  This program is distributed in the hope that it will be useful,
7  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  *  GNU General Public License for more details.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 2 or
13  *  later as publishhed by the Free Software Foundation.
14  *
15  *      You need to have something like this in struct spi_board_info
16  *      {
17  *              .modalias       = "ads7871",
18  *              .max_speed_hz   = 2*1000*1000,
19  *              .chip_select    = 0,
20  *              .bus_num        = 1,
21  *      },
22  */
23
24 /*From figure 18 in the datasheet*/
25 /*Register addresses*/
26 #define REG_LS_BYTE     0 /*A/D Output Data, LS Byte*/
27 #define REG_MS_BYTE     1 /*A/D Output Data, MS Byte*/
28 #define REG_PGA_VALID   2 /*PGA Valid Register*/
29 #define REG_AD_CONTROL  3 /*A/D Control Register*/
30 #define REG_GAIN_MUX    4 /*Gain/Mux Register*/
31 #define REG_IO_STATE    5 /*Digital I/O State Register*/
32 #define REG_IO_CONTROL  6 /*Digital I/O Control Register*/
33 #define REG_OSC_CONTROL 7 /*Rev/Oscillator Control Register*/
34 #define REG_SER_CONTROL 24 /*Serial Interface Control Register*/
35 #define REG_ID          31 /*ID Register*/
36
37 /*
38  * From figure 17 in the datasheet
39  * These bits get ORed with the address to form
40  * the instruction byte
41  */
42 /*Instruction Bit masks*/
43 #define INST_MODE_bm    (1<<7)
44 #define INST_READ_bm    (1<<6)
45 #define INST_16BIT_bm   (1<<5)
46
47 /*From figure 18 in the datasheet*/
48 /*bit masks for Rev/Oscillator Control Register*/
49 #define MUX_CNV_bv      7
50 #define MUX_CNV_bm      (1<<MUX_CNV_bv)
51 #define MUX_M3_bm       (1<<3) /*M3 selects single ended*/
52 #define MUX_G_bv        4 /*allows for reg = (gain << MUX_G_bv) | ...*/
53
54 /*From figure 18 in the datasheet*/
55 /*bit masks for Rev/Oscillator Control Register*/
56 #define OSC_OSCR_bm     (1<<5)
57 #define OSC_OSCE_bm     (1<<4)
58 #define OSC_REFE_bm     (1<<3)
59 #define OSC_BUFE_bm     (1<<2)
60 #define OSC_R2V_bm      (1<<1)
61 #define OSC_RBG_bm      (1<<0)
62
63 #include <linux/module.h>
64 #include <linux/init.h>
65 #include <linux/spi/spi.h>
66 #include <linux/hwmon.h>
67 #include <linux/hwmon-sysfs.h>
68 #include <linux/err.h>
69 #include <linux/mutex.h>
70 #include <linux/delay.h>
71
72 #define DEVICE_NAME     "ads7871"
73
74 struct ads7871_data {
75         struct device   *hwmon_dev;
76         struct mutex    update_lock;
77 };
78
79 static int ads7871_read_reg8(struct spi_device *spi, int reg)
80 {
81         int ret;
82         reg = reg | INST_READ_bm;
83         ret = spi_w8r8(spi, reg);
84         return ret;
85 }
86
87 static int ads7871_read_reg16(struct spi_device *spi, int reg)
88 {
89         int ret;
90         reg = reg | INST_READ_bm | INST_16BIT_bm;
91         ret = spi_w8r16(spi, reg);
92         return ret;
93 }
94
95 static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
96 {
97         u8 tmp[2] = {reg, val};
98         return spi_write(spi, tmp, sizeof(tmp));
99 }
100
101 static ssize_t show_voltage(struct device *dev,
102                 struct device_attribute *da, char *buf)
103 {
104         struct spi_device *spi = to_spi_device(dev);
105         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
106         int ret, val, i = 0;
107         uint8_t channel, mux_cnv;
108
109         channel = attr->index;
110         /*
111          * TODO: add support for conversions
112          * other than single ended with a gain of 1
113          */
114         /*MUX_M3_bm forces single ended*/
115         /*This is also where the gain of the PGA would be set*/
116         ads7871_write_reg8(spi, REG_GAIN_MUX,
117                 (MUX_CNV_bm | MUX_M3_bm | channel));
118
119         ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
120         mux_cnv = ((ret & MUX_CNV_bm)>>MUX_CNV_bv);
121         /*
122          * on 400MHz arm9 platform the conversion
123          * is already done when we do this test
124          */
125         while ((i < 2) && mux_cnv) {
126                 i++;
127                 ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
128                 mux_cnv = ((ret & MUX_CNV_bm)>>MUX_CNV_bv);
129                 msleep_interruptible(1);
130         }
131
132         if (mux_cnv == 0) {
133                 val = ads7871_read_reg16(spi, REG_LS_BYTE);
134                 /*result in volts*10000 = (val/8192)*2.5*10000*/
135                 val = ((val>>2) * 25000) / 8192;
136                 return sprintf(buf, "%d\n", val);
137         } else {
138                 return -1;
139         }
140 }
141
142 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_voltage, NULL, 0);
143 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 1);
144 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 2);
145 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 3);
146 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 4);
147 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 5);
148 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 6);
149 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 7);
150
151 static struct attribute *ads7871_attributes[] = {
152         &sensor_dev_attr_in0_input.dev_attr.attr,
153         &sensor_dev_attr_in1_input.dev_attr.attr,
154         &sensor_dev_attr_in2_input.dev_attr.attr,
155         &sensor_dev_attr_in3_input.dev_attr.attr,
156         &sensor_dev_attr_in4_input.dev_attr.attr,
157         &sensor_dev_attr_in5_input.dev_attr.attr,
158         &sensor_dev_attr_in6_input.dev_attr.attr,
159         &sensor_dev_attr_in7_input.dev_attr.attr,
160         NULL
161 };
162
163 static const struct attribute_group ads7871_group = {
164         .attrs = ads7871_attributes,
165 };
166
167 static int __devinit ads7871_probe(struct spi_device *spi)
168 {
169         int ret, err;
170         uint8_t val;
171         struct ads7871_data *pdata;
172
173         dev_dbg(&spi->dev, "probe\n");
174
175         /* Configure the SPI bus */
176         spi->mode = (SPI_MODE_0);
177         spi->bits_per_word = 8;
178         spi_setup(spi);
179
180         ads7871_write_reg8(spi, REG_SER_CONTROL, 0);
181         ads7871_write_reg8(spi, REG_AD_CONTROL, 0);
182
183         val = (OSC_OSCR_bm | OSC_OSCE_bm | OSC_REFE_bm | OSC_BUFE_bm);
184         ads7871_write_reg8(spi, REG_OSC_CONTROL, val);
185         ret = ads7871_read_reg8(spi, REG_OSC_CONTROL);
186
187         dev_dbg(&spi->dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret);
188         /*
189          * because there is no other error checking on an SPI bus
190          * we need to make sure we really have a chip
191          */
192         if (val != ret) {
193                 err = -ENODEV;
194                 goto exit;
195         }
196
197         pdata = kzalloc(sizeof(struct ads7871_data), GFP_KERNEL);
198         if (!pdata) {
199                 err = -ENOMEM;
200                 goto exit;
201         }
202
203         err = sysfs_create_group(&spi->dev.kobj, &ads7871_group);
204         if (err < 0)
205                 goto error_free;
206
207         spi_set_drvdata(spi, pdata);
208
209         pdata->hwmon_dev = hwmon_device_register(&spi->dev);
210         if (IS_ERR(pdata->hwmon_dev)) {
211                 err = PTR_ERR(pdata->hwmon_dev);
212                 goto error_remove;
213         }
214
215         return 0;
216
217 error_remove:
218         sysfs_remove_group(&spi->dev.kobj, &ads7871_group);
219 error_free:
220         kfree(pdata);
221 exit:
222         return err;
223 }
224
225 static int __devexit ads7871_remove(struct spi_device *spi)
226 {
227         struct ads7871_data *pdata = spi_get_drvdata(spi);
228
229         hwmon_device_unregister(pdata->hwmon_dev);
230         sysfs_remove_group(&spi->dev.kobj, &ads7871_group);
231         kfree(pdata);
232         return 0;
233 }
234
235 static struct spi_driver ads7871_driver = {
236         .driver = {
237                 .name = DEVICE_NAME,
238                 .owner = THIS_MODULE,
239         },
240
241         .probe = ads7871_probe,
242         .remove = __devexit_p(ads7871_remove),
243 };
244
245 module_spi_driver(ads7871_driver);
246
247 MODULE_AUTHOR("Paul Thomas <pthomas8589@gmail.com>");
248 MODULE_DESCRIPTION("TI ADS7871 A/D driver");
249 MODULE_LICENSE("GPL");