blob: 29082c8463f4984fe31ef3a77c055c78a3557566 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jean Delvareb0b349a2011-05-25 20:43:33 +02002/*
3 * emc6w201.c - Hardware monitoring driver for the SMSC EMC6W201
Jean Delvare7c81c60f2014-01-29 20:40:08 +01004 * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
Jean Delvareb0b349a2011-05-25 20:43:33 +02005 */
6
7#include <linux/module.h>
Jean Delvareb0b349a2011-05-25 20:43:33 +02008#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/jiffies.h>
11#include <linux/i2c.h>
12#include <linux/hwmon.h>
13#include <linux/hwmon-sysfs.h>
14#include <linux/err.h>
15#include <linux/mutex.h>
16
17/*
18 * Addresses to scan
19 */
20
21static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
22
23/*
24 * The EMC6W201 registers
25 */
26
27#define EMC6W201_REG_IN(nr) (0x20 + (nr))
28#define EMC6W201_REG_TEMP(nr) (0x26 + (nr))
29#define EMC6W201_REG_FAN(nr) (0x2C + (nr) * 2)
30#define EMC6W201_REG_COMPANY 0x3E
31#define EMC6W201_REG_VERSTEP 0x3F
32#define EMC6W201_REG_CONFIG 0x40
33#define EMC6W201_REG_IN_LOW(nr) (0x4A + (nr) * 2)
34#define EMC6W201_REG_IN_HIGH(nr) (0x4B + (nr) * 2)
35#define EMC6W201_REG_TEMP_LOW(nr) (0x56 + (nr) * 2)
36#define EMC6W201_REG_TEMP_HIGH(nr) (0x57 + (nr) * 2)
37#define EMC6W201_REG_FAN_MIN(nr) (0x62 + (nr) * 2)
38
Guenter Roeck94e44412013-09-06 14:05:43 +020039enum subfeature { input, min, max };
Jean Delvareb0b349a2011-05-25 20:43:33 +020040
41/*
42 * Per-device data
43 */
44
45struct emc6w201_data {
Axel Lin7b3dadc2014-06-29 11:32:01 +080046 struct i2c_client *client;
Jean Delvareb0b349a2011-05-25 20:43:33 +020047 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +030048 bool valid; /* false until following fields are valid */
Jean Delvareb0b349a2011-05-25 20:43:33 +020049 unsigned long last_updated; /* in jiffies */
50
51 /* registers values */
52 u8 in[3][6];
53 s8 temp[3][6];
54 u16 fan[2][5];
55};
56
57/*
58 * Combine LSB and MSB registers in a single value
59 * Locking: must be called with data->update_lock held
60 */
61static u16 emc6w201_read16(struct i2c_client *client, u8 reg)
62{
63 int lsb, msb;
64
65 lsb = i2c_smbus_read_byte_data(client, reg);
66 msb = i2c_smbus_read_byte_data(client, reg + 1);
Jean Delvareb6b2a1e2011-07-03 13:32:53 +020067 if (unlikely(lsb < 0 || msb < 0)) {
68 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
69 16, "read", reg);
Jean Delvareb0b349a2011-05-25 20:43:33 +020070 return 0xFFFF; /* Arbitrary value */
71 }
72
73 return (msb << 8) | lsb;
74}
75
76/*
77 * Write 16-bit value to LSB and MSB registers
78 * Locking: must be called with data->update_lock held
79 */
80static int emc6w201_write16(struct i2c_client *client, u8 reg, u16 val)
81{
82 int err;
83
84 err = i2c_smbus_write_byte_data(client, reg, val & 0xff);
Jean Delvareb6b2a1e2011-07-03 13:32:53 +020085 if (likely(!err))
Jean Delvareb0b349a2011-05-25 20:43:33 +020086 err = i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
Jean Delvareb6b2a1e2011-07-03 13:32:53 +020087 if (unlikely(err < 0))
88 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
89 16, "write", reg);
90
91 return err;
92}
93
94/* Read 8-bit value from register */
95static u8 emc6w201_read8(struct i2c_client *client, u8 reg)
96{
97 int val;
98
99 val = i2c_smbus_read_byte_data(client, reg);
100 if (unlikely(val < 0)) {
101 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
102 8, "read", reg);
103 return 0x00; /* Arbitrary value */
104 }
105
106 return val;
107}
108
109/* Write 8-bit value to register */
110static int emc6w201_write8(struct i2c_client *client, u8 reg, u8 val)
111{
112 int err;
113
114 err = i2c_smbus_write_byte_data(client, reg, val);
115 if (unlikely(err < 0))
116 dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
117 8, "write", reg);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200118
119 return err;
120}
121
122static struct emc6w201_data *emc6w201_update_device(struct device *dev)
123{
Axel Lin7b3dadc2014-06-29 11:32:01 +0800124 struct emc6w201_data *data = dev_get_drvdata(dev);
125 struct i2c_client *client = data->client;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200126 int nr;
127
128 mutex_lock(&data->update_lock);
129
130 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
131 for (nr = 0; nr < 6; nr++) {
132 data->in[input][nr] =
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200133 emc6w201_read8(client,
Jean Delvareb0b349a2011-05-25 20:43:33 +0200134 EMC6W201_REG_IN(nr));
135 data->in[min][nr] =
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200136 emc6w201_read8(client,
Jean Delvareb0b349a2011-05-25 20:43:33 +0200137 EMC6W201_REG_IN_LOW(nr));
138 data->in[max][nr] =
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200139 emc6w201_read8(client,
Jean Delvareb0b349a2011-05-25 20:43:33 +0200140 EMC6W201_REG_IN_HIGH(nr));
141 }
142
143 for (nr = 0; nr < 6; nr++) {
144 data->temp[input][nr] =
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200145 emc6w201_read8(client,
Jean Delvareb0b349a2011-05-25 20:43:33 +0200146 EMC6W201_REG_TEMP(nr));
147 data->temp[min][nr] =
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200148 emc6w201_read8(client,
Jean Delvareb0b349a2011-05-25 20:43:33 +0200149 EMC6W201_REG_TEMP_LOW(nr));
150 data->temp[max][nr] =
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200151 emc6w201_read8(client,
Jean Delvareb0b349a2011-05-25 20:43:33 +0200152 EMC6W201_REG_TEMP_HIGH(nr));
153 }
154
155 for (nr = 0; nr < 5; nr++) {
156 data->fan[input][nr] =
157 emc6w201_read16(client,
158 EMC6W201_REG_FAN(nr));
159 data->fan[min][nr] =
160 emc6w201_read16(client,
161 EMC6W201_REG_FAN_MIN(nr));
162 }
163
164 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +0300165 data->valid = true;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200166 }
167
168 mutex_unlock(&data->update_lock);
169
170 return data;
171}
172
173/*
174 * Sysfs callback functions
175 */
176
Guenter Roeck86266ca2012-12-18 18:16:08 -0800177static const s16 nominal_mv[6] = { 2500, 1500, 3300, 5000, 1500, 1500 };
Jean Delvareb0b349a2011-05-25 20:43:33 +0200178
Guenter Roeck7a61d712018-12-10 14:02:06 -0800179static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
180 char *buf)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200181{
182 struct emc6w201_data *data = emc6w201_update_device(dev);
183 int sf = to_sensor_dev_attr_2(devattr)->index;
184 int nr = to_sensor_dev_attr_2(devattr)->nr;
185
186 return sprintf(buf, "%u\n",
187 (unsigned)data->in[sf][nr] * nominal_mv[nr] / 0xC0);
188}
189
Guenter Roeck7a61d712018-12-10 14:02:06 -0800190static ssize_t in_store(struct device *dev, struct device_attribute *devattr,
191 const char *buf, size_t count)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200192{
Axel Lin7b3dadc2014-06-29 11:32:01 +0800193 struct emc6w201_data *data = dev_get_drvdata(dev);
194 struct i2c_client *client = data->client;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200195 int sf = to_sensor_dev_attr_2(devattr)->index;
196 int nr = to_sensor_dev_attr_2(devattr)->nr;
197 int err;
198 long val;
199 u8 reg;
200
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100201 err = kstrtol(buf, 10, &val);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200202 if (err < 0)
203 return err;
204
Guenter Roeck59715f42016-12-04 18:20:52 -0800205 val = clamp_val(val, 0, 255 * nominal_mv[nr] / 192);
206 val = DIV_ROUND_CLOSEST(val * 192, nominal_mv[nr]);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200207 reg = (sf == min) ? EMC6W201_REG_IN_LOW(nr)
208 : EMC6W201_REG_IN_HIGH(nr);
209
210 mutex_lock(&data->update_lock);
Guenter Roeck59715f42016-12-04 18:20:52 -0800211 data->in[sf][nr] = val;
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200212 err = emc6w201_write8(client, reg, data->in[sf][nr]);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200213 mutex_unlock(&data->update_lock);
214
215 return err < 0 ? err : count;
216}
217
Guenter Roeck7a61d712018-12-10 14:02:06 -0800218static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
219 char *buf)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200220{
221 struct emc6w201_data *data = emc6w201_update_device(dev);
222 int sf = to_sensor_dev_attr_2(devattr)->index;
223 int nr = to_sensor_dev_attr_2(devattr)->nr;
224
225 return sprintf(buf, "%d\n", (int)data->temp[sf][nr] * 1000);
226}
227
Guenter Roeck7a61d712018-12-10 14:02:06 -0800228static ssize_t temp_store(struct device *dev,
229 struct device_attribute *devattr, const char *buf,
230 size_t count)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200231{
Axel Lin7b3dadc2014-06-29 11:32:01 +0800232 struct emc6w201_data *data = dev_get_drvdata(dev);
233 struct i2c_client *client = data->client;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200234 int sf = to_sensor_dev_attr_2(devattr)->index;
235 int nr = to_sensor_dev_attr_2(devattr)->nr;
236 int err;
237 long val;
238 u8 reg;
239
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100240 err = kstrtol(buf, 10, &val);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200241 if (err < 0)
242 return err;
243
Guenter Roeck59715f42016-12-04 18:20:52 -0800244 val = clamp_val(val, -127000, 127000);
Guenter Roeck539a7192014-08-05 09:54:04 -0700245 val = DIV_ROUND_CLOSEST(val, 1000);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200246 reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr)
247 : EMC6W201_REG_TEMP_HIGH(nr);
248
249 mutex_lock(&data->update_lock);
Guenter Roeck59715f42016-12-04 18:20:52 -0800250 data->temp[sf][nr] = val;
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200251 err = emc6w201_write8(client, reg, data->temp[sf][nr]);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200252 mutex_unlock(&data->update_lock);
253
254 return err < 0 ? err : count;
255}
256
Guenter Roeck7a61d712018-12-10 14:02:06 -0800257static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
258 char *buf)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200259{
260 struct emc6w201_data *data = emc6w201_update_device(dev);
261 int sf = to_sensor_dev_attr_2(devattr)->index;
262 int nr = to_sensor_dev_attr_2(devattr)->nr;
263 unsigned rpm;
264
265 if (data->fan[sf][nr] == 0 || data->fan[sf][nr] == 0xFFFF)
266 rpm = 0;
267 else
268 rpm = 5400000U / data->fan[sf][nr];
269
270 return sprintf(buf, "%u\n", rpm);
271}
272
Guenter Roeck7a61d712018-12-10 14:02:06 -0800273static ssize_t fan_store(struct device *dev, struct device_attribute *devattr,
274 const char *buf, size_t count)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200275{
Axel Lin7b3dadc2014-06-29 11:32:01 +0800276 struct emc6w201_data *data = dev_get_drvdata(dev);
277 struct i2c_client *client = data->client;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200278 int sf = to_sensor_dev_attr_2(devattr)->index;
279 int nr = to_sensor_dev_attr_2(devattr)->nr;
280 int err;
281 unsigned long val;
282
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100283 err = kstrtoul(buf, 10, &val);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200284 if (err < 0)
285 return err;
286
287 if (val == 0) {
288 val = 0xFFFF;
289 } else {
290 val = DIV_ROUND_CLOSEST(5400000U, val);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800291 val = clamp_val(val, 0, 0xFFFE);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200292 }
293
294 mutex_lock(&data->update_lock);
295 data->fan[sf][nr] = val;
296 err = emc6w201_write16(client, EMC6W201_REG_FAN_MIN(nr),
297 data->fan[sf][nr]);
298 mutex_unlock(&data->update_lock);
299
300 return err < 0 ? err : count;
301}
302
Guenter Roeck7a61d712018-12-10 14:02:06 -0800303static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, input);
304static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, min);
305static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, max);
306static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, input);
307static SENSOR_DEVICE_ATTR_2_RW(in1_min, in, 1, min);
308static SENSOR_DEVICE_ATTR_2_RW(in1_max, in, 1, max);
309static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, input);
310static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, min);
311static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, max);
312static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, input);
313static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, min);
314static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, max);
315static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, input);
316static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, min);
317static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, max);
318static SENSOR_DEVICE_ATTR_2_RO(in5_input, in, 5, input);
319static SENSOR_DEVICE_ATTR_2_RW(in5_min, in, 5, min);
320static SENSOR_DEVICE_ATTR_2_RW(in5_max, in, 5, max);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200321
Guenter Roeck7a61d712018-12-10 14:02:06 -0800322static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, input);
323static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0, min);
324static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0, max);
325static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 1, input);
326static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, min);
327static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 1, max);
328static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 2, input);
329static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 2, min);
330static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, max);
331static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 3, input);
332static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 3, min);
333static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 3, max);
334static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 4, input);
335static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 4, min);
336static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 4, max);
337static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 5, input);
338static SENSOR_DEVICE_ATTR_2_RW(temp6_min, temp, 5, min);
339static SENSOR_DEVICE_ATTR_2_RW(temp6_max, temp, 5, max);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200340
Guenter Roeck7a61d712018-12-10 14:02:06 -0800341static SENSOR_DEVICE_ATTR_2_RO(fan1_input, fan, 0, input);
342static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan, 0, min);
343static SENSOR_DEVICE_ATTR_2_RO(fan2_input, fan, 1, input);
344static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan, 1, min);
345static SENSOR_DEVICE_ATTR_2_RO(fan3_input, fan, 2, input);
346static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan, 2, min);
347static SENSOR_DEVICE_ATTR_2_RO(fan4_input, fan, 3, input);
348static SENSOR_DEVICE_ATTR_2_RW(fan4_min, fan, 3, min);
349static SENSOR_DEVICE_ATTR_2_RO(fan5_input, fan, 4, input);
350static SENSOR_DEVICE_ATTR_2_RW(fan5_min, fan, 4, min);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200351
Axel Lin7b3dadc2014-06-29 11:32:01 +0800352static struct attribute *emc6w201_attrs[] = {
Jean Delvareb0b349a2011-05-25 20:43:33 +0200353 &sensor_dev_attr_in0_input.dev_attr.attr,
354 &sensor_dev_attr_in0_min.dev_attr.attr,
355 &sensor_dev_attr_in0_max.dev_attr.attr,
356 &sensor_dev_attr_in1_input.dev_attr.attr,
357 &sensor_dev_attr_in1_min.dev_attr.attr,
358 &sensor_dev_attr_in1_max.dev_attr.attr,
359 &sensor_dev_attr_in2_input.dev_attr.attr,
360 &sensor_dev_attr_in2_min.dev_attr.attr,
361 &sensor_dev_attr_in2_max.dev_attr.attr,
362 &sensor_dev_attr_in3_input.dev_attr.attr,
363 &sensor_dev_attr_in3_min.dev_attr.attr,
364 &sensor_dev_attr_in3_max.dev_attr.attr,
365 &sensor_dev_attr_in4_input.dev_attr.attr,
366 &sensor_dev_attr_in4_min.dev_attr.attr,
367 &sensor_dev_attr_in4_max.dev_attr.attr,
368 &sensor_dev_attr_in5_input.dev_attr.attr,
369 &sensor_dev_attr_in5_min.dev_attr.attr,
370 &sensor_dev_attr_in5_max.dev_attr.attr,
371
372 &sensor_dev_attr_temp1_input.dev_attr.attr,
373 &sensor_dev_attr_temp1_min.dev_attr.attr,
374 &sensor_dev_attr_temp1_max.dev_attr.attr,
375 &sensor_dev_attr_temp2_input.dev_attr.attr,
376 &sensor_dev_attr_temp2_min.dev_attr.attr,
377 &sensor_dev_attr_temp2_max.dev_attr.attr,
378 &sensor_dev_attr_temp3_input.dev_attr.attr,
379 &sensor_dev_attr_temp3_min.dev_attr.attr,
380 &sensor_dev_attr_temp3_max.dev_attr.attr,
381 &sensor_dev_attr_temp4_input.dev_attr.attr,
382 &sensor_dev_attr_temp4_min.dev_attr.attr,
383 &sensor_dev_attr_temp4_max.dev_attr.attr,
384 &sensor_dev_attr_temp5_input.dev_attr.attr,
385 &sensor_dev_attr_temp5_min.dev_attr.attr,
386 &sensor_dev_attr_temp5_max.dev_attr.attr,
387 &sensor_dev_attr_temp6_input.dev_attr.attr,
388 &sensor_dev_attr_temp6_min.dev_attr.attr,
389 &sensor_dev_attr_temp6_max.dev_attr.attr,
390
391 &sensor_dev_attr_fan1_input.dev_attr.attr,
392 &sensor_dev_attr_fan1_min.dev_attr.attr,
393 &sensor_dev_attr_fan2_input.dev_attr.attr,
394 &sensor_dev_attr_fan2_min.dev_attr.attr,
395 &sensor_dev_attr_fan3_input.dev_attr.attr,
396 &sensor_dev_attr_fan3_min.dev_attr.attr,
397 &sensor_dev_attr_fan4_input.dev_attr.attr,
398 &sensor_dev_attr_fan4_min.dev_attr.attr,
399 &sensor_dev_attr_fan5_input.dev_attr.attr,
400 &sensor_dev_attr_fan5_min.dev_attr.attr,
401 NULL
402};
403
Axel Lin7b3dadc2014-06-29 11:32:01 +0800404ATTRIBUTE_GROUPS(emc6w201);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200405
406/*
407 * Driver interface
408 */
409
410/* Return 0 if detection is successful, -ENODEV otherwise */
411static int emc6w201_detect(struct i2c_client *client,
412 struct i2c_board_info *info)
413{
414 struct i2c_adapter *adapter = client->adapter;
415 int company, verstep, config;
416
417 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
418 return -ENODEV;
419
420 /* Identification */
421 company = i2c_smbus_read_byte_data(client, EMC6W201_REG_COMPANY);
422 if (company != 0x5C)
423 return -ENODEV;
424 verstep = i2c_smbus_read_byte_data(client, EMC6W201_REG_VERSTEP);
425 if (verstep < 0 || (verstep & 0xF0) != 0xB0)
426 return -ENODEV;
427 if ((verstep & 0x0F) > 2) {
Colin Ian King51b8c2c2016-06-24 18:31:32 +0100428 dev_dbg(&client->dev, "Unknown EMC6W201 stepping %d\n",
Jean Delvareb0b349a2011-05-25 20:43:33 +0200429 verstep & 0x0F);
430 return -ENODEV;
431 }
432
433 /* Check configuration */
434 config = i2c_smbus_read_byte_data(client, EMC6W201_REG_CONFIG);
Jean Delvareb6b2a1e2011-07-03 13:32:53 +0200435 if (config < 0 || (config & 0xF4) != 0x04)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200436 return -ENODEV;
437 if (!(config & 0x01)) {
438 dev_err(&client->dev, "Monitoring not enabled\n");
439 return -ENODEV;
440 }
441
442 strlcpy(info->type, "emc6w201", I2C_NAME_SIZE);
443
444 return 0;
445}
446
Stephen Kitt67487032020-08-13 18:02:22 +0200447static int emc6w201_probe(struct i2c_client *client)
Jean Delvareb0b349a2011-05-25 20:43:33 +0200448{
Axel Lin7b3dadc2014-06-29 11:32:01 +0800449 struct device *dev = &client->dev;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200450 struct emc6w201_data *data;
Axel Lin7b3dadc2014-06-29 11:32:01 +0800451 struct device *hwmon_dev;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200452
Axel Lin7b3dadc2014-06-29 11:32:01 +0800453 data = devm_kzalloc(dev, sizeof(struct emc6w201_data), GFP_KERNEL);
Guenter Roeck6ecffe12012-06-02 09:58:04 -0700454 if (!data)
455 return -ENOMEM;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200456
Axel Lin7b3dadc2014-06-29 11:32:01 +0800457 data->client = client;
Jean Delvareb0b349a2011-05-25 20:43:33 +0200458 mutex_init(&data->update_lock);
459
Axel Lin7b3dadc2014-06-29 11:32:01 +0800460 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
461 data,
462 emc6w201_groups);
463 return PTR_ERR_OR_ZERO(hwmon_dev);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200464}
465
466static const struct i2c_device_id emc6w201_id[] = {
467 { "emc6w201", 0 },
468 { }
469};
470MODULE_DEVICE_TABLE(i2c, emc6w201_id);
471
472static struct i2c_driver emc6w201_driver = {
473 .class = I2C_CLASS_HWMON,
474 .driver = {
475 .name = "emc6w201",
476 },
Stephen Kitt67487032020-08-13 18:02:22 +0200477 .probe_new = emc6w201_probe,
Jean Delvareb0b349a2011-05-25 20:43:33 +0200478 .id_table = emc6w201_id,
479 .detect = emc6w201_detect,
480 .address_list = normal_i2c,
481};
482
Axel Linf0967ee2012-01-20 15:38:18 +0800483module_i2c_driver(emc6w201_driver);
Jean Delvareb0b349a2011-05-25 20:43:33 +0200484
Jean Delvare7c81c60f2014-01-29 20:40:08 +0100485MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Jean Delvareb0b349a2011-05-25 20:43:33 +0200486MODULE_DESCRIPTION("SMSC EMC6W201 hardware monitoring driver");
487MODULE_LICENSE("GPL");