blob: de698dc73020ccdf7edf88d8697fa1740c49c67b [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
33/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050034static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020038I2C_CLIENT_INSMOD_1(lm75);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Many LM75 constants specified below */
41
42/* The LM75 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define LM75_REG_CONF 0x01
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020044static const u8 LM75_REG_TEMP[3] = {
45 0x00, /* input */
46 0x03, /* max */
47 0x02, /* hyst */
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Each client has this additional data */
51struct lm75_data {
52 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -070053 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010054 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char valid; /* !=0 if following fields are valid */
56 unsigned long last_updated; /* In jiffies */
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020057 u16 temp[3]; /* Register values,
58 0 = input
59 1 = max
60 2 = hyst */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63static int lm75_attach_adapter(struct i2c_adapter *adapter);
64static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
65static void lm75_init_client(struct i2c_client *client);
66static int lm75_detach_client(struct i2c_client *client);
67static int lm75_read_value(struct i2c_client *client, u8 reg);
68static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
69static struct lm75_data *lm75_update_device(struct device *dev);
70
71
72/* This is the driver that will be inserted */
73static struct i2c_driver lm75_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010074 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010075 .name = "lm75",
76 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .attach_adapter = lm75_attach_adapter,
78 .detach_client = lm75_detach_client,
79};
80
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020081static ssize_t show_temp(struct device *dev, struct device_attribute *da,
82 char *buf)
83{
84 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
85 struct lm75_data *data = lm75_update_device(dev);
86 return sprintf(buf, "%d\n",
87 LM75_TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020090static ssize_t set_temp(struct device *dev, struct device_attribute *da,
91 const char *buf, size_t count)
92{
93 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
94 struct i2c_client *client = to_i2c_client(dev);
95 struct lm75_data *data = i2c_get_clientdata(client);
96 int nr = attr->index;
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +020097 long temp = simple_strtol(buf, NULL, 10);
Jean Delvare9ca8e40c82007-05-08 17:22:01 +020098
99 mutex_lock(&data->update_lock);
100 data->temp[nr] = LM75_TEMP_TO_REG(temp);
101 lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
102 mutex_unlock(&data->update_lock);
103 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200106static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
107 show_temp, set_temp, 1);
108static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
109 show_temp, set_temp, 2);
110static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112static int lm75_attach_adapter(struct i2c_adapter *adapter)
113{
114 if (!(adapter->class & I2C_CLASS_HWMON))
115 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200116 return i2c_probe(adapter, &addr_data, lm75_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200119static struct attribute *lm75_attributes[] = {
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200120 &sensor_dev_attr_temp1_input.dev_attr.attr,
121 &sensor_dev_attr_temp1_max.dev_attr.attr,
122 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200123
124 NULL
125};
126
127static const struct attribute_group lm75_group = {
128 .attrs = lm75_attributes,
129};
130
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200131/* This function is called by i2c_probe */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
133{
134 int i;
135 struct i2c_client *new_client;
136 struct lm75_data *data;
137 int err = 0;
138 const char *name = "";
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
141 I2C_FUNC_SMBUS_WORD_DATA))
142 goto exit;
143
144 /* OK. For now, we presume we have a valid client. We now create the
145 client structure, even though we cannot fill it completely yet.
146 But it allows us to access lm75_{read,write}_value. */
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200147 if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 err = -ENOMEM;
149 goto exit;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 new_client = &data->client;
153 i2c_set_clientdata(new_client, data);
154 new_client->addr = address;
155 new_client->adapter = adapter;
156 new_client->driver = &lm75_driver;
157 new_client->flags = 0;
158
159 /* Now, we do the remaining detection. There is no identification-
160 dedicated register so we have to rely on several tricks:
161 unused bits, registers cycling over 8-address boundaries,
162 addresses 0x04-0x07 returning the last read value.
163 The cycling+unused addresses combination is not tested,
164 since it would significantly slow the detection down and would
165 hardly add any value. */
166 if (kind < 0) {
167 int cur, conf, hyst, os;
168
169 /* Unused addresses */
170 cur = i2c_smbus_read_word_data(new_client, 0);
171 conf = i2c_smbus_read_byte_data(new_client, 1);
172 hyst = i2c_smbus_read_word_data(new_client, 2);
173 if (i2c_smbus_read_word_data(new_client, 4) != hyst
174 || i2c_smbus_read_word_data(new_client, 5) != hyst
175 || i2c_smbus_read_word_data(new_client, 6) != hyst
176 || i2c_smbus_read_word_data(new_client, 7) != hyst)
177 goto exit_free;
178 os = i2c_smbus_read_word_data(new_client, 3);
179 if (i2c_smbus_read_word_data(new_client, 4) != os
180 || i2c_smbus_read_word_data(new_client, 5) != os
181 || i2c_smbus_read_word_data(new_client, 6) != os
182 || i2c_smbus_read_word_data(new_client, 7) != os)
183 goto exit_free;
184
185 /* Unused bits */
186 if (conf & 0xe0)
187 goto exit_free;
188
189 /* Addresses cycling */
190 for (i = 8; i < 0xff; i += 8)
191 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
192 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
193 || i2c_smbus_read_word_data(new_client, i + 3) != os)
194 goto exit_free;
195 }
196
197 /* Determine the chip type - only one kind supported! */
198 if (kind <= 0)
199 kind = lm75;
200
201 if (kind == lm75) {
202 name = "lm75";
203 }
204
205 /* Fill in the remaining client fields and put it into the global list */
206 strlcpy(new_client->name, name, I2C_NAME_SIZE);
207 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100208 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* Tell the I2C layer a new client has arrived */
211 if ((err = i2c_attach_client(new_client)))
212 goto exit_free;
213
214 /* Initialize the LM75 chip */
215 lm75_init_client(new_client);
216
217 /* Register sysfs hooks */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200218 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
219 goto exit_detach;
220
Tony Jones1beeffe2007-08-20 13:46:20 -0700221 data->hwmon_dev = hwmon_device_register(&new_client->dev);
222 if (IS_ERR(data->hwmon_dev)) {
223 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200224 goto exit_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400225 }
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200229exit_remove:
230 sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400231exit_detach:
232 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233exit_free:
234 kfree(data);
235exit:
236 return err;
237}
238
239static int lm75_detach_client(struct i2c_client *client)
240{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400241 struct lm75_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -0700242 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200243 sysfs_remove_group(&client->dev.kobj, &lm75_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400245 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
247}
248
249/* All registers are word-sized, except for the configuration register.
250 LM75 uses a high-byte first convention, which is exactly opposite to
Jean Delvareccd6bef2008-02-19 12:42:58 +0100251 the SMBus standard. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int lm75_read_value(struct i2c_client *client, u8 reg)
253{
David Brownellbcccc3a2008-05-03 19:19:16 -0700254 int value;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (reg == LM75_REG_CONF)
257 return i2c_smbus_read_byte_data(client, reg);
David Brownellbcccc3a2008-05-03 19:19:16 -0700258
259 value = i2c_smbus_read_word_data(client, reg);
260 return (value < 0) ? value : swab16(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
264{
265 if (reg == LM75_REG_CONF)
266 return i2c_smbus_write_byte_data(client, reg, value);
267 else
268 return i2c_smbus_write_word_data(client, reg, swab16(value));
269}
270
271static void lm75_init_client(struct i2c_client *client)
272{
Jean Delvaree647ecf2005-07-27 21:28:28 +0200273 int reg;
274
275 /* Enable if in shutdown mode */
276 reg = lm75_read_value(client, LM75_REG_CONF);
277 if (reg >= 0 && (reg & 0x01))
278 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static struct lm75_data *lm75_update_device(struct device *dev)
282{
283 struct i2c_client *client = to_i2c_client(dev);
284 struct lm75_data *data = i2c_get_clientdata(client);
285
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100286 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
289 || !data->valid) {
Jean Delvare9ca8e40c82007-05-08 17:22:01 +0200290 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dev_dbg(&client->dev, "Starting lm75 update\n");
292
David Brownellbcccc3a2008-05-03 19:19:16 -0700293 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
294 int status;
295
296 status = lm75_read_value(client, LM75_REG_TEMP[i]);
297 if (status < 0)
298 dev_dbg(&client->dev, "reg %d, err %d\n",
299 LM75_REG_TEMP[i], status);
300 else
301 data->temp[i] = status;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 data->last_updated = jiffies;
304 data->valid = 1;
305 }
306
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100307 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 return data;
310}
311
312static int __init sensors_lm75_init(void)
313{
314 return i2c_add_driver(&lm75_driver);
315}
316
317static void __exit sensors_lm75_exit(void)
318{
319 i2c_del_driver(&lm75_driver);
320}
321
322MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
323MODULE_DESCRIPTION("LM75 driver");
324MODULE_LICENSE("GPL");
325
326module_init(sensors_lm75_init);
327module_exit(sensors_lm75_exit);