2 * emc6w201.c - Hardware monitoring driver for the SMSC EMC6W201
3 * Copyright (C) 2011 Jean Delvare <khali@linux-fr.org>
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; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/module.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
35 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
38 * The EMC6W201 registers
41 #define EMC6W201_REG_IN(nr) (0x20 + (nr))
42 #define EMC6W201_REG_TEMP(nr) (0x26 + (nr))
43 #define EMC6W201_REG_FAN(nr) (0x2C + (nr) * 2)
44 #define EMC6W201_REG_COMPANY 0x3E
45 #define EMC6W201_REG_VERSTEP 0x3F
46 #define EMC6W201_REG_CONFIG 0x40
47 #define EMC6W201_REG_IN_LOW(nr) (0x4A + (nr) * 2)
48 #define EMC6W201_REG_IN_HIGH(nr) (0x4B + (nr) * 2)
49 #define EMC6W201_REG_TEMP_LOW(nr) (0x56 + (nr) * 2)
50 #define EMC6W201_REG_TEMP_HIGH(nr) (0x57 + (nr) * 2)
51 #define EMC6W201_REG_FAN_MIN(nr) (0x62 + (nr) * 2)
53 enum { input
, min
, max
} subfeature
;
59 struct emc6w201_data
{
60 struct device
*hwmon_dev
;
61 struct mutex update_lock
;
62 char valid
; /* zero until following fields are valid */
63 unsigned long last_updated
; /* in jiffies */
65 /* registers values */
72 * Combine LSB and MSB registers in a single value
73 * Locking: must be called with data->update_lock held
75 static u16
emc6w201_read16(struct i2c_client
*client
, u8 reg
)
79 lsb
= i2c_smbus_read_byte_data(client
, reg
);
80 msb
= i2c_smbus_read_byte_data(client
, reg
+ 1);
81 if (unlikely(lsb
< 0 || msb
< 0)) {
82 dev_err(&client
->dev
, "%d-bit %s failed at 0x%02x\n",
84 return 0xFFFF; /* Arbitrary value */
87 return (msb
<< 8) | lsb
;
91 * Write 16-bit value to LSB and MSB registers
92 * Locking: must be called with data->update_lock held
94 static int emc6w201_write16(struct i2c_client
*client
, u8 reg
, u16 val
)
98 err
= i2c_smbus_write_byte_data(client
, reg
, val
& 0xff);
100 err
= i2c_smbus_write_byte_data(client
, reg
+ 1, val
>> 8);
101 if (unlikely(err
< 0))
102 dev_err(&client
->dev
, "%d-bit %s failed at 0x%02x\n",
108 /* Read 8-bit value from register */
109 static u8
emc6w201_read8(struct i2c_client
*client
, u8 reg
)
113 val
= i2c_smbus_read_byte_data(client
, reg
);
114 if (unlikely(val
< 0)) {
115 dev_err(&client
->dev
, "%d-bit %s failed at 0x%02x\n",
117 return 0x00; /* Arbitrary value */
123 /* Write 8-bit value to register */
124 static int emc6w201_write8(struct i2c_client
*client
, u8 reg
, u8 val
)
128 err
= i2c_smbus_write_byte_data(client
, reg
, val
);
129 if (unlikely(err
< 0))
130 dev_err(&client
->dev
, "%d-bit %s failed at 0x%02x\n",
136 static struct emc6w201_data
*emc6w201_update_device(struct device
*dev
)
138 struct i2c_client
*client
= to_i2c_client(dev
);
139 struct emc6w201_data
*data
= i2c_get_clientdata(client
);
142 mutex_lock(&data
->update_lock
);
144 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
145 for (nr
= 0; nr
< 6; nr
++) {
146 data
->in
[input
][nr
] =
147 emc6w201_read8(client
,
148 EMC6W201_REG_IN(nr
));
150 emc6w201_read8(client
,
151 EMC6W201_REG_IN_LOW(nr
));
153 emc6w201_read8(client
,
154 EMC6W201_REG_IN_HIGH(nr
));
157 for (nr
= 0; nr
< 6; nr
++) {
158 data
->temp
[input
][nr
] =
159 emc6w201_read8(client
,
160 EMC6W201_REG_TEMP(nr
));
161 data
->temp
[min
][nr
] =
162 emc6w201_read8(client
,
163 EMC6W201_REG_TEMP_LOW(nr
));
164 data
->temp
[max
][nr
] =
165 emc6w201_read8(client
,
166 EMC6W201_REG_TEMP_HIGH(nr
));
169 for (nr
= 0; nr
< 5; nr
++) {
170 data
->fan
[input
][nr
] =
171 emc6w201_read16(client
,
172 EMC6W201_REG_FAN(nr
));
174 emc6w201_read16(client
,
175 EMC6W201_REG_FAN_MIN(nr
));
178 data
->last_updated
= jiffies
;
182 mutex_unlock(&data
->update_lock
);
188 * Sysfs callback functions
191 static const u16 nominal_mv
[6] = { 2500, 1500, 3300, 5000, 1500, 1500 };
193 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*devattr
,
196 struct emc6w201_data
*data
= emc6w201_update_device(dev
);
197 int sf
= to_sensor_dev_attr_2(devattr
)->index
;
198 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
200 return sprintf(buf
, "%u\n",
201 (unsigned)data
->in
[sf
][nr
] * nominal_mv
[nr
] / 0xC0);
204 static ssize_t
set_in(struct device
*dev
, struct device_attribute
*devattr
,
205 const char *buf
, size_t count
)
207 struct i2c_client
*client
= to_i2c_client(dev
);
208 struct emc6w201_data
*data
= i2c_get_clientdata(client
);
209 int sf
= to_sensor_dev_attr_2(devattr
)->index
;
210 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
215 err
= strict_strtol(buf
, 10, &val
);
219 val
= DIV_ROUND_CLOSEST(val
* 0xC0, nominal_mv
[nr
]);
220 reg
= (sf
== min
) ? EMC6W201_REG_IN_LOW(nr
)
221 : EMC6W201_REG_IN_HIGH(nr
);
223 mutex_lock(&data
->update_lock
);
224 data
->in
[sf
][nr
] = SENSORS_LIMIT(val
, 0, 255);
225 err
= emc6w201_write8(client
, reg
, data
->in
[sf
][nr
]);
226 mutex_unlock(&data
->update_lock
);
228 return err
< 0 ? err
: count
;
231 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*devattr
,
234 struct emc6w201_data
*data
= emc6w201_update_device(dev
);
235 int sf
= to_sensor_dev_attr_2(devattr
)->index
;
236 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
238 return sprintf(buf
, "%d\n", (int)data
->temp
[sf
][nr
] * 1000);
241 static ssize_t
set_temp(struct device
*dev
, struct device_attribute
*devattr
,
242 const char *buf
, size_t count
)
244 struct i2c_client
*client
= to_i2c_client(dev
);
245 struct emc6w201_data
*data
= i2c_get_clientdata(client
);
246 int sf
= to_sensor_dev_attr_2(devattr
)->index
;
247 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
252 err
= strict_strtol(buf
, 10, &val
);
257 reg
= (sf
== min
) ? EMC6W201_REG_TEMP_LOW(nr
)
258 : EMC6W201_REG_TEMP_HIGH(nr
);
260 mutex_lock(&data
->update_lock
);
261 data
->temp
[sf
][nr
] = SENSORS_LIMIT(val
, -127, 128);
262 err
= emc6w201_write8(client
, reg
, data
->temp
[sf
][nr
]);
263 mutex_unlock(&data
->update_lock
);
265 return err
< 0 ? err
: count
;
268 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*devattr
,
271 struct emc6w201_data
*data
= emc6w201_update_device(dev
);
272 int sf
= to_sensor_dev_attr_2(devattr
)->index
;
273 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
276 if (data
->fan
[sf
][nr
] == 0 || data
->fan
[sf
][nr
] == 0xFFFF)
279 rpm
= 5400000U / data
->fan
[sf
][nr
];
281 return sprintf(buf
, "%u\n", rpm
);
284 static ssize_t
set_fan(struct device
*dev
, struct device_attribute
*devattr
,
285 const char *buf
, size_t count
)
287 struct i2c_client
*client
= to_i2c_client(dev
);
288 struct emc6w201_data
*data
= i2c_get_clientdata(client
);
289 int sf
= to_sensor_dev_attr_2(devattr
)->index
;
290 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
294 err
= strict_strtoul(buf
, 10, &val
);
301 val
= DIV_ROUND_CLOSEST(5400000U, val
);
302 val
= SENSORS_LIMIT(val
, 0, 0xFFFE);
305 mutex_lock(&data
->update_lock
);
306 data
->fan
[sf
][nr
] = val
;
307 err
= emc6w201_write16(client
, EMC6W201_REG_FAN_MIN(nr
),
309 mutex_unlock(&data
->update_lock
);
311 return err
< 0 ? err
: count
;
314 static SENSOR_DEVICE_ATTR_2(in0_input
, S_IRUGO
, show_in
, NULL
, 0, input
);
315 static SENSOR_DEVICE_ATTR_2(in0_min
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
317 static SENSOR_DEVICE_ATTR_2(in0_max
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
319 static SENSOR_DEVICE_ATTR_2(in1_input
, S_IRUGO
, show_in
, NULL
, 1, input
);
320 static SENSOR_DEVICE_ATTR_2(in1_min
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
322 static SENSOR_DEVICE_ATTR_2(in1_max
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
324 static SENSOR_DEVICE_ATTR_2(in2_input
, S_IRUGO
, show_in
, NULL
, 2, input
);
325 static SENSOR_DEVICE_ATTR_2(in2_min
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
327 static SENSOR_DEVICE_ATTR_2(in2_max
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
329 static SENSOR_DEVICE_ATTR_2(in3_input
, S_IRUGO
, show_in
, NULL
, 3, input
);
330 static SENSOR_DEVICE_ATTR_2(in3_min
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
332 static SENSOR_DEVICE_ATTR_2(in3_max
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
334 static SENSOR_DEVICE_ATTR_2(in4_input
, S_IRUGO
, show_in
, NULL
, 4, input
);
335 static SENSOR_DEVICE_ATTR_2(in4_min
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
337 static SENSOR_DEVICE_ATTR_2(in4_max
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
339 static SENSOR_DEVICE_ATTR_2(in5_input
, S_IRUGO
, show_in
, NULL
, 5, input
);
340 static SENSOR_DEVICE_ATTR_2(in5_min
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
342 static SENSOR_DEVICE_ATTR_2(in5_max
, S_IRUGO
| S_IWUSR
, show_in
, set_in
,
345 static SENSOR_DEVICE_ATTR_2(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0, input
);
346 static SENSOR_DEVICE_ATTR_2(temp1_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
348 static SENSOR_DEVICE_ATTR_2(temp1_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
350 static SENSOR_DEVICE_ATTR_2(temp2_input
, S_IRUGO
, show_temp
, NULL
, 1, input
);
351 static SENSOR_DEVICE_ATTR_2(temp2_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
353 static SENSOR_DEVICE_ATTR_2(temp2_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
355 static SENSOR_DEVICE_ATTR_2(temp3_input
, S_IRUGO
, show_temp
, NULL
, 2, input
);
356 static SENSOR_DEVICE_ATTR_2(temp3_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
358 static SENSOR_DEVICE_ATTR_2(temp3_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
360 static SENSOR_DEVICE_ATTR_2(temp4_input
, S_IRUGO
, show_temp
, NULL
, 3, input
);
361 static SENSOR_DEVICE_ATTR_2(temp4_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
363 static SENSOR_DEVICE_ATTR_2(temp4_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
365 static SENSOR_DEVICE_ATTR_2(temp5_input
, S_IRUGO
, show_temp
, NULL
, 4, input
);
366 static SENSOR_DEVICE_ATTR_2(temp5_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
368 static SENSOR_DEVICE_ATTR_2(temp5_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
370 static SENSOR_DEVICE_ATTR_2(temp6_input
, S_IRUGO
, show_temp
, NULL
, 5, input
);
371 static SENSOR_DEVICE_ATTR_2(temp6_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
373 static SENSOR_DEVICE_ATTR_2(temp6_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
376 static SENSOR_DEVICE_ATTR_2(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0, input
);
377 static SENSOR_DEVICE_ATTR_2(fan1_min
, S_IRUGO
| S_IWUSR
, show_fan
, set_fan
,
379 static SENSOR_DEVICE_ATTR_2(fan2_input
, S_IRUGO
, show_fan
, NULL
, 1, input
);
380 static SENSOR_DEVICE_ATTR_2(fan2_min
, S_IRUGO
| S_IWUSR
, show_fan
, set_fan
,
382 static SENSOR_DEVICE_ATTR_2(fan3_input
, S_IRUGO
, show_fan
, NULL
, 2, input
);
383 static SENSOR_DEVICE_ATTR_2(fan3_min
, S_IRUGO
| S_IWUSR
, show_fan
, set_fan
,
385 static SENSOR_DEVICE_ATTR_2(fan4_input
, S_IRUGO
, show_fan
, NULL
, 3, input
);
386 static SENSOR_DEVICE_ATTR_2(fan4_min
, S_IRUGO
| S_IWUSR
, show_fan
, set_fan
,
388 static SENSOR_DEVICE_ATTR_2(fan5_input
, S_IRUGO
, show_fan
, NULL
, 4, input
);
389 static SENSOR_DEVICE_ATTR_2(fan5_min
, S_IRUGO
| S_IWUSR
, show_fan
, set_fan
,
392 static struct attribute
*emc6w201_attributes
[] = {
393 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
394 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
395 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
396 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
397 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
398 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
399 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
400 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
401 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
402 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
403 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
404 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
405 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
406 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
407 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
408 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
409 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
410 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
412 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
413 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
414 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
415 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
416 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
417 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
418 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
419 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
420 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
421 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
422 &sensor_dev_attr_temp4_min
.dev_attr
.attr
,
423 &sensor_dev_attr_temp4_max
.dev_attr
.attr
,
424 &sensor_dev_attr_temp5_input
.dev_attr
.attr
,
425 &sensor_dev_attr_temp5_min
.dev_attr
.attr
,
426 &sensor_dev_attr_temp5_max
.dev_attr
.attr
,
427 &sensor_dev_attr_temp6_input
.dev_attr
.attr
,
428 &sensor_dev_attr_temp6_min
.dev_attr
.attr
,
429 &sensor_dev_attr_temp6_max
.dev_attr
.attr
,
431 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
432 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
433 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
434 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
435 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
436 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
437 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
438 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
439 &sensor_dev_attr_fan5_input
.dev_attr
.attr
,
440 &sensor_dev_attr_fan5_min
.dev_attr
.attr
,
444 static const struct attribute_group emc6w201_group
= {
445 .attrs
= emc6w201_attributes
,
452 /* Return 0 if detection is successful, -ENODEV otherwise */
453 static int emc6w201_detect(struct i2c_client
*client
,
454 struct i2c_board_info
*info
)
456 struct i2c_adapter
*adapter
= client
->adapter
;
457 int company
, verstep
, config
;
459 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
463 company
= i2c_smbus_read_byte_data(client
, EMC6W201_REG_COMPANY
);
466 verstep
= i2c_smbus_read_byte_data(client
, EMC6W201_REG_VERSTEP
);
467 if (verstep
< 0 || (verstep
& 0xF0) != 0xB0)
469 if ((verstep
& 0x0F) > 2) {
470 dev_dbg(&client
->dev
, "Unknwown EMC6W201 stepping %d\n",
475 /* Check configuration */
476 config
= i2c_smbus_read_byte_data(client
, EMC6W201_REG_CONFIG
);
477 if (config
< 0 || (config
& 0xF4) != 0x04)
479 if (!(config
& 0x01)) {
480 dev_err(&client
->dev
, "Monitoring not enabled\n");
484 strlcpy(info
->type
, "emc6w201", I2C_NAME_SIZE
);
489 static int emc6w201_probe(struct i2c_client
*client
,
490 const struct i2c_device_id
*id
)
492 struct emc6w201_data
*data
;
495 data
= kzalloc(sizeof(struct emc6w201_data
), GFP_KERNEL
);
501 i2c_set_clientdata(client
, data
);
502 mutex_init(&data
->update_lock
);
504 /* Create sysfs attribute */
505 err
= sysfs_create_group(&client
->dev
.kobj
, &emc6w201_group
);
509 /* Expose as a hwmon device */
510 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
511 if (IS_ERR(data
->hwmon_dev
)) {
512 err
= PTR_ERR(data
->hwmon_dev
);
519 sysfs_remove_group(&client
->dev
.kobj
, &emc6w201_group
);
526 static int emc6w201_remove(struct i2c_client
*client
)
528 struct emc6w201_data
*data
= i2c_get_clientdata(client
);
530 hwmon_device_unregister(data
->hwmon_dev
);
531 sysfs_remove_group(&client
->dev
.kobj
, &emc6w201_group
);
537 static const struct i2c_device_id emc6w201_id
[] = {
541 MODULE_DEVICE_TABLE(i2c
, emc6w201_id
);
543 static struct i2c_driver emc6w201_driver
= {
544 .class = I2C_CLASS_HWMON
,
548 .probe
= emc6w201_probe
,
549 .remove
= emc6w201_remove
,
550 .id_table
= emc6w201_id
,
551 .detect
= emc6w201_detect
,
552 .address_list
= normal_i2c
,
555 static int __init
sensors_emc6w201_init(void)
557 return i2c_add_driver(&emc6w201_driver
);
559 module_init(sensors_emc6w201_init
);
561 static void __exit
sensors_emc6w201_exit(void)
563 i2c_del_driver(&emc6w201_driver
);
565 module_exit(sensors_emc6w201_exit
);
567 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
568 MODULE_DESCRIPTION("SMSC EMC6W201 hardware monitoring driver");
569 MODULE_LICENSE("GPL");