2 * adt7410.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/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33 #include <linux/delay.h>
36 * ADT7410 registers definition
39 #define ADT7410_TEMPERATURE 0
40 #define ADT7410_STATUS 2
41 #define ADT7410_CONFIG 3
42 #define ADT7410_T_ALARM_HIGH 4
43 #define ADT7410_T_ALARM_LOW 6
44 #define ADT7410_T_CRIT 8
45 #define ADT7410_T_HYST 0xA
50 #define ADT7410_STAT_T_LOW (1 << 4)
51 #define ADT7410_STAT_T_HIGH (1 << 5)
52 #define ADT7410_STAT_T_CRIT (1 << 6)
53 #define ADT7410_STAT_NOT_RDY (1 << 7)
58 #define ADT7410_FAULT_QUEUE_MASK (1 << 0 | 1 << 1)
59 #define ADT7410_CT_POLARITY (1 << 2)
60 #define ADT7410_INT_POLARITY (1 << 3)
61 #define ADT7410_EVENT_MODE (1 << 4)
62 #define ADT7410_MODE_MASK (1 << 5 | 1 << 6)
63 #define ADT7410_FULL (0 << 5 | 0 << 6)
64 #define ADT7410_PD (1 << 5 | 1 << 6)
65 #define ADT7410_RESOLUTION (1 << 7)
70 #define ADT7410_T13_VALUE_MASK 0xFFF8
71 #define ADT7410_T_HYST_MASK 0xF
73 /* straight from the datasheet */
74 #define ADT7410_TEMP_MIN (-55000)
75 #define ADT7410_TEMP_MAX 150000
77 enum adt7410_type { /* keep sorted in alphabetical order */
81 static const u8 ADT7410_REG_TEMP[4] = {
82 ADT7410_TEMPERATURE, /* input */
83 ADT7410_T_ALARM_HIGH, /* high */
84 ADT7410_T_ALARM_LOW, /* low */
85 ADT7410_T_CRIT, /* critical */
88 /* Each client has this additional data */
90 struct device *hwmon_dev;
91 struct mutex update_lock;
94 bool valid; /* true if registers valid */
95 unsigned long last_updated; /* In jiffies */
96 s16 temp[4]; /* Register values,
101 u8 hyst; /* hysteresis offset */
105 * adt7410 register access by I2C
107 static int adt7410_temp_ready(struct i2c_client *client)
111 for (i = 0; i < 6; i++) {
112 status = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
115 if (!(status & ADT7410_STAT_NOT_RDY))
122 static int adt7410_update_temp(struct device *dev)
124 struct i2c_client *client = to_i2c_client(dev);
125 struct adt7410_data *data = i2c_get_clientdata(client);
128 mutex_lock(&data->update_lock);
130 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
134 dev_dbg(&client->dev, "Starting update\n");
136 ret = adt7410_temp_ready(client); /* check for new value */
140 temp = i2c_smbus_read_word_swapped(client, ADT7410_REG_TEMP[0]);
143 dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
144 ADT7410_REG_TEMP[0], ret);
147 data->temp[0] = temp;
149 data->last_updated = jiffies;
154 mutex_unlock(&data->update_lock);
158 static int adt7410_fill_cache(struct i2c_client *client)
160 struct adt7410_data *data = i2c_get_clientdata(client);
164 for (i = 1; i < ARRAY_SIZE(ADT7410_REG_TEMP); i++) {
165 ret = i2c_smbus_read_word_swapped(client, ADT7410_REG_TEMP[i]);
167 dev_dbg(&client->dev,
168 "Failed to read value: reg %d, error %d\n",
169 ADT7410_REG_TEMP[i], ret);
175 ret = i2c_smbus_read_byte_data(client, ADT7410_T_HYST);
177 dev_dbg(&client->dev,
178 "Failed to read value: hyst reg, error %d\n",
187 static s16 ADT7410_TEMP_TO_REG(long temp)
189 return DIV_ROUND_CLOSEST(clamp_val(temp, ADT7410_TEMP_MIN,
190 ADT7410_TEMP_MAX) * 128, 1000);
193 static int ADT7410_REG_TO_TEMP(struct adt7410_data *data, s16 reg)
195 /* in 13 bit mode, bits 0-2 are status flags - mask them out */
196 if (!(data->config & ADT7410_RESOLUTION))
197 reg &= ADT7410_T13_VALUE_MASK;
199 * temperature is stored in twos complement format, in steps of
202 return DIV_ROUND_CLOSEST(reg * 1000, 128);
205 /*-----------------------------------------------------------------------*/
207 /* sysfs attributes for hwmon */
209 static ssize_t adt7410_show_temp(struct device *dev,
210 struct device_attribute *da, char *buf)
212 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
213 struct i2c_client *client = to_i2c_client(dev);
214 struct adt7410_data *data = i2c_get_clientdata(client);
216 if (attr->index == 0) {
219 ret = adt7410_update_temp(dev);
224 return sprintf(buf, "%d\n", ADT7410_REG_TO_TEMP(data,
225 data->temp[attr->index]));
228 static ssize_t adt7410_set_temp(struct device *dev,
229 struct device_attribute *da,
230 const char *buf, size_t count)
232 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
233 struct i2c_client *client = to_i2c_client(dev);
234 struct adt7410_data *data = i2c_get_clientdata(client);
235 int nr = attr->index;
239 ret = kstrtol(buf, 10, &temp);
243 mutex_lock(&data->update_lock);
244 data->temp[nr] = ADT7410_TEMP_TO_REG(temp);
245 ret = i2c_smbus_write_word_swapped(client, ADT7410_REG_TEMP[nr],
249 mutex_unlock(&data->update_lock);
253 static ssize_t adt7410_show_t_hyst(struct device *dev,
254 struct device_attribute *da,
257 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
258 struct i2c_client *client = to_i2c_client(dev);
259 struct adt7410_data *data = i2c_get_clientdata(client);
260 int nr = attr->index;
263 hyst = (data->hyst & ADT7410_T_HYST_MASK) * 1000;
266 * hysteresis is stored as a 4 bit offset in the device, convert it
267 * to an absolute value
269 if (nr == 2) /* min has positive offset, others have negative */
271 return sprintf(buf, "%d\n",
272 ADT7410_REG_TO_TEMP(data, data->temp[nr]) - hyst);
275 static ssize_t adt7410_set_t_hyst(struct device *dev,
276 struct device_attribute *da,
277 const char *buf, size_t count)
279 struct i2c_client *client = to_i2c_client(dev);
280 struct adt7410_data *data = i2c_get_clientdata(client);
284 ret = kstrtol(buf, 10, &hyst);
287 /* convert absolute hysteresis value to a 4 bit delta value */
288 limit = ADT7410_REG_TO_TEMP(data, data->temp[1]);
289 hyst = clamp_val(hyst, ADT7410_TEMP_MIN, ADT7410_TEMP_MAX);
290 data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000), 0,
291 ADT7410_T_HYST_MASK);
292 ret = i2c_smbus_write_byte_data(client, ADT7410_T_HYST, data->hyst);
299 static ssize_t adt7410_show_alarm(struct device *dev,
300 struct device_attribute *da,
303 struct i2c_client *client = to_i2c_client(dev);
304 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
307 ret = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
311 return sprintf(buf, "%d\n", !!(ret & attr->index));
314 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7410_show_temp, NULL, 0);
315 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
316 adt7410_show_temp, adt7410_set_temp, 1);
317 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
318 adt7410_show_temp, adt7410_set_temp, 2);
319 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
320 adt7410_show_temp, adt7410_set_temp, 3);
321 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
322 adt7410_show_t_hyst, adt7410_set_t_hyst, 1);
323 static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
324 adt7410_show_t_hyst, NULL, 2);
325 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
326 adt7410_show_t_hyst, NULL, 3);
327 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, adt7410_show_alarm,
328 NULL, ADT7410_STAT_T_LOW);
329 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adt7410_show_alarm,
330 NULL, ADT7410_STAT_T_HIGH);
331 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, adt7410_show_alarm,
332 NULL, ADT7410_STAT_T_CRIT);
334 static struct attribute *adt7410_attributes[] = {
335 &sensor_dev_attr_temp1_input.dev_attr.attr,
336 &sensor_dev_attr_temp1_max.dev_attr.attr,
337 &sensor_dev_attr_temp1_min.dev_attr.attr,
338 &sensor_dev_attr_temp1_crit.dev_attr.attr,
339 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
340 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
341 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
342 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
343 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
344 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
348 static const struct attribute_group adt7410_group = {
349 .attrs = adt7410_attributes,
352 /*-----------------------------------------------------------------------*/
354 /* device probe and removal */
356 static int adt7410_probe(struct i2c_client *client,
357 const struct i2c_device_id *id)
359 struct adt7410_data *data;
362 if (!i2c_check_functionality(client->adapter,
363 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
366 data = devm_kzalloc(&client->dev, sizeof(struct adt7410_data),
371 i2c_set_clientdata(client, data);
372 mutex_init(&data->update_lock);
374 /* configure as specified */
375 ret = i2c_smbus_read_byte_data(client, ADT7410_CONFIG);
377 dev_dbg(&client->dev, "Can't read config? %d\n", ret);
380 data->oldconfig = ret;
382 * Set to 16 bit resolution, continous conversion and comparator mode.
384 ret &= ~ADT7410_MODE_MASK;
385 data->config = ret | ADT7410_FULL | ADT7410_RESOLUTION |
387 if (data->config != data->oldconfig) {
388 ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
393 dev_dbg(&client->dev, "Config %02x\n", data->config);
395 ret = adt7410_fill_cache(client);
399 /* Register sysfs hooks */
400 ret = sysfs_create_group(&client->dev.kobj, &adt7410_group);
404 data->hwmon_dev = hwmon_device_register(&client->dev);
405 if (IS_ERR(data->hwmon_dev)) {
406 ret = PTR_ERR(data->hwmon_dev);
410 dev_info(&client->dev, "sensor '%s'\n", client->name);
415 sysfs_remove_group(&client->dev.kobj, &adt7410_group);
417 i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->oldconfig);
421 static int adt7410_remove(struct i2c_client *client)
423 struct adt7410_data *data = i2c_get_clientdata(client);
425 hwmon_device_unregister(data->hwmon_dev);
426 sysfs_remove_group(&client->dev.kobj, &adt7410_group);
427 if (data->oldconfig != data->config)
428 i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
433 static const struct i2c_device_id adt7410_ids[] = {
434 { "adt7410", adt7410, },
435 { "adt7420", adt7410, },
438 MODULE_DEVICE_TABLE(i2c, adt7410_ids);
440 #ifdef CONFIG_PM_SLEEP
441 static int adt7410_suspend(struct device *dev)
444 struct i2c_client *client = to_i2c_client(dev);
445 struct adt7410_data *data = i2c_get_clientdata(client);
447 ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
448 data->config | ADT7410_PD);
452 static int adt7410_resume(struct device *dev)
455 struct i2c_client *client = to_i2c_client(dev);
456 struct adt7410_data *data = i2c_get_clientdata(client);
458 ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->config);
462 static SIMPLE_DEV_PM_OPS(adt7410_dev_pm_ops, adt7410_suspend, adt7410_resume);
464 #define ADT7410_DEV_PM_OPS (&adt7410_dev_pm_ops)
466 #define ADT7410_DEV_PM_OPS NULL
467 #endif /* CONFIG_PM */
469 static struct i2c_driver adt7410_driver = {
470 .class = I2C_CLASS_HWMON,
473 .pm = ADT7410_DEV_PM_OPS,
475 .probe = adt7410_probe,
476 .remove = adt7410_remove,
477 .id_table = adt7410_ids,
478 .address_list = I2C_ADDRS(0x48, 0x49, 0x4a, 0x4b),
481 module_i2c_driver(adt7410_driver);
483 MODULE_AUTHOR("Hartmut Knaack");
484 MODULE_DESCRIPTION("ADT7410/ADT7420 driver");
485 MODULE_LICENSE("GPL");