2 * gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
5 * Kyösti Mälkki <kmalkki@cc.hut.fi>
6 * Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/hwmon-vid.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <linux/sysfs.h>
36 /* Type of the extra sensor */
37 static unsigned short extra_sensor_type;
38 module_param(extra_sensor_type, ushort, 0);
39 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
41 /* Addresses to scan */
42 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
45 * Many GL520 constants specified below
46 * One of the inputs can be configured as either temp or voltage.
47 * That's why _TEMP2 and _IN4 access the same register
50 /* The GL520 registers */
51 #define GL520_REG_CHIP_ID 0x00
52 #define GL520_REG_REVISION 0x01
53 #define GL520_REG_CONF 0x03
54 #define GL520_REG_MASK 0x11
56 #define GL520_REG_VID_INPUT 0x02
58 static const u8 GL520_REG_IN_INPUT[] = { 0x15, 0x14, 0x13, 0x0d, 0x0e };
59 static const u8 GL520_REG_IN_LIMIT[] = { 0x0c, 0x09, 0x0a, 0x0b };
60 static const u8 GL520_REG_IN_MIN[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 };
61 static const u8 GL520_REG_IN_MAX[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 };
63 static const u8 GL520_REG_TEMP_INPUT[] = { 0x04, 0x0e };
64 static const u8 GL520_REG_TEMP_MAX[] = { 0x05, 0x17 };
65 static const u8 GL520_REG_TEMP_MAX_HYST[] = { 0x06, 0x18 };
67 #define GL520_REG_FAN_INPUT 0x07
68 #define GL520_REG_FAN_MIN 0x08
69 #define GL520_REG_FAN_DIV 0x0f
70 #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
72 #define GL520_REG_ALARMS 0x12
73 #define GL520_REG_BEEP_MASK 0x10
74 #define GL520_REG_BEEP_ENABLE GL520_REG_CONF
78 struct i2c_client *client;
79 const struct attribute_group *groups[3];
80 struct mutex update_lock;
81 char valid; /* zero until the following fields are valid */
82 unsigned long last_updated; /* in jiffies */
86 u8 in_input[5]; /* [0] = VVD */
87 u8 in_min[5]; /* [0] = VDD */
88 u8 in_max[5]; /* [0] = VDD */
104 * Registers 0x07 to 0x0c are word-sized, others are byte-sized
105 * GL520 uses a high-byte first convention
107 static int gl520_read_value(struct i2c_client *client, u8 reg)
109 if ((reg >= 0x07) && (reg <= 0x0c))
110 return i2c_smbus_read_word_swapped(client, reg);
112 return i2c_smbus_read_byte_data(client, reg);
115 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
117 if ((reg >= 0x07) && (reg <= 0x0c))
118 return i2c_smbus_write_word_swapped(client, reg, value);
120 return i2c_smbus_write_byte_data(client, reg, value);
123 static struct gl520_data *gl520_update_device(struct device *dev)
125 struct gl520_data *data = dev_get_drvdata(dev);
126 struct i2c_client *client = data->client;
129 mutex_lock(&data->update_lock);
131 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
133 dev_dbg(&client->dev, "Starting gl520sm update\n");
135 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
136 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
137 data->vid = gl520_read_value(client,
138 GL520_REG_VID_INPUT) & 0x1f;
140 for (i = 0; i < 4; i++) {
141 data->in_input[i] = gl520_read_value(client,
142 GL520_REG_IN_INPUT[i]);
143 val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]);
144 data->in_min[i] = val & 0xff;
145 data->in_max[i] = (val >> 8) & 0xff;
148 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
149 data->fan_input[0] = (val >> 8) & 0xff;
150 data->fan_input[1] = val & 0xff;
152 val = gl520_read_value(client, GL520_REG_FAN_MIN);
153 data->fan_min[0] = (val >> 8) & 0xff;
154 data->fan_min[1] = val & 0xff;
156 data->temp_input[0] = gl520_read_value(client,
157 GL520_REG_TEMP_INPUT[0]);
158 data->temp_max[0] = gl520_read_value(client,
159 GL520_REG_TEMP_MAX[0]);
160 data->temp_max_hyst[0] = gl520_read_value(client,
161 GL520_REG_TEMP_MAX_HYST[0]);
163 val = gl520_read_value(client, GL520_REG_FAN_DIV);
164 data->fan_div[0] = (val >> 6) & 0x03;
165 data->fan_div[1] = (val >> 4) & 0x03;
166 data->fan_off = (val >> 2) & 0x01;
168 data->alarms &= data->alarm_mask;
170 val = gl520_read_value(client, GL520_REG_CONF);
171 data->beep_enable = !((val >> 2) & 1);
173 /* Temp1 and Vin4 are the same input */
174 if (data->two_temps) {
175 data->temp_input[1] = gl520_read_value(client,
176 GL520_REG_TEMP_INPUT[1]);
177 data->temp_max[1] = gl520_read_value(client,
178 GL520_REG_TEMP_MAX[1]);
179 data->temp_max_hyst[1] = gl520_read_value(client,
180 GL520_REG_TEMP_MAX_HYST[1]);
182 data->in_input[4] = gl520_read_value(client,
183 GL520_REG_IN_INPUT[4]);
184 data->in_min[4] = gl520_read_value(client,
185 GL520_REG_IN_MIN[4]);
186 data->in_max[4] = gl520_read_value(client,
187 GL520_REG_IN_MAX[4]);
190 data->last_updated = jiffies;
194 mutex_unlock(&data->update_lock);
203 static ssize_t cpu0_vid_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
206 struct gl520_data *data = gl520_update_device(dev);
207 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
209 static DEVICE_ATTR_RO(cpu0_vid);
211 #define VDD_FROM_REG(val) DIV_ROUND_CLOSEST((val) * 95, 4)
212 #define VDD_CLAMP(val) clamp_val(val, 0, 255 * 95 / 4)
213 #define VDD_TO_REG(val) DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
215 #define IN_FROM_REG(val) ((val) * 19)
216 #define IN_CLAMP(val) clamp_val(val, 0, 255 * 19)
217 #define IN_TO_REG(val) DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
219 static ssize_t in_input_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 int n = to_sensor_dev_attr(attr)->index;
223 struct gl520_data *data = gl520_update_device(dev);
224 u8 r = data->in_input[n];
227 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
229 return sprintf(buf, "%d\n", IN_FROM_REG(r));
232 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
235 int n = to_sensor_dev_attr(attr)->index;
236 struct gl520_data *data = gl520_update_device(dev);
237 u8 r = data->in_min[n];
240 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
242 return sprintf(buf, "%d\n", IN_FROM_REG(r));
245 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
248 int n = to_sensor_dev_attr(attr)->index;
249 struct gl520_data *data = gl520_update_device(dev);
250 u8 r = data->in_max[n];
253 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
255 return sprintf(buf, "%d\n", IN_FROM_REG(r));
258 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t count)
261 struct gl520_data *data = dev_get_drvdata(dev);
262 struct i2c_client *client = data->client;
263 int n = to_sensor_dev_attr(attr)->index;
268 err = kstrtol(buf, 10, &v);
272 mutex_lock(&data->update_lock);
282 gl520_write_value(client, GL520_REG_IN_MIN[n],
283 (gl520_read_value(client, GL520_REG_IN_MIN[n])
286 gl520_write_value(client, GL520_REG_IN_MIN[n], r);
288 mutex_unlock(&data->update_lock);
292 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
295 struct gl520_data *data = dev_get_drvdata(dev);
296 struct i2c_client *client = data->client;
297 int n = to_sensor_dev_attr(attr)->index;
302 err = kstrtol(buf, 10, &v);
311 mutex_lock(&data->update_lock);
316 gl520_write_value(client, GL520_REG_IN_MAX[n],
317 (gl520_read_value(client, GL520_REG_IN_MAX[n])
318 & ~0xff00) | (r << 8));
320 gl520_write_value(client, GL520_REG_IN_MAX[n], r);
322 mutex_unlock(&data->update_lock);
326 static SENSOR_DEVICE_ATTR_RO(in0_input, in_input, 0);
327 static SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
328 static SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
329 static SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
330 static SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
331 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
332 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
333 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
334 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
335 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
336 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
337 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
338 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
339 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
340 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
342 #define DIV_FROM_REG(val) (1 << (val))
343 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) << (div))))
345 #define FAN_BASE(div) (480000 >> (div))
346 #define FAN_CLAMP(val, div) clamp_val(val, FAN_BASE(div) / 255, \
348 #define FAN_TO_REG(val, div) ((val) == 0 ? 0 : \
349 DIV_ROUND_CLOSEST(480000, \
350 FAN_CLAMP(val, div) << (div)))
352 static ssize_t fan_input_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 int n = to_sensor_dev_attr(attr)->index;
356 struct gl520_data *data = gl520_update_device(dev);
358 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n],
362 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
365 int n = to_sensor_dev_attr(attr)->index;
366 struct gl520_data *data = gl520_update_device(dev);
368 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n],
372 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
375 int n = to_sensor_dev_attr(attr)->index;
376 struct gl520_data *data = gl520_update_device(dev);
378 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n]));
381 static ssize_t fan1_off_show(struct device *dev,
382 struct device_attribute *attr, char *buf)
384 struct gl520_data *data = gl520_update_device(dev);
385 return sprintf(buf, "%d\n", data->fan_off);
388 static ssize_t fan_min_store(struct device *dev,
389 struct device_attribute *attr, const char *buf,
392 struct gl520_data *data = dev_get_drvdata(dev);
393 struct i2c_client *client = data->client;
394 int n = to_sensor_dev_attr(attr)->index;
399 err = kstrtoul(buf, 10, &v);
403 mutex_lock(&data->update_lock);
404 r = FAN_TO_REG(v, data->fan_div[n]);
405 data->fan_min[n] = r;
408 gl520_write_value(client, GL520_REG_FAN_MIN,
409 (gl520_read_value(client, GL520_REG_FAN_MIN)
410 & ~0xff00) | (r << 8));
412 gl520_write_value(client, GL520_REG_FAN_MIN,
413 (gl520_read_value(client, GL520_REG_FAN_MIN)
416 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
417 if (data->fan_min[n] == 0)
418 data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40;
420 data->alarm_mask |= (n == 0) ? 0x20 : 0x40;
421 data->beep_mask &= data->alarm_mask;
422 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
424 mutex_unlock(&data->update_lock);
428 static ssize_t fan_div_store(struct device *dev,
429 struct device_attribute *attr, const char *buf,
432 struct gl520_data *data = dev_get_drvdata(dev);
433 struct i2c_client *client = data->client;
434 int n = to_sensor_dev_attr(attr)->index;
439 err = kstrtoul(buf, 10, &v);
457 dev_err(&client->dev,
458 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
462 mutex_lock(&data->update_lock);
463 data->fan_div[n] = r;
466 gl520_write_value(client, GL520_REG_FAN_DIV,
467 (gl520_read_value(client, GL520_REG_FAN_DIV)
468 & ~0xc0) | (r << 6));
470 gl520_write_value(client, GL520_REG_FAN_DIV,
471 (gl520_read_value(client, GL520_REG_FAN_DIV)
472 & ~0x30) | (r << 4));
474 mutex_unlock(&data->update_lock);
478 static ssize_t fan1_off_store(struct device *dev,
479 struct device_attribute *attr, const char *buf,
482 struct gl520_data *data = dev_get_drvdata(dev);
483 struct i2c_client *client = data->client;
488 err = kstrtoul(buf, 10, &v);
494 mutex_lock(&data->update_lock);
496 gl520_write_value(client, GL520_REG_FAN_OFF,
497 (gl520_read_value(client, GL520_REG_FAN_OFF)
498 & ~0x0c) | (r << 2));
499 mutex_unlock(&data->update_lock);
503 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
504 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
505 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
506 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
507 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
508 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
509 static DEVICE_ATTR_RW(fan1_off);
511 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
512 #define TEMP_CLAMP(val) clamp_val(val, -130000, 125000)
513 #define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 130)
515 static ssize_t temp_input_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 int n = to_sensor_dev_attr(attr)->index;
519 struct gl520_data *data = gl520_update_device(dev);
521 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n]));
524 static ssize_t temp_max_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 int n = to_sensor_dev_attr(attr)->index;
528 struct gl520_data *data = gl520_update_device(dev);
530 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n]));
533 static ssize_t temp_max_hyst_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
536 int n = to_sensor_dev_attr(attr)->index;
537 struct gl520_data *data = gl520_update_device(dev);
539 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n]));
542 static ssize_t temp_max_store(struct device *dev,
543 struct device_attribute *attr, const char *buf,
546 struct gl520_data *data = dev_get_drvdata(dev);
547 struct i2c_client *client = data->client;
548 int n = to_sensor_dev_attr(attr)->index;
552 err = kstrtol(buf, 10, &v);
556 mutex_lock(&data->update_lock);
557 data->temp_max[n] = TEMP_TO_REG(v);
558 gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]);
559 mutex_unlock(&data->update_lock);
563 static ssize_t temp_max_hyst_store(struct device *dev,
564 struct device_attribute *attr,
565 const char *buf, size_t count)
567 struct gl520_data *data = dev_get_drvdata(dev);
568 struct i2c_client *client = data->client;
569 int n = to_sensor_dev_attr(attr)->index;
573 err = kstrtol(buf, 10, &v);
577 mutex_lock(&data->update_lock);
578 data->temp_max_hyst[n] = TEMP_TO_REG(v);
579 gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n],
580 data->temp_max_hyst[n]);
581 mutex_unlock(&data->update_lock);
585 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
586 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
587 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
588 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
589 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_max_hyst, 0);
590 static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_max_hyst, 1);
592 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
595 struct gl520_data *data = gl520_update_device(dev);
596 return sprintf(buf, "%d\n", data->alarms);
599 static ssize_t beep_enable_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
602 struct gl520_data *data = gl520_update_device(dev);
603 return sprintf(buf, "%d\n", data->beep_enable);
606 static ssize_t beep_mask_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct gl520_data *data = gl520_update_device(dev);
610 return sprintf(buf, "%d\n", data->beep_mask);
613 static ssize_t beep_enable_store(struct device *dev,
614 struct device_attribute *attr,
615 const char *buf, size_t count)
617 struct gl520_data *data = dev_get_drvdata(dev);
618 struct i2c_client *client = data->client;
623 err = kstrtoul(buf, 10, &v);
629 mutex_lock(&data->update_lock);
630 data->beep_enable = !r;
631 gl520_write_value(client, GL520_REG_BEEP_ENABLE,
632 (gl520_read_value(client, GL520_REG_BEEP_ENABLE)
633 & ~0x04) | (r << 2));
634 mutex_unlock(&data->update_lock);
638 static ssize_t beep_mask_store(struct device *dev,
639 struct device_attribute *attr, const char *buf,
642 struct gl520_data *data = dev_get_drvdata(dev);
643 struct i2c_client *client = data->client;
647 err = kstrtoul(buf, 10, &r);
651 mutex_lock(&data->update_lock);
652 r &= data->alarm_mask;
654 gl520_write_value(client, GL520_REG_BEEP_MASK, r);
655 mutex_unlock(&data->update_lock);
659 static DEVICE_ATTR_RO(alarms);
660 static DEVICE_ATTR_RW(beep_enable);
661 static DEVICE_ATTR_RW(beep_mask);
663 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
666 int bit_nr = to_sensor_dev_attr(attr)->index;
667 struct gl520_data *data = gl520_update_device(dev);
669 return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1);
672 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
673 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
674 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
675 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
676 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
677 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 5);
678 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 6);
679 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 7);
680 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 7);
682 static ssize_t beep_show(struct device *dev, struct device_attribute *attr,
685 int bitnr = to_sensor_dev_attr(attr)->index;
686 struct gl520_data *data = gl520_update_device(dev);
688 return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
691 static ssize_t beep_store(struct device *dev, struct device_attribute *attr,
692 const char *buf, size_t count)
694 struct gl520_data *data = dev_get_drvdata(dev);
695 struct i2c_client *client = data->client;
696 int bitnr = to_sensor_dev_attr(attr)->index;
701 err = kstrtoul(buf, 10, &bit);
707 mutex_lock(&data->update_lock);
708 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
710 data->beep_mask |= (1 << bitnr);
712 data->beep_mask &= ~(1 << bitnr);
713 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
714 mutex_unlock(&data->update_lock);
718 static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
719 static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
720 static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
721 static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
722 static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
723 static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 5);
724 static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 6);
725 static SENSOR_DEVICE_ATTR_RW(temp2_beep, beep, 7);
726 static SENSOR_DEVICE_ATTR_RW(in4_beep, beep, 7);
728 static struct attribute *gl520_attributes[] = {
729 &dev_attr_cpu0_vid.attr,
731 &sensor_dev_attr_in0_input.dev_attr.attr,
732 &sensor_dev_attr_in0_min.dev_attr.attr,
733 &sensor_dev_attr_in0_max.dev_attr.attr,
734 &sensor_dev_attr_in0_alarm.dev_attr.attr,
735 &sensor_dev_attr_in0_beep.dev_attr.attr,
736 &sensor_dev_attr_in1_input.dev_attr.attr,
737 &sensor_dev_attr_in1_min.dev_attr.attr,
738 &sensor_dev_attr_in1_max.dev_attr.attr,
739 &sensor_dev_attr_in1_alarm.dev_attr.attr,
740 &sensor_dev_attr_in1_beep.dev_attr.attr,
741 &sensor_dev_attr_in2_input.dev_attr.attr,
742 &sensor_dev_attr_in2_min.dev_attr.attr,
743 &sensor_dev_attr_in2_max.dev_attr.attr,
744 &sensor_dev_attr_in2_alarm.dev_attr.attr,
745 &sensor_dev_attr_in2_beep.dev_attr.attr,
746 &sensor_dev_attr_in3_input.dev_attr.attr,
747 &sensor_dev_attr_in3_min.dev_attr.attr,
748 &sensor_dev_attr_in3_max.dev_attr.attr,
749 &sensor_dev_attr_in3_alarm.dev_attr.attr,
750 &sensor_dev_attr_in3_beep.dev_attr.attr,
752 &sensor_dev_attr_fan1_input.dev_attr.attr,
753 &sensor_dev_attr_fan1_min.dev_attr.attr,
754 &sensor_dev_attr_fan1_div.dev_attr.attr,
755 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
756 &sensor_dev_attr_fan1_beep.dev_attr.attr,
757 &dev_attr_fan1_off.attr,
758 &sensor_dev_attr_fan2_input.dev_attr.attr,
759 &sensor_dev_attr_fan2_min.dev_attr.attr,
760 &sensor_dev_attr_fan2_div.dev_attr.attr,
761 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
762 &sensor_dev_attr_fan2_beep.dev_attr.attr,
764 &sensor_dev_attr_temp1_input.dev_attr.attr,
765 &sensor_dev_attr_temp1_max.dev_attr.attr,
766 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
767 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
768 &sensor_dev_attr_temp1_beep.dev_attr.attr,
770 &dev_attr_alarms.attr,
771 &dev_attr_beep_enable.attr,
772 &dev_attr_beep_mask.attr,
776 static const struct attribute_group gl520_group = {
777 .attrs = gl520_attributes,
780 static struct attribute *gl520_attributes_in4[] = {
781 &sensor_dev_attr_in4_input.dev_attr.attr,
782 &sensor_dev_attr_in4_min.dev_attr.attr,
783 &sensor_dev_attr_in4_max.dev_attr.attr,
784 &sensor_dev_attr_in4_alarm.dev_attr.attr,
785 &sensor_dev_attr_in4_beep.dev_attr.attr,
789 static struct attribute *gl520_attributes_temp2[] = {
790 &sensor_dev_attr_temp2_input.dev_attr.attr,
791 &sensor_dev_attr_temp2_max.dev_attr.attr,
792 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
793 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
794 &sensor_dev_attr_temp2_beep.dev_attr.attr,
798 static const struct attribute_group gl520_group_in4 = {
799 .attrs = gl520_attributes_in4,
802 static const struct attribute_group gl520_group_temp2 = {
803 .attrs = gl520_attributes_temp2,
811 /* Return 0 if detection is successful, -ENODEV otherwise */
812 static int gl520_detect(struct i2c_client *client, struct i2c_board_info *info)
814 struct i2c_adapter *adapter = client->adapter;
816 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
817 I2C_FUNC_SMBUS_WORD_DATA))
820 /* Determine the chip type. */
821 if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) ||
822 ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
823 ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
824 dev_dbg(&client->dev, "Unknown chip type, skipping\n");
828 strlcpy(info->type, "gl520sm", I2C_NAME_SIZE);
833 /* Called when we have found a new GL520SM. */
834 static void gl520_init_client(struct i2c_client *client)
836 struct gl520_data *data = i2c_get_clientdata(client);
839 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
841 data->alarm_mask = 0xff;
842 data->vrm = vid_which_vrm();
844 if (extra_sensor_type == 1)
846 else if (extra_sensor_type == 2)
848 data->two_temps = !(conf & 0x10);
850 /* If IRQ# is disabled, we can safely force comparator mode */
854 /* Enable monitoring if needed */
858 gl520_write_value(client, GL520_REG_CONF, conf);
860 gl520_update_device(&(client->dev));
862 if (data->fan_min[0] == 0)
863 data->alarm_mask &= ~0x20;
864 if (data->fan_min[1] == 0)
865 data->alarm_mask &= ~0x40;
867 data->beep_mask &= data->alarm_mask;
868 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
871 static int gl520_probe(struct i2c_client *client,
872 const struct i2c_device_id *id)
874 struct device *dev = &client->dev;
875 struct device *hwmon_dev;
876 struct gl520_data *data;
878 data = devm_kzalloc(dev, sizeof(struct gl520_data), GFP_KERNEL);
882 i2c_set_clientdata(client, data);
883 mutex_init(&data->update_lock);
884 data->client = client;
886 /* Initialize the GL520SM chip */
887 gl520_init_client(client);
890 data->groups[0] = &gl520_group;
893 data->groups[1] = &gl520_group_temp2;
895 data->groups[1] = &gl520_group_in4;
897 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
899 return PTR_ERR_OR_ZERO(hwmon_dev);
902 static const struct i2c_device_id gl520_id[] = {
906 MODULE_DEVICE_TABLE(i2c, gl520_id);
908 static struct i2c_driver gl520_driver = {
909 .class = I2C_CLASS_HWMON,
913 .probe = gl520_probe,
914 .id_table = gl520_id,
915 .detect = gl520_detect,
916 .address_list = normal_i2c,
919 module_i2c_driver(gl520_driver);
921 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
922 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
923 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
924 MODULE_DESCRIPTION("GL520SM driver");
925 MODULE_LICENSE("GPL");