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/workqueue.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/list.h>
17 #include <linux/spi/spi.h>
18 #include <linux/rtc.h>
26 #define AD7816_FULL 0x1
28 #define AD7816_CS_MASK 0x7
29 #define AD7816_CS_MAX 0x4
32 * AD7816 temperature masks
34 #define AD7816_VALUE_OFFSET 6
35 #define AD7816_BOUND_VALUE_BASE 0x8
36 #define AD7816_BOUND_VALUE_MIN -95
37 #define AD7816_BOUND_VALUE_MAX 152
38 #define AD7816_TEMP_FLOAT_OFFSET 2
39 #define AD7816_TEMP_FLOAT_MASK 0x3
43 * struct ad7816_chip_info - chip specifc information
46 struct ad7816_chip_info {
48 struct spi_device *spi_dev;
49 struct iio_dev *indio_dev;
50 struct work_struct thresh_work;
55 u8 oti_data[AD7816_CS_MAX+1];
56 u8 channel_id; /* 0 always be temperature */
61 * ad7816 data access by SPI
63 static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
65 struct spi_device *spi_dev = chip->spi_dev;
68 gpio_set_value(chip->rdwr_pin, 1);
69 gpio_set_value(chip->rdwr_pin, 0);
70 ret = spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id));
72 dev_err(&spi_dev->dev, "SPI channel setting error\n");
75 gpio_set_value(chip->rdwr_pin, 1);
78 if (chip->mode == AD7816_PD) { /* operating mode 2 */
79 gpio_set_value(chip->convert_pin, 1);
80 gpio_set_value(chip->convert_pin, 0);
81 } else { /* operating mode 1 */
82 gpio_set_value(chip->convert_pin, 0);
83 gpio_set_value(chip->convert_pin, 1);
86 while (gpio_get_value(chip->busy_pin))
89 gpio_set_value(chip->rdwr_pin, 0);
90 gpio_set_value(chip->rdwr_pin, 1);
91 ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
93 dev_err(&spi_dev->dev, "SPI data read error\n");
97 *data = be16_to_cpu(*data);
102 static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
104 struct spi_device *spi_dev = chip->spi_dev;
107 gpio_set_value(chip->rdwr_pin, 1);
108 gpio_set_value(chip->rdwr_pin, 0);
109 ret = spi_write(spi_dev, &data, sizeof(data));
111 dev_err(&spi_dev->dev, "SPI oti data write error\n");
116 static ssize_t ad7816_show_mode(struct device *dev,
117 struct device_attribute *attr,
120 struct iio_dev *dev_info = dev_get_drvdata(dev);
121 struct ad7816_chip_info *chip = dev_info->dev_data;
124 return sprintf(buf, "power-save\n");
126 return sprintf(buf, "full\n");
129 static ssize_t ad7816_store_mode(struct device *dev,
130 struct device_attribute *attr,
134 struct iio_dev *dev_info = dev_get_drvdata(dev);
135 struct ad7816_chip_info *chip = dev_info->dev_data;
137 if (strcmp(buf, "full")) {
138 gpio_set_value(chip->rdwr_pin, 1);
139 chip->mode = AD7816_FULL;
141 gpio_set_value(chip->rdwr_pin, 0);
142 chip->mode = AD7816_PD;
148 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
153 static ssize_t ad7816_show_available_modes(struct device *dev,
154 struct device_attribute *attr,
157 return sprintf(buf, "full\npower-save\n");
160 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7816_show_available_modes, NULL, 0);
162 static ssize_t ad7816_show_channel(struct device *dev,
163 struct device_attribute *attr,
166 struct iio_dev *dev_info = dev_get_drvdata(dev);
167 struct ad7816_chip_info *chip = dev_info->dev_data;
169 return sprintf(buf, "%d\n", chip->channel_id);
172 static ssize_t ad7816_store_channel(struct device *dev,
173 struct device_attribute *attr,
177 struct iio_dev *dev_info = dev_get_drvdata(dev);
178 struct ad7816_chip_info *chip = dev_info->dev_data;
182 ret = strict_strtoul(buf, 10, &data);
186 if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) {
187 dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n",
190 } else if (strcmp(chip->name, "ad7818") == 0 && data > 1) {
191 dev_err(&chip->spi_dev->dev,
192 "Invalid channel id %lu for ad7818.\n", data);
194 } else if (strcmp(chip->name, "ad7816") == 0 && data > 0) {
195 dev_err(&chip->spi_dev->dev,
196 "Invalid channel id %lu for ad7816.\n", data);
200 chip->channel_id = data;
205 static IIO_DEVICE_ATTR(channel, S_IRUGO | S_IWUSR,
207 ad7816_store_channel,
211 static ssize_t ad7816_show_value(struct device *dev,
212 struct device_attribute *attr,
215 struct iio_dev *dev_info = dev_get_drvdata(dev);
216 struct ad7816_chip_info *chip = dev_info->dev_data;
221 ret = ad7816_spi_read(chip, &data);
225 data >>= AD7816_VALUE_OFFSET;
227 if (chip->channel_id == 0) {
228 value = (s8)((data >> AD7816_TEMP_FLOAT_OFFSET) - 103);
229 data &= AD7816_TEMP_FLOAT_MASK;
231 data = (1 << AD7816_TEMP_FLOAT_OFFSET) - data;
232 return sprintf(buf, "%d.%.2d\n", value, data * 25);
234 return sprintf(buf, "%u\n", data);
237 static IIO_DEVICE_ATTR(value, S_IRUGO, ad7816_show_value, NULL, 0);
239 static ssize_t ad7816_show_name(struct device *dev,
240 struct device_attribute *attr,
243 struct iio_dev *dev_info = dev_get_drvdata(dev);
244 struct ad7816_chip_info *chip = dev_info->dev_data;
245 return sprintf(buf, "%s\n", chip->name);
248 static IIO_DEVICE_ATTR(name, S_IRUGO, ad7816_show_name, NULL, 0);
250 static struct attribute *ad7816_attributes[] = {
251 &iio_dev_attr_available_modes.dev_attr.attr,
252 &iio_dev_attr_mode.dev_attr.attr,
253 &iio_dev_attr_channel.dev_attr.attr,
254 &iio_dev_attr_value.dev_attr.attr,
255 &iio_dev_attr_name.dev_attr.attr,
259 static const struct attribute_group ad7816_attribute_group = {
260 .attrs = ad7816_attributes,
264 * temperature bound events
267 #define IIO_EVENT_CODE_AD7816_OTI IIO_BUFFER_EVENT_CODE(0)
269 static void ad7816_interrupt_bh(struct work_struct *work_s)
271 struct ad7816_chip_info *chip =
272 container_of(work_s, struct ad7816_chip_info, thresh_work);
274 enable_irq(chip->spi_dev->irq);
276 iio_push_event(chip->indio_dev, 0,
277 IIO_EVENT_CODE_AD7816_OTI,
278 chip->last_timestamp);
281 static int ad7816_interrupt(struct iio_dev *dev_info,
286 struct ad7816_chip_info *chip = dev_info->dev_data;
288 chip->last_timestamp = timestamp;
289 schedule_work(&chip->thresh_work);
294 IIO_EVENT_SH(ad7816, &ad7816_interrupt);
296 static ssize_t ad7816_show_oti(struct device *dev,
297 struct device_attribute *attr,
300 struct iio_dev *dev_info = dev_get_drvdata(dev);
301 struct ad7816_chip_info *chip = dev_info->dev_data;
304 if (chip->channel_id > AD7816_CS_MAX) {
305 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
307 } else if (chip->channel_id == 0) {
308 value = AD7816_BOUND_VALUE_MIN +
309 (chip->oti_data[chip->channel_id] -
310 AD7816_BOUND_VALUE_BASE);
311 return sprintf(buf, "%d\n", value);
313 return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]);
316 static inline ssize_t ad7816_set_oti(struct device *dev,
317 struct device_attribute *attr,
321 struct iio_dev *dev_info = dev_get_drvdata(dev);
322 struct ad7816_chip_info *chip = dev_info->dev_data;
327 ret = strict_strtol(buf, 10, &value);
329 if (chip->channel_id > AD7816_CS_MAX) {
330 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
332 } else if (chip->channel_id == 0) {
333 if (ret || value < AD7816_BOUND_VALUE_MIN ||
334 value > AD7816_BOUND_VALUE_MAX)
337 data = (u8)(value - AD7816_BOUND_VALUE_MIN +
338 AD7816_BOUND_VALUE_BASE);
340 if (ret || value < AD7816_BOUND_VALUE_BASE || value > 255)
346 ret = ad7816_spi_write(chip, data);
350 chip->oti_data[chip->channel_id] = data;
355 IIO_EVENT_ATTR_SH(oti, iio_event_ad7816,
356 ad7816_show_oti, ad7816_set_oti, 0);
358 static struct attribute *ad7816_event_attributes[] = {
359 &iio_event_attr_oti.dev_attr.attr,
363 static struct attribute_group ad7816_event_attribute_group = {
364 .attrs = ad7816_event_attributes,
368 * device probe and remove
371 static int __devinit ad7816_probe(struct spi_device *spi_dev)
373 struct ad7816_chip_info *chip;
374 unsigned short *pins = spi_dev->dev.platform_data;
379 dev_err(&spi_dev->dev, "No necessary GPIO platform data.\n");
383 chip = kzalloc(sizeof(struct ad7816_chip_info), GFP_KERNEL);
388 /* this is only used for device removal purposes */
389 dev_set_drvdata(&spi_dev->dev, chip);
391 chip->spi_dev = spi_dev;
392 chip->name = spi_dev->modalias;
393 for (i = 0; i <= AD7816_CS_MAX; i++)
394 chip->oti_data[i] = 203;
395 chip->rdwr_pin = pins[0];
396 chip->convert_pin = pins[1];
397 chip->busy_pin = pins[2];
399 ret = gpio_request(chip->rdwr_pin, chip->name);
401 dev_err(&spi_dev->dev, "Fail to request rdwr gpio PIN %d.\n",
403 goto error_free_chip;
405 gpio_direction_input(chip->rdwr_pin);
406 ret = gpio_request(chip->convert_pin, chip->name);
408 dev_err(&spi_dev->dev, "Fail to request convert gpio PIN %d.\n",
410 goto error_free_gpio_rdwr;
412 gpio_direction_input(chip->convert_pin);
413 ret = gpio_request(chip->busy_pin, chip->name);
415 dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n",
417 goto error_free_gpio_convert;
419 gpio_direction_input(chip->busy_pin);
421 chip->indio_dev = iio_allocate_device();
422 if (chip->indio_dev == NULL) {
424 goto error_free_gpio;
427 chip->indio_dev->dev.parent = &spi_dev->dev;
428 chip->indio_dev->attrs = &ad7816_attribute_group;
429 chip->indio_dev->event_attrs = &ad7816_event_attribute_group;
430 chip->indio_dev->dev_data = (void *)chip;
431 chip->indio_dev->driver_module = THIS_MODULE;
432 chip->indio_dev->num_interrupt_lines = 1;
433 chip->indio_dev->modes = INDIO_DIRECT_MODE;
435 ret = iio_device_register(chip->indio_dev);
440 /* Only low trigger is supported in ad7816/7/8 */
441 ret = iio_register_interrupt_line(spi_dev->irq,
447 goto error_unreg_dev;
450 * The event handler list element refer to iio_event_ad7816.
451 * All event attributes bind to the same event handler.
452 * So, only register event handler once.
454 iio_add_event_to_list(&iio_event_ad7816,
455 &chip->indio_dev->interrupts[0]->ev_list);
457 INIT_WORK(&chip->thresh_work, ad7816_interrupt_bh);
460 dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n",
466 iio_device_unregister(chip->indio_dev);
468 iio_free_device(chip->indio_dev);
470 gpio_free(chip->busy_pin);
471 error_free_gpio_convert:
472 gpio_free(chip->convert_pin);
473 error_free_gpio_rdwr:
474 gpio_free(chip->rdwr_pin);
481 static int __devexit ad7816_remove(struct spi_device *spi_dev)
483 struct ad7816_chip_info *chip = dev_get_drvdata(&spi_dev->dev);
484 struct iio_dev *indio_dev = chip->indio_dev;
486 dev_set_drvdata(&spi_dev->dev, NULL);
488 iio_unregister_interrupt_line(indio_dev, 0);
489 iio_device_unregister(indio_dev);
490 iio_free_device(chip->indio_dev);
491 gpio_free(chip->busy_pin);
492 gpio_free(chip->convert_pin);
493 gpio_free(chip->rdwr_pin);
499 static const struct spi_device_id ad7816_id[] = {
506 MODULE_DEVICE_TABLE(spi, ad7816_id);
508 static struct spi_driver ad7816_driver = {
511 .bus = &spi_bus_type,
512 .owner = THIS_MODULE,
514 .probe = ad7816_probe,
515 .remove = __devexit_p(ad7816_remove),
516 .id_table = ad7816_id,
519 static __init int ad7816_init(void)
521 return spi_register_driver(&ad7816_driver);
524 static __exit void ad7816_exit(void)
526 spi_unregister_driver(&ad7816_driver);
529 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
530 MODULE_DESCRIPTION("Analog Devices AD7816/7/8 digital"
531 " temperature sensor driver");
532 MODULE_LICENSE("GPL v2");
534 module_init(ad7816_init);
535 module_exit(ad7816_exit);