2 * adt7x10.c - Part of lm_sensors, Linux kernel modules for hardware
4 * This driver handles the ADT7410 and compatible digital temperature sensors.
5 * Hartmut Knaack <knaack.h@gmx.de> 2012-07-22
6 * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
7 * and adt7410.c from iio-staging by Sonic Zhang <sonic.zhang@analog.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/err.h>
31 #include <linux/mutex.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
40 #define ADT7X10_STAT_T_LOW (1 << 4)
41 #define ADT7X10_STAT_T_HIGH (1 << 5)
42 #define ADT7X10_STAT_T_CRIT (1 << 6)
43 #define ADT7X10_STAT_NOT_RDY (1 << 7)
48 #define ADT7X10_FAULT_QUEUE_MASK (1 << 0 | 1 << 1)
49 #define ADT7X10_CT_POLARITY (1 << 2)
50 #define ADT7X10_INT_POLARITY (1 << 3)
51 #define ADT7X10_EVENT_MODE (1 << 4)
52 #define ADT7X10_MODE_MASK (1 << 5 | 1 << 6)
53 #define ADT7X10_FULL (0 << 5 | 0 << 6)
54 #define ADT7X10_PD (1 << 5 | 1 << 6)
55 #define ADT7X10_RESOLUTION (1 << 7)
60 #define ADT7X10_T13_VALUE_MASK 0xFFF8
61 #define ADT7X10_T_HYST_MASK 0xF
63 /* straight from the datasheet */
64 #define ADT7X10_TEMP_MIN (-55000)
65 #define ADT7X10_TEMP_MAX 150000
67 /* Each client has this additional data */
69 const struct adt7x10_ops
*ops
;
71 struct device
*hwmon_dev
;
72 struct mutex update_lock
;
75 bool valid
; /* true if registers valid */
76 unsigned long last_updated
; /* In jiffies */
77 s16 temp
[4]; /* Register values,
82 u8 hyst
; /* hysteresis offset */
85 static int adt7x10_read_byte(struct device
*dev
, u8 reg
)
87 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
88 return d
->ops
->read_byte(dev
, reg
);
91 static int adt7x10_write_byte(struct device
*dev
, u8 reg
, u8 data
)
93 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
94 return d
->ops
->write_byte(dev
, reg
, data
);
97 static int adt7x10_read_word(struct device
*dev
, u8 reg
)
99 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
100 return d
->ops
->read_word(dev
, reg
);
103 static int adt7x10_write_word(struct device
*dev
, u8 reg
, u16 data
)
105 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
106 return d
->ops
->write_word(dev
, reg
, data
);
109 static const u8 ADT7X10_REG_TEMP
[4] = {
110 ADT7X10_TEMPERATURE
, /* input */
111 ADT7X10_T_ALARM_HIGH
, /* high */
112 ADT7X10_T_ALARM_LOW
, /* low */
113 ADT7X10_T_CRIT
, /* critical */
116 static irqreturn_t
adt7x10_irq_handler(int irq
, void *private)
118 struct device
*dev
= private;
121 status
= adt7x10_read_byte(dev
, ADT7X10_STATUS
);
125 if (status
& ADT7X10_STAT_T_HIGH
)
126 sysfs_notify(&dev
->kobj
, NULL
, "temp1_max_alarm");
127 if (status
& ADT7X10_STAT_T_LOW
)
128 sysfs_notify(&dev
->kobj
, NULL
, "temp1_min_alarm");
129 if (status
& ADT7X10_STAT_T_CRIT
)
130 sysfs_notify(&dev
->kobj
, NULL
, "temp1_crit_alarm");
135 static int adt7x10_temp_ready(struct device
*dev
)
139 for (i
= 0; i
< 6; i
++) {
140 status
= adt7x10_read_byte(dev
, ADT7X10_STATUS
);
143 if (!(status
& ADT7X10_STAT_NOT_RDY
))
150 static int adt7x10_update_temp(struct device
*dev
)
152 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
155 mutex_lock(&data
->update_lock
);
157 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
161 dev_dbg(dev
, "Starting update\n");
163 ret
= adt7x10_temp_ready(dev
); /* check for new value */
167 temp
= adt7x10_read_word(dev
, ADT7X10_REG_TEMP
[0]);
170 dev_dbg(dev
, "Failed to read value: reg %d, error %d\n",
171 ADT7X10_REG_TEMP
[0], ret
);
174 data
->temp
[0] = temp
;
175 data
->last_updated
= jiffies
;
180 mutex_unlock(&data
->update_lock
);
184 static int adt7x10_fill_cache(struct device
*dev
)
186 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
190 for (i
= 1; i
< ARRAY_SIZE(data
->temp
); i
++) {
191 ret
= adt7x10_read_word(dev
, ADT7X10_REG_TEMP
[i
]);
193 dev_dbg(dev
, "Failed to read value: reg %d, error %d\n",
194 ADT7X10_REG_TEMP
[i
], ret
);
200 ret
= adt7x10_read_byte(dev
, ADT7X10_T_HYST
);
202 dev_dbg(dev
, "Failed to read value: reg %d, error %d\n",
203 ADT7X10_T_HYST
, ret
);
211 static s16
ADT7X10_TEMP_TO_REG(long temp
)
213 return DIV_ROUND_CLOSEST(clamp_val(temp
, ADT7X10_TEMP_MIN
,
214 ADT7X10_TEMP_MAX
) * 128, 1000);
217 static int ADT7X10_REG_TO_TEMP(struct adt7x10_data
*data
, s16 reg
)
219 /* in 13 bit mode, bits 0-2 are status flags - mask them out */
220 if (!(data
->config
& ADT7X10_RESOLUTION
))
221 reg
&= ADT7X10_T13_VALUE_MASK
;
223 * temperature is stored in twos complement format, in steps of
226 return DIV_ROUND_CLOSEST(reg
* 1000, 128);
229 /*-----------------------------------------------------------------------*/
231 /* sysfs attributes for hwmon */
233 static ssize_t
adt7x10_show_temp(struct device
*dev
,
234 struct device_attribute
*da
,
237 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
238 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
241 if (attr
->index
== 0) {
244 ret
= adt7x10_update_temp(dev
);
249 return sprintf(buf
, "%d\n", ADT7X10_REG_TO_TEMP(data
,
250 data
->temp
[attr
->index
]));
253 static ssize_t
adt7x10_set_temp(struct device
*dev
,
254 struct device_attribute
*da
,
255 const char *buf
, size_t count
)
257 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
258 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
259 int nr
= attr
->index
;
263 ret
= kstrtol(buf
, 10, &temp
);
267 mutex_lock(&data
->update_lock
);
268 data
->temp
[nr
] = ADT7X10_TEMP_TO_REG(temp
);
269 ret
= adt7x10_write_word(dev
, ADT7X10_REG_TEMP
[nr
], data
->temp
[nr
]);
272 mutex_unlock(&data
->update_lock
);
276 static ssize_t
adt7x10_show_t_hyst(struct device
*dev
,
277 struct device_attribute
*da
,
280 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
281 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
282 int nr
= attr
->index
;
285 hyst
= (data
->hyst
& ADT7X10_T_HYST_MASK
) * 1000;
288 * hysteresis is stored as a 4 bit offset in the device, convert it
289 * to an absolute value
291 if (nr
== 2) /* min has positive offset, others have negative */
293 return sprintf(buf
, "%d\n",
294 ADT7X10_REG_TO_TEMP(data
, data
->temp
[nr
]) - hyst
);
297 static ssize_t
adt7x10_set_t_hyst(struct device
*dev
,
298 struct device_attribute
*da
,
299 const char *buf
, size_t count
)
301 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
305 ret
= kstrtol(buf
, 10, &hyst
);
308 /* convert absolute hysteresis value to a 4 bit delta value */
309 limit
= ADT7X10_REG_TO_TEMP(data
, data
->temp
[1]);
310 hyst
= clamp_val(hyst
, ADT7X10_TEMP_MIN
, ADT7X10_TEMP_MAX
);
311 data
->hyst
= clamp_val(DIV_ROUND_CLOSEST(limit
- hyst
, 1000),
312 0, ADT7X10_T_HYST_MASK
);
313 ret
= adt7x10_write_byte(dev
, ADT7X10_T_HYST
, data
->hyst
);
320 static ssize_t
adt7x10_show_alarm(struct device
*dev
,
321 struct device_attribute
*da
,
324 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
327 ret
= adt7x10_read_byte(dev
, ADT7X10_STATUS
);
331 return sprintf(buf
, "%d\n", !!(ret
& attr
->index
));
334 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*da
,
337 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
339 return sprintf(buf
, "%s\n", data
->name
);
342 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, adt7x10_show_temp
, NULL
, 0);
343 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
,
344 adt7x10_show_temp
, adt7x10_set_temp
, 1);
345 static SENSOR_DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
,
346 adt7x10_show_temp
, adt7x10_set_temp
, 2);
347 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IWUSR
| S_IRUGO
,
348 adt7x10_show_temp
, adt7x10_set_temp
, 3);
349 static SENSOR_DEVICE_ATTR(temp1_max_hyst
, S_IWUSR
| S_IRUGO
,
350 adt7x10_show_t_hyst
, adt7x10_set_t_hyst
, 1);
351 static SENSOR_DEVICE_ATTR(temp1_min_hyst
, S_IRUGO
,
352 adt7x10_show_t_hyst
, NULL
, 2);
353 static SENSOR_DEVICE_ATTR(temp1_crit_hyst
, S_IRUGO
,
354 adt7x10_show_t_hyst
, NULL
, 3);
355 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, adt7x10_show_alarm
,
356 NULL
, ADT7X10_STAT_T_LOW
);
357 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, adt7x10_show_alarm
,
358 NULL
, ADT7X10_STAT_T_HIGH
);
359 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, adt7x10_show_alarm
,
360 NULL
, ADT7X10_STAT_T_CRIT
);
361 static DEVICE_ATTR_RO(name
);
363 static struct attribute
*adt7x10_attributes
[] = {
364 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
365 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
366 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
367 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
368 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
369 &sensor_dev_attr_temp1_min_hyst
.dev_attr
.attr
,
370 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
371 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
372 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
373 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
377 static const struct attribute_group adt7x10_group
= {
378 .attrs
= adt7x10_attributes
,
381 int adt7x10_probe(struct device
*dev
, const char *name
, int irq
,
382 const struct adt7x10_ops
*ops
)
384 struct adt7x10_data
*data
;
387 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
394 dev_set_drvdata(dev
, data
);
395 mutex_init(&data
->update_lock
);
397 /* configure as specified */
398 ret
= adt7x10_read_byte(dev
, ADT7X10_CONFIG
);
400 dev_dbg(dev
, "Can't read config? %d\n", ret
);
403 data
->oldconfig
= ret
;
406 * Set to 16 bit resolution, continous conversion and comparator mode.
408 data
->config
= data
->oldconfig
;
409 data
->config
&= ~(ADT7X10_MODE_MASK
| ADT7X10_CT_POLARITY
|
410 ADT7X10_INT_POLARITY
);
411 data
->config
|= ADT7X10_FULL
| ADT7X10_RESOLUTION
| ADT7X10_EVENT_MODE
;
413 if (data
->config
!= data
->oldconfig
) {
414 ret
= adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->config
);
418 dev_dbg(dev
, "Config %02x\n", data
->config
);
420 ret
= adt7x10_fill_cache(dev
);
424 /* Register sysfs hooks */
425 ret
= sysfs_create_group(&dev
->kobj
, &adt7x10_group
);
430 * The I2C device will already have it's own 'name' attribute, but for
431 * the SPI device we need to register it. name will only be non NULL if
432 * the device doesn't register the 'name' attribute on its own.
435 ret
= device_create_file(dev
, &dev_attr_name
);
440 data
->hwmon_dev
= hwmon_device_register(dev
);
441 if (IS_ERR(data
->hwmon_dev
)) {
442 ret
= PTR_ERR(data
->hwmon_dev
);
443 goto exit_remove_name
;
447 ret
= request_threaded_irq(irq
, NULL
, adt7x10_irq_handler
,
448 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
451 goto exit_hwmon_device_unregister
;
456 exit_hwmon_device_unregister
:
457 hwmon_device_unregister(data
->hwmon_dev
);
460 device_remove_file(dev
, &dev_attr_name
);
462 sysfs_remove_group(&dev
->kobj
, &adt7x10_group
);
464 adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->oldconfig
);
467 EXPORT_SYMBOL_GPL(adt7x10_probe
);
469 int adt7x10_remove(struct device
*dev
, int irq
)
471 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
476 hwmon_device_unregister(data
->hwmon_dev
);
478 device_remove_file(dev
, &dev_attr_name
);
479 sysfs_remove_group(&dev
->kobj
, &adt7x10_group
);
480 if (data
->oldconfig
!= data
->config
)
481 adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->oldconfig
);
484 EXPORT_SYMBOL_GPL(adt7x10_remove
);
486 #ifdef CONFIG_PM_SLEEP
488 static int adt7x10_suspend(struct device
*dev
)
490 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
492 return adt7x10_write_byte(dev
, ADT7X10_CONFIG
,
493 data
->config
| ADT7X10_PD
);
496 static int adt7x10_resume(struct device
*dev
)
498 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
500 return adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->config
);
503 SIMPLE_DEV_PM_OPS(adt7x10_dev_pm_ops
, adt7x10_suspend
, adt7x10_resume
);
504 EXPORT_SYMBOL_GPL(adt7x10_dev_pm_ops
);
506 #endif /* CONFIG_PM_SLEEP */
508 MODULE_AUTHOR("Hartmut Knaack");
509 MODULE_DESCRIPTION("ADT7410/ADT7420, ADT7310/ADT7320 common code");
510 MODULE_LICENSE("GPL");