Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / adc / adt75.c
blob38f141de6a4be018054dfa424126df05c4232761
1 /*
2 * ADT75 digital temperature sensor driver supporting ADT75
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
16 #include "../iio.h"
17 #include "../sysfs.h"
20 * ADT75 registers definition
23 #define ADT75_TEMPERATURE 0
24 #define ADT75_CONFIG 1
25 #define ADT75_T_HYST 2
26 #define ADT75_T_OS 3
27 #define ADT75_ONESHOT 4
30 * ADT75 config
32 #define ADT75_PD 0x1
33 #define ADT75_OS_INT 0x2
34 #define ADT75_OS_POLARITY 0x4
35 #define ADT75_FAULT_QUEUE_MASK 0x18
36 #define ADT75_FAULT_QUEUE_OFFSET 3
37 #define ADT75_SMBUS_ALART 0x8
40 * ADT75 masks
42 #define ADT75_VALUE_SIGN 0x800
43 #define ADT75_VALUE_OFFSET 4
44 #define ADT75_VALUE_FLOAT_OFFSET 4
45 #define ADT75_VALUE_FLOAT_MASK 0xF
49 * struct adt75_chip_info - chip specifc information
52 struct adt75_chip_info {
53 struct i2c_client *client;
54 u8 config;
58 * adt75 register access by I2C
61 static int adt75_i2c_read(struct iio_dev *dev_info, u8 reg, u8 *data)
63 struct adt75_chip_info *chip = iio_priv(dev_info);
64 struct i2c_client *client = chip->client;
65 int ret = 0, len;
67 ret = i2c_smbus_write_byte(client, reg);
68 if (ret < 0) {
69 dev_err(&client->dev, "I2C read register address error\n");
70 return ret;
73 if (reg == ADT75_CONFIG || reg == ADT75_ONESHOT)
74 len = 1;
75 else
76 len = 2;
78 ret = i2c_master_recv(client, data, len);
79 if (ret < 0) {
80 dev_err(&client->dev, "I2C read error\n");
81 return ret;
84 return ret;
87 static int adt75_i2c_write(struct iio_dev *dev_info, u8 reg, u8 data)
89 struct adt75_chip_info *chip = iio_priv(dev_info);
90 struct i2c_client *client = chip->client;
91 int ret = 0;
93 if (reg == ADT75_CONFIG || reg == ADT75_ONESHOT)
94 ret = i2c_smbus_write_byte_data(client, reg, data);
95 else
96 ret = i2c_smbus_write_word_data(client, reg, data);
98 if (ret < 0)
99 dev_err(&client->dev, "I2C write error\n");
101 return ret;
104 static ssize_t adt75_show_mode(struct device *dev,
105 struct device_attribute *attr,
106 char *buf)
108 struct adt75_chip_info *chip = iio_priv(dev_get_drvdata(dev));
110 if (chip->config & ADT75_PD)
111 return sprintf(buf, "power-save\n");
112 else
113 return sprintf(buf, "full\n");
116 static ssize_t adt75_store_mode(struct device *dev,
117 struct device_attribute *attr,
118 const char *buf,
119 size_t len)
121 struct iio_dev *dev_info = dev_get_drvdata(dev);
122 struct adt75_chip_info *chip = iio_priv(dev_info);
123 int ret;
124 u8 config;
126 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
127 if (ret)
128 return -EIO;
130 config = chip->config & ~ADT75_PD;
131 if (!strcmp(buf, "full"))
132 config |= ADT75_PD;
134 ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
135 if (ret)
136 return -EIO;
138 chip->config = config;
140 return ret;
143 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
144 adt75_show_mode,
145 adt75_store_mode,
148 static ssize_t adt75_show_available_modes(struct device *dev,
149 struct device_attribute *attr,
150 char *buf)
152 return sprintf(buf, "full\npower-down\n");
155 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt75_show_available_modes, NULL, 0);
157 static ssize_t adt75_show_oneshot(struct device *dev,
158 struct device_attribute *attr,
159 char *buf)
161 struct adt75_chip_info *chip = iio_priv(dev_get_drvdata(dev));
163 return sprintf(buf, "%d\n", !!(chip->config & ADT75_ONESHOT));
166 static ssize_t adt75_store_oneshot(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf,
169 size_t len)
171 struct iio_dev *dev_info = dev_get_drvdata(dev);
172 struct adt75_chip_info *chip = iio_priv(dev_info);
173 unsigned long data = 0;
174 int ret;
175 u8 config;
177 ret = strict_strtoul(buf, 10, &data);
178 if (ret)
179 return -EINVAL;
182 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
183 if (ret)
184 return -EIO;
186 config = chip->config & ~ADT75_ONESHOT;
187 if (data)
188 config |= ADT75_ONESHOT;
190 ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
191 if (ret)
192 return -EIO;
194 chip->config = config;
196 return ret;
199 static IIO_DEVICE_ATTR(oneshot, S_IRUGO | S_IWUSR,
200 adt75_show_oneshot,
201 adt75_store_oneshot,
204 static ssize_t adt75_show_value(struct device *dev,
205 struct device_attribute *attr,
206 char *buf)
208 struct iio_dev *dev_info = dev_get_drvdata(dev);
209 struct adt75_chip_info *chip = iio_priv(dev_info);
210 u16 data;
211 char sign = ' ';
212 int ret;
214 if (chip->config & ADT75_PD) {
215 dev_err(dev, "Can't read value in power-down mode.\n");
216 return -EIO;
219 if (chip->config & ADT75_ONESHOT) {
220 /* write to active converter */
221 ret = i2c_smbus_write_byte(chip->client, ADT75_ONESHOT);
222 if (ret)
223 return -EIO;
226 ret = adt75_i2c_read(dev_info, ADT75_TEMPERATURE, (u8 *)&data);
227 if (ret)
228 return -EIO;
230 data = swab16(data) >> ADT75_VALUE_OFFSET;
231 if (data & ADT75_VALUE_SIGN) {
232 /* convert supplement to positive value */
233 data = (ADT75_VALUE_SIGN << 1) - data;
234 sign = '-';
237 return sprintf(buf, "%c%d.%.4d\n", sign,
238 (data >> ADT75_VALUE_FLOAT_OFFSET),
239 (data & ADT75_VALUE_FLOAT_MASK) * 625);
242 static IIO_DEVICE_ATTR(value, S_IRUGO, adt75_show_value, NULL, 0);
244 static struct attribute *adt75_attributes[] = {
245 &iio_dev_attr_available_modes.dev_attr.attr,
246 &iio_dev_attr_mode.dev_attr.attr,
247 &iio_dev_attr_oneshot.dev_attr.attr,
248 &iio_dev_attr_value.dev_attr.attr,
249 NULL,
252 static const struct attribute_group adt75_attribute_group = {
253 .attrs = adt75_attributes,
257 * temperature bound events
260 #define IIO_EVENT_CODE_ADT75_OTI IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_TEMP, \
261 0, \
262 IIO_EV_TYPE_THRESH, \
263 IIO_EV_DIR_FALLING)
265 static irqreturn_t adt75_event_handler(int irq, void *private)
267 iio_push_event(private, 0,
268 IIO_EVENT_CODE_ADT75_OTI,
269 iio_get_time_ns());
271 return IRQ_HANDLED;
274 static ssize_t adt75_show_oti_mode(struct device *dev,
275 struct device_attribute *attr,
276 char *buf)
278 struct iio_dev *dev_info = dev_get_drvdata(dev);
279 struct adt75_chip_info *chip = iio_priv(dev_info);
280 int ret;
282 /* retrive ALART status */
283 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
284 if (ret)
285 return -EIO;
287 if (chip->config & ADT75_OS_INT)
288 return sprintf(buf, "interrupt\n");
289 else
290 return sprintf(buf, "comparator\n");
293 static ssize_t adt75_set_oti_mode(struct device *dev,
294 struct device_attribute *attr,
295 const char *buf,
296 size_t len)
298 struct iio_dev *dev_info = dev_get_drvdata(dev);
299 struct adt75_chip_info *chip = iio_priv(dev_info);
300 int ret;
301 u8 config;
303 /* retrive ALART status */
304 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
305 if (ret)
306 return -EIO;
308 config = chip->config & ~ADT75_OS_INT;
309 if (strcmp(buf, "comparator") != 0)
310 config |= ADT75_OS_INT;
312 ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
313 if (ret)
314 return -EIO;
316 chip->config = config;
318 return ret;
321 static ssize_t adt75_show_available_oti_modes(struct device *dev,
322 struct device_attribute *attr,
323 char *buf)
325 return sprintf(buf, "comparator\ninterrupt\n");
328 static ssize_t adt75_show_smbus_alart(struct device *dev,
329 struct device_attribute *attr,
330 char *buf)
332 struct iio_dev *dev_info = dev_get_drvdata(dev);
333 struct adt75_chip_info *chip = iio_priv(dev_info);
334 int ret;
336 /* retrive ALART status */
337 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
338 if (ret)
339 return -EIO;
341 return sprintf(buf, "%d\n", !!(chip->config & ADT75_SMBUS_ALART));
344 static ssize_t adt75_set_smbus_alart(struct device *dev,
345 struct device_attribute *attr,
346 const char *buf,
347 size_t len)
349 struct iio_dev *dev_info = dev_get_drvdata(dev);
350 struct adt75_chip_info *chip = iio_priv(dev_info);
351 unsigned long data = 0;
352 int ret;
353 u8 config;
355 ret = strict_strtoul(buf, 10, &data);
356 if (ret)
357 return -EINVAL;
359 /* retrive ALART status */
360 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
361 if (ret)
362 return -EIO;
364 config = chip->config & ~ADT75_SMBUS_ALART;
365 if (data)
366 config |= ADT75_SMBUS_ALART;
368 ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
369 if (ret)
370 return -EIO;
372 chip->config = config;
374 return ret;
377 static ssize_t adt75_show_fault_queue(struct device *dev,
378 struct device_attribute *attr,
379 char *buf)
381 struct iio_dev *dev_info = dev_get_drvdata(dev);
382 struct adt75_chip_info *chip = iio_priv(dev_info);
383 int ret;
385 /* retrive ALART status */
386 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
387 if (ret)
388 return -EIO;
390 return sprintf(buf, "%d\n", (chip->config & ADT75_FAULT_QUEUE_MASK) >>
391 ADT75_FAULT_QUEUE_OFFSET);
394 static ssize_t adt75_set_fault_queue(struct device *dev,
395 struct device_attribute *attr,
396 const char *buf,
397 size_t len)
399 struct iio_dev *dev_info = dev_get_drvdata(dev);
400 struct adt75_chip_info *chip = iio_priv(dev_info);
401 unsigned long data;
402 int ret;
403 u8 config;
405 ret = strict_strtoul(buf, 10, &data);
406 if (ret || data > 3)
407 return -EINVAL;
409 /* retrive ALART status */
410 ret = adt75_i2c_read(dev_info, ADT75_CONFIG, &chip->config);
411 if (ret)
412 return -EIO;
414 config = chip->config & ~ADT75_FAULT_QUEUE_MASK;
415 config |= (data << ADT75_FAULT_QUEUE_OFFSET);
416 ret = adt75_i2c_write(dev_info, ADT75_CONFIG, config);
417 if (ret)
418 return -EIO;
420 chip->config = config;
422 return ret;
424 static inline ssize_t adt75_show_t_bound(struct device *dev,
425 struct device_attribute *attr,
426 char *buf)
428 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
429 struct iio_dev *dev_info = dev_get_drvdata(dev);
430 u16 data;
431 char sign = ' ';
432 int ret;
434 ret = adt75_i2c_read(dev_info, this_attr->address, (u8 *)&data);
435 if (ret)
436 return -EIO;
438 data = swab16(data) >> ADT75_VALUE_OFFSET;
439 if (data & ADT75_VALUE_SIGN) {
440 /* convert supplement to positive value */
441 data = (ADT75_VALUE_SIGN << 1) - data;
442 sign = '-';
445 return sprintf(buf, "%c%d.%.4d\n", sign,
446 (data >> ADT75_VALUE_FLOAT_OFFSET),
447 (data & ADT75_VALUE_FLOAT_MASK) * 625);
450 static inline ssize_t adt75_set_t_bound(struct device *dev,
451 struct device_attribute *attr,
452 const char *buf,
453 size_t len)
455 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
456 struct iio_dev *dev_info = dev_get_drvdata(dev);
457 long tmp1, tmp2;
458 u16 data;
459 char *pos;
460 int ret;
462 pos = strchr(buf, '.');
464 ret = strict_strtol(buf, 10, &tmp1);
466 if (ret || tmp1 > 127 || tmp1 < -128)
467 return -EINVAL;
469 if (pos) {
470 len = strlen(pos);
471 if (len > ADT75_VALUE_FLOAT_OFFSET)
472 len = ADT75_VALUE_FLOAT_OFFSET;
473 pos[len] = 0;
474 ret = strict_strtol(pos, 10, &tmp2);
476 if (!ret)
477 tmp2 = (tmp2 / 625) * 625;
480 if (tmp1 < 0)
481 data = (u16)(-tmp1);
482 else
483 data = (u16)tmp1;
484 data = (data << ADT75_VALUE_FLOAT_OFFSET) | (tmp2 & ADT75_VALUE_FLOAT_MASK);
485 if (tmp1 < 0)
486 /* convert positive value to supplyment */
487 data = (ADT75_VALUE_SIGN << 1) - data;
488 data <<= ADT75_VALUE_OFFSET;
489 data = swab16(data);
491 ret = adt75_i2c_write(dev_info, this_attr->address, (u8)data);
492 if (ret)
493 return -EIO;
495 return ret;
499 static IIO_DEVICE_ATTR(oti_mode,
500 S_IRUGO | S_IWUSR,
501 adt75_show_oti_mode, adt75_set_oti_mode, 0);
502 static IIO_DEVICE_ATTR(available_oti_modes,
503 S_IRUGO,
504 adt75_show_available_oti_modes, NULL, 0);
505 static IIO_DEVICE_ATTR(smbus_alart,
506 S_IRUGO | S_IWUSR,
507 adt75_show_smbus_alart, adt75_set_smbus_alart, 0);
508 static IIO_DEVICE_ATTR(fault_queue,
509 S_IRUGO | S_IWUSR,
510 adt75_show_fault_queue, adt75_set_fault_queue, 0);
511 static IIO_DEVICE_ATTR(t_os_value,
512 S_IRUGO | S_IWUSR,
513 adt75_show_t_bound, adt75_set_t_bound,
514 ADT75_T_OS);
515 static IIO_DEVICE_ATTR(t_hyst_value,
516 S_IRUGO | S_IWUSR,
517 adt75_show_t_bound, adt75_set_t_bound,
518 ADT75_T_HYST);
520 static struct attribute *adt75_event_attributes[] = {
521 &iio_dev_attr_oti_mode.dev_attr.attr,
522 &iio_dev_attr_available_oti_modes.dev_attr.attr,
523 &iio_dev_attr_smbus_alart.dev_attr.attr,
524 &iio_dev_attr_fault_queue.dev_attr.attr,
525 &iio_dev_attr_t_os_value.dev_attr.attr,
526 &iio_dev_attr_t_hyst_value.dev_attr.attr,
527 NULL,
530 static struct attribute_group adt75_event_attribute_group = {
531 .attrs = adt75_event_attributes,
534 static const struct iio_info adt75_info = {
535 .attrs = &adt75_attribute_group,
536 .num_interrupt_lines = 1,
537 .event_attrs = &adt75_event_attribute_group,
538 .driver_module = THIS_MODULE,
542 * device probe and remove
545 static int __devinit adt75_probe(struct i2c_client *client,
546 const struct i2c_device_id *id)
548 struct adt75_chip_info *chip;
549 struct iio_dev *indio_dev;
550 int ret = 0;
552 indio_dev = iio_allocate_device(sizeof(*chip));
553 if (indio_dev == NULL) {
554 ret = -ENOMEM;
555 goto error_ret;
557 chip = iio_priv(indio_dev);
559 /* this is only used for device removal purposes */
560 i2c_set_clientdata(client, indio_dev);
562 chip->client = client;
564 indio_dev->name = id->name;
565 indio_dev->dev.parent = &client->dev;
566 indio_dev->info = &adt75_info;
567 indio_dev->modes = INDIO_DIRECT_MODE;
569 ret = iio_device_register(indio_dev);
570 if (ret)
571 goto error_free_dev;
573 if (client->irq > 0) {
574 ret = request_threaded_irq(client->irq,
575 NULL,
576 &adt75_event_handler,
577 IRQF_TRIGGER_LOW,
578 indio_dev->name,
579 indio_dev);
580 if (ret)
581 goto error_unreg_dev;
583 ret = adt75_i2c_read(indio_dev, ADT75_CONFIG, &chip->config);
584 if (ret) {
585 ret = -EIO;
586 goto error_unreg_irq;
589 /* set irq polarity low level */
590 chip->config &= ~ADT75_OS_POLARITY;
592 ret = adt75_i2c_write(indio_dev, ADT75_CONFIG, chip->config);
593 if (ret) {
594 ret = -EIO;
595 goto error_unreg_irq;
599 dev_info(&client->dev, "%s temperature sensor registered.\n",
600 indio_dev->name);
602 return 0;
603 error_unreg_irq:
604 free_irq(client->irq, indio_dev);
605 error_unreg_dev:
606 iio_device_unregister(indio_dev);
607 error_free_dev:
608 iio_free_device(indio_dev);
609 error_ret:
610 return ret;
613 static int __devexit adt75_remove(struct i2c_client *client)
615 struct iio_dev *indio_dev = i2c_get_clientdata(client);
617 if (client->irq)
618 free_irq(client->irq, indio_dev);
619 iio_device_unregister(indio_dev);
620 iio_free_device(indio_dev);
622 return 0;
625 static const struct i2c_device_id adt75_id[] = {
626 { "adt75", 0 },
630 MODULE_DEVICE_TABLE(i2c, adt75_id);
632 static struct i2c_driver adt75_driver = {
633 .driver = {
634 .name = "adt75",
636 .probe = adt75_probe,
637 .remove = __devexit_p(adt75_remove),
638 .id_table = adt75_id,
641 static __init int adt75_init(void)
643 return i2c_add_driver(&adt75_driver);
646 static __exit void adt75_exit(void)
648 i2c_del_driver(&adt75_driver);
651 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
652 MODULE_DESCRIPTION("Analog Devices ADT75 digital"
653 " temperature sensor driver");
654 MODULE_LICENSE("GPL v2");
656 module_init(adt75_init);
657 module_exit(adt75_exit);