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.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/i2c.h>
33 #include <linux/i2c-sensor.h>
36 /* Addresses to scan */
37 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
38 static unsigned short normal_i2c_range[] = { 0x48, 0x4b, I2C_CLIENT_END };
39 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
40 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
42 /* Insmod parameters */
43 SENSORS_INSMOD_1(lm77);
45 /* The LM77 registers */
46 #define LM77_REG_TEMP 0x00
47 #define LM77_REG_CONF 0x01
48 #define LM77_REG_TEMP_HYST 0x02
49 #define LM77_REG_TEMP_CRIT 0x03
50 #define LM77_REG_TEMP_MIN 0x04
51 #define LM77_REG_TEMP_MAX 0x05
53 /* Each client has this additional data */
55 struct i2c_client client;
56 struct semaphore update_lock;
58 unsigned long last_updated; /* In jiffies */
59 int temp_input; /* Temperatures */
67 static int lm77_attach_adapter(struct i2c_adapter *adapter);
68 static int lm77_detect(struct i2c_adapter *adapter, int address, int kind);
69 static void lm77_init_client(struct i2c_client *client);
70 static int lm77_detach_client(struct i2c_client *client);
71 static u16 lm77_read_value(struct i2c_client *client, u8 reg);
72 static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
74 static struct lm77_data *lm77_update_device(struct device *dev);
77 /* This is the driver that will be inserted */
78 static struct i2c_driver lm77_driver = {
81 .flags = I2C_DF_NOTIFY,
82 .attach_adapter = lm77_attach_adapter,
83 .detach_client = lm77_detach_client,
86 static int lm77_id = 0;
88 /* straight from the datasheet */
89 #define LM77_TEMP_MIN (-55000)
90 #define LM77_TEMP_MAX 125000
92 /* In the temperature registers, the low 3 bits are not part of the
93 temperature values; they are the status bits. */
94 static inline u16 LM77_TEMP_TO_REG(int temp)
96 int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
97 return (u16)((ntemp / 500) * 8);
100 static inline int LM77_TEMP_FROM_REG(u16 reg)
102 return ((int)reg / 8) * 500;
107 /* read routines for temperature limits */
108 #define show(value) \
109 static ssize_t show_##value(struct device *dev, char *buf) \
111 struct lm77_data *data = lm77_update_device(dev); \
112 return sprintf(buf, "%d\n", data->value); \
121 /* read routines for hysteresis values */
122 static ssize_t show_temp_crit_hyst(struct device *dev, char *buf)
124 struct lm77_data *data = lm77_update_device(dev);
125 return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
127 static ssize_t show_temp_min_hyst(struct device *dev, char *buf)
129 struct lm77_data *data = lm77_update_device(dev);
130 return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
132 static ssize_t show_temp_max_hyst(struct device *dev, char *buf)
134 struct lm77_data *data = lm77_update_device(dev);
135 return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
139 #define set(value, reg) \
140 static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \
142 struct i2c_client *client = to_i2c_client(dev); \
143 struct lm77_data *data = i2c_get_clientdata(client); \
144 data->value = simple_strtoul(buf, NULL, 10); \
145 lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
149 set(temp_min, LM77_REG_TEMP_MIN);
150 set(temp_max, LM77_REG_TEMP_MAX);
152 /* hysteresis is stored as a relative value on the chip, so it has to be
154 static ssize_t set_temp_crit_hyst(struct device *dev, const char *buf, size_t count)
156 struct i2c_client *client = to_i2c_client(dev);
157 struct lm77_data *data = i2c_get_clientdata(client);
158 data->temp_hyst = data->temp_crit - simple_strtoul(buf, NULL, 10);
159 lm77_write_value(client, LM77_REG_TEMP_HYST,
160 LM77_TEMP_TO_REG(data->temp_hyst));
164 /* preserve hysteresis when setting T_crit */
165 static ssize_t set_temp_crit(struct device *dev, const char *buf, size_t count)
167 struct i2c_client *client = to_i2c_client(dev);
168 struct lm77_data *data = i2c_get_clientdata(client);
169 int oldcrithyst = data->temp_crit - data->temp_hyst;
170 data->temp_crit = simple_strtoul(buf, NULL, 10);
171 data->temp_hyst = data->temp_crit - oldcrithyst;
172 lm77_write_value(client, LM77_REG_TEMP_CRIT,
173 LM77_TEMP_TO_REG(data->temp_crit));
174 lm77_write_value(client, LM77_REG_TEMP_HYST,
175 LM77_TEMP_TO_REG(data->temp_hyst));
179 static DEVICE_ATTR(temp1_input, S_IRUGO,
180 show_temp_input, NULL);
181 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
182 show_temp_crit, set_temp_crit);
183 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
184 show_temp_min, set_temp_min);
185 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
186 show_temp_max, set_temp_max);
188 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
189 show_temp_crit_hyst, set_temp_crit_hyst);
190 static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
191 show_temp_min_hyst, NULL);
192 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
193 show_temp_max_hyst, NULL);
195 static DEVICE_ATTR(alarms, S_IRUGO,
198 static int lm77_attach_adapter(struct i2c_adapter *adapter)
200 if (!(adapter->class & I2C_CLASS_HWMON))
202 return i2c_detect(adapter, &addr_data, lm77_detect);
205 /* This function is called by i2c_detect */
206 static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
208 struct i2c_client *new_client;
209 struct lm77_data *data;
211 const char *name = "";
213 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
214 I2C_FUNC_SMBUS_WORD_DATA))
217 /* OK. For now, we presume we have a valid client. We now create the
218 client structure, even though we cannot fill it completely yet.
219 But it allows us to access lm77_{read,write}_value. */
220 if (!(data = kmalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
224 memset(data, 0, sizeof(struct lm77_data));
226 new_client = &data->client;
227 i2c_set_clientdata(new_client, data);
228 new_client->addr = address;
229 new_client->adapter = adapter;
230 new_client->driver = &lm77_driver;
231 new_client->flags = 0;
233 /* Here comes the remaining detection. Since the LM77 has no
234 register dedicated to identification, we have to rely on the
237 1. the high 4 bits represent the sign and thus they should
239 2. the high 3 bits are unused in the configuration register
240 3. addresses 0x06 and 0x07 return the last read value
241 4. registers cycling over 8-address boundaries
243 Word-sized registers are high-byte first. */
245 int i, cur, conf, hyst, crit, min, max;
247 /* addresses cycling */
248 cur = i2c_smbus_read_word_data(new_client, 0);
249 conf = i2c_smbus_read_byte_data(new_client, 1);
250 hyst = i2c_smbus_read_word_data(new_client, 2);
251 crit = i2c_smbus_read_word_data(new_client, 3);
252 min = i2c_smbus_read_word_data(new_client, 4);
253 max = i2c_smbus_read_word_data(new_client, 5);
254 for (i = 8; i <= 0xff; i += 8)
255 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
256 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
257 || i2c_smbus_read_word_data(new_client, i + 3) != crit
258 || i2c_smbus_read_word_data(new_client, i + 4) != min
259 || i2c_smbus_read_word_data(new_client, i + 5) != max)
263 if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
264 || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
265 || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
266 || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
267 || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
274 /* 0x06 and 0x07 return the last read value */
275 cur = i2c_smbus_read_word_data(new_client, 0);
276 if (i2c_smbus_read_word_data(new_client, 6) != cur
277 || i2c_smbus_read_word_data(new_client, 7) != cur)
279 hyst = i2c_smbus_read_word_data(new_client, 2);
280 if (i2c_smbus_read_word_data(new_client, 6) != hyst
281 || i2c_smbus_read_word_data(new_client, 7) != hyst)
283 min = i2c_smbus_read_word_data(new_client, 4);
284 if (i2c_smbus_read_word_data(new_client, 6) != min
285 || i2c_smbus_read_word_data(new_client, 7) != min)
290 /* Determine the chip type - only one kind supported! */
298 /* Fill in the remaining client fields and put it into the global list */
299 strlcpy(new_client->name, name, I2C_NAME_SIZE);
301 new_client->id = lm77_id++;
303 init_MUTEX(&data->update_lock);
305 /* Tell the I2C layer a new client has arrived */
306 if ((err = i2c_attach_client(new_client)))
309 /* Initialize the LM77 chip */
310 lm77_init_client(new_client);
312 /* Register sysfs hooks */
313 device_create_file(&new_client->dev, &dev_attr_temp1_input);
314 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
315 device_create_file(&new_client->dev, &dev_attr_temp1_min);
316 device_create_file(&new_client->dev, &dev_attr_temp1_max);
317 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
318 device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
319 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
320 device_create_file(&new_client->dev, &dev_attr_alarms);
329 static int lm77_detach_client(struct i2c_client *client)
331 i2c_detach_client(client);
332 kfree(i2c_get_clientdata(client));
336 /* All registers are word-sized, except for the configuration register.
337 The LM77 uses the high-byte first convention. */
338 static u16 lm77_read_value(struct i2c_client *client, u8 reg)
340 if (reg == LM77_REG_CONF)
341 return i2c_smbus_read_byte_data(client, reg);
343 return swab16(i2c_smbus_read_word_data(client, reg));
346 static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
348 if (reg == LM77_REG_CONF)
349 return i2c_smbus_write_byte_data(client, reg, value);
351 return i2c_smbus_write_word_data(client, reg, swab16(value));
354 static void lm77_init_client(struct i2c_client *client)
356 /* Initialize the LM77 chip - turn off shutdown mode */
357 int conf = lm77_read_value(client, LM77_REG_CONF);
359 lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
362 static struct lm77_data *lm77_update_device(struct device *dev)
364 struct i2c_client *client = to_i2c_client(dev);
365 struct lm77_data *data = i2c_get_clientdata(client);
367 down(&data->update_lock);
369 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
370 (jiffies < data->last_updated) || !data->valid) {
371 dev_dbg(&client->dev, "Starting lm77 update\n");
373 LM77_TEMP_FROM_REG(lm77_read_value(client,
376 LM77_TEMP_FROM_REG(lm77_read_value(client,
377 LM77_REG_TEMP_HYST));
379 LM77_TEMP_FROM_REG(lm77_read_value(client,
380 LM77_REG_TEMP_CRIT));
382 LM77_TEMP_FROM_REG(lm77_read_value(client,
385 LM77_TEMP_FROM_REG(lm77_read_value(client,
388 lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
389 data->last_updated = jiffies;
393 up(&data->update_lock);
398 static int __init sensors_lm77_init(void)
400 return i2c_add_driver(&lm77_driver);
403 static void __exit sensors_lm77_exit(void)
405 i2c_del_driver(&lm77_driver);
408 MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
409 MODULE_DESCRIPTION("LM77 driver");
410 MODULE_LICENSE("GPL");
412 module_init(sensors_lm77_init);
413 module_exit(sensors_lm77_exit);