Commit | Line | Data |
---|---|---|
ddaecd5b SZ |
1 | /* |
2 | * AD7291 digital temperature sensor driver supporting AD7291 | |
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> | |
ddaecd5b SZ |
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/i2c.h> | |
ddaecd5b SZ |
17 | |
18 | #include "../iio.h" | |
19 | #include "../sysfs.h" | |
20 | ||
21 | /* | |
22 | * AD7291 registers definition | |
23 | */ | |
24 | #define AD7291_COMMAND 0 | |
25 | #define AD7291_VOLTAGE 1 | |
26 | #define AD7291_T_SENSE 2 | |
27 | #define AD7291_T_AVERAGE 3 | |
28 | #define AD7291_VOLTAGE_LIMIT_BASE 4 | |
29 | #define AD7291_VOLTAGE_LIMIT_COUNT 8 | |
30 | #define AD7291_T_SENSE_HIGH 0x1c | |
31 | #define AD7291_T_SENSE_LOW 0x1d | |
32 | #define AD7291_T_SENSE_HYST 0x1e | |
33 | #define AD7291_VOLTAGE_ALERT_STATUS 0x1f | |
34 | #define AD7291_T_ALERT_STATUS 0x20 | |
35 | ||
36 | /* | |
37 | * AD7291 command | |
38 | */ | |
39 | #define AD7291_AUTOCYCLE 0x1 | |
40 | #define AD7291_RESET 0x2 | |
41 | #define AD7291_ALART_CLEAR 0x4 | |
42 | #define AD7291_ALART_POLARITY 0x8 | |
43 | #define AD7291_EXT_REF 0x10 | |
44 | #define AD7291_NOISE_DELAY 0x20 | |
45 | #define AD7291_T_SENSE_MASK 0x40 | |
46 | #define AD7291_VOLTAGE_MASK 0xff00 | |
47 | #define AD7291_VOLTAGE_OFFSET 0x8 | |
48 | ||
49 | /* | |
50 | * AD7291 value masks | |
51 | */ | |
52 | #define AD7291_CHANNEL_MASK 0xf000 | |
53 | #define AD7291_VALUE_MASK 0xfff | |
54 | #define AD7291_T_VALUE_SIGN 0x400 | |
55 | #define AD7291_T_VALUE_FLOAT_OFFSET 2 | |
56 | #define AD7291_T_VALUE_FLOAT_MASK 0x2 | |
57 | ||
58 | /* | |
59 | * struct ad7291_chip_info - chip specifc information | |
60 | */ | |
61 | ||
62 | struct ad7291_chip_info { | |
ddaecd5b SZ |
63 | struct i2c_client *client; |
64 | struct iio_dev *indio_dev; | |
ddaecd5b SZ |
65 | u16 command; |
66 | u8 channels; /* Active voltage channels */ | |
67 | }; | |
68 | ||
69 | /* | |
70 | * struct ad7291_chip_info - chip specifc information | |
71 | */ | |
72 | ||
73 | struct ad7291_limit_regs { | |
74 | u16 data_high; | |
75 | u16 data_low; | |
76 | u16 hysteresis; | |
77 | }; | |
78 | ||
79 | /* | |
80 | * ad7291 register access by I2C | |
81 | */ | |
82 | static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data) | |
83 | { | |
84 | struct i2c_client *client = chip->client; | |
85 | int ret = 0; | |
86 | ||
87 | ret = i2c_smbus_read_word_data(client, reg); | |
88 | if (ret < 0) { | |
89 | dev_err(&client->dev, "I2C read error\n"); | |
90 | return ret; | |
91 | } | |
92 | ||
93 | *data = swab16((u16)ret); | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data) | |
99 | { | |
100 | struct i2c_client *client = chip->client; | |
101 | int ret = 0; | |
102 | ||
103 | ret = i2c_smbus_write_word_data(client, reg, swab16(data)); | |
104 | if (ret < 0) | |
105 | dev_err(&client->dev, "I2C write error\n"); | |
106 | ||
107 | return ret; | |
108 | } | |
109 | ||
110 | /* Returns negative errno, or else the number of words read. */ | |
111 | static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data) | |
112 | { | |
113 | struct i2c_client *client = chip->client; | |
114 | u8 commands[4]; | |
115 | int ret = 0; | |
116 | int i, count; | |
117 | ||
118 | if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE) | |
119 | count = 2; | |
120 | else if (reg == AD7291_VOLTAGE) { | |
121 | if (!chip->channels) { | |
122 | dev_err(&client->dev, "No voltage channel is selected.\n"); | |
123 | return -EINVAL; | |
124 | } | |
125 | count = 2 + chip->channels * 2; | |
126 | } else { | |
127 | dev_err(&client->dev, "I2C wrong data register\n"); | |
128 | return -EINVAL; | |
129 | } | |
130 | ||
131 | commands[0] = 0; | |
132 | commands[1] = (chip->command >> 8) & 0xff; | |
133 | commands[2] = chip->command & 0xff; | |
134 | commands[3] = reg; | |
135 | ||
136 | ret = i2c_master_send(client, commands, 4); | |
137 | if (ret < 0) { | |
138 | dev_err(&client->dev, "I2C master send error\n"); | |
139 | return ret; | |
140 | } | |
141 | ||
142 | ret = i2c_master_recv(client, (u8 *)data, count); | |
143 | if (ret < 0) { | |
144 | dev_err(&client->dev, "I2C master receive error\n"); | |
145 | return ret; | |
146 | } | |
147 | ret >>= 2; | |
148 | ||
149 | for (i = 0; i < ret; i++) | |
150 | data[i] = swab16(data[i]); | |
151 | ||
152 | return ret; | |
153 | } | |
154 | ||
155 | static ssize_t ad7291_show_mode(struct device *dev, | |
156 | struct device_attribute *attr, | |
157 | char *buf) | |
158 | { | |
159 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
160 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
161 | ||
162 | if (chip->command & AD7291_AUTOCYCLE) | |
163 | return sprintf(buf, "autocycle\n"); | |
164 | else | |
165 | return sprintf(buf, "command\n"); | |
166 | } | |
167 | ||
168 | static ssize_t ad7291_store_mode(struct device *dev, | |
169 | struct device_attribute *attr, | |
170 | const char *buf, | |
171 | size_t len) | |
172 | { | |
173 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
174 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
175 | u16 command; | |
176 | int ret; | |
177 | ||
178 | command = chip->command & (~AD7291_AUTOCYCLE); | |
179 | if (strcmp(buf, "autocycle")) | |
180 | command |= AD7291_AUTOCYCLE; | |
181 | ||
182 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, command); | |
183 | if (ret) | |
184 | return -EIO; | |
185 | ||
186 | chip->command = command; | |
187 | ||
188 | return ret; | |
189 | } | |
190 | ||
191 | static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, | |
192 | ad7291_show_mode, | |
193 | ad7291_store_mode, | |
194 | 0); | |
195 | ||
196 | static ssize_t ad7291_show_available_modes(struct device *dev, | |
197 | struct device_attribute *attr, | |
198 | char *buf) | |
199 | { | |
200 | return sprintf(buf, "command\nautocycle\n"); | |
201 | } | |
202 | ||
203 | static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0); | |
204 | ||
205 | static ssize_t ad7291_store_reset(struct device *dev, | |
206 | struct device_attribute *attr, | |
207 | const char *buf, | |
208 | size_t len) | |
209 | { | |
210 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
211 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
212 | u16 command; | |
213 | int ret; | |
214 | ||
215 | command = chip->command | AD7291_RESET; | |
216 | ||
217 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, command); | |
218 | if (ret) | |
219 | return -EIO; | |
220 | ||
221 | return ret; | |
222 | } | |
223 | ||
224 | static IIO_DEVICE_ATTR(reset, S_IWUSR, | |
225 | NULL, | |
226 | ad7291_store_reset, | |
227 | 0); | |
228 | ||
229 | static ssize_t ad7291_show_ext_ref(struct device *dev, | |
230 | struct device_attribute *attr, | |
231 | char *buf) | |
232 | { | |
233 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
234 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
235 | ||
236 | return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF)); | |
237 | } | |
238 | ||
239 | static ssize_t ad7291_store_ext_ref(struct device *dev, | |
240 | struct device_attribute *attr, | |
241 | const char *buf, | |
242 | size_t len) | |
243 | { | |
244 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
245 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
246 | u16 command; | |
247 | int ret; | |
248 | ||
249 | command = chip->command & (~AD7291_EXT_REF); | |
250 | if (strcmp(buf, "1")) | |
251 | command |= AD7291_EXT_REF; | |
252 | ||
253 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, command); | |
254 | if (ret) | |
255 | return -EIO; | |
256 | ||
257 | chip->command = command; | |
258 | ||
259 | return ret; | |
260 | } | |
261 | ||
262 | static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR, | |
263 | ad7291_show_ext_ref, | |
264 | ad7291_store_ext_ref, | |
265 | 0); | |
266 | ||
267 | static ssize_t ad7291_show_noise_delay(struct device *dev, | |
268 | struct device_attribute *attr, | |
269 | char *buf) | |
270 | { | |
271 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
272 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
273 | ||
274 | return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY)); | |
275 | } | |
276 | ||
277 | static ssize_t ad7291_store_noise_delay(struct device *dev, | |
278 | struct device_attribute *attr, | |
279 | const char *buf, | |
280 | size_t len) | |
281 | { | |
282 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
283 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
284 | u16 command; | |
285 | int ret; | |
286 | ||
287 | command = chip->command & (~AD7291_NOISE_DELAY); | |
288 | if (strcmp(buf, "1")) | |
289 | command |= AD7291_NOISE_DELAY; | |
290 | ||
291 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, command); | |
292 | if (ret) | |
293 | return -EIO; | |
294 | ||
295 | chip->command = command; | |
296 | ||
297 | return ret; | |
298 | } | |
299 | ||
300 | static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR, | |
301 | ad7291_show_noise_delay, | |
302 | ad7291_store_noise_delay, | |
303 | 0); | |
304 | ||
305 | static ssize_t ad7291_show_t_sense(struct device *dev, | |
306 | struct device_attribute *attr, | |
307 | char *buf) | |
308 | { | |
309 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
310 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
311 | u16 data; | |
312 | char sign = ' '; | |
313 | int ret; | |
314 | ||
315 | ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data); | |
316 | if (ret) | |
317 | return -EIO; | |
318 | ||
319 | if (data & AD7291_T_VALUE_SIGN) { | |
320 | /* convert supplement to positive value */ | |
321 | data = (AD7291_T_VALUE_SIGN << 1) - data; | |
322 | sign = '-'; | |
323 | } | |
324 | ||
325 | return sprintf(buf, "%c%d.%.2d\n", sign, | |
326 | (data >> AD7291_T_VALUE_FLOAT_OFFSET), | |
327 | (data & AD7291_T_VALUE_FLOAT_MASK) * 25); | |
328 | } | |
329 | ||
330 | static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0); | |
331 | ||
332 | static ssize_t ad7291_show_t_average(struct device *dev, | |
333 | struct device_attribute *attr, | |
334 | char *buf) | |
335 | { | |
336 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
337 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
338 | u16 data; | |
339 | char sign = ' '; | |
340 | int ret; | |
341 | ||
342 | ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data); | |
343 | if (ret) | |
344 | return -EIO; | |
345 | ||
346 | if (data & AD7291_T_VALUE_SIGN) { | |
347 | /* convert supplement to positive value */ | |
348 | data = (AD7291_T_VALUE_SIGN << 1) - data; | |
349 | sign = '-'; | |
350 | } | |
351 | ||
352 | return sprintf(buf, "%c%d.%.2d\n", sign, | |
353 | (data >> AD7291_T_VALUE_FLOAT_OFFSET), | |
354 | (data & AD7291_T_VALUE_FLOAT_MASK) * 25); | |
355 | } | |
356 | ||
357 | static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0); | |
358 | ||
359 | static ssize_t ad7291_show_voltage(struct device *dev, | |
360 | struct device_attribute *attr, | |
361 | char *buf) | |
362 | { | |
363 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
364 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
365 | u16 data[AD7291_VOLTAGE_LIMIT_COUNT]; | |
366 | int i, size, ret; | |
367 | ||
368 | ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data); | |
369 | if (ret) | |
370 | return -EIO; | |
371 | ||
372 | for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) { | |
373 | if (chip->command & (AD7291_T_SENSE_MASK << i)) { | |
374 | ret = sprintf(buf, "channel[%d]=%d\n", i, | |
375 | data[i] & AD7291_VALUE_MASK); | |
376 | if (ret < 0) | |
377 | break; | |
378 | buf += ret; | |
379 | size += ret; | |
380 | } | |
381 | } | |
382 | ||
383 | return size; | |
384 | } | |
385 | ||
386 | static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0); | |
387 | ||
388 | static ssize_t ad7291_show_channel_mask(struct device *dev, | |
389 | struct device_attribute *attr, | |
390 | char *buf) | |
391 | { | |
392 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
393 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
394 | ||
395 | return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >> | |
396 | AD7291_VOLTAGE_OFFSET); | |
397 | } | |
398 | ||
399 | static ssize_t ad7291_store_channel_mask(struct device *dev, | |
400 | struct device_attribute *attr, | |
401 | const char *buf, | |
402 | size_t len) | |
403 | { | |
404 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
405 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
406 | u16 command; | |
407 | unsigned long data; | |
408 | int i, ret; | |
409 | ||
410 | ret = strict_strtoul(buf, 16, &data); | |
411 | if (ret || data > 0xff) | |
412 | return -EINVAL; | |
413 | ||
414 | command = chip->command & (~AD7291_VOLTAGE_MASK); | |
415 | command |= data << AD7291_VOLTAGE_OFFSET; | |
416 | ||
417 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, command); | |
418 | if (ret) | |
419 | return -EIO; | |
420 | ||
421 | chip->command = command; | |
422 | ||
423 | for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) { | |
424 | if (chip->command & (AD7291_T_SENSE_MASK << i)) | |
425 | chip->channels++; | |
426 | } | |
427 | ||
428 | return ret; | |
429 | } | |
430 | ||
431 | static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR, | |
432 | ad7291_show_channel_mask, | |
433 | ad7291_store_channel_mask, | |
434 | 0); | |
435 | ||
ddaecd5b SZ |
436 | static struct attribute *ad7291_attributes[] = { |
437 | &iio_dev_attr_available_modes.dev_attr.attr, | |
438 | &iio_dev_attr_mode.dev_attr.attr, | |
439 | &iio_dev_attr_reset.dev_attr.attr, | |
440 | &iio_dev_attr_ext_ref.dev_attr.attr, | |
441 | &iio_dev_attr_noise_delay.dev_attr.attr, | |
442 | &iio_dev_attr_t_sense.dev_attr.attr, | |
443 | &iio_dev_attr_t_average.dev_attr.attr, | |
444 | &iio_dev_attr_voltage.dev_attr.attr, | |
445 | &iio_dev_attr_channel_mask.dev_attr.attr, | |
ddaecd5b SZ |
446 | NULL, |
447 | }; | |
448 | ||
449 | static const struct attribute_group ad7291_attribute_group = { | |
450 | .attrs = ad7291_attributes, | |
451 | }; | |
452 | ||
453 | /* | |
454 | * temperature bound events | |
455 | */ | |
456 | ||
457 | #define IIO_EVENT_CODE_AD7291_T_SENSE_HIGH IIO_BUFFER_EVENT_CODE(0) | |
458 | #define IIO_EVENT_CODE_AD7291_T_SENSE_LOW IIO_BUFFER_EVENT_CODE(1) | |
459 | #define IIO_EVENT_CODE_AD7291_T_AVG_HIGH IIO_BUFFER_EVENT_CODE(2) | |
460 | #define IIO_EVENT_CODE_AD7291_T_AVG_LOW IIO_BUFFER_EVENT_CODE(3) | |
461 | #define IIO_EVENT_CODE_AD7291_VOLTAGE_BASE IIO_BUFFER_EVENT_CODE(4) | |
462 | ||
58c0323c | 463 | static irqreturn_t ad7291_event_handler(int irq, void *private) |
ddaecd5b | 464 | { |
58c0323c JC |
465 | struct iio_dev *indio_dev = private; |
466 | struct ad7291_chip_info *chip = iio_dev_get_devdata(private); | |
ddaecd5b SZ |
467 | u16 t_status, v_status; |
468 | u16 command; | |
469 | int i; | |
58c0323c | 470 | s64 timestamp = iio_get_time_ns(); |
ddaecd5b SZ |
471 | |
472 | if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status)) | |
58c0323c | 473 | return IRQ_HANDLED; |
ddaecd5b SZ |
474 | |
475 | if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status)) | |
58c0323c | 476 | return IRQ_HANDLED; |
ddaecd5b SZ |
477 | |
478 | if (!(t_status || v_status)) | |
58c0323c | 479 | return IRQ_HANDLED; |
ddaecd5b SZ |
480 | |
481 | command = chip->command | AD7291_ALART_CLEAR; | |
482 | ad7291_i2c_write(chip, AD7291_COMMAND, command); | |
483 | ||
484 | command = chip->command & ~AD7291_ALART_CLEAR; | |
485 | ad7291_i2c_write(chip, AD7291_COMMAND, command); | |
486 | ||
ddaecd5b SZ |
487 | for (i = 0; i < 4; i++) { |
488 | if (t_status & (1 << i)) | |
58c0323c | 489 | iio_push_event(indio_dev, 0, |
ddaecd5b | 490 | IIO_EVENT_CODE_AD7291_T_SENSE_HIGH + i, |
58c0323c | 491 | timestamp); |
ddaecd5b SZ |
492 | } |
493 | ||
494 | for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i++) { | |
495 | if (v_status & (1 << i)) | |
58c0323c | 496 | iio_push_event(indio_dev, 0, |
ddaecd5b | 497 | IIO_EVENT_CODE_AD7291_VOLTAGE_BASE + i, |
58c0323c | 498 | timestamp); |
ddaecd5b | 499 | } |
ddaecd5b | 500 | |
58c0323c | 501 | return IRQ_HANDLED; |
ddaecd5b SZ |
502 | } |
503 | ||
ddaecd5b SZ |
504 | static inline ssize_t ad7291_show_t_bound(struct device *dev, |
505 | struct device_attribute *attr, | |
ddaecd5b SZ |
506 | char *buf) |
507 | { | |
508 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
509 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
58c0323c | 510 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
ddaecd5b SZ |
511 | u16 data; |
512 | char sign = ' '; | |
513 | int ret; | |
514 | ||
58c0323c | 515 | ret = ad7291_i2c_read(chip, this_attr->address, &data); |
ddaecd5b SZ |
516 | if (ret) |
517 | return -EIO; | |
518 | ||
519 | data &= AD7291_VALUE_MASK; | |
520 | if (data & AD7291_T_VALUE_SIGN) { | |
521 | /* convert supplement to positive value */ | |
522 | data = (AD7291_T_VALUE_SIGN << 1) - data; | |
523 | sign = '-'; | |
524 | } | |
525 | ||
526 | return sprintf(buf, "%c%d.%.2d\n", sign, | |
527 | data >> AD7291_T_VALUE_FLOAT_OFFSET, | |
528 | (data & AD7291_T_VALUE_FLOAT_MASK) * 25); | |
529 | } | |
530 | ||
531 | static inline ssize_t ad7291_set_t_bound(struct device *dev, | |
532 | struct device_attribute *attr, | |
ddaecd5b SZ |
533 | const char *buf, |
534 | size_t len) | |
535 | { | |
536 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
537 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
58c0323c | 538 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
ddaecd5b SZ |
539 | long tmp1, tmp2; |
540 | u16 data; | |
541 | char *pos; | |
542 | int ret; | |
543 | ||
544 | pos = strchr(buf, '.'); | |
545 | ||
546 | ret = strict_strtol(buf, 10, &tmp1); | |
547 | ||
548 | if (ret || tmp1 > 127 || tmp1 < -128) | |
549 | return -EINVAL; | |
550 | ||
551 | if (pos) { | |
552 | len = strlen(pos); | |
553 | if (len > AD7291_T_VALUE_FLOAT_OFFSET) | |
554 | len = AD7291_T_VALUE_FLOAT_OFFSET; | |
555 | pos[len] = 0; | |
556 | ret = strict_strtol(pos, 10, &tmp2); | |
557 | ||
558 | if (!ret) | |
559 | tmp2 = (tmp2 / 25) * 25; | |
560 | } | |
561 | ||
562 | if (tmp1 < 0) | |
563 | data = (u16)(-tmp1); | |
564 | else | |
565 | data = (u16)tmp1; | |
566 | data = (data << AD7291_T_VALUE_FLOAT_OFFSET) | | |
567 | (tmp2 & AD7291_T_VALUE_FLOAT_MASK); | |
568 | if (tmp1 < 0) | |
569 | /* convert positive value to supplyment */ | |
570 | data = (AD7291_T_VALUE_SIGN << 1) - data; | |
571 | ||
58c0323c | 572 | ret = ad7291_i2c_write(chip, this_attr->address, data); |
ddaecd5b SZ |
573 | if (ret) |
574 | return -EIO; | |
575 | ||
576 | return ret; | |
577 | } | |
578 | ||
ddaecd5b SZ |
579 | static inline ssize_t ad7291_show_v_bound(struct device *dev, |
580 | struct device_attribute *attr, | |
581 | u8 bound_reg, | |
582 | char *buf) | |
583 | { | |
584 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
585 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
586 | u16 data; | |
587 | int ret; | |
588 | ||
589 | if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE || | |
590 | bound_reg >= AD7291_VOLTAGE_LIMIT_BASE + | |
591 | AD7291_VOLTAGE_LIMIT_COUNT) | |
592 | return -EINVAL; | |
593 | ||
594 | ret = ad7291_i2c_read(chip, bound_reg, &data); | |
595 | if (ret) | |
596 | return -EIO; | |
597 | ||
598 | data &= AD7291_VALUE_MASK; | |
599 | ||
600 | return sprintf(buf, "%d\n", data); | |
601 | } | |
602 | ||
603 | static inline ssize_t ad7291_set_v_bound(struct device *dev, | |
604 | struct device_attribute *attr, | |
605 | u8 bound_reg, | |
606 | const char *buf, | |
607 | size_t len) | |
608 | { | |
609 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
610 | struct ad7291_chip_info *chip = dev_info->dev_data; | |
611 | unsigned long value; | |
612 | u16 data; | |
613 | int ret; | |
614 | ||
615 | if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE || | |
616 | bound_reg >= AD7291_VOLTAGE_LIMIT_BASE + | |
617 | AD7291_VOLTAGE_LIMIT_COUNT) | |
618 | return -EINVAL; | |
619 | ||
620 | ret = strict_strtoul(buf, 10, &value); | |
621 | ||
622 | if (ret || value >= 4096) | |
623 | return -EINVAL; | |
624 | ||
625 | data = (u16)value; | |
626 | ret = ad7291_i2c_write(chip, bound_reg, data); | |
627 | if (ret) | |
628 | return -EIO; | |
629 | ||
630 | return ret; | |
631 | } | |
632 | ||
58c0323c JC |
633 | static IIO_DEVICE_ATTR(t_sense_high_value, |
634 | S_IRUGO | S_IWUSR, | |
635 | ad7291_show_t_bound, ad7291_set_t_bound, | |
636 | AD7291_T_SENSE_HIGH); | |
637 | static IIO_DEVICE_ATTR(t_sense_low_value, | |
638 | S_IRUGO | S_IWUSR, | |
639 | ad7291_show_t_bound, ad7291_set_t_bound, | |
640 | AD7291_T_SENSE_LOW); | |
641 | static IIO_DEVICE_ATTR(t_sense_hyst_value, | |
642 | S_IRUGO | S_IWUSR, | |
643 | ad7291_show_t_bound, ad7291_set_t_bound, | |
644 | AD7291_T_SENSE_HYST); | |
645 | static IIO_DEVICE_ATTR(v0_high, | |
646 | S_IRUGO | S_IWUSR, | |
647 | ad7291_show_t_bound, ad7291_set_t_bound, 0x04); | |
648 | static IIO_DEVICE_ATTR(v0_low, | |
649 | S_IRUGO | S_IWUSR, | |
650 | ad7291_show_t_bound, ad7291_set_t_bound, 0x05); | |
651 | static IIO_DEVICE_ATTR(v0_hyst, | |
652 | S_IRUGO | S_IWUSR, | |
653 | ad7291_show_t_bound, ad7291_set_t_bound, 0x06); | |
654 | static IIO_DEVICE_ATTR(v1_high, | |
655 | S_IRUGO | S_IWUSR, | |
656 | ad7291_show_t_bound, ad7291_set_t_bound, 0x07); | |
657 | static IIO_DEVICE_ATTR(v1_low, | |
658 | S_IRUGO | S_IWUSR, | |
659 | ad7291_show_t_bound, ad7291_set_t_bound, 0x08); | |
660 | static IIO_DEVICE_ATTR(v1_hyst, | |
661 | S_IRUGO | S_IWUSR, | |
662 | ad7291_show_t_bound, ad7291_set_t_bound, 0x09); | |
663 | static IIO_DEVICE_ATTR(v2_high, | |
664 | S_IRUGO | S_IWUSR, | |
665 | ad7291_show_t_bound, ad7291_set_t_bound, 0x0A); | |
666 | static IIO_DEVICE_ATTR(v2_low, | |
667 | S_IRUGO | S_IWUSR, | |
668 | ad7291_show_t_bound, ad7291_set_t_bound, 0x0B); | |
669 | static IIO_DEVICE_ATTR(v2_hyst, | |
670 | S_IRUGO | S_IWUSR, | |
671 | ad7291_show_t_bound, ad7291_set_t_bound, 0x0C); | |
672 | static IIO_DEVICE_ATTR(v3_high, | |
673 | S_IRUGO | S_IWUSR, | |
674 | /* Datasheet suggests this one and this one only | |
675 | has the registers in different order */ | |
676 | ad7291_show_t_bound, ad7291_set_t_bound, 0x0E); | |
677 | static IIO_DEVICE_ATTR(v3_low, | |
678 | S_IRUGO | S_IWUSR, | |
679 | ad7291_show_t_bound, ad7291_set_t_bound, 0x0D); | |
680 | static IIO_DEVICE_ATTR(v3_hyst, | |
681 | S_IRUGO | S_IWUSR, | |
682 | ad7291_show_t_bound, ad7291_set_t_bound, 0x0F); | |
683 | static IIO_DEVICE_ATTR(v4_high, | |
684 | S_IRUGO | S_IWUSR, | |
685 | ad7291_show_t_bound, ad7291_set_t_bound, 0x10); | |
686 | static IIO_DEVICE_ATTR(v4_low, | |
687 | S_IRUGO | S_IWUSR, | |
688 | ad7291_show_t_bound, ad7291_set_t_bound, 0x11); | |
689 | static IIO_DEVICE_ATTR(v4_hyst, | |
690 | S_IRUGO | S_IWUSR, | |
691 | ad7291_show_t_bound, ad7291_set_t_bound, 0x12); | |
692 | static IIO_DEVICE_ATTR(v5_high, | |
693 | S_IRUGO | S_IWUSR, | |
694 | ad7291_show_t_bound, ad7291_set_t_bound, 0x13); | |
695 | static IIO_DEVICE_ATTR(v5_low, | |
696 | S_IRUGO | S_IWUSR, | |
697 | ad7291_show_t_bound, ad7291_set_t_bound, 0x14); | |
698 | static IIO_DEVICE_ATTR(v5_hyst, | |
699 | S_IRUGO | S_IWUSR, | |
700 | ad7291_show_t_bound, ad7291_set_t_bound, 0x15); | |
701 | static IIO_DEVICE_ATTR(v6_high, | |
702 | S_IRUGO | S_IWUSR, | |
703 | ad7291_show_t_bound, ad7291_set_t_bound, 0x16); | |
704 | static IIO_DEVICE_ATTR(v6_low, | |
705 | S_IRUGO | S_IWUSR, | |
706 | ad7291_show_t_bound, ad7291_set_t_bound, 0x17); | |
707 | static IIO_DEVICE_ATTR(v6_hyst, | |
708 | S_IRUGO | S_IWUSR, | |
709 | ad7291_show_t_bound, ad7291_set_t_bound, 0x18); | |
710 | static IIO_DEVICE_ATTR(v7_high, | |
711 | S_IRUGO | S_IWUSR, | |
712 | ad7291_show_t_bound, ad7291_set_t_bound, 0x19); | |
713 | static IIO_DEVICE_ATTR(v7_low, | |
714 | S_IRUGO | S_IWUSR, | |
715 | ad7291_show_t_bound, ad7291_set_t_bound, 0x1A); | |
716 | static IIO_DEVICE_ATTR(v7_hyst, | |
717 | S_IRUGO | S_IWUSR, | |
718 | ad7291_show_t_bound, ad7291_set_t_bound, 0x1B); | |
ddaecd5b SZ |
719 | |
720 | static struct attribute *ad7291_event_attributes[] = { | |
58c0323c JC |
721 | &iio_dev_attr_t_sense_high_value.dev_attr.attr, |
722 | &iio_dev_attr_t_sense_low_value.dev_attr.attr, | |
723 | &iio_dev_attr_t_sense_hyst_value.dev_attr.attr, | |
724 | &iio_dev_attr_v0_high.dev_attr.attr, | |
725 | &iio_dev_attr_v0_low.dev_attr.attr, | |
726 | &iio_dev_attr_v0_hyst.dev_attr.attr, | |
727 | &iio_dev_attr_v1_high.dev_attr.attr, | |
728 | &iio_dev_attr_v1_low.dev_attr.attr, | |
729 | &iio_dev_attr_v1_hyst.dev_attr.attr, | |
730 | &iio_dev_attr_v2_high.dev_attr.attr, | |
731 | &iio_dev_attr_v2_low.dev_attr.attr, | |
732 | &iio_dev_attr_v2_hyst.dev_attr.attr, | |
733 | &iio_dev_attr_v3_high.dev_attr.attr, | |
734 | &iio_dev_attr_v3_low.dev_attr.attr, | |
735 | &iio_dev_attr_v3_hyst.dev_attr.attr, | |
736 | &iio_dev_attr_v4_high.dev_attr.attr, | |
737 | &iio_dev_attr_v4_low.dev_attr.attr, | |
738 | &iio_dev_attr_v4_hyst.dev_attr.attr, | |
739 | &iio_dev_attr_v5_high.dev_attr.attr, | |
740 | &iio_dev_attr_v5_low.dev_attr.attr, | |
741 | &iio_dev_attr_v5_hyst.dev_attr.attr, | |
742 | &iio_dev_attr_v6_high.dev_attr.attr, | |
743 | &iio_dev_attr_v6_low.dev_attr.attr, | |
744 | &iio_dev_attr_v6_hyst.dev_attr.attr, | |
745 | &iio_dev_attr_v7_high.dev_attr.attr, | |
746 | &iio_dev_attr_v7_low.dev_attr.attr, | |
747 | &iio_dev_attr_v7_hyst.dev_attr.attr, | |
ddaecd5b SZ |
748 | NULL, |
749 | }; | |
750 | ||
751 | static struct attribute_group ad7291_event_attribute_group = { | |
752 | .attrs = ad7291_event_attributes, | |
753 | }; | |
754 | ||
755 | /* | |
756 | * device probe and remove | |
757 | */ | |
758 | ||
759 | static int __devinit ad7291_probe(struct i2c_client *client, | |
760 | const struct i2c_device_id *id) | |
761 | { | |
762 | struct ad7291_chip_info *chip; | |
763 | int ret = 0; | |
764 | ||
765 | chip = kzalloc(sizeof(struct ad7291_chip_info), GFP_KERNEL); | |
766 | ||
767 | if (chip == NULL) | |
768 | return -ENOMEM; | |
769 | ||
770 | /* this is only used for device removal purposes */ | |
771 | i2c_set_clientdata(client, chip); | |
772 | ||
773 | chip->client = client; | |
ddaecd5b SZ |
774 | chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK; |
775 | ||
6f7c8ee5 | 776 | chip->indio_dev = iio_allocate_device(0); |
ddaecd5b SZ |
777 | if (chip->indio_dev == NULL) { |
778 | ret = -ENOMEM; | |
779 | goto error_free_chip; | |
780 | } | |
781 | ||
845bd12a | 782 | chip->indio_dev->name = id->name; |
ddaecd5b SZ |
783 | chip->indio_dev->dev.parent = &client->dev; |
784 | chip->indio_dev->attrs = &ad7291_attribute_group; | |
785 | chip->indio_dev->event_attrs = &ad7291_event_attribute_group; | |
786 | chip->indio_dev->dev_data = (void *)chip; | |
787 | chip->indio_dev->driver_module = THIS_MODULE; | |
788 | chip->indio_dev->num_interrupt_lines = 1; | |
789 | chip->indio_dev->modes = INDIO_DIRECT_MODE; | |
790 | ||
791 | ret = iio_device_register(chip->indio_dev); | |
792 | if (ret) | |
793 | goto error_free_dev; | |
794 | ||
795 | if (client->irq > 0) { | |
58c0323c JC |
796 | ret = request_threaded_irq(client->irq, |
797 | NULL, | |
798 | &ad7291_event_handler, | |
799 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, | |
845bd12a | 800 | id->name, |
58c0323c | 801 | chip->indio_dev); |
ddaecd5b SZ |
802 | if (ret) |
803 | goto error_unreg_dev; | |
804 | ||
ddaecd5b SZ |
805 | /* set irq polarity low level */ |
806 | chip->command |= AD7291_ALART_POLARITY; | |
807 | } | |
808 | ||
809 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command); | |
810 | if (ret) { | |
811 | ret = -EIO; | |
812 | goto error_unreg_irq; | |
813 | } | |
814 | ||
815 | dev_info(&client->dev, "%s temperature sensor registered.\n", | |
816 | id->name); | |
817 | ||
818 | return 0; | |
819 | ||
820 | error_unreg_irq: | |
58c0323c | 821 | free_irq(client->irq, chip->indio_dev); |
ddaecd5b SZ |
822 | error_unreg_dev: |
823 | iio_device_unregister(chip->indio_dev); | |
824 | error_free_dev: | |
825 | iio_free_device(chip->indio_dev); | |
826 | error_free_chip: | |
827 | kfree(chip); | |
828 | ||
829 | return ret; | |
830 | } | |
831 | ||
832 | static int __devexit ad7291_remove(struct i2c_client *client) | |
833 | { | |
834 | struct ad7291_chip_info *chip = i2c_get_clientdata(client); | |
835 | struct iio_dev *indio_dev = chip->indio_dev; | |
836 | ||
837 | if (client->irq) | |
58c0323c | 838 | free_irq(client->irq, chip->indio_dev); |
ddaecd5b SZ |
839 | iio_device_unregister(indio_dev); |
840 | iio_free_device(chip->indio_dev); | |
841 | kfree(chip); | |
842 | ||
843 | return 0; | |
844 | } | |
845 | ||
846 | static const struct i2c_device_id ad7291_id[] = { | |
847 | { "ad7291", 0 }, | |
848 | {} | |
849 | }; | |
850 | ||
851 | MODULE_DEVICE_TABLE(i2c, ad7291_id); | |
852 | ||
853 | static struct i2c_driver ad7291_driver = { | |
854 | .driver = { | |
855 | .name = "ad7291", | |
856 | }, | |
857 | .probe = ad7291_probe, | |
858 | .remove = __devexit_p(ad7291_remove), | |
859 | .id_table = ad7291_id, | |
860 | }; | |
861 | ||
862 | static __init int ad7291_init(void) | |
863 | { | |
864 | return i2c_add_driver(&ad7291_driver); | |
865 | } | |
866 | ||
867 | static __exit void ad7291_exit(void) | |
868 | { | |
869 | i2c_del_driver(&ad7291_driver); | |
870 | } | |
871 | ||
872 | MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); | |
873 | MODULE_DESCRIPTION("Analog Devices AD7291 digital" | |
874 | " temperature sensor driver"); | |
875 | MODULE_LICENSE("GPL v2"); | |
876 | ||
877 | module_init(ad7291_init); | |
878 | module_exit(ad7291_exit); |