2 * ADT75 digital temperature sensor driver supporting ADT75
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
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>
20 * ADT75 registers definition
23 #define ADT75_TEMPERATURE 0
24 #define ADT75_CONFIG 1
25 #define ADT75_T_HYST 2
27 #define ADT75_ONESHOT 4
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
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
;
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
;
67 ret
= i2c_smbus_write_byte(client
, reg
);
69 dev_err(&client
->dev
, "I2C read register address error\n");
73 if (reg
== ADT75_CONFIG
|| reg
== ADT75_ONESHOT
)
78 ret
= i2c_master_recv(client
, data
, len
);
80 dev_err(&client
->dev
, "I2C read error\n");
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
;
93 if (reg
== ADT75_CONFIG
|| reg
== ADT75_ONESHOT
)
94 ret
= i2c_smbus_write_byte_data(client
, reg
, data
);
96 ret
= i2c_smbus_write_word_data(client
, reg
, data
);
99 dev_err(&client
->dev
, "I2C write error\n");
104 static ssize_t
adt75_show_mode(struct device
*dev
,
105 struct device_attribute
*attr
,
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");
113 return sprintf(buf
, "full\n");
116 static ssize_t
adt75_store_mode(struct device
*dev
,
117 struct device_attribute
*attr
,
121 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
122 struct adt75_chip_info
*chip
= iio_priv(dev_info
);
126 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
130 config
= chip
->config
& ~ADT75_PD
;
131 if (!strcmp(buf
, "full"))
134 ret
= adt75_i2c_write(dev_info
, ADT75_CONFIG
, config
);
138 chip
->config
= config
;
143 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
148 static ssize_t
adt75_show_available_modes(struct device
*dev
,
149 struct device_attribute
*attr
,
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
,
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
,
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;
177 ret
= strict_strtoul(buf
, 10, &data
);
182 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
186 config
= chip
->config
& ~ADT75_ONESHOT
;
188 config
|= ADT75_ONESHOT
;
190 ret
= adt75_i2c_write(dev_info
, ADT75_CONFIG
, config
);
194 chip
->config
= config
;
199 static IIO_DEVICE_ATTR(oneshot
, S_IRUGO
| S_IWUSR
,
204 static ssize_t
adt75_show_value(struct device
*dev
,
205 struct device_attribute
*attr
,
208 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
209 struct adt75_chip_info
*chip
= iio_priv(dev_info
);
214 if (chip
->config
& ADT75_PD
) {
215 dev_err(dev
, "Can't read value in power-down mode.\n");
219 if (chip
->config
& ADT75_ONESHOT
) {
220 /* write to active converter */
221 ret
= i2c_smbus_write_byte(chip
->client
, ADT75_ONESHOT
);
226 ret
= adt75_i2c_read(dev_info
, ADT75_TEMPERATURE
, (u8
*)&data
);
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
;
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
,
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, \
262 IIO_EV_TYPE_THRESH, \
265 static irqreturn_t
adt75_event_handler(int irq
, void *private)
267 iio_push_event(private, 0,
268 IIO_EVENT_CODE_ADT75_OTI
,
274 static ssize_t
adt75_show_oti_mode(struct device
*dev
,
275 struct device_attribute
*attr
,
278 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
279 struct adt75_chip_info
*chip
= iio_priv(dev_info
);
282 /* retrive ALART status */
283 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
287 if (chip
->config
& ADT75_OS_INT
)
288 return sprintf(buf
, "interrupt\n");
290 return sprintf(buf
, "comparator\n");
293 static ssize_t
adt75_set_oti_mode(struct device
*dev
,
294 struct device_attribute
*attr
,
298 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
299 struct adt75_chip_info
*chip
= iio_priv(dev_info
);
303 /* retrive ALART status */
304 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
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
);
316 chip
->config
= config
;
321 static ssize_t
adt75_show_available_oti_modes(struct device
*dev
,
322 struct device_attribute
*attr
,
325 return sprintf(buf
, "comparator\ninterrupt\n");
328 static ssize_t
adt75_show_smbus_alart(struct device
*dev
,
329 struct device_attribute
*attr
,
332 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
333 struct adt75_chip_info
*chip
= iio_priv(dev_info
);
336 /* retrive ALART status */
337 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
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
,
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;
355 ret
= strict_strtoul(buf
, 10, &data
);
359 /* retrive ALART status */
360 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
364 config
= chip
->config
& ~ADT75_SMBUS_ALART
;
366 config
|= ADT75_SMBUS_ALART
;
368 ret
= adt75_i2c_write(dev_info
, ADT75_CONFIG
, config
);
372 chip
->config
= config
;
377 static ssize_t
adt75_show_fault_queue(struct device
*dev
,
378 struct device_attribute
*attr
,
381 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
382 struct adt75_chip_info
*chip
= iio_priv(dev_info
);
385 /* retrive ALART status */
386 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
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
,
399 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
400 struct adt75_chip_info
*chip
= iio_priv(dev_info
);
405 ret
= strict_strtoul(buf
, 10, &data
);
409 /* retrive ALART status */
410 ret
= adt75_i2c_read(dev_info
, ADT75_CONFIG
, &chip
->config
);
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
);
420 chip
->config
= config
;
424 static inline ssize_t
adt75_show_t_bound(struct device
*dev
,
425 struct device_attribute
*attr
,
428 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
429 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
434 ret
= adt75_i2c_read(dev_info
, this_attr
->address
, (u8
*)&data
);
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
;
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
,
455 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
456 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
462 pos
= strchr(buf
, '.');
464 ret
= strict_strtol(buf
, 10, &tmp1
);
466 if (ret
|| tmp1
> 127 || tmp1
< -128)
471 if (len
> ADT75_VALUE_FLOAT_OFFSET
)
472 len
= ADT75_VALUE_FLOAT_OFFSET
;
474 ret
= strict_strtol(pos
, 10, &tmp2
);
477 tmp2
= (tmp2
/ 625) * 625;
484 data
= (data
<< ADT75_VALUE_FLOAT_OFFSET
) | (tmp2
& ADT75_VALUE_FLOAT_MASK
);
486 /* convert positive value to supplyment */
487 data
= (ADT75_VALUE_SIGN
<< 1) - data
;
488 data
<<= ADT75_VALUE_OFFSET
;
491 ret
= adt75_i2c_write(dev_info
, this_attr
->address
, (u8
)data
);
499 static IIO_DEVICE_ATTR(oti_mode
,
501 adt75_show_oti_mode
, adt75_set_oti_mode
, 0);
502 static IIO_DEVICE_ATTR(available_oti_modes
,
504 adt75_show_available_oti_modes
, NULL
, 0);
505 static IIO_DEVICE_ATTR(smbus_alart
,
507 adt75_show_smbus_alart
, adt75_set_smbus_alart
, 0);
508 static IIO_DEVICE_ATTR(fault_queue
,
510 adt75_show_fault_queue
, adt75_set_fault_queue
, 0);
511 static IIO_DEVICE_ATTR(t_os_value
,
513 adt75_show_t_bound
, adt75_set_t_bound
,
515 static IIO_DEVICE_ATTR(t_hyst_value
,
517 adt75_show_t_bound
, adt75_set_t_bound
,
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
,
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
;
552 indio_dev
= iio_allocate_device(sizeof(*chip
));
553 if (indio_dev
== NULL
) {
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
);
573 if (client
->irq
> 0) {
574 ret
= request_threaded_irq(client
->irq
,
576 &adt75_event_handler
,
581 goto error_unreg_dev
;
583 ret
= adt75_i2c_read(indio_dev
, ADT75_CONFIG
, &chip
->config
);
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
);
595 goto error_unreg_irq
;
599 dev_info(&client
->dev
, "%s temperature sensor registered.\n",
604 free_irq(client
->irq
, indio_dev
);
606 iio_device_unregister(indio_dev
);
608 iio_free_device(indio_dev
);
613 static int __devexit
adt75_remove(struct i2c_client
*client
)
615 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
618 free_irq(client
->irq
, indio_dev
);
619 iio_device_unregister(indio_dev
);
620 iio_free_device(indio_dev
);
625 static const struct i2c_device_id adt75_id
[] = {
630 MODULE_DEVICE_TABLE(i2c
, adt75_id
);
632 static struct i2c_driver adt75_driver
= {
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
);