Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
  4 *             monitoring
  5 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
  6 * Kyosti Malkki <kmalkki@cc.hut.fi>
  7 * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and
  8 * Jean Delvare <jdelvare@suse.de>
  9 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 10 * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
 11 * and advice of Greg Kroah-Hartman.
 12 *
 13 * Notes about the port:
 14 * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
 15 * in1 nor in2. The original driver had an ugly workaround to get them
 16 * anyway (changing limits and watching alarms trigger and wear off).
 17 * We did not keep that part of the original driver in the Linux 2.6
 18 * version, since it was making the driver significantly more complex
 19 * with no real benefit.
 20 */
 21
 22#include <linux/module.h>
 23#include <linux/init.h>
 24#include <linux/slab.h>
 25#include <linux/jiffies.h>
 26#include <linux/i2c.h>
 27#include <linux/hwmon.h>
 28#include <linux/hwmon-sysfs.h>
 29#include <linux/err.h>
 30#include <linux/mutex.h>
 31#include <linux/sysfs.h>
 32
 33/* Addresses to scan */
 34static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
 35
 36enum chips { gl518sm_r00, gl518sm_r80 };
 37
 38/* Many GL518 constants specified below */
 39
 40/* The GL518 registers */
 41#define GL518_REG_CHIP_ID	0x00
 42#define GL518_REG_REVISION	0x01
 43#define GL518_REG_VENDOR_ID	0x02
 44#define GL518_REG_CONF		0x03
 45#define GL518_REG_TEMP_IN	0x04
 46#define GL518_REG_TEMP_MAX	0x05
 47#define GL518_REG_TEMP_HYST	0x06
 48#define GL518_REG_FAN_COUNT	0x07
 49#define GL518_REG_FAN_LIMIT	0x08
 50#define GL518_REG_VIN1_LIMIT	0x09
 51#define GL518_REG_VIN2_LIMIT	0x0a
 52#define GL518_REG_VIN3_LIMIT	0x0b
 53#define GL518_REG_VDD_LIMIT	0x0c
 54#define GL518_REG_VIN3		0x0d
 55#define GL518_REG_MISC		0x0f
 56#define GL518_REG_ALARM		0x10
 57#define GL518_REG_MASK		0x11
 58#define GL518_REG_INT		0x12
 59#define GL518_REG_VIN2		0x13
 60#define GL518_REG_VIN1		0x14
 61#define GL518_REG_VDD		0x15
 62
 63
 64/*
 65 * Conversions. Rounding and limit checking is only done on the TO_REG
 66 * variants. Note that you should be a bit careful with which arguments
 67 * these macros are called: arguments may be evaluated more than once.
 68 * Fixing this is just not worth it.
 69 */
 70
 71#define RAW_FROM_REG(val)	val
 72
 73#define BOOL_FROM_REG(val)	((val) ? 0 : 1)
 74#define BOOL_TO_REG(val)	((val) ? 0 : 1)
 75
 76#define TEMP_CLAMP(val)		clamp_val(val, -119000, 136000)
 77#define TEMP_TO_REG(val)	(DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 119)
 78#define TEMP_FROM_REG(val)	(((val) - 119) * 1000)
 79
 80static inline u8 FAN_TO_REG(long rpm, int div)
 81{
 82	long rpmdiv;
 83	if (rpm == 0)
 84		return 0;
 85	rpmdiv = clamp_val(rpm, 1, 960000) * div;
 86	return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
 87}
 88#define FAN_FROM_REG(val, div)	((val) == 0 ? 0 : (480000 / ((val) * (div))))
 89
 90#define IN_CLAMP(val)		clamp_val(val, 0, 255 * 19)
 91#define IN_TO_REG(val)		DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
 92#define IN_FROM_REG(val)	((val) * 19)
 93
 94#define VDD_CLAMP(val)		clamp_val(val, 0, 255 * 95 / 4)
 95#define VDD_TO_REG(val)		DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
 96#define VDD_FROM_REG(val)	DIV_ROUND_CLOSEST((val) * 95, 4)
 97
 98#define DIV_FROM_REG(val)	(1 << (val))
 99
100#define BEEP_MASK_TO_REG(val)	((val) & 0x7f & data->alarm_mask)
101#define BEEP_MASK_FROM_REG(val)	((val) & 0x7f)
102
103/* Each client has this additional data */
104struct gl518_data {
105	struct i2c_client *client;
106	const struct attribute_group *groups[3];
107	enum chips type;
108
109	struct mutex update_lock;
110	char valid;		/* !=0 if following fields are valid */
111	unsigned long last_updated;	/* In jiffies */
112
113	u8 voltage_in[4];	/* Register values; [0] = VDD */
114	u8 voltage_min[4];	/* Register values; [0] = VDD */
115	u8 voltage_max[4];	/* Register values; [0] = VDD */
116	u8 fan_in[2];
117	u8 fan_min[2];
118	u8 fan_div[2];		/* Register encoding, shifted right */
119	u8 fan_auto1;		/* Boolean */
120	u8 temp_in;		/* Register values */
121	u8 temp_max;		/* Register values */
122	u8 temp_hyst;		/* Register values */
123	u8 alarms;		/* Register value */
124	u8 alarm_mask;
125	u8 beep_mask;		/* Register value */
126	u8 beep_enable;		/* Boolean */
127};
128
129/*
130 * Registers 0x07 to 0x0c are word-sized, others are byte-sized
131 * GL518 uses a high-byte first convention, which is exactly opposite to
132 * the SMBus standard.
133 */
134static int gl518_read_value(struct i2c_client *client, u8 reg)
135{
136	if ((reg >= 0x07) && (reg <= 0x0c))
137		return i2c_smbus_read_word_swapped(client, reg);
138	else
139		return i2c_smbus_read_byte_data(client, reg);
140}
141
142static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
143{
144	if ((reg >= 0x07) && (reg <= 0x0c))
145		return i2c_smbus_write_word_swapped(client, reg, value);
146	else
147		return i2c_smbus_write_byte_data(client, reg, value);
148}
149
150static struct gl518_data *gl518_update_device(struct device *dev)
151{
152	struct gl518_data *data = dev_get_drvdata(dev);
153	struct i2c_client *client = data->client;
154	int val;
155
156	mutex_lock(&data->update_lock);
157
158	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
159	    || !data->valid) {
160		dev_dbg(&client->dev, "Starting gl518 update\n");
161
162		data->alarms = gl518_read_value(client, GL518_REG_INT);
163		data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
164
165		val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
166		data->voltage_min[0] = val & 0xff;
167		data->voltage_max[0] = (val >> 8) & 0xff;
168		val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
169		data->voltage_min[1] = val & 0xff;
170		data->voltage_max[1] = (val >> 8) & 0xff;
171		val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
172		data->voltage_min[2] = val & 0xff;
173		data->voltage_max[2] = (val >> 8) & 0xff;
174		val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
175		data->voltage_min[3] = val & 0xff;
176		data->voltage_max[3] = (val >> 8) & 0xff;
177
178		val = gl518_read_value(client, GL518_REG_FAN_COUNT);
179		data->fan_in[0] = (val >> 8) & 0xff;
180		data->fan_in[1] = val & 0xff;
181
182		val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
183		data->fan_min[0] = (val >> 8) & 0xff;
184		data->fan_min[1] = val & 0xff;
185
186		data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
187		data->temp_max =
188		    gl518_read_value(client, GL518_REG_TEMP_MAX);
189		data->temp_hyst =
190		    gl518_read_value(client, GL518_REG_TEMP_HYST);
191
192		val = gl518_read_value(client, GL518_REG_MISC);
193		data->fan_div[0] = (val >> 6) & 0x03;
194		data->fan_div[1] = (val >> 4) & 0x03;
195		data->fan_auto1  = (val >> 3) & 0x01;
196
197		data->alarms &= data->alarm_mask;
198
199		val = gl518_read_value(client, GL518_REG_CONF);
200		data->beep_enable = (val >> 2) & 1;
201
202		if (data->type != gl518sm_r00) {
203			data->voltage_in[0] =
204			    gl518_read_value(client, GL518_REG_VDD);
205			data->voltage_in[1] =
206			    gl518_read_value(client, GL518_REG_VIN1);
207			data->voltage_in[2] =
208			    gl518_read_value(client, GL518_REG_VIN2);
209		}
210		data->voltage_in[3] =
211		    gl518_read_value(client, GL518_REG_VIN3);
212
213		data->last_updated = jiffies;
214		data->valid = 1;
215	}
216
217	mutex_unlock(&data->update_lock);
218
219	return data;
220}
221
222/*
223 * Sysfs stuff
224 */
225
226#define show(type, suffix, value)					\
227static ssize_t show_##suffix(struct device *dev,			\
228			     struct device_attribute *attr, char *buf)	\
229{									\
230	struct gl518_data *data = gl518_update_device(dev);		\
231	return sprintf(buf, "%d\n", type##_FROM_REG(data->value));	\
232}
233
234show(TEMP, temp_input1, temp_in);
235show(TEMP, temp_max1, temp_max);
236show(TEMP, temp_hyst1, temp_hyst);
237show(BOOL, fan_auto1, fan_auto1);
238show(VDD, in_input0, voltage_in[0]);
239show(IN, in_input1, voltage_in[1]);
240show(IN, in_input2, voltage_in[2]);
241show(IN, in_input3, voltage_in[3]);
242show(VDD, in_min0, voltage_min[0]);
243show(IN, in_min1, voltage_min[1]);
244show(IN, in_min2, voltage_min[2]);
245show(IN, in_min3, voltage_min[3]);
246show(VDD, in_max0, voltage_max[0]);
247show(IN, in_max1, voltage_max[1]);
248show(IN, in_max2, voltage_max[2]);
249show(IN, in_max3, voltage_max[3]);
250show(RAW, alarms, alarms);
251show(BOOL, beep_enable, beep_enable);
252show(BEEP_MASK, beep_mask, beep_mask);
253
254static ssize_t fan_input_show(struct device *dev,
255			      struct device_attribute *attr, char *buf)
256{
257	int nr = to_sensor_dev_attr(attr)->index;
258	struct gl518_data *data = gl518_update_device(dev);
259	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
260					DIV_FROM_REG(data->fan_div[nr])));
261}
262
263static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
264			    char *buf)
265{
266	int nr = to_sensor_dev_attr(attr)->index;
267	struct gl518_data *data = gl518_update_device(dev);
268	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
269					DIV_FROM_REG(data->fan_div[nr])));
270}
271
272static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
273			    char *buf)
274{
275	int nr = to_sensor_dev_attr(attr)->index;
276	struct gl518_data *data = gl518_update_device(dev);
277	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
278}
279
280#define set(type, suffix, value, reg)					\
281static ssize_t set_##suffix(struct device *dev,				\
282			    struct device_attribute *attr,		\
283			    const char *buf, size_t count)		\
284{									\
285	struct gl518_data *data = dev_get_drvdata(dev);			\
286	struct i2c_client *client = data->client;			\
287	long val;							\
288	int err = kstrtol(buf, 10, &val);				\
289	if (err)							\
290		return err;						\
291									\
292	mutex_lock(&data->update_lock);					\
293	data->value = type##_TO_REG(val);				\
294	gl518_write_value(client, reg, data->value);			\
295	mutex_unlock(&data->update_lock);				\
296	return count;							\
297}
298
299#define set_bits(type, suffix, value, reg, mask, shift)			\
300static ssize_t set_##suffix(struct device *dev,				\
301			    struct device_attribute *attr,		\
302			    const char *buf, size_t count)		\
303{									\
304	struct gl518_data *data = dev_get_drvdata(dev);			\
305	struct i2c_client *client = data->client;			\
306	int regvalue;							\
307	unsigned long val;						\
308	int err = kstrtoul(buf, 10, &val);				\
309	if (err)							\
310		return err;						\
311									\
312	mutex_lock(&data->update_lock);					\
313	regvalue = gl518_read_value(client, reg);			\
314	data->value = type##_TO_REG(val);				\
315	regvalue = (regvalue & ~mask) | (data->value << shift);		\
316	gl518_write_value(client, reg, regvalue);			\
317	mutex_unlock(&data->update_lock);				\
318	return count;							\
319}
320
321#define set_low(type, suffix, value, reg)				\
322	set_bits(type, suffix, value, reg, 0x00ff, 0)
323#define set_high(type, suffix, value, reg)				\
324	set_bits(type, suffix, value, reg, 0xff00, 8)
325
326set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
327set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
328set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
329set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
330set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
331set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
332set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
333set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
334set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
335set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
336set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
337set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
338set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
339
340static ssize_t fan_min_store(struct device *dev,
341			     struct device_attribute *attr, const char *buf,
342			     size_t count)
343{
344	struct gl518_data *data = dev_get_drvdata(dev);
345	struct i2c_client *client = data->client;
346	int nr = to_sensor_dev_attr(attr)->index;
347	int regvalue;
348	unsigned long val;
349	int err;
350
351	err = kstrtoul(buf, 10, &val);
352	if (err)
353		return err;
354
355	mutex_lock(&data->update_lock);
356	regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
357	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
358	regvalue = (regvalue & (0xff << (8 * nr)))
359		 | (data->fan_min[nr] << (8 * (1 - nr)));
360	gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
361
362	data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
363	if (data->fan_min[nr] == 0)
364		data->alarm_mask &= ~(0x20 << nr);
365	else
366		data->alarm_mask |= (0x20 << nr);
367	data->beep_mask &= data->alarm_mask;
368	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
369
370	mutex_unlock(&data->update_lock);
371	return count;
372}
373
374static ssize_t fan_div_store(struct device *dev,
375			     struct device_attribute *attr, const char *buf,
376			     size_t count)
377{
378	struct gl518_data *data = dev_get_drvdata(dev);
379	struct i2c_client *client = data->client;
380	int nr = to_sensor_dev_attr(attr)->index;
381	int regvalue;
382	unsigned long val;
383	int err;
384
385	err = kstrtoul(buf, 10, &val);
386	if (err)
387		return err;
388
389	switch (val) {
390	case 1:
391		val = 0;
392		break;
393	case 2:
394		val = 1;
395		break;
396	case 4:
397		val = 2;
398		break;
399	case 8:
400		val = 3;
401		break;
402	default:
403		dev_err(dev,
404			"Invalid fan clock divider %lu, choose one of 1, 2, 4 or 8\n",
405			val);
406		return -EINVAL;
407	}
408
409	mutex_lock(&data->update_lock);
410	regvalue = gl518_read_value(client, GL518_REG_MISC);
411	data->fan_div[nr] = val;
412	regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
413		 | (data->fan_div[nr] << (6 - 2 * nr));
414	gl518_write_value(client, GL518_REG_MISC, regvalue);
415	mutex_unlock(&data->update_lock);
416	return count;
417}
418
419static DEVICE_ATTR(temp1_input, 0444, show_temp_input1, NULL);
420static DEVICE_ATTR(temp1_max, 0644, show_temp_max1, set_temp_max1);
421static DEVICE_ATTR(temp1_max_hyst, 0644,
422		   show_temp_hyst1, set_temp_hyst1);
423static DEVICE_ATTR(fan1_auto, 0644, show_fan_auto1, set_fan_auto1);
424static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
425static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
426static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
427static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
428static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
429static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
430static DEVICE_ATTR(in0_input, 0444, show_in_input0, NULL);
431static DEVICE_ATTR(in1_input, 0444, show_in_input1, NULL);
432static DEVICE_ATTR(in2_input, 0444, show_in_input2, NULL);
433static DEVICE_ATTR(in3_input, 0444, show_in_input3, NULL);
434static DEVICE_ATTR(in0_min, 0644, show_in_min0, set_in_min0);
435static DEVICE_ATTR(in1_min, 0644, show_in_min1, set_in_min1);
436static DEVICE_ATTR(in2_min, 0644, show_in_min2, set_in_min2);
437static DEVICE_ATTR(in3_min, 0644, show_in_min3, set_in_min3);
438static DEVICE_ATTR(in0_max, 0644, show_in_max0, set_in_max0);
439static DEVICE_ATTR(in1_max, 0644, show_in_max1, set_in_max1);
440static DEVICE_ATTR(in2_max, 0644, show_in_max2, set_in_max2);
441static DEVICE_ATTR(in3_max, 0644, show_in_max3, set_in_max3);
442static DEVICE_ATTR(alarms, 0444, show_alarms, NULL);
443static DEVICE_ATTR(beep_enable, 0644,
444		   show_beep_enable, set_beep_enable);
445static DEVICE_ATTR(beep_mask, 0644,
446		   show_beep_mask, set_beep_mask);
 
 
 
 
447
448static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
449			  char *buf)
450{
451	int bitnr = to_sensor_dev_attr(attr)->index;
452	struct gl518_data *data = gl518_update_device(dev);
453	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
454}
455
456static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
457static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
458static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
459static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
460static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
461static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 5);
462static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 6);
463
464static ssize_t beep_show(struct device *dev, struct device_attribute *attr,
465			 char *buf)
466{
467	int bitnr = to_sensor_dev_attr(attr)->index;
468	struct gl518_data *data = gl518_update_device(dev);
469	return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
470}
471
472static ssize_t beep_store(struct device *dev, struct device_attribute *attr,
473			  const char *buf, size_t count)
474{
475	struct gl518_data *data = dev_get_drvdata(dev);
476	struct i2c_client *client = data->client;
477	int bitnr = to_sensor_dev_attr(attr)->index;
478	unsigned long bit;
479	int err;
480
481	err = kstrtoul(buf, 10, &bit);
482	if (err)
483		return err;
484
485	if (bit & ~1)
486		return -EINVAL;
487
488	mutex_lock(&data->update_lock);
489	data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
490	if (bit)
491		data->beep_mask |= (1 << bitnr);
492	else
493		data->beep_mask &= ~(1 << bitnr);
494	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
495	mutex_unlock(&data->update_lock);
496	return count;
497}
498
499static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
500static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
501static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
502static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
503static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
504static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 5);
505static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 6);
506
507static struct attribute *gl518_attributes[] = {
508	&dev_attr_in3_input.attr,
509	&dev_attr_in0_min.attr,
510	&dev_attr_in1_min.attr,
511	&dev_attr_in2_min.attr,
512	&dev_attr_in3_min.attr,
513	&dev_attr_in0_max.attr,
514	&dev_attr_in1_max.attr,
515	&dev_attr_in2_max.attr,
516	&dev_attr_in3_max.attr,
517	&sensor_dev_attr_in0_alarm.dev_attr.attr,
518	&sensor_dev_attr_in1_alarm.dev_attr.attr,
519	&sensor_dev_attr_in2_alarm.dev_attr.attr,
520	&sensor_dev_attr_in3_alarm.dev_attr.attr,
521	&sensor_dev_attr_in0_beep.dev_attr.attr,
522	&sensor_dev_attr_in1_beep.dev_attr.attr,
523	&sensor_dev_attr_in2_beep.dev_attr.attr,
524	&sensor_dev_attr_in3_beep.dev_attr.attr,
525
526	&dev_attr_fan1_auto.attr,
527	&sensor_dev_attr_fan1_input.dev_attr.attr,
528	&sensor_dev_attr_fan2_input.dev_attr.attr,
529	&sensor_dev_attr_fan1_min.dev_attr.attr,
530	&sensor_dev_attr_fan2_min.dev_attr.attr,
531	&sensor_dev_attr_fan1_div.dev_attr.attr,
532	&sensor_dev_attr_fan2_div.dev_attr.attr,
533	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
534	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
535	&sensor_dev_attr_fan1_beep.dev_attr.attr,
536	&sensor_dev_attr_fan2_beep.dev_attr.attr,
537
538	&dev_attr_temp1_input.attr,
539	&dev_attr_temp1_max.attr,
540	&dev_attr_temp1_max_hyst.attr,
541	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
542	&sensor_dev_attr_temp1_beep.dev_attr.attr,
543
544	&dev_attr_alarms.attr,
545	&dev_attr_beep_enable.attr,
546	&dev_attr_beep_mask.attr,
547	NULL
548};
549
550static const struct attribute_group gl518_group = {
551	.attrs = gl518_attributes,
552};
553
554static struct attribute *gl518_attributes_r80[] = {
555	&dev_attr_in0_input.attr,
556	&dev_attr_in1_input.attr,
557	&dev_attr_in2_input.attr,
558	NULL
559};
560
561static const struct attribute_group gl518_group_r80 = {
562	.attrs = gl518_attributes_r80,
563};
564
565/*
566 * Real code
567 */
568
569/* Return 0 if detection is successful, -ENODEV otherwise */
570static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
571{
572	struct i2c_adapter *adapter = client->adapter;
573	int rev;
574
575	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
576				     I2C_FUNC_SMBUS_WORD_DATA))
577		return -ENODEV;
578
579	/* Now, we do the remaining detection. */
580	if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
581	 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
582		return -ENODEV;
583
584	/* Determine the chip type. */
585	rev = gl518_read_value(client, GL518_REG_REVISION);
586	if (rev != 0x00 && rev != 0x80)
587		return -ENODEV;
588
589	strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
590
591	return 0;
592}
593
594/*
595 * Called when we have found a new GL518SM.
596 * Note that we preserve D4:NoFan2 and D2:beep_enable.
597 */
598static void gl518_init_client(struct i2c_client *client)
599{
600	/* Make sure we leave D7:Reset untouched */
601	u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
602
603	/* Comparator mode (D3=0), standby mode (D6=0) */
604	gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
605
606	/* Never interrupts */
607	gl518_write_value(client, GL518_REG_MASK, 0x00);
608
609	/* Clear status register (D5=1), start (D6=1) */
610	gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
611	gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
612}
613
614static int gl518_probe(struct i2c_client *client,
615		       const struct i2c_device_id *id)
616{
617	struct device *dev = &client->dev;
618	struct device *hwmon_dev;
619	struct gl518_data *data;
620	int revision;
621
622	data = devm_kzalloc(dev, sizeof(struct gl518_data), GFP_KERNEL);
623	if (!data)
624		return -ENOMEM;
625
626	data->client = client;
627	revision = gl518_read_value(client, GL518_REG_REVISION);
628	data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
629	mutex_init(&data->update_lock);
630
631	/* Initialize the GL518SM chip */
632	data->alarm_mask = 0xff;
633	gl518_init_client(client);
634
635	/* sysfs hooks */
636	data->groups[0] = &gl518_group;
637	if (data->type == gl518sm_r80)
638		data->groups[1] = &gl518_group_r80;
639
640	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
641							   data, data->groups);
642	return PTR_ERR_OR_ZERO(hwmon_dev);
643}
644
645static const struct i2c_device_id gl518_id[] = {
646	{ "gl518sm", 0 },
647	{ }
648};
649MODULE_DEVICE_TABLE(i2c, gl518_id);
650
651static struct i2c_driver gl518_driver = {
652	.class		= I2C_CLASS_HWMON,
653	.driver = {
654		.name	= "gl518sm",
655	},
656	.probe		= gl518_probe,
657	.id_table	= gl518_id,
658	.detect		= gl518_detect,
659	.address_list	= normal_i2c,
660};
661
662module_i2c_driver(gl518_driver);
663
664MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
665	"Kyosti Malkki <kmalkki@cc.hut.fi> and "
666	"Hong-Gunn Chew <hglinux@gunnet.org>");
667MODULE_DESCRIPTION("GL518SM driver");
668MODULE_LICENSE("GPL");
v4.17
 
  1/*
  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 <jdelvare@suse.de>
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published by
 11 * the Free Software Foundation; either version 2 of the License, or
 12 * (at your option) any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 22 *
 23 * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
 24 * and advice of Greg Kroah-Hartman.
 25 *
 26 * Notes about the port:
 27 * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
 28 * in1 nor in2. The original driver had an ugly workaround to get them
 29 * anyway (changing limits and watching alarms trigger and wear off).
 30 * We did not keep that part of the original driver in the Linux 2.6
 31 * version, since it was making the driver significantly more complex
 32 * with no real benefit.
 33 */
 34
 35#include <linux/module.h>
 36#include <linux/init.h>
 37#include <linux/slab.h>
 38#include <linux/jiffies.h>
 39#include <linux/i2c.h>
 40#include <linux/hwmon.h>
 41#include <linux/hwmon-sysfs.h>
 42#include <linux/err.h>
 43#include <linux/mutex.h>
 44#include <linux/sysfs.h>
 45
 46/* Addresses to scan */
 47static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
 48
 49enum chips { gl518sm_r00, gl518sm_r80 };
 50
 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_CLAMP(val)		clamp_val(val, -119000, 136000)
 90#define TEMP_TO_REG(val)	(DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 119)
 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;
 98	rpmdiv = clamp_val(rpm, 1, 960000) * div;
 99	return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
100}
101#define FAN_FROM_REG(val, div)	((val) == 0 ? 0 : (480000 / ((val) * (div))))
102
103#define IN_CLAMP(val)		clamp_val(val, 0, 255 * 19)
104#define IN_TO_REG(val)		DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
105#define IN_FROM_REG(val)	((val) * 19)
106
107#define VDD_CLAMP(val)		clamp_val(val, 0, 255 * 95 / 4)
108#define VDD_TO_REG(val)		DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
109#define VDD_FROM_REG(val)	DIV_ROUND_CLOSEST((val) * 95, 4)
110
111#define DIV_FROM_REG(val)	(1 << (val))
112
113#define BEEP_MASK_TO_REG(val)	((val) & 0x7f & data->alarm_mask)
114#define BEEP_MASK_FROM_REG(val)	((val) & 0x7f)
115
116/* Each client has this additional data */
117struct gl518_data {
118	struct i2c_client *client;
119	const struct attribute_group *groups[3];
120	enum chips type;
121
122	struct mutex update_lock;
123	char valid;		/* !=0 if following fields are valid */
124	unsigned long last_updated;	/* In jiffies */
125
126	u8 voltage_in[4];	/* Register values; [0] = VDD */
127	u8 voltage_min[4];	/* Register values; [0] = VDD */
128	u8 voltage_max[4];	/* Register values; [0] = VDD */
129	u8 fan_in[2];
130	u8 fan_min[2];
131	u8 fan_div[2];		/* Register encoding, shifted right */
132	u8 fan_auto1;		/* Boolean */
133	u8 temp_in;		/* Register values */
134	u8 temp_max;		/* Register values */
135	u8 temp_hyst;		/* Register values */
136	u8 alarms;		/* Register value */
137	u8 alarm_mask;
138	u8 beep_mask;		/* Register value */
139	u8 beep_enable;		/* Boolean */
140};
141
142/*
143 * Registers 0x07 to 0x0c are word-sized, others are byte-sized
144 * GL518 uses a high-byte first convention, which is exactly opposite to
145 * the SMBus standard.
146 */
147static int gl518_read_value(struct i2c_client *client, u8 reg)
148{
149	if ((reg >= 0x07) && (reg <= 0x0c))
150		return i2c_smbus_read_word_swapped(client, reg);
151	else
152		return i2c_smbus_read_byte_data(client, reg);
153}
154
155static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
156{
157	if ((reg >= 0x07) && (reg <= 0x0c))
158		return i2c_smbus_write_word_swapped(client, reg, value);
159	else
160		return i2c_smbus_write_byte_data(client, reg, value);
161}
162
163static struct gl518_data *gl518_update_device(struct device *dev)
164{
165	struct gl518_data *data = dev_get_drvdata(dev);
166	struct i2c_client *client = data->client;
167	int val;
168
169	mutex_lock(&data->update_lock);
170
171	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
172	    || !data->valid) {
173		dev_dbg(&client->dev, "Starting gl518 update\n");
174
175		data->alarms = gl518_read_value(client, GL518_REG_INT);
176		data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
177
178		val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
179		data->voltage_min[0] = val & 0xff;
180		data->voltage_max[0] = (val >> 8) & 0xff;
181		val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
182		data->voltage_min[1] = val & 0xff;
183		data->voltage_max[1] = (val >> 8) & 0xff;
184		val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
185		data->voltage_min[2] = val & 0xff;
186		data->voltage_max[2] = (val >> 8) & 0xff;
187		val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
188		data->voltage_min[3] = val & 0xff;
189		data->voltage_max[3] = (val >> 8) & 0xff;
190
191		val = gl518_read_value(client, GL518_REG_FAN_COUNT);
192		data->fan_in[0] = (val >> 8) & 0xff;
193		data->fan_in[1] = val & 0xff;
194
195		val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
196		data->fan_min[0] = (val >> 8) & 0xff;
197		data->fan_min[1] = val & 0xff;
198
199		data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
200		data->temp_max =
201		    gl518_read_value(client, GL518_REG_TEMP_MAX);
202		data->temp_hyst =
203		    gl518_read_value(client, GL518_REG_TEMP_HYST);
204
205		val = gl518_read_value(client, GL518_REG_MISC);
206		data->fan_div[0] = (val >> 6) & 0x03;
207		data->fan_div[1] = (val >> 4) & 0x03;
208		data->fan_auto1  = (val >> 3) & 0x01;
209
210		data->alarms &= data->alarm_mask;
211
212		val = gl518_read_value(client, GL518_REG_CONF);
213		data->beep_enable = (val >> 2) & 1;
214
215		if (data->type != gl518sm_r00) {
216			data->voltage_in[0] =
217			    gl518_read_value(client, GL518_REG_VDD);
218			data->voltage_in[1] =
219			    gl518_read_value(client, GL518_REG_VIN1);
220			data->voltage_in[2] =
221			    gl518_read_value(client, GL518_REG_VIN2);
222		}
223		data->voltage_in[3] =
224		    gl518_read_value(client, GL518_REG_VIN3);
225
226		data->last_updated = jiffies;
227		data->valid = 1;
228	}
229
230	mutex_unlock(&data->update_lock);
231
232	return data;
233}
234
235/*
236 * Sysfs stuff
237 */
238
239#define show(type, suffix, value)					\
240static ssize_t show_##suffix(struct device *dev,			\
241			     struct device_attribute *attr, char *buf)	\
242{									\
243	struct gl518_data *data = gl518_update_device(dev);		\
244	return sprintf(buf, "%d\n", type##_FROM_REG(data->value));	\
245}
246
247show(TEMP, temp_input1, temp_in);
248show(TEMP, temp_max1, temp_max);
249show(TEMP, temp_hyst1, temp_hyst);
250show(BOOL, fan_auto1, fan_auto1);
251show(VDD, in_input0, voltage_in[0]);
252show(IN, in_input1, voltage_in[1]);
253show(IN, in_input2, voltage_in[2]);
254show(IN, in_input3, voltage_in[3]);
255show(VDD, in_min0, voltage_min[0]);
256show(IN, in_min1, voltage_min[1]);
257show(IN, in_min2, voltage_min[2]);
258show(IN, in_min3, voltage_min[3]);
259show(VDD, in_max0, voltage_max[0]);
260show(IN, in_max1, voltage_max[1]);
261show(IN, in_max2, voltage_max[2]);
262show(IN, in_max3, voltage_max[3]);
263show(RAW, alarms, alarms);
264show(BOOL, beep_enable, beep_enable);
265show(BEEP_MASK, beep_mask, beep_mask);
266
267static ssize_t show_fan_input(struct device *dev,
268			      struct device_attribute *attr, char *buf)
269{
270	int nr = to_sensor_dev_attr(attr)->index;
271	struct gl518_data *data = gl518_update_device(dev);
272	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
273					DIV_FROM_REG(data->fan_div[nr])));
274}
275
276static ssize_t show_fan_min(struct device *dev,
277			    struct device_attribute *attr, char *buf)
278{
279	int nr = to_sensor_dev_attr(attr)->index;
280	struct gl518_data *data = gl518_update_device(dev);
281	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
282					DIV_FROM_REG(data->fan_div[nr])));
283}
284
285static ssize_t show_fan_div(struct device *dev,
286			    struct device_attribute *attr, char *buf)
287{
288	int nr = to_sensor_dev_attr(attr)->index;
289	struct gl518_data *data = gl518_update_device(dev);
290	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
291}
292
293#define set(type, suffix, value, reg)					\
294static ssize_t set_##suffix(struct device *dev,				\
295			    struct device_attribute *attr,		\
296			    const char *buf, size_t count)		\
297{									\
298	struct gl518_data *data = dev_get_drvdata(dev);			\
299	struct i2c_client *client = data->client;			\
300	long val;							\
301	int err = kstrtol(buf, 10, &val);				\
302	if (err)							\
303		return err;						\
304									\
305	mutex_lock(&data->update_lock);					\
306	data->value = type##_TO_REG(val);				\
307	gl518_write_value(client, reg, data->value);			\
308	mutex_unlock(&data->update_lock);				\
309	return count;							\
310}
311
312#define set_bits(type, suffix, value, reg, mask, shift)			\
313static ssize_t set_##suffix(struct device *dev,				\
314			    struct device_attribute *attr,		\
315			    const char *buf, size_t count)		\
316{									\
317	struct gl518_data *data = dev_get_drvdata(dev);			\
318	struct i2c_client *client = data->client;			\
319	int regvalue;							\
320	unsigned long val;						\
321	int err = kstrtoul(buf, 10, &val);				\
322	if (err)							\
323		return err;						\
324									\
325	mutex_lock(&data->update_lock);					\
326	regvalue = gl518_read_value(client, reg);			\
327	data->value = type##_TO_REG(val);				\
328	regvalue = (regvalue & ~mask) | (data->value << shift);		\
329	gl518_write_value(client, reg, regvalue);			\
330	mutex_unlock(&data->update_lock);				\
331	return count;							\
332}
333
334#define set_low(type, suffix, value, reg)				\
335	set_bits(type, suffix, value, reg, 0x00ff, 0)
336#define set_high(type, suffix, value, reg)				\
337	set_bits(type, suffix, value, reg, 0xff00, 8)
338
339set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
340set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
341set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
342set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
343set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
344set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
345set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
346set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
347set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
348set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
349set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
350set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
351set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
352
353static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
354			   const char *buf, size_t count)
 
355{
356	struct gl518_data *data = dev_get_drvdata(dev);
357	struct i2c_client *client = data->client;
358	int nr = to_sensor_dev_attr(attr)->index;
359	int regvalue;
360	unsigned long val;
361	int err;
362
363	err = kstrtoul(buf, 10, &val);
364	if (err)
365		return err;
366
367	mutex_lock(&data->update_lock);
368	regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
369	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
370	regvalue = (regvalue & (0xff << (8 * nr)))
371		 | (data->fan_min[nr] << (8 * (1 - nr)));
372	gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
373
374	data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
375	if (data->fan_min[nr] == 0)
376		data->alarm_mask &= ~(0x20 << nr);
377	else
378		data->alarm_mask |= (0x20 << nr);
379	data->beep_mask &= data->alarm_mask;
380	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
381
382	mutex_unlock(&data->update_lock);
383	return count;
384}
385
386static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
387			   const char *buf, size_t count)
 
388{
389	struct gl518_data *data = dev_get_drvdata(dev);
390	struct i2c_client *client = data->client;
391	int nr = to_sensor_dev_attr(attr)->index;
392	int regvalue;
393	unsigned long val;
394	int err;
395
396	err = kstrtoul(buf, 10, &val);
397	if (err)
398		return err;
399
400	switch (val) {
401	case 1:
402		val = 0;
403		break;
404	case 2:
405		val = 1;
406		break;
407	case 4:
408		val = 2;
409		break;
410	case 8:
411		val = 3;
412		break;
413	default:
414		dev_err(dev,
415			"Invalid fan clock divider %lu, choose one of 1, 2, 4 or 8\n",
416			val);
417		return -EINVAL;
418	}
419
420	mutex_lock(&data->update_lock);
421	regvalue = gl518_read_value(client, GL518_REG_MISC);
422	data->fan_div[nr] = val;
423	regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
424		 | (data->fan_div[nr] << (6 - 2 * nr));
425	gl518_write_value(client, GL518_REG_MISC, regvalue);
426	mutex_unlock(&data->update_lock);
427	return count;
428}
429
430static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
431static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
432static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
433	show_temp_hyst1, set_temp_hyst1);
434static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
435static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
436static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
437static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO,
438	show_fan_min, set_fan_min, 0);
439static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO,
440	show_fan_min, set_fan_min, 1);
441static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO,
442	show_fan_div, set_fan_div, 0);
443static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO,
444	show_fan_div, set_fan_div, 1);
445static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
446static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
447static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
448static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
449static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
450static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
451static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
452static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
453static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
454static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
455static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
456static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
457static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
458static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
459	show_beep_enable, set_beep_enable);
460static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
461	show_beep_mask, set_beep_mask);
462
463static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
464			  char *buf)
465{
466	int bitnr = to_sensor_dev_attr(attr)->index;
467	struct gl518_data *data = gl518_update_device(dev);
468	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
469}
470
471static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
472static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
473static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
474static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
475static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
476static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
477static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
478
479static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
480			  char *buf)
481{
482	int bitnr = to_sensor_dev_attr(attr)->index;
483	struct gl518_data *data = gl518_update_device(dev);
484	return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
485}
486
487static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
488			const char *buf, size_t count)
489{
490	struct gl518_data *data = dev_get_drvdata(dev);
491	struct i2c_client *client = data->client;
492	int bitnr = to_sensor_dev_attr(attr)->index;
493	unsigned long bit;
494	int err;
495
496	err = kstrtoul(buf, 10, &bit);
497	if (err)
498		return err;
499
500	if (bit & ~1)
501		return -EINVAL;
502
503	mutex_lock(&data->update_lock);
504	data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
505	if (bit)
506		data->beep_mask |= (1 << bitnr);
507	else
508		data->beep_mask &= ~(1 << bitnr);
509	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
510	mutex_unlock(&data->update_lock);
511	return count;
512}
513
514static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0);
515static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1);
516static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2);
517static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3);
518static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4);
519static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5);
520static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6);
521
522static struct attribute *gl518_attributes[] = {
523	&dev_attr_in3_input.attr,
524	&dev_attr_in0_min.attr,
525	&dev_attr_in1_min.attr,
526	&dev_attr_in2_min.attr,
527	&dev_attr_in3_min.attr,
528	&dev_attr_in0_max.attr,
529	&dev_attr_in1_max.attr,
530	&dev_attr_in2_max.attr,
531	&dev_attr_in3_max.attr,
532	&sensor_dev_attr_in0_alarm.dev_attr.attr,
533	&sensor_dev_attr_in1_alarm.dev_attr.attr,
534	&sensor_dev_attr_in2_alarm.dev_attr.attr,
535	&sensor_dev_attr_in3_alarm.dev_attr.attr,
536	&sensor_dev_attr_in0_beep.dev_attr.attr,
537	&sensor_dev_attr_in1_beep.dev_attr.attr,
538	&sensor_dev_attr_in2_beep.dev_attr.attr,
539	&sensor_dev_attr_in3_beep.dev_attr.attr,
540
541	&dev_attr_fan1_auto.attr,
542	&sensor_dev_attr_fan1_input.dev_attr.attr,
543	&sensor_dev_attr_fan2_input.dev_attr.attr,
544	&sensor_dev_attr_fan1_min.dev_attr.attr,
545	&sensor_dev_attr_fan2_min.dev_attr.attr,
546	&sensor_dev_attr_fan1_div.dev_attr.attr,
547	&sensor_dev_attr_fan2_div.dev_attr.attr,
548	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
549	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
550	&sensor_dev_attr_fan1_beep.dev_attr.attr,
551	&sensor_dev_attr_fan2_beep.dev_attr.attr,
552
553	&dev_attr_temp1_input.attr,
554	&dev_attr_temp1_max.attr,
555	&dev_attr_temp1_max_hyst.attr,
556	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
557	&sensor_dev_attr_temp1_beep.dev_attr.attr,
558
559	&dev_attr_alarms.attr,
560	&dev_attr_beep_enable.attr,
561	&dev_attr_beep_mask.attr,
562	NULL
563};
564
565static const struct attribute_group gl518_group = {
566	.attrs = gl518_attributes,
567};
568
569static struct attribute *gl518_attributes_r80[] = {
570	&dev_attr_in0_input.attr,
571	&dev_attr_in1_input.attr,
572	&dev_attr_in2_input.attr,
573	NULL
574};
575
576static const struct attribute_group gl518_group_r80 = {
577	.attrs = gl518_attributes_r80,
578};
579
580/*
581 * Real code
582 */
583
584/* Return 0 if detection is successful, -ENODEV otherwise */
585static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
586{
587	struct i2c_adapter *adapter = client->adapter;
588	int rev;
589
590	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
591				     I2C_FUNC_SMBUS_WORD_DATA))
592		return -ENODEV;
593
594	/* Now, we do the remaining detection. */
595	if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
596	 || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
597		return -ENODEV;
598
599	/* Determine the chip type. */
600	rev = gl518_read_value(client, GL518_REG_REVISION);
601	if (rev != 0x00 && rev != 0x80)
602		return -ENODEV;
603
604	strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
605
606	return 0;
607}
608
609/*
610 * Called when we have found a new GL518SM.
611 * Note that we preserve D4:NoFan2 and D2:beep_enable.
612 */
613static void gl518_init_client(struct i2c_client *client)
614{
615	/* Make sure we leave D7:Reset untouched */
616	u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
617
618	/* Comparator mode (D3=0), standby mode (D6=0) */
619	gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
620
621	/* Never interrupts */
622	gl518_write_value(client, GL518_REG_MASK, 0x00);
623
624	/* Clear status register (D5=1), start (D6=1) */
625	gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
626	gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
627}
628
629static int gl518_probe(struct i2c_client *client,
630		       const struct i2c_device_id *id)
631{
632	struct device *dev = &client->dev;
633	struct device *hwmon_dev;
634	struct gl518_data *data;
635	int revision;
636
637	data = devm_kzalloc(dev, sizeof(struct gl518_data), GFP_KERNEL);
638	if (!data)
639		return -ENOMEM;
640
641	data->client = client;
642	revision = gl518_read_value(client, GL518_REG_REVISION);
643	data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
644	mutex_init(&data->update_lock);
645
646	/* Initialize the GL518SM chip */
647	data->alarm_mask = 0xff;
648	gl518_init_client(client);
649
650	/* sysfs hooks */
651	data->groups[0] = &gl518_group;
652	if (data->type == gl518sm_r80)
653		data->groups[1] = &gl518_group_r80;
654
655	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
656							   data, data->groups);
657	return PTR_ERR_OR_ZERO(hwmon_dev);
658}
659
660static const struct i2c_device_id gl518_id[] = {
661	{ "gl518sm", 0 },
662	{ }
663};
664MODULE_DEVICE_TABLE(i2c, gl518_id);
665
666static struct i2c_driver gl518_driver = {
667	.class		= I2C_CLASS_HWMON,
668	.driver = {
669		.name	= "gl518sm",
670	},
671	.probe		= gl518_probe,
672	.id_table	= gl518_id,
673	.detect		= gl518_detect,
674	.address_list	= normal_i2c,
675};
676
677module_i2c_driver(gl518_driver);
678
679MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
680	"Kyosti Malkki <kmalkki@cc.hut.fi> and "
681	"Hong-Gunn Chew <hglinux@gunnet.org>");
682MODULE_DESCRIPTION("GL518SM driver");
683MODULE_LICENSE("GPL");