aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/hwmon/Kconfig10
-rw-r--r--drivers/hwmon/Makefile1
-rw-r--r--drivers/hwmon/adt7410.c459
3 files changed, 470 insertions, 0 deletions
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index b0a2e4c37e12..f50cb440575d 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -179,6 +179,16 @@ config SENSORS_ADM9240
179 This driver can also be built as a module. If so, the module 179 This driver can also be built as a module. If so, the module
180 will be called adm9240. 180 will be called adm9240.
181 181
182config SENSORS_ADT7410
183 tristate "Analog Devices ADT7410"
184 depends on I2C && EXPERIMENTAL
185 help
186 If you say yes here you get support for the Analog Devices
187 ADT7410 temperature monitoring chip.
188
189 This driver can also be built as a module. If so, the module
190 will be called adt7410.
191
182config SENSORS_ADT7411 192config SENSORS_ADT7411
183 tristate "Analog Devices ADT7411" 193 tristate "Analog Devices ADT7411"
184 depends on I2C && EXPERIMENTAL 194 depends on I2C && EXPERIMENTAL
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index 7aa98119c4ab..8b3d4382f61e 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -34,6 +34,7 @@ obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o
34obj-$(CONFIG_SENSORS_ADS1015) += ads1015.o 34obj-$(CONFIG_SENSORS_ADS1015) += ads1015.o
35obj-$(CONFIG_SENSORS_ADS7828) += ads7828.o 35obj-$(CONFIG_SENSORS_ADS7828) += ads7828.o
36obj-$(CONFIG_SENSORS_ADS7871) += ads7871.o 36obj-$(CONFIG_SENSORS_ADS7871) += ads7871.o
37obj-$(CONFIG_SENSORS_ADT7410) += adt7410.o
37obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o 38obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o
38obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o 39obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o
39obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o 40obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o
diff --git a/drivers/hwmon/adt7410.c b/drivers/hwmon/adt7410.c
new file mode 100644
index 000000000000..f8606dff3d1c
--- /dev/null
+++ b/drivers/hwmon/adt7410.c
@@ -0,0 +1,459 @@
1/*
2 * adt7410.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
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>
8 *
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.
13 *
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.
18 *
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.
22 */
23
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>
34
35/*
36 * ADT7410 registers definition
37 */
38
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
46
47/*
48 * ADT7410 status
49 */
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)
54
55/*
56 * ADT7410 config
57 */
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)
66
67/*
68 * ADT7410 masks
69 */
70#define ADT7410_T13_VALUE_MASK 0xFFF8
71#define ADT7410_T_HYST_MASK 0xF
72
73/* straight from the datasheet */
74#define ADT7410_TEMP_MIN (-55000)
75#define ADT7410_TEMP_MAX 150000
76
77enum adt7410_type { /* keep sorted in alphabetical order */
78 adt7410,
79};
80
81/* Addresses scanned */
82static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
83 I2C_CLIENT_END };
84
85static const u8 ADT7410_REG_TEMP[4] = {
86 ADT7410_TEMPERATURE, /* input */
87 ADT7410_T_ALARM_HIGH, /* high */
88 ADT7410_T_ALARM_LOW, /* low */
89 ADT7410_T_CRIT, /* critical */
90};
91
92/* Each client has this additional data */
93struct adt7410_data {
94 struct device *hwmon_dev;
95 struct mutex update_lock;
96 u8 config;
97 u8 oldconfig;
98 bool valid; /* true if registers valid */
99 unsigned long last_updated; /* In jiffies */
100 s16 temp[4]; /* Register values,
101 0 = input
102 1 = high
103 2 = low
104 3 = critical */
105 u8 hyst; /* hysteresis offset */
106};
107
108/*
109 * adt7410 register access by I2C
110 */
111static int adt7410_temp_ready(struct i2c_client *client)
112{
113 int i, status;
114
115 for (i = 0; i < 6; i++) {
116 status = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
117 if (status < 0)
118 return status;
119 if (!(status & ADT7410_STAT_NOT_RDY))
120 return 0;
121 msleep(60);
122 }
123 return -ETIMEDOUT;
124}
125
126static struct adt7410_data *adt7410_update_device(struct device *dev)
127{
128 struct i2c_client *client = to_i2c_client(dev);
129 struct adt7410_data *data = i2c_get_clientdata(client);
130 struct adt7410_data *ret = data;
131 mutex_lock(&data->update_lock);
132
133 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
134 || !data->valid) {
135 int i, status;
136
137 dev_dbg(&client->dev, "Starting update\n");
138
139 status = adt7410_temp_ready(client); /* check for new value */
140 if (unlikely(status)) {
141 ret = ERR_PTR(status);
142 goto abort;
143 }
144 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
145 status = i2c_smbus_read_word_swapped(client,
146 ADT7410_REG_TEMP[i]);
147 if (unlikely(status < 0)) {
148 dev_dbg(dev,
149 "Failed to read value: reg %d, error %d\n",
150 ADT7410_REG_TEMP[i], status);
151 ret = ERR_PTR(status);
152 goto abort;
153 }
154 data->temp[i] = status;
155 }
156 status = i2c_smbus_read_byte_data(client, ADT7410_T_HYST);
157 if (unlikely(status < 0)) {
158 dev_dbg(dev,
159 "Failed to read value: reg %d, error %d\n",
160 ADT7410_T_HYST, status);
161 ret = ERR_PTR(status);
162 goto abort;
163 }
164 data->hyst = status;
165 data->last_updated = jiffies;
166 data->valid = true;
167 }
168
169abort:
170 mutex_unlock(&data->update_lock);
171 return ret;
172}
173
174static s16 ADT7410_TEMP_TO_REG(long temp)
175{
176 return DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, ADT7410_TEMP_MIN,
177 ADT7410_TEMP_MAX) * 128, 1000);
178}
179
180static int ADT7410_REG_TO_TEMP(struct adt7410_data *data, s16 reg)
181{
182 /* in 13 bit mode, bits 0-2 are status flags - mask them out */
183 if (!(data->config & ADT7410_RESOLUTION))
184 reg &= ADT7410_T13_VALUE_MASK;
185 /*
186 * temperature is stored in twos complement format, in steps of
187 * 1/128°C
188 */
189 return DIV_ROUND_CLOSEST(reg * 1000, 128);
190}
191
192/*-----------------------------------------------------------------------*/
193
194/* sysfs attributes for hwmon */
195
196static ssize_t adt7410_show_temp(struct device *dev,
197 struct device_attribute *da, char *buf)
198{
199 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
200 struct adt7410_data *data = adt7410_update_device(dev);
201
202 if (IS_ERR(data))
203 return PTR_ERR(data);
204
205 return sprintf(buf, "%d\n", ADT7410_REG_TO_TEMP(data,
206 data->temp[attr->index]));
207}
208
209static ssize_t adt7410_set_temp(struct device *dev,
210 struct device_attribute *da,
211 const char *buf, size_t count)
212{
213 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
214 struct i2c_client *client = to_i2c_client(dev);
215 struct adt7410_data *data = i2c_get_clientdata(client);
216 int nr = attr->index;
217 long temp;
218 int ret;
219
220 ret = kstrtol(buf, 10, &temp);
221 if (ret)
222 return ret;
223
224 mutex_lock(&data->update_lock);
225 data->temp[nr] = ADT7410_TEMP_TO_REG(temp);
226 ret = i2c_smbus_write_word_swapped(client, ADT7410_REG_TEMP[nr],
227 data->temp[nr]);
228 if (ret)
229 count = ret;
230 mutex_unlock(&data->update_lock);
231 return count;
232}
233
234static ssize_t adt7410_show_t_hyst(struct device *dev,
235 struct device_attribute *da,
236 char *buf)
237{
238 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
239 struct adt7410_data *data = adt7410_update_device(dev);
240 int nr = attr->index;
241 int hyst = (data->hyst & ADT7410_T_HYST_MASK) * 1000;
242
243 /*
244 * hysteresis is stored as a 4 bit offset in the device, convert it
245 * to an absolute value
246 */
247 if (nr == 2) /* min has positive offset, others have negative */
248 hyst = -hyst;
249 return sprintf(buf, "%d\n",
250 ADT7410_REG_TO_TEMP(data, data->temp[nr]) - hyst);
251}
252
253static ssize_t adt7410_set_t_hyst(struct device *dev,
254 struct device_attribute *da,
255 const char *buf, size_t count)
256{
257 struct i2c_client *client = to_i2c_client(dev);
258 struct adt7410_data *data = i2c_get_clientdata(client);
259 int limit, ret;
260 long hyst;
261
262 ret = kstrtol(buf, 10, &hyst);
263 if (ret)
264 return ret;
265 /* convert absolute hysteresis value to a 4 bit delta value */
266 limit = ADT7410_REG_TO_TEMP(data, data->temp[1]);
267 hyst = SENSORS_LIMIT(hyst, ADT7410_TEMP_MIN, ADT7410_TEMP_MAX);
268 data->hyst = SENSORS_LIMIT(DIV_ROUND_CLOSEST(limit - hyst, 1000),
269 0, ADT7410_T_HYST_MASK);
270 ret = i2c_smbus_write_byte_data(client, ADT7410_T_HYST, data->hyst);
271 if (ret)
272 return ret;
273
274 return count;
275}
276
277static ssize_t adt7410_show_alarm(struct device *dev,
278 struct device_attribute *da,
279 char *buf)
280{
281 struct i2c_client *client = to_i2c_client(dev);
282 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
283 int ret;
284
285 ret = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
286 if (ret < 0)
287 return ret;
288
289 return sprintf(buf, "%d\n", !!(ret & attr->index));
290}
291
292static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7410_show_temp, NULL, 0);
293static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
294 adt7410_show_temp, adt7410_set_temp, 1);
295static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
296 adt7410_show_temp, adt7410_set_temp, 2);
297static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
298 adt7410_show_temp, adt7410_set_temp, 3);
299static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
300 adt7410_show_t_hyst, adt7410_set_t_hyst, 1);
301static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
302 adt7410_show_t_hyst, NULL, 2);
303static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
304 adt7410_show_t_hyst, NULL, 3);
305static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, adt7410_show_alarm,
306 NULL, ADT7410_STAT_T_LOW);
307static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adt7410_show_alarm,
308 NULL, ADT7410_STAT_T_HIGH);
309static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, adt7410_show_alarm,
310 NULL, ADT7410_STAT_T_CRIT);
311
312static struct attribute *adt7410_attributes[] = {
313 &sensor_dev_attr_temp1_input.dev_attr.attr,
314 &sensor_dev_attr_temp1_max.dev_attr.attr,
315 &sensor_dev_attr_temp1_min.dev_attr.attr,
316 &sensor_dev_attr_temp1_crit.dev_attr.attr,
317 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
318 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
319 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
320 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
321 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
322 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
323 NULL
324};
325
326static const struct attribute_group adt7410_group = {
327 .attrs = adt7410_attributes,
328};
329
330/*-----------------------------------------------------------------------*/
331
332/* device probe and removal */
333
334static int adt7410_probe(struct i2c_client *client,
335 const struct i2c_device_id *id)
336{
337 struct adt7410_data *data;
338 int ret;
339
340 if (!i2c_check_functionality(client->adapter,
341 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
342 return -ENODEV;
343
344 data = devm_kzalloc(&client->dev, sizeof(struct adt7410_data),
345 GFP_KERNEL);
346 if (!data)
347 return -ENOMEM;
348
349 i2c_set_clientdata(client, data);
350 mutex_init(&data->update_lock);
351
352 /* configure as specified */
353 ret = i2c_smbus_read_byte_data(client, ADT7410_CONFIG);
354 if (ret < 0) {
355 dev_dbg(&client->dev, "Can't read config? %d\n", ret);
356 return ret;
357 }
358 data->oldconfig = ret;
359 /*
360 * Set to 16 bit resolution, continous conversion and comparator mode.
361 */
362 data->config = ret | ADT7410_FULL | ADT7410_RESOLUTION |
363 ADT7410_EVENT_MODE;
364 if (data->config != data->oldconfig) {
365 ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
366 data->config);
367 if (ret)
368 return ret;
369 }
370 dev_dbg(&client->dev, "Config %02x\n", data->config);
371
372 /* Register sysfs hooks */
373 ret = sysfs_create_group(&client->dev.kobj, &adt7410_group);
374 if (ret)
375 goto exit_restore;
376
377 data->hwmon_dev = hwmon_device_register(&client->dev);
378 if (IS_ERR(data->hwmon_dev)) {
379 ret = PTR_ERR(data->hwmon_dev);
380 goto exit_remove;
381 }
382
383 dev_info(&client->dev, "sensor '%s'\n", client->name);
384
385 return 0;
386
387exit_remove:
388 sysfs_remove_group(&client->dev.kobj, &adt7410_group);
389exit_restore:
390 i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->oldconfig);
391 return ret;
392}
393
394static int adt7410_remove(struct i2c_client *client)
395{
396 struct adt7410_data *data = i2c_get_clientdata(client);
397
398 hwmon_device_unregister(data->hwmon_dev);
399 sysfs_remove_group(&client->dev.kobj, &adt7410_group);
400 if (data->oldconfig != data->config)
401 i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
402 data->oldconfig);
403 return 0;
404}
405
406static const struct i2c_device_id adt7410_ids[] = {
407 { "adt7410", adt7410, },
408 { /* LIST END */ }
409};
410MODULE_DEVICE_TABLE(i2c, adt7410_ids);
411
412#ifdef CONFIG_PM
413static int adt7410_suspend(struct device *dev)
414{
415 int ret;
416 struct i2c_client *client = to_i2c_client(dev);
417 struct adt7410_data *data = i2c_get_clientdata(client);
418
419 ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
420 data->config | ADT7410_PD);
421 return ret;
422}
423
424static int adt7410_resume(struct device *dev)
425{
426 int ret;
427 struct i2c_client *client = to_i2c_client(dev);
428 struct adt7410_data *data = i2c_get_clientdata(client);
429
430 ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->config);
431 return ret;
432}
433
434static const struct dev_pm_ops adt7410_dev_pm_ops = {
435 .suspend = adt7410_suspend,
436 .resume = adt7410_resume,
437};
438#define ADT7410_DEV_PM_OPS (&adt7410_dev_pm_ops)
439#else
440#define ADT7410_DEV_PM_OPS NULL
441#endif /* CONFIG_PM */
442
443static struct i2c_driver adt7410_driver = {
444 .class = I2C_CLASS_HWMON,
445 .driver = {
446 .name = "adt7410",
447 .pm = ADT7410_DEV_PM_OPS,
448 },
449 .probe = adt7410_probe,
450 .remove = adt7410_remove,
451 .id_table = adt7410_ids,
452 .address_list = normal_i2c,
453};
454
455module_i2c_driver(adt7410_driver);
456
457MODULE_AUTHOR("Hartmut Knaack");
458MODULE_DESCRIPTION("ADT7410 driver");
459MODULE_LICENSE("GPL");