1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * adt7x10.c - Part of lm_sensors, Linux kernel modules for hardware
5 * This driver handles the ADT7410 and compatible digital temperature sensors.
6 * Hartmut Knaack <knaack.h@gmx.de> 2012-07-22
7 * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
8 * and adt7410.c from iio-staging by Sonic Zhang <sonic.zhang@analog.com>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/jiffies.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
27 #define ADT7X10_STAT_T_LOW (1 << 4)
28 #define ADT7X10_STAT_T_HIGH (1 << 5)
29 #define ADT7X10_STAT_T_CRIT (1 << 6)
30 #define ADT7X10_STAT_NOT_RDY (1 << 7)
35 #define ADT7X10_FAULT_QUEUE_MASK (1 << 0 | 1 << 1)
36 #define ADT7X10_CT_POLARITY (1 << 2)
37 #define ADT7X10_INT_POLARITY (1 << 3)
38 #define ADT7X10_EVENT_MODE (1 << 4)
39 #define ADT7X10_MODE_MASK (1 << 5 | 1 << 6)
40 #define ADT7X10_FULL (0 << 5 | 0 << 6)
41 #define ADT7X10_PD (1 << 5 | 1 << 6)
42 #define ADT7X10_RESOLUTION (1 << 7)
47 #define ADT7X10_T13_VALUE_MASK 0xFFF8
48 #define ADT7X10_T_HYST_MASK 0xF
50 /* straight from the datasheet */
51 #define ADT7X10_TEMP_MIN (-55000)
52 #define ADT7X10_TEMP_MAX 150000
54 /* Each client has this additional data */
56 const struct adt7x10_ops
*ops
;
58 struct device
*hwmon_dev
;
59 struct mutex update_lock
;
62 bool valid
; /* true if registers valid */
63 unsigned long last_updated
; /* In jiffies */
64 s16 temp
[4]; /* Register values,
69 u8 hyst
; /* hysteresis offset */
72 static int adt7x10_read_byte(struct device
*dev
, u8 reg
)
74 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
75 return d
->ops
->read_byte(dev
, reg
);
78 static int adt7x10_write_byte(struct device
*dev
, u8 reg
, u8 data
)
80 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
81 return d
->ops
->write_byte(dev
, reg
, data
);
84 static int adt7x10_read_word(struct device
*dev
, u8 reg
)
86 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
87 return d
->ops
->read_word(dev
, reg
);
90 static int adt7x10_write_word(struct device
*dev
, u8 reg
, u16 data
)
92 struct adt7x10_data
*d
= dev_get_drvdata(dev
);
93 return d
->ops
->write_word(dev
, reg
, data
);
96 static const u8 ADT7X10_REG_TEMP
[4] = {
97 ADT7X10_TEMPERATURE
, /* input */
98 ADT7X10_T_ALARM_HIGH
, /* high */
99 ADT7X10_T_ALARM_LOW
, /* low */
100 ADT7X10_T_CRIT
, /* critical */
103 static irqreturn_t
adt7x10_irq_handler(int irq
, void *private)
105 struct device
*dev
= private;
108 status
= adt7x10_read_byte(dev
, ADT7X10_STATUS
);
112 if (status
& ADT7X10_STAT_T_HIGH
)
113 sysfs_notify(&dev
->kobj
, NULL
, "temp1_max_alarm");
114 if (status
& ADT7X10_STAT_T_LOW
)
115 sysfs_notify(&dev
->kobj
, NULL
, "temp1_min_alarm");
116 if (status
& ADT7X10_STAT_T_CRIT
)
117 sysfs_notify(&dev
->kobj
, NULL
, "temp1_crit_alarm");
122 static int adt7x10_temp_ready(struct device
*dev
)
126 for (i
= 0; i
< 6; i
++) {
127 status
= adt7x10_read_byte(dev
, ADT7X10_STATUS
);
130 if (!(status
& ADT7X10_STAT_NOT_RDY
))
137 static int adt7x10_update_temp(struct device
*dev
)
139 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
142 mutex_lock(&data
->update_lock
);
144 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
148 dev_dbg(dev
, "Starting update\n");
150 ret
= adt7x10_temp_ready(dev
); /* check for new value */
154 temp
= adt7x10_read_word(dev
, ADT7X10_REG_TEMP
[0]);
157 dev_dbg(dev
, "Failed to read value: reg %d, error %d\n",
158 ADT7X10_REG_TEMP
[0], ret
);
161 data
->temp
[0] = temp
;
162 data
->last_updated
= jiffies
;
167 mutex_unlock(&data
->update_lock
);
171 static int adt7x10_fill_cache(struct device
*dev
)
173 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
177 for (i
= 1; i
< ARRAY_SIZE(data
->temp
); i
++) {
178 ret
= adt7x10_read_word(dev
, ADT7X10_REG_TEMP
[i
]);
180 dev_dbg(dev
, "Failed to read value: reg %d, error %d\n",
181 ADT7X10_REG_TEMP
[i
], ret
);
187 ret
= adt7x10_read_byte(dev
, ADT7X10_T_HYST
);
189 dev_dbg(dev
, "Failed to read value: reg %d, error %d\n",
190 ADT7X10_T_HYST
, ret
);
198 static s16
ADT7X10_TEMP_TO_REG(long temp
)
200 return DIV_ROUND_CLOSEST(clamp_val(temp
, ADT7X10_TEMP_MIN
,
201 ADT7X10_TEMP_MAX
) * 128, 1000);
204 static int ADT7X10_REG_TO_TEMP(struct adt7x10_data
*data
, s16 reg
)
206 /* in 13 bit mode, bits 0-2 are status flags - mask them out */
207 if (!(data
->config
& ADT7X10_RESOLUTION
))
208 reg
&= ADT7X10_T13_VALUE_MASK
;
210 * temperature is stored in twos complement format, in steps of
213 return DIV_ROUND_CLOSEST(reg
* 1000, 128);
216 /*-----------------------------------------------------------------------*/
218 /* sysfs attributes for hwmon */
220 static ssize_t
adt7x10_temp_show(struct device
*dev
,
221 struct device_attribute
*da
, char *buf
)
223 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
224 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
227 if (attr
->index
== 0) {
230 ret
= adt7x10_update_temp(dev
);
235 return sprintf(buf
, "%d\n", ADT7X10_REG_TO_TEMP(data
,
236 data
->temp
[attr
->index
]));
239 static ssize_t
adt7x10_temp_store(struct device
*dev
,
240 struct device_attribute
*da
,
241 const char *buf
, size_t count
)
243 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
244 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
245 int nr
= attr
->index
;
249 ret
= kstrtol(buf
, 10, &temp
);
253 mutex_lock(&data
->update_lock
);
254 data
->temp
[nr
] = ADT7X10_TEMP_TO_REG(temp
);
255 ret
= adt7x10_write_word(dev
, ADT7X10_REG_TEMP
[nr
], data
->temp
[nr
]);
258 mutex_unlock(&data
->update_lock
);
262 static ssize_t
adt7x10_t_hyst_show(struct device
*dev
,
263 struct device_attribute
*da
, char *buf
)
265 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
266 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
267 int nr
= attr
->index
;
270 hyst
= (data
->hyst
& ADT7X10_T_HYST_MASK
) * 1000;
273 * hysteresis is stored as a 4 bit offset in the device, convert it
274 * to an absolute value
276 if (nr
== 2) /* min has positive offset, others have negative */
278 return sprintf(buf
, "%d\n",
279 ADT7X10_REG_TO_TEMP(data
, data
->temp
[nr
]) - hyst
);
282 static ssize_t
adt7x10_t_hyst_store(struct device
*dev
,
283 struct device_attribute
*da
,
284 const char *buf
, size_t count
)
286 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
290 ret
= kstrtol(buf
, 10, &hyst
);
293 /* convert absolute hysteresis value to a 4 bit delta value */
294 limit
= ADT7X10_REG_TO_TEMP(data
, data
->temp
[1]);
295 hyst
= clamp_val(hyst
, ADT7X10_TEMP_MIN
, ADT7X10_TEMP_MAX
);
296 data
->hyst
= clamp_val(DIV_ROUND_CLOSEST(limit
- hyst
, 1000),
297 0, ADT7X10_T_HYST_MASK
);
298 ret
= adt7x10_write_byte(dev
, ADT7X10_T_HYST
, data
->hyst
);
305 static ssize_t
adt7x10_alarm_show(struct device
*dev
,
306 struct device_attribute
*da
, char *buf
)
308 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
311 ret
= adt7x10_read_byte(dev
, ADT7X10_STATUS
);
315 return sprintf(buf
, "%d\n", !!(ret
& attr
->index
));
318 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*da
,
321 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
323 return sprintf(buf
, "%s\n", data
->name
);
326 static SENSOR_DEVICE_ATTR_RO(temp1_input
, adt7x10_temp
, 0);
327 static SENSOR_DEVICE_ATTR_RW(temp1_max
, adt7x10_temp
, 1);
328 static SENSOR_DEVICE_ATTR_RW(temp1_min
, adt7x10_temp
, 2);
329 static SENSOR_DEVICE_ATTR_RW(temp1_crit
, adt7x10_temp
, 3);
330 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst
, adt7x10_t_hyst
, 1);
331 static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst
, adt7x10_t_hyst
, 2);
332 static SENSOR_DEVICE_ATTR_RO(temp1_crit_hyst
, adt7x10_t_hyst
, 3);
333 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm
, adt7x10_alarm
,
335 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm
, adt7x10_alarm
,
336 ADT7X10_STAT_T_HIGH
);
337 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm
, adt7x10_alarm
,
338 ADT7X10_STAT_T_CRIT
);
339 static DEVICE_ATTR_RO(name
);
341 static struct attribute
*adt7x10_attributes
[] = {
342 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
343 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
344 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
345 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
346 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
347 &sensor_dev_attr_temp1_min_hyst
.dev_attr
.attr
,
348 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
349 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
350 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
351 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
355 static const struct attribute_group adt7x10_group
= {
356 .attrs
= adt7x10_attributes
,
359 int adt7x10_probe(struct device
*dev
, const char *name
, int irq
,
360 const struct adt7x10_ops
*ops
)
362 struct adt7x10_data
*data
;
365 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
372 dev_set_drvdata(dev
, data
);
373 mutex_init(&data
->update_lock
);
375 /* configure as specified */
376 ret
= adt7x10_read_byte(dev
, ADT7X10_CONFIG
);
378 dev_dbg(dev
, "Can't read config? %d\n", ret
);
381 data
->oldconfig
= ret
;
384 * Set to 16 bit resolution, continous conversion and comparator mode.
386 data
->config
= data
->oldconfig
;
387 data
->config
&= ~(ADT7X10_MODE_MASK
| ADT7X10_CT_POLARITY
|
388 ADT7X10_INT_POLARITY
);
389 data
->config
|= ADT7X10_FULL
| ADT7X10_RESOLUTION
| ADT7X10_EVENT_MODE
;
391 if (data
->config
!= data
->oldconfig
) {
392 ret
= adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->config
);
396 dev_dbg(dev
, "Config %02x\n", data
->config
);
398 ret
= adt7x10_fill_cache(dev
);
402 /* Register sysfs hooks */
403 ret
= sysfs_create_group(&dev
->kobj
, &adt7x10_group
);
408 * The I2C device will already have it's own 'name' attribute, but for
409 * the SPI device we need to register it. name will only be non NULL if
410 * the device doesn't register the 'name' attribute on its own.
413 ret
= device_create_file(dev
, &dev_attr_name
);
418 data
->hwmon_dev
= hwmon_device_register(dev
);
419 if (IS_ERR(data
->hwmon_dev
)) {
420 ret
= PTR_ERR(data
->hwmon_dev
);
421 goto exit_remove_name
;
425 ret
= request_threaded_irq(irq
, NULL
, adt7x10_irq_handler
,
426 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
429 goto exit_hwmon_device_unregister
;
434 exit_hwmon_device_unregister
:
435 hwmon_device_unregister(data
->hwmon_dev
);
438 device_remove_file(dev
, &dev_attr_name
);
440 sysfs_remove_group(&dev
->kobj
, &adt7x10_group
);
442 adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->oldconfig
);
445 EXPORT_SYMBOL_GPL(adt7x10_probe
);
447 int adt7x10_remove(struct device
*dev
, int irq
)
449 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
454 hwmon_device_unregister(data
->hwmon_dev
);
456 device_remove_file(dev
, &dev_attr_name
);
457 sysfs_remove_group(&dev
->kobj
, &adt7x10_group
);
458 if (data
->oldconfig
!= data
->config
)
459 adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->oldconfig
);
462 EXPORT_SYMBOL_GPL(adt7x10_remove
);
464 #ifdef CONFIG_PM_SLEEP
466 static int adt7x10_suspend(struct device
*dev
)
468 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
470 return adt7x10_write_byte(dev
, ADT7X10_CONFIG
,
471 data
->config
| ADT7X10_PD
);
474 static int adt7x10_resume(struct device
*dev
)
476 struct adt7x10_data
*data
= dev_get_drvdata(dev
);
478 return adt7x10_write_byte(dev
, ADT7X10_CONFIG
, data
->config
);
481 SIMPLE_DEV_PM_OPS(adt7x10_dev_pm_ops
, adt7x10_suspend
, adt7x10_resume
);
482 EXPORT_SYMBOL_GPL(adt7x10_dev_pm_ops
);
484 #endif /* CONFIG_PM_SLEEP */
486 MODULE_AUTHOR("Hartmut Knaack");
487 MODULE_DESCRIPTION("ADT7410/ADT7420, ADT7310/ADT7320 common code");
488 MODULE_LICENSE("GPL");