35 #include <linux/module.h>
37 #include <linux/slab.h>
39 #include <linux/i2c.h>
47 static const unsigned short normal_i2c[] = { 0x2c, 0x2d,
I2C_CLIENT_END };
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
84 #define RAW_FROM_REG(val) val
86 #define BOOL_FROM_REG(val) ((val) ? 0 : 1)
87 #define BOOL_TO_REG(val) ((val) ? 0 : 1)
89 #define TEMP_TO_REG(val) SENSORS_LIMIT(((((val) < 0 ? \
91 (val) + 500) / 1000) + 119), 0, 255)
92 #define TEMP_FROM_REG(val) (((val) - 119) * 1000)
99 rpmdiv = SENSORS_LIMIT(rpm, 1, 960000) *
div;
100 return SENSORS_LIMIT((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
102 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) * (div))))
104 #define IN_TO_REG(val) SENSORS_LIMIT((((val) + 9) / 19), 0, 255)
105 #define IN_FROM_REG(val) ((val) * 19)
107 #define VDD_TO_REG(val) SENSORS_LIMIT((((val) * 4 + 47) / 95), 0, 255)
108 #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4)
110 #define DIV_FROM_REG(val) (1 << (val))
112 #define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
113 #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
161 .probe = gl518_probe,
162 .remove = gl518_remove,
163 .id_table = gl518_id,
164 .detect = gl518_detect,
165 .address_list = normal_i2c,
172 #define show(type, suffix, value) \
173 static ssize_t show_##suffix(struct device *dev, \
174 struct device_attribute *attr, char *buf) \
176 struct gl518_data *data = gl518_update_device(dev); \
177 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
184 show(VDD, in_input0, voltage_in[0]);
185 show(
IN, in_input1, voltage_in[1]);
186 show(
IN, in_input2, voltage_in[2]);
187 show(
IN, in_input3, voltage_in[3]);
188 show(VDD, in_min0, voltage_min[0]);
189 show(
IN, in_min1, voltage_min[1]);
190 show(
IN, in_min2, voltage_min[2]);
191 show(
IN, in_min3, voltage_min[3]);
192 show(VDD, in_max0, voltage_max[0]);
193 show(
IN, in_max1, voltage_max[1]);
194 show(
IN, in_max2, voltage_max[2]);
195 show(
IN, in_max3, voltage_max[3]);
196 show(RAW, alarms, alarms);
197 show(
BOOL, beep_enable, beep_enable);
198 show(BEEP_MASK, beep_mask, beep_mask);
213 struct gl518_data *data = gl518_update_device(dev);
222 struct gl518_data *data = gl518_update_device(dev);
226 #define set(type, suffix, value, reg) \
227 static ssize_t set_##suffix(struct device *dev, \
228 struct device_attribute *attr, \
229 const char *buf, size_t count) \
231 struct i2c_client *client = to_i2c_client(dev); \
232 struct gl518_data *data = i2c_get_clientdata(client); \
234 int err = kstrtol(buf, 10, &val); \
238 mutex_lock(&data->update_lock); \
239 data->value = type##_TO_REG(val); \
240 gl518_write_value(client, reg, data->value); \
241 mutex_unlock(&data->update_lock); \
245 #define set_bits(type, suffix, value, reg, mask, shift) \
246 static ssize_t set_##suffix(struct device *dev, \
247 struct device_attribute *attr, \
248 const char *buf, size_t count) \
250 struct i2c_client *client = to_i2c_client(dev); \
251 struct gl518_data *data = i2c_get_clientdata(client); \
254 int err = kstrtoul(buf, 10, &val); \
258 mutex_lock(&data->update_lock); \
259 regvalue = gl518_read_value(client, reg); \
260 data->value = type##_TO_REG(val); \
261 regvalue = (regvalue & ~mask) | (data->value << shift); \
262 gl518_write_value(client, reg, regvalue); \
263 mutex_unlock(&data->update_lock); \
267 #define set_low(type, suffix, value, reg) \
268 set_bits(type, suffix, value, reg, 0x00ff, 0)
269 #define set_high(type, suffix, value, reg) \
270 set_bits(type, suffix, value, reg, 0xff00, 8)
287 const char *buf,
size_t count)
290 struct gl518_data *data = i2c_get_clientdata(client);
296 err = kstrtoul(buf, 10, &val);
303 regvalue = (regvalue & (0xff << (8 *
nr)))
304 | (data->
fan_min[nr] << (8 * (1 - nr)));
320 const char *buf,
size_t count)
323 struct gl518_data *data = i2c_get_clientdata(client);
329 err = kstrtoul(buf, 10, &val);
347 dev_err(dev,
"Invalid fan clock divider %lu, choose one "
348 "of 1, 2, 4 or 8\n", val);
355 regvalue = (regvalue & ~(0xc0 >> (2 *
nr)))
356 | (data->
fan_div[nr] << (6 - 2 * nr));
365 show_temp_hyst1, set_temp_hyst1);
370 show_fan_min, set_fan_min, 0);
372 show_fan_min, set_fan_min, 1);
374 show_fan_div, set_fan_div, 0);
376 show_fan_div, set_fan_div, 1);
391 show_beep_enable, set_beep_enable);
393 show_beep_mask, set_beep_mask);
399 struct gl518_data *data = gl518_update_device(dev);
415 struct gl518_data *data = gl518_update_device(dev);
420 const char *buf,
size_t count)
423 struct gl518_data *data = i2c_get_clientdata(client);
428 err = kstrtoul(buf, 10, &bit);
454 static struct attribute *gl518_attributes[] = {
455 &dev_attr_in3_input.attr,
456 &dev_attr_in0_min.attr,
457 &dev_attr_in1_min.attr,
458 &dev_attr_in2_min.attr,
459 &dev_attr_in3_min.attr,
460 &dev_attr_in0_max.attr,
461 &dev_attr_in1_max.attr,
462 &dev_attr_in2_max.attr,
463 &dev_attr_in3_max.attr,
464 &sensor_dev_attr_in0_alarm.dev_attr.attr,
465 &sensor_dev_attr_in1_alarm.dev_attr.attr,
466 &sensor_dev_attr_in2_alarm.dev_attr.attr,
467 &sensor_dev_attr_in3_alarm.dev_attr.attr,
468 &sensor_dev_attr_in0_beep.dev_attr.attr,
469 &sensor_dev_attr_in1_beep.dev_attr.attr,
470 &sensor_dev_attr_in2_beep.dev_attr.attr,
471 &sensor_dev_attr_in3_beep.dev_attr.attr,
473 &dev_attr_fan1_auto.attr,
474 &sensor_dev_attr_fan1_input.dev_attr.attr,
475 &sensor_dev_attr_fan2_input.dev_attr.attr,
476 &sensor_dev_attr_fan1_min.dev_attr.attr,
477 &sensor_dev_attr_fan2_min.dev_attr.attr,
478 &sensor_dev_attr_fan1_div.dev_attr.attr,
479 &sensor_dev_attr_fan2_div.dev_attr.attr,
480 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
481 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
482 &sensor_dev_attr_fan1_beep.dev_attr.attr,
483 &sensor_dev_attr_fan2_beep.dev_attr.attr,
485 &dev_attr_temp1_input.attr,
486 &dev_attr_temp1_max.attr,
487 &dev_attr_temp1_max_hyst.attr,
488 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
489 &sensor_dev_attr_temp1_beep.dev_attr.attr,
491 &dev_attr_alarms.attr,
492 &dev_attr_beep_enable.attr,
493 &dev_attr_beep_mask.attr,
498 .attrs = gl518_attributes,
501 static struct attribute *gl518_attributes_r80[] = {
502 &dev_attr_in0_input.attr,
503 &dev_attr_in1_input.attr,
504 &dev_attr_in2_input.attr,
509 .attrs = gl518_attributes_r80,
533 if (rev != 0x00 && rev != 0x80)
541 static int gl518_probe(
struct i2c_client *client,
552 i2c_set_clientdata(client, data);
559 gl518_init_client(client);
568 goto exit_remove_files;
574 goto exit_remove_files;
591 static void gl518_init_client(
struct i2c_client *client)
607 static int gl518_remove(
struct i2c_client *client)
609 struct gl518_data *data = i2c_get_clientdata(client);
626 if ((reg >= 0x07) && (reg <= 0x0c))
627 return i2c_smbus_read_word_swapped(client, reg);
634 if ((reg >= 0x07) && (reg <= 0x0c))
635 return i2c_smbus_write_word_swapped(client, reg, value);
643 struct gl518_data *data = i2c_get_clientdata(client);
650 dev_dbg(&client->
dev,
"Starting gl518 update\n");
669 data->
fan_in[0] = (val >> 8) & 0xff;
670 data->
fan_in[1] = val & 0xff;
673 data->
fan_min[0] = (val >> 8) & 0xff;
683 data->
fan_div[0] = (val >> 6) & 0x03;
684 data->
fan_div[1] = (val >> 4) & 0x03;