Commit | Line | Data |
---|---|---|
d42139a3 MP |
1 | /* |
2 | * adcxx.c | |
3 | * | |
4 | * The adcxx4s is an AD converter family from National Semiconductor (NS). | |
5 | * | |
6 | * Copyright (c) 2008 Marc Pignat <marc.pignat@hevs.ch> | |
7 | * | |
8 | * The adcxx4s communicates with a host processor via an SPI/Microwire Bus | |
9 | * interface. This driver supports the whole family of devices with name | |
10 | * ADC<bb><c>S<sss>, where | |
11 | * * bb is the resolution in number of bits (8, 10, 12) | |
12 | * * c is the number of channels (1, 2, 4, 8) | |
13 | * * sss is the maximum conversion speed (021 for 200 kSPS, 051 for 500 kSPS | |
14 | * and 101 for 1 MSPS) | |
15 | * | |
16 | * Complete datasheets are available at National's website here: | |
17 | * http://www.national.com/ds/DC/ADC<bb><c>S<sss>.pdf | |
18 | * | |
19 | * Handling of 8, 10 and 12 bits converters are the same, the | |
20 | * unavailable bits are 0 :) | |
21 | * | |
22 | * This program is free software; you can redistribute it and/or modify | |
23 | * it under the terms of the GNU General Public License as published by | |
24 | * the Free Software Foundation; either version 2 of the License, or | |
25 | * (at your option) any later version. | |
26 | * | |
27 | * This program is distributed in the hope that it will be useful, | |
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
30 | * GNU General Public License for more details. | |
31 | * | |
32 | * You should have received a copy of the GNU General Public License | |
33 | * along with this program; if not, write to the Free Software | |
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
35 | */ | |
36 | ||
37 | #include <linux/init.h> | |
38 | #include <linux/module.h> | |
39 | #include <linux/kernel.h> | |
40 | #include <linux/device.h> | |
41 | #include <linux/err.h> | |
42 | #include <linux/sysfs.h> | |
43 | #include <linux/hwmon.h> | |
44 | #include <linux/hwmon-sysfs.h> | |
45 | #include <linux/mutex.h> | |
d2a5c10f | 46 | #include <linux/mod_devicetable.h> |
d42139a3 MP |
47 | #include <linux/spi/spi.h> |
48 | ||
49 | #define DRVNAME "adcxx" | |
50 | ||
51 | struct adcxx { | |
52 | struct device *hwmon_dev; | |
53 | struct mutex lock; | |
54 | u32 channels; | |
55 | u32 reference; /* in millivolts */ | |
56 | }; | |
57 | ||
58 | /* sysfs hook function */ | |
59 | static ssize_t adcxx_read(struct device *dev, | |
60 | struct device_attribute *devattr, char *buf) | |
61 | { | |
62 | struct spi_device *spi = to_spi_device(dev); | |
63 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
64 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
5748150e | 65 | u8 tx_buf[2]; |
d42139a3 MP |
66 | u8 rx_buf[2]; |
67 | int status; | |
5748150e | 68 | u32 value; |
d42139a3 MP |
69 | |
70 | if (mutex_lock_interruptible(&adc->lock)) | |
71 | return -ERESTARTSYS; | |
72 | ||
5748150e JMG |
73 | if (adc->channels == 1) { |
74 | status = spi_read(spi, rx_buf, sizeof(rx_buf)); | |
75 | } else { | |
76 | tx_buf[0] = attr->index << 3; /* other bits are don't care */ | |
77 | status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf), | |
78 | rx_buf, sizeof(rx_buf)); | |
79 | } | |
d42139a3 | 80 | if (status < 0) { |
5748150e | 81 | dev_warn(dev, "SPI synch. transfer failed with status %d\n", |
d42139a3 MP |
82 | status); |
83 | goto out; | |
84 | } | |
85 | ||
86 | value = (rx_buf[0] << 8) + rx_buf[1]; | |
87 | dev_dbg(dev, "raw value = 0x%x\n", value); | |
88 | ||
89 | value = value * adc->reference >> 12; | |
90 | status = sprintf(buf, "%d\n", value); | |
91 | out: | |
92 | mutex_unlock(&adc->lock); | |
93 | return status; | |
94 | } | |
95 | ||
96 | static ssize_t adcxx_show_min(struct device *dev, | |
97 | struct device_attribute *devattr, char *buf) | |
98 | { | |
99 | /* The minimum reference is 0 for this chip family */ | |
100 | return sprintf(buf, "0\n"); | |
101 | } | |
102 | ||
103 | static ssize_t adcxx_show_max(struct device *dev, | |
104 | struct device_attribute *devattr, char *buf) | |
105 | { | |
106 | struct spi_device *spi = to_spi_device(dev); | |
107 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
108 | u32 reference; | |
109 | ||
110 | if (mutex_lock_interruptible(&adc->lock)) | |
111 | return -ERESTARTSYS; | |
112 | ||
113 | reference = adc->reference; | |
114 | ||
115 | mutex_unlock(&adc->lock); | |
116 | ||
117 | return sprintf(buf, "%d\n", reference); | |
118 | } | |
119 | ||
120 | static ssize_t adcxx_set_max(struct device *dev, | |
121 | struct device_attribute *devattr, const char *buf, size_t count) | |
122 | { | |
123 | struct spi_device *spi = to_spi_device(dev); | |
124 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
125 | unsigned long value; | |
126 | ||
127 | if (strict_strtoul(buf, 10, &value)) | |
128 | return -EINVAL; | |
129 | ||
130 | if (mutex_lock_interruptible(&adc->lock)) | |
131 | return -ERESTARTSYS; | |
132 | ||
133 | adc->reference = value; | |
134 | ||
135 | mutex_unlock(&adc->lock); | |
136 | ||
137 | return count; | |
138 | } | |
139 | ||
140 | static ssize_t adcxx_show_name(struct device *dev, struct device_attribute | |
141 | *devattr, char *buf) | |
142 | { | |
143 | struct spi_device *spi = to_spi_device(dev); | |
144 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
145 | ||
146 | return sprintf(buf, "adcxx%ds\n", adc->channels); | |
147 | } | |
148 | ||
149 | static struct sensor_device_attribute ad_input[] = { | |
150 | SENSOR_ATTR(name, S_IRUGO, adcxx_show_name, NULL, 0), | |
151 | SENSOR_ATTR(in_min, S_IRUGO, adcxx_show_min, NULL, 0), | |
152 | SENSOR_ATTR(in_max, S_IWUSR | S_IRUGO, adcxx_show_max, | |
153 | adcxx_set_max, 0), | |
154 | SENSOR_ATTR(in0_input, S_IRUGO, adcxx_read, NULL, 0), | |
155 | SENSOR_ATTR(in1_input, S_IRUGO, adcxx_read, NULL, 1), | |
156 | SENSOR_ATTR(in2_input, S_IRUGO, adcxx_read, NULL, 2), | |
157 | SENSOR_ATTR(in3_input, S_IRUGO, adcxx_read, NULL, 3), | |
158 | SENSOR_ATTR(in4_input, S_IRUGO, adcxx_read, NULL, 4), | |
159 | SENSOR_ATTR(in5_input, S_IRUGO, adcxx_read, NULL, 5), | |
160 | SENSOR_ATTR(in6_input, S_IRUGO, adcxx_read, NULL, 6), | |
161 | SENSOR_ATTR(in7_input, S_IRUGO, adcxx_read, NULL, 7), | |
162 | }; | |
163 | ||
164 | /*----------------------------------------------------------------------*/ | |
165 | ||
d2a5c10f | 166 | static int __devinit adcxx_probe(struct spi_device *spi) |
d42139a3 | 167 | { |
d2a5c10f | 168 | int channels = spi_get_device_id(spi)->driver_data; |
d42139a3 MP |
169 | struct adcxx *adc; |
170 | int status; | |
171 | int i; | |
172 | ||
173 | adc = kzalloc(sizeof *adc, GFP_KERNEL); | |
174 | if (!adc) | |
175 | return -ENOMEM; | |
176 | ||
177 | /* set a default value for the reference */ | |
178 | adc->reference = 3300; | |
179 | adc->channels = channels; | |
180 | mutex_init(&adc->lock); | |
181 | ||
182 | mutex_lock(&adc->lock); | |
183 | ||
184 | dev_set_drvdata(&spi->dev, adc); | |
185 | ||
186 | for (i = 0; i < 3 + adc->channels; i++) { | |
187 | status = device_create_file(&spi->dev, &ad_input[i].dev_attr); | |
188 | if (status) { | |
189 | dev_err(&spi->dev, "device_create_file failed.\n"); | |
190 | goto out_err; | |
191 | } | |
192 | } | |
193 | ||
194 | adc->hwmon_dev = hwmon_device_register(&spi->dev); | |
195 | if (IS_ERR(adc->hwmon_dev)) { | |
196 | dev_err(&spi->dev, "hwmon_device_register failed.\n"); | |
197 | status = PTR_ERR(adc->hwmon_dev); | |
198 | goto out_err; | |
199 | } | |
200 | ||
201 | mutex_unlock(&adc->lock); | |
202 | return 0; | |
203 | ||
204 | out_err: | |
205 | for (i--; i >= 0; i--) | |
206 | device_remove_file(&spi->dev, &ad_input[i].dev_attr); | |
207 | ||
208 | dev_set_drvdata(&spi->dev, NULL); | |
209 | mutex_unlock(&adc->lock); | |
210 | kfree(adc); | |
211 | return status; | |
212 | } | |
213 | ||
d42139a3 MP |
214 | static int __devexit adcxx_remove(struct spi_device *spi) |
215 | { | |
216 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
217 | int i; | |
218 | ||
219 | mutex_lock(&adc->lock); | |
220 | hwmon_device_unregister(adc->hwmon_dev); | |
221 | for (i = 0; i < 3 + adc->channels; i++) | |
222 | device_remove_file(&spi->dev, &ad_input[i].dev_attr); | |
223 | ||
224 | dev_set_drvdata(&spi->dev, NULL); | |
225 | mutex_unlock(&adc->lock); | |
226 | kfree(adc); | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
d2a5c10f AV |
231 | static const struct spi_device_id adcxx_ids[] = { |
232 | { "adcxx1s", 1 }, | |
233 | { "adcxx2s", 2 }, | |
234 | { "adcxx4s", 4 }, | |
235 | { "adcxx8s", 8 }, | |
236 | { }, | |
d42139a3 | 237 | }; |
d2a5c10f | 238 | MODULE_DEVICE_TABLE(spi, adcxx_ids); |
d42139a3 | 239 | |
d2a5c10f | 240 | static struct spi_driver adcxx_driver = { |
d42139a3 | 241 | .driver = { |
d2a5c10f | 242 | .name = "adcxx", |
d42139a3 MP |
243 | .owner = THIS_MODULE, |
244 | }, | |
d2a5c10f AV |
245 | .id_table = adcxx_ids, |
246 | .probe = adcxx_probe, | |
d42139a3 MP |
247 | .remove = __devexit_p(adcxx_remove), |
248 | }; | |
249 | ||
250 | static int __init init_adcxx(void) | |
251 | { | |
d2a5c10f | 252 | return spi_register_driver(&adcxx_driver); |
d42139a3 MP |
253 | } |
254 | ||
255 | static void __exit exit_adcxx(void) | |
256 | { | |
d2a5c10f | 257 | spi_unregister_driver(&adcxx_driver); |
d42139a3 MP |
258 | } |
259 | ||
260 | module_init(init_adcxx); | |
261 | module_exit(exit_adcxx); | |
262 | ||
263 | MODULE_AUTHOR("Marc Pignat"); | |
264 | MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver"); | |
265 | MODULE_LICENSE("GPL"); |