2 * AD7816 digital temperature sensor driver supporting AD7816/7/8
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/gpio.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/spi/spi.h>
24 #define AD7816_FULL 0x1
26 #define AD7816_CS_MASK 0x7
27 #define AD7816_CS_MAX 0x4
30 * AD7816 temperature masks
32 #define AD7816_VALUE_OFFSET 6
33 #define AD7816_BOUND_VALUE_BASE 0x8
34 #define AD7816_BOUND_VALUE_MIN -95
35 #define AD7816_BOUND_VALUE_MAX 152
36 #define AD7816_TEMP_FLOAT_OFFSET 2
37 #define AD7816_TEMP_FLOAT_MASK 0x3
41 * struct ad7816_chip_info - chip specifc information
44 struct ad7816_chip_info {
45 struct spi_device *spi_dev;
49 u8 oti_data[AD7816_CS_MAX+1];
50 u8 channel_id; /* 0 always be temperature */
55 * ad7816 data access by SPI
57 static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
59 struct spi_device *spi_dev = chip->spi_dev;
62 gpio_set_value(chip->rdwr_pin, 1);
63 gpio_set_value(chip->rdwr_pin, 0);
64 ret = spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id));
66 dev_err(&spi_dev->dev, "SPI channel setting error\n");
69 gpio_set_value(chip->rdwr_pin, 1);
72 if (chip->mode == AD7816_PD) { /* operating mode 2 */
73 gpio_set_value(chip->convert_pin, 1);
74 gpio_set_value(chip->convert_pin, 0);
75 } else { /* operating mode 1 */
76 gpio_set_value(chip->convert_pin, 0);
77 gpio_set_value(chip->convert_pin, 1);
80 while (gpio_get_value(chip->busy_pin))
83 gpio_set_value(chip->rdwr_pin, 0);
84 gpio_set_value(chip->rdwr_pin, 1);
85 ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
87 dev_err(&spi_dev->dev, "SPI data read error\n");
91 *data = be16_to_cpu(*data);
96 static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
98 struct spi_device *spi_dev = chip->spi_dev;
101 gpio_set_value(chip->rdwr_pin, 1);
102 gpio_set_value(chip->rdwr_pin, 0);
103 ret = spi_write(spi_dev, &data, sizeof(data));
105 dev_err(&spi_dev->dev, "SPI oti data write error\n");
110 static ssize_t ad7816_show_mode(struct device *dev,
111 struct device_attribute *attr,
114 struct iio_dev *dev_info = dev_get_drvdata(dev);
115 struct ad7816_chip_info *chip = iio_priv(dev_info);
118 return sprintf(buf, "power-save\n");
120 return sprintf(buf, "full\n");
123 static ssize_t ad7816_store_mode(struct device *dev,
124 struct device_attribute *attr,
128 struct iio_dev *dev_info = dev_get_drvdata(dev);
129 struct ad7816_chip_info *chip = iio_priv(dev_info);
131 if (strcmp(buf, "full")) {
132 gpio_set_value(chip->rdwr_pin, 1);
133 chip->mode = AD7816_FULL;
135 gpio_set_value(chip->rdwr_pin, 0);
136 chip->mode = AD7816_PD;
142 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
147 static ssize_t ad7816_show_available_modes(struct device *dev,
148 struct device_attribute *attr,
151 return sprintf(buf, "full\npower-save\n");
154 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7816_show_available_modes, NULL, 0);
156 static ssize_t ad7816_show_channel(struct device *dev,
157 struct device_attribute *attr,
160 struct iio_dev *dev_info = dev_get_drvdata(dev);
161 struct ad7816_chip_info *chip = iio_priv(dev_info);
163 return sprintf(buf, "%d\n", chip->channel_id);
166 static ssize_t ad7816_store_channel(struct device *dev,
167 struct device_attribute *attr,
171 struct iio_dev *dev_info = dev_get_drvdata(dev);
172 struct ad7816_chip_info *chip = iio_priv(dev_info);
176 ret = strict_strtoul(buf, 10, &data);
180 if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) {
181 dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n",
182 data, dev_info->name);
184 } else if (strcmp(dev_info->name, "ad7818") == 0 && data > 1) {
185 dev_err(&chip->spi_dev->dev,
186 "Invalid channel id %lu for ad7818.\n", data);
188 } else if (strcmp(dev_info->name, "ad7816") == 0 && data > 0) {
189 dev_err(&chip->spi_dev->dev,
190 "Invalid channel id %lu for ad7816.\n", data);
194 chip->channel_id = data;
199 static IIO_DEVICE_ATTR(channel, S_IRUGO | S_IWUSR,
201 ad7816_store_channel,
205 static ssize_t ad7816_show_value(struct device *dev,
206 struct device_attribute *attr,
209 struct iio_dev *dev_info = dev_get_drvdata(dev);
210 struct ad7816_chip_info *chip = iio_priv(dev_info);
215 ret = ad7816_spi_read(chip, &data);
219 data >>= AD7816_VALUE_OFFSET;
221 if (chip->channel_id == 0) {
222 value = (s8)((data >> AD7816_TEMP_FLOAT_OFFSET) - 103);
223 data &= AD7816_TEMP_FLOAT_MASK;
225 data = (1 << AD7816_TEMP_FLOAT_OFFSET) - data;
226 return sprintf(buf, "%d.%.2d\n", value, data * 25);
228 return sprintf(buf, "%u\n", data);
231 static IIO_DEVICE_ATTR(value, S_IRUGO, ad7816_show_value, NULL, 0);
233 static struct attribute *ad7816_attributes[] = {
234 &iio_dev_attr_available_modes.dev_attr.attr,
235 &iio_dev_attr_mode.dev_attr.attr,
236 &iio_dev_attr_channel.dev_attr.attr,
237 &iio_dev_attr_value.dev_attr.attr,
241 static const struct attribute_group ad7816_attribute_group = {
242 .attrs = ad7816_attributes,
246 * temperature bound events
249 #define IIO_EVENT_CODE_AD7816_OTI IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_TEMP, \
251 IIO_EV_TYPE_THRESH, \
254 static irqreturn_t ad7816_event_handler(int irq, void *private)
256 iio_push_event(private, 0,
257 IIO_EVENT_CODE_AD7816_OTI,
262 static ssize_t ad7816_show_oti(struct device *dev,
263 struct device_attribute *attr,
266 struct iio_dev *dev_info = dev_get_drvdata(dev);
267 struct ad7816_chip_info *chip = iio_priv(dev_info);
270 if (chip->channel_id > AD7816_CS_MAX) {
271 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
273 } else if (chip->channel_id == 0) {
274 value = AD7816_BOUND_VALUE_MIN +
275 (chip->oti_data[chip->channel_id] -
276 AD7816_BOUND_VALUE_BASE);
277 return sprintf(buf, "%d\n", value);
279 return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]);
282 static inline ssize_t ad7816_set_oti(struct device *dev,
283 struct device_attribute *attr,
287 struct iio_dev *dev_info = dev_get_drvdata(dev);
288 struct ad7816_chip_info *chip = iio_priv(dev_info);
293 ret = strict_strtol(buf, 10, &value);
295 if (chip->channel_id > AD7816_CS_MAX) {
296 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
298 } else if (chip->channel_id == 0) {
299 if (ret || value < AD7816_BOUND_VALUE_MIN ||
300 value > AD7816_BOUND_VALUE_MAX)
303 data = (u8)(value - AD7816_BOUND_VALUE_MIN +
304 AD7816_BOUND_VALUE_BASE);
306 if (ret || value < AD7816_BOUND_VALUE_BASE || value > 255)
312 ret = ad7816_spi_write(chip, data);
316 chip->oti_data[chip->channel_id] = data;
321 static IIO_DEVICE_ATTR(oti, S_IRUGO | S_IWUSR,
322 ad7816_show_oti, ad7816_set_oti, 0);
324 static struct attribute *ad7816_event_attributes[] = {
325 &iio_dev_attr_oti.dev_attr.attr,
329 static struct attribute_group ad7816_event_attribute_group = {
330 .attrs = ad7816_event_attributes,
333 static const struct iio_info ad7816_info = {
334 .attrs = &ad7816_attribute_group,
335 .num_interrupt_lines = 1,
336 .event_attrs = &ad7816_event_attribute_group,
337 .driver_module = THIS_MODULE,
341 * device probe and remove
344 static int __devinit ad7816_probe(struct spi_device *spi_dev)
346 struct ad7816_chip_info *chip;
347 struct iio_dev *indio_dev;
348 unsigned short *pins = spi_dev->dev.platform_data;
353 dev_err(&spi_dev->dev, "No necessary GPIO platform data.\n");
357 indio_dev = iio_allocate_device(sizeof(*chip));
358 if (indio_dev == NULL) {
362 chip = iio_priv(indio_dev);
363 /* this is only used for device removal purposes */
364 dev_set_drvdata(&spi_dev->dev, indio_dev);
366 chip->spi_dev = spi_dev;
367 for (i = 0; i <= AD7816_CS_MAX; i++)
368 chip->oti_data[i] = 203;
369 chip->rdwr_pin = pins[0];
370 chip->convert_pin = pins[1];
371 chip->busy_pin = pins[2];
373 ret = gpio_request(chip->rdwr_pin, spi_get_device_id(spi_dev)->name);
375 dev_err(&spi_dev->dev, "Fail to request rdwr gpio PIN %d.\n",
377 goto error_free_device;
379 gpio_direction_input(chip->rdwr_pin);
380 ret = gpio_request(chip->convert_pin, spi_get_device_id(spi_dev)->name);
382 dev_err(&spi_dev->dev, "Fail to request convert gpio PIN %d.\n",
384 goto error_free_gpio_rdwr;
386 gpio_direction_input(chip->convert_pin);
387 ret = gpio_request(chip->busy_pin, spi_get_device_id(spi_dev)->name);
389 dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n",
391 goto error_free_gpio_convert;
393 gpio_direction_input(chip->busy_pin);
395 indio_dev->name = spi_get_device_id(spi_dev)->name;
396 indio_dev->dev.parent = &spi_dev->dev;
397 indio_dev->info = &ad7816_info;
398 indio_dev->modes = INDIO_DIRECT_MODE;
400 ret = iio_device_register(indio_dev);
402 goto error_free_gpio;
405 /* Only low trigger is supported in ad7816/7/8 */
406 ret = request_threaded_irq(spi_dev->irq,
408 &ad7816_event_handler,
413 goto error_unreg_dev;
416 dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n",
422 iio_device_unregister(indio_dev);
424 gpio_free(chip->busy_pin);
425 error_free_gpio_convert:
426 gpio_free(chip->convert_pin);
427 error_free_gpio_rdwr:
428 gpio_free(chip->rdwr_pin);
430 iio_free_device(indio_dev);
435 static int __devexit ad7816_remove(struct spi_device *spi_dev)
437 struct iio_dev *indio_dev = dev_get_drvdata(&spi_dev->dev);
438 struct ad7816_chip_info *chip = iio_priv(indio_dev);
440 dev_set_drvdata(&spi_dev->dev, NULL);
442 free_irq(spi_dev->irq, indio_dev);
443 gpio_free(chip->busy_pin);
444 gpio_free(chip->convert_pin);
445 gpio_free(chip->rdwr_pin);
446 iio_device_unregister(indio_dev);
447 iio_free_device(indio_dev);
452 static const struct spi_device_id ad7816_id[] = {
459 MODULE_DEVICE_TABLE(spi, ad7816_id);
461 static struct spi_driver ad7816_driver = {
464 .bus = &spi_bus_type,
465 .owner = THIS_MODULE,
467 .probe = ad7816_probe,
468 .remove = __devexit_p(ad7816_remove),
469 .id_table = ad7816_id,
472 static __init int ad7816_init(void)
474 return spi_register_driver(&ad7816_driver);
477 static __exit void ad7816_exit(void)
479 spi_unregister_driver(&ad7816_driver);
482 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
483 MODULE_DESCRIPTION("Analog Devices AD7816/7/8 digital"
484 " temperature sensor driver");
485 MODULE_LICENSE("GPL v2");
487 module_init(ad7816_init);
488 module_exit(ad7816_exit);