blob: 57c51ac37c04b121a66d09247bb8ddf15d513d68 [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
21#include <linux/config.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
27#include <linux/i2c-sensor.h>
28#include "lm75.h"
29
30
31/* Addresses to scan */
32static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
33 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
34static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
35
36/* Insmod parameters */
37SENSORS_INSMOD_1(lm75);
38
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;
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 */
54 u16 temp_max;
55 u16 temp_hyst;
56};
57
58static int lm75_attach_adapter(struct i2c_adapter *adapter);
59static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
60static void lm75_init_client(struct i2c_client *client);
61static int lm75_detach_client(struct i2c_client *client);
62static int lm75_read_value(struct i2c_client *client, u8 reg);
63static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
64static struct lm75_data *lm75_update_device(struct device *dev);
65
66
67/* This is the driver that will be inserted */
68static struct i2c_driver lm75_driver = {
69 .owner = THIS_MODULE,
70 .name = "lm75",
71 .id = I2C_DRIVERID_LM75,
72 .flags = I2C_DF_NOTIFY,
73 .attach_adapter = lm75_attach_adapter,
74 .detach_client = lm75_detach_client,
75};
76
77#define show(value) \
Yani Ioannou30f74292005-05-17 06:41:35 -040078static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{ \
80 struct lm75_data *data = lm75_update_device(dev); \
81 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
82}
83show(temp_max);
84show(temp_hyst);
85show(temp_input);
86
87#define set(value, reg) \
Yani Ioannou30f74292005-05-17 06:41:35 -040088static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{ \
90 struct i2c_client *client = to_i2c_client(dev); \
91 struct lm75_data *data = i2c_get_clientdata(client); \
92 int temp = simple_strtoul(buf, NULL, 10); \
93 \
94 down(&data->update_lock); \
95 data->value = LM75_TEMP_TO_REG(temp); \
96 lm75_write_value(client, reg, data->value); \
97 up(&data->update_lock); \
98 return count; \
99}
100set(temp_max, LM75_REG_TEMP_OS);
101set(temp_hyst, LM75_REG_TEMP_HYST);
102
103static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
104static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
105static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
106
107static int lm75_attach_adapter(struct i2c_adapter *adapter)
108{
109 if (!(adapter->class & I2C_CLASS_HWMON))
110 return 0;
111 return i2c_detect(adapter, &addr_data, lm75_detect);
112}
113
114/* This function is called by i2c_detect */
115static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
116{
117 int i;
118 struct i2c_client *new_client;
119 struct lm75_data *data;
120 int err = 0;
121 const char *name = "";
122
123 /* Make sure we aren't probing the ISA bus!! This is just a safety check
124 at this moment; i2c_detect really won't call us. */
125#ifdef DEBUG
126 if (i2c_is_isa_adapter(adapter)) {
127 dev_dbg(&adapter->dev,
128 "lm75_detect called for an ISA bus adapter?!?\n");
129 goto exit;
130 }
131#endif
132
133 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
134 I2C_FUNC_SMBUS_WORD_DATA))
135 goto exit;
136
137 /* OK. For now, we presume we have a valid client. We now create the
138 client structure, even though we cannot fill it completely yet.
139 But it allows us to access lm75_{read,write}_value. */
140 if (!(data = kmalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
141 err = -ENOMEM;
142 goto exit;
143 }
144 memset(data, 0, sizeof(struct lm75_data));
145
146 new_client = &data->client;
147 i2c_set_clientdata(new_client, data);
148 new_client->addr = address;
149 new_client->adapter = adapter;
150 new_client->driver = &lm75_driver;
151 new_client->flags = 0;
152
153 /* Now, we do the remaining detection. There is no identification-
154 dedicated register so we have to rely on several tricks:
155 unused bits, registers cycling over 8-address boundaries,
156 addresses 0x04-0x07 returning the last read value.
157 The cycling+unused addresses combination is not tested,
158 since it would significantly slow the detection down and would
159 hardly add any value. */
160 if (kind < 0) {
161 int cur, conf, hyst, os;
162
163 /* Unused addresses */
164 cur = i2c_smbus_read_word_data(new_client, 0);
165 conf = i2c_smbus_read_byte_data(new_client, 1);
166 hyst = i2c_smbus_read_word_data(new_client, 2);
167 if (i2c_smbus_read_word_data(new_client, 4) != hyst
168 || i2c_smbus_read_word_data(new_client, 5) != hyst
169 || i2c_smbus_read_word_data(new_client, 6) != hyst
170 || i2c_smbus_read_word_data(new_client, 7) != hyst)
171 goto exit_free;
172 os = i2c_smbus_read_word_data(new_client, 3);
173 if (i2c_smbus_read_word_data(new_client, 4) != os
174 || i2c_smbus_read_word_data(new_client, 5) != os
175 || i2c_smbus_read_word_data(new_client, 6) != os
176 || i2c_smbus_read_word_data(new_client, 7) != os)
177 goto exit_free;
178
179 /* Unused bits */
180 if (conf & 0xe0)
181 goto exit_free;
182
183 /* Addresses cycling */
184 for (i = 8; i < 0xff; i += 8)
185 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
186 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
187 || i2c_smbus_read_word_data(new_client, i + 3) != os)
188 goto exit_free;
189 }
190
191 /* Determine the chip type - only one kind supported! */
192 if (kind <= 0)
193 kind = lm75;
194
195 if (kind == lm75) {
196 name = "lm75";
197 }
198
199 /* Fill in the remaining client fields and put it into the global list */
200 strlcpy(new_client->name, name, I2C_NAME_SIZE);
201 data->valid = 0;
202 init_MUTEX(&data->update_lock);
203
204 /* Tell the I2C layer a new client has arrived */
205 if ((err = i2c_attach_client(new_client)))
206 goto exit_free;
207
208 /* Initialize the LM75 chip */
209 lm75_init_client(new_client);
210
211 /* Register sysfs hooks */
212 device_create_file(&new_client->dev, &dev_attr_temp1_max);
213 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
214 device_create_file(&new_client->dev, &dev_attr_temp1_input);
215
216 return 0;
217
218exit_free:
219 kfree(data);
220exit:
221 return err;
222}
223
224static int lm75_detach_client(struct i2c_client *client)
225{
226 i2c_detach_client(client);
227 kfree(i2c_get_clientdata(client));
228 return 0;
229}
230
231/* All registers are word-sized, except for the configuration register.
232 LM75 uses a high-byte first convention, which is exactly opposite to
233 the usual practice. */
234static int lm75_read_value(struct i2c_client *client, u8 reg)
235{
236 if (reg == LM75_REG_CONF)
237 return i2c_smbus_read_byte_data(client, reg);
238 else
239 return swab16(i2c_smbus_read_word_data(client, reg));
240}
241
242/* All registers are word-sized, except for the configuration register.
243 LM75 uses a high-byte first convention, which is exactly opposite to
244 the usual practice. */
245static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
246{
247 if (reg == LM75_REG_CONF)
248 return i2c_smbus_write_byte_data(client, reg, value);
249 else
250 return i2c_smbus_write_word_data(client, reg, swab16(value));
251}
252
253static void lm75_init_client(struct i2c_client *client)
254{
255 /* Initialize the LM75 chip */
256 lm75_write_value(client, LM75_REG_CONF, 0);
257}
258
259static struct lm75_data *lm75_update_device(struct device *dev)
260{
261 struct i2c_client *client = to_i2c_client(dev);
262 struct lm75_data *data = i2c_get_clientdata(client);
263
264 down(&data->update_lock);
265
266 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
267 || !data->valid) {
268 dev_dbg(&client->dev, "Starting lm75 update\n");
269
270 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
271 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
272 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
273 data->last_updated = jiffies;
274 data->valid = 1;
275 }
276
277 up(&data->update_lock);
278
279 return data;
280}
281
282static int __init sensors_lm75_init(void)
283{
284 return i2c_add_driver(&lm75_driver);
285}
286
287static void __exit sensors_lm75_exit(void)
288{
289 i2c_del_driver(&lm75_driver);
290}
291
292MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
293MODULE_DESCRIPTION("LM75 driver");
294MODULE_LICENSE("GPL");
295
296module_init(sensors_lm75_init);
297module_exit(sensors_lm75_exit);