]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/hwmon/lm77.c
hwmon: (lm77) Do not preserve hysteresis when updating critical temp limit
[mirror_ubuntu-hirsute-kernel.git] / drivers / hwmon / lm77.c
CommitLineData
1da177e4 1/*
02fe2fd9
GR
2 * lm77.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 *
5 * Copyright (c) 2004 Andras BALI <drewie@freemail.hu>
6 *
7 * Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>. The LM77
8 * is a temperature sensor and thermal window comparator with 0.5 deg
9 * resolution made by National Semiconductor. Complete datasheet can be
10 * obtained at their site:
11 * http://www.national.com/pf/LM/LM77.html
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
02fe2fd9 22 */
1da177e4 23
1da177e4
LT
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>
943b0830 29#include <linux/hwmon.h>
1f52af0f 30#include <linux/hwmon-sysfs.h>
943b0830 31#include <linux/err.h>
9a61bf63 32#include <linux/mutex.h>
1da177e4
LT
33
34/* Addresses to scan */
25e9c86d
MH
35static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
36 I2C_CLIENT_END };
1da177e4 37
1da177e4
LT
38/* The LM77 registers */
39#define LM77_REG_TEMP 0x00
40#define LM77_REG_CONF 0x01
41#define LM77_REG_TEMP_HYST 0x02
42#define LM77_REG_TEMP_CRIT 0x03
43#define LM77_REG_TEMP_MIN 0x04
44#define LM77_REG_TEMP_MAX 0x05
45
46/* Each client has this additional data */
47struct lm77_data {
02fe2fd9 48 struct device *hwmon_dev;
9a61bf63 49 struct mutex update_lock;
1da177e4
LT
50 char valid;
51 unsigned long last_updated; /* In jiffies */
52 int temp_input; /* Temperatures */
53 int temp_crit;
54 int temp_min;
55 int temp_max;
56 int temp_hyst;
57 u8 alarms;
58};
59
1da177e4
LT
60/* straight from the datasheet */
61#define LM77_TEMP_MIN (-55000)
62#define LM77_TEMP_MAX 125000
63
02fe2fd9
GR
64/*
65 * In the temperature registers, the low 3 bits are not part of the
66 * temperature values; they are the status bits.
67 */
413b6451 68static inline s16 LM77_TEMP_TO_REG(int temp)
1da177e4 69{
2a844c14 70 int ntemp = clamp_val(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
413b6451 71 return (ntemp / 500) * 8;
1da177e4
LT
72}
73
413b6451 74static inline int LM77_TEMP_FROM_REG(s16 reg)
1da177e4 75{
413b6451 76 return (reg / 8) * 500;
1da177e4
LT
77}
78
9f9edcd4
GR
79/*
80 * All registers are word-sized, except for the configuration register.
81 * The LM77 uses the high-byte first convention.
82 */
83static u16 lm77_read_value(struct i2c_client *client, u8 reg)
84{
85 if (reg == LM77_REG_CONF)
86 return i2c_smbus_read_byte_data(client, reg);
87 else
88 return i2c_smbus_read_word_swapped(client, reg);
89}
90
91static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
92{
93 if (reg == LM77_REG_CONF)
94 return i2c_smbus_write_byte_data(client, reg, value);
95 else
96 return i2c_smbus_write_word_swapped(client, reg, value);
97}
98
99static struct lm77_data *lm77_update_device(struct device *dev)
100{
101 struct i2c_client *client = to_i2c_client(dev);
102 struct lm77_data *data = i2c_get_clientdata(client);
103
104 mutex_lock(&data->update_lock);
105
106 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
107 || !data->valid) {
108 dev_dbg(&client->dev, "Starting lm77 update\n");
109 data->temp_input =
110 LM77_TEMP_FROM_REG(lm77_read_value(client,
111 LM77_REG_TEMP));
112 data->temp_hyst =
113 LM77_TEMP_FROM_REG(lm77_read_value(client,
114 LM77_REG_TEMP_HYST));
115 data->temp_crit =
116 LM77_TEMP_FROM_REG(lm77_read_value(client,
117 LM77_REG_TEMP_CRIT));
118 data->temp_min =
119 LM77_TEMP_FROM_REG(lm77_read_value(client,
120 LM77_REG_TEMP_MIN));
121 data->temp_max =
122 LM77_TEMP_FROM_REG(lm77_read_value(client,
123 LM77_REG_TEMP_MAX));
124 data->alarms =
125 lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
126 data->last_updated = jiffies;
127 data->valid = 1;
128 }
129
130 mutex_unlock(&data->update_lock);
131
132 return data;
133}
134
1da177e4
LT
135/* sysfs stuff */
136
137/* read routines for temperature limits */
138#define show(value) \
02fe2fd9
GR
139static ssize_t show_##value(struct device *dev, \
140 struct device_attribute *attr, \
141 char *buf) \
1da177e4
LT
142{ \
143 struct lm77_data *data = lm77_update_device(dev); \
144 return sprintf(buf, "%d\n", data->value); \
145}
146
147show(temp_input);
148show(temp_crit);
149show(temp_min);
150show(temp_max);
1da177e4
LT
151
152/* read routines for hysteresis values */
02fe2fd9
GR
153static ssize_t show_temp_crit_hyst(struct device *dev,
154 struct device_attribute *attr, char *buf)
1da177e4
LT
155{
156 struct lm77_data *data = lm77_update_device(dev);
157 return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
158}
02fe2fd9
GR
159static ssize_t show_temp_min_hyst(struct device *dev,
160 struct device_attribute *attr, char *buf)
1da177e4
LT
161{
162 struct lm77_data *data = lm77_update_device(dev);
163 return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
164}
02fe2fd9
GR
165static ssize_t show_temp_max_hyst(struct device *dev,
166 struct device_attribute *attr, char *buf)
1da177e4
LT
167{
168 struct lm77_data *data = lm77_update_device(dev);
169 return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
170}
171
172/* write routines */
173#define set(value, reg) \
02fe2fd9
GR
174static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \
175 const char *buf, size_t count) \
176{ \
177 struct i2c_client *client = to_i2c_client(dev); \
178 struct lm77_data *data = i2c_get_clientdata(client); \
179 long val; \
180 int err = kstrtol(buf, 10, &val); \
181 if (err) \
182 return err; \
183 \
184 mutex_lock(&data->update_lock); \
185 data->value = val; \
186 lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
187 mutex_unlock(&data->update_lock); \
188 return count; \
1da177e4
LT
189}
190
191set(temp_min, LM77_REG_TEMP_MIN);
192set(temp_max, LM77_REG_TEMP_MAX);
193
02fe2fd9
GR
194/*
195 * hysteresis is stored as a relative value on the chip, so it has to be
196 * converted first
197 */
198static ssize_t set_temp_crit_hyst(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf, size_t count)
1da177e4
LT
201{
202 struct i2c_client *client = to_i2c_client(dev);
203 struct lm77_data *data = i2c_get_clientdata(client);
02fe2fd9
GR
204 unsigned long val;
205 int err;
206
207 err = kstrtoul(buf, 10, &val);
208 if (err)
209 return err;
1da177e4 210
9a61bf63 211 mutex_lock(&data->update_lock);
1da177e4
LT
212 data->temp_hyst = data->temp_crit - val;
213 lm77_write_value(client, LM77_REG_TEMP_HYST,
214 LM77_TEMP_TO_REG(data->temp_hyst));
9a61bf63 215 mutex_unlock(&data->update_lock);
1da177e4
LT
216 return count;
217}
218
02fe2fd9
GR
219static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
220 const char *buf, size_t count)
1da177e4
LT
221{
222 struct i2c_client *client = to_i2c_client(dev);
223 struct lm77_data *data = i2c_get_clientdata(client);
02fe2fd9
GR
224 unsigned long val;
225 int err;
226
227 err = kstrtoul(buf, 10, &val);
228 if (err)
229 return err;
230
9a61bf63 231 mutex_lock(&data->update_lock);
1da177e4 232 data->temp_crit = val;
1da177e4
LT
233 lm77_write_value(client, LM77_REG_TEMP_CRIT,
234 LM77_TEMP_TO_REG(data->temp_crit));
9a61bf63 235 mutex_unlock(&data->update_lock);
1da177e4
LT
236 return count;
237}
238
1f52af0f
JD
239static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
242 int bitnr = to_sensor_dev_attr(attr)->index;
243 struct lm77_data *data = lm77_update_device(dev);
244 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
245}
246
1da177e4
LT
247static DEVICE_ATTR(temp1_input, S_IRUGO,
248 show_temp_input, NULL);
249static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
250 show_temp_crit, set_temp_crit);
251static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
252 show_temp_min, set_temp_min);
253static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
254 show_temp_max, set_temp_max);
255
256static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
257 show_temp_crit_hyst, set_temp_crit_hyst);
258static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
259 show_temp_min_hyst, NULL);
260static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
261 show_temp_max_hyst, NULL);
262
1f52af0f
JD
263static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
264static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
265static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
1da177e4 266
0501a381
MH
267static struct attribute *lm77_attributes[] = {
268 &dev_attr_temp1_input.attr,
269 &dev_attr_temp1_crit.attr,
270 &dev_attr_temp1_min.attr,
271 &dev_attr_temp1_max.attr,
272 &dev_attr_temp1_crit_hyst.attr,
273 &dev_attr_temp1_min_hyst.attr,
274 &dev_attr_temp1_max_hyst.attr,
1f52af0f
JD
275 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
276 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
277 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0501a381
MH
278 NULL
279};
280
281static const struct attribute_group lm77_group = {
282 .attrs = lm77_attributes,
283};
284
a189dd62 285/* Return 0 if detection is successful, -ENODEV otherwise */
efd2d117 286static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info)
1da177e4 287{
efd2d117 288 struct i2c_adapter *adapter = client->adapter;
747d9bed 289 int i, cur, conf, hyst, crit, min, max;
1da177e4
LT
290
291 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
292 I2C_FUNC_SMBUS_WORD_DATA))
a189dd62 293 return -ENODEV;
1da177e4 294
02fe2fd9
GR
295 /*
296 * Here comes the remaining detection. Since the LM77 has no
297 * register dedicated to identification, we have to rely on the
298 * following tricks:
299 *
300 * 1. the high 4 bits represent the sign and thus they should
301 * always be the same
302 * 2. the high 3 bits are unused in the configuration register
303 * 3. addresses 0x06 and 0x07 return the last read value
304 * 4. registers cycling over 8-address boundaries
305 *
306 * Word-sized registers are high-byte first.
307 */
1da177e4 308
747d9bed 309 /* addresses cycling */
efd2d117
GR
310 cur = i2c_smbus_read_word_data(client, 0);
311 conf = i2c_smbus_read_byte_data(client, 1);
312 hyst = i2c_smbus_read_word_data(client, 2);
313 crit = i2c_smbus_read_word_data(client, 3);
314 min = i2c_smbus_read_word_data(client, 4);
315 max = i2c_smbus_read_word_data(client, 5);
747d9bed 316 for (i = 8; i <= 0xff; i += 8) {
efd2d117
GR
317 if (i2c_smbus_read_byte_data(client, i + 1) != conf
318 || i2c_smbus_read_word_data(client, i + 2) != hyst
319 || i2c_smbus_read_word_data(client, i + 3) != crit
320 || i2c_smbus_read_word_data(client, i + 4) != min
321 || i2c_smbus_read_word_data(client, i + 5) != max)
a189dd62 322 return -ENODEV;
747d9bed 323 }
1da177e4 324
747d9bed
JD
325 /* sign bits */
326 if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
327 || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
328 || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
329 || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
330 || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
331 return -ENODEV;
1da177e4 332
747d9bed
JD
333 /* unused bits */
334 if (conf & 0xe0)
335 return -ENODEV;
336
337 /* 0x06 and 0x07 return the last read value */
efd2d117
GR
338 cur = i2c_smbus_read_word_data(client, 0);
339 if (i2c_smbus_read_word_data(client, 6) != cur
340 || i2c_smbus_read_word_data(client, 7) != cur)
747d9bed 341 return -ENODEV;
efd2d117
GR
342 hyst = i2c_smbus_read_word_data(client, 2);
343 if (i2c_smbus_read_word_data(client, 6) != hyst
344 || i2c_smbus_read_word_data(client, 7) != hyst)
747d9bed 345 return -ENODEV;
efd2d117
GR
346 min = i2c_smbus_read_word_data(client, 4);
347 if (i2c_smbus_read_word_data(client, 6) != min
348 || i2c_smbus_read_word_data(client, 7) != min)
747d9bed 349 return -ENODEV;
1da177e4 350
a189dd62 351 strlcpy(info->type, "lm77", I2C_NAME_SIZE);
1da177e4 352
a189dd62
JD
353 return 0;
354}
355
9f9edcd4
GR
356static void lm77_init_client(struct i2c_client *client)
357{
358 /* Initialize the LM77 chip - turn off shutdown mode */
359 int conf = lm77_read_value(client, LM77_REG_CONF);
360 if (conf & 1)
361 lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
362}
363
efd2d117 364static int lm77_probe(struct i2c_client *client, const struct i2c_device_id *id)
a189dd62 365{
efd2d117 366 struct device *dev = &client->dev;
a189dd62
JD
367 struct lm77_data *data;
368 int err;
369
52714c04
GR
370 data = devm_kzalloc(dev, sizeof(struct lm77_data), GFP_KERNEL);
371 if (!data)
372 return -ENOMEM;
1da177e4 373
efd2d117 374 i2c_set_clientdata(client, data);
9a61bf63 375 mutex_init(&data->update_lock);
1da177e4 376
1da177e4 377 /* Initialize the LM77 chip */
efd2d117 378 lm77_init_client(client);
1da177e4
LT
379
380 /* Register sysfs hooks */
efd2d117 381 err = sysfs_create_group(&dev->kobj, &lm77_group);
02fe2fd9 382 if (err)
52714c04 383 return err;
0501a381 384
efd2d117 385 data->hwmon_dev = hwmon_device_register(dev);
1beeffe4
TJ
386 if (IS_ERR(data->hwmon_dev)) {
387 err = PTR_ERR(data->hwmon_dev);
0501a381 388 goto exit_remove;
943b0830
MH
389 }
390
1da177e4
LT
391 return 0;
392
0501a381 393exit_remove:
efd2d117 394 sysfs_remove_group(&dev->kobj, &lm77_group);
1da177e4
LT
395 return err;
396}
397
a189dd62 398static int lm77_remove(struct i2c_client *client)
1da177e4 399{
943b0830 400 struct lm77_data *data = i2c_get_clientdata(client);
1beeffe4 401 hwmon_device_unregister(data->hwmon_dev);
0501a381 402 sysfs_remove_group(&client->dev.kobj, &lm77_group);
1da177e4
LT
403 return 0;
404}
405
9f9edcd4
GR
406static const struct i2c_device_id lm77_id[] = {
407 { "lm77", 0 },
408 { }
409};
410MODULE_DEVICE_TABLE(i2c, lm77_id);
1da177e4 411
9f9edcd4
GR
412/* This is the driver that will be inserted */
413static struct i2c_driver lm77_driver = {
414 .class = I2C_CLASS_HWMON,
415 .driver = {
416 .name = "lm77",
417 },
418 .probe = lm77_probe,
419 .remove = lm77_remove,
420 .id_table = lm77_id,
421 .detect = lm77_detect,
422 .address_list = normal_i2c,
423};
1da177e4 424
f0967eea 425module_i2c_driver(lm77_driver);
1da177e4
LT
426
427MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
428MODULE_DESCRIPTION("LM77 driver");
429MODULE_LICENSE("GPL");