2 * lm77.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (c) 2004 Andras BALI <drewie@freemail.hu>
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
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.
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.
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>
34 /* Addresses to scan */
35 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
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
46 /* Each client has this additional data */
48 struct device *hwmon_dev;
49 struct mutex update_lock;
51 unsigned long last_updated; /* In jiffies */
52 int temp_input; /* Temperatures */
60 /* straight from the datasheet */
61 #define LM77_TEMP_MIN (-55000)
62 #define LM77_TEMP_MAX 125000
65 * In the temperature registers, the low 3 bits are not part of the
66 * temperature values; they are the status bits.
68 static inline s16 LM77_TEMP_TO_REG(int temp)
70 int ntemp = clamp_val(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
71 return (ntemp / 500) * 8;
74 static inline int LM77_TEMP_FROM_REG(s16 reg)
76 return (reg / 8) * 500;
80 * All registers are word-sized, except for the configuration register.
81 * The LM77 uses the high-byte first convention.
83 static u16 lm77_read_value(struct i2c_client *client, u8 reg)
85 if (reg == LM77_REG_CONF)
86 return i2c_smbus_read_byte_data(client, reg);
88 return i2c_smbus_read_word_swapped(client, reg);
91 static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
93 if (reg == LM77_REG_CONF)
94 return i2c_smbus_write_byte_data(client, reg, value);
96 return i2c_smbus_write_word_swapped(client, reg, value);
99 static struct lm77_data *lm77_update_device(struct device *dev)
101 struct i2c_client *client = to_i2c_client(dev);
102 struct lm77_data *data = i2c_get_clientdata(client);
104 mutex_lock(&data->update_lock);
106 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
108 dev_dbg(&client->dev, "Starting lm77 update\n");
110 LM77_TEMP_FROM_REG(lm77_read_value(client,
113 LM77_TEMP_FROM_REG(lm77_read_value(client,
114 LM77_REG_TEMP_HYST));
116 LM77_TEMP_FROM_REG(lm77_read_value(client,
117 LM77_REG_TEMP_CRIT));
119 LM77_TEMP_FROM_REG(lm77_read_value(client,
122 LM77_TEMP_FROM_REG(lm77_read_value(client,
125 lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
126 data->last_updated = jiffies;
130 mutex_unlock(&data->update_lock);
137 /* read routines for temperature limits */
138 #define show(value) \
139 static ssize_t show_##value(struct device *dev, \
140 struct device_attribute *attr, \
143 struct lm77_data *data = lm77_update_device(dev); \
144 return sprintf(buf, "%d\n", data->value); \
152 /* read routines for hysteresis values */
153 static ssize_t show_temp_crit_hyst(struct device *dev,
154 struct device_attribute *attr, char *buf)
156 struct lm77_data *data = lm77_update_device(dev);
157 return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
159 static ssize_t show_temp_min_hyst(struct device *dev,
160 struct device_attribute *attr, char *buf)
162 struct lm77_data *data = lm77_update_device(dev);
163 return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
165 static ssize_t show_temp_max_hyst(struct device *dev,
166 struct device_attribute *attr, char *buf)
168 struct lm77_data *data = lm77_update_device(dev);
169 return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
173 #define set(value, reg) \
174 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \
175 const char *buf, size_t count) \
177 struct i2c_client *client = to_i2c_client(dev); \
178 struct lm77_data *data = i2c_get_clientdata(client); \
180 int err = kstrtol(buf, 10, &val); \
184 mutex_lock(&data->update_lock); \
186 lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
187 mutex_unlock(&data->update_lock); \
191 set(temp_min, LM77_REG_TEMP_MIN);
192 set(temp_max, LM77_REG_TEMP_MAX);
195 * hysteresis is stored as a relative value on the chip, so it has to be
198 static ssize_t set_temp_crit_hyst(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf, size_t count)
202 struct i2c_client *client = to_i2c_client(dev);
203 struct lm77_data *data = i2c_get_clientdata(client);
207 err = kstrtoul(buf, 10, &val);
211 mutex_lock(&data->update_lock);
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));
215 mutex_unlock(&data->update_lock);
219 /* preserve hysteresis when setting T_crit */
220 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
221 const char *buf, size_t count)
223 struct i2c_client *client = to_i2c_client(dev);
224 struct lm77_data *data = i2c_get_clientdata(client);
229 err = kstrtoul(buf, 10, &val);
233 mutex_lock(&data->update_lock);
234 oldcrithyst = data->temp_crit - data->temp_hyst;
235 data->temp_crit = val;
236 data->temp_hyst = data->temp_crit - oldcrithyst;
237 lm77_write_value(client, LM77_REG_TEMP_CRIT,
238 LM77_TEMP_TO_REG(data->temp_crit));
239 lm77_write_value(client, LM77_REG_TEMP_HYST,
240 LM77_TEMP_TO_REG(data->temp_hyst));
241 mutex_unlock(&data->update_lock);
245 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
248 int bitnr = to_sensor_dev_attr(attr)->index;
249 struct lm77_data *data = lm77_update_device(dev);
250 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
253 static DEVICE_ATTR(temp1_input, S_IRUGO,
254 show_temp_input, NULL);
255 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
256 show_temp_crit, set_temp_crit);
257 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
258 show_temp_min, set_temp_min);
259 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
260 show_temp_max, set_temp_max);
262 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
263 show_temp_crit_hyst, set_temp_crit_hyst);
264 static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
265 show_temp_min_hyst, NULL);
266 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
267 show_temp_max_hyst, NULL);
269 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
270 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
271 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
273 static struct attribute *lm77_attributes[] = {
274 &dev_attr_temp1_input.attr,
275 &dev_attr_temp1_crit.attr,
276 &dev_attr_temp1_min.attr,
277 &dev_attr_temp1_max.attr,
278 &dev_attr_temp1_crit_hyst.attr,
279 &dev_attr_temp1_min_hyst.attr,
280 &dev_attr_temp1_max_hyst.attr,
281 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
282 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
283 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
287 static const struct attribute_group lm77_group = {
288 .attrs = lm77_attributes,
291 /* Return 0 if detection is successful, -ENODEV otherwise */
292 static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info)
294 struct i2c_adapter *adapter = client->adapter;
295 int i, cur, conf, hyst, crit, min, max;
297 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
298 I2C_FUNC_SMBUS_WORD_DATA))
302 * Here comes the remaining detection. Since the LM77 has no
303 * register dedicated to identification, we have to rely on the
306 * 1. the high 4 bits represent the sign and thus they should
308 * 2. the high 3 bits are unused in the configuration register
309 * 3. addresses 0x06 and 0x07 return the last read value
310 * 4. registers cycling over 8-address boundaries
312 * Word-sized registers are high-byte first.
315 /* addresses cycling */
316 cur = i2c_smbus_read_word_data(client, 0);
317 conf = i2c_smbus_read_byte_data(client, 1);
318 hyst = i2c_smbus_read_word_data(client, 2);
319 crit = i2c_smbus_read_word_data(client, 3);
320 min = i2c_smbus_read_word_data(client, 4);
321 max = i2c_smbus_read_word_data(client, 5);
322 for (i = 8; i <= 0xff; i += 8) {
323 if (i2c_smbus_read_byte_data(client, i + 1) != conf
324 || i2c_smbus_read_word_data(client, i + 2) != hyst
325 || i2c_smbus_read_word_data(client, i + 3) != crit
326 || i2c_smbus_read_word_data(client, i + 4) != min
327 || i2c_smbus_read_word_data(client, i + 5) != max)
332 if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
333 || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
334 || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
335 || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
336 || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
343 /* 0x06 and 0x07 return the last read value */
344 cur = i2c_smbus_read_word_data(client, 0);
345 if (i2c_smbus_read_word_data(client, 6) != cur
346 || i2c_smbus_read_word_data(client, 7) != cur)
348 hyst = i2c_smbus_read_word_data(client, 2);
349 if (i2c_smbus_read_word_data(client, 6) != hyst
350 || i2c_smbus_read_word_data(client, 7) != hyst)
352 min = i2c_smbus_read_word_data(client, 4);
353 if (i2c_smbus_read_word_data(client, 6) != min
354 || i2c_smbus_read_word_data(client, 7) != min)
357 strlcpy(info->type, "lm77", I2C_NAME_SIZE);
362 static void lm77_init_client(struct i2c_client *client)
364 /* Initialize the LM77 chip - turn off shutdown mode */
365 int conf = lm77_read_value(client, LM77_REG_CONF);
367 lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
370 static int lm77_probe(struct i2c_client *client, const struct i2c_device_id *id)
372 struct device *dev = &client->dev;
373 struct lm77_data *data;
376 data = devm_kzalloc(dev, sizeof(struct lm77_data), GFP_KERNEL);
380 i2c_set_clientdata(client, data);
381 mutex_init(&data->update_lock);
383 /* Initialize the LM77 chip */
384 lm77_init_client(client);
386 /* Register sysfs hooks */
387 err = sysfs_create_group(&dev->kobj, &lm77_group);
391 data->hwmon_dev = hwmon_device_register(dev);
392 if (IS_ERR(data->hwmon_dev)) {
393 err = PTR_ERR(data->hwmon_dev);
400 sysfs_remove_group(&dev->kobj, &lm77_group);
404 static int lm77_remove(struct i2c_client *client)
406 struct lm77_data *data = i2c_get_clientdata(client);
407 hwmon_device_unregister(data->hwmon_dev);
408 sysfs_remove_group(&client->dev.kobj, &lm77_group);
412 static const struct i2c_device_id lm77_id[] = {
416 MODULE_DEVICE_TABLE(i2c, lm77_id);
418 /* This is the driver that will be inserted */
419 static struct i2c_driver lm77_driver = {
420 .class = I2C_CLASS_HWMON,
425 .remove = lm77_remove,
427 .detect = lm77_detect,
428 .address_list = normal_i2c,
431 module_i2c_driver(lm77_driver);
433 MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
434 MODULE_DESCRIPTION("LM77 driver");
435 MODULE_LICENSE("GPL");