2 lm75.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-sensor.h>
30 /* Addresses to scan */
31 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
32 static unsigned short normal_i2c_range[] = { 0x48, 0x4f, I2C_CLIENT_END };
33 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
34 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
36 /* Insmod parameters */
37 SENSORS_INSMOD_1(lm75);
39 /* Many LM75 constants specified below */
41 /* The LM75 registers */
42 #define LM75_REG_TEMP 0x00
43 #define LM75_REG_CONF 0x01
44 #define LM75_REG_TEMP_HYST 0x02
45 #define LM75_REG_TEMP_OS 0x03
47 /* Each client has this additional data */
49 struct i2c_client client;
50 struct semaphore update_lock;
51 char valid; /* !=0 if following fields are valid */
52 unsigned long last_updated; /* In jiffies */
53 u16 temp_input; /* Register values */
58 static int lm75_attach_adapter(struct i2c_adapter *adapter);
59 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
60 static void lm75_init_client(struct i2c_client *client);
61 static int lm75_detach_client(struct i2c_client *client);
62 static int lm75_read_value(struct i2c_client *client, u8 reg);
63 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
64 static struct lm75_data *lm75_update_device(struct device *dev);
67 /* This is the driver that will be inserted */
68 static struct i2c_driver lm75_driver = {
71 .id = I2C_DRIVERID_LM75,
72 .flags = I2C_DF_NOTIFY,
73 .attach_adapter = lm75_attach_adapter,
74 .detach_client = lm75_detach_client,
77 static int lm75_id = 0;
80 static ssize_t show_##value(struct device *dev, char *buf) \
82 struct lm75_data *data = lm75_update_device(dev); \
83 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
89 #define set(value, reg) \
90 static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \
92 struct i2c_client *client = to_i2c_client(dev); \
93 struct lm75_data *data = i2c_get_clientdata(client); \
94 int temp = simple_strtoul(buf, NULL, 10); \
95 data->value = LM75_TEMP_TO_REG(temp); \
96 lm75_write_value(client, reg, data->value); \
99 set(temp_max, LM75_REG_TEMP_OS);
100 set(temp_hyst, LM75_REG_TEMP_HYST);
102 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
103 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
104 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
106 static int lm75_attach_adapter(struct i2c_adapter *adapter)
108 if (!(adapter->class & I2C_CLASS_HWMON))
110 return i2c_detect(adapter, &addr_data, lm75_detect);
113 /* This function is called by i2c_detect */
114 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
116 int i, cur, conf, hyst, os;
117 struct i2c_client *new_client;
118 struct lm75_data *data;
120 const char *name = "";
122 /* Make sure we aren't probing the ISA bus!! This is just a safety check
123 at this moment; i2c_detect really won't call us. */
125 if (i2c_is_isa_adapter(adapter)) {
126 dev_dbg(&adapter->dev,
127 "lm75_detect called for an ISA bus adapter?!?\n");
132 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
133 I2C_FUNC_SMBUS_WORD_DATA))
136 /* OK. For now, we presume we have a valid client. We now create the
137 client structure, even though we cannot fill it completely yet.
138 But it allows us to access lm75_{read,write}_value. */
139 if (!(data = kmalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
143 memset(data, 0, sizeof(struct lm75_data));
145 new_client = &data->client;
146 i2c_set_clientdata(new_client, data);
147 new_client->addr = address;
148 new_client->adapter = adapter;
149 new_client->driver = &lm75_driver;
150 new_client->flags = 0;
152 /* Now, we do the remaining detection. It is lousy. */
154 cur = i2c_smbus_read_word_data(new_client, 0);
155 conf = i2c_smbus_read_byte_data(new_client, 1);
156 hyst = i2c_smbus_read_word_data(new_client, 2);
157 os = i2c_smbus_read_word_data(new_client, 3);
158 for (i = 0; i <= 0x1f; i++)
159 if ((i2c_smbus_read_byte_data(new_client, i * 8 + 1) != conf) ||
160 (i2c_smbus_read_word_data(new_client, i * 8 + 2) != hyst) ||
161 (i2c_smbus_read_word_data(new_client, i * 8 + 3) != os))
165 /* Determine the chip type - only one kind supported! */
173 /* Fill in the remaining client fields and put it into the global list */
174 strlcpy(new_client->name, name, I2C_NAME_SIZE);
176 new_client->id = lm75_id++;
178 init_MUTEX(&data->update_lock);
180 /* Tell the I2C layer a new client has arrived */
181 if ((err = i2c_attach_client(new_client)))
184 /* Initialize the LM75 chip */
185 lm75_init_client(new_client);
187 /* Register sysfs hooks */
188 device_create_file(&new_client->dev, &dev_attr_temp1_max);
189 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
190 device_create_file(&new_client->dev, &dev_attr_temp1_input);
200 static int lm75_detach_client(struct i2c_client *client)
202 i2c_detach_client(client);
203 kfree(i2c_get_clientdata(client));
207 /* All registers are word-sized, except for the configuration register.
208 LM75 uses a high-byte first convention, which is exactly opposite to
209 the usual practice. */
210 static int lm75_read_value(struct i2c_client *client, u8 reg)
212 if (reg == LM75_REG_CONF)
213 return i2c_smbus_read_byte_data(client, reg);
215 return swab16(i2c_smbus_read_word_data(client, reg));
218 /* All registers are word-sized, except for the configuration register.
219 LM75 uses a high-byte first convention, which is exactly opposite to
220 the usual practice. */
221 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
223 if (reg == LM75_REG_CONF)
224 return i2c_smbus_write_byte_data(client, reg, value);
226 return i2c_smbus_write_word_data(client, reg, swab16(value));
229 static void lm75_init_client(struct i2c_client *client)
231 /* Initialize the LM75 chip */
232 lm75_write_value(client, LM75_REG_CONF, 0);
235 static struct lm75_data *lm75_update_device(struct device *dev)
237 struct i2c_client *client = to_i2c_client(dev);
238 struct lm75_data *data = i2c_get_clientdata(client);
240 down(&data->update_lock);
242 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
243 (jiffies < data->last_updated) || !data->valid) {
244 dev_dbg(&client->dev, "Starting lm75 update\n");
246 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
247 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
248 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
249 data->last_updated = jiffies;
253 up(&data->update_lock);
258 static int __init sensors_lm75_init(void)
260 return i2c_add_driver(&lm75_driver);
263 static void __exit sensors_lm75_exit(void)
265 i2c_del_driver(&lm75_driver);
268 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
269 MODULE_DESCRIPTION("LM75 driver");
270 MODULE_LICENSE("GPL");
272 module_init(sensors_lm75_init);
273 module_exit(sensors_lm75_exit);