Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 1 | /* |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 2 | * w83l786ng.c - Linux kernel driver for hardware monitoring |
| 3 | * Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation - version 2. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301 USA. |
| 18 | */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 19 | |
| 20 | /* |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 21 | * Supports following chips: |
| 22 | * |
Jean Delvare | 4101ece | 2012-11-05 21:54:40 +0100 | [diff] [blame] | 23 | * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 24 | * w83l786ng 3 2 2 2 0x7b 0x5ca3 yes no |
| 25 | */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/hwmon.h> |
| 32 | #include <linux/hwmon-vid.h> |
| 33 | #include <linux/hwmon-sysfs.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/mutex.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 36 | #include <linux/jiffies.h> |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 37 | |
| 38 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 39 | static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END }; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 40 | |
| 41 | /* Insmod parameters */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 42 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 43 | static bool reset; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 44 | module_param(reset, bool, 0); |
| 45 | MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended"); |
| 46 | |
| 47 | #define W83L786NG_REG_IN_MIN(nr) (0x2C + (nr) * 2) |
| 48 | #define W83L786NG_REG_IN_MAX(nr) (0x2B + (nr) * 2) |
| 49 | #define W83L786NG_REG_IN(nr) ((nr) + 0x20) |
| 50 | |
| 51 | #define W83L786NG_REG_FAN(nr) ((nr) + 0x28) |
| 52 | #define W83L786NG_REG_FAN_MIN(nr) ((nr) + 0x3B) |
| 53 | |
| 54 | #define W83L786NG_REG_CONFIG 0x40 |
| 55 | #define W83L786NG_REG_ALARM1 0x41 |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 56 | #define W83L786NG_REG_ALARM2 0x42 |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 57 | #define W83L786NG_REG_GPIO_EN 0x47 |
| 58 | #define W83L786NG_REG_MAN_ID2 0x4C |
| 59 | #define W83L786NG_REG_MAN_ID1 0x4D |
| 60 | #define W83L786NG_REG_CHIP_ID 0x4E |
| 61 | |
| 62 | #define W83L786NG_REG_DIODE 0x53 |
| 63 | #define W83L786NG_REG_FAN_DIV 0x54 |
| 64 | #define W83L786NG_REG_FAN_CFG 0x80 |
| 65 | |
| 66 | #define W83L786NG_REG_TOLERANCE 0x8D |
| 67 | |
| 68 | static const u8 W83L786NG_REG_TEMP[2][3] = { |
| 69 | { 0x25, /* TEMP 0 in DataSheet */ |
| 70 | 0x35, /* TEMP 0 Over in DataSheet */ |
| 71 | 0x36 }, /* TEMP 0 Hyst in DataSheet */ |
| 72 | { 0x26, /* TEMP 1 in DataSheet */ |
| 73 | 0x37, /* TEMP 1 Over in DataSheet */ |
| 74 | 0x38 } /* TEMP 1 Hyst in DataSheet */ |
| 75 | }; |
| 76 | |
| 77 | static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7}; |
| 78 | static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4}; |
| 79 | |
| 80 | /* FAN Duty Cycle, be used to control */ |
| 81 | static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87}; |
| 82 | |
| 83 | |
| 84 | static inline u8 |
| 85 | FAN_TO_REG(long rpm, int div) |
| 86 | { |
| 87 | if (rpm == 0) |
| 88 | return 255; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 89 | rpm = clamp_val(rpm, 1, 1000000); |
| 90 | return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 91 | } |
| 92 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 93 | #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 94 | ((val) == 255 ? 0 : \ |
| 95 | 1350000 / ((val) * (div)))) |
| 96 | |
| 97 | /* for temp */ |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 98 | #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \ |
| 99 | : (val)) / 1000, 0, 0xff)) |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 100 | #define TEMP_FROM_REG(val) (((val) & 0x80 ? \ |
| 101 | (val) - 0x100 : (val)) * 1000) |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 102 | |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 103 | /* |
| 104 | * The analog voltage inputs have 8mV LSB. Since the sysfs output is |
| 105 | * in mV as would be measured on the chip input pin, need to just |
| 106 | * multiply/divide by 8 to translate from/to register values. |
| 107 | */ |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 108 | #define IN_TO_REG(val) (clamp_val((((val) + 4) / 8), 0, 255)) |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 109 | #define IN_FROM_REG(val) ((val) * 8) |
| 110 | |
| 111 | #define DIV_FROM_REG(val) (1 << (val)) |
| 112 | |
| 113 | static inline u8 |
| 114 | DIV_TO_REG(long val) |
| 115 | { |
| 116 | int i; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 117 | val = clamp_val(val, 1, 128) >> 1; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 118 | for (i = 0; i < 7; i++) { |
| 119 | if (val == 0) |
| 120 | break; |
| 121 | val >>= 1; |
| 122 | } |
Frans Meulenbroeks | 7fe83ad | 2012-01-05 19:50:18 +0100 | [diff] [blame] | 123 | return (u8)i; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | struct w83l786ng_data { |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 127 | struct device *hwmon_dev; |
| 128 | struct mutex update_lock; |
| 129 | char valid; /* !=0 if following fields are valid */ |
| 130 | unsigned long last_updated; /* In jiffies */ |
| 131 | unsigned long last_nonvolatile; /* In jiffies, last time we update the |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 132 | * nonvolatile registers */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 133 | |
| 134 | u8 in[3]; |
| 135 | u8 in_max[3]; |
| 136 | u8 in_min[3]; |
| 137 | u8 fan[2]; |
| 138 | u8 fan_div[2]; |
| 139 | u8 fan_min[2]; |
| 140 | u8 temp_type[2]; |
| 141 | u8 temp[2][3]; |
| 142 | u8 pwm[2]; |
| 143 | u8 pwm_mode[2]; /* 0->DC variable voltage |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 144 | * 1->PWM variable duty cycle */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 145 | |
| 146 | u8 pwm_enable[2]; /* 1->manual |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 147 | * 2->thermal cruise (also called SmartFan I) */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 148 | u8 tolerance[2]; |
| 149 | }; |
| 150 | |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 151 | static u8 |
| 152 | w83l786ng_read_value(struct i2c_client *client, u8 reg) |
| 153 | { |
| 154 | return i2c_smbus_read_byte_data(client, reg); |
| 155 | } |
| 156 | |
| 157 | static int |
| 158 | w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 159 | { |
| 160 | return i2c_smbus_write_byte_data(client, reg, value); |
| 161 | } |
| 162 | |
Axel Lin | bab711a | 2014-06-25 09:38:57 +0800 | [diff] [blame^] | 163 | static struct w83l786ng_data *w83l786ng_update_device(struct device *dev) |
| 164 | { |
| 165 | struct i2c_client *client = to_i2c_client(dev); |
| 166 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
| 167 | int i, j; |
| 168 | u8 reg_tmp, pwmcfg; |
| 169 | |
| 170 | mutex_lock(&data->update_lock); |
| 171 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 172 | || !data->valid) { |
| 173 | dev_dbg(&client->dev, "Updating w83l786ng data.\n"); |
| 174 | |
| 175 | /* Update the voltages measured value and limits */ |
| 176 | for (i = 0; i < 3; i++) { |
| 177 | data->in[i] = w83l786ng_read_value(client, |
| 178 | W83L786NG_REG_IN(i)); |
| 179 | data->in_min[i] = w83l786ng_read_value(client, |
| 180 | W83L786NG_REG_IN_MIN(i)); |
| 181 | data->in_max[i] = w83l786ng_read_value(client, |
| 182 | W83L786NG_REG_IN_MAX(i)); |
| 183 | } |
| 184 | |
| 185 | /* Update the fan counts and limits */ |
| 186 | for (i = 0; i < 2; i++) { |
| 187 | data->fan[i] = w83l786ng_read_value(client, |
| 188 | W83L786NG_REG_FAN(i)); |
| 189 | data->fan_min[i] = w83l786ng_read_value(client, |
| 190 | W83L786NG_REG_FAN_MIN(i)); |
| 191 | } |
| 192 | |
| 193 | /* Update the fan divisor */ |
| 194 | reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV); |
| 195 | data->fan_div[0] = reg_tmp & 0x07; |
| 196 | data->fan_div[1] = (reg_tmp >> 4) & 0x07; |
| 197 | |
| 198 | pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG); |
| 199 | for (i = 0; i < 2; i++) { |
| 200 | data->pwm_mode[i] = |
| 201 | ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1) |
| 202 | ? 0 : 1; |
| 203 | data->pwm_enable[i] = |
| 204 | ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1; |
| 205 | data->pwm[i] = |
| 206 | (w83l786ng_read_value(client, W83L786NG_REG_PWM[i]) |
| 207 | & 0x0f) * 0x11; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* Update the temperature sensors */ |
| 212 | for (i = 0; i < 2; i++) { |
| 213 | for (j = 0; j < 3; j++) { |
| 214 | data->temp[i][j] = w83l786ng_read_value(client, |
| 215 | W83L786NG_REG_TEMP[i][j]); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* Update Smart Fan I/II tolerance */ |
| 220 | reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE); |
| 221 | data->tolerance[0] = reg_tmp & 0x0f; |
| 222 | data->tolerance[1] = (reg_tmp >> 4) & 0x0f; |
| 223 | |
| 224 | data->last_updated = jiffies; |
| 225 | data->valid = 1; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | mutex_unlock(&data->update_lock); |
| 230 | |
| 231 | return data; |
| 232 | } |
| 233 | |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 234 | /* following are the sysfs callback functions */ |
| 235 | #define show_in_reg(reg) \ |
| 236 | static ssize_t \ |
| 237 | show_##reg(struct device *dev, struct device_attribute *attr, \ |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 238 | char *buf) \ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 239 | { \ |
| 240 | int nr = to_sensor_dev_attr(attr)->index; \ |
| 241 | struct w83l786ng_data *data = w83l786ng_update_device(dev); \ |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 242 | return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | show_in_reg(in) |
| 246 | show_in_reg(in_min) |
| 247 | show_in_reg(in_max) |
| 248 | |
| 249 | #define store_in_reg(REG, reg) \ |
| 250 | static ssize_t \ |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 251 | store_in_##reg(struct device *dev, struct device_attribute *attr, \ |
| 252 | const char *buf, size_t count) \ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 253 | { \ |
| 254 | int nr = to_sensor_dev_attr(attr)->index; \ |
| 255 | struct i2c_client *client = to_i2c_client(dev); \ |
| 256 | struct w83l786ng_data *data = i2c_get_clientdata(client); \ |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 257 | unsigned long val; \ |
| 258 | int err = kstrtoul(buf, 10, &val); \ |
| 259 | if (err) \ |
| 260 | return err; \ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 261 | mutex_lock(&data->update_lock); \ |
| 262 | data->in_##reg[nr] = IN_TO_REG(val); \ |
| 263 | w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \ |
| 264 | data->in_##reg[nr]); \ |
| 265 | mutex_unlock(&data->update_lock); \ |
| 266 | return count; \ |
| 267 | } |
| 268 | |
| 269 | store_in_reg(MIN, min) |
| 270 | store_in_reg(MAX, max) |
| 271 | |
| 272 | static struct sensor_device_attribute sda_in_input[] = { |
| 273 | SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0), |
| 274 | SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1), |
| 275 | SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2), |
| 276 | }; |
| 277 | |
| 278 | static struct sensor_device_attribute sda_in_min[] = { |
| 279 | SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0), |
| 280 | SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1), |
| 281 | SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2), |
| 282 | }; |
| 283 | |
| 284 | static struct sensor_device_attribute sda_in_max[] = { |
| 285 | SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0), |
| 286 | SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1), |
| 287 | SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2), |
| 288 | }; |
| 289 | |
| 290 | #define show_fan_reg(reg) \ |
| 291 | static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ |
| 292 | char *buf) \ |
| 293 | { \ |
| 294 | int nr = to_sensor_dev_attr(attr)->index; \ |
| 295 | struct w83l786ng_data *data = w83l786ng_update_device(dev); \ |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 296 | return sprintf(buf, "%d\n", \ |
Axel Lin | a8d4d82 | 2014-06-25 09:02:32 +0800 | [diff] [blame] | 297 | FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | show_fan_reg(fan); |
| 301 | show_fan_reg(fan_min); |
| 302 | |
| 303 | static ssize_t |
| 304 | store_fan_min(struct device *dev, struct device_attribute *attr, |
| 305 | const char *buf, size_t count) |
| 306 | { |
| 307 | int nr = to_sensor_dev_attr(attr)->index; |
| 308 | struct i2c_client *client = to_i2c_client(dev); |
| 309 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 310 | unsigned long val; |
| 311 | int err; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 312 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 313 | err = kstrtoul(buf, 10, &val); |
| 314 | if (err) |
| 315 | return err; |
| 316 | |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 317 | mutex_lock(&data->update_lock); |
| 318 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
| 319 | w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr), |
| 320 | data->fan_min[nr]); |
| 321 | mutex_unlock(&data->update_lock); |
| 322 | |
| 323 | return count; |
| 324 | } |
| 325 | |
| 326 | static ssize_t |
| 327 | show_fan_div(struct device *dev, struct device_attribute *attr, |
| 328 | char *buf) |
| 329 | { |
| 330 | int nr = to_sensor_dev_attr(attr)->index; |
| 331 | struct w83l786ng_data *data = w83l786ng_update_device(dev); |
| 332 | return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr])); |
| 333 | } |
| 334 | |
Guenter Roeck | 01d9def | 2012-01-19 11:02:28 -0800 | [diff] [blame] | 335 | /* |
| 336 | * Note: we save and restore the fan minimum here, because its value is |
| 337 | * determined in part by the fan divisor. This follows the principle of |
| 338 | * least surprise; the user doesn't expect the fan minimum to change just |
| 339 | * because the divisor changed. |
| 340 | */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 341 | static ssize_t |
| 342 | store_fan_div(struct device *dev, struct device_attribute *attr, |
| 343 | const char *buf, size_t count) |
| 344 | { |
| 345 | int nr = to_sensor_dev_attr(attr)->index; |
| 346 | struct i2c_client *client = to_i2c_client(dev); |
| 347 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
| 348 | |
| 349 | unsigned long min; |
| 350 | u8 tmp_fan_div; |
| 351 | u8 fan_div_reg; |
| 352 | u8 keep_mask = 0; |
| 353 | u8 new_shift = 0; |
| 354 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 355 | unsigned long val; |
| 356 | int err; |
| 357 | |
| 358 | err = kstrtoul(buf, 10, &val); |
| 359 | if (err) |
| 360 | return err; |
| 361 | |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 362 | /* Save fan_min */ |
| 363 | mutex_lock(&data->update_lock); |
| 364 | min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])); |
| 365 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 366 | data->fan_div[nr] = DIV_TO_REG(val); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 367 | |
| 368 | switch (nr) { |
| 369 | case 0: |
| 370 | keep_mask = 0xf8; |
| 371 | new_shift = 0; |
| 372 | break; |
| 373 | case 1: |
| 374 | keep_mask = 0x8f; |
| 375 | new_shift = 4; |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV) |
| 380 | & keep_mask; |
| 381 | |
| 382 | tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask; |
| 383 | |
| 384 | w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV, |
| 385 | fan_div_reg | tmp_fan_div); |
| 386 | |
| 387 | /* Restore fan_min */ |
| 388 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
| 389 | w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr), |
| 390 | data->fan_min[nr]); |
| 391 | mutex_unlock(&data->update_lock); |
| 392 | |
| 393 | return count; |
| 394 | } |
| 395 | |
| 396 | static struct sensor_device_attribute sda_fan_input[] = { |
| 397 | SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0), |
| 398 | SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1), |
| 399 | }; |
| 400 | |
| 401 | static struct sensor_device_attribute sda_fan_min[] = { |
| 402 | SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, |
| 403 | store_fan_min, 0), |
| 404 | SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, |
| 405 | store_fan_min, 1), |
| 406 | }; |
| 407 | |
| 408 | static struct sensor_device_attribute sda_fan_div[] = { |
| 409 | SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div, |
| 410 | store_fan_div, 0), |
| 411 | SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div, |
| 412 | store_fan_div, 1), |
| 413 | }; |
| 414 | |
| 415 | |
| 416 | /* read/write the temperature, includes measured value and limits */ |
| 417 | |
| 418 | static ssize_t |
| 419 | show_temp(struct device *dev, struct device_attribute *attr, char *buf) |
| 420 | { |
| 421 | struct sensor_device_attribute_2 *sensor_attr = |
| 422 | to_sensor_dev_attr_2(attr); |
| 423 | int nr = sensor_attr->nr; |
| 424 | int index = sensor_attr->index; |
| 425 | struct w83l786ng_data *data = w83l786ng_update_device(dev); |
| 426 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index])); |
| 427 | } |
| 428 | |
| 429 | static ssize_t |
| 430 | store_temp(struct device *dev, struct device_attribute *attr, |
| 431 | const char *buf, size_t count) |
| 432 | { |
| 433 | struct sensor_device_attribute_2 *sensor_attr = |
| 434 | to_sensor_dev_attr_2(attr); |
| 435 | int nr = sensor_attr->nr; |
| 436 | int index = sensor_attr->index; |
| 437 | struct i2c_client *client = to_i2c_client(dev); |
| 438 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 439 | long val; |
| 440 | int err; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 441 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 442 | err = kstrtol(buf, 10, &val); |
| 443 | if (err) |
| 444 | return err; |
| 445 | |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 446 | mutex_lock(&data->update_lock); |
| 447 | data->temp[nr][index] = TEMP_TO_REG(val); |
| 448 | w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index], |
| 449 | data->temp[nr][index]); |
| 450 | mutex_unlock(&data->update_lock); |
| 451 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 452 | return count; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static struct sensor_device_attribute_2 sda_temp_input[] = { |
| 456 | SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0), |
| 457 | SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0), |
| 458 | }; |
| 459 | |
| 460 | static struct sensor_device_attribute_2 sda_temp_max[] = { |
| 461 | SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, |
| 462 | show_temp, store_temp, 0, 1), |
| 463 | SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, |
| 464 | show_temp, store_temp, 1, 1), |
| 465 | }; |
| 466 | |
| 467 | static struct sensor_device_attribute_2 sda_temp_max_hyst[] = { |
| 468 | SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR, |
| 469 | show_temp, store_temp, 0, 2), |
| 470 | SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR, |
| 471 | show_temp, store_temp, 1, 2), |
| 472 | }; |
| 473 | |
| 474 | #define show_pwm_reg(reg) \ |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 475 | static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ |
| 476 | char *buf) \ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 477 | { \ |
| 478 | struct w83l786ng_data *data = w83l786ng_update_device(dev); \ |
| 479 | int nr = to_sensor_dev_attr(attr)->index; \ |
| 480 | return sprintf(buf, "%d\n", data->reg[nr]); \ |
| 481 | } |
| 482 | |
| 483 | show_pwm_reg(pwm_mode) |
| 484 | show_pwm_reg(pwm_enable) |
| 485 | show_pwm_reg(pwm) |
| 486 | |
| 487 | static ssize_t |
| 488 | store_pwm_mode(struct device *dev, struct device_attribute *attr, |
| 489 | const char *buf, size_t count) |
| 490 | { |
| 491 | int nr = to_sensor_dev_attr(attr)->index; |
| 492 | struct i2c_client *client = to_i2c_client(dev); |
| 493 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 494 | u8 reg; |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 495 | unsigned long val; |
| 496 | int err; |
| 497 | |
| 498 | err = kstrtoul(buf, 10, &val); |
| 499 | if (err) |
| 500 | return err; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 501 | |
| 502 | if (val > 1) |
| 503 | return -EINVAL; |
| 504 | mutex_lock(&data->update_lock); |
| 505 | data->pwm_mode[nr] = val; |
| 506 | reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG); |
| 507 | reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]); |
| 508 | if (!val) |
| 509 | reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr]; |
| 510 | w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg); |
| 511 | mutex_unlock(&data->update_lock); |
| 512 | return count; |
| 513 | } |
| 514 | |
| 515 | static ssize_t |
| 516 | store_pwm(struct device *dev, struct device_attribute *attr, |
| 517 | const char *buf, size_t count) |
| 518 | { |
| 519 | int nr = to_sensor_dev_attr(attr)->index; |
| 520 | struct i2c_client *client = to_i2c_client(dev); |
| 521 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 522 | unsigned long val; |
| 523 | int err; |
| 524 | |
| 525 | err = kstrtoul(buf, 10, &val); |
| 526 | if (err) |
| 527 | return err; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 528 | val = clamp_val(val, 0, 255); |
Jean Delvare | 33a7ab9 | 2013-12-12 08:05:32 +0100 | [diff] [blame] | 529 | val = DIV_ROUND_CLOSEST(val, 0x11); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 530 | |
| 531 | mutex_lock(&data->update_lock); |
Jean Delvare | 33a7ab9 | 2013-12-12 08:05:32 +0100 | [diff] [blame] | 532 | data->pwm[nr] = val * 0x11; |
| 533 | val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 534 | w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val); |
| 535 | mutex_unlock(&data->update_lock); |
| 536 | return count; |
| 537 | } |
| 538 | |
| 539 | static ssize_t |
| 540 | store_pwm_enable(struct device *dev, struct device_attribute *attr, |
| 541 | const char *buf, size_t count) |
| 542 | { |
| 543 | int nr = to_sensor_dev_attr(attr)->index; |
| 544 | struct i2c_client *client = to_i2c_client(dev); |
| 545 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 546 | u8 reg; |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 547 | unsigned long val; |
| 548 | int err; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 549 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 550 | err = kstrtoul(buf, 10, &val); |
| 551 | if (err) |
| 552 | return err; |
| 553 | |
| 554 | if (!val || val > 2) /* only modes 1 and 2 are supported */ |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 555 | return -EINVAL; |
| 556 | |
| 557 | mutex_lock(&data->update_lock); |
| 558 | reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG); |
| 559 | data->pwm_enable[nr] = val; |
Brian Carnes | cf7559b | 2013-12-12 08:05:32 +0100 | [diff] [blame] | 560 | reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 561 | reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr]; |
| 562 | w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg); |
| 563 | mutex_unlock(&data->update_lock); |
| 564 | return count; |
| 565 | } |
| 566 | |
| 567 | static struct sensor_device_attribute sda_pwm[] = { |
| 568 | SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0), |
| 569 | SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1), |
| 570 | }; |
| 571 | |
| 572 | static struct sensor_device_attribute sda_pwm_mode[] = { |
| 573 | SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode, |
| 574 | store_pwm_mode, 0), |
| 575 | SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode, |
| 576 | store_pwm_mode, 1), |
| 577 | }; |
| 578 | |
| 579 | static struct sensor_device_attribute sda_pwm_enable[] = { |
| 580 | SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable, |
| 581 | store_pwm_enable, 0), |
| 582 | SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable, |
| 583 | store_pwm_enable, 1), |
| 584 | }; |
| 585 | |
| 586 | /* For Smart Fan I/Thermal Cruise and Smart Fan II */ |
| 587 | static ssize_t |
| 588 | show_tolerance(struct device *dev, struct device_attribute *attr, char *buf) |
| 589 | { |
| 590 | int nr = to_sensor_dev_attr(attr)->index; |
| 591 | struct w83l786ng_data *data = w83l786ng_update_device(dev); |
| 592 | return sprintf(buf, "%ld\n", (long)data->tolerance[nr]); |
| 593 | } |
| 594 | |
| 595 | static ssize_t |
| 596 | store_tolerance(struct device *dev, struct device_attribute *attr, |
| 597 | const char *buf, size_t count) |
| 598 | { |
| 599 | int nr = to_sensor_dev_attr(attr)->index; |
| 600 | struct i2c_client *client = to_i2c_client(dev); |
| 601 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 602 | u8 tol_tmp, tol_mask; |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 603 | unsigned long val; |
| 604 | int err; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 605 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 606 | err = kstrtoul(buf, 10, &val); |
| 607 | if (err) |
| 608 | return err; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 609 | |
| 610 | mutex_lock(&data->update_lock); |
| 611 | tol_mask = w83l786ng_read_value(client, |
| 612 | W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 613 | tol_tmp = clamp_val(val, 0, 15); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 614 | tol_tmp &= 0x0f; |
| 615 | data->tolerance[nr] = tol_tmp; |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 616 | if (nr == 1) |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 617 | tol_tmp <<= 4; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 618 | |
| 619 | w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE, |
| 620 | tol_mask | tol_tmp); |
| 621 | mutex_unlock(&data->update_lock); |
| 622 | return count; |
| 623 | } |
| 624 | |
| 625 | static struct sensor_device_attribute sda_tolerance[] = { |
| 626 | SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, |
| 627 | show_tolerance, store_tolerance, 0), |
| 628 | SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, |
| 629 | show_tolerance, store_tolerance, 1), |
| 630 | }; |
| 631 | |
| 632 | |
| 633 | #define IN_UNIT_ATTRS(X) \ |
| 634 | &sda_in_input[X].dev_attr.attr, \ |
| 635 | &sda_in_min[X].dev_attr.attr, \ |
| 636 | &sda_in_max[X].dev_attr.attr |
| 637 | |
| 638 | #define FAN_UNIT_ATTRS(X) \ |
| 639 | &sda_fan_input[X].dev_attr.attr, \ |
| 640 | &sda_fan_min[X].dev_attr.attr, \ |
| 641 | &sda_fan_div[X].dev_attr.attr |
| 642 | |
| 643 | #define TEMP_UNIT_ATTRS(X) \ |
| 644 | &sda_temp_input[X].dev_attr.attr, \ |
| 645 | &sda_temp_max[X].dev_attr.attr, \ |
| 646 | &sda_temp_max_hyst[X].dev_attr.attr |
| 647 | |
| 648 | #define PWM_UNIT_ATTRS(X) \ |
| 649 | &sda_pwm[X].dev_attr.attr, \ |
| 650 | &sda_pwm_mode[X].dev_attr.attr, \ |
| 651 | &sda_pwm_enable[X].dev_attr.attr |
| 652 | |
| 653 | #define TOLERANCE_UNIT_ATTRS(X) \ |
| 654 | &sda_tolerance[X].dev_attr.attr |
| 655 | |
| 656 | static struct attribute *w83l786ng_attributes[] = { |
| 657 | IN_UNIT_ATTRS(0), |
| 658 | IN_UNIT_ATTRS(1), |
| 659 | IN_UNIT_ATTRS(2), |
| 660 | FAN_UNIT_ATTRS(0), |
| 661 | FAN_UNIT_ATTRS(1), |
| 662 | TEMP_UNIT_ATTRS(0), |
| 663 | TEMP_UNIT_ATTRS(1), |
| 664 | PWM_UNIT_ATTRS(0), |
| 665 | PWM_UNIT_ATTRS(1), |
| 666 | TOLERANCE_UNIT_ATTRS(0), |
| 667 | TOLERANCE_UNIT_ATTRS(1), |
| 668 | NULL |
| 669 | }; |
| 670 | |
| 671 | static const struct attribute_group w83l786ng_group = { |
| 672 | .attrs = w83l786ng_attributes, |
| 673 | }; |
| 674 | |
| 675 | static int |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 676 | w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info) |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 677 | { |
Jean Delvare | 33468e7 | 2008-07-16 19:30:18 +0200 | [diff] [blame] | 678 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 679 | u16 man_id; |
| 680 | u8 chip_id; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 681 | |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 682 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 33468e7 | 2008-07-16 19:30:18 +0200 | [diff] [blame] | 683 | return -ENODEV; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 684 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 685 | /* Detection */ |
| 686 | if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) { |
| 687 | dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n", |
| 688 | client->addr); |
| 689 | return -ENODEV; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 690 | } |
| 691 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 692 | /* Identification */ |
| 693 | man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) + |
| 694 | w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2); |
| 695 | chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 696 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 697 | if (man_id != 0x5CA3 || /* Winbond */ |
| 698 | chip_id != 0x80) { /* W83L786NG */ |
| 699 | dev_dbg(&adapter->dev, |
| 700 | "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n", |
| 701 | man_id, chip_id); |
| 702 | return -ENODEV; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 703 | } |
| 704 | |
Jean Delvare | 33468e7 | 2008-07-16 19:30:18 +0200 | [diff] [blame] | 705 | strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 706 | |
Jean Delvare | 33468e7 | 2008-07-16 19:30:18 +0200 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
Axel Lin | bab711a | 2014-06-25 09:38:57 +0800 | [diff] [blame^] | 710 | static void w83l786ng_init_client(struct i2c_client *client) |
| 711 | { |
| 712 | u8 tmp; |
| 713 | |
| 714 | if (reset) |
| 715 | w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80); |
| 716 | |
| 717 | /* Start monitoring */ |
| 718 | tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG); |
| 719 | if (!(tmp & 0x01)) |
| 720 | w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01); |
| 721 | } |
| 722 | |
Jean Delvare | 33468e7 | 2008-07-16 19:30:18 +0200 | [diff] [blame] | 723 | static int |
| 724 | w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 725 | { |
| 726 | struct device *dev = &client->dev; |
| 727 | struct w83l786ng_data *data; |
| 728 | int i, err = 0; |
| 729 | u8 reg_tmp; |
| 730 | |
Guenter Roeck | 816864d | 2012-06-02 11:48:02 -0700 | [diff] [blame] | 731 | data = devm_kzalloc(&client->dev, sizeof(struct w83l786ng_data), |
| 732 | GFP_KERNEL); |
| 733 | if (!data) |
| 734 | return -ENOMEM; |
Jean Delvare | 33468e7 | 2008-07-16 19:30:18 +0200 | [diff] [blame] | 735 | |
| 736 | i2c_set_clientdata(client, data); |
| 737 | mutex_init(&data->update_lock); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 738 | |
| 739 | /* Initialize the chip */ |
| 740 | w83l786ng_init_client(client); |
| 741 | |
| 742 | /* A few vars need to be filled upon startup */ |
| 743 | for (i = 0; i < 2; i++) { |
| 744 | data->fan_min[i] = w83l786ng_read_value(client, |
| 745 | W83L786NG_REG_FAN_MIN(i)); |
| 746 | } |
| 747 | |
| 748 | /* Update the fan divisor */ |
| 749 | reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV); |
| 750 | data->fan_div[0] = reg_tmp & 0x07; |
| 751 | data->fan_div[1] = (reg_tmp >> 4) & 0x07; |
| 752 | |
| 753 | /* Register sysfs hooks */ |
Guenter Roeck | ca3ccad | 2012-01-15 11:03:08 -0800 | [diff] [blame] | 754 | err = sysfs_create_group(&client->dev.kobj, &w83l786ng_group); |
| 755 | if (err) |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 756 | goto exit_remove; |
| 757 | |
| 758 | data->hwmon_dev = hwmon_device_register(dev); |
| 759 | if (IS_ERR(data->hwmon_dev)) { |
| 760 | err = PTR_ERR(data->hwmon_dev); |
| 761 | goto exit_remove; |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | |
| 766 | /* Unregister sysfs hooks */ |
| 767 | |
| 768 | exit_remove: |
| 769 | sysfs_remove_group(&client->dev.kobj, &w83l786ng_group); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 770 | return err; |
| 771 | } |
| 772 | |
| 773 | static int |
Jean Delvare | 33468e7 | 2008-07-16 19:30:18 +0200 | [diff] [blame] | 774 | w83l786ng_remove(struct i2c_client *client) |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 775 | { |
| 776 | struct w83l786ng_data *data = i2c_get_clientdata(client); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 777 | |
| 778 | hwmon_device_unregister(data->hwmon_dev); |
| 779 | sysfs_remove_group(&client->dev.kobj, &w83l786ng_group); |
| 780 | |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Axel Lin | bab711a | 2014-06-25 09:38:57 +0800 | [diff] [blame^] | 784 | static const struct i2c_device_id w83l786ng_id[] = { |
| 785 | { "w83l786ng", 0 }, |
| 786 | { } |
| 787 | }; |
| 788 | MODULE_DEVICE_TABLE(i2c, w83l786ng_id); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 789 | |
Axel Lin | bab711a | 2014-06-25 09:38:57 +0800 | [diff] [blame^] | 790 | static struct i2c_driver w83l786ng_driver = { |
| 791 | .class = I2C_CLASS_HWMON, |
| 792 | .driver = { |
| 793 | .name = "w83l786ng", |
| 794 | }, |
| 795 | .probe = w83l786ng_probe, |
| 796 | .remove = w83l786ng_remove, |
| 797 | .id_table = w83l786ng_id, |
| 798 | .detect = w83l786ng_detect, |
| 799 | .address_list = normal_i2c, |
| 800 | }; |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 801 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 802 | module_i2c_driver(w83l786ng_driver); |
Kevin Lo | 85f03bc | 2007-11-23 09:31:52 +0800 | [diff] [blame] | 803 | |
| 804 | MODULE_AUTHOR("Kevin Lo"); |
| 805 | MODULE_DESCRIPTION("w83l786ng driver"); |
| 806 | MODULE_LICENSE("GPL"); |