11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/list.h>
17 #include <linux/module.h>
26 #define AD7816_FULL 0x1
28 #define AD7816_CS_MASK 0x7
29 #define AD7816_CS_MAX 0x4
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
68 dev_err(&spi_dev->
dev,
"SPI channel setting error\n");
87 ret = spi_read(spi_dev, (
u8 *)data,
sizeof(*data));
89 dev_err(&spi_dev->
dev,
"SPI data read error\n");
105 ret = spi_write(spi_dev, &data,
sizeof(data));
107 dev_err(&spi_dev->
dev,
"SPI oti data write error\n");
116 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
120 return sprintf(buf,
"power-save\n");
130 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
133 if (
strcmp(buf,
"full")) {
149 static ssize_t ad7816_show_available_modes(
struct device *dev,
153 return sprintf(buf,
"full\npower-save\n");
162 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
173 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
184 data, indio_dev->
name);
186 }
else if (
strcmp(indio_dev->
name,
"ad7818") == 0 && data > 1) {
188 "Invalid channel id %lu for ad7818.\n", data);
190 }
else if (
strcmp(indio_dev->
name,
"ad7816") == 0 && data > 0) {
192 "Invalid channel id %lu for ad7816.\n", data);
203 ad7816_store_channel,
211 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
217 ret = ad7816_spi_read(chip, &data);
228 return sprintf(buf,
"%d.%.2d\n", value, data * 25);
230 return sprintf(buf,
"%u\n", data);
235 static struct attribute *ad7816_attributes[] = {
236 &iio_dev_attr_available_modes.dev_attr.attr,
237 &iio_dev_attr_mode.dev_attr.attr,
238 &iio_dev_attr_channel.dev_attr.attr,
239 &iio_dev_attr_value.dev_attr.attr,
244 .attrs = ad7816_attributes,
251 #define IIO_EVENT_CODE_AD7816_OTI IIO_UNMOD_EVENT_CODE(IIO_TEMP, \
253 IIO_EV_TYPE_THRESH, \
256 static irqreturn_t ad7816_event_handler(
int irq,
void *
private)
266 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
277 return sprintf(buf,
"%d\n", value);
287 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
306 if (ret || value < AD7816_BOUND_VALUE_BASE || value > 255)
312 ret = ad7816_spi_write(chip, data);
322 ad7816_show_oti, ad7816_set_oti, 0);
324 static struct attribute *ad7816_event_attributes[] = {
325 &iio_dev_attr_oti.dev_attr.attr,
330 .attrs = ad7816_event_attributes,
334 static const struct iio_info ad7816_info = {
335 .attrs = &ad7816_attribute_group,
336 .event_attrs = &ad7816_event_attribute_group,
348 unsigned short *
pins = spi_dev->
dev.platform_data;
353 dev_err(&spi_dev->
dev,
"No necessary GPIO platform data.\n");
358 if (indio_dev ==
NULL) {
362 chip = iio_priv(indio_dev);
375 dev_err(&spi_dev->
dev,
"Fail to request rdwr gpio PIN %d.\n",
377 goto error_free_device;
382 dev_err(&spi_dev->
dev,
"Fail to request convert gpio PIN %d.\n",
384 goto error_free_gpio_rdwr;
389 dev_err(&spi_dev->
dev,
"Fail to request busy gpio PIN %d.\n",
391 goto error_free_gpio_convert;
396 indio_dev->
dev.parent = &spi_dev->
dev;
397 indio_dev->
info = &ad7816_info;
404 &ad7816_event_handler,
409 goto error_free_gpio;
416 dev_info(&spi_dev->
dev,
"%s temperature sensor and ADC registered.\n",
424 error_free_gpio_convert:
426 error_free_gpio_rdwr:
465 .probe = ad7816_probe,
467 .id_table = ad7816_id,
473 " temperature sensor driver");