blob: 33e9e8a8d1cea07d3613b3d1f67d2390fcadf6ac [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 */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050047static const 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;
Jean Delvare72240302007-10-23 14:02:24 +020099 rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) * div;
100 return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Jean Delvare72240302007-10-23 14:02:24 +0200102#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val)*(div))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define DIV_FROM_REG(val) (1 << (val))
111
112#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
113#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
114
115/* Each client has this additional data */
116struct gl518_data {
117 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700118 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 enum chips type;
120
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100121 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 char valid; /* !=0 if following fields are valid */
123 unsigned long last_updated; /* In jiffies */
124
125 u8 voltage_in[4]; /* Register values; [0] = VDD */
126 u8 voltage_min[4]; /* Register values; [0] = VDD */
127 u8 voltage_max[4]; /* Register values; [0] = VDD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 u8 fan_in[2];
129 u8 fan_min[2];
130 u8 fan_div[2]; /* Register encoding, shifted right */
131 u8 fan_auto1; /* Boolean */
132 u8 temp_in; /* Register values */
133 u8 temp_max; /* Register values */
134 u8 temp_hyst; /* Register values */
135 u8 alarms; /* Register value */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200136 u8 alarm_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 u8 beep_mask; /* Register value */
138 u8 beep_enable; /* Boolean */
139};
140
141static int gl518_attach_adapter(struct i2c_adapter *adapter);
142static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
143static void gl518_init_client(struct i2c_client *client);
144static int gl518_detach_client(struct i2c_client *client);
145static int gl518_read_value(struct i2c_client *client, u8 reg);
146static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
147static struct gl518_data *gl518_update_device(struct device *dev);
148
149/* This is the driver that will be inserted */
150static struct i2c_driver gl518_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100151 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100152 .name = "gl518sm",
153 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 .attach_adapter = gl518_attach_adapter,
155 .detach_client = gl518_detach_client,
156};
157
158/*
159 * Sysfs stuff
160 */
161
162#define show(type, suffix, value) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400163static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{ \
165 struct gl518_data *data = gl518_update_device(dev); \
166 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169show(TEMP, temp_input1, temp_in);
170show(TEMP, temp_max1, temp_max);
171show(TEMP, temp_hyst1, temp_hyst);
172show(BOOL, fan_auto1, fan_auto1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173show(VDD, in_input0, voltage_in[0]);
174show(IN, in_input1, voltage_in[1]);
175show(IN, in_input2, voltage_in[2]);
176show(IN, in_input3, voltage_in[3]);
177show(VDD, in_min0, voltage_min[0]);
178show(IN, in_min1, voltage_min[1]);
179show(IN, in_min2, voltage_min[2]);
180show(IN, in_min3, voltage_min[3]);
181show(VDD, in_max0, voltage_max[0]);
182show(IN, in_max1, voltage_max[1]);
183show(IN, in_max2, voltage_max[2]);
184show(IN, in_max3, voltage_max[3]);
185show(RAW, alarms, alarms);
186show(BOOL, beep_enable, beep_enable);
187show(BEEP_MASK, beep_mask, beep_mask);
188
Jean Delvare28292e72007-10-22 17:46:42 +0200189static ssize_t show_fan_input(struct device *dev,
190 struct device_attribute *attr, char *buf)
191{
192 int nr = to_sensor_dev_attr(attr)->index;
193 struct gl518_data *data = gl518_update_device(dev);
194 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
195 DIV_FROM_REG(data->fan_div[nr])));
196}
197
198static ssize_t show_fan_min(struct device *dev,
199 struct device_attribute *attr, char *buf)
200{
201 int nr = to_sensor_dev_attr(attr)->index;
202 struct gl518_data *data = gl518_update_device(dev);
203 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
204 DIV_FROM_REG(data->fan_div[nr])));
205}
206
207static ssize_t show_fan_div(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 int nr = to_sensor_dev_attr(attr)->index;
211 struct gl518_data *data = gl518_update_device(dev);
212 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#define set(type, suffix, value, reg) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400216static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 size_t count) \
218{ \
219 struct i2c_client *client = to_i2c_client(dev); \
220 struct gl518_data *data = i2c_get_clientdata(client); \
221 long val = simple_strtol(buf, NULL, 10); \
222 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100223 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 data->value = type##_TO_REG(val); \
225 gl518_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100226 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return count; \
228}
229
230#define set_bits(type, suffix, value, reg, mask, shift) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400231static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 size_t count) \
233{ \
234 struct i2c_client *client = to_i2c_client(dev); \
235 struct gl518_data *data = i2c_get_clientdata(client); \
236 int regvalue; \
237 unsigned long val = simple_strtoul(buf, NULL, 10); \
238 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100239 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 regvalue = gl518_read_value(client, reg); \
241 data->value = type##_TO_REG(val); \
242 regvalue = (regvalue & ~mask) | (data->value << shift); \
243 gl518_write_value(client, reg, regvalue); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100244 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return count; \
246}
247
248#define set_low(type, suffix, value, reg) \
249 set_bits(type, suffix, value, reg, 0x00ff, 0)
250#define set_high(type, suffix, value, reg) \
251 set_bits(type, suffix, value, reg, 0xff00, 8)
252
253set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
254set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
255set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
257set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
258set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
259set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
260set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
261set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
262set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
263set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
264set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
265set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
266
Jean Delvare28292e72007-10-22 17:46:42 +0200267static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
268 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct i2c_client *client = to_i2c_client(dev);
271 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200272 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int regvalue;
274 unsigned long val = simple_strtoul(buf, NULL, 10);
275
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100276 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
Jean Delvare28292e72007-10-22 17:46:42 +0200278 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
279 regvalue = (regvalue & (0xff << (8 * nr)))
280 | (data->fan_min[nr] << (8 * (1 - nr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
282
283 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
Jean Delvare28292e72007-10-22 17:46:42 +0200284 if (data->fan_min[nr] == 0)
285 data->alarm_mask &= ~(0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 else
Jean Delvare28292e72007-10-22 17:46:42 +0200287 data->alarm_mask |= (0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 data->beep_mask &= data->alarm_mask;
289 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
290
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100291 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return count;
293}
294
Jean Delvare28292e72007-10-22 17:46:42 +0200295static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct i2c_client *client = to_i2c_client(dev);
299 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200300 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int regvalue;
302 unsigned long val = simple_strtoul(buf, NULL, 10);
303
Jean Delvare21df67b2007-10-22 17:47:58 +0200304 switch (val) {
305 case 1: val = 0; break;
306 case 2: val = 1; break;
307 case 4: val = 2; break;
308 case 8: val = 3; break;
309 default:
310 dev_err(dev, "Invalid fan clock divider %lu, choose one "
311 "of 1, 2, 4 or 8\n", val);
312 return -EINVAL;
313 }
314
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 mutex_lock(&data->update_lock);
Jean Delvare28292e72007-10-22 17:46:42 +0200316 regvalue = gl518_read_value(client, GL518_REG_MISC);
Jean Delvare21df67b2007-10-22 17:47:58 +0200317 data->fan_div[nr] = val;
Jean Delvare28292e72007-10-22 17:46:42 +0200318 regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
319 | (data->fan_div[nr] << (6 - 2 * nr));
320 gl518_write_value(client, GL518_REG_MISC, regvalue);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100321 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return count;
323}
324
325static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
326static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
327static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
328 show_temp_hyst1, set_temp_hyst1);
329static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
Jean Delvare28292e72007-10-22 17:46:42 +0200330static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
331static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
332static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
333 show_fan_min, set_fan_min, 0);
334static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
335 show_fan_min, set_fan_min, 1);
336static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
337 show_fan_div, set_fan_div, 0);
338static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
339 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
341static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
342static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
343static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
344static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
345static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
346static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
347static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
348static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
349static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
350static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
351static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
352static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
353static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
354 show_beep_enable, set_beep_enable);
355static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
356 show_beep_mask, set_beep_mask);
357
Jean Delvareda6848d2007-10-22 17:47:16 +0200358static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
359 char *buf)
360{
361 int bitnr = to_sensor_dev_attr(attr)->index;
362 struct gl518_data *data = gl518_update_device(dev);
363 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
364}
365
366static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
367static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
368static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
369static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
370static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
371static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
372static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
373
374static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
375 char *buf)
376{
377 int bitnr = to_sensor_dev_attr(attr)->index;
378 struct gl518_data *data = gl518_update_device(dev);
379 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
380}
381
382static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
383 const char *buf, size_t count)
384{
385 struct i2c_client *client = to_i2c_client(dev);
386 struct gl518_data *data = i2c_get_clientdata(client);
387 int bitnr = to_sensor_dev_attr(attr)->index;
388 unsigned long bit;
389
390 bit = simple_strtoul(buf, NULL, 10);
391 if (bit & ~1)
392 return -EINVAL;
393
394 mutex_lock(&data->update_lock);
395 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
396 if (bit)
397 data->beep_mask |= (1 << bitnr);
398 else
399 data->beep_mask &= ~(1 << bitnr);
400 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
401 mutex_unlock(&data->update_lock);
402 return count;
403}
404
405static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
406static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
407static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
408static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
409static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
410static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
411static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
412
Jean Delvare87808be2006-09-24 21:17:13 +0200413static struct attribute *gl518_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +0200414 &dev_attr_in3_input.attr,
415 &dev_attr_in0_min.attr,
416 &dev_attr_in1_min.attr,
417 &dev_attr_in2_min.attr,
418 &dev_attr_in3_min.attr,
419 &dev_attr_in0_max.attr,
420 &dev_attr_in1_max.attr,
421 &dev_attr_in2_max.attr,
422 &dev_attr_in3_max.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200423 &sensor_dev_attr_in0_alarm.dev_attr.attr,
424 &sensor_dev_attr_in1_alarm.dev_attr.attr,
425 &sensor_dev_attr_in2_alarm.dev_attr.attr,
426 &sensor_dev_attr_in3_alarm.dev_attr.attr,
427 &sensor_dev_attr_in0_beep.dev_attr.attr,
428 &sensor_dev_attr_in1_beep.dev_attr.attr,
429 &sensor_dev_attr_in2_beep.dev_attr.attr,
430 &sensor_dev_attr_in3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200431
432 &dev_attr_fan1_auto.attr,
Jean Delvare28292e72007-10-22 17:46:42 +0200433 &sensor_dev_attr_fan1_input.dev_attr.attr,
434 &sensor_dev_attr_fan2_input.dev_attr.attr,
435 &sensor_dev_attr_fan1_min.dev_attr.attr,
436 &sensor_dev_attr_fan2_min.dev_attr.attr,
437 &sensor_dev_attr_fan1_div.dev_attr.attr,
438 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200439 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
440 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
441 &sensor_dev_attr_fan1_beep.dev_attr.attr,
442 &sensor_dev_attr_fan2_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200443
444 &dev_attr_temp1_input.attr,
445 &dev_attr_temp1_max.attr,
446 &dev_attr_temp1_max_hyst.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200447 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
448 &sensor_dev_attr_temp1_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200449
450 &dev_attr_alarms.attr,
451 &dev_attr_beep_enable.attr,
452 &dev_attr_beep_mask.attr,
453 NULL
454};
455
456static const struct attribute_group gl518_group = {
457 .attrs = gl518_attributes,
458};
459
Jean Delvare5314f5c2007-10-22 17:46:17 +0200460static struct attribute *gl518_attributes_r80[] = {
461 &dev_attr_in0_input.attr,
462 &dev_attr_in1_input.attr,
463 &dev_attr_in2_input.attr,
464 NULL
465};
466
467static const struct attribute_group gl518_group_r80 = {
468 .attrs = gl518_attributes_r80,
469};
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
472 * Real code
473 */
474
475static int gl518_attach_adapter(struct i2c_adapter *adapter)
476{
477 if (!(adapter->class & I2C_CLASS_HWMON))
478 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200479 return i2c_probe(adapter, &addr_data, gl518_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
483{
484 int i;
Jean Delvarea5955ed2007-10-22 17:45:08 +0200485 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct gl518_data *data;
487 int err = 0;
488
489 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
490 I2C_FUNC_SMBUS_WORD_DATA))
491 goto exit;
492
493 /* OK. For now, we presume we have a valid client. We now create the
494 client structure, even though we cannot fill it completely yet.
495 But it allows us to access gl518_{read,write}_value. */
496
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200497 if (!(data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 err = -ENOMEM;
499 goto exit;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jean Delvarea5955ed2007-10-22 17:45:08 +0200502 client = &data->client;
503 i2c_set_clientdata(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Jean Delvarea5955ed2007-10-22 17:45:08 +0200505 client->addr = address;
506 client->adapter = adapter;
507 client->driver = &gl518_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* Now, we do the remaining detection. */
510
511 if (kind < 0) {
Jean Delvarea5955ed2007-10-22 17:45:08 +0200512 if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
513 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto exit_free;
515 }
516
517 /* Determine the chip type. */
518 if (kind <= 0) {
Jean Delvarea5955ed2007-10-22 17:45:08 +0200519 i = gl518_read_value(client, GL518_REG_REVISION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (i == 0x00) {
521 kind = gl518sm_r00;
522 } else if (i == 0x80) {
523 kind = gl518sm_r80;
524 } else {
525 if (kind <= 0)
526 dev_info(&adapter->dev,
527 "Ignoring 'force' parameter for unknown "
528 "chip at adapter %d, address 0x%02x\n",
529 i2c_adapter_id(adapter), address);
530 goto exit_free;
531 }
532 }
533
534 /* Fill in the remaining client fields */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200535 strlcpy(client->name, "gl518sm", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 data->type = kind;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100537 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /* Tell the I2C layer a new client has arrived */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200540 if ((err = i2c_attach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto exit_free;
542
543 /* Initialize the GL518SM chip */
544 data->alarm_mask = 0xff;
Jean Delvarea5955ed2007-10-22 17:45:08 +0200545 gl518_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* Register sysfs hooks */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200548 if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
Jean Delvare87808be2006-09-24 21:17:13 +0200549 goto exit_detach;
Jean Delvare5314f5c2007-10-22 17:46:17 +0200550 if (data->type == gl518sm_r80)
551 if ((err = sysfs_create_group(&client->dev.kobj,
552 &gl518_group_r80)))
553 goto exit_remove_files;
Jean Delvare87808be2006-09-24 21:17:13 +0200554
Jean Delvarea5955ed2007-10-22 17:45:08 +0200555 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700556 if (IS_ERR(data->hwmon_dev)) {
557 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200558 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400559 }
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
562
Jean Delvare87808be2006-09-24 21:17:13 +0200563exit_remove_files:
Jean Delvarea5955ed2007-10-22 17:45:08 +0200564 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200565 if (data->type == gl518sm_r80)
566 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400567exit_detach:
Jean Delvarea5955ed2007-10-22 17:45:08 +0200568 i2c_detach_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569exit_free:
570 kfree(data);
571exit:
572 return err;
573}
574
575
576/* Called when we have found a new GL518SM.
577 Note that we preserve D4:NoFan2 and D2:beep_enable. */
578static void gl518_init_client(struct i2c_client *client)
579{
580 /* Make sure we leave D7:Reset untouched */
581 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
582
583 /* Comparator mode (D3=0), standby mode (D6=0) */
584 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
585
586 /* Never interrupts */
587 gl518_write_value(client, GL518_REG_MASK, 0x00);
588
589 /* Clear status register (D5=1), start (D6=1) */
590 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
591 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
592}
593
594static int gl518_detach_client(struct i2c_client *client)
595{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400596 struct gl518_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int err;
598
Tony Jones1beeffe2007-08-20 13:46:20 -0700599 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200600 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200601 if (data->type == gl518sm_r80)
602 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400603
Jean Delvare7bef5592005-07-27 22:14:49 +0200604 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400607 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
609}
610
Jean Delvarea5955ed2007-10-22 17:45:08 +0200611/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 GL518 uses a high-byte first convention, which is exactly opposite to
Jean Delvarea5955ed2007-10-22 17:45:08 +0200613 the SMBus standard. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static int gl518_read_value(struct i2c_client *client, u8 reg)
615{
616 if ((reg >= 0x07) && (reg <= 0x0c))
617 return swab16(i2c_smbus_read_word_data(client, reg));
618 else
619 return i2c_smbus_read_byte_data(client, reg);
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
623{
624 if ((reg >= 0x07) && (reg <= 0x0c))
625 return i2c_smbus_write_word_data(client, reg, swab16(value));
626 else
627 return i2c_smbus_write_byte_data(client, reg, value);
628}
629
630static struct gl518_data *gl518_update_device(struct device *dev)
631{
632 struct i2c_client *client = to_i2c_client(dev);
633 struct gl518_data *data = i2c_get_clientdata(client);
634 int val;
635
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100636 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
639 || !data->valid) {
640 dev_dbg(&client->dev, "Starting gl518 update\n");
641
642 data->alarms = gl518_read_value(client, GL518_REG_INT);
643 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
644
645 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
646 data->voltage_min[0] = val & 0xff;
647 data->voltage_max[0] = (val >> 8) & 0xff;
648 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
649 data->voltage_min[1] = val & 0xff;
650 data->voltage_max[1] = (val >> 8) & 0xff;
651 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
652 data->voltage_min[2] = val & 0xff;
653 data->voltage_max[2] = (val >> 8) & 0xff;
654 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
655 data->voltage_min[3] = val & 0xff;
656 data->voltage_max[3] = (val >> 8) & 0xff;
657
658 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
659 data->fan_in[0] = (val >> 8) & 0xff;
660 data->fan_in[1] = val & 0xff;
661
662 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
663 data->fan_min[0] = (val >> 8) & 0xff;
664 data->fan_min[1] = val & 0xff;
665
666 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
667 data->temp_max =
668 gl518_read_value(client, GL518_REG_TEMP_MAX);
669 data->temp_hyst =
670 gl518_read_value(client, GL518_REG_TEMP_HYST);
671
672 val = gl518_read_value(client, GL518_REG_MISC);
673 data->fan_div[0] = (val >> 6) & 0x03;
674 data->fan_div[1] = (val >> 4) & 0x03;
675 data->fan_auto1 = (val >> 3) & 0x01;
676
677 data->alarms &= data->alarm_mask;
678
679 val = gl518_read_value(client, GL518_REG_CONF);
680 data->beep_enable = (val >> 2) & 1;
681
682 if (data->type != gl518sm_r00) {
683 data->voltage_in[0] =
684 gl518_read_value(client, GL518_REG_VDD);
685 data->voltage_in[1] =
686 gl518_read_value(client, GL518_REG_VIN1);
687 data->voltage_in[2] =
688 gl518_read_value(client, GL518_REG_VIN2);
689 }
690 data->voltage_in[3] =
691 gl518_read_value(client, GL518_REG_VIN3);
692
693 data->last_updated = jiffies;
694 data->valid = 1;
695 }
696
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100697 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return data;
700}
701
702static int __init sensors_gl518sm_init(void)
703{
704 return i2c_add_driver(&gl518_driver);
705}
706
707static void __exit sensors_gl518sm_exit(void)
708{
709 i2c_del_driver(&gl518_driver);
710}
711
712MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
713 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
714 "Hong-Gunn Chew <hglinux@gunnet.org>");
715MODULE_DESCRIPTION("GL518SM driver");
716MODULE_LICENSE("GPL");
717
718module_init(sensors_gl518sm_init);
719module_exit(sensors_gl518sm_exit);