4 * The adcxx4s is an AD converter family from National Semiconductor (NS).
6 * Copyright (c) 2008 Marc Pignat <marc.pignat@hevs.ch>
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
16 * Complete datasheets are available at National's website here:
17 * http://www.national.com/ds/DC/ADC<bb><c>S<sss>.pdf
19 * Handling of 8, 10 and 12 bits converters are the same, the
20 * unavailable bits are 0 :)
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.
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.
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.
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/slab.h>
41 #include <linux/device.h>
42 #include <linux/err.h>
43 #include <linux/sysfs.h>
44 #include <linux/hwmon.h>
45 #include <linux/hwmon-sysfs.h>
46 #include <linux/mutex.h>
47 #include <linux/mod_devicetable.h>
48 #include <linux/spi/spi.h>
50 #define DRVNAME "adcxx"
53 struct device *hwmon_dev;
56 u32 reference; /* in millivolts */
59 /* sysfs hook function */
60 static ssize_t adcxx_read(struct device *dev,
61 struct device_attribute *devattr, char *buf)
63 struct spi_device *spi = to_spi_device(dev);
64 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
65 struct adcxx *adc = spi_get_drvdata(spi);
71 if (mutex_lock_interruptible(&adc->lock))
74 if (adc->channels == 1) {
75 status = spi_read(spi, rx_buf, sizeof(rx_buf));
77 tx_buf[0] = attr->index << 3; /* other bits are don't care */
78 status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf),
79 rx_buf, sizeof(rx_buf));
82 dev_warn(dev, "SPI synch. transfer failed with status %d\n",
87 value = (rx_buf[0] << 8) + rx_buf[1];
88 dev_dbg(dev, "raw value = 0x%x\n", value);
90 value = value * adc->reference >> 12;
91 status = sprintf(buf, "%d\n", value);
93 mutex_unlock(&adc->lock);
97 static ssize_t adcxx_show_min(struct device *dev,
98 struct device_attribute *devattr, char *buf)
100 /* The minimum reference is 0 for this chip family */
101 return sprintf(buf, "0\n");
104 static ssize_t adcxx_show_max(struct device *dev,
105 struct device_attribute *devattr, char *buf)
107 struct spi_device *spi = to_spi_device(dev);
108 struct adcxx *adc = spi_get_drvdata(spi);
111 if (mutex_lock_interruptible(&adc->lock))
114 reference = adc->reference;
116 mutex_unlock(&adc->lock);
118 return sprintf(buf, "%d\n", reference);
121 static ssize_t adcxx_set_max(struct device *dev,
122 struct device_attribute *devattr, const char *buf, size_t count)
124 struct spi_device *spi = to_spi_device(dev);
125 struct adcxx *adc = spi_get_drvdata(spi);
128 if (kstrtoul(buf, 10, &value))
131 if (mutex_lock_interruptible(&adc->lock))
134 adc->reference = value;
136 mutex_unlock(&adc->lock);
141 static ssize_t adcxx_show_name(struct device *dev, struct device_attribute
144 return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
147 static struct sensor_device_attribute ad_input[] = {
148 SENSOR_ATTR(name, S_IRUGO, adcxx_show_name, NULL, 0),
149 SENSOR_ATTR(in_min, S_IRUGO, adcxx_show_min, NULL, 0),
150 SENSOR_ATTR(in_max, S_IWUSR | S_IRUGO, adcxx_show_max,
152 SENSOR_ATTR(in0_input, S_IRUGO, adcxx_read, NULL, 0),
153 SENSOR_ATTR(in1_input, S_IRUGO, adcxx_read, NULL, 1),
154 SENSOR_ATTR(in2_input, S_IRUGO, adcxx_read, NULL, 2),
155 SENSOR_ATTR(in3_input, S_IRUGO, adcxx_read, NULL, 3),
156 SENSOR_ATTR(in4_input, S_IRUGO, adcxx_read, NULL, 4),
157 SENSOR_ATTR(in5_input, S_IRUGO, adcxx_read, NULL, 5),
158 SENSOR_ATTR(in6_input, S_IRUGO, adcxx_read, NULL, 6),
159 SENSOR_ATTR(in7_input, S_IRUGO, adcxx_read, NULL, 7),
162 /*----------------------------------------------------------------------*/
164 static int __devinit adcxx_probe(struct spi_device *spi)
166 int channels = spi_get_device_id(spi)->driver_data;
171 adc = devm_kzalloc(&spi->dev, sizeof(*adc), GFP_KERNEL);
175 /* set a default value for the reference */
176 adc->reference = 3300;
177 adc->channels = channels;
178 mutex_init(&adc->lock);
180 mutex_lock(&adc->lock);
182 spi_set_drvdata(spi, adc);
184 for (i = 0; i < 3 + adc->channels; i++) {
185 status = device_create_file(&spi->dev, &ad_input[i].dev_attr);
187 dev_err(&spi->dev, "device_create_file failed.\n");
192 adc->hwmon_dev = hwmon_device_register(&spi->dev);
193 if (IS_ERR(adc->hwmon_dev)) {
194 dev_err(&spi->dev, "hwmon_device_register failed.\n");
195 status = PTR_ERR(adc->hwmon_dev);
199 mutex_unlock(&adc->lock);
203 for (i--; i >= 0; i--)
204 device_remove_file(&spi->dev, &ad_input[i].dev_attr);
206 spi_set_drvdata(spi, NULL);
207 mutex_unlock(&adc->lock);
211 static int __devexit adcxx_remove(struct spi_device *spi)
213 struct adcxx *adc = spi_get_drvdata(spi);
216 mutex_lock(&adc->lock);
217 hwmon_device_unregister(adc->hwmon_dev);
218 for (i = 0; i < 3 + adc->channels; i++)
219 device_remove_file(&spi->dev, &ad_input[i].dev_attr);
221 spi_set_drvdata(spi, NULL);
222 mutex_unlock(&adc->lock);
227 static const struct spi_device_id adcxx_ids[] = {
234 MODULE_DEVICE_TABLE(spi, adcxx_ids);
236 static struct spi_driver adcxx_driver = {
239 .owner = THIS_MODULE,
241 .id_table = adcxx_ids,
242 .probe = adcxx_probe,
243 .remove = __devexit_p(adcxx_remove),
246 module_spi_driver(adcxx_driver);
248 MODULE_AUTHOR("Marc Pignat");
249 MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver");
250 MODULE_LICENSE("GPL");