]>
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 <[email protected]> | |
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> | |
46 | #include <linux/spi/spi.h> | |
47 | ||
48 | #define DRVNAME "adcxx" | |
49 | ||
50 | struct adcxx { | |
51 | struct device *hwmon_dev; | |
52 | struct mutex lock; | |
53 | u32 channels; | |
54 | u32 reference; /* in millivolts */ | |
55 | }; | |
56 | ||
57 | /* sysfs hook function */ | |
58 | static ssize_t adcxx_read(struct device *dev, | |
59 | struct device_attribute *devattr, char *buf) | |
60 | { | |
61 | struct spi_device *spi = to_spi_device(dev); | |
62 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
63 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
64 | u8 tx_buf[2] = { attr->index << 3 }; /* other bits are don't care */ | |
65 | u8 rx_buf[2]; | |
66 | int status; | |
67 | int value; | |
68 | ||
69 | if (mutex_lock_interruptible(&adc->lock)) | |
70 | return -ERESTARTSYS; | |
71 | ||
72 | status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf), | |
73 | rx_buf, sizeof(rx_buf)); | |
74 | if (status < 0) { | |
75 | dev_warn(dev, "spi_write_then_read failed with status %d\n", | |
76 | status); | |
77 | goto out; | |
78 | } | |
79 | ||
80 | value = (rx_buf[0] << 8) + rx_buf[1]; | |
81 | dev_dbg(dev, "raw value = 0x%x\n", value); | |
82 | ||
83 | value = value * adc->reference >> 12; | |
84 | status = sprintf(buf, "%d\n", value); | |
85 | out: | |
86 | mutex_unlock(&adc->lock); | |
87 | return status; | |
88 | } | |
89 | ||
90 | static ssize_t adcxx_show_min(struct device *dev, | |
91 | struct device_attribute *devattr, char *buf) | |
92 | { | |
93 | /* The minimum reference is 0 for this chip family */ | |
94 | return sprintf(buf, "0\n"); | |
95 | } | |
96 | ||
97 | static ssize_t adcxx_show_max(struct device *dev, | |
98 | struct device_attribute *devattr, char *buf) | |
99 | { | |
100 | struct spi_device *spi = to_spi_device(dev); | |
101 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
102 | u32 reference; | |
103 | ||
104 | if (mutex_lock_interruptible(&adc->lock)) | |
105 | return -ERESTARTSYS; | |
106 | ||
107 | reference = adc->reference; | |
108 | ||
109 | mutex_unlock(&adc->lock); | |
110 | ||
111 | return sprintf(buf, "%d\n", reference); | |
112 | } | |
113 | ||
114 | static ssize_t adcxx_set_max(struct device *dev, | |
115 | struct device_attribute *devattr, const char *buf, size_t count) | |
116 | { | |
117 | struct spi_device *spi = to_spi_device(dev); | |
118 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
119 | unsigned long value; | |
120 | ||
121 | if (strict_strtoul(buf, 10, &value)) | |
122 | return -EINVAL; | |
123 | ||
124 | if (mutex_lock_interruptible(&adc->lock)) | |
125 | return -ERESTARTSYS; | |
126 | ||
127 | adc->reference = value; | |
128 | ||
129 | mutex_unlock(&adc->lock); | |
130 | ||
131 | return count; | |
132 | } | |
133 | ||
134 | static ssize_t adcxx_show_name(struct device *dev, struct device_attribute | |
135 | *devattr, char *buf) | |
136 | { | |
137 | struct spi_device *spi = to_spi_device(dev); | |
138 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
139 | ||
140 | return sprintf(buf, "adcxx%ds\n", adc->channels); | |
141 | } | |
142 | ||
143 | static struct sensor_device_attribute ad_input[] = { | |
144 | SENSOR_ATTR(name, S_IRUGO, adcxx_show_name, NULL, 0), | |
145 | SENSOR_ATTR(in_min, S_IRUGO, adcxx_show_min, NULL, 0), | |
146 | SENSOR_ATTR(in_max, S_IWUSR | S_IRUGO, adcxx_show_max, | |
147 | adcxx_set_max, 0), | |
148 | SENSOR_ATTR(in0_input, S_IRUGO, adcxx_read, NULL, 0), | |
149 | SENSOR_ATTR(in1_input, S_IRUGO, adcxx_read, NULL, 1), | |
150 | SENSOR_ATTR(in2_input, S_IRUGO, adcxx_read, NULL, 2), | |
151 | SENSOR_ATTR(in3_input, S_IRUGO, adcxx_read, NULL, 3), | |
152 | SENSOR_ATTR(in4_input, S_IRUGO, adcxx_read, NULL, 4), | |
153 | SENSOR_ATTR(in5_input, S_IRUGO, adcxx_read, NULL, 5), | |
154 | SENSOR_ATTR(in6_input, S_IRUGO, adcxx_read, NULL, 6), | |
155 | SENSOR_ATTR(in7_input, S_IRUGO, adcxx_read, NULL, 7), | |
156 | }; | |
157 | ||
158 | /*----------------------------------------------------------------------*/ | |
159 | ||
160 | static int __devinit adcxx_probe(struct spi_device *spi, int channels) | |
161 | { | |
162 | struct adcxx *adc; | |
163 | int status; | |
164 | int i; | |
165 | ||
166 | adc = kzalloc(sizeof *adc, GFP_KERNEL); | |
167 | if (!adc) | |
168 | return -ENOMEM; | |
169 | ||
170 | /* set a default value for the reference */ | |
171 | adc->reference = 3300; | |
172 | adc->channels = channels; | |
173 | mutex_init(&adc->lock); | |
174 | ||
175 | mutex_lock(&adc->lock); | |
176 | ||
177 | dev_set_drvdata(&spi->dev, adc); | |
178 | ||
179 | for (i = 0; i < 3 + adc->channels; i++) { | |
180 | status = device_create_file(&spi->dev, &ad_input[i].dev_attr); | |
181 | if (status) { | |
182 | dev_err(&spi->dev, "device_create_file failed.\n"); | |
183 | goto out_err; | |
184 | } | |
185 | } | |
186 | ||
187 | adc->hwmon_dev = hwmon_device_register(&spi->dev); | |
188 | if (IS_ERR(adc->hwmon_dev)) { | |
189 | dev_err(&spi->dev, "hwmon_device_register failed.\n"); | |
190 | status = PTR_ERR(adc->hwmon_dev); | |
191 | goto out_err; | |
192 | } | |
193 | ||
194 | mutex_unlock(&adc->lock); | |
195 | return 0; | |
196 | ||
197 | out_err: | |
198 | for (i--; i >= 0; i--) | |
199 | device_remove_file(&spi->dev, &ad_input[i].dev_attr); | |
200 | ||
201 | dev_set_drvdata(&spi->dev, NULL); | |
202 | mutex_unlock(&adc->lock); | |
203 | kfree(adc); | |
204 | return status; | |
205 | } | |
206 | ||
207 | static int __devinit adcxx1s_probe(struct spi_device *spi) | |
208 | { | |
209 | return adcxx_probe(spi, 1); | |
210 | } | |
211 | ||
212 | static int __devinit adcxx2s_probe(struct spi_device *spi) | |
213 | { | |
214 | return adcxx_probe(spi, 2); | |
215 | } | |
216 | ||
217 | static int __devinit adcxx4s_probe(struct spi_device *spi) | |
218 | { | |
219 | return adcxx_probe(spi, 4); | |
220 | } | |
221 | ||
222 | static int __devinit adcxx8s_probe(struct spi_device *spi) | |
223 | { | |
224 | return adcxx_probe(spi, 8); | |
225 | } | |
226 | ||
227 | static int __devexit adcxx_remove(struct spi_device *spi) | |
228 | { | |
229 | struct adcxx *adc = dev_get_drvdata(&spi->dev); | |
230 | int i; | |
231 | ||
232 | mutex_lock(&adc->lock); | |
233 | hwmon_device_unregister(adc->hwmon_dev); | |
234 | for (i = 0; i < 3 + adc->channels; i++) | |
235 | device_remove_file(&spi->dev, &ad_input[i].dev_attr); | |
236 | ||
237 | dev_set_drvdata(&spi->dev, NULL); | |
238 | mutex_unlock(&adc->lock); | |
239 | kfree(adc); | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
244 | static struct spi_driver adcxx1s_driver = { | |
245 | .driver = { | |
246 | .name = "adcxx1s", | |
247 | .owner = THIS_MODULE, | |
248 | }, | |
249 | .probe = adcxx1s_probe, | |
250 | .remove = __devexit_p(adcxx_remove), | |
251 | }; | |
252 | ||
253 | static struct spi_driver adcxx2s_driver = { | |
254 | .driver = { | |
255 | .name = "adcxx2s", | |
256 | .owner = THIS_MODULE, | |
257 | }, | |
258 | .probe = adcxx2s_probe, | |
259 | .remove = __devexit_p(adcxx_remove), | |
260 | }; | |
261 | ||
262 | static struct spi_driver adcxx4s_driver = { | |
263 | .driver = { | |
264 | .name = "adcxx4s", | |
265 | .owner = THIS_MODULE, | |
266 | }, | |
267 | .probe = adcxx4s_probe, | |
268 | .remove = __devexit_p(adcxx_remove), | |
269 | }; | |
270 | ||
271 | static struct spi_driver adcxx8s_driver = { | |
272 | .driver = { | |
273 | .name = "adcxx8s", | |
274 | .owner = THIS_MODULE, | |
275 | }, | |
276 | .probe = adcxx8s_probe, | |
277 | .remove = __devexit_p(adcxx_remove), | |
278 | }; | |
279 | ||
280 | static int __init init_adcxx(void) | |
281 | { | |
282 | int status; | |
283 | status = spi_register_driver(&adcxx1s_driver); | |
284 | if (status) | |
285 | goto reg_1_failed; | |
286 | ||
287 | status = spi_register_driver(&adcxx2s_driver); | |
288 | if (status) | |
289 | goto reg_2_failed; | |
290 | ||
291 | status = spi_register_driver(&adcxx4s_driver); | |
292 | if (status) | |
293 | goto reg_4_failed; | |
294 | ||
295 | status = spi_register_driver(&adcxx8s_driver); | |
296 | if (status) | |
297 | goto reg_8_failed; | |
298 | ||
299 | return status; | |
300 | ||
301 | reg_8_failed: | |
302 | spi_unregister_driver(&adcxx4s_driver); | |
303 | reg_4_failed: | |
304 | spi_unregister_driver(&adcxx2s_driver); | |
305 | reg_2_failed: | |
306 | spi_unregister_driver(&adcxx1s_driver); | |
307 | reg_1_failed: | |
308 | return status; | |
309 | } | |
310 | ||
311 | static void __exit exit_adcxx(void) | |
312 | { | |
313 | spi_unregister_driver(&adcxx1s_driver); | |
314 | spi_unregister_driver(&adcxx2s_driver); | |
315 | spi_unregister_driver(&adcxx4s_driver); | |
316 | spi_unregister_driver(&adcxx8s_driver); | |
317 | } | |
318 | ||
319 | module_init(init_adcxx); | |
320 | module_exit(exit_adcxx); | |
321 | ||
322 | MODULE_AUTHOR("Marc Pignat"); | |
323 | MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver"); | |
324 | MODULE_LICENSE("GPL"); | |
325 | ||
326 | MODULE_ALIAS("adcxx1s"); | |
327 | MODULE_ALIAS("adcxx2s"); | |
328 | MODULE_ALIAS("adcxx4s"); | |
329 | MODULE_ALIAS("adcxx8s"); |