xref: /openbmc/linux/drivers/hwmon/adcxx.c (revision b6dcefde)
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>
46 #include <linux/mod_devicetable.h>
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);
65 	u8 tx_buf[2] = { attr->index << 3 }; /* other bits are don't care */
66 	u8 rx_buf[2];
67 	int status;
68 	int value;
69 
70 	if (mutex_lock_interruptible(&adc->lock))
71 		return -ERESTARTSYS;
72 
73 	status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf),
74 					rx_buf, sizeof(rx_buf));
75 	if (status < 0) {
76 		dev_warn(dev, "spi_write_then_read failed with status %d\n",
77 				status);
78 		goto out;
79 	}
80 
81 	value = (rx_buf[0] << 8) + rx_buf[1];
82 	dev_dbg(dev, "raw value = 0x%x\n", value);
83 
84 	value = value * adc->reference >> 12;
85 	status = sprintf(buf, "%d\n", value);
86 out:
87 	mutex_unlock(&adc->lock);
88 	return status;
89 }
90 
91 static ssize_t adcxx_show_min(struct device *dev,
92 		struct device_attribute *devattr, char *buf)
93 {
94 	/* The minimum reference is 0 for this chip family */
95 	return sprintf(buf, "0\n");
96 }
97 
98 static ssize_t adcxx_show_max(struct device *dev,
99 		struct device_attribute *devattr, char *buf)
100 {
101 	struct spi_device *spi = to_spi_device(dev);
102 	struct adcxx *adc = dev_get_drvdata(&spi->dev);
103 	u32 reference;
104 
105 	if (mutex_lock_interruptible(&adc->lock))
106 		return -ERESTARTSYS;
107 
108 	reference = adc->reference;
109 
110 	mutex_unlock(&adc->lock);
111 
112 	return sprintf(buf, "%d\n", reference);
113 }
114 
115 static ssize_t adcxx_set_max(struct device *dev,
116 	struct device_attribute *devattr, const char *buf, size_t count)
117 {
118 	struct spi_device *spi = to_spi_device(dev);
119 	struct adcxx *adc = dev_get_drvdata(&spi->dev);
120 	unsigned long value;
121 
122 	if (strict_strtoul(buf, 10, &value))
123 		return -EINVAL;
124 
125 	if (mutex_lock_interruptible(&adc->lock))
126 		return -ERESTARTSYS;
127 
128 	adc->reference = value;
129 
130 	mutex_unlock(&adc->lock);
131 
132 	return count;
133 }
134 
135 static ssize_t adcxx_show_name(struct device *dev, struct device_attribute
136 			      *devattr, char *buf)
137 {
138 	struct spi_device *spi = to_spi_device(dev);
139 	struct adcxx *adc = dev_get_drvdata(&spi->dev);
140 
141 	return sprintf(buf, "adcxx%ds\n", adc->channels);
142 }
143 
144 static struct sensor_device_attribute ad_input[] = {
145 	SENSOR_ATTR(name, S_IRUGO, adcxx_show_name, NULL, 0),
146 	SENSOR_ATTR(in_min, S_IRUGO, adcxx_show_min, NULL, 0),
147 	SENSOR_ATTR(in_max, S_IWUSR | S_IRUGO, adcxx_show_max,
148 					adcxx_set_max, 0),
149 	SENSOR_ATTR(in0_input, S_IRUGO, adcxx_read, NULL, 0),
150 	SENSOR_ATTR(in1_input, S_IRUGO, adcxx_read, NULL, 1),
151 	SENSOR_ATTR(in2_input, S_IRUGO, adcxx_read, NULL, 2),
152 	SENSOR_ATTR(in3_input, S_IRUGO, adcxx_read, NULL, 3),
153 	SENSOR_ATTR(in4_input, S_IRUGO, adcxx_read, NULL, 4),
154 	SENSOR_ATTR(in5_input, S_IRUGO, adcxx_read, NULL, 5),
155 	SENSOR_ATTR(in6_input, S_IRUGO, adcxx_read, NULL, 6),
156 	SENSOR_ATTR(in7_input, S_IRUGO, adcxx_read, NULL, 7),
157 };
158 
159 /*----------------------------------------------------------------------*/
160 
161 static int __devinit adcxx_probe(struct spi_device *spi)
162 {
163 	int channels = spi_get_device_id(spi)->driver_data;
164 	struct adcxx *adc;
165 	int status;
166 	int i;
167 
168 	adc = kzalloc(sizeof *adc, GFP_KERNEL);
169 	if (!adc)
170 		return -ENOMEM;
171 
172 	/* set a default value for the reference */
173 	adc->reference = 3300;
174 	adc->channels = channels;
175 	mutex_init(&adc->lock);
176 
177 	mutex_lock(&adc->lock);
178 
179 	dev_set_drvdata(&spi->dev, adc);
180 
181 	for (i = 0; i < 3 + adc->channels; i++) {
182 		status = device_create_file(&spi->dev, &ad_input[i].dev_attr);
183 		if (status) {
184 			dev_err(&spi->dev, "device_create_file failed.\n");
185 			goto out_err;
186 		}
187 	}
188 
189 	adc->hwmon_dev = hwmon_device_register(&spi->dev);
190 	if (IS_ERR(adc->hwmon_dev)) {
191 		dev_err(&spi->dev, "hwmon_device_register failed.\n");
192 		status = PTR_ERR(adc->hwmon_dev);
193 		goto out_err;
194 	}
195 
196 	mutex_unlock(&adc->lock);
197 	return 0;
198 
199 out_err:
200 	for (i--; i >= 0; i--)
201 		device_remove_file(&spi->dev, &ad_input[i].dev_attr);
202 
203 	dev_set_drvdata(&spi->dev, NULL);
204 	mutex_unlock(&adc->lock);
205 	kfree(adc);
206 	return status;
207 }
208 
209 static int __devexit adcxx_remove(struct spi_device *spi)
210 {
211 	struct adcxx *adc = dev_get_drvdata(&spi->dev);
212 	int i;
213 
214 	mutex_lock(&adc->lock);
215 	hwmon_device_unregister(adc->hwmon_dev);
216 	for (i = 0; i < 3 + adc->channels; i++)
217 		device_remove_file(&spi->dev, &ad_input[i].dev_attr);
218 
219 	dev_set_drvdata(&spi->dev, NULL);
220 	mutex_unlock(&adc->lock);
221 	kfree(adc);
222 
223 	return 0;
224 }
225 
226 static const struct spi_device_id adcxx_ids[] = {
227 	{ "adcxx1s", 1 },
228 	{ "adcxx2s", 2 },
229 	{ "adcxx4s", 4 },
230 	{ "adcxx8s", 8 },
231 	{ },
232 };
233 MODULE_DEVICE_TABLE(spi, adcxx_ids);
234 
235 static struct spi_driver adcxx_driver = {
236 	.driver = {
237 		.name	= "adcxx",
238 		.owner	= THIS_MODULE,
239 	},
240 	.id_table = adcxx_ids,
241 	.probe	= adcxx_probe,
242 	.remove	= __devexit_p(adcxx_remove),
243 };
244 
245 static int __init init_adcxx(void)
246 {
247 	return spi_register_driver(&adcxx_driver);
248 }
249 
250 static void __exit exit_adcxx(void)
251 {
252 	spi_unregister_driver(&adcxx_driver);
253 }
254 
255 module_init(init_adcxx);
256 module_exit(exit_adcxx);
257 
258 MODULE_AUTHOR("Marc Pignat");
259 MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver");
260 MODULE_LICENSE("GPL");
261