blob: 535ad8776a689568f50ad5f1a11d422b6db80479 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040040#include <linux/hwmon.h>
Jean Delvare28292e72007-10-22 17:46:42 +020041#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040042#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010043#include <linux/mutex.h>
Jean Delvare87808be2006-09-24 21:17:13 +020044#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* Addresses to scan */
47static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020050I2C_CLIENT_INSMOD_2(gl518sm_r00, gl518sm_r80);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Many GL518 constants specified below */
53
54/* The GL518 registers */
55#define GL518_REG_CHIP_ID 0x00
56#define GL518_REG_REVISION 0x01
57#define GL518_REG_VENDOR_ID 0x02
58#define GL518_REG_CONF 0x03
59#define GL518_REG_TEMP_IN 0x04
60#define GL518_REG_TEMP_MAX 0x05
61#define GL518_REG_TEMP_HYST 0x06
62#define GL518_REG_FAN_COUNT 0x07
63#define GL518_REG_FAN_LIMIT 0x08
64#define GL518_REG_VIN1_LIMIT 0x09
65#define GL518_REG_VIN2_LIMIT 0x0a
66#define GL518_REG_VIN3_LIMIT 0x0b
67#define GL518_REG_VDD_LIMIT 0x0c
68#define GL518_REG_VIN3 0x0d
69#define GL518_REG_MISC 0x0f
70#define GL518_REG_ALARM 0x10
71#define GL518_REG_MASK 0x11
72#define GL518_REG_INT 0x12
73#define GL518_REG_VIN2 0x13
74#define GL518_REG_VIN1 0x14
75#define GL518_REG_VDD 0x15
76
77
78/*
79 * Conversions. Rounding and limit checking is only done on the TO_REG
80 * variants. Note that you should be a bit careful with which arguments
81 * these macros are called: arguments may be evaluated more than once.
82 * Fixing this is just not worth it.
83 */
84
85#define RAW_FROM_REG(val) val
86
87#define BOOL_FROM_REG(val) ((val)?0:1)
88#define BOOL_TO_REG(val) ((val)?0:1)
89
90#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
91 (val)-500:(val)+500)/1000)+119),0,255))
92#define TEMP_FROM_REG(val) (((val) - 119) * 1000)
93
94static inline u8 FAN_TO_REG(long rpm, int div)
95{
96 long rpmdiv;
97 if (rpm == 0)
98 return 0;
99 rpmdiv = SENSORS_LIMIT(rpm, 1, 1920000) * div;
100 return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
101}
102#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (960000/((val)*(div))))
103
104#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
105#define IN_FROM_REG(val) ((val)*19)
106
107#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
108#define VDD_FROM_REG(val) (((val)*95+2)/4)
109
110#define DIV_TO_REG(val) ((val)==4?2:(val)==2?1:(val)==1?0:3)
111#define DIV_FROM_REG(val) (1 << (val))
112
113#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
114#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
115
116/* Each client has this additional data */
117struct gl518_data {
118 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700119 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 enum chips type;
121
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100122 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 char valid; /* !=0 if following fields are valid */
124 unsigned long last_updated; /* In jiffies */
125
126 u8 voltage_in[4]; /* Register values; [0] = VDD */
127 u8 voltage_min[4]; /* Register values; [0] = VDD */
128 u8 voltage_max[4]; /* Register values; [0] = VDD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 u8 fan_in[2];
130 u8 fan_min[2];
131 u8 fan_div[2]; /* Register encoding, shifted right */
132 u8 fan_auto1; /* Boolean */
133 u8 temp_in; /* Register values */
134 u8 temp_max; /* Register values */
135 u8 temp_hyst; /* Register values */
136 u8 alarms; /* Register value */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200137 u8 alarm_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 u8 beep_mask; /* Register value */
139 u8 beep_enable; /* Boolean */
140};
141
142static int gl518_attach_adapter(struct i2c_adapter *adapter);
143static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
144static void gl518_init_client(struct i2c_client *client);
145static int gl518_detach_client(struct i2c_client *client);
146static int gl518_read_value(struct i2c_client *client, u8 reg);
147static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
148static struct gl518_data *gl518_update_device(struct device *dev);
149
150/* This is the driver that will be inserted */
151static struct i2c_driver gl518_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100152 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100153 .name = "gl518sm",
154 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .attach_adapter = gl518_attach_adapter,
156 .detach_client = gl518_detach_client,
157};
158
159/*
160 * Sysfs stuff
161 */
162
163#define show(type, suffix, value) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400164static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{ \
166 struct gl518_data *data = gl518_update_device(dev); \
167 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170show(TEMP, temp_input1, temp_in);
171show(TEMP, temp_max1, temp_max);
172show(TEMP, temp_hyst1, temp_hyst);
173show(BOOL, fan_auto1, fan_auto1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174show(VDD, in_input0, voltage_in[0]);
175show(IN, in_input1, voltage_in[1]);
176show(IN, in_input2, voltage_in[2]);
177show(IN, in_input3, voltage_in[3]);
178show(VDD, in_min0, voltage_min[0]);
179show(IN, in_min1, voltage_min[1]);
180show(IN, in_min2, voltage_min[2]);
181show(IN, in_min3, voltage_min[3]);
182show(VDD, in_max0, voltage_max[0]);
183show(IN, in_max1, voltage_max[1]);
184show(IN, in_max2, voltage_max[2]);
185show(IN, in_max3, voltage_max[3]);
186show(RAW, alarms, alarms);
187show(BOOL, beep_enable, beep_enable);
188show(BEEP_MASK, beep_mask, beep_mask);
189
Jean Delvare28292e72007-10-22 17:46:42 +0200190static ssize_t show_fan_input(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
193 int nr = to_sensor_dev_attr(attr)->index;
194 struct gl518_data *data = gl518_update_device(dev);
195 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
196 DIV_FROM_REG(data->fan_div[nr])));
197}
198
199static ssize_t show_fan_min(struct device *dev,
200 struct device_attribute *attr, char *buf)
201{
202 int nr = to_sensor_dev_attr(attr)->index;
203 struct gl518_data *data = gl518_update_device(dev);
204 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
205 DIV_FROM_REG(data->fan_div[nr])));
206}
207
208static ssize_t show_fan_div(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 int nr = to_sensor_dev_attr(attr)->index;
212 struct gl518_data *data = gl518_update_device(dev);
213 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#define set(type, suffix, value, reg) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400217static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 size_t count) \
219{ \
220 struct i2c_client *client = to_i2c_client(dev); \
221 struct gl518_data *data = i2c_get_clientdata(client); \
222 long val = simple_strtol(buf, NULL, 10); \
223 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100224 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 data->value = type##_TO_REG(val); \
226 gl518_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100227 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return count; \
229}
230
231#define set_bits(type, suffix, value, reg, mask, shift) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400232static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 size_t count) \
234{ \
235 struct i2c_client *client = to_i2c_client(dev); \
236 struct gl518_data *data = i2c_get_clientdata(client); \
237 int regvalue; \
238 unsigned long val = simple_strtoul(buf, NULL, 10); \
239 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100240 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 regvalue = gl518_read_value(client, reg); \
242 data->value = type##_TO_REG(val); \
243 regvalue = (regvalue & ~mask) | (data->value << shift); \
244 gl518_write_value(client, reg, regvalue); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100245 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return count; \
247}
248
249#define set_low(type, suffix, value, reg) \
250 set_bits(type, suffix, value, reg, 0x00ff, 0)
251#define set_high(type, suffix, value, reg) \
252 set_bits(type, suffix, value, reg, 0xff00, 8)
253
254set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
255set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
256set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
258set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
259set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
260set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
261set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
262set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
263set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
264set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
265set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
266set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
267
Jean Delvare28292e72007-10-22 17:46:42 +0200268static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
269 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct i2c_client *client = to_i2c_client(dev);
272 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200273 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int regvalue;
275 unsigned long val = simple_strtoul(buf, NULL, 10);
276
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100277 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
Jean Delvare28292e72007-10-22 17:46:42 +0200279 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
280 regvalue = (regvalue & (0xff << (8 * nr)))
281 | (data->fan_min[nr] << (8 * (1 - nr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
283
284 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
Jean Delvare28292e72007-10-22 17:46:42 +0200285 if (data->fan_min[nr] == 0)
286 data->alarm_mask &= ~(0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 else
Jean Delvare28292e72007-10-22 17:46:42 +0200288 data->alarm_mask |= (0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 data->beep_mask &= data->alarm_mask;
290 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
291
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100292 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return count;
294}
295
Jean Delvare28292e72007-10-22 17:46:42 +0200296static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
297 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct i2c_client *client = to_i2c_client(dev);
300 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200301 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int regvalue;
303 unsigned long val = simple_strtoul(buf, NULL, 10);
304
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100305 mutex_lock(&data->update_lock);
Jean Delvare28292e72007-10-22 17:46:42 +0200306 regvalue = gl518_read_value(client, GL518_REG_MISC);
307 data->fan_div[nr] = DIV_TO_REG(val);
308 regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
309 | (data->fan_div[nr] << (6 - 2 * nr));
310 gl518_write_value(client, GL518_REG_MISC, regvalue);
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);
Jean Delvare28292e72007-10-22 17:46:42 +0200320static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
321static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
322static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
323 show_fan_min, set_fan_min, 0);
324static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
325 show_fan_min, set_fan_min, 1);
326static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
327 show_fan_div, set_fan_div, 0);
328static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
329 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
331static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
332static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
333static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
334static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
335static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
336static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
337static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
338static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
339static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
340static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
341static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
342static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
343static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
344 show_beep_enable, set_beep_enable);
345static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
346 show_beep_mask, set_beep_mask);
347
Jean Delvareda6848d2007-10-22 17:47:16 +0200348static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
349 char *buf)
350{
351 int bitnr = to_sensor_dev_attr(attr)->index;
352 struct gl518_data *data = gl518_update_device(dev);
353 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
354}
355
356static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
357static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
358static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
359static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
360static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
361static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
362static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
363
364static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
365 char *buf)
366{
367 int bitnr = to_sensor_dev_attr(attr)->index;
368 struct gl518_data *data = gl518_update_device(dev);
369 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
370}
371
372static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
373 const char *buf, size_t count)
374{
375 struct i2c_client *client = to_i2c_client(dev);
376 struct gl518_data *data = i2c_get_clientdata(client);
377 int bitnr = to_sensor_dev_attr(attr)->index;
378 unsigned long bit;
379
380 bit = simple_strtoul(buf, NULL, 10);
381 if (bit & ~1)
382 return -EINVAL;
383
384 mutex_lock(&data->update_lock);
385 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
386 if (bit)
387 data->beep_mask |= (1 << bitnr);
388 else
389 data->beep_mask &= ~(1 << bitnr);
390 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
391 mutex_unlock(&data->update_lock);
392 return count;
393}
394
395static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
396static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
397static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
398static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
399static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
400static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
401static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
402
Jean Delvare87808be2006-09-24 21:17:13 +0200403static struct attribute *gl518_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +0200404 &dev_attr_in3_input.attr,
405 &dev_attr_in0_min.attr,
406 &dev_attr_in1_min.attr,
407 &dev_attr_in2_min.attr,
408 &dev_attr_in3_min.attr,
409 &dev_attr_in0_max.attr,
410 &dev_attr_in1_max.attr,
411 &dev_attr_in2_max.attr,
412 &dev_attr_in3_max.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200413 &sensor_dev_attr_in0_alarm.dev_attr.attr,
414 &sensor_dev_attr_in1_alarm.dev_attr.attr,
415 &sensor_dev_attr_in2_alarm.dev_attr.attr,
416 &sensor_dev_attr_in3_alarm.dev_attr.attr,
417 &sensor_dev_attr_in0_beep.dev_attr.attr,
418 &sensor_dev_attr_in1_beep.dev_attr.attr,
419 &sensor_dev_attr_in2_beep.dev_attr.attr,
420 &sensor_dev_attr_in3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200421
422 &dev_attr_fan1_auto.attr,
Jean Delvare28292e72007-10-22 17:46:42 +0200423 &sensor_dev_attr_fan1_input.dev_attr.attr,
424 &sensor_dev_attr_fan2_input.dev_attr.attr,
425 &sensor_dev_attr_fan1_min.dev_attr.attr,
426 &sensor_dev_attr_fan2_min.dev_attr.attr,
427 &sensor_dev_attr_fan1_div.dev_attr.attr,
428 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200429 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
430 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
431 &sensor_dev_attr_fan1_beep.dev_attr.attr,
432 &sensor_dev_attr_fan2_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200433
434 &dev_attr_temp1_input.attr,
435 &dev_attr_temp1_max.attr,
436 &dev_attr_temp1_max_hyst.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200437 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
438 &sensor_dev_attr_temp1_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200439
440 &dev_attr_alarms.attr,
441 &dev_attr_beep_enable.attr,
442 &dev_attr_beep_mask.attr,
443 NULL
444};
445
446static const struct attribute_group gl518_group = {
447 .attrs = gl518_attributes,
448};
449
Jean Delvare5314f5c2007-10-22 17:46:17 +0200450static struct attribute *gl518_attributes_r80[] = {
451 &dev_attr_in0_input.attr,
452 &dev_attr_in1_input.attr,
453 &dev_attr_in2_input.attr,
454 NULL
455};
456
457static const struct attribute_group gl518_group_r80 = {
458 .attrs = gl518_attributes_r80,
459};
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
462 * Real code
463 */
464
465static int gl518_attach_adapter(struct i2c_adapter *adapter)
466{
467 if (!(adapter->class & I2C_CLASS_HWMON))
468 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200469 return i2c_probe(adapter, &addr_data, gl518_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
473{
474 int i;
Jean Delvarea5955ed2007-10-22 17:45:08 +0200475 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct gl518_data *data;
477 int err = 0;
478
479 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
480 I2C_FUNC_SMBUS_WORD_DATA))
481 goto exit;
482
483 /* OK. For now, we presume we have a valid client. We now create the
484 client structure, even though we cannot fill it completely yet.
485 But it allows us to access gl518_{read,write}_value. */
486
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200487 if (!(data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 err = -ENOMEM;
489 goto exit;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Jean Delvarea5955ed2007-10-22 17:45:08 +0200492 client = &data->client;
493 i2c_set_clientdata(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jean Delvarea5955ed2007-10-22 17:45:08 +0200495 client->addr = address;
496 client->adapter = adapter;
497 client->driver = &gl518_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Now, we do the remaining detection. */
500
501 if (kind < 0) {
Jean Delvarea5955ed2007-10-22 17:45:08 +0200502 if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
503 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto exit_free;
505 }
506
507 /* Determine the chip type. */
508 if (kind <= 0) {
Jean Delvarea5955ed2007-10-22 17:45:08 +0200509 i = gl518_read_value(client, GL518_REG_REVISION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (i == 0x00) {
511 kind = gl518sm_r00;
512 } else if (i == 0x80) {
513 kind = gl518sm_r80;
514 } else {
515 if (kind <= 0)
516 dev_info(&adapter->dev,
517 "Ignoring 'force' parameter for unknown "
518 "chip at adapter %d, address 0x%02x\n",
519 i2c_adapter_id(adapter), address);
520 goto exit_free;
521 }
522 }
523
524 /* Fill in the remaining client fields */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200525 strlcpy(client->name, "gl518sm", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 data->type = kind;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100527 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* Tell the I2C layer a new client has arrived */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200530 if ((err = i2c_attach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto exit_free;
532
533 /* Initialize the GL518SM chip */
534 data->alarm_mask = 0xff;
Jean Delvarea5955ed2007-10-22 17:45:08 +0200535 gl518_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* Register sysfs hooks */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200538 if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
Jean Delvare87808be2006-09-24 21:17:13 +0200539 goto exit_detach;
Jean Delvare5314f5c2007-10-22 17:46:17 +0200540 if (data->type == gl518sm_r80)
541 if ((err = sysfs_create_group(&client->dev.kobj,
542 &gl518_group_r80)))
543 goto exit_remove_files;
Jean Delvare87808be2006-09-24 21:17:13 +0200544
Jean Delvarea5955ed2007-10-22 17:45:08 +0200545 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700546 if (IS_ERR(data->hwmon_dev)) {
547 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200548 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400549 }
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552
Jean Delvare87808be2006-09-24 21:17:13 +0200553exit_remove_files:
Jean Delvarea5955ed2007-10-22 17:45:08 +0200554 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200555 if (data->type == gl518sm_r80)
556 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400557exit_detach:
Jean Delvarea5955ed2007-10-22 17:45:08 +0200558 i2c_detach_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559exit_free:
560 kfree(data);
561exit:
562 return err;
563}
564
565
566/* Called when we have found a new GL518SM.
567 Note that we preserve D4:NoFan2 and D2:beep_enable. */
568static void gl518_init_client(struct i2c_client *client)
569{
570 /* Make sure we leave D7:Reset untouched */
571 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
572
573 /* Comparator mode (D3=0), standby mode (D6=0) */
574 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
575
576 /* Never interrupts */
577 gl518_write_value(client, GL518_REG_MASK, 0x00);
578
579 /* Clear status register (D5=1), start (D6=1) */
580 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
581 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
582}
583
584static int gl518_detach_client(struct i2c_client *client)
585{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400586 struct gl518_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int err;
588
Tony Jones1beeffe2007-08-20 13:46:20 -0700589 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200590 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200591 if (data->type == gl518sm_r80)
592 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400593
Jean Delvare7bef5592005-07-27 22:14:49 +0200594 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400597 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599}
600
Jean Delvarea5955ed2007-10-22 17:45:08 +0200601/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 GL518 uses a high-byte first convention, which is exactly opposite to
Jean Delvarea5955ed2007-10-22 17:45:08 +0200603 the SMBus standard. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static int gl518_read_value(struct i2c_client *client, u8 reg)
605{
606 if ((reg >= 0x07) && (reg <= 0x0c))
607 return swab16(i2c_smbus_read_word_data(client, reg));
608 else
609 return i2c_smbus_read_byte_data(client, reg);
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
613{
614 if ((reg >= 0x07) && (reg <= 0x0c))
615 return i2c_smbus_write_word_data(client, reg, swab16(value));
616 else
617 return i2c_smbus_write_byte_data(client, reg, value);
618}
619
620static struct gl518_data *gl518_update_device(struct device *dev)
621{
622 struct i2c_client *client = to_i2c_client(dev);
623 struct gl518_data *data = i2c_get_clientdata(client);
624 int val;
625
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100626 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
629 || !data->valid) {
630 dev_dbg(&client->dev, "Starting gl518 update\n");
631
632 data->alarms = gl518_read_value(client, GL518_REG_INT);
633 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
634
635 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
636 data->voltage_min[0] = val & 0xff;
637 data->voltage_max[0] = (val >> 8) & 0xff;
638 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
639 data->voltage_min[1] = val & 0xff;
640 data->voltage_max[1] = (val >> 8) & 0xff;
641 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
642 data->voltage_min[2] = val & 0xff;
643 data->voltage_max[2] = (val >> 8) & 0xff;
644 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
645 data->voltage_min[3] = val & 0xff;
646 data->voltage_max[3] = (val >> 8) & 0xff;
647
648 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
649 data->fan_in[0] = (val >> 8) & 0xff;
650 data->fan_in[1] = val & 0xff;
651
652 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
653 data->fan_min[0] = (val >> 8) & 0xff;
654 data->fan_min[1] = val & 0xff;
655
656 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
657 data->temp_max =
658 gl518_read_value(client, GL518_REG_TEMP_MAX);
659 data->temp_hyst =
660 gl518_read_value(client, GL518_REG_TEMP_HYST);
661
662 val = gl518_read_value(client, GL518_REG_MISC);
663 data->fan_div[0] = (val >> 6) & 0x03;
664 data->fan_div[1] = (val >> 4) & 0x03;
665 data->fan_auto1 = (val >> 3) & 0x01;
666
667 data->alarms &= data->alarm_mask;
668
669 val = gl518_read_value(client, GL518_REG_CONF);
670 data->beep_enable = (val >> 2) & 1;
671
672 if (data->type != gl518sm_r00) {
673 data->voltage_in[0] =
674 gl518_read_value(client, GL518_REG_VDD);
675 data->voltage_in[1] =
676 gl518_read_value(client, GL518_REG_VIN1);
677 data->voltage_in[2] =
678 gl518_read_value(client, GL518_REG_VIN2);
679 }
680 data->voltage_in[3] =
681 gl518_read_value(client, GL518_REG_VIN3);
682
683 data->last_updated = jiffies;
684 data->valid = 1;
685 }
686
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100687 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return data;
690}
691
692static int __init sensors_gl518sm_init(void)
693{
694 return i2c_add_driver(&gl518_driver);
695}
696
697static void __exit sensors_gl518sm_exit(void)
698{
699 i2c_del_driver(&gl518_driver);
700}
701
702MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
703 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
704 "Hong-Gunn Chew <hglinux@gunnet.org>");
705MODULE_DESCRIPTION("GL518SM driver");
706MODULE_LICENSE("GPL");
707
708module_init(sensors_gl518sm_init);
709module_exit(sensors_gl518sm_exit);