[PATCH] hwmon: hwmon vs i2c, second round (05/11)
[deliverable/linux.git] / drivers / hwmon / lm75.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
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/i2c-sensor.h>
943b0830
MH
27#include <linux/hwmon.h>
28#include <linux/err.h>
1da177e4
LT
29#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 };
1da177e4
LT
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;
943b0830 50 struct class_device *class_dev;
1da177e4
LT
51 struct semaphore update_lock;
52 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 = {
70 .owner = THIS_MODULE,
71 .name = "lm75",
72 .id = I2C_DRIVERID_LM75,
73 .flags = I2C_DF_NOTIFY,
74 .attach_adapter = lm75_attach_adapter,
75 .detach_client = lm75_detach_client,
76};
77
78#define show(value) \
30f74292 79static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
80{ \
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) \
30f74292 89static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
1da177e4
LT
90{ \
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 \
95 down(&data->update_lock); \
96 data->value = LM75_TEMP_TO_REG(temp); \
97 lm75_write_value(client, reg, data->value); \
98 up(&data->update_lock); \
99 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;
2ed2dc3c 112 return i2c_probe(adapter, &addr_data, lm75_detect);
1da177e4
LT
113}
114
2ed2dc3c 115/* This function is called by i2c_probe */
1da177e4
LT
116static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
117{
118 int i;
119 struct i2c_client *new_client;
120 struct lm75_data *data;
121 int err = 0;
122 const char *name = "";
123
1da177e4
LT
124 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
125 I2C_FUNC_SMBUS_WORD_DATA))
126 goto exit;
127
128 /* OK. For now, we presume we have a valid client. We now create the
129 client structure, even though we cannot fill it completely yet.
130 But it allows us to access lm75_{read,write}_value. */
131 if (!(data = kmalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
132 err = -ENOMEM;
133 goto exit;
134 }
135 memset(data, 0, sizeof(struct lm75_data));
136
137 new_client = &data->client;
138 i2c_set_clientdata(new_client, data);
139 new_client->addr = address;
140 new_client->adapter = adapter;
141 new_client->driver = &lm75_driver;
142 new_client->flags = 0;
143
144 /* Now, we do the remaining detection. There is no identification-
145 dedicated register so we have to rely on several tricks:
146 unused bits, registers cycling over 8-address boundaries,
147 addresses 0x04-0x07 returning the last read value.
148 The cycling+unused addresses combination is not tested,
149 since it would significantly slow the detection down and would
150 hardly add any value. */
151 if (kind < 0) {
152 int cur, conf, hyst, os;
153
154 /* Unused addresses */
155 cur = i2c_smbus_read_word_data(new_client, 0);
156 conf = i2c_smbus_read_byte_data(new_client, 1);
157 hyst = i2c_smbus_read_word_data(new_client, 2);
158 if (i2c_smbus_read_word_data(new_client, 4) != hyst
159 || i2c_smbus_read_word_data(new_client, 5) != hyst
160 || i2c_smbus_read_word_data(new_client, 6) != hyst
161 || i2c_smbus_read_word_data(new_client, 7) != hyst)
162 goto exit_free;
163 os = i2c_smbus_read_word_data(new_client, 3);
164 if (i2c_smbus_read_word_data(new_client, 4) != os
165 || i2c_smbus_read_word_data(new_client, 5) != os
166 || i2c_smbus_read_word_data(new_client, 6) != os
167 || i2c_smbus_read_word_data(new_client, 7) != os)
168 goto exit_free;
169
170 /* Unused bits */
171 if (conf & 0xe0)
172 goto exit_free;
173
174 /* Addresses cycling */
175 for (i = 8; i < 0xff; i += 8)
176 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
177 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
178 || i2c_smbus_read_word_data(new_client, i + 3) != os)
179 goto exit_free;
180 }
181
182 /* Determine the chip type - only one kind supported! */
183 if (kind <= 0)
184 kind = lm75;
185
186 if (kind == lm75) {
187 name = "lm75";
188 }
189
190 /* Fill in the remaining client fields and put it into the global list */
191 strlcpy(new_client->name, name, I2C_NAME_SIZE);
192 data->valid = 0;
193 init_MUTEX(&data->update_lock);
194
195 /* Tell the I2C layer a new client has arrived */
196 if ((err = i2c_attach_client(new_client)))
197 goto exit_free;
198
199 /* Initialize the LM75 chip */
200 lm75_init_client(new_client);
201
202 /* Register sysfs hooks */
943b0830
MH
203 data->class_dev = hwmon_device_register(&new_client->dev);
204 if (IS_ERR(data->class_dev)) {
205 err = PTR_ERR(data->class_dev);
206 goto exit_detach;
207 }
208
1da177e4
LT
209 device_create_file(&new_client->dev, &dev_attr_temp1_max);
210 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
211 device_create_file(&new_client->dev, &dev_attr_temp1_input);
212
213 return 0;
214
943b0830
MH
215exit_detach:
216 i2c_detach_client(new_client);
1da177e4
LT
217exit_free:
218 kfree(data);
219exit:
220 return err;
221}
222
223static int lm75_detach_client(struct i2c_client *client)
224{
943b0830
MH
225 struct lm75_data *data = i2c_get_clientdata(client);
226 hwmon_device_unregister(data->class_dev);
1da177e4 227 i2c_detach_client(client);
943b0830 228 kfree(data);
1da177e4
LT
229 return 0;
230}
231
232/* All registers are word-sized, except for the configuration register.
233 LM75 uses a high-byte first convention, which is exactly opposite to
234 the usual practice. */
235static int lm75_read_value(struct i2c_client *client, u8 reg)
236{
237 if (reg == LM75_REG_CONF)
238 return i2c_smbus_read_byte_data(client, reg);
239 else
240 return swab16(i2c_smbus_read_word_data(client, reg));
241}
242
243/* All registers are word-sized, except for the configuration register.
244 LM75 uses a high-byte first convention, which is exactly opposite to
245 the usual practice. */
246static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
247{
248 if (reg == LM75_REG_CONF)
249 return i2c_smbus_write_byte_data(client, reg, value);
250 else
251 return i2c_smbus_write_word_data(client, reg, swab16(value));
252}
253
254static void lm75_init_client(struct i2c_client *client)
255{
e647ecf1
JD
256 int reg;
257
258 /* Enable if in shutdown mode */
259 reg = lm75_read_value(client, LM75_REG_CONF);
260 if (reg >= 0 && (reg & 0x01))
261 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
1da177e4
LT
262}
263
264static struct lm75_data *lm75_update_device(struct device *dev)
265{
266 struct i2c_client *client = to_i2c_client(dev);
267 struct lm75_data *data = i2c_get_clientdata(client);
268
269 down(&data->update_lock);
270
271 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
272 || !data->valid) {
273 dev_dbg(&client->dev, "Starting lm75 update\n");
274
275 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
276 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
277 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
278 data->last_updated = jiffies;
279 data->valid = 1;
280 }
281
282 up(&data->update_lock);
283
284 return data;
285}
286
287static int __init sensors_lm75_init(void)
288{
289 return i2c_add_driver(&lm75_driver);
290}
291
292static void __exit sensors_lm75_exit(void)
293{
294 i2c_del_driver(&lm75_driver);
295}
296
297MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
298MODULE_DESCRIPTION("LM75 driver");
299MODULE_LICENSE("GPL");
300
301module_init(sensors_lm75_init);
302module_exit(sensors_lm75_exit);
This page took 0.08329 seconds and 5 git commands to generate.