blob: 32487c19cbfc5c2ed5dccea9dcfd7256727786bf [file] [log] [blame]
Kevin Lo85f03bc2007-11-23 09:31:52 +08001/*
Guenter Roeck01d9def2012-01-19 11:02:28 -08002 * 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 Lo85f03bc2007-11-23 09:31:52 +080019
20/*
Guenter Roeck01d9def2012-01-19 11:02:28 -080021 * Supports following chips:
22 *
Jean Delvare4101ece2012-11-05 21:54:40 +010023 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeck01d9def2012-01-19 11:02:28 -080024 * w83l786ng 3 2 2 2 0x7b 0x5ca3 yes no
25 */
Kevin Lo85f03bc2007-11-23 09:31:52 +080026
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 Delvaredcd8f392012-10-10 15:25:56 +020036#include <linux/jiffies.h>
Kevin Lo85f03bc2007-11-23 09:31:52 +080037
38/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050039static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END };
Kevin Lo85f03bc2007-11-23 09:31:52 +080040
41/* Insmod parameters */
Kevin Lo85f03bc2007-11-23 09:31:52 +080042
Rusty Russell90ab5ee2012-01-13 09:32:20 +103043static bool reset;
Kevin Lo85f03bc2007-11-23 09:31:52 +080044module_param(reset, bool, 0);
45MODULE_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 Roeckca3ccad2012-01-15 11:03:08 -080056#define W83L786NG_REG_ALARM2 0x42
Kevin Lo85f03bc2007-11-23 09:31:52 +080057#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
68static 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
77static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7};
78static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4};
79
80/* FAN Duty Cycle, be used to control */
81static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87};
82
83
84static inline u8
85FAN_TO_REG(long rpm, int div)
86{
87 if (rpm == 0)
88 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -080089 rpm = clamp_val(rpm, 1, 1000000);
90 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Kevin Lo85f03bc2007-11-23 09:31:52 +080091}
92
Guenter Roeckca3ccad2012-01-15 11:03:08 -080093#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
Kevin Lo85f03bc2007-11-23 09:31:52 +080094 ((val) == 255 ? 0 : \
95 1350000 / ((val) * (div))))
96
97/* for temp */
Guenter Roeck2a844c12013-01-09 08:09:34 -080098#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \
99 : (val)) / 1000, 0, 0xff))
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800100#define TEMP_FROM_REG(val) (((val) & 0x80 ? \
101 (val) - 0x100 : (val)) * 1000)
Kevin Lo85f03bc2007-11-23 09:31:52 +0800102
Guenter Roeck01d9def2012-01-19 11:02:28 -0800103/*
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 Roeck2a844c12013-01-09 08:09:34 -0800108#define IN_TO_REG(val) (clamp_val((((val) + 4) / 8), 0, 255))
Kevin Lo85f03bc2007-11-23 09:31:52 +0800109#define IN_FROM_REG(val) ((val) * 8)
110
111#define DIV_FROM_REG(val) (1 << (val))
112
113static inline u8
114DIV_TO_REG(long val)
115{
116 int i;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800117 val = clamp_val(val, 1, 128) >> 1;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800118 for (i = 0; i < 7; i++) {
119 if (val == 0)
120 break;
121 val >>= 1;
122 }
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100123 return (u8)i;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800124}
125
126struct w83l786ng_data {
Kevin Lo85f03bc2007-11-23 09:31:52 +0800127 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 Roeck01d9def2012-01-19 11:02:28 -0800132 * nonvolatile registers */
Kevin Lo85f03bc2007-11-23 09:31:52 +0800133
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 Roeck01d9def2012-01-19 11:02:28 -0800144 * 1->PWM variable duty cycle */
Kevin Lo85f03bc2007-11-23 09:31:52 +0800145
146 u8 pwm_enable[2]; /* 1->manual
Guenter Roeck01d9def2012-01-19 11:02:28 -0800147 * 2->thermal cruise (also called SmartFan I) */
Kevin Lo85f03bc2007-11-23 09:31:52 +0800148 u8 tolerance[2];
149};
150
Jean Delvare33468e72008-07-16 19:30:18 +0200151static int w83l786ng_probe(struct i2c_client *client,
152 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100153static int w83l786ng_detect(struct i2c_client *client,
Jean Delvare33468e72008-07-16 19:30:18 +0200154 struct i2c_board_info *info);
155static int w83l786ng_remove(struct i2c_client *client);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800156static void w83l786ng_init_client(struct i2c_client *client);
157static struct w83l786ng_data *w83l786ng_update_device(struct device *dev);
158
Jean Delvare33468e72008-07-16 19:30:18 +0200159static const struct i2c_device_id w83l786ng_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100160 { "w83l786ng", 0 },
Jean Delvare33468e72008-07-16 19:30:18 +0200161 { }
162};
163MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
164
Kevin Lo85f03bc2007-11-23 09:31:52 +0800165static struct i2c_driver w83l786ng_driver = {
Jean Delvare33468e72008-07-16 19:30:18 +0200166 .class = I2C_CLASS_HWMON,
Kevin Lo85f03bc2007-11-23 09:31:52 +0800167 .driver = {
168 .name = "w83l786ng",
169 },
Jean Delvare33468e72008-07-16 19:30:18 +0200170 .probe = w83l786ng_probe,
171 .remove = w83l786ng_remove,
172 .id_table = w83l786ng_id,
173 .detect = w83l786ng_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100174 .address_list = normal_i2c,
Kevin Lo85f03bc2007-11-23 09:31:52 +0800175};
176
177static u8
178w83l786ng_read_value(struct i2c_client *client, u8 reg)
179{
180 return i2c_smbus_read_byte_data(client, reg);
181}
182
183static int
184w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
185{
186 return i2c_smbus_write_byte_data(client, reg, value);
187}
188
189/* following are the sysfs callback functions */
190#define show_in_reg(reg) \
191static ssize_t \
192show_##reg(struct device *dev, struct device_attribute *attr, \
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800193 char *buf) \
Kevin Lo85f03bc2007-11-23 09:31:52 +0800194{ \
195 int nr = to_sensor_dev_attr(attr)->index; \
196 struct w83l786ng_data *data = w83l786ng_update_device(dev); \
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800197 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
Kevin Lo85f03bc2007-11-23 09:31:52 +0800198}
199
200show_in_reg(in)
201show_in_reg(in_min)
202show_in_reg(in_max)
203
204#define store_in_reg(REG, reg) \
205static ssize_t \
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800206store_in_##reg(struct device *dev, struct device_attribute *attr, \
207 const char *buf, size_t count) \
Kevin Lo85f03bc2007-11-23 09:31:52 +0800208{ \
209 int nr = to_sensor_dev_attr(attr)->index; \
210 struct i2c_client *client = to_i2c_client(dev); \
211 struct w83l786ng_data *data = i2c_get_clientdata(client); \
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800212 unsigned long val; \
213 int err = kstrtoul(buf, 10, &val); \
214 if (err) \
215 return err; \
Kevin Lo85f03bc2007-11-23 09:31:52 +0800216 mutex_lock(&data->update_lock); \
217 data->in_##reg[nr] = IN_TO_REG(val); \
218 w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
219 data->in_##reg[nr]); \
220 mutex_unlock(&data->update_lock); \
221 return count; \
222}
223
224store_in_reg(MIN, min)
225store_in_reg(MAX, max)
226
227static struct sensor_device_attribute sda_in_input[] = {
228 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
229 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
230 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
231};
232
233static struct sensor_device_attribute sda_in_min[] = {
234 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
235 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
236 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
237};
238
239static struct sensor_device_attribute sda_in_max[] = {
240 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
241 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
242 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
243};
244
245#define show_fan_reg(reg) \
246static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
247 char *buf) \
248{ \
249 int nr = to_sensor_dev_attr(attr)->index; \
250 struct w83l786ng_data *data = w83l786ng_update_device(dev); \
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800251 return sprintf(buf, "%d\n", \
Axel Lina8d4d822014-06-25 09:02:32 +0800252 FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
Kevin Lo85f03bc2007-11-23 09:31:52 +0800253}
254
255show_fan_reg(fan);
256show_fan_reg(fan_min);
257
258static ssize_t
259store_fan_min(struct device *dev, struct device_attribute *attr,
260 const char *buf, size_t count)
261{
262 int nr = to_sensor_dev_attr(attr)->index;
263 struct i2c_client *client = to_i2c_client(dev);
264 struct w83l786ng_data *data = i2c_get_clientdata(client);
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800265 unsigned long val;
266 int err;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800267
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800268 err = kstrtoul(buf, 10, &val);
269 if (err)
270 return err;
271
Kevin Lo85f03bc2007-11-23 09:31:52 +0800272 mutex_lock(&data->update_lock);
273 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
274 w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
275 data->fan_min[nr]);
276 mutex_unlock(&data->update_lock);
277
278 return count;
279}
280
281static ssize_t
282show_fan_div(struct device *dev, struct device_attribute *attr,
283 char *buf)
284{
285 int nr = to_sensor_dev_attr(attr)->index;
286 struct w83l786ng_data *data = w83l786ng_update_device(dev);
287 return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
288}
289
Guenter Roeck01d9def2012-01-19 11:02:28 -0800290/*
291 * Note: we save and restore the fan minimum here, because its value is
292 * determined in part by the fan divisor. This follows the principle of
293 * least surprise; the user doesn't expect the fan minimum to change just
294 * because the divisor changed.
295 */
Kevin Lo85f03bc2007-11-23 09:31:52 +0800296static ssize_t
297store_fan_div(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count)
299{
300 int nr = to_sensor_dev_attr(attr)->index;
301 struct i2c_client *client = to_i2c_client(dev);
302 struct w83l786ng_data *data = i2c_get_clientdata(client);
303
304 unsigned long min;
305 u8 tmp_fan_div;
306 u8 fan_div_reg;
307 u8 keep_mask = 0;
308 u8 new_shift = 0;
309
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800310 unsigned long val;
311 int err;
312
313 err = kstrtoul(buf, 10, &val);
314 if (err)
315 return err;
316
Kevin Lo85f03bc2007-11-23 09:31:52 +0800317 /* Save fan_min */
318 mutex_lock(&data->update_lock);
319 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
320
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800321 data->fan_div[nr] = DIV_TO_REG(val);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800322
323 switch (nr) {
324 case 0:
325 keep_mask = 0xf8;
326 new_shift = 0;
327 break;
328 case 1:
329 keep_mask = 0x8f;
330 new_shift = 4;
331 break;
332 }
333
334 fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
335 & keep_mask;
336
337 tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
338
339 w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV,
340 fan_div_reg | tmp_fan_div);
341
342 /* Restore fan_min */
343 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
344 w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
345 data->fan_min[nr]);
346 mutex_unlock(&data->update_lock);
347
348 return count;
349}
350
351static struct sensor_device_attribute sda_fan_input[] = {
352 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
353 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
354};
355
356static struct sensor_device_attribute sda_fan_min[] = {
357 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
358 store_fan_min, 0),
359 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
360 store_fan_min, 1),
361};
362
363static struct sensor_device_attribute sda_fan_div[] = {
364 SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
365 store_fan_div, 0),
366 SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
367 store_fan_div, 1),
368};
369
370
371/* read/write the temperature, includes measured value and limits */
372
373static ssize_t
374show_temp(struct device *dev, struct device_attribute *attr, char *buf)
375{
376 struct sensor_device_attribute_2 *sensor_attr =
377 to_sensor_dev_attr_2(attr);
378 int nr = sensor_attr->nr;
379 int index = sensor_attr->index;
380 struct w83l786ng_data *data = w83l786ng_update_device(dev);
381 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
382}
383
384static ssize_t
385store_temp(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
387{
388 struct sensor_device_attribute_2 *sensor_attr =
389 to_sensor_dev_attr_2(attr);
390 int nr = sensor_attr->nr;
391 int index = sensor_attr->index;
392 struct i2c_client *client = to_i2c_client(dev);
393 struct w83l786ng_data *data = i2c_get_clientdata(client);
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800394 long val;
395 int err;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800396
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800397 err = kstrtol(buf, 10, &val);
398 if (err)
399 return err;
400
Kevin Lo85f03bc2007-11-23 09:31:52 +0800401 mutex_lock(&data->update_lock);
402 data->temp[nr][index] = TEMP_TO_REG(val);
403 w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index],
404 data->temp[nr][index]);
405 mutex_unlock(&data->update_lock);
406
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800407 return count;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800408}
409
410static struct sensor_device_attribute_2 sda_temp_input[] = {
411 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
412 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
413};
414
415static struct sensor_device_attribute_2 sda_temp_max[] = {
416 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
417 show_temp, store_temp, 0, 1),
418 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
419 show_temp, store_temp, 1, 1),
420};
421
422static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
423 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
424 show_temp, store_temp, 0, 2),
425 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
426 show_temp, store_temp, 1, 2),
427};
428
429#define show_pwm_reg(reg) \
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800430static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
431 char *buf) \
Kevin Lo85f03bc2007-11-23 09:31:52 +0800432{ \
433 struct w83l786ng_data *data = w83l786ng_update_device(dev); \
434 int nr = to_sensor_dev_attr(attr)->index; \
435 return sprintf(buf, "%d\n", data->reg[nr]); \
436}
437
438show_pwm_reg(pwm_mode)
439show_pwm_reg(pwm_enable)
440show_pwm_reg(pwm)
441
442static ssize_t
443store_pwm_mode(struct device *dev, struct device_attribute *attr,
444 const char *buf, size_t count)
445{
446 int nr = to_sensor_dev_attr(attr)->index;
447 struct i2c_client *client = to_i2c_client(dev);
448 struct w83l786ng_data *data = i2c_get_clientdata(client);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800449 u8 reg;
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800450 unsigned long val;
451 int err;
452
453 err = kstrtoul(buf, 10, &val);
454 if (err)
455 return err;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800456
457 if (val > 1)
458 return -EINVAL;
459 mutex_lock(&data->update_lock);
460 data->pwm_mode[nr] = val;
461 reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
462 reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]);
463 if (!val)
464 reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
465 w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
466 mutex_unlock(&data->update_lock);
467 return count;
468}
469
470static ssize_t
471store_pwm(struct device *dev, struct device_attribute *attr,
472 const char *buf, size_t count)
473{
474 int nr = to_sensor_dev_attr(attr)->index;
475 struct i2c_client *client = to_i2c_client(dev);
476 struct w83l786ng_data *data = i2c_get_clientdata(client);
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800477 unsigned long val;
478 int err;
479
480 err = kstrtoul(buf, 10, &val);
481 if (err)
482 return err;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800483 val = clamp_val(val, 0, 255);
Jean Delvare33a7ab92013-12-12 08:05:32 +0100484 val = DIV_ROUND_CLOSEST(val, 0x11);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800485
486 mutex_lock(&data->update_lock);
Jean Delvare33a7ab92013-12-12 08:05:32 +0100487 data->pwm[nr] = val * 0x11;
488 val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800489 w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
490 mutex_unlock(&data->update_lock);
491 return count;
492}
493
494static ssize_t
495store_pwm_enable(struct device *dev, struct device_attribute *attr,
496 const char *buf, size_t count)
497{
498 int nr = to_sensor_dev_attr(attr)->index;
499 struct i2c_client *client = to_i2c_client(dev);
500 struct w83l786ng_data *data = i2c_get_clientdata(client);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800501 u8 reg;
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800502 unsigned long val;
503 int err;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800504
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800505 err = kstrtoul(buf, 10, &val);
506 if (err)
507 return err;
508
509 if (!val || val > 2) /* only modes 1 and 2 are supported */
Kevin Lo85f03bc2007-11-23 09:31:52 +0800510 return -EINVAL;
511
512 mutex_lock(&data->update_lock);
513 reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
514 data->pwm_enable[nr] = val;
Brian Carnescf7559b2013-12-12 08:05:32 +0100515 reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800516 reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr];
517 w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
518 mutex_unlock(&data->update_lock);
519 return count;
520}
521
522static struct sensor_device_attribute sda_pwm[] = {
523 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
524 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
525};
526
527static struct sensor_device_attribute sda_pwm_mode[] = {
528 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
529 store_pwm_mode, 0),
530 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
531 store_pwm_mode, 1),
532};
533
534static struct sensor_device_attribute sda_pwm_enable[] = {
535 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
536 store_pwm_enable, 0),
537 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
538 store_pwm_enable, 1),
539};
540
541/* For Smart Fan I/Thermal Cruise and Smart Fan II */
542static ssize_t
543show_tolerance(struct device *dev, struct device_attribute *attr, char *buf)
544{
545 int nr = to_sensor_dev_attr(attr)->index;
546 struct w83l786ng_data *data = w83l786ng_update_device(dev);
547 return sprintf(buf, "%ld\n", (long)data->tolerance[nr]);
548}
549
550static ssize_t
551store_tolerance(struct device *dev, struct device_attribute *attr,
552 const char *buf, size_t count)
553{
554 int nr = to_sensor_dev_attr(attr)->index;
555 struct i2c_client *client = to_i2c_client(dev);
556 struct w83l786ng_data *data = i2c_get_clientdata(client);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800557 u8 tol_tmp, tol_mask;
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800558 unsigned long val;
559 int err;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800560
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800561 err = kstrtoul(buf, 10, &val);
562 if (err)
563 return err;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800564
565 mutex_lock(&data->update_lock);
566 tol_mask = w83l786ng_read_value(client,
567 W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800568 tol_tmp = clamp_val(val, 0, 15);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800569 tol_tmp &= 0x0f;
570 data->tolerance[nr] = tol_tmp;
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800571 if (nr == 1)
Kevin Lo85f03bc2007-11-23 09:31:52 +0800572 tol_tmp <<= 4;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800573
574 w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
575 tol_mask | tol_tmp);
576 mutex_unlock(&data->update_lock);
577 return count;
578}
579
580static struct sensor_device_attribute sda_tolerance[] = {
581 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO,
582 show_tolerance, store_tolerance, 0),
583 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO,
584 show_tolerance, store_tolerance, 1),
585};
586
587
588#define IN_UNIT_ATTRS(X) \
589 &sda_in_input[X].dev_attr.attr, \
590 &sda_in_min[X].dev_attr.attr, \
591 &sda_in_max[X].dev_attr.attr
592
593#define FAN_UNIT_ATTRS(X) \
594 &sda_fan_input[X].dev_attr.attr, \
595 &sda_fan_min[X].dev_attr.attr, \
596 &sda_fan_div[X].dev_attr.attr
597
598#define TEMP_UNIT_ATTRS(X) \
599 &sda_temp_input[X].dev_attr.attr, \
600 &sda_temp_max[X].dev_attr.attr, \
601 &sda_temp_max_hyst[X].dev_attr.attr
602
603#define PWM_UNIT_ATTRS(X) \
604 &sda_pwm[X].dev_attr.attr, \
605 &sda_pwm_mode[X].dev_attr.attr, \
606 &sda_pwm_enable[X].dev_attr.attr
607
608#define TOLERANCE_UNIT_ATTRS(X) \
609 &sda_tolerance[X].dev_attr.attr
610
611static struct attribute *w83l786ng_attributes[] = {
612 IN_UNIT_ATTRS(0),
613 IN_UNIT_ATTRS(1),
614 IN_UNIT_ATTRS(2),
615 FAN_UNIT_ATTRS(0),
616 FAN_UNIT_ATTRS(1),
617 TEMP_UNIT_ATTRS(0),
618 TEMP_UNIT_ATTRS(1),
619 PWM_UNIT_ATTRS(0),
620 PWM_UNIT_ATTRS(1),
621 TOLERANCE_UNIT_ATTRS(0),
622 TOLERANCE_UNIT_ATTRS(1),
623 NULL
624};
625
626static const struct attribute_group w83l786ng_group = {
627 .attrs = w83l786ng_attributes,
628};
629
630static int
Jean Delvare310ec792009-12-14 21:17:23 +0100631w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
Kevin Lo85f03bc2007-11-23 09:31:52 +0800632{
Jean Delvare33468e72008-07-16 19:30:18 +0200633 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100634 u16 man_id;
635 u8 chip_id;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800636
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800637 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare33468e72008-07-16 19:30:18 +0200638 return -ENODEV;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800639
Jean Delvare52df6442009-12-09 20:35:57 +0100640 /* Detection */
641 if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) {
642 dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n",
643 client->addr);
644 return -ENODEV;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800645 }
646
Jean Delvare52df6442009-12-09 20:35:57 +0100647 /* Identification */
648 man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) +
649 w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2);
650 chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800651
Jean Delvare52df6442009-12-09 20:35:57 +0100652 if (man_id != 0x5CA3 || /* Winbond */
653 chip_id != 0x80) { /* W83L786NG */
654 dev_dbg(&adapter->dev,
655 "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
656 man_id, chip_id);
657 return -ENODEV;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800658 }
659
Jean Delvare33468e72008-07-16 19:30:18 +0200660 strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800661
Jean Delvare33468e72008-07-16 19:30:18 +0200662 return 0;
663}
664
665static int
666w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
667{
668 struct device *dev = &client->dev;
669 struct w83l786ng_data *data;
670 int i, err = 0;
671 u8 reg_tmp;
672
Guenter Roeck816864d2012-06-02 11:48:02 -0700673 data = devm_kzalloc(&client->dev, sizeof(struct w83l786ng_data),
674 GFP_KERNEL);
675 if (!data)
676 return -ENOMEM;
Jean Delvare33468e72008-07-16 19:30:18 +0200677
678 i2c_set_clientdata(client, data);
679 mutex_init(&data->update_lock);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800680
681 /* Initialize the chip */
682 w83l786ng_init_client(client);
683
684 /* A few vars need to be filled upon startup */
685 for (i = 0; i < 2; i++) {
686 data->fan_min[i] = w83l786ng_read_value(client,
687 W83L786NG_REG_FAN_MIN(i));
688 }
689
690 /* Update the fan divisor */
691 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
692 data->fan_div[0] = reg_tmp & 0x07;
693 data->fan_div[1] = (reg_tmp >> 4) & 0x07;
694
695 /* Register sysfs hooks */
Guenter Roeckca3ccad2012-01-15 11:03:08 -0800696 err = sysfs_create_group(&client->dev.kobj, &w83l786ng_group);
697 if (err)
Kevin Lo85f03bc2007-11-23 09:31:52 +0800698 goto exit_remove;
699
700 data->hwmon_dev = hwmon_device_register(dev);
701 if (IS_ERR(data->hwmon_dev)) {
702 err = PTR_ERR(data->hwmon_dev);
703 goto exit_remove;
704 }
705
706 return 0;
707
708 /* Unregister sysfs hooks */
709
710exit_remove:
711 sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800712 return err;
713}
714
715static int
Jean Delvare33468e72008-07-16 19:30:18 +0200716w83l786ng_remove(struct i2c_client *client)
Kevin Lo85f03bc2007-11-23 09:31:52 +0800717{
718 struct w83l786ng_data *data = i2c_get_clientdata(client);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800719
720 hwmon_device_unregister(data->hwmon_dev);
721 sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
722
Kevin Lo85f03bc2007-11-23 09:31:52 +0800723 return 0;
724}
725
726static void
727w83l786ng_init_client(struct i2c_client *client)
728{
729 u8 tmp;
730
731 if (reset)
732 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
733
734 /* Start monitoring */
735 tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
736 if (!(tmp & 0x01))
737 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
738}
739
740static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
741{
742 struct i2c_client *client = to_i2c_client(dev);
743 struct w83l786ng_data *data = i2c_get_clientdata(client);
744 int i, j;
745 u8 reg_tmp, pwmcfg;
746
747 mutex_lock(&data->update_lock);
748 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
749 || !data->valid) {
750 dev_dbg(&client->dev, "Updating w83l786ng data.\n");
751
752 /* Update the voltages measured value and limits */
753 for (i = 0; i < 3; i++) {
754 data->in[i] = w83l786ng_read_value(client,
755 W83L786NG_REG_IN(i));
756 data->in_min[i] = w83l786ng_read_value(client,
757 W83L786NG_REG_IN_MIN(i));
758 data->in_max[i] = w83l786ng_read_value(client,
759 W83L786NG_REG_IN_MAX(i));
760 }
761
762 /* Update the fan counts and limits */
763 for (i = 0; i < 2; i++) {
764 data->fan[i] = w83l786ng_read_value(client,
765 W83L786NG_REG_FAN(i));
766 data->fan_min[i] = w83l786ng_read_value(client,
767 W83L786NG_REG_FAN_MIN(i));
768 }
769
770 /* Update the fan divisor */
771 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
772 data->fan_div[0] = reg_tmp & 0x07;
773 data->fan_div[1] = (reg_tmp >> 4) & 0x07;
774
775 pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
776 for (i = 0; i < 2; i++) {
777 data->pwm_mode[i] =
778 ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
779 ? 0 : 1;
780 data->pwm_enable[i] =
Brian Carnescf7559b2013-12-12 08:05:32 +0100781 ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
Jean Delvare33a7ab92013-12-12 08:05:32 +0100782 data->pwm[i] =
783 (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
784 & 0x0f) * 0x11;
Kevin Lo85f03bc2007-11-23 09:31:52 +0800785 }
786
787
788 /* Update the temperature sensors */
789 for (i = 0; i < 2; i++) {
790 for (j = 0; j < 3; j++) {
791 data->temp[i][j] = w83l786ng_read_value(client,
792 W83L786NG_REG_TEMP[i][j]);
793 }
794 }
795
796 /* Update Smart Fan I/II tolerance */
797 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
798 data->tolerance[0] = reg_tmp & 0x0f;
799 data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
800
801 data->last_updated = jiffies;
802 data->valid = 1;
803
804 }
805
806 mutex_unlock(&data->update_lock);
807
808 return data;
809}
810
Axel Linf0967ee2012-01-20 15:38:18 +0800811module_i2c_driver(w83l786ng_driver);
Kevin Lo85f03bc2007-11-23 09:31:52 +0800812
813MODULE_AUTHOR("Kevin Lo");
814MODULE_DESCRIPTION("w83l786ng driver");
815MODULE_LICENSE("GPL");