2 * emc6w201.c - Hardware monitoring driver for the SMSC EMC6W201
3 * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
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/init.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
34 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37 * The EMC6W201 registers
40 #define EMC6W201_REG_IN(nr) (0x20 + (nr))
41 #define EMC6W201_REG_TEMP(nr) (0x26 + (nr))
42 #define EMC6W201_REG_FAN(nr) (0x2C + (nr) * 2)
43 #define EMC6W201_REG_COMPANY 0x3E
44 #define EMC6W201_REG_VERSTEP 0x3F
45 #define EMC6W201_REG_CONFIG 0x40
46 #define EMC6W201_REG_IN_LOW(nr) (0x4A + (nr) * 2)
47 #define EMC6W201_REG_IN_HIGH(nr) (0x4B + (nr) * 2)
48 #define EMC6W201_REG_TEMP_LOW(nr) (0x56 + (nr) * 2)
49 #define EMC6W201_REG_TEMP_HIGH(nr) (0x57 + (nr) * 2)
50 #define EMC6W201_REG_FAN_MIN(nr) (0x62 + (nr) * 2)
52 enum subfeature { input, min, max };
58 struct emc6w201_data {
59 struct i2c_client *client;
60 struct mutex update_lock;
61 char valid; /* zero until following fields are valid */
62 unsigned long last_updated; /* in jiffies */
64 /* registers values */
71 * Combine LSB and MSB registers in a single value
72 * Locking: must be called with data->update_lock held
74 static u16 emc6w201_read16(struct i2c_client *client, u8 reg)
78 lsb = i2c_smbus_read_byte_data(client, reg);
79 msb = i2c_smbus_read_byte_data(client, reg + 1);
80 if (unlikely(lsb < 0 || msb < 0)) {
81 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
83 return 0xFFFF; /* Arbitrary value */
86 return (msb << 8) | lsb;
90 * Write 16-bit value to LSB and MSB registers
91 * Locking: must be called with data->update_lock held
93 static int emc6w201_write16(struct i2c_client *client, u8 reg, u16 val)
97 err = i2c_smbus_write_byte_data(client, reg, val & 0xff);
99 err = i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
100 if (unlikely(err < 0))
101 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
107 /* Read 8-bit value from register */
108 static u8 emc6w201_read8(struct i2c_client *client, u8 reg)
112 val = i2c_smbus_read_byte_data(client, reg);
113 if (unlikely(val < 0)) {
114 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
116 return 0x00; /* Arbitrary value */
122 /* Write 8-bit value to register */
123 static int emc6w201_write8(struct i2c_client *client, u8 reg, u8 val)
127 err = i2c_smbus_write_byte_data(client, reg, val);
128 if (unlikely(err < 0))
129 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
135 static struct emc6w201_data *emc6w201_update_device(struct device *dev)
137 struct emc6w201_data *data = dev_get_drvdata(dev);
138 struct i2c_client *client = data->client;
141 mutex_lock(&data->update_lock);
143 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
144 for (nr = 0; nr < 6; nr++) {
145 data->in[input][nr] =
146 emc6w201_read8(client,
147 EMC6W201_REG_IN(nr));
149 emc6w201_read8(client,
150 EMC6W201_REG_IN_LOW(nr));
152 emc6w201_read8(client,
153 EMC6W201_REG_IN_HIGH(nr));
156 for (nr = 0; nr < 6; nr++) {
157 data->temp[input][nr] =
158 emc6w201_read8(client,
159 EMC6W201_REG_TEMP(nr));
160 data->temp[min][nr] =
161 emc6w201_read8(client,
162 EMC6W201_REG_TEMP_LOW(nr));
163 data->temp[max][nr] =
164 emc6w201_read8(client,
165 EMC6W201_REG_TEMP_HIGH(nr));
168 for (nr = 0; nr < 5; nr++) {
169 data->fan[input][nr] =
170 emc6w201_read16(client,
171 EMC6W201_REG_FAN(nr));
173 emc6w201_read16(client,
174 EMC6W201_REG_FAN_MIN(nr));
177 data->last_updated = jiffies;
181 mutex_unlock(&data->update_lock);
187 * Sysfs callback functions
190 static const s16 nominal_mv[6] = { 2500, 1500, 3300, 5000, 1500, 1500 };
192 static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
195 struct emc6w201_data *data = emc6w201_update_device(dev);
196 int sf = to_sensor_dev_attr_2(devattr)->index;
197 int nr = to_sensor_dev_attr_2(devattr)->nr;
199 return sprintf(buf, "%u\n",
200 (unsigned)data->in[sf][nr] * nominal_mv[nr] / 0xC0);
203 static ssize_t in_store(struct device *dev, struct device_attribute *devattr,
204 const char *buf, size_t count)
206 struct emc6w201_data *data = dev_get_drvdata(dev);
207 struct i2c_client *client = data->client;
208 int sf = to_sensor_dev_attr_2(devattr)->index;
209 int nr = to_sensor_dev_attr_2(devattr)->nr;
214 err = kstrtol(buf, 10, &val);
218 val = clamp_val(val, 0, 255 * nominal_mv[nr] / 192);
219 val = DIV_ROUND_CLOSEST(val * 192, 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] = val;
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 temp_show(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 temp_store(struct device *dev,
242 struct device_attribute *devattr, const char *buf,
245 struct emc6w201_data *data = dev_get_drvdata(dev);
246 struct i2c_client *client = data->client;
247 int sf = to_sensor_dev_attr_2(devattr)->index;
248 int nr = to_sensor_dev_attr_2(devattr)->nr;
253 err = kstrtol(buf, 10, &val);
257 val = clamp_val(val, -127000, 127000);
258 val = DIV_ROUND_CLOSEST(val, 1000);
259 reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr)
260 : EMC6W201_REG_TEMP_HIGH(nr);
262 mutex_lock(&data->update_lock);
263 data->temp[sf][nr] = val;
264 err = emc6w201_write8(client, reg, data->temp[sf][nr]);
265 mutex_unlock(&data->update_lock);
267 return err < 0 ? err : count;
270 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
273 struct emc6w201_data *data = emc6w201_update_device(dev);
274 int sf = to_sensor_dev_attr_2(devattr)->index;
275 int nr = to_sensor_dev_attr_2(devattr)->nr;
278 if (data->fan[sf][nr] == 0 || data->fan[sf][nr] == 0xFFFF)
281 rpm = 5400000U / data->fan[sf][nr];
283 return sprintf(buf, "%u\n", rpm);
286 static ssize_t fan_store(struct device *dev, struct device_attribute *devattr,
287 const char *buf, size_t count)
289 struct emc6w201_data *data = dev_get_drvdata(dev);
290 struct i2c_client *client = data->client;
291 int sf = to_sensor_dev_attr_2(devattr)->index;
292 int nr = to_sensor_dev_attr_2(devattr)->nr;
296 err = kstrtoul(buf, 10, &val);
303 val = DIV_ROUND_CLOSEST(5400000U, val);
304 val = clamp_val(val, 0, 0xFFFE);
307 mutex_lock(&data->update_lock);
308 data->fan[sf][nr] = val;
309 err = emc6w201_write16(client, EMC6W201_REG_FAN_MIN(nr),
311 mutex_unlock(&data->update_lock);
313 return err < 0 ? err : count;
316 static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, input);
317 static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, min);
318 static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, max);
319 static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, input);
320 static SENSOR_DEVICE_ATTR_2_RW(in1_min, in, 1, min);
321 static SENSOR_DEVICE_ATTR_2_RW(in1_max, in, 1, max);
322 static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, input);
323 static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, min);
324 static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, max);
325 static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, input);
326 static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, min);
327 static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, max);
328 static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, input);
329 static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, min);
330 static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, max);
331 static SENSOR_DEVICE_ATTR_2_RO(in5_input, in, 5, input);
332 static SENSOR_DEVICE_ATTR_2_RW(in5_min, in, 5, min);
333 static SENSOR_DEVICE_ATTR_2_RW(in5_max, in, 5, max);
335 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, input);
336 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0, min);
337 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0, max);
338 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 1, input);
339 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, min);
340 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 1, max);
341 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 2, input);
342 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 2, min);
343 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, max);
344 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 3, input);
345 static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 3, min);
346 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 3, max);
347 static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 4, input);
348 static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 4, min);
349 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 4, max);
350 static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 5, input);
351 static SENSOR_DEVICE_ATTR_2_RW(temp6_min, temp, 5, min);
352 static SENSOR_DEVICE_ATTR_2_RW(temp6_max, temp, 5, max);
354 static SENSOR_DEVICE_ATTR_2_RO(fan1_input, fan, 0, input);
355 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan, 0, min);
356 static SENSOR_DEVICE_ATTR_2_RO(fan2_input, fan, 1, input);
357 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan, 1, min);
358 static SENSOR_DEVICE_ATTR_2_RO(fan3_input, fan, 2, input);
359 static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan, 2, min);
360 static SENSOR_DEVICE_ATTR_2_RO(fan4_input, fan, 3, input);
361 static SENSOR_DEVICE_ATTR_2_RW(fan4_min, fan, 3, min);
362 static SENSOR_DEVICE_ATTR_2_RO(fan5_input, fan, 4, input);
363 static SENSOR_DEVICE_ATTR_2_RW(fan5_min, fan, 4, min);
365 static struct attribute *emc6w201_attrs[] = {
366 &sensor_dev_attr_in0_input.dev_attr.attr,
367 &sensor_dev_attr_in0_min.dev_attr.attr,
368 &sensor_dev_attr_in0_max.dev_attr.attr,
369 &sensor_dev_attr_in1_input.dev_attr.attr,
370 &sensor_dev_attr_in1_min.dev_attr.attr,
371 &sensor_dev_attr_in1_max.dev_attr.attr,
372 &sensor_dev_attr_in2_input.dev_attr.attr,
373 &sensor_dev_attr_in2_min.dev_attr.attr,
374 &sensor_dev_attr_in2_max.dev_attr.attr,
375 &sensor_dev_attr_in3_input.dev_attr.attr,
376 &sensor_dev_attr_in3_min.dev_attr.attr,
377 &sensor_dev_attr_in3_max.dev_attr.attr,
378 &sensor_dev_attr_in4_input.dev_attr.attr,
379 &sensor_dev_attr_in4_min.dev_attr.attr,
380 &sensor_dev_attr_in4_max.dev_attr.attr,
381 &sensor_dev_attr_in5_input.dev_attr.attr,
382 &sensor_dev_attr_in5_min.dev_attr.attr,
383 &sensor_dev_attr_in5_max.dev_attr.attr,
385 &sensor_dev_attr_temp1_input.dev_attr.attr,
386 &sensor_dev_attr_temp1_min.dev_attr.attr,
387 &sensor_dev_attr_temp1_max.dev_attr.attr,
388 &sensor_dev_attr_temp2_input.dev_attr.attr,
389 &sensor_dev_attr_temp2_min.dev_attr.attr,
390 &sensor_dev_attr_temp2_max.dev_attr.attr,
391 &sensor_dev_attr_temp3_input.dev_attr.attr,
392 &sensor_dev_attr_temp3_min.dev_attr.attr,
393 &sensor_dev_attr_temp3_max.dev_attr.attr,
394 &sensor_dev_attr_temp4_input.dev_attr.attr,
395 &sensor_dev_attr_temp4_min.dev_attr.attr,
396 &sensor_dev_attr_temp4_max.dev_attr.attr,
397 &sensor_dev_attr_temp5_input.dev_attr.attr,
398 &sensor_dev_attr_temp5_min.dev_attr.attr,
399 &sensor_dev_attr_temp5_max.dev_attr.attr,
400 &sensor_dev_attr_temp6_input.dev_attr.attr,
401 &sensor_dev_attr_temp6_min.dev_attr.attr,
402 &sensor_dev_attr_temp6_max.dev_attr.attr,
404 &sensor_dev_attr_fan1_input.dev_attr.attr,
405 &sensor_dev_attr_fan1_min.dev_attr.attr,
406 &sensor_dev_attr_fan2_input.dev_attr.attr,
407 &sensor_dev_attr_fan2_min.dev_attr.attr,
408 &sensor_dev_attr_fan3_input.dev_attr.attr,
409 &sensor_dev_attr_fan3_min.dev_attr.attr,
410 &sensor_dev_attr_fan4_input.dev_attr.attr,
411 &sensor_dev_attr_fan4_min.dev_attr.attr,
412 &sensor_dev_attr_fan5_input.dev_attr.attr,
413 &sensor_dev_attr_fan5_min.dev_attr.attr,
417 ATTRIBUTE_GROUPS(emc6w201);
423 /* Return 0 if detection is successful, -ENODEV otherwise */
424 static int emc6w201_detect(struct i2c_client *client,
425 struct i2c_board_info *info)
427 struct i2c_adapter *adapter = client->adapter;
428 int company, verstep, config;
430 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
434 company = i2c_smbus_read_byte_data(client, EMC6W201_REG_COMPANY);
437 verstep = i2c_smbus_read_byte_data(client, EMC6W201_REG_VERSTEP);
438 if (verstep < 0 || (verstep & 0xF0) != 0xB0)
440 if ((verstep & 0x0F) > 2) {
441 dev_dbg(&client->dev, "Unknown EMC6W201 stepping %d\n",
446 /* Check configuration */
447 config = i2c_smbus_read_byte_data(client, EMC6W201_REG_CONFIG);
448 if (config < 0 || (config & 0xF4) != 0x04)
450 if (!(config & 0x01)) {
451 dev_err(&client->dev, "Monitoring not enabled\n");
455 strlcpy(info->type, "emc6w201", I2C_NAME_SIZE);
460 static int emc6w201_probe(struct i2c_client *client,
461 const struct i2c_device_id *id)
463 struct device *dev = &client->dev;
464 struct emc6w201_data *data;
465 struct device *hwmon_dev;
467 data = devm_kzalloc(dev, sizeof(struct emc6w201_data), GFP_KERNEL);
471 data->client = client;
472 mutex_init(&data->update_lock);
474 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
477 return PTR_ERR_OR_ZERO(hwmon_dev);
480 static const struct i2c_device_id emc6w201_id[] = {
484 MODULE_DEVICE_TABLE(i2c, emc6w201_id);
486 static struct i2c_driver emc6w201_driver = {
487 .class = I2C_CLASS_HWMON,
491 .probe = emc6w201_probe,
492 .id_table = emc6w201_id,
493 .detect = emc6w201_detect,
494 .address_list = normal_i2c,
497 module_i2c_driver(emc6w201_driver);
499 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
500 MODULE_DESCRIPTION("SMSC EMC6W201 hardware monitoring driver");
501 MODULE_LICENSE("GPL");