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/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
34 * This driver handles the LM75 and compatible digital temperature sensors.
35 * Compatibles include at least the DS75, DS1775, MCP980x, STDS75, TCN75,
36 * TMP100, TMP101, TMP75, TMP175, and TMP275.
39 /* Addresses scanned by legacy style driver binding */
40 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
41 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
43 /* Insmod parameters (only for legacy style driver binding) */
44 I2C_CLIENT_INSMOD_1(lm75);
47 /* The LM75 registers */
48 #define LM75_REG_CONF 0x01
49 static const u8 LM75_REG_TEMP[3] = {
55 /* Each client has this additional data */
57 struct i2c_client client;
58 struct device *hwmon_dev;
59 struct mutex update_lock;
60 char valid; /* !=0 if registers are valid */
61 unsigned long last_updated; /* In jiffies */
62 u16 temp[3]; /* Register values,
68 static void lm75_init_client(struct i2c_client *client);
69 static int lm75_read_value(struct i2c_client *client, u8 reg);
70 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
71 static struct lm75_data *lm75_update_device(struct device *dev);
74 /*-----------------------------------------------------------------------*/
76 /* sysfs attributes for hwmon */
78 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
81 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
82 struct lm75_data *data = lm75_update_device(dev);
83 return sprintf(buf, "%d\n",
84 LM75_TEMP_FROM_REG(data->temp[attr->index]));
87 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
88 const char *buf, size_t count)
90 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
91 struct i2c_client *client = to_i2c_client(dev);
92 struct lm75_data *data = i2c_get_clientdata(client);
94 long temp = simple_strtol(buf, NULL, 10);
96 mutex_lock(&data->update_lock);
97 data->temp[nr] = LM75_TEMP_TO_REG(temp);
98 lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
99 mutex_unlock(&data->update_lock);
103 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
104 show_temp, set_temp, 1);
105 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
106 show_temp, set_temp, 2);
107 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
109 static struct attribute *lm75_attributes[] = {
110 &sensor_dev_attr_temp1_input.dev_attr.attr,
111 &sensor_dev_attr_temp1_max.dev_attr.attr,
112 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
117 static const struct attribute_group lm75_group = {
118 .attrs = lm75_attributes,
121 /*-----------------------------------------------------------------------*/
123 /* "Legacy" I2C driver binding */
125 static struct i2c_driver lm75_driver;
127 /* This function is called by i2c_probe */
128 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
131 struct i2c_client *new_client;
132 struct lm75_data *data;
135 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
136 I2C_FUNC_SMBUS_WORD_DATA))
139 /* OK. For now, we presume we have a valid address. We create the
140 client structure, even though there may be no sensor present.
141 But it allows us to use i2c_smbus_read_*_data() calls. */
142 if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
147 new_client = &data->client;
148 i2c_set_clientdata(new_client, data);
149 new_client->addr = address;
150 new_client->adapter = adapter;
151 new_client->driver = &lm75_driver;
152 new_client->flags = 0;
154 /* Now, we do the remaining detection. There is no identification-
155 dedicated register so we have to rely on several tricks:
156 unused bits, registers cycling over 8-address boundaries,
157 addresses 0x04-0x07 returning the last read value.
158 The cycling+unused addresses combination is not tested,
159 since it would significantly slow the detection down and would
160 hardly add any value. */
162 int cur, conf, hyst, os;
164 /* Unused addresses */
165 cur = i2c_smbus_read_word_data(new_client, 0);
166 conf = i2c_smbus_read_byte_data(new_client, 1);
167 hyst = i2c_smbus_read_word_data(new_client, 2);
168 if (i2c_smbus_read_word_data(new_client, 4) != hyst
169 || i2c_smbus_read_word_data(new_client, 5) != hyst
170 || i2c_smbus_read_word_data(new_client, 6) != hyst
171 || i2c_smbus_read_word_data(new_client, 7) != hyst)
173 os = i2c_smbus_read_word_data(new_client, 3);
174 if (i2c_smbus_read_word_data(new_client, 4) != os
175 || i2c_smbus_read_word_data(new_client, 5) != os
176 || i2c_smbus_read_word_data(new_client, 6) != os
177 || i2c_smbus_read_word_data(new_client, 7) != os)
184 /* Addresses cycling */
185 for (i = 8; i < 0xff; i += 8)
186 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
187 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
188 || i2c_smbus_read_word_data(new_client, i + 3) != os)
192 /* NOTE: we treat "force=..." and "force_lm75=..." the same. */
193 strlcpy(new_client->name, "lm75", I2C_NAME_SIZE);
195 /* Fill in the remaining client fields and put it into the global list */
197 mutex_init(&data->update_lock);
199 /* Tell the I2C layer a new client has arrived */
200 if ((err = i2c_attach_client(new_client)))
203 /* Initialize the LM75 chip */
204 lm75_init_client(new_client);
206 /* Register sysfs hooks */
207 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
210 data->hwmon_dev = hwmon_device_register(&new_client->dev);
211 if (IS_ERR(data->hwmon_dev)) {
212 err = PTR_ERR(data->hwmon_dev);
219 sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
221 i2c_detach_client(new_client);
228 static int lm75_attach_adapter(struct i2c_adapter *adapter)
230 if (!(adapter->class & I2C_CLASS_HWMON))
232 return i2c_probe(adapter, &addr_data, lm75_detect);
235 static int lm75_detach_client(struct i2c_client *client)
237 struct lm75_data *data = i2c_get_clientdata(client);
238 hwmon_device_unregister(data->hwmon_dev);
239 sysfs_remove_group(&client->dev.kobj, &lm75_group);
240 i2c_detach_client(client);
245 static struct i2c_driver lm75_driver = {
249 .attach_adapter = lm75_attach_adapter,
250 .detach_client = lm75_detach_client,
253 /*-----------------------------------------------------------------------*/
255 /* register access */
257 /* All registers are word-sized, except for the configuration register.
258 LM75 uses a high-byte first convention, which is exactly opposite to
259 the SMBus standard. */
260 static int lm75_read_value(struct i2c_client *client, u8 reg)
264 if (reg == LM75_REG_CONF)
265 return i2c_smbus_read_byte_data(client, reg);
267 value = i2c_smbus_read_word_data(client, reg);
268 return (value < 0) ? value : swab16(value);
271 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
273 if (reg == LM75_REG_CONF)
274 return i2c_smbus_write_byte_data(client, reg, value);
276 return i2c_smbus_write_word_data(client, reg, swab16(value));
279 static void lm75_init_client(struct i2c_client *client)
283 /* Enable if in shutdown mode */
284 reg = lm75_read_value(client, LM75_REG_CONF);
285 if (reg >= 0 && (reg & 0x01))
286 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
289 static struct lm75_data *lm75_update_device(struct device *dev)
291 struct i2c_client *client = to_i2c_client(dev);
292 struct lm75_data *data = i2c_get_clientdata(client);
294 mutex_lock(&data->update_lock);
296 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
299 dev_dbg(&client->dev, "Starting lm75 update\n");
301 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
304 status = lm75_read_value(client, LM75_REG_TEMP[i]);
306 dev_dbg(&client->dev, "reg %d, err %d\n",
307 LM75_REG_TEMP[i], status);
309 data->temp[i] = status;
311 data->last_updated = jiffies;
315 mutex_unlock(&data->update_lock);
320 /*-----------------------------------------------------------------------*/
324 static int __init sensors_lm75_init(void)
326 return i2c_add_driver(&lm75_driver);
329 static void __exit sensors_lm75_exit(void)
331 i2c_del_driver(&lm75_driver);
334 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
335 MODULE_DESCRIPTION("LM75 driver");
336 MODULE_LICENSE("GPL");
338 module_init(sensors_lm75_init);
339 module_exit(sensors_lm75_exit);