blob: a13e2da97e3099957b157fc088e8f62dad2327c0 [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
Jean Delvaree5e9f442009-12-14 21:17:27 +010049enum chips { gl518sm_r00, gl518sm_r80 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* Many GL518 constants specified below */
52
53/* The GL518 registers */
54#define GL518_REG_CHIP_ID 0x00
55#define GL518_REG_REVISION 0x01
56#define GL518_REG_VENDOR_ID 0x02
57#define GL518_REG_CONF 0x03
58#define GL518_REG_TEMP_IN 0x04
59#define GL518_REG_TEMP_MAX 0x05
60#define GL518_REG_TEMP_HYST 0x06
61#define GL518_REG_FAN_COUNT 0x07
62#define GL518_REG_FAN_LIMIT 0x08
63#define GL518_REG_VIN1_LIMIT 0x09
64#define GL518_REG_VIN2_LIMIT 0x0a
65#define GL518_REG_VIN3_LIMIT 0x0b
66#define GL518_REG_VDD_LIMIT 0x0c
67#define GL518_REG_VIN3 0x0d
68#define GL518_REG_MISC 0x0f
69#define GL518_REG_ALARM 0x10
70#define GL518_REG_MASK 0x11
71#define GL518_REG_INT 0x12
72#define GL518_REG_VIN2 0x13
73#define GL518_REG_VIN1 0x14
74#define GL518_REG_VDD 0x15
75
76
77/*
78 * Conversions. Rounding and limit checking is only done on the TO_REG
79 * variants. Note that you should be a bit careful with which arguments
80 * these macros are called: arguments may be evaluated more than once.
81 * Fixing this is just not worth it.
82 */
83
84#define RAW_FROM_REG(val) val
85
86#define BOOL_FROM_REG(val) ((val)?0:1)
87#define BOOL_TO_REG(val) ((val)?0:1)
88
89#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
90 (val)-500:(val)+500)/1000)+119),0,255))
91#define TEMP_FROM_REG(val) (((val) - 119) * 1000)
92
93static inline u8 FAN_TO_REG(long rpm, int div)
94{
95 long rpmdiv;
96 if (rpm == 0)
97 return 0;
Jean Delvare72240302007-10-23 14:02:24 +020098 rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) * div;
99 return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Jean Delvare72240302007-10-23 14:02:24 +0200101#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val)*(div))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
104#define IN_FROM_REG(val) ((val)*19)
105
106#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
107#define VDD_FROM_REG(val) (((val)*95+2)/4)
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define DIV_FROM_REG(val) (1 << (val))
110
111#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
112#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
113
114/* Each client has this additional data */
115struct gl518_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700116 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 enum chips type;
118
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100119 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 char valid; /* !=0 if following fields are valid */
121 unsigned long last_updated; /* In jiffies */
122
123 u8 voltage_in[4]; /* Register values; [0] = VDD */
124 u8 voltage_min[4]; /* Register values; [0] = VDD */
125 u8 voltage_max[4]; /* Register values; [0] = VDD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 u8 fan_in[2];
127 u8 fan_min[2];
128 u8 fan_div[2]; /* Register encoding, shifted right */
129 u8 fan_auto1; /* Boolean */
130 u8 temp_in; /* Register values */
131 u8 temp_max; /* Register values */
132 u8 temp_hyst; /* Register values */
133 u8 alarms; /* Register value */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200134 u8 alarm_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 u8 beep_mask; /* Register value */
136 u8 beep_enable; /* Boolean */
137};
138
Jean Delvare95d80e72008-07-16 19:30:13 +0200139static int gl518_probe(struct i2c_client *client,
140 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100141static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static void gl518_init_client(struct i2c_client *client);
Jean Delvare95d80e72008-07-16 19:30:13 +0200143static int gl518_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int gl518_read_value(struct i2c_client *client, u8 reg);
145static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
146static struct gl518_data *gl518_update_device(struct device *dev);
147
Jean Delvare95d80e72008-07-16 19:30:13 +0200148static const struct i2c_device_id gl518_id[] = {
149 { "gl518sm", 0 },
150 { }
151};
152MODULE_DEVICE_TABLE(i2c, gl518_id);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/* This is the driver that will be inserted */
155static struct i2c_driver gl518_driver = {
Jean Delvare95d80e72008-07-16 19:30:13 +0200156 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100157 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100158 .name = "gl518sm",
159 },
Jean Delvare95d80e72008-07-16 19:30:13 +0200160 .probe = gl518_probe,
161 .remove = gl518_remove,
162 .id_table = gl518_id,
163 .detect = gl518_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100164 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167/*
168 * Sysfs stuff
169 */
170
171#define show(type, suffix, value) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400172static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{ \
174 struct gl518_data *data = gl518_update_device(dev); \
175 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178show(TEMP, temp_input1, temp_in);
179show(TEMP, temp_max1, temp_max);
180show(TEMP, temp_hyst1, temp_hyst);
181show(BOOL, fan_auto1, fan_auto1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182show(VDD, in_input0, voltage_in[0]);
183show(IN, in_input1, voltage_in[1]);
184show(IN, in_input2, voltage_in[2]);
185show(IN, in_input3, voltage_in[3]);
186show(VDD, in_min0, voltage_min[0]);
187show(IN, in_min1, voltage_min[1]);
188show(IN, in_min2, voltage_min[2]);
189show(IN, in_min3, voltage_min[3]);
190show(VDD, in_max0, voltage_max[0]);
191show(IN, in_max1, voltage_max[1]);
192show(IN, in_max2, voltage_max[2]);
193show(IN, in_max3, voltage_max[3]);
194show(RAW, alarms, alarms);
195show(BOOL, beep_enable, beep_enable);
196show(BEEP_MASK, beep_mask, beep_mask);
197
Jean Delvare28292e72007-10-22 17:46:42 +0200198static ssize_t show_fan_input(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_in[nr],
204 DIV_FROM_REG(data->fan_div[nr])));
205}
206
207static ssize_t show_fan_min(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", FAN_FROM_REG(data->fan_min[nr],
213 DIV_FROM_REG(data->fan_div[nr])));
214}
215
216static ssize_t show_fan_div(struct device *dev,
217 struct device_attribute *attr, char *buf)
218{
219 int nr = to_sensor_dev_attr(attr)->index;
220 struct gl518_data *data = gl518_update_device(dev);
221 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#define set(type, suffix, value, reg) \
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 long val = simple_strtol(buf, NULL, 10); \
231 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100232 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 data->value = type##_TO_REG(val); \
234 gl518_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100235 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return count; \
237}
238
239#define set_bits(type, suffix, value, reg, mask, shift) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400240static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 size_t count) \
242{ \
243 struct i2c_client *client = to_i2c_client(dev); \
244 struct gl518_data *data = i2c_get_clientdata(client); \
245 int regvalue; \
246 unsigned long val = simple_strtoul(buf, NULL, 10); \
247 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100248 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 regvalue = gl518_read_value(client, reg); \
250 data->value = type##_TO_REG(val); \
251 regvalue = (regvalue & ~mask) | (data->value << shift); \
252 gl518_write_value(client, reg, regvalue); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100253 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return count; \
255}
256
257#define set_low(type, suffix, value, reg) \
258 set_bits(type, suffix, value, reg, 0x00ff, 0)
259#define set_high(type, suffix, value, reg) \
260 set_bits(type, suffix, value, reg, 0xff00, 8)
261
262set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
263set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
264set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
266set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
267set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
268set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
269set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
270set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
271set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
272set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
273set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
274set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
275
Jean Delvare28292e72007-10-22 17:46:42 +0200276static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct i2c_client *client = to_i2c_client(dev);
280 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200281 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int regvalue;
283 unsigned long val = simple_strtoul(buf, NULL, 10);
284
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100285 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
Jean Delvare28292e72007-10-22 17:46:42 +0200287 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
288 regvalue = (regvalue & (0xff << (8 * nr)))
289 | (data->fan_min[nr] << (8 * (1 - nr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
291
292 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
Jean Delvare28292e72007-10-22 17:46:42 +0200293 if (data->fan_min[nr] == 0)
294 data->alarm_mask &= ~(0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 else
Jean Delvare28292e72007-10-22 17:46:42 +0200296 data->alarm_mask |= (0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 data->beep_mask &= data->alarm_mask;
298 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
299
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100300 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return count;
302}
303
Jean Delvare28292e72007-10-22 17:46:42 +0200304static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct i2c_client *client = to_i2c_client(dev);
308 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200309 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int regvalue;
311 unsigned long val = simple_strtoul(buf, NULL, 10);
312
Jean Delvare21df67b2007-10-22 17:47:58 +0200313 switch (val) {
314 case 1: val = 0; break;
315 case 2: val = 1; break;
316 case 4: val = 2; break;
317 case 8: val = 3; break;
318 default:
319 dev_err(dev, "Invalid fan clock divider %lu, choose one "
320 "of 1, 2, 4 or 8\n", val);
321 return -EINVAL;
322 }
323
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100324 mutex_lock(&data->update_lock);
Jean Delvare28292e72007-10-22 17:46:42 +0200325 regvalue = gl518_read_value(client, GL518_REG_MISC);
Jean Delvare21df67b2007-10-22 17:47:58 +0200326 data->fan_div[nr] = val;
Jean Delvare28292e72007-10-22 17:46:42 +0200327 regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
328 | (data->fan_div[nr] << (6 - 2 * nr));
329 gl518_write_value(client, GL518_REG_MISC, regvalue);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100330 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return count;
332}
333
334static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
335static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
336static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
337 show_temp_hyst1, set_temp_hyst1);
338static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
Jean Delvare28292e72007-10-22 17:46:42 +0200339static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
340static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
341static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
342 show_fan_min, set_fan_min, 0);
343static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
344 show_fan_min, set_fan_min, 1);
345static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
346 show_fan_div, set_fan_div, 0);
347static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
348 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
350static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
351static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
352static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
353static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
354static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
355static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
356static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
357static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
358static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
359static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
360static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
361static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
362static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
363 show_beep_enable, set_beep_enable);
364static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
365 show_beep_mask, set_beep_mask);
366
Jean Delvareda6848d2007-10-22 17:47:16 +0200367static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
368 char *buf)
369{
370 int bitnr = to_sensor_dev_attr(attr)->index;
371 struct gl518_data *data = gl518_update_device(dev);
372 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
373}
374
375static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
376static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
377static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
378static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
379static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
380static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
381static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
382
383static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
384 char *buf)
385{
386 int bitnr = to_sensor_dev_attr(attr)->index;
387 struct gl518_data *data = gl518_update_device(dev);
388 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
389}
390
391static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
392 const char *buf, size_t count)
393{
394 struct i2c_client *client = to_i2c_client(dev);
395 struct gl518_data *data = i2c_get_clientdata(client);
396 int bitnr = to_sensor_dev_attr(attr)->index;
397 unsigned long bit;
398
399 bit = simple_strtoul(buf, NULL, 10);
400 if (bit & ~1)
401 return -EINVAL;
402
403 mutex_lock(&data->update_lock);
404 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
405 if (bit)
406 data->beep_mask |= (1 << bitnr);
407 else
408 data->beep_mask &= ~(1 << bitnr);
409 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
410 mutex_unlock(&data->update_lock);
411 return count;
412}
413
414static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
415static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
416static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
417static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
418static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
419static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
420static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
421
Jean Delvare87808be2006-09-24 21:17:13 +0200422static struct attribute *gl518_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +0200423 &dev_attr_in3_input.attr,
424 &dev_attr_in0_min.attr,
425 &dev_attr_in1_min.attr,
426 &dev_attr_in2_min.attr,
427 &dev_attr_in3_min.attr,
428 &dev_attr_in0_max.attr,
429 &dev_attr_in1_max.attr,
430 &dev_attr_in2_max.attr,
431 &dev_attr_in3_max.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200432 &sensor_dev_attr_in0_alarm.dev_attr.attr,
433 &sensor_dev_attr_in1_alarm.dev_attr.attr,
434 &sensor_dev_attr_in2_alarm.dev_attr.attr,
435 &sensor_dev_attr_in3_alarm.dev_attr.attr,
436 &sensor_dev_attr_in0_beep.dev_attr.attr,
437 &sensor_dev_attr_in1_beep.dev_attr.attr,
438 &sensor_dev_attr_in2_beep.dev_attr.attr,
439 &sensor_dev_attr_in3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200440
441 &dev_attr_fan1_auto.attr,
Jean Delvare28292e72007-10-22 17:46:42 +0200442 &sensor_dev_attr_fan1_input.dev_attr.attr,
443 &sensor_dev_attr_fan2_input.dev_attr.attr,
444 &sensor_dev_attr_fan1_min.dev_attr.attr,
445 &sensor_dev_attr_fan2_min.dev_attr.attr,
446 &sensor_dev_attr_fan1_div.dev_attr.attr,
447 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200448 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
449 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
450 &sensor_dev_attr_fan1_beep.dev_attr.attr,
451 &sensor_dev_attr_fan2_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200452
453 &dev_attr_temp1_input.attr,
454 &dev_attr_temp1_max.attr,
455 &dev_attr_temp1_max_hyst.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200456 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
457 &sensor_dev_attr_temp1_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200458
459 &dev_attr_alarms.attr,
460 &dev_attr_beep_enable.attr,
461 &dev_attr_beep_mask.attr,
462 NULL
463};
464
465static const struct attribute_group gl518_group = {
466 .attrs = gl518_attributes,
467};
468
Jean Delvare5314f5c2007-10-22 17:46:17 +0200469static struct attribute *gl518_attributes_r80[] = {
470 &dev_attr_in0_input.attr,
471 &dev_attr_in1_input.attr,
472 &dev_attr_in2_input.attr,
473 NULL
474};
475
476static const struct attribute_group gl518_group_r80 = {
477 .attrs = gl518_attributes_r80,
478};
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * Real code
482 */
483
Jean Delvare95d80e72008-07-16 19:30:13 +0200484/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100485static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Jean Delvare95d80e72008-07-16 19:30:13 +0200487 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100488 int rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
491 I2C_FUNC_SMBUS_WORD_DATA))
Jean Delvare95d80e72008-07-16 19:30:13 +0200492 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Now, we do the remaining detection. */
Jean Delvare52df6442009-12-09 20:35:57 +0100495 if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
496 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
497 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Determine the chip type. */
Jean Delvare52df6442009-12-09 20:35:57 +0100500 rev = gl518_read_value(client, GL518_REG_REVISION);
501 if (rev != 0x00 && rev != 0x80)
502 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jean Delvare95d80e72008-07-16 19:30:13 +0200504 strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Jean Delvare95d80e72008-07-16 19:30:13 +0200506 return 0;
507}
508
509static int gl518_probe(struct i2c_client *client,
510 const struct i2c_device_id *id)
511{
512 struct gl518_data *data;
513 int err, revision;
514
515 data = kzalloc(sizeof(struct gl518_data), GFP_KERNEL);
516 if (!data) {
517 err = -ENOMEM;
518 goto exit;
519 }
520
521 i2c_set_clientdata(client, data);
522 revision = gl518_read_value(client, GL518_REG_REVISION);
523 data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
524 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* Initialize the GL518SM chip */
527 data->alarm_mask = 0xff;
Jean Delvarea5955ed2007-10-22 17:45:08 +0200528 gl518_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* Register sysfs hooks */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200531 if ((err = sysfs_create_group(&client->dev.kobj, &gl518_group)))
Jean Delvare95d80e72008-07-16 19:30:13 +0200532 goto exit_free;
Jean Delvare5314f5c2007-10-22 17:46:17 +0200533 if (data->type == gl518sm_r80)
534 if ((err = sysfs_create_group(&client->dev.kobj,
535 &gl518_group_r80)))
536 goto exit_remove_files;
Jean Delvare87808be2006-09-24 21:17:13 +0200537
Jean Delvarea5955ed2007-10-22 17:45:08 +0200538 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700539 if (IS_ERR(data->hwmon_dev)) {
540 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200541 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return 0;
545
Jean Delvare87808be2006-09-24 21:17:13 +0200546exit_remove_files:
Jean Delvarea5955ed2007-10-22 17:45:08 +0200547 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200548 if (data->type == gl518sm_r80)
549 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550exit_free:
551 kfree(data);
552exit:
553 return err;
554}
555
556
557/* Called when we have found a new GL518SM.
558 Note that we preserve D4:NoFan2 and D2:beep_enable. */
559static void gl518_init_client(struct i2c_client *client)
560{
561 /* Make sure we leave D7:Reset untouched */
562 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
563
564 /* Comparator mode (D3=0), standby mode (D6=0) */
565 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
566
567 /* Never interrupts */
568 gl518_write_value(client, GL518_REG_MASK, 0x00);
569
570 /* Clear status register (D5=1), start (D6=1) */
571 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
572 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
573}
574
Jean Delvare95d80e72008-07-16 19:30:13 +0200575static int gl518_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400577 struct gl518_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Tony Jones1beeffe2007-08-20 13:46:20 -0700579 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200580 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200581 if (data->type == gl518sm_r80)
582 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400583
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400584 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586}
587
Jean Delvarea5955ed2007-10-22 17:45:08 +0200588/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 GL518 uses a high-byte first convention, which is exactly opposite to
Jean Delvarea5955ed2007-10-22 17:45:08 +0200590 the SMBus standard. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static int gl518_read_value(struct i2c_client *client, u8 reg)
592{
593 if ((reg >= 0x07) && (reg <= 0x0c))
Jean Delvare90f41022011-11-04 12:00:47 +0100594 return i2c_smbus_read_word_swapped(client, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 else
596 return i2c_smbus_read_byte_data(client, reg);
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
600{
601 if ((reg >= 0x07) && (reg <= 0x0c))
Jean Delvare90f41022011-11-04 12:00:47 +0100602 return i2c_smbus_write_word_swapped(client, reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else
604 return i2c_smbus_write_byte_data(client, reg, value);
605}
606
607static struct gl518_data *gl518_update_device(struct device *dev)
608{
609 struct i2c_client *client = to_i2c_client(dev);
610 struct gl518_data *data = i2c_get_clientdata(client);
611 int val;
612
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100613 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
616 || !data->valid) {
617 dev_dbg(&client->dev, "Starting gl518 update\n");
618
619 data->alarms = gl518_read_value(client, GL518_REG_INT);
620 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
621
622 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
623 data->voltage_min[0] = val & 0xff;
624 data->voltage_max[0] = (val >> 8) & 0xff;
625 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
626 data->voltage_min[1] = val & 0xff;
627 data->voltage_max[1] = (val >> 8) & 0xff;
628 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
629 data->voltage_min[2] = val & 0xff;
630 data->voltage_max[2] = (val >> 8) & 0xff;
631 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
632 data->voltage_min[3] = val & 0xff;
633 data->voltage_max[3] = (val >> 8) & 0xff;
634
635 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
636 data->fan_in[0] = (val >> 8) & 0xff;
637 data->fan_in[1] = val & 0xff;
638
639 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
640 data->fan_min[0] = (val >> 8) & 0xff;
641 data->fan_min[1] = val & 0xff;
642
643 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
644 data->temp_max =
645 gl518_read_value(client, GL518_REG_TEMP_MAX);
646 data->temp_hyst =
647 gl518_read_value(client, GL518_REG_TEMP_HYST);
648
649 val = gl518_read_value(client, GL518_REG_MISC);
650 data->fan_div[0] = (val >> 6) & 0x03;
651 data->fan_div[1] = (val >> 4) & 0x03;
652 data->fan_auto1 = (val >> 3) & 0x01;
653
654 data->alarms &= data->alarm_mask;
655
656 val = gl518_read_value(client, GL518_REG_CONF);
657 data->beep_enable = (val >> 2) & 1;
658
659 if (data->type != gl518sm_r00) {
660 data->voltage_in[0] =
661 gl518_read_value(client, GL518_REG_VDD);
662 data->voltage_in[1] =
663 gl518_read_value(client, GL518_REG_VIN1);
664 data->voltage_in[2] =
665 gl518_read_value(client, GL518_REG_VIN2);
666 }
667 data->voltage_in[3] =
668 gl518_read_value(client, GL518_REG_VIN3);
669
670 data->last_updated = jiffies;
671 data->valid = 1;
672 }
673
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100674 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 return data;
677}
678
679static int __init sensors_gl518sm_init(void)
680{
681 return i2c_add_driver(&gl518_driver);
682}
683
684static void __exit sensors_gl518sm_exit(void)
685{
686 i2c_del_driver(&gl518_driver);
687}
688
689MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
690 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
691 "Hong-Gunn Chew <hglinux@gunnet.org>");
692MODULE_DESCRIPTION("GL518SM driver");
693MODULE_LICENSE("GPL");
694
695module_init(sensors_gl518sm_init);
696module_exit(sensors_gl518sm_exit);