| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 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> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 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> | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 47 | #include <linux/mod_devicetable.h> | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 48 | #include <linux/spi/spi.h> | 
 | 49 |  | 
 | 50 | #define DRVNAME		"adcxx" | 
 | 51 |  | 
 | 52 | struct adcxx { | 
 | 53 | 	struct device *hwmon_dev; | 
 | 54 | 	struct mutex lock; | 
 | 55 | 	u32 channels; | 
 | 56 | 	u32 reference; /* in millivolts */ | 
 | 57 | }; | 
 | 58 |  | 
 | 59 | /* sysfs hook function */ | 
 | 60 | static ssize_t adcxx_read(struct device *dev, | 
 | 61 | 		struct device_attribute *devattr, char *buf) | 
 | 62 | { | 
 | 63 | 	struct spi_device *spi = to_spi_device(dev); | 
 | 64 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 65 | 	struct adcxx *adc = spi_get_drvdata(spi); | 
| José Miguel Gonçalves | 5748150 | 2010-03-05 13:43:58 -0800 | [diff] [blame] | 66 | 	u8 tx_buf[2]; | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 67 | 	u8 rx_buf[2]; | 
 | 68 | 	int status; | 
| José Miguel Gonçalves | 5748150 | 2010-03-05 13:43:58 -0800 | [diff] [blame] | 69 | 	u32 value; | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 70 |  | 
 | 71 | 	if (mutex_lock_interruptible(&adc->lock)) | 
 | 72 | 		return -ERESTARTSYS; | 
 | 73 |  | 
| José Miguel Gonçalves | 5748150 | 2010-03-05 13:43:58 -0800 | [diff] [blame] | 74 | 	if (adc->channels == 1) { | 
 | 75 | 		status = spi_read(spi, rx_buf, sizeof(rx_buf)); | 
 | 76 | 	} else { | 
 | 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)); | 
 | 80 | 	} | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 81 | 	if (status < 0) { | 
| José Miguel Gonçalves | 5748150 | 2010-03-05 13:43:58 -0800 | [diff] [blame] | 82 | 		dev_warn(dev, "SPI synch. transfer failed with status %d\n", | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 83 | 				status); | 
 | 84 | 		goto out; | 
 | 85 | 	} | 
 | 86 |  | 
 | 87 | 	value = (rx_buf[0] << 8) + rx_buf[1]; | 
 | 88 | 	dev_dbg(dev, "raw value = 0x%x\n", value); | 
 | 89 |  | 
 | 90 | 	value = value * adc->reference >> 12; | 
 | 91 | 	status = sprintf(buf, "%d\n", value); | 
 | 92 | out: | 
 | 93 | 	mutex_unlock(&adc->lock); | 
 | 94 | 	return status; | 
 | 95 | } | 
 | 96 |  | 
 | 97 | static ssize_t adcxx_show_min(struct device *dev, | 
 | 98 | 		struct device_attribute *devattr, char *buf) | 
 | 99 | { | 
 | 100 | 	/* The minimum reference is 0 for this chip family */ | 
 | 101 | 	return sprintf(buf, "0\n"); | 
 | 102 | } | 
 | 103 |  | 
 | 104 | static ssize_t adcxx_show_max(struct device *dev, | 
 | 105 | 		struct device_attribute *devattr, char *buf) | 
 | 106 | { | 
 | 107 | 	struct spi_device *spi = to_spi_device(dev); | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 108 | 	struct adcxx *adc = spi_get_drvdata(spi); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 109 | 	u32 reference; | 
 | 110 |  | 
 | 111 | 	if (mutex_lock_interruptible(&adc->lock)) | 
 | 112 | 		return -ERESTARTSYS; | 
 | 113 |  | 
 | 114 | 	reference = adc->reference; | 
 | 115 |  | 
 | 116 | 	mutex_unlock(&adc->lock); | 
 | 117 |  | 
 | 118 | 	return sprintf(buf, "%d\n", reference); | 
 | 119 | } | 
 | 120 |  | 
 | 121 | static ssize_t adcxx_set_max(struct device *dev, | 
 | 122 | 	struct device_attribute *devattr, const char *buf, size_t count) | 
 | 123 | { | 
 | 124 | 	struct spi_device *spi = to_spi_device(dev); | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 125 | 	struct adcxx *adc = spi_get_drvdata(spi); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 126 | 	unsigned long value; | 
 | 127 |  | 
 | 128 | 	if (strict_strtoul(buf, 10, &value)) | 
 | 129 | 		return -EINVAL; | 
 | 130 |  | 
 | 131 | 	if (mutex_lock_interruptible(&adc->lock)) | 
 | 132 | 		return -ERESTARTSYS; | 
 | 133 |  | 
 | 134 | 	adc->reference = value; | 
 | 135 |  | 
 | 136 | 	mutex_unlock(&adc->lock); | 
 | 137 |  | 
 | 138 | 	return count; | 
 | 139 | } | 
 | 140 |  | 
 | 141 | static ssize_t adcxx_show_name(struct device *dev, struct device_attribute | 
 | 142 | 			      *devattr, char *buf) | 
 | 143 | { | 
 | 144 | 	struct spi_device *spi = to_spi_device(dev); | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 145 | 	struct adcxx *adc = spi_get_drvdata(spi); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 146 |  | 
 | 147 | 	return sprintf(buf, "adcxx%ds\n", adc->channels); | 
 | 148 | } | 
 | 149 |  | 
 | 150 | static struct sensor_device_attribute ad_input[] = { | 
 | 151 | 	SENSOR_ATTR(name, S_IRUGO, adcxx_show_name, NULL, 0), | 
 | 152 | 	SENSOR_ATTR(in_min, S_IRUGO, adcxx_show_min, NULL, 0), | 
 | 153 | 	SENSOR_ATTR(in_max, S_IWUSR | S_IRUGO, adcxx_show_max, | 
 | 154 | 					adcxx_set_max, 0), | 
 | 155 | 	SENSOR_ATTR(in0_input, S_IRUGO, adcxx_read, NULL, 0), | 
 | 156 | 	SENSOR_ATTR(in1_input, S_IRUGO, adcxx_read, NULL, 1), | 
 | 157 | 	SENSOR_ATTR(in2_input, S_IRUGO, adcxx_read, NULL, 2), | 
 | 158 | 	SENSOR_ATTR(in3_input, S_IRUGO, adcxx_read, NULL, 3), | 
 | 159 | 	SENSOR_ATTR(in4_input, S_IRUGO, adcxx_read, NULL, 4), | 
 | 160 | 	SENSOR_ATTR(in5_input, S_IRUGO, adcxx_read, NULL, 5), | 
 | 161 | 	SENSOR_ATTR(in6_input, S_IRUGO, adcxx_read, NULL, 6), | 
 | 162 | 	SENSOR_ATTR(in7_input, S_IRUGO, adcxx_read, NULL, 7), | 
 | 163 | }; | 
 | 164 |  | 
 | 165 | /*----------------------------------------------------------------------*/ | 
 | 166 |  | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 167 | static int __devinit adcxx_probe(struct spi_device *spi) | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 168 | { | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 169 | 	int channels = spi_get_device_id(spi)->driver_data; | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 170 | 	struct adcxx *adc; | 
 | 171 | 	int status; | 
 | 172 | 	int i; | 
 | 173 |  | 
 | 174 | 	adc = kzalloc(sizeof *adc, GFP_KERNEL); | 
 | 175 | 	if (!adc) | 
 | 176 | 		return -ENOMEM; | 
 | 177 |  | 
 | 178 | 	/* set a default value for the reference */ | 
 | 179 | 	adc->reference = 3300; | 
 | 180 | 	adc->channels = channels; | 
 | 181 | 	mutex_init(&adc->lock); | 
 | 182 |  | 
 | 183 | 	mutex_lock(&adc->lock); | 
 | 184 |  | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 185 | 	spi_set_drvdata(spi, adc); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 186 |  | 
 | 187 | 	for (i = 0; i < 3 + adc->channels; i++) { | 
 | 188 | 		status = device_create_file(&spi->dev, &ad_input[i].dev_attr); | 
 | 189 | 		if (status) { | 
 | 190 | 			dev_err(&spi->dev, "device_create_file failed.\n"); | 
 | 191 | 			goto out_err; | 
 | 192 | 		} | 
 | 193 | 	} | 
 | 194 |  | 
 | 195 | 	adc->hwmon_dev = hwmon_device_register(&spi->dev); | 
 | 196 | 	if (IS_ERR(adc->hwmon_dev)) { | 
 | 197 | 		dev_err(&spi->dev, "hwmon_device_register failed.\n"); | 
 | 198 | 		status = PTR_ERR(adc->hwmon_dev); | 
 | 199 | 		goto out_err; | 
 | 200 | 	} | 
 | 201 |  | 
 | 202 | 	mutex_unlock(&adc->lock); | 
 | 203 | 	return 0; | 
 | 204 |  | 
 | 205 | out_err: | 
 | 206 | 	for (i--; i >= 0; i--) | 
 | 207 | 		device_remove_file(&spi->dev, &ad_input[i].dev_attr); | 
 | 208 |  | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 209 | 	spi_set_drvdata(spi, NULL); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 210 | 	mutex_unlock(&adc->lock); | 
 | 211 | 	kfree(adc); | 
 | 212 | 	return status; | 
 | 213 | } | 
 | 214 |  | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 215 | static int __devexit adcxx_remove(struct spi_device *spi) | 
 | 216 | { | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 217 | 	struct adcxx *adc = spi_get_drvdata(spi); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 218 | 	int i; | 
 | 219 |  | 
 | 220 | 	mutex_lock(&adc->lock); | 
 | 221 | 	hwmon_device_unregister(adc->hwmon_dev); | 
 | 222 | 	for (i = 0; i < 3 + adc->channels; i++) | 
 | 223 | 		device_remove_file(&spi->dev, &ad_input[i].dev_attr); | 
 | 224 |  | 
| Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 225 | 	spi_set_drvdata(spi, NULL); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 226 | 	mutex_unlock(&adc->lock); | 
 | 227 | 	kfree(adc); | 
 | 228 |  | 
 | 229 | 	return 0; | 
 | 230 | } | 
 | 231 |  | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 232 | static const struct spi_device_id adcxx_ids[] = { | 
 | 233 | 	{ "adcxx1s", 1 }, | 
 | 234 | 	{ "adcxx2s", 2 }, | 
 | 235 | 	{ "adcxx4s", 4 }, | 
 | 236 | 	{ "adcxx8s", 8 }, | 
 | 237 | 	{ }, | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 238 | }; | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 239 | MODULE_DEVICE_TABLE(spi, adcxx_ids); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 240 |  | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 241 | static struct spi_driver adcxx_driver = { | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 242 | 	.driver = { | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 243 | 		.name	= "adcxx", | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 244 | 		.owner	= THIS_MODULE, | 
 | 245 | 	}, | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 246 | 	.id_table = adcxx_ids, | 
 | 247 | 	.probe	= adcxx_probe, | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 248 | 	.remove	= __devexit_p(adcxx_remove), | 
 | 249 | }; | 
 | 250 |  | 
 | 251 | static int __init init_adcxx(void) | 
 | 252 | { | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 253 | 	return spi_register_driver(&adcxx_driver); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 254 | } | 
 | 255 |  | 
 | 256 | static void __exit exit_adcxx(void) | 
 | 257 | { | 
| Anton Vorontsov | d2a5c10 | 2009-09-22 16:46:05 -0700 | [diff] [blame] | 258 | 	spi_unregister_driver(&adcxx_driver); | 
| Marc Pignat | d42139a | 2008-08-15 00:40:34 -0700 | [diff] [blame] | 259 | } | 
 | 260 |  | 
 | 261 | module_init(init_adcxx); | 
 | 262 | module_exit(exit_adcxx); | 
 | 263 |  | 
 | 264 | MODULE_AUTHOR("Marc Pignat"); | 
 | 265 | MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver"); | 
 | 266 | MODULE_LICENSE("GPL"); |