blob: 213fb170d3973834e786020b909a4fd3b77f7340 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * w83l785ts.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Inspired from the lm83 driver. The W83L785TS-S is a sensor chip made
7 * by Winbond. It reports a single external temperature with a 1 deg
8 * resolution and a 3 deg accuracy. Datasheet can be obtained from
9 * Winbond's website at:
10 * http://www.winbond-usa.com/products/winbond_products/pdfs/PCIC/W83L785TS-S.pdf
11 *
12 * Ported to Linux 2.6 by Wolfgang Ziegler <nuppla@gmx.at> and Jean Delvare
13 * <khali@linux-fr.org>.
14 *
15 * Thanks to James Bolt <james@evilpenguin.com> for benchmarking the read
16 * error handling mechanism.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 */
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/module.h>
34#include <linux/delay.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
39#include <linux/i2c-sensor.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040040#include <linux/hwmon.h>
41#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* How many retries on register read error */
44#define MAX_RETRIES 5
45
46/*
47 * Address to scan
48 * Address is fully defined internally and cannot be changed.
49 */
50
51static unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * Insmod parameters
55 */
56
57SENSORS_INSMOD_1(w83l785ts);
58
59/*
60 * The W83L785TS-S registers
61 * Manufacturer ID is 0x5CA3 for Winbond.
62 */
63
64#define W83L785TS_REG_MAN_ID1 0x4D
65#define W83L785TS_REG_MAN_ID2 0x4C
66#define W83L785TS_REG_CHIP_ID 0x4E
67#define W83L785TS_REG_CONFIG 0x40
68#define W83L785TS_REG_TYPE 0x52
69#define W83L785TS_REG_TEMP 0x27
70#define W83L785TS_REG_TEMP_OVER 0x53 /* not sure about this one */
71
72/*
73 * Conversions
74 * The W83L785TS-S uses signed 8-bit values.
75 */
76
77#define TEMP_FROM_REG(val) ((val & 0x80 ? val-0x100 : val) * 1000)
78
79/*
80 * Functions declaration
81 */
82
83static int w83l785ts_attach_adapter(struct i2c_adapter *adapter);
84static int w83l785ts_detect(struct i2c_adapter *adapter, int address,
85 int kind);
86static int w83l785ts_detach_client(struct i2c_client *client);
87static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval);
88static struct w83l785ts_data *w83l785ts_update_device(struct device *dev);
89
90/*
91 * Driver data (common to all clients)
92 */
93
94static struct i2c_driver w83l785ts_driver = {
95 .owner = THIS_MODULE,
96 .name = "w83l785ts",
97 .id = I2C_DRIVERID_W83L785TS,
98 .flags = I2C_DF_NOTIFY,
99 .attach_adapter = w83l785ts_attach_adapter,
100 .detach_client = w83l785ts_detach_client,
101};
102
103/*
104 * Client data (each client gets its own)
105 */
106
107struct w83l785ts_data {
108 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400109 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct semaphore update_lock;
111 char valid; /* zero until following fields are valid */
112 unsigned long last_updated; /* in jiffies */
113
114 /* registers values */
115 u8 temp, temp_over;
116};
117
118/*
119 * Sysfs stuff
120 */
121
Yani Ioannoue404e272005-05-17 06:42:58 -0400122static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct w83l785ts_data *data = w83l785ts_update_device(dev);
125 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
126}
127
Yani Ioannoue404e272005-05-17 06:42:58 -0400128static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct w83l785ts_data *data = w83l785ts_update_device(dev);
131 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
132}
133
134static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
135static DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_over, NULL);
136
137/*
138 * Real code
139 */
140
141static int w83l785ts_attach_adapter(struct i2c_adapter *adapter)
142{
143 if (!(adapter->class & I2C_CLASS_HWMON))
144 return 0;
145 return i2c_detect(adapter, &addr_data, w83l785ts_detect);
146}
147
148/*
149 * The following function does more than just detection. If detection
150 * succeeds, it also registers the new chip.
151 */
152static int w83l785ts_detect(struct i2c_adapter *adapter, int address, int kind)
153{
154 struct i2c_client *new_client;
155 struct w83l785ts_data *data;
156 int err = 0;
157
158
159 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
160 goto exit;
161
162 if (!(data = kmalloc(sizeof(struct w83l785ts_data), GFP_KERNEL))) {
163 err = -ENOMEM;
164 goto exit;
165 }
166 memset(data, 0, sizeof(struct w83l785ts_data));
167
168
169 /* The common I2C client data is placed right before the
170 * W83L785TS-specific data. */
171 new_client = &data->client;
172 i2c_set_clientdata(new_client, data);
173 new_client->addr = address;
174 new_client->adapter = adapter;
175 new_client->driver = &w83l785ts_driver;
176 new_client->flags = 0;
177
178 /*
179 * Now we do the remaining detection. A negative kind means that
180 * the driver was loaded with no force parameter (default), so we
181 * must both detect and identify the chip (actually there is only
182 * one possible kind of chip for now, W83L785TS-S). A zero kind means
183 * that the driver was loaded with the force parameter, the detection
184 * step shall be skipped. A positive kind means that the driver
185 * was loaded with the force parameter and a given kind of chip is
186 * requested, so both the detection and the identification steps
187 * are skipped.
188 */
189 if (kind < 0) { /* detection */
190 if (((w83l785ts_read_value(new_client,
191 W83L785TS_REG_CONFIG, 0) & 0x80) != 0x00)
192 || ((w83l785ts_read_value(new_client,
193 W83L785TS_REG_TYPE, 0) & 0xFC) != 0x00)) {
194 dev_dbg(&adapter->dev,
195 "W83L785TS-S detection failed at 0x%02x.\n",
196 address);
197 goto exit_free;
198 }
199 }
200
201 if (kind <= 0) { /* identification */
202 u16 man_id;
203 u8 chip_id;
204
205 man_id = (w83l785ts_read_value(new_client,
206 W83L785TS_REG_MAN_ID1, 0) << 8) +
207 w83l785ts_read_value(new_client,
208 W83L785TS_REG_MAN_ID2, 0);
209 chip_id = w83l785ts_read_value(new_client,
210 W83L785TS_REG_CHIP_ID, 0);
211
212 if (man_id == 0x5CA3) { /* Winbond */
213 if (chip_id == 0x70) { /* W83L785TS-S */
214 kind = w83l785ts;
215 }
216 }
217
218 if (kind <= 0) { /* identification failed */
219 dev_info(&adapter->dev,
220 "Unsupported chip (man_id=0x%04X, "
221 "chip_id=0x%02X).\n", man_id, chip_id);
222 goto exit_free;
223 }
224 }
225
226 /* We can fill in the remaining client fields. */
227 strlcpy(new_client->name, "w83l785ts", I2C_NAME_SIZE);
228 data->valid = 0;
229 init_MUTEX(&data->update_lock);
230
231 /* Default values in case the first read fails (unlikely). */
232 data->temp_over = data->temp = 0;
233
234 /* Tell the I2C layer a new client has arrived. */
235 if ((err = i2c_attach_client(new_client)))
236 goto exit_free;
237
238 /*
239 * Initialize the W83L785TS chip
240 * Nothing yet, assume it is already started.
241 */
242
243 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400244 data->class_dev = hwmon_device_register(&new_client->dev);
245 if (IS_ERR(data->class_dev)) {
246 err = PTR_ERR(data->class_dev);
247 goto exit_detach;
248 }
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 device_create_file(&new_client->dev, &dev_attr_temp1_input);
251 device_create_file(&new_client->dev, &dev_attr_temp1_max);
252
253 return 0;
254
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400255exit_detach:
256 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257exit_free:
258 kfree(data);
259exit:
260 return err;
261}
262
263static int w83l785ts_detach_client(struct i2c_client *client)
264{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400265 struct w83l785ts_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int err;
267
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400268 hwmon_device_unregister(data->class_dev);
269
Jean Delvare7bef5592005-07-27 22:14:49 +0200270 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400273 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
275}
276
277static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval)
278{
279 int value, i;
280
281 /* Frequent read errors have been reported on Asus boards, so we
282 * retry on read errors. If it still fails (unlikely), return the
283 * default value requested by the caller. */
284 for (i = 1; i <= MAX_RETRIES; i++) {
285 value = i2c_smbus_read_byte_data(client, reg);
286 if (value >= 0) {
287 dev_dbg(&client->dev, "Read 0x%02x from register "
288 "0x%02x.\n", value, reg);
289 return value;
290 }
291 dev_dbg(&client->dev, "Read failed, will retry in %d.\n", i);
292 msleep(i);
293 }
294
295 dev_err(&client->dev, "Couldn't read value from register 0x%02x. "
296 "Please report.\n", reg);
297 return defval;
298}
299
300static struct w83l785ts_data *w83l785ts_update_device(struct device *dev)
301{
302 struct i2c_client *client = to_i2c_client(dev);
303 struct w83l785ts_data *data = i2c_get_clientdata(client);
304
305 down(&data->update_lock);
306
307 if (!data->valid || time_after(jiffies, data->last_updated + HZ * 2)) {
308 dev_dbg(&client->dev, "Updating w83l785ts data.\n");
309 data->temp = w83l785ts_read_value(client,
310 W83L785TS_REG_TEMP, data->temp);
311 data->temp_over = w83l785ts_read_value(client,
312 W83L785TS_REG_TEMP_OVER, data->temp_over);
313
314 data->last_updated = jiffies;
315 data->valid = 1;
316 }
317
318 up(&data->update_lock);
319
320 return data;
321}
322
323static int __init sensors_w83l785ts_init(void)
324{
325 return i2c_add_driver(&w83l785ts_driver);
326}
327
328static void __exit sensors_w83l785ts_exit(void)
329{
330 i2c_del_driver(&w83l785ts_driver);
331}
332
333MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
334MODULE_DESCRIPTION("W83L785TS-S driver");
335MODULE_LICENSE("GPL");
336
337module_init(sensors_w83l785ts_init);
338module_exit(sensors_w83l785ts_exit);