blob: c103640455a324614f253c741807d34de7e31af7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 * Kyosti Malkki <kmalkki@cc.hut.fi>
6 * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and
7 * Jean Delvare <khali@linux-fr.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
24 * and advice of Greg Kroah-Hartman.
25 *
26 * Notes about the port:
27 * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
28 * in1 nor in2. The original driver had an ugly workaround to get them
29 * anyway (changing limits and watching alarms trigger and wear off).
30 * We did not keep that part of the original driver in the Linux 2.6
31 * version, since it was making the driver significantly more complex
32 * with no real benefit.
33 *
34 * History:
35 * 2004-01-28 Original port. (Hong-Gunn Chew)
36 * 2004-01-31 Code review and approval. (Jean Delvare)
37 */
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/jiffies.h>
43#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040044#include <linux/hwmon.h>
45#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010046#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020047#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Addresses to scan */
50static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020053I2C_CLIENT_INSMOD_2(gl518sm_r00, gl518sm_r80);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* Many GL518 constants specified below */
56
57/* The GL518 registers */
58#define GL518_REG_CHIP_ID 0x00
59#define GL518_REG_REVISION 0x01
60#define GL518_REG_VENDOR_ID 0x02
61#define GL518_REG_CONF 0x03
62#define GL518_REG_TEMP_IN 0x04
63#define GL518_REG_TEMP_MAX 0x05
64#define GL518_REG_TEMP_HYST 0x06
65#define GL518_REG_FAN_COUNT 0x07
66#define GL518_REG_FAN_LIMIT 0x08
67#define GL518_REG_VIN1_LIMIT 0x09
68#define GL518_REG_VIN2_LIMIT 0x0a
69#define GL518_REG_VIN3_LIMIT 0x0b
70#define GL518_REG_VDD_LIMIT 0x0c
71#define GL518_REG_VIN3 0x0d
72#define GL518_REG_MISC 0x0f
73#define GL518_REG_ALARM 0x10
74#define GL518_REG_MASK 0x11
75#define GL518_REG_INT 0x12
76#define GL518_REG_VIN2 0x13
77#define GL518_REG_VIN1 0x14
78#define GL518_REG_VDD 0x15
79
80
81/*
82 * Conversions. Rounding and limit checking is only done on the TO_REG
83 * variants. Note that you should be a bit careful with which arguments
84 * these macros are called: arguments may be evaluated more than once.
85 * Fixing this is just not worth it.
86 */
87
88#define RAW_FROM_REG(val) val
89
90#define BOOL_FROM_REG(val) ((val)?0:1)
91#define BOOL_TO_REG(val) ((val)?0:1)
92
93#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
94 (val)-500:(val)+500)/1000)+119),0,255))
95#define TEMP_FROM_REG(val) (((val) - 119) * 1000)
96
97static inline u8 FAN_TO_REG(long rpm, int div)
98{
99 long rpmdiv;
100 if (rpm == 0)
101 return 0;
102 rpmdiv = SENSORS_LIMIT(rpm, 1, 1920000) * div;
103 return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
104}
105#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (960000/((val)*(div))))
106
107#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
108#define IN_FROM_REG(val) ((val)*19)
109
110#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
111#define VDD_FROM_REG(val) (((val)*95+2)/4)
112
113#define DIV_TO_REG(val) ((val)==4?2:(val)==2?1:(val)==1?0:3)
114#define DIV_FROM_REG(val) (1 << (val))
115
116#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
117#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
118
119/* Each client has this additional data */
120struct gl518_data {
121 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400122 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 enum chips type;
124
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100125 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char valid; /* !=0 if following fields are valid */
127 unsigned long last_updated; /* In jiffies */
128
129 u8 voltage_in[4]; /* Register values; [0] = VDD */
130 u8 voltage_min[4]; /* Register values; [0] = VDD */
131 u8 voltage_max[4]; /* Register values; [0] = VDD */
132 u8 iter_voltage_in[4]; /* Register values; [0] = VDD */
133 u8 fan_in[2];
134 u8 fan_min[2];
135 u8 fan_div[2]; /* Register encoding, shifted right */
136 u8 fan_auto1; /* Boolean */
137 u8 temp_in; /* Register values */
138 u8 temp_max; /* Register values */
139 u8 temp_hyst; /* Register values */
140 u8 alarms; /* Register value */
141 u8 alarm_mask; /* Register value */
142 u8 beep_mask; /* Register value */
143 u8 beep_enable; /* Boolean */
144};
145
146static int gl518_attach_adapter(struct i2c_adapter *adapter);
147static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
148static void gl518_init_client(struct i2c_client *client);
149static int gl518_detach_client(struct i2c_client *client);
150static int gl518_read_value(struct i2c_client *client, u8 reg);
151static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
152static struct gl518_data *gl518_update_device(struct device *dev);
153
154/* This is the driver that will be inserted */
155static struct i2c_driver gl518_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100156 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100157 .name = "gl518sm",
158 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .id = I2C_DRIVERID_GL518,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .attach_adapter = gl518_attach_adapter,
161 .detach_client = gl518_detach_client,
162};
163
164/*
165 * Sysfs stuff
166 */
167
168#define show(type, suffix, value) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400169static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{ \
171 struct gl518_data *data = gl518_update_device(dev); \
172 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
173}
174
175#define show_fan(suffix, value, index) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400176static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{ \
178 struct gl518_data *data = gl518_update_device(dev); \
179 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \
180 DIV_FROM_REG(data->fan_div[index]))); \
181}
182
183show(TEMP, temp_input1, temp_in);
184show(TEMP, temp_max1, temp_max);
185show(TEMP, temp_hyst1, temp_hyst);
186show(BOOL, fan_auto1, fan_auto1);
187show_fan(fan_input1, fan_in, 0);
188show_fan(fan_input2, fan_in, 1);
189show_fan(fan_min1, fan_min, 0);
190show_fan(fan_min2, fan_min, 1);
191show(DIV, fan_div1, fan_div[0]);
192show(DIV, fan_div2, fan_div[1]);
193show(VDD, in_input0, voltage_in[0]);
194show(IN, in_input1, voltage_in[1]);
195show(IN, in_input2, voltage_in[2]);
196show(IN, in_input3, voltage_in[3]);
197show(VDD, in_min0, voltage_min[0]);
198show(IN, in_min1, voltage_min[1]);
199show(IN, in_min2, voltage_min[2]);
200show(IN, in_min3, voltage_min[3]);
201show(VDD, in_max0, voltage_max[0]);
202show(IN, in_max1, voltage_max[1]);
203show(IN, in_max2, voltage_max[2]);
204show(IN, in_max3, voltage_max[3]);
205show(RAW, alarms, alarms);
206show(BOOL, beep_enable, beep_enable);
207show(BEEP_MASK, beep_mask, beep_mask);
208
209#define set(type, suffix, value, reg) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400210static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 size_t count) \
212{ \
213 struct i2c_client *client = to_i2c_client(dev); \
214 struct gl518_data *data = i2c_get_clientdata(client); \
215 long val = simple_strtol(buf, NULL, 10); \
216 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100217 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 data->value = type##_TO_REG(val); \
219 gl518_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100220 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return count; \
222}
223
224#define set_bits(type, suffix, value, reg, mask, shift) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400225static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 size_t count) \
227{ \
228 struct i2c_client *client = to_i2c_client(dev); \
229 struct gl518_data *data = i2c_get_clientdata(client); \
230 int regvalue; \
231 unsigned long val = simple_strtoul(buf, NULL, 10); \
232 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100233 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 regvalue = gl518_read_value(client, reg); \
235 data->value = type##_TO_REG(val); \
236 regvalue = (regvalue & ~mask) | (data->value << shift); \
237 gl518_write_value(client, reg, regvalue); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100238 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return count; \
240}
241
242#define set_low(type, suffix, value, reg) \
243 set_bits(type, suffix, value, reg, 0x00ff, 0)
244#define set_high(type, suffix, value, reg) \
245 set_bits(type, suffix, value, reg, 0xff00, 8)
246
247set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
248set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
249set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
250set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6);
251set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4);
252set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
253set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
254set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
255set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
256set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
257set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
258set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
259set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
260set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
261set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
262
Yani Ioannou30f74292005-05-17 06:41:35 -0400263static ssize_t set_fan_min1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct i2c_client *client = to_i2c_client(dev);
266 struct gl518_data *data = i2c_get_clientdata(client);
267 int regvalue;
268 unsigned long val = simple_strtoul(buf, NULL, 10);
269
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
272 data->fan_min[0] = FAN_TO_REG(val,
273 DIV_FROM_REG(data->fan_div[0]));
274 regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8);
275 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
276
277 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
278 if (data->fan_min[0] == 0)
279 data->alarm_mask &= ~0x20;
280 else
281 data->alarm_mask |= 0x20;
282 data->beep_mask &= data->alarm_mask;
283 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
284
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100285 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return count;
287}
288
Yani Ioannou30f74292005-05-17 06:41:35 -0400289static ssize_t set_fan_min2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct i2c_client *client = to_i2c_client(dev);
292 struct gl518_data *data = i2c_get_clientdata(client);
293 int regvalue;
294 unsigned long val = simple_strtoul(buf, NULL, 10);
295
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100296 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
298 data->fan_min[1] = FAN_TO_REG(val,
299 DIV_FROM_REG(data->fan_div[1]));
300 regvalue = (regvalue & 0xff00) | data->fan_min[1];
301 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
302
303 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
304 if (data->fan_min[1] == 0)
305 data->alarm_mask &= ~0x40;
306 else
307 data->alarm_mask |= 0x40;
308 data->beep_mask &= data->alarm_mask;
309 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
310
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100311 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return count;
313}
314
315static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
316static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
317static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
318 show_temp_hyst1, set_temp_hyst1);
319static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
320static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
321static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
322static DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1);
323static DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2);
324static DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1);
325static DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2);
326static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
327static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
328static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
329static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
330static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
331static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
332static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
333static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
334static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
335static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
336static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
337static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
338static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
339static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
340 show_beep_enable, set_beep_enable);
341static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
342 show_beep_mask, set_beep_mask);
343
Jean Delvare87808be2006-09-24 21:17:13 +0200344static struct attribute *gl518_attributes[] = {
345 &dev_attr_in0_input.attr,
346 &dev_attr_in1_input.attr,
347 &dev_attr_in2_input.attr,
348 &dev_attr_in3_input.attr,
349 &dev_attr_in0_min.attr,
350 &dev_attr_in1_min.attr,
351 &dev_attr_in2_min.attr,
352 &dev_attr_in3_min.attr,
353 &dev_attr_in0_max.attr,
354 &dev_attr_in1_max.attr,
355 &dev_attr_in2_max.attr,
356 &dev_attr_in3_max.attr,
357
358 &dev_attr_fan1_auto.attr,
359 &dev_attr_fan1_input.attr,
360 &dev_attr_fan2_input.attr,
361 &dev_attr_fan1_min.attr,
362 &dev_attr_fan2_min.attr,
363 &dev_attr_fan1_div.attr,
364 &dev_attr_fan2_div.attr,
365
366 &dev_attr_temp1_input.attr,
367 &dev_attr_temp1_max.attr,
368 &dev_attr_temp1_max_hyst.attr,
369
370 &dev_attr_alarms.attr,
371 &dev_attr_beep_enable.attr,
372 &dev_attr_beep_mask.attr,
373 NULL
374};
375
376static const struct attribute_group gl518_group = {
377 .attrs = gl518_attributes,
378};
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * Real code
382 */
383
384static int gl518_attach_adapter(struct i2c_adapter *adapter)
385{
386 if (!(adapter->class & I2C_CLASS_HWMON))
387 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200388 return i2c_probe(adapter, &addr_data, gl518_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
392{
393 int i;
394 struct i2c_client *new_client;
395 struct gl518_data *data;
396 int err = 0;
397
398 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
399 I2C_FUNC_SMBUS_WORD_DATA))
400 goto exit;
401
402 /* OK. For now, we presume we have a valid client. We now create the
403 client structure, even though we cannot fill it completely yet.
404 But it allows us to access gl518_{read,write}_value. */
405
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200406 if (!(data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 err = -ENOMEM;
408 goto exit;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 new_client = &data->client;
412 i2c_set_clientdata(new_client, data);
413
414 new_client->addr = address;
415 new_client->adapter = adapter;
416 new_client->driver = &gl518_driver;
417 new_client->flags = 0;
418
419 /* Now, we do the remaining detection. */
420
421 if (kind < 0) {
422 if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80)
423 || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80))
424 goto exit_free;
425 }
426
427 /* Determine the chip type. */
428 if (kind <= 0) {
429 i = gl518_read_value(new_client, GL518_REG_REVISION);
430 if (i == 0x00) {
431 kind = gl518sm_r00;
432 } else if (i == 0x80) {
433 kind = gl518sm_r80;
434 } else {
435 if (kind <= 0)
436 dev_info(&adapter->dev,
437 "Ignoring 'force' parameter for unknown "
438 "chip at adapter %d, address 0x%02x\n",
439 i2c_adapter_id(adapter), address);
440 goto exit_free;
441 }
442 }
443
444 /* Fill in the remaining client fields */
445 strlcpy(new_client->name, "gl518sm", I2C_NAME_SIZE);
446 data->type = kind;
447 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100448 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* Tell the I2C layer a new client has arrived */
451 if ((err = i2c_attach_client(new_client)))
452 goto exit_free;
453
454 /* Initialize the GL518SM chip */
455 data->alarm_mask = 0xff;
456 data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0;
457 gl518_init_client((struct i2c_client *) new_client);
458
459 /* Register sysfs hooks */
Jean Delvare87808be2006-09-24 21:17:13 +0200460 if ((err = sysfs_create_group(&new_client->dev.kobj, &gl518_group)))
461 goto exit_detach;
462
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400463 data->class_dev = hwmon_device_register(&new_client->dev);
464 if (IS_ERR(data->class_dev)) {
465 err = PTR_ERR(data->class_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200466 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400467 }
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 0;
470
Jean Delvare87808be2006-09-24 21:17:13 +0200471exit_remove_files:
472 sysfs_remove_group(&new_client->dev.kobj, &gl518_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400473exit_detach:
474 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475exit_free:
476 kfree(data);
477exit:
478 return err;
479}
480
481
482/* Called when we have found a new GL518SM.
483 Note that we preserve D4:NoFan2 and D2:beep_enable. */
484static void gl518_init_client(struct i2c_client *client)
485{
486 /* Make sure we leave D7:Reset untouched */
487 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
488
489 /* Comparator mode (D3=0), standby mode (D6=0) */
490 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
491
492 /* Never interrupts */
493 gl518_write_value(client, GL518_REG_MASK, 0x00);
494
495 /* Clear status register (D5=1), start (D6=1) */
496 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
497 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
498}
499
500static int gl518_detach_client(struct i2c_client *client)
501{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400502 struct gl518_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int err;
504
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400505 hwmon_device_unregister(data->class_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200506 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400507
Jean Delvare7bef5592005-07-27 22:14:49 +0200508 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400511 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0;
513}
514
515/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
516 GL518 uses a high-byte first convention, which is exactly opposite to
517 the usual practice. */
518static int gl518_read_value(struct i2c_client *client, u8 reg)
519{
520 if ((reg >= 0x07) && (reg <= 0x0c))
521 return swab16(i2c_smbus_read_word_data(client, reg));
522 else
523 return i2c_smbus_read_byte_data(client, reg);
524}
525
526/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
527 GL518 uses a high-byte first convention, which is exactly opposite to
528 the usual practice. */
529static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
530{
531 if ((reg >= 0x07) && (reg <= 0x0c))
532 return i2c_smbus_write_word_data(client, reg, swab16(value));
533 else
534 return i2c_smbus_write_byte_data(client, reg, value);
535}
536
537static struct gl518_data *gl518_update_device(struct device *dev)
538{
539 struct i2c_client *client = to_i2c_client(dev);
540 struct gl518_data *data = i2c_get_clientdata(client);
541 int val;
542
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100543 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
546 || !data->valid) {
547 dev_dbg(&client->dev, "Starting gl518 update\n");
548
549 data->alarms = gl518_read_value(client, GL518_REG_INT);
550 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
551
552 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
553 data->voltage_min[0] = val & 0xff;
554 data->voltage_max[0] = (val >> 8) & 0xff;
555 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
556 data->voltage_min[1] = val & 0xff;
557 data->voltage_max[1] = (val >> 8) & 0xff;
558 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
559 data->voltage_min[2] = val & 0xff;
560 data->voltage_max[2] = (val >> 8) & 0xff;
561 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
562 data->voltage_min[3] = val & 0xff;
563 data->voltage_max[3] = (val >> 8) & 0xff;
564
565 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
566 data->fan_in[0] = (val >> 8) & 0xff;
567 data->fan_in[1] = val & 0xff;
568
569 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
570 data->fan_min[0] = (val >> 8) & 0xff;
571 data->fan_min[1] = val & 0xff;
572
573 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
574 data->temp_max =
575 gl518_read_value(client, GL518_REG_TEMP_MAX);
576 data->temp_hyst =
577 gl518_read_value(client, GL518_REG_TEMP_HYST);
578
579 val = gl518_read_value(client, GL518_REG_MISC);
580 data->fan_div[0] = (val >> 6) & 0x03;
581 data->fan_div[1] = (val >> 4) & 0x03;
582 data->fan_auto1 = (val >> 3) & 0x01;
583
584 data->alarms &= data->alarm_mask;
585
586 val = gl518_read_value(client, GL518_REG_CONF);
587 data->beep_enable = (val >> 2) & 1;
588
589 if (data->type != gl518sm_r00) {
590 data->voltage_in[0] =
591 gl518_read_value(client, GL518_REG_VDD);
592 data->voltage_in[1] =
593 gl518_read_value(client, GL518_REG_VIN1);
594 data->voltage_in[2] =
595 gl518_read_value(client, GL518_REG_VIN2);
596 }
597 data->voltage_in[3] =
598 gl518_read_value(client, GL518_REG_VIN3);
599
600 data->last_updated = jiffies;
601 data->valid = 1;
602 }
603
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100604 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 return data;
607}
608
609static int __init sensors_gl518sm_init(void)
610{
611 return i2c_add_driver(&gl518_driver);
612}
613
614static void __exit sensors_gl518sm_exit(void)
615{
616 i2c_del_driver(&gl518_driver);
617}
618
619MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
620 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
621 "Hong-Gunn Chew <hglinux@gunnet.org>");
622MODULE_DESCRIPTION("GL518SM driver");
623MODULE_LICENSE("GPL");
624
625module_init(sensors_gl518sm_init);
626module_exit(sensors_gl518sm_exit);