blob: 7c65b8bb6d721498c9ac1e0f2dc968bb1b2f4744 [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>
27#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "lm75.h"
30
31
32/* Addresses to scan */
33static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
34 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020037I2C_CLIENT_INSMOD_1(lm75);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* Many LM75 constants specified below */
40
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
46
47/* Each client has this additional data */
48struct lm75_data {
49 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -040050 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010051 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 char valid; /* !=0 if following fields are valid */
53 unsigned long last_updated; /* In jiffies */
54 u16 temp_input; /* Register values */
55 u16 temp_max;
56 u16 temp_hyst;
57};
58
59static int lm75_attach_adapter(struct i2c_adapter *adapter);
60static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
61static void lm75_init_client(struct i2c_client *client);
62static int lm75_detach_client(struct i2c_client *client);
63static int lm75_read_value(struct i2c_client *client, u8 reg);
64static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
65static struct lm75_data *lm75_update_device(struct device *dev);
66
67
68/* This is the driver that will be inserted */
69static struct i2c_driver lm75_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010070 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010071 .name = "lm75",
72 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .id = I2C_DRIVERID_LM75,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .attach_adapter = lm75_attach_adapter,
75 .detach_client = lm75_detach_client,
76};
77
78#define show(value) \
Yani Ioannou30f74292005-05-17 06:41:35 -040079static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{ \
81 struct lm75_data *data = lm75_update_device(dev); \
82 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
83}
84show(temp_max);
85show(temp_hyst);
86show(temp_input);
87
88#define set(value, reg) \
Yani Ioannou30f74292005-05-17 06:41:35 -040089static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{ \
91 struct i2c_client *client = to_i2c_client(dev); \
92 struct lm75_data *data = i2c_get_clientdata(client); \
93 int temp = simple_strtoul(buf, NULL, 10); \
94 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +010095 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 data->value = LM75_TEMP_TO_REG(temp); \
97 lm75_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +010098 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return count; \
100}
101set(temp_max, LM75_REG_TEMP_OS);
102set(temp_hyst, LM75_REG_TEMP_HYST);
103
104static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
105static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
106static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
107
108static int lm75_attach_adapter(struct i2c_adapter *adapter)
109{
110 if (!(adapter->class & I2C_CLASS_HWMON))
111 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200112 return i2c_probe(adapter, &addr_data, lm75_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200115static struct attribute *lm75_attributes[] = {
116 &dev_attr_temp1_input.attr,
117 &dev_attr_temp1_max.attr,
118 &dev_attr_temp1_max_hyst.attr,
119
120 NULL
121};
122
123static const struct attribute_group lm75_group = {
124 .attrs = lm75_attributes,
125};
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;
134 const char *name = "";
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
137 I2C_FUNC_SMBUS_WORD_DATA))
138 goto exit;
139
140 /* OK. For now, we presume we have a valid client. We now create the
141 client structure, even though we cannot fill it completely yet.
142 But it allows us to access lm75_{read,write}_value. */
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200143 if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 err = -ENOMEM;
145 goto exit;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 new_client = &data->client;
149 i2c_set_clientdata(new_client, data);
150 new_client->addr = address;
151 new_client->adapter = adapter;
152 new_client->driver = &lm75_driver;
153 new_client->flags = 0;
154
155 /* Now, we do the remaining detection. There is no identification-
156 dedicated register so we have to rely on several tricks:
157 unused bits, registers cycling over 8-address boundaries,
158 addresses 0x04-0x07 returning the last read value.
159 The cycling+unused addresses combination is not tested,
160 since it would significantly slow the detection down and would
161 hardly add any value. */
162 if (kind < 0) {
163 int cur, conf, hyst, os;
164
165 /* Unused addresses */
166 cur = i2c_smbus_read_word_data(new_client, 0);
167 conf = i2c_smbus_read_byte_data(new_client, 1);
168 hyst = i2c_smbus_read_word_data(new_client, 2);
169 if (i2c_smbus_read_word_data(new_client, 4) != hyst
170 || i2c_smbus_read_word_data(new_client, 5) != hyst
171 || i2c_smbus_read_word_data(new_client, 6) != hyst
172 || i2c_smbus_read_word_data(new_client, 7) != hyst)
173 goto exit_free;
174 os = i2c_smbus_read_word_data(new_client, 3);
175 if (i2c_smbus_read_word_data(new_client, 4) != os
176 || i2c_smbus_read_word_data(new_client, 5) != os
177 || i2c_smbus_read_word_data(new_client, 6) != os
178 || i2c_smbus_read_word_data(new_client, 7) != os)
179 goto exit_free;
180
181 /* Unused bits */
182 if (conf & 0xe0)
183 goto exit_free;
184
185 /* Addresses cycling */
186 for (i = 8; i < 0xff; i += 8)
187 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
188 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
189 || i2c_smbus_read_word_data(new_client, i + 3) != os)
190 goto exit_free;
191 }
192
193 /* Determine the chip type - only one kind supported! */
194 if (kind <= 0)
195 kind = lm75;
196
197 if (kind == lm75) {
198 name = "lm75";
199 }
200
201 /* Fill in the remaining client fields and put it into the global list */
202 strlcpy(new_client->name, name, I2C_NAME_SIZE);
203 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100204 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Tell the I2C layer a new client has arrived */
207 if ((err = i2c_attach_client(new_client)))
208 goto exit_free;
209
210 /* Initialize the LM75 chip */
211 lm75_init_client(new_client);
212
213 /* Register sysfs hooks */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200214 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
215 goto exit_detach;
216
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400217 data->class_dev = hwmon_device_register(&new_client->dev);
218 if (IS_ERR(data->class_dev)) {
219 err = PTR_ERR(data->class_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200220 goto exit_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200225exit_remove:
226 sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400227exit_detach:
228 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229exit_free:
230 kfree(data);
231exit:
232 return err;
233}
234
235static 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);
238 hwmon_device_unregister(data->class_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
245/* All registers are word-sized, except for the configuration register.
246 LM75 uses a high-byte first convention, which is exactly opposite to
247 the usual practice. */
248static int lm75_read_value(struct i2c_client *client, u8 reg)
249{
250 if (reg == LM75_REG_CONF)
251 return i2c_smbus_read_byte_data(client, reg);
252 else
253 return swab16(i2c_smbus_read_word_data(client, reg));
254}
255
256/* All registers are word-sized, except for the configuration register.
257 LM75 uses a high-byte first convention, which is exactly opposite to
258 the usual practice. */
259static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
260{
261 if (reg == LM75_REG_CONF)
262 return i2c_smbus_write_byte_data(client, reg, value);
263 else
264 return i2c_smbus_write_word_data(client, reg, swab16(value));
265}
266
267static void lm75_init_client(struct i2c_client *client)
268{
Jean Delvaree647ecf2005-07-27 21:28:28 +0200269 int reg;
270
271 /* Enable if in shutdown mode */
272 reg = lm75_read_value(client, LM75_REG_CONF);
273 if (reg >= 0 && (reg & 0x01))
274 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277static struct lm75_data *lm75_update_device(struct device *dev)
278{
279 struct i2c_client *client = to_i2c_client(dev);
280 struct lm75_data *data = i2c_get_clientdata(client);
281
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100282 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
285 || !data->valid) {
286 dev_dbg(&client->dev, "Starting lm75 update\n");
287
288 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
289 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
290 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
291 data->last_updated = jiffies;
292 data->valid = 1;
293 }
294
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100295 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 return data;
298}
299
300static int __init sensors_lm75_init(void)
301{
302 return i2c_add_driver(&lm75_driver);
303}
304
305static void __exit sensors_lm75_exit(void)
306{
307 i2c_del_driver(&lm75_driver);
308}
309
310MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
311MODULE_DESCRIPTION("LM75 driver");
312MODULE_LICENSE("GPL");
313
314module_init(sensors_lm75_init);
315module_exit(sensors_lm75_exit);