Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 <khali@linux-fr.org> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare |
| 24 | * and advice of Greg Kroah-Hartman. |
| 25 | * |
| 26 | * Notes about the port: |
| 27 | * Release 0x00 of the GL518SM chipset doesn't support reading of in0, |
| 28 | * in1 nor in2. The original driver had an ugly workaround to get them |
| 29 | * anyway (changing limits and watching alarms trigger and wear off). |
| 30 | * We did not keep that part of the original driver in the Linux 2.6 |
| 31 | * version, since it was making the driver significantly more complex |
| 32 | * with no real benefit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 40 | #include <linux/hwmon.h> |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 41 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 42 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 43 | #include <linux/mutex.h> |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 44 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 47 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 49 | enum chips { gl518sm_r00, gl518sm_r80 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 86 | #define BOOL_FROM_REG(val) ((val) ? 0 : 1) |
| 87 | #define BOOL_TO_REG(val) ((val) ? 0 : 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 89 | #define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 90 | (val) - 500 : \ |
| 91 | (val) + 500) / 1000) + 119), 0, 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #define TEMP_FROM_REG(val) (((val) - 119) * 1000) |
| 93 | |
| 94 | static inline u8 FAN_TO_REG(long rpm, int div) |
| 95 | { |
| 96 | long rpmdiv; |
| 97 | if (rpm == 0) |
| 98 | return 0; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 99 | rpmdiv = clamp_val(rpm, 1, 960000) * div; |
| 100 | return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 102 | #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) * (div)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 104 | #define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255) |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 105 | #define IN_FROM_REG(val) ((val) * 19) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 107 | #define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255) |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 108 | #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #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 */ |
| 116 | struct gl518_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 117 | struct device *hwmon_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | enum chips type; |
| 119 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 120 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | 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 Delvare | a5955ed | 2007-10-22 17:45:08 +0200 | [diff] [blame] | 135 | u8 alarm_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | u8 beep_mask; /* Register value */ |
| 137 | u8 beep_enable; /* Boolean */ |
| 138 | }; |
| 139 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 140 | static int gl518_probe(struct i2c_client *client, |
| 141 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 142 | static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | static void gl518_init_client(struct i2c_client *client); |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 144 | static int gl518_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | static int gl518_read_value(struct i2c_client *client, u8 reg); |
| 146 | static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value); |
| 147 | static struct gl518_data *gl518_update_device(struct device *dev); |
| 148 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 149 | static const struct i2c_device_id gl518_id[] = { |
| 150 | { "gl518sm", 0 }, |
| 151 | { } |
| 152 | }; |
| 153 | MODULE_DEVICE_TABLE(i2c, gl518_id); |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* This is the driver that will be inserted */ |
| 156 | static struct i2c_driver gl518_driver = { |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 157 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 158 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 159 | .name = "gl518sm", |
| 160 | }, |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 161 | .probe = gl518_probe, |
| 162 | .remove = gl518_remove, |
| 163 | .id_table = gl518_id, |
| 164 | .detect = gl518_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 165 | .address_list = normal_i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /* |
| 169 | * Sysfs stuff |
| 170 | */ |
| 171 | |
| 172 | #define show(type, suffix, value) \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 173 | static ssize_t show_##suffix(struct device *dev, \ |
| 174 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { \ |
| 176 | struct gl518_data *data = gl518_update_device(dev); \ |
| 177 | return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \ |
| 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | show(TEMP, temp_input1, temp_in); |
| 181 | show(TEMP, temp_max1, temp_max); |
| 182 | show(TEMP, temp_hyst1, temp_hyst); |
| 183 | show(BOOL, fan_auto1, fan_auto1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 199 | |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 200 | static ssize_t show_fan_input(struct device *dev, |
| 201 | struct device_attribute *attr, char *buf) |
| 202 | { |
| 203 | int nr = to_sensor_dev_attr(attr)->index; |
| 204 | struct gl518_data *data = gl518_update_device(dev); |
| 205 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr], |
| 206 | DIV_FROM_REG(data->fan_div[nr]))); |
| 207 | } |
| 208 | |
| 209 | static ssize_t show_fan_min(struct device *dev, |
| 210 | struct device_attribute *attr, char *buf) |
| 211 | { |
| 212 | int nr = to_sensor_dev_attr(attr)->index; |
| 213 | struct gl518_data *data = gl518_update_device(dev); |
| 214 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 215 | DIV_FROM_REG(data->fan_div[nr]))); |
| 216 | } |
| 217 | |
| 218 | static ssize_t show_fan_div(struct device *dev, |
| 219 | struct device_attribute *attr, char *buf) |
| 220 | { |
| 221 | int nr = to_sensor_dev_attr(attr)->index; |
| 222 | struct gl518_data *data = gl518_update_device(dev); |
| 223 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
| 224 | } |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | #define set(type, suffix, value, reg) \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 227 | static ssize_t set_##suffix(struct device *dev, \ |
| 228 | struct device_attribute *attr, \ |
| 229 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { \ |
| 231 | struct i2c_client *client = to_i2c_client(dev); \ |
| 232 | struct gl518_data *data = i2c_get_clientdata(client); \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 233 | long val; \ |
| 234 | int err = kstrtol(buf, 10, &val); \ |
| 235 | if (err) \ |
| 236 | return err; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 238 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | data->value = type##_TO_REG(val); \ |
| 240 | gl518_write_value(client, reg, data->value); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 241 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return count; \ |
| 243 | } |
| 244 | |
| 245 | #define set_bits(type, suffix, value, reg, mask, shift) \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 246 | static ssize_t set_##suffix(struct device *dev, \ |
| 247 | struct device_attribute *attr, \ |
| 248 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { \ |
| 250 | struct i2c_client *client = to_i2c_client(dev); \ |
| 251 | struct gl518_data *data = i2c_get_clientdata(client); \ |
| 252 | int regvalue; \ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 253 | unsigned long val; \ |
| 254 | int err = kstrtoul(buf, 10, &val); \ |
| 255 | if (err) \ |
| 256 | return err; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 258 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 263 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | return count; \ |
| 265 | } |
| 266 | |
| 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) |
| 271 | |
| 272 | set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX); |
| 273 | set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST); |
| 274 | set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT); |
| 276 | set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT); |
| 277 | set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT); |
| 278 | set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT); |
| 279 | set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT); |
| 280 | set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT); |
| 281 | set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT); |
| 282 | set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT); |
| 283 | set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2); |
| 284 | set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM); |
| 285 | |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 286 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 287 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | struct i2c_client *client = to_i2c_client(dev); |
| 290 | struct gl518_data *data = i2c_get_clientdata(client); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 291 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | int regvalue; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 293 | unsigned long val; |
| 294 | int err; |
| 295 | |
| 296 | err = kstrtoul(buf, 10, &val); |
| 297 | if (err) |
| 298 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 300 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 302 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
| 303 | regvalue = (regvalue & (0xff << (8 * nr))) |
| 304 | | (data->fan_min[nr] << (8 * (1 - nr))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); |
| 306 | |
| 307 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 308 | if (data->fan_min[nr] == 0) |
| 309 | data->alarm_mask &= ~(0x20 << nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | else |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 311 | data->alarm_mask |= (0x20 << nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | data->beep_mask &= data->alarm_mask; |
| 313 | gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); |
| 314 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 315 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return count; |
| 317 | } |
| 318 | |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 319 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
| 320 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| 322 | struct i2c_client *client = to_i2c_client(dev); |
| 323 | struct gl518_data *data = i2c_get_clientdata(client); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 324 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | int regvalue; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 326 | unsigned long val; |
| 327 | int err; |
| 328 | |
| 329 | err = kstrtoul(buf, 10, &val); |
| 330 | if (err) |
| 331 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Jean Delvare | 21df67b | 2007-10-22 17:47:58 +0200 | [diff] [blame] | 333 | switch (val) { |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 334 | case 1: |
| 335 | val = 0; |
| 336 | break; |
| 337 | case 2: |
| 338 | val = 1; |
| 339 | break; |
| 340 | case 4: |
| 341 | val = 2; |
| 342 | break; |
| 343 | case 8: |
| 344 | val = 3; |
| 345 | break; |
Jean Delvare | 21df67b | 2007-10-22 17:47:58 +0200 | [diff] [blame] | 346 | default: |
| 347 | dev_err(dev, "Invalid fan clock divider %lu, choose one " |
| 348 | "of 1, 2, 4 or 8\n", val); |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 352 | mutex_lock(&data->update_lock); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 353 | regvalue = gl518_read_value(client, GL518_REG_MISC); |
Jean Delvare | 21df67b | 2007-10-22 17:47:58 +0200 | [diff] [blame] | 354 | data->fan_div[nr] = val; |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 355 | regvalue = (regvalue & ~(0xc0 >> (2 * nr))) |
| 356 | | (data->fan_div[nr] << (6 - 2 * nr)); |
| 357 | gl518_write_value(client, GL518_REG_MISC, regvalue); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 358 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return count; |
| 360 | } |
| 361 | |
| 362 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL); |
| 363 | static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1); |
| 364 | static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO, |
| 365 | show_temp_hyst1, set_temp_hyst1); |
| 366 | static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1); |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 367 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0); |
| 368 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1); |
| 369 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, |
| 370 | show_fan_min, set_fan_min, 0); |
| 371 | static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, |
| 372 | show_fan_min, set_fan_min, 1); |
| 373 | static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, |
| 374 | show_fan_div, set_fan_div, 0); |
| 375 | static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, |
| 376 | show_fan_div, set_fan_div, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL); |
| 378 | static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL); |
| 379 | static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL); |
| 380 | static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL); |
| 381 | static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0); |
| 382 | static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1); |
| 383 | static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2); |
| 384 | static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3); |
| 385 | static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0); |
| 386 | static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1); |
| 387 | static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2); |
| 388 | static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3); |
| 389 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 390 | static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO, |
| 391 | show_beep_enable, set_beep_enable); |
| 392 | static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO, |
| 393 | show_beep_mask, set_beep_mask); |
| 394 | |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 395 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 396 | char *buf) |
| 397 | { |
| 398 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 399 | struct gl518_data *data = gl518_update_device(dev); |
| 400 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 401 | } |
| 402 | |
| 403 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 404 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 405 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 406 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 407 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 408 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 409 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 410 | |
| 411 | static ssize_t show_beep(struct device *dev, struct device_attribute *attr, |
| 412 | char *buf) |
| 413 | { |
| 414 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 415 | struct gl518_data *data = gl518_update_device(dev); |
| 416 | return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1); |
| 417 | } |
| 418 | |
| 419 | static ssize_t set_beep(struct device *dev, struct device_attribute *attr, |
| 420 | const char *buf, size_t count) |
| 421 | { |
| 422 | struct i2c_client *client = to_i2c_client(dev); |
| 423 | struct gl518_data *data = i2c_get_clientdata(client); |
| 424 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 425 | unsigned long bit; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 426 | int err; |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 427 | |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 428 | err = kstrtoul(buf, 10, &bit); |
| 429 | if (err) |
| 430 | return err; |
| 431 | |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 432 | if (bit & ~1) |
| 433 | return -EINVAL; |
| 434 | |
| 435 | mutex_lock(&data->update_lock); |
| 436 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); |
| 437 | if (bit) |
| 438 | data->beep_mask |= (1 << bitnr); |
| 439 | else |
| 440 | data->beep_mask &= ~(1 << bitnr); |
| 441 | gl518_write_value(client, GL518_REG_ALARM, data->beep_mask); |
| 442 | mutex_unlock(&data->update_lock); |
| 443 | return count; |
| 444 | } |
| 445 | |
| 446 | static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 0); |
| 447 | static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 1); |
| 448 | static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 2); |
| 449 | static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 3); |
| 450 | static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 4); |
| 451 | static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 5); |
| 452 | static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_beep, set_beep, 6); |
| 453 | |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 454 | static struct attribute *gl518_attributes[] = { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 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, |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 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, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 472 | |
| 473 | &dev_attr_fan1_auto.attr, |
Jean Delvare | 28292e7 | 2007-10-22 17:46:42 +0200 | [diff] [blame] | 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, |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 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, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 484 | |
| 485 | &dev_attr_temp1_input.attr, |
| 486 | &dev_attr_temp1_max.attr, |
| 487 | &dev_attr_temp1_max_hyst.attr, |
Jean Delvare | da6848d | 2007-10-22 17:47:16 +0200 | [diff] [blame] | 488 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 489 | &sensor_dev_attr_temp1_beep.dev_attr.attr, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 490 | |
| 491 | &dev_attr_alarms.attr, |
| 492 | &dev_attr_beep_enable.attr, |
| 493 | &dev_attr_beep_mask.attr, |
| 494 | NULL |
| 495 | }; |
| 496 | |
| 497 | static const struct attribute_group gl518_group = { |
| 498 | .attrs = gl518_attributes, |
| 499 | }; |
| 500 | |
Jean Delvare | 5314f5c | 2007-10-22 17:46:17 +0200 | [diff] [blame] | 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, |
| 505 | NULL |
| 506 | }; |
| 507 | |
| 508 | static const struct attribute_group gl518_group_r80 = { |
| 509 | .attrs = gl518_attributes_r80, |
| 510 | }; |
| 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | /* |
| 513 | * Real code |
| 514 | */ |
| 515 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 516 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 517 | static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 519 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 520 | int rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
| 522 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 523 | I2C_FUNC_SMBUS_WORD_DATA)) |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 524 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | /* Now, we do the remaining detection. */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 527 | if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80) |
| 528 | || (gl518_read_value(client, GL518_REG_CONF) & 0x80)) |
| 529 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
| 531 | /* Determine the chip type. */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 532 | rev = gl518_read_value(client, GL518_REG_REVISION); |
| 533 | if (rev != 0x00 && rev != 0x80) |
| 534 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 536 | strlcpy(info->type, "gl518sm", I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int gl518_probe(struct i2c_client *client, |
| 542 | const struct i2c_device_id *id) |
| 543 | { |
| 544 | struct gl518_data *data; |
| 545 | int err, revision; |
| 546 | |
Guenter Roeck | bba636d | 2012-06-02 09:58:06 -0700 | [diff] [blame] | 547 | data = devm_kzalloc(&client->dev, sizeof(struct gl518_data), |
| 548 | GFP_KERNEL); |
| 549 | if (!data) |
| 550 | return -ENOMEM; |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 551 | |
| 552 | i2c_set_clientdata(client, data); |
| 553 | revision = gl518_read_value(client, GL518_REG_REVISION); |
| 554 | data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00; |
| 555 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | /* Initialize the GL518SM chip */ |
| 558 | data->alarm_mask = 0xff; |
Jean Delvare | a5955ed | 2007-10-22 17:45:08 +0200 | [diff] [blame] | 559 | gl518_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | /* Register sysfs hooks */ |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 562 | err = sysfs_create_group(&client->dev.kobj, &gl518_group); |
| 563 | if (err) |
Guenter Roeck | bba636d | 2012-06-02 09:58:06 -0700 | [diff] [blame] | 564 | return err; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 565 | if (data->type == gl518sm_r80) { |
| 566 | err = sysfs_create_group(&client->dev.kobj, &gl518_group_r80); |
| 567 | if (err) |
Jean Delvare | 5314f5c | 2007-10-22 17:46:17 +0200 | [diff] [blame] | 568 | goto exit_remove_files; |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 569 | } |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 570 | |
Jean Delvare | a5955ed | 2007-10-22 17:45:08 +0200 | [diff] [blame] | 571 | data->hwmon_dev = hwmon_device_register(&client->dev); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 572 | if (IS_ERR(data->hwmon_dev)) { |
| 573 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 574 | goto exit_remove_files; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 575 | } |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return 0; |
| 578 | |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 579 | exit_remove_files: |
Jean Delvare | a5955ed | 2007-10-22 17:45:08 +0200 | [diff] [blame] | 580 | sysfs_remove_group(&client->dev.kobj, &gl518_group); |
Jean Delvare | 5314f5c | 2007-10-22 17:46:17 +0200 | [diff] [blame] | 581 | if (data->type == gl518sm_r80) |
| 582 | sysfs_remove_group(&client->dev.kobj, &gl518_group_r80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return err; |
| 584 | } |
| 585 | |
| 586 | |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 587 | /* |
| 588 | * Called when we have found a new GL518SM. |
| 589 | * Note that we preserve D4:NoFan2 and D2:beep_enable. |
| 590 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | static void gl518_init_client(struct i2c_client *client) |
| 592 | { |
| 593 | /* Make sure we leave D7:Reset untouched */ |
| 594 | u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f; |
| 595 | |
| 596 | /* Comparator mode (D3=0), standby mode (D6=0) */ |
| 597 | gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37)); |
| 598 | |
| 599 | /* Never interrupts */ |
| 600 | gl518_write_value(client, GL518_REG_MASK, 0x00); |
| 601 | |
| 602 | /* Clear status register (D5=1), start (D6=1) */ |
| 603 | gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue); |
| 604 | gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue); |
| 605 | } |
| 606 | |
Jean Delvare | 95d80e7 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 607 | static int gl518_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 609 | struct gl518_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 611 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 612 | sysfs_remove_group(&client->dev.kobj, &gl518_group); |
Jean Delvare | 5314f5c | 2007-10-22 17:46:17 +0200 | [diff] [blame] | 613 | if (data->type == gl518sm_r80) |
| 614 | sysfs_remove_group(&client->dev.kobj, &gl518_group_r80); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
Guenter Roeck | 228f8e0 | 2012-01-14 13:32:55 -0800 | [diff] [blame] | 619 | /* |
| 620 | * Registers 0x07 to 0x0c are word-sized, others are byte-sized |
| 621 | * GL518 uses a high-byte first convention, which is exactly opposite to |
| 622 | * the SMBus standard. |
| 623 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | static int gl518_read_value(struct i2c_client *client, u8 reg) |
| 625 | { |
| 626 | if ((reg >= 0x07) && (reg <= 0x0c)) |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 627 | return i2c_smbus_read_word_swapped(client, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | else |
| 629 | return i2c_smbus_read_byte_data(client, reg); |
| 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) |
| 633 | { |
| 634 | if ((reg >= 0x07) && (reg <= 0x0c)) |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 635 | return i2c_smbus_write_word_swapped(client, reg, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | else |
| 637 | return i2c_smbus_write_byte_data(client, reg, value); |
| 638 | } |
| 639 | |
| 640 | static struct gl518_data *gl518_update_device(struct device *dev) |
| 641 | { |
| 642 | struct i2c_client *client = to_i2c_client(dev); |
| 643 | struct gl518_data *data = i2c_get_clientdata(client); |
| 644 | int val; |
| 645 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 646 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 649 | || !data->valid) { |
| 650 | dev_dbg(&client->dev, "Starting gl518 update\n"); |
| 651 | |
| 652 | data->alarms = gl518_read_value(client, GL518_REG_INT); |
| 653 | data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); |
| 654 | |
| 655 | val = gl518_read_value(client, GL518_REG_VDD_LIMIT); |
| 656 | data->voltage_min[0] = val & 0xff; |
| 657 | data->voltage_max[0] = (val >> 8) & 0xff; |
| 658 | val = gl518_read_value(client, GL518_REG_VIN1_LIMIT); |
| 659 | data->voltage_min[1] = val & 0xff; |
| 660 | data->voltage_max[1] = (val >> 8) & 0xff; |
| 661 | val = gl518_read_value(client, GL518_REG_VIN2_LIMIT); |
| 662 | data->voltage_min[2] = val & 0xff; |
| 663 | data->voltage_max[2] = (val >> 8) & 0xff; |
| 664 | val = gl518_read_value(client, GL518_REG_VIN3_LIMIT); |
| 665 | data->voltage_min[3] = val & 0xff; |
| 666 | data->voltage_max[3] = (val >> 8) & 0xff; |
| 667 | |
| 668 | val = gl518_read_value(client, GL518_REG_FAN_COUNT); |
| 669 | data->fan_in[0] = (val >> 8) & 0xff; |
| 670 | data->fan_in[1] = val & 0xff; |
| 671 | |
| 672 | val = gl518_read_value(client, GL518_REG_FAN_LIMIT); |
| 673 | data->fan_min[0] = (val >> 8) & 0xff; |
| 674 | data->fan_min[1] = val & 0xff; |
| 675 | |
| 676 | data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN); |
| 677 | data->temp_max = |
| 678 | gl518_read_value(client, GL518_REG_TEMP_MAX); |
| 679 | data->temp_hyst = |
| 680 | gl518_read_value(client, GL518_REG_TEMP_HYST); |
| 681 | |
| 682 | val = gl518_read_value(client, GL518_REG_MISC); |
| 683 | data->fan_div[0] = (val >> 6) & 0x03; |
| 684 | data->fan_div[1] = (val >> 4) & 0x03; |
| 685 | data->fan_auto1 = (val >> 3) & 0x01; |
| 686 | |
| 687 | data->alarms &= data->alarm_mask; |
| 688 | |
| 689 | val = gl518_read_value(client, GL518_REG_CONF); |
| 690 | data->beep_enable = (val >> 2) & 1; |
| 691 | |
| 692 | if (data->type != gl518sm_r00) { |
| 693 | data->voltage_in[0] = |
| 694 | gl518_read_value(client, GL518_REG_VDD); |
| 695 | data->voltage_in[1] = |
| 696 | gl518_read_value(client, GL518_REG_VIN1); |
| 697 | data->voltage_in[2] = |
| 698 | gl518_read_value(client, GL518_REG_VIN2); |
| 699 | } |
| 700 | data->voltage_in[3] = |
| 701 | gl518_read_value(client, GL518_REG_VIN3); |
| 702 | |
| 703 | data->last_updated = jiffies; |
| 704 | data->valid = 1; |
| 705 | } |
| 706 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 707 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
| 709 | return data; |
| 710 | } |
| 711 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 712 | module_i2c_driver(gl518_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " |
| 715 | "Kyosti Malkki <kmalkki@cc.hut.fi> and " |
| 716 | "Hong-Gunn Chew <hglinux@gunnet.org>"); |
| 717 | MODULE_DESCRIPTION("GL518SM driver"); |
| 718 | MODULE_LICENSE("GPL"); |