1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * The adcxx4s is an AD converter family from National Semiconductor (NS).
7 * Copyright (c) 2008 Marc Pignat <marc.pignat@hevs.ch>
9 * The adcxx4s communicates with a host processor via an SPI/Microwire Bus
10 * interface. This driver supports the whole family of devices with name
11 * ADC<bb><c>S<sss>, where
12 * * bb is the resolution in number of bits (8, 10, 12)
13 * * c is the number of channels (1, 2, 4, 8)
14 * * sss is the maximum conversion speed (021 for 200 kSPS, 051 for 500 kSPS
17 * Complete datasheets are available at National's website here:
18 * http://www.national.com/ds/DC/ADC<bb><c>S<sss>.pdf
20 * Handling of 8, 10 and 12 bits converters are the same, the
21 * unavailable bits are 0 :)
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/device.h>
29 #include <linux/err.h>
30 #include <linux/sysfs.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/mutex.h>
34 #include <linux/mod_devicetable.h>
35 #include <linux/spi/spi.h>
37 #define DRVNAME "adcxx"
40 struct device
*hwmon_dev
;
43 u32 reference
; /* in millivolts */
46 /* sysfs hook function */
47 static ssize_t
adcxx_show(struct device
*dev
,
48 struct device_attribute
*devattr
, char *buf
)
50 struct spi_device
*spi
= to_spi_device(dev
);
51 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
52 struct adcxx
*adc
= spi_get_drvdata(spi
);
58 if (mutex_lock_interruptible(&adc
->lock
))
61 if (adc
->channels
== 1) {
62 status
= spi_read(spi
, rx_buf
, sizeof(rx_buf
));
64 tx_buf
[0] = attr
->index
<< 3; /* other bits are don't care */
65 status
= spi_write_then_read(spi
, tx_buf
, sizeof(tx_buf
),
66 rx_buf
, sizeof(rx_buf
));
69 dev_warn(dev
, "SPI synch. transfer failed with status %d\n",
74 value
= (rx_buf
[0] << 8) + rx_buf
[1];
75 dev_dbg(dev
, "raw value = 0x%x\n", value
);
77 value
= value
* adc
->reference
>> 12;
78 status
= sprintf(buf
, "%d\n", value
);
80 mutex_unlock(&adc
->lock
);
84 static ssize_t
adcxx_min_show(struct device
*dev
,
85 struct device_attribute
*devattr
, char *buf
)
87 /* The minimum reference is 0 for this chip family */
88 return sprintf(buf
, "0\n");
91 static ssize_t
adcxx_max_show(struct device
*dev
,
92 struct device_attribute
*devattr
, char *buf
)
94 struct spi_device
*spi
= to_spi_device(dev
);
95 struct adcxx
*adc
= spi_get_drvdata(spi
);
98 if (mutex_lock_interruptible(&adc
->lock
))
101 reference
= adc
->reference
;
103 mutex_unlock(&adc
->lock
);
105 return sprintf(buf
, "%d\n", reference
);
108 static ssize_t
adcxx_max_store(struct device
*dev
,
109 struct device_attribute
*devattr
,
110 const char *buf
, size_t count
)
112 struct spi_device
*spi
= to_spi_device(dev
);
113 struct adcxx
*adc
= spi_get_drvdata(spi
);
116 if (kstrtoul(buf
, 10, &value
))
119 if (mutex_lock_interruptible(&adc
->lock
))
122 adc
->reference
= value
;
124 mutex_unlock(&adc
->lock
);
129 static ssize_t
adcxx_name_show(struct device
*dev
,
130 struct device_attribute
*devattr
, char *buf
)
132 return sprintf(buf
, "%s\n", to_spi_device(dev
)->modalias
);
135 static struct sensor_device_attribute ad_input
[] = {
136 SENSOR_ATTR_RO(name
, adcxx_name
, 0),
137 SENSOR_ATTR_RO(in_min
, adcxx_min
, 0),
138 SENSOR_ATTR_RW(in_max
, adcxx_max
, 0),
139 SENSOR_ATTR_RO(in0_input
, adcxx
, 0),
140 SENSOR_ATTR_RO(in1_input
, adcxx
, 1),
141 SENSOR_ATTR_RO(in2_input
, adcxx
, 2),
142 SENSOR_ATTR_RO(in3_input
, adcxx
, 3),
143 SENSOR_ATTR_RO(in4_input
, adcxx
, 4),
144 SENSOR_ATTR_RO(in5_input
, adcxx
, 5),
145 SENSOR_ATTR_RO(in6_input
, adcxx
, 6),
146 SENSOR_ATTR_RO(in7_input
, adcxx
, 7),
149 /*----------------------------------------------------------------------*/
151 static int adcxx_probe(struct spi_device
*spi
)
153 int channels
= spi_get_device_id(spi
)->driver_data
;
158 adc
= devm_kzalloc(&spi
->dev
, sizeof(*adc
), GFP_KERNEL
);
162 /* set a default value for the reference */
163 adc
->reference
= 3300;
164 adc
->channels
= channels
;
165 mutex_init(&adc
->lock
);
167 mutex_lock(&adc
->lock
);
169 spi_set_drvdata(spi
, adc
);
171 for (i
= 0; i
< 3 + adc
->channels
; i
++) {
172 status
= device_create_file(&spi
->dev
, &ad_input
[i
].dev_attr
);
174 dev_err(&spi
->dev
, "device_create_file failed.\n");
179 adc
->hwmon_dev
= hwmon_device_register(&spi
->dev
);
180 if (IS_ERR(adc
->hwmon_dev
)) {
181 dev_err(&spi
->dev
, "hwmon_device_register failed.\n");
182 status
= PTR_ERR(adc
->hwmon_dev
);
186 mutex_unlock(&adc
->lock
);
190 for (i
--; i
>= 0; i
--)
191 device_remove_file(&spi
->dev
, &ad_input
[i
].dev_attr
);
193 mutex_unlock(&adc
->lock
);
197 static int adcxx_remove(struct spi_device
*spi
)
199 struct adcxx
*adc
= spi_get_drvdata(spi
);
202 mutex_lock(&adc
->lock
);
203 hwmon_device_unregister(adc
->hwmon_dev
);
204 for (i
= 0; i
< 3 + adc
->channels
; i
++)
205 device_remove_file(&spi
->dev
, &ad_input
[i
].dev_attr
);
207 mutex_unlock(&adc
->lock
);
212 static const struct spi_device_id adcxx_ids
[] = {
219 MODULE_DEVICE_TABLE(spi
, adcxx_ids
);
221 static struct spi_driver adcxx_driver
= {
225 .id_table
= adcxx_ids
,
226 .probe
= adcxx_probe
,
227 .remove
= adcxx_remove
,
230 module_spi_driver(adcxx_driver
);
232 MODULE_AUTHOR("Marc Pignat");
233 MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver");
234 MODULE_LICENSE("GPL");