xref: /linux/drivers/staging/iio/adc/ad7816.c (revision a13d7201d7deedcbb6ac6efa94a1a7d34d3d79ec)
1 /*
2  * AD7816 digital temperature sensor driver supporting AD7816/7/8
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8 
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>
17 #include <linux/module.h>
18 
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
22 
23 /*
24  * AD7816 config masks
25  */
26 #define AD7816_FULL			0x1
27 #define AD7816_PD			0x2
28 #define AD7816_CS_MASK			0x7
29 #define AD7816_CS_MAX			0x4
30 
31 /*
32  * AD7816 temperature masks
33  */
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
40 
41 
42 /*
43  * struct ad7816_chip_info - chip specific information
44  */
45 
46 struct ad7816_chip_info {
47 	struct spi_device *spi_dev;
48 	u16 rdwr_pin;
49 	u16 convert_pin;
50 	u16 busy_pin;
51 	u8  oti_data[AD7816_CS_MAX+1];
52 	u8  channel_id;	/* 0 always be temperature */
53 	u8  mode;
54 };
55 
56 /*
57  * ad7816 data access by SPI
58  */
59 static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
60 {
61 	struct spi_device *spi_dev = chip->spi_dev;
62 	int ret = 0;
63 
64 	gpio_set_value(chip->rdwr_pin, 1);
65 	gpio_set_value(chip->rdwr_pin, 0);
66 	ret = spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id));
67 	if (ret < 0) {
68 		dev_err(&spi_dev->dev, "SPI channel setting error\n");
69 		return ret;
70 	}
71 	gpio_set_value(chip->rdwr_pin, 1);
72 
73 
74 	if (chip->mode == AD7816_PD) { /* operating mode 2 */
75 		gpio_set_value(chip->convert_pin, 1);
76 		gpio_set_value(chip->convert_pin, 0);
77 	} else { /* operating mode 1 */
78 		gpio_set_value(chip->convert_pin, 0);
79 		gpio_set_value(chip->convert_pin, 1);
80 	}
81 
82 	while (gpio_get_value(chip->busy_pin))
83 		cpu_relax();
84 
85 	gpio_set_value(chip->rdwr_pin, 0);
86 	gpio_set_value(chip->rdwr_pin, 1);
87 	ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
88 	if (ret < 0) {
89 		dev_err(&spi_dev->dev, "SPI data read error\n");
90 		return ret;
91 	}
92 
93 	*data = be16_to_cpu(*data);
94 
95 	return ret;
96 }
97 
98 static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
99 {
100 	struct spi_device *spi_dev = chip->spi_dev;
101 	int ret = 0;
102 
103 	gpio_set_value(chip->rdwr_pin, 1);
104 	gpio_set_value(chip->rdwr_pin, 0);
105 	ret = spi_write(spi_dev, &data, sizeof(data));
106 	if (ret < 0)
107 		dev_err(&spi_dev->dev, "SPI oti data write error\n");
108 
109 	return ret;
110 }
111 
112 static ssize_t ad7816_show_mode(struct device *dev,
113 		struct device_attribute *attr,
114 		char *buf)
115 {
116 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
117 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
118 
119 	if (chip->mode)
120 		return sprintf(buf, "power-save\n");
121 	return sprintf(buf, "full\n");
122 }
123 
124 static ssize_t ad7816_store_mode(struct device *dev,
125 		struct device_attribute *attr,
126 		const char *buf,
127 		size_t len)
128 {
129 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
130 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
131 
132 	if (strcmp(buf, "full")) {
133 		gpio_set_value(chip->rdwr_pin, 1);
134 		chip->mode = AD7816_FULL;
135 	} else {
136 		gpio_set_value(chip->rdwr_pin, 0);
137 		chip->mode = AD7816_PD;
138 	}
139 
140 	return len;
141 }
142 
143 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
144 		ad7816_show_mode,
145 		ad7816_store_mode,
146 		0);
147 
148 static ssize_t ad7816_show_available_modes(struct device *dev,
149 		struct device_attribute *attr,
150 		char *buf)
151 {
152 	return sprintf(buf, "full\npower-save\n");
153 }
154 
155 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7816_show_available_modes,
156 			NULL, 0);
157 
158 static ssize_t ad7816_show_channel(struct device *dev,
159 		struct device_attribute *attr,
160 		char *buf)
161 {
162 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
163 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
164 
165 	return sprintf(buf, "%d\n", chip->channel_id);
166 }
167 
168 static ssize_t ad7816_store_channel(struct device *dev,
169 		struct device_attribute *attr,
170 		const char *buf,
171 		size_t len)
172 {
173 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
174 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
175 	unsigned long data;
176 	int ret;
177 
178 	ret = kstrtoul(buf, 10, &data);
179 	if (ret)
180 		return ret;
181 
182 	if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) {
183 		dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n",
184 			data, indio_dev->name);
185 		return -EINVAL;
186 	} else if (strcmp(indio_dev->name, "ad7818") == 0 && data > 1) {
187 		dev_err(&chip->spi_dev->dev,
188 			"Invalid channel id %lu for ad7818.\n", data);
189 		return -EINVAL;
190 	} else if (strcmp(indio_dev->name, "ad7816") == 0 && data > 0) {
191 		dev_err(&chip->spi_dev->dev,
192 			"Invalid channel id %lu for ad7816.\n", data);
193 		return -EINVAL;
194 	}
195 
196 	chip->channel_id = data;
197 
198 	return len;
199 }
200 
201 static IIO_DEVICE_ATTR(channel, S_IRUGO | S_IWUSR,
202 		ad7816_show_channel,
203 		ad7816_store_channel,
204 		0);
205 
206 
207 static ssize_t ad7816_show_value(struct device *dev,
208 		struct device_attribute *attr,
209 		char *buf)
210 {
211 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
212 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
213 	u16 data;
214 	s8 value;
215 	int ret;
216 
217 	ret = ad7816_spi_read(chip, &data);
218 	if (ret)
219 		return -EIO;
220 
221 	data >>= AD7816_VALUE_OFFSET;
222 
223 	if (chip->channel_id == 0) {
224 		value = (s8)((data >> AD7816_TEMP_FLOAT_OFFSET) - 103);
225 		data &= AD7816_TEMP_FLOAT_MASK;
226 		if (value < 0)
227 			data = (1 << AD7816_TEMP_FLOAT_OFFSET) - data;
228 		return sprintf(buf, "%d.%.2d\n", value, data * 25);
229 	}
230 	return sprintf(buf, "%u\n", data);
231 }
232 
233 static IIO_DEVICE_ATTR(value, S_IRUGO, ad7816_show_value, NULL, 0);
234 
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,
240 	NULL,
241 };
242 
243 static const struct attribute_group ad7816_attribute_group = {
244 	.attrs = ad7816_attributes,
245 };
246 
247 /*
248  * temperature bound events
249  */
250 
251 #define IIO_EVENT_CODE_AD7816_OTI IIO_UNMOD_EVENT_CODE(IIO_TEMP,	\
252 						       0,		\
253 						       IIO_EV_TYPE_THRESH, \
254 						       IIO_EV_DIR_FALLING)
255 
256 static irqreturn_t ad7816_event_handler(int irq, void *private)
257 {
258 	iio_push_event(private, IIO_EVENT_CODE_AD7816_OTI, iio_get_time_ns());
259 	return IRQ_HANDLED;
260 }
261 
262 static ssize_t ad7816_show_oti(struct device *dev,
263 		struct device_attribute *attr,
264 		char *buf)
265 {
266 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
267 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
268 	int value;
269 
270 	if (chip->channel_id > AD7816_CS_MAX) {
271 		dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
272 		return -EINVAL;
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);
278 	}
279 	return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]);
280 }
281 
282 static inline ssize_t ad7816_set_oti(struct device *dev,
283 		struct device_attribute *attr,
284 		const char *buf,
285 		size_t len)
286 {
287 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
288 	struct ad7816_chip_info *chip = iio_priv(indio_dev);
289 	long value;
290 	u8 data;
291 	int ret;
292 
293 	ret = kstrtol(buf, 10, &value);
294 	if (ret)
295 		return ret;
296 
297 	if (chip->channel_id > AD7816_CS_MAX) {
298 		dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id);
299 		return -EINVAL;
300 	} else if (chip->channel_id == 0) {
301 		if (ret || value < AD7816_BOUND_VALUE_MIN ||
302 			value > AD7816_BOUND_VALUE_MAX)
303 			return -EINVAL;
304 
305 		data = (u8)(value - AD7816_BOUND_VALUE_MIN +
306 			AD7816_BOUND_VALUE_BASE);
307 	} else {
308 		if (ret || value < AD7816_BOUND_VALUE_BASE || value > 255)
309 			return -EINVAL;
310 
311 		data = (u8)value;
312 	}
313 
314 	ret = ad7816_spi_write(chip, data);
315 	if (ret)
316 		return -EIO;
317 
318 	chip->oti_data[chip->channel_id] = data;
319 
320 	return len;
321 }
322 
323 static IIO_DEVICE_ATTR(oti, S_IRUGO | S_IWUSR,
324 		       ad7816_show_oti, ad7816_set_oti, 0);
325 
326 static struct attribute *ad7816_event_attributes[] = {
327 	&iio_dev_attr_oti.dev_attr.attr,
328 	NULL,
329 };
330 
331 static struct attribute_group ad7816_event_attribute_group = {
332 	.attrs = ad7816_event_attributes,
333 	.name = "events",
334 };
335 
336 static const struct iio_info ad7816_info = {
337 	.attrs = &ad7816_attribute_group,
338 	.event_attrs = &ad7816_event_attribute_group,
339 	.driver_module = THIS_MODULE,
340 };
341 
342 /*
343  * device probe and remove
344  */
345 
346 static int ad7816_probe(struct spi_device *spi_dev)
347 {
348 	struct ad7816_chip_info *chip;
349 	struct iio_dev *indio_dev;
350 	unsigned short *pins = spi_dev->dev.platform_data;
351 	int ret = 0;
352 	int i;
353 
354 	if (!pins) {
355 		dev_err(&spi_dev->dev, "No necessary GPIO platform data.\n");
356 		return -EINVAL;
357 	}
358 
359 	indio_dev = devm_iio_device_alloc(&spi_dev->dev, sizeof(*chip));
360 	if (!indio_dev)
361 		return -ENOMEM;
362 	chip = iio_priv(indio_dev);
363 	/* this is only used for device removal purposes */
364 	dev_set_drvdata(&spi_dev->dev, indio_dev);
365 
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];
372 
373 	ret = devm_gpio_request(&spi_dev->dev, chip->rdwr_pin,
374 					spi_get_device_id(spi_dev)->name);
375 	if (ret) {
376 		dev_err(&spi_dev->dev, "Fail to request rdwr gpio PIN %d.\n",
377 			chip->rdwr_pin);
378 		return ret;
379 	}
380 	gpio_direction_input(chip->rdwr_pin);
381 	ret = devm_gpio_request(&spi_dev->dev, chip->convert_pin,
382 					spi_get_device_id(spi_dev)->name);
383 	if (ret) {
384 		dev_err(&spi_dev->dev, "Fail to request convert gpio PIN %d.\n",
385 			chip->convert_pin);
386 		return ret;
387 	}
388 	gpio_direction_input(chip->convert_pin);
389 	ret = devm_gpio_request(&spi_dev->dev, chip->busy_pin,
390 					spi_get_device_id(spi_dev)->name);
391 	if (ret) {
392 		dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n",
393 			chip->busy_pin);
394 		return ret;
395 	}
396 	gpio_direction_input(chip->busy_pin);
397 
398 	indio_dev->name = spi_get_device_id(spi_dev)->name;
399 	indio_dev->dev.parent = &spi_dev->dev;
400 	indio_dev->info = &ad7816_info;
401 	indio_dev->modes = INDIO_DIRECT_MODE;
402 
403 	if (spi_dev->irq) {
404 		/* Only low trigger is supported in ad7816/7/8 */
405 		ret = devm_request_threaded_irq(&spi_dev->dev, spi_dev->irq,
406 						NULL,
407 						&ad7816_event_handler,
408 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
409 						indio_dev->name,
410 						indio_dev);
411 		if (ret)
412 			return ret;
413 	}
414 
415 	ret = devm_iio_device_register(&spi_dev->dev, indio_dev);
416 	if (ret)
417 		return ret;
418 
419 	dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n",
420 			 indio_dev->name);
421 
422 	return 0;
423 }
424 
425 static const struct spi_device_id ad7816_id[] = {
426 	{ "ad7816", 0 },
427 	{ "ad7817", 0 },
428 	{ "ad7818", 0 },
429 	{}
430 };
431 
432 MODULE_DEVICE_TABLE(spi, ad7816_id);
433 
434 static struct spi_driver ad7816_driver = {
435 	.driver = {
436 		.name = "ad7816",
437 		.owner = THIS_MODULE,
438 	},
439 	.probe = ad7816_probe,
440 	.id_table = ad7816_id,
441 };
442 module_spi_driver(ad7816_driver);
443 
444 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
445 MODULE_DESCRIPTION("Analog Devices AD7816/7/8 digital temperature sensor driver");
446 MODULE_LICENSE("GPL v2");
447