blob: 25ed265841004c0596d34484920aa11e5fd13b5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
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.
10
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.
15
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.
19*/
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040026#include <linux/hwmon.h>
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020027#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040028#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "lm75.h"
31
32
David Brownell01a52392008-04-21 12:10:53 -070033/*
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.
37 */
38
39/* Addresses scanned by legacy style driver binding */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050040static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
David Brownell01a52392008-04-21 12:10:53 -070043/* Insmod parameters (only for legacy style driver binding) */
Jean Delvaref4b50262005-07-31 21:49:03 +020044I2C_CLIENT_INSMOD_1(lm75);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* The LM75 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define LM75_REG_CONF 0x01
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020049static const u8 LM75_REG_TEMP[3] = {
50 0x00, /* input */
51 0x03, /* max */
52 0x02, /* hyst */
53};
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* Each client has this additional data */
56struct lm75_data {
57 struct i2c_client client;
David Brownell01a52392008-04-21 12:10:53 -070058 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010059 struct mutex update_lock;
David Brownell01a52392008-04-21 12:10:53 -070060 char valid; /* !=0 if registers are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 unsigned long last_updated; /* In jiffies */
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020062 u16 temp[3]; /* Register values,
63 0 = input
64 1 = max
65 2 = hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void lm75_init_client(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int lm75_read_value(struct i2c_client *client, u8 reg);
70static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
71static struct lm75_data *lm75_update_device(struct device *dev);
72
73
David Brownell01a52392008-04-21 12:10:53 -070074/*-----------------------------------------------------------------------*/
75
76/* sysfs attributes for hwmon */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020078static ssize_t show_temp(struct device *dev, struct device_attribute *da,
79 char *buf)
80{
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]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020087static ssize_t set_temp(struct device *dev, struct device_attribute *da,
88 const char *buf, size_t count)
89{
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);
93 int nr = attr->index;
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +020094 long temp = simple_strtol(buf, NULL, 10);
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020095
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);
100 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200103static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
104 show_temp, set_temp, 1);
105static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
106 show_temp, set_temp, 2);
107static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200109static struct attribute *lm75_attributes[] = {
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200110 &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,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200113
114 NULL
115};
116
117static const struct attribute_group lm75_group = {
118 .attrs = lm75_attributes,
119};
120
David Brownell01a52392008-04-21 12:10:53 -0700121/*-----------------------------------------------------------------------*/
122
123/* "Legacy" I2C driver binding */
124
125static struct i2c_driver lm75_driver;
126
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200127/* This function is called by i2c_probe */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
129{
130 int i;
131 struct i2c_client *new_client;
132 struct lm75_data *data;
133 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
136 I2C_FUNC_SMBUS_WORD_DATA))
137 goto exit;
138
David Brownell01a52392008-04-21 12:10:53 -0700139 /* 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. */
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200142 if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 err = -ENOMEM;
144 goto exit;
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
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;
153
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. */
161 if (kind < 0) {
162 int cur, conf, hyst, os;
163
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)
David Brownell01a52392008-04-21 12:10:53 -0700172 goto exit_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 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)
David Brownell01a52392008-04-21 12:10:53 -0700178 goto exit_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Unused bits */
181 if (conf & 0xe0)
David Brownell01a52392008-04-21 12:10:53 -0700182 goto exit_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
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)
189 goto exit_free;
190 }
191
David Brownell01a52392008-04-21 12:10:53 -0700192 /* NOTE: we treat "force=..." and "force_lm75=..." the same. */
193 strlcpy(new_client->name, "lm75", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /* Fill in the remaining client fields and put it into the global list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100197 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* Tell the I2C layer a new client has arrived */
200 if ((err = i2c_attach_client(new_client)))
201 goto exit_free;
202
203 /* Initialize the LM75 chip */
204 lm75_init_client(new_client);
David Brownell01a52392008-04-21 12:10:53 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Register sysfs hooks */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200207 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
208 goto exit_detach;
209
Tony Jones1beeffe2007-08-20 13:46:20 -0700210 data->hwmon_dev = hwmon_device_register(&new_client->dev);
211 if (IS_ERR(data->hwmon_dev)) {
212 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200213 goto exit_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200218exit_remove:
219 sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400220exit_detach:
221 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222exit_free:
223 kfree(data);
224exit:
225 return err;
226}
227
David Brownell01a52392008-04-21 12:10:53 -0700228static int lm75_attach_adapter(struct i2c_adapter *adapter)
229{
230 if (!(adapter->class & I2C_CLASS_HWMON))
231 return 0;
232 return i2c_probe(adapter, &addr_data, lm75_detect);
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static int lm75_detach_client(struct i2c_client *client)
236{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400237 struct lm75_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -0700238 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200239 sysfs_remove_group(&client->dev.kobj, &lm75_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400241 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
David Brownell01a52392008-04-21 12:10:53 -0700245static struct i2c_driver lm75_driver = {
246 .driver = {
247 .name = "lm75",
248 },
249 .attach_adapter = lm75_attach_adapter,
250 .detach_client = lm75_detach_client,
251};
252
253/*-----------------------------------------------------------------------*/
254
255/* register access */
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/* All registers are word-sized, except for the configuration register.
258 LM75 uses a high-byte first convention, which is exactly opposite to
Jean Delvareccd6bef2008-02-19 12:42:58 +0100259 the SMBus standard. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int lm75_read_value(struct i2c_client *client, u8 reg)
261{
David Brownellbcccc3a2008-05-03 19:19:16 -0700262 int value;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (reg == LM75_REG_CONF)
265 return i2c_smbus_read_byte_data(client, reg);
David Brownellbcccc3a2008-05-03 19:19:16 -0700266
267 value = i2c_smbus_read_word_data(client, reg);
268 return (value < 0) ? value : swab16(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
272{
273 if (reg == LM75_REG_CONF)
274 return i2c_smbus_write_byte_data(client, reg, value);
275 else
276 return i2c_smbus_write_word_data(client, reg, swab16(value));
277}
278
279static void lm75_init_client(struct i2c_client *client)
280{
Jean Delvaree647ecf2005-07-27 21:28:28 +0200281 int reg;
282
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289static struct lm75_data *lm75_update_device(struct device *dev)
290{
291 struct i2c_client *client = to_i2c_client(dev);
292 struct lm75_data *data = i2c_get_clientdata(client);
293
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100294 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
297 || !data->valid) {
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200298 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 dev_dbg(&client->dev, "Starting lm75 update\n");
300
David Brownellbcccc3a2008-05-03 19:19:16 -0700301 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
302 int status;
303
304 status = lm75_read_value(client, LM75_REG_TEMP[i]);
305 if (status < 0)
306 dev_dbg(&client->dev, "reg %d, err %d\n",
307 LM75_REG_TEMP[i], status);
308 else
309 data->temp[i] = status;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 data->last_updated = jiffies;
312 data->valid = 1;
313 }
314
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return data;
318}
319
David Brownell01a52392008-04-21 12:10:53 -0700320/*-----------------------------------------------------------------------*/
321
322/* module glue */
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static int __init sensors_lm75_init(void)
325{
326 return i2c_add_driver(&lm75_driver);
327}
328
329static void __exit sensors_lm75_exit(void)
330{
331 i2c_del_driver(&lm75_driver);
332}
333
334MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
335MODULE_DESCRIPTION("LM75 driver");
336MODULE_LICENSE("GPL");
337
338module_init(sensors_lm75_init);
339module_exit(sensors_lm75_exit);