blob: 0307d8218f872211f96330caf08de44a1fdad055 [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
Jean Delvare7c81c602014-01-29 20:40:08 +01007 * Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Guenter Roeck228f8e02012-01-14 13:32:55 -080086#define BOOL_FROM_REG(val) ((val) ? 0 : 1)
87#define BOOL_TO_REG(val) ((val) ? 0 : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Guenter Roeck2a844c12013-01-09 08:09:34 -080089#define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \
Guenter Roeck228f8e02012-01-14 13:32:55 -080090 (val) - 500 : \
91 (val) + 500) / 1000) + 119), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#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;
Guenter Roeck2a844c12013-01-09 08:09:34 -080099 rpmdiv = clamp_val(rpm, 1, 960000) * div;
100 return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Guenter Roeck228f8e02012-01-14 13:32:55 -0800102#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) * (div))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Guenter Roeck2a844c12013-01-09 08:09:34 -0800104#define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255)
Guenter Roeck228f8e02012-01-14 13:32:55 -0800105#define IN_FROM_REG(val) ((val) * 19)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Guenter Roeck2a844c12013-01-09 08:09:34 -0800107#define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255)
Guenter Roeck228f8e02012-01-14 13:32:55 -0800108#define VDD_FROM_REG(val) (((val) * 95 + 2) / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
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 {
Tony Jones1beeffe2007-08-20 13:46:20 -0700117 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 enum chips type;
119
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100120 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 char valid; /* !=0 if following fields are valid */
122 unsigned long last_updated; /* In jiffies */
123
124 u8 voltage_in[4]; /* Register values; [0] = VDD */
125 u8 voltage_min[4]; /* Register values; [0] = VDD */
126 u8 voltage_max[4]; /* Register values; [0] = VDD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 u8 fan_in[2];
128 u8 fan_min[2];
129 u8 fan_div[2]; /* Register encoding, shifted right */
130 u8 fan_auto1; /* Boolean */
131 u8 temp_in; /* Register values */
132 u8 temp_max; /* Register values */
133 u8 temp_hyst; /* Register values */
134 u8 alarms; /* Register value */
Jean Delvarea5955ed2007-10-22 17:45:08 +0200135 u8 alarm_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 u8 beep_mask; /* Register value */
137 u8 beep_enable; /* Boolean */
138};
139
Axel Lind4a972a2014-07-03 09:46:51 +0800140/*
141 * Registers 0x07 to 0x0c are word-sized, others are byte-sized
142 * GL518 uses a high-byte first convention, which is exactly opposite to
143 * the SMBus standard.
144 */
145static int gl518_read_value(struct i2c_client *client, u8 reg)
146{
147 if ((reg >= 0x07) && (reg <= 0x0c))
148 return i2c_smbus_read_word_swapped(client, reg);
149 else
150 return i2c_smbus_read_byte_data(client, reg);
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Axel Lind4a972a2014-07-03 09:46:51 +0800153static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
154{
155 if ((reg >= 0x07) && (reg <= 0x0c))
156 return i2c_smbus_write_word_swapped(client, reg, value);
157 else
158 return i2c_smbus_write_byte_data(client, reg, value);
159}
Jean Delvare95d80e72008-07-16 19:30:13 +0200160
Axel Lind4a972a2014-07-03 09:46:51 +0800161static struct gl518_data *gl518_update_device(struct device *dev)
162{
163 struct i2c_client *client = to_i2c_client(dev);
164 struct gl518_data *data = i2c_get_clientdata(client);
165 int val;
166
167 mutex_lock(&data->update_lock);
168
169 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
170 || !data->valid) {
171 dev_dbg(&client->dev, "Starting gl518 update\n");
172
173 data->alarms = gl518_read_value(client, GL518_REG_INT);
174 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
175
176 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
177 data->voltage_min[0] = val & 0xff;
178 data->voltage_max[0] = (val >> 8) & 0xff;
179 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
180 data->voltage_min[1] = val & 0xff;
181 data->voltage_max[1] = (val >> 8) & 0xff;
182 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
183 data->voltage_min[2] = val & 0xff;
184 data->voltage_max[2] = (val >> 8) & 0xff;
185 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
186 data->voltage_min[3] = val & 0xff;
187 data->voltage_max[3] = (val >> 8) & 0xff;
188
189 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
190 data->fan_in[0] = (val >> 8) & 0xff;
191 data->fan_in[1] = val & 0xff;
192
193 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
194 data->fan_min[0] = (val >> 8) & 0xff;
195 data->fan_min[1] = val & 0xff;
196
197 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
198 data->temp_max =
199 gl518_read_value(client, GL518_REG_TEMP_MAX);
200 data->temp_hyst =
201 gl518_read_value(client, GL518_REG_TEMP_HYST);
202
203 val = gl518_read_value(client, GL518_REG_MISC);
204 data->fan_div[0] = (val >> 6) & 0x03;
205 data->fan_div[1] = (val >> 4) & 0x03;
206 data->fan_auto1 = (val >> 3) & 0x01;
207
208 data->alarms &= data->alarm_mask;
209
210 val = gl518_read_value(client, GL518_REG_CONF);
211 data->beep_enable = (val >> 2) & 1;
212
213 if (data->type != gl518sm_r00) {
214 data->voltage_in[0] =
215 gl518_read_value(client, GL518_REG_VDD);
216 data->voltage_in[1] =
217 gl518_read_value(client, GL518_REG_VIN1);
218 data->voltage_in[2] =
219 gl518_read_value(client, GL518_REG_VIN2);
220 }
221 data->voltage_in[3] =
222 gl518_read_value(client, GL518_REG_VIN3);
223
224 data->last_updated = jiffies;
225 data->valid = 1;
226 }
227
228 mutex_unlock(&data->update_lock);
229
230 return data;
231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
234 * Sysfs stuff
235 */
236
237#define show(type, suffix, value) \
Guenter Roeck228f8e02012-01-14 13:32:55 -0800238static ssize_t show_##suffix(struct device *dev, \
239 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{ \
241 struct gl518_data *data = gl518_update_device(dev); \
242 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245show(TEMP, temp_input1, temp_in);
246show(TEMP, temp_max1, temp_max);
247show(TEMP, temp_hyst1, temp_hyst);
248show(BOOL, fan_auto1, fan_auto1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249show(VDD, in_input0, voltage_in[0]);
250show(IN, in_input1, voltage_in[1]);
251show(IN, in_input2, voltage_in[2]);
252show(IN, in_input3, voltage_in[3]);
253show(VDD, in_min0, voltage_min[0]);
254show(IN, in_min1, voltage_min[1]);
255show(IN, in_min2, voltage_min[2]);
256show(IN, in_min3, voltage_min[3]);
257show(VDD, in_max0, voltage_max[0]);
258show(IN, in_max1, voltage_max[1]);
259show(IN, in_max2, voltage_max[2]);
260show(IN, in_max3, voltage_max[3]);
261show(RAW, alarms, alarms);
262show(BOOL, beep_enable, beep_enable);
263show(BEEP_MASK, beep_mask, beep_mask);
264
Jean Delvare28292e72007-10-22 17:46:42 +0200265static ssize_t show_fan_input(struct device *dev,
266 struct device_attribute *attr, char *buf)
267{
268 int nr = to_sensor_dev_attr(attr)->index;
269 struct gl518_data *data = gl518_update_device(dev);
270 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
271 DIV_FROM_REG(data->fan_div[nr])));
272}
273
274static ssize_t show_fan_min(struct device *dev,
275 struct device_attribute *attr, char *buf)
276{
277 int nr = to_sensor_dev_attr(attr)->index;
278 struct gl518_data *data = gl518_update_device(dev);
279 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
280 DIV_FROM_REG(data->fan_div[nr])));
281}
282
283static ssize_t show_fan_div(struct device *dev,
284 struct device_attribute *attr, char *buf)
285{
286 int nr = to_sensor_dev_attr(attr)->index;
287 struct gl518_data *data = gl518_update_device(dev);
288 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#define set(type, suffix, value, reg) \
Guenter Roeck228f8e02012-01-14 13:32:55 -0800292static ssize_t set_##suffix(struct device *dev, \
293 struct device_attribute *attr, \
294 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{ \
296 struct i2c_client *client = to_i2c_client(dev); \
297 struct gl518_data *data = i2c_get_clientdata(client); \
Guenter Roeck228f8e02012-01-14 13:32:55 -0800298 long val; \
299 int err = kstrtol(buf, 10, &val); \
300 if (err) \
301 return err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100303 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 data->value = type##_TO_REG(val); \
305 gl518_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100306 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return count; \
308}
309
310#define set_bits(type, suffix, value, reg, mask, shift) \
Guenter Roeck228f8e02012-01-14 13:32:55 -0800311static ssize_t set_##suffix(struct device *dev, \
312 struct device_attribute *attr, \
313 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{ \
315 struct i2c_client *client = to_i2c_client(dev); \
316 struct gl518_data *data = i2c_get_clientdata(client); \
317 int regvalue; \
Guenter Roeck228f8e02012-01-14 13:32:55 -0800318 unsigned long val; \
319 int err = kstrtoul(buf, 10, &val); \
320 if (err) \
321 return err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100323 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 regvalue = gl518_read_value(client, reg); \
325 data->value = type##_TO_REG(val); \
326 regvalue = (regvalue & ~mask) | (data->value << shift); \
327 gl518_write_value(client, reg, regvalue); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100328 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return count; \
330}
331
332#define set_low(type, suffix, value, reg) \
333 set_bits(type, suffix, value, reg, 0x00ff, 0)
334#define set_high(type, suffix, value, reg) \
335 set_bits(type, suffix, value, reg, 0xff00, 8)
336
337set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
338set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
339set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
341set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
342set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
343set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
344set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
345set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
346set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
347set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
348set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
349set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
350
Jean Delvare28292e72007-10-22 17:46:42 +0200351static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
352 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct i2c_client *client = to_i2c_client(dev);
355 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200356 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int regvalue;
Guenter Roeck228f8e02012-01-14 13:32:55 -0800358 unsigned long val;
359 int err;
360
361 err = kstrtoul(buf, 10, &val);
362 if (err)
363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100365 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
Jean Delvare28292e72007-10-22 17:46:42 +0200367 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
368 regvalue = (regvalue & (0xff << (8 * nr)))
369 | (data->fan_min[nr] << (8 * (1 - nr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
371
372 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
Jean Delvare28292e72007-10-22 17:46:42 +0200373 if (data->fan_min[nr] == 0)
374 data->alarm_mask &= ~(0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 else
Jean Delvare28292e72007-10-22 17:46:42 +0200376 data->alarm_mask |= (0x20 << nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 data->beep_mask &= data->alarm_mask;
378 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
379
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100380 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return count;
382}
383
Jean Delvare28292e72007-10-22 17:46:42 +0200384static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
385 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct gl518_data *data = i2c_get_clientdata(client);
Jean Delvare28292e72007-10-22 17:46:42 +0200389 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int regvalue;
Guenter Roeck228f8e02012-01-14 13:32:55 -0800391 unsigned long val;
392 int err;
393
394 err = kstrtoul(buf, 10, &val);
395 if (err)
396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Jean Delvare21df67b2007-10-22 17:47:58 +0200398 switch (val) {
Guenter Roeck228f8e02012-01-14 13:32:55 -0800399 case 1:
400 val = 0;
401 break;
402 case 2:
403 val = 1;
404 break;
405 case 4:
406 val = 2;
407 break;
408 case 8:
409 val = 3;
410 break;
Jean Delvare21df67b2007-10-22 17:47:58 +0200411 default:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800412 dev_err(dev,
413 "Invalid fan clock divider %lu, choose one of 1, 2, 4 or 8\n",
414 val);
Jean Delvare21df67b2007-10-22 17:47:58 +0200415 return -EINVAL;
416 }
417
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100418 mutex_lock(&data->update_lock);
Jean Delvare28292e72007-10-22 17:46:42 +0200419 regvalue = gl518_read_value(client, GL518_REG_MISC);
Jean Delvare21df67b2007-10-22 17:47:58 +0200420 data->fan_div[nr] = val;
Jean Delvare28292e72007-10-22 17:46:42 +0200421 regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
422 | (data->fan_div[nr] << (6 - 2 * nr));
423 gl518_write_value(client, GL518_REG_MISC, regvalue);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100424 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return count;
426}
427
428static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
429static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
430static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
431 show_temp_hyst1, set_temp_hyst1);
432static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
Jean Delvare28292e72007-10-22 17:46:42 +0200433static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
434static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
435static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
436 show_fan_min, set_fan_min, 0);
437static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
438 show_fan_min, set_fan_min, 1);
439static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
440 show_fan_div, set_fan_div, 0);
441static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
442 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
444static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
445static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
446static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
447static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
448static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
449static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
450static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
451static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
452static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
453static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
454static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
455static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
456static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
457 show_beep_enable, set_beep_enable);
458static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
459 show_beep_mask, set_beep_mask);
460
Jean Delvareda6848d2007-10-22 17:47:16 +0200461static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
462 char *buf)
463{
464 int bitnr = to_sensor_dev_attr(attr)->index;
465 struct gl518_data *data = gl518_update_device(dev);
466 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
467}
468
469static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
470static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
471static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
472static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
473static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
474static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
475static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
476
477static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
478 char *buf)
479{
480 int bitnr = to_sensor_dev_attr(attr)->index;
481 struct gl518_data *data = gl518_update_device(dev);
482 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
483}
484
485static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
486 const char *buf, size_t count)
487{
488 struct i2c_client *client = to_i2c_client(dev);
489 struct gl518_data *data = i2c_get_clientdata(client);
490 int bitnr = to_sensor_dev_attr(attr)->index;
491 unsigned long bit;
Guenter Roeck228f8e02012-01-14 13:32:55 -0800492 int err;
Jean Delvareda6848d2007-10-22 17:47:16 +0200493
Guenter Roeck228f8e02012-01-14 13:32:55 -0800494 err = kstrtoul(buf, 10, &bit);
495 if (err)
496 return err;
497
Jean Delvareda6848d2007-10-22 17:47:16 +0200498 if (bit & ~1)
499 return -EINVAL;
500
501 mutex_lock(&data->update_lock);
502 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
503 if (bit)
504 data->beep_mask |= (1 << bitnr);
505 else
506 data->beep_mask &= ~(1 << bitnr);
507 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
508 mutex_unlock(&data->update_lock);
509 return count;
510}
511
512static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
513static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
514static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
515static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
516static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
517static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
518static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
519
Jean Delvare87808be2006-09-24 21:17:13 +0200520static struct attribute *gl518_attributes[] = {
Jean Delvare87808be2006-09-24 21:17:13 +0200521 &dev_attr_in3_input.attr,
522 &dev_attr_in0_min.attr,
523 &dev_attr_in1_min.attr,
524 &dev_attr_in2_min.attr,
525 &dev_attr_in3_min.attr,
526 &dev_attr_in0_max.attr,
527 &dev_attr_in1_max.attr,
528 &dev_attr_in2_max.attr,
529 &dev_attr_in3_max.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200530 &sensor_dev_attr_in0_alarm.dev_attr.attr,
531 &sensor_dev_attr_in1_alarm.dev_attr.attr,
532 &sensor_dev_attr_in2_alarm.dev_attr.attr,
533 &sensor_dev_attr_in3_alarm.dev_attr.attr,
534 &sensor_dev_attr_in0_beep.dev_attr.attr,
535 &sensor_dev_attr_in1_beep.dev_attr.attr,
536 &sensor_dev_attr_in2_beep.dev_attr.attr,
537 &sensor_dev_attr_in3_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200538
539 &dev_attr_fan1_auto.attr,
Jean Delvare28292e72007-10-22 17:46:42 +0200540 &sensor_dev_attr_fan1_input.dev_attr.attr,
541 &sensor_dev_attr_fan2_input.dev_attr.attr,
542 &sensor_dev_attr_fan1_min.dev_attr.attr,
543 &sensor_dev_attr_fan2_min.dev_attr.attr,
544 &sensor_dev_attr_fan1_div.dev_attr.attr,
545 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200546 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
547 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
548 &sensor_dev_attr_fan1_beep.dev_attr.attr,
549 &sensor_dev_attr_fan2_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200550
551 &dev_attr_temp1_input.attr,
552 &dev_attr_temp1_max.attr,
553 &dev_attr_temp1_max_hyst.attr,
Jean Delvareda6848d2007-10-22 17:47:16 +0200554 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
555 &sensor_dev_attr_temp1_beep.dev_attr.attr,
Jean Delvare87808be2006-09-24 21:17:13 +0200556
557 &dev_attr_alarms.attr,
558 &dev_attr_beep_enable.attr,
559 &dev_attr_beep_mask.attr,
560 NULL
561};
562
563static const struct attribute_group gl518_group = {
564 .attrs = gl518_attributes,
565};
566
Jean Delvare5314f5c2007-10-22 17:46:17 +0200567static struct attribute *gl518_attributes_r80[] = {
568 &dev_attr_in0_input.attr,
569 &dev_attr_in1_input.attr,
570 &dev_attr_in2_input.attr,
571 NULL
572};
573
574static const struct attribute_group gl518_group_r80 = {
575 .attrs = gl518_attributes_r80,
576};
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Real code
580 */
581
Jean Delvare95d80e72008-07-16 19:30:13 +0200582/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100583static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jean Delvare95d80e72008-07-16 19:30:13 +0200585 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100586 int rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
589 I2C_FUNC_SMBUS_WORD_DATA))
Jean Delvare95d80e72008-07-16 19:30:13 +0200590 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* Now, we do the remaining detection. */
Jean Delvare52df6442009-12-09 20:35:57 +0100593 if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
594 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
595 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Determine the chip type. */
Jean Delvare52df6442009-12-09 20:35:57 +0100598 rev = gl518_read_value(client, GL518_REG_REVISION);
599 if (rev != 0x00 && rev != 0x80)
600 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jean Delvare95d80e72008-07-16 19:30:13 +0200602 strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jean Delvare95d80e72008-07-16 19:30:13 +0200604 return 0;
605}
606
Axel Lind4a972a2014-07-03 09:46:51 +0800607/*
608 * Called when we have found a new GL518SM.
609 * Note that we preserve D4:NoFan2 and D2:beep_enable.
610 */
611static void gl518_init_client(struct i2c_client *client)
612{
613 /* Make sure we leave D7:Reset untouched */
614 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
615
616 /* Comparator mode (D3=0), standby mode (D6=0) */
617 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
618
619 /* Never interrupts */
620 gl518_write_value(client, GL518_REG_MASK, 0x00);
621
622 /* Clear status register (D5=1), start (D6=1) */
623 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
624 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
625}
626
Jean Delvare95d80e72008-07-16 19:30:13 +0200627static int gl518_probe(struct i2c_client *client,
628 const struct i2c_device_id *id)
629{
630 struct gl518_data *data;
631 int err, revision;
632
Guenter Roeckbba636d2012-06-02 09:58:06 -0700633 data = devm_kzalloc(&client->dev, sizeof(struct gl518_data),
634 GFP_KERNEL);
635 if (!data)
636 return -ENOMEM;
Jean Delvare95d80e72008-07-16 19:30:13 +0200637
638 i2c_set_clientdata(client, data);
639 revision = gl518_read_value(client, GL518_REG_REVISION);
640 data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
641 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* Initialize the GL518SM chip */
644 data->alarm_mask = 0xff;
Jean Delvarea5955ed2007-10-22 17:45:08 +0200645 gl518_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* Register sysfs hooks */
Guenter Roeck228f8e02012-01-14 13:32:55 -0800648 err = sysfs_create_group(&client->dev.kobj, &gl518_group);
649 if (err)
Guenter Roeckbba636d2012-06-02 09:58:06 -0700650 return err;
Guenter Roeck228f8e02012-01-14 13:32:55 -0800651 if (data->type == gl518sm_r80) {
652 err = sysfs_create_group(&client->dev.kobj, &gl518_group_r80);
653 if (err)
Jean Delvare5314f5c2007-10-22 17:46:17 +0200654 goto exit_remove_files;
Guenter Roeck228f8e02012-01-14 13:32:55 -0800655 }
Jean Delvare87808be2006-09-24 21:17:13 +0200656
Jean Delvarea5955ed2007-10-22 17:45:08 +0200657 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700658 if (IS_ERR(data->hwmon_dev)) {
659 err = PTR_ERR(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200660 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664
Jean Delvare87808be2006-09-24 21:17:13 +0200665exit_remove_files:
Jean Delvarea5955ed2007-10-22 17:45:08 +0200666 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200667 if (data->type == gl518sm_r80)
668 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return err;
670}
671
Jean Delvare95d80e72008-07-16 19:30:13 +0200672static int gl518_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400674 struct gl518_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Tony Jones1beeffe2007-08-20 13:46:20 -0700676 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare87808be2006-09-24 21:17:13 +0200677 sysfs_remove_group(&client->dev.kobj, &gl518_group);
Jean Delvare5314f5c2007-10-22 17:46:17 +0200678 if (data->type == gl518sm_r80)
679 sysfs_remove_group(&client->dev.kobj, &gl518_group_r80);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682}
683
Axel Lind4a972a2014-07-03 09:46:51 +0800684static const struct i2c_device_id gl518_id[] = {
685 { "gl518sm", 0 },
686 { }
687};
688MODULE_DEVICE_TABLE(i2c, gl518_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Axel Lind4a972a2014-07-03 09:46:51 +0800690static struct i2c_driver gl518_driver = {
691 .class = I2C_CLASS_HWMON,
692 .driver = {
693 .name = "gl518sm",
694 },
695 .probe = gl518_probe,
696 .remove = gl518_remove,
697 .id_table = gl518_id,
698 .detect = gl518_detect,
699 .address_list = normal_i2c,
700};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Axel Linf0967ee2012-01-20 15:38:18 +0800702module_i2c_driver(gl518_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
705 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
706 "Hong-Gunn Chew <hglinux@gunnet.org>");
707MODULE_DESCRIPTION("GL518SM driver");
708MODULE_LICENSE("GPL");