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