Commit | Line | Data |
---|---|---|
3434f378 GR |
1 | /* |
2 | * nct7802 - Driver for Nuvoton NCT7802Y | |
3 | * | |
4 | * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | */ | |
16 | ||
17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
18 | ||
19 | #include <linux/err.h> | |
20 | #include <linux/i2c.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/hwmon.h> | |
23 | #include <linux/hwmon-sysfs.h> | |
24 | #include <linux/jiffies.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/mutex.h> | |
27 | #include <linux/regmap.h> | |
28 | #include <linux/slab.h> | |
29 | ||
30 | #define DRVNAME "nct7802" | |
31 | ||
32 | static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e }; | |
33 | ||
34 | static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = { | |
35 | { 0x40, 0x00, 0x42, 0x44, 0x46 }, | |
36 | { 0x3f, 0x00, 0x41, 0x43, 0x45 }, | |
37 | }; | |
38 | ||
39 | static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 }; | |
40 | ||
41 | static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = { | |
42 | { 0, 0, 4, 0, 4 }, | |
43 | { 2, 0, 6, 2, 6 }, | |
44 | }; | |
45 | ||
46 | #define REG_BANK 0x00 | |
47 | #define REG_TEMP_LSB 0x05 | |
48 | #define REG_TEMP_PECI_LSB 0x08 | |
49 | #define REG_VOLTAGE_LOW 0x0f | |
50 | #define REG_FANCOUNT_LOW 0x13 | |
51 | #define REG_START 0x21 | |
fcdc5739 | 52 | #define REG_MODE 0x22 /* 7.2.32 Mode Selection Register */ |
3434f378 GR |
53 | #define REG_PECI_ENABLE 0x23 |
54 | #define REG_FAN_ENABLE 0x24 | |
55 | #define REG_VMON_ENABLE 0x25 | |
56 | #define REG_VENDOR_ID 0xfd | |
57 | #define REG_CHIP_ID 0xfe | |
58 | #define REG_VERSION_ID 0xff | |
59 | ||
60 | /* | |
61 | * Data structures and manipulation thereof | |
62 | */ | |
63 | ||
64 | struct nct7802_data { | |
65 | struct regmap *regmap; | |
66 | struct mutex access_lock; /* for multi-byte read and write operations */ | |
67 | }; | |
68 | ||
fcdc5739 CS |
69 | static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr, |
70 | char *buf) | |
71 | { | |
72 | struct nct7802_data *data = dev_get_drvdata(dev); | |
73 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); | |
74 | unsigned int mode; | |
75 | int ret; | |
76 | ||
77 | ret = regmap_read(data->regmap, REG_MODE, &mode); | |
78 | if (ret < 0) | |
79 | return ret; | |
80 | ||
81 | return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2); | |
82 | } | |
83 | ||
84 | static ssize_t store_temp_type(struct device *dev, | |
85 | struct device_attribute *attr, | |
86 | const char *buf, size_t count) | |
87 | { | |
88 | struct nct7802_data *data = dev_get_drvdata(dev); | |
89 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); | |
90 | unsigned int type; | |
91 | int err; | |
92 | ||
93 | err = kstrtouint(buf, 0, &type); | |
94 | if (err < 0) | |
95 | return err; | |
96 | if (sattr->index == 2 && type != 4) /* RD3 */ | |
97 | return -EINVAL; | |
98 | if (type < 3 || type > 4) | |
99 | return -EINVAL; | |
100 | err = regmap_update_bits(data->regmap, REG_MODE, | |
101 | 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index); | |
102 | return err ? : count; | |
103 | } | |
104 | ||
ea33597c CS |
105 | static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, |
106 | char *buf) | |
107 | { | |
108 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
109 | struct nct7802_data *data = dev_get_drvdata(dev); | |
110 | unsigned int val; | |
111 | int ret; | |
112 | ||
113 | ret = regmap_read(data->regmap, attr->index, &val); | |
114 | if (ret < 0) | |
115 | return ret; | |
116 | ||
117 | return sprintf(buf, "%d\n", val); | |
118 | } | |
119 | ||
120 | static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr, | |
121 | const char *buf, size_t count) | |
122 | { | |
123 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
124 | struct nct7802_data *data = dev_get_drvdata(dev); | |
125 | int err; | |
126 | u8 val; | |
127 | ||
128 | err = kstrtou8(buf, 0, &val); | |
129 | if (err < 0) | |
130 | return err; | |
131 | ||
132 | err = regmap_write(data->regmap, attr->index, val); | |
133 | return err ? : count; | |
134 | } | |
fcdc5739 | 135 | |
3434f378 GR |
136 | static int nct7802_read_temp(struct nct7802_data *data, |
137 | u8 reg_temp, u8 reg_temp_low, int *temp) | |
138 | { | |
139 | unsigned int t1, t2 = 0; | |
140 | int err; | |
141 | ||
142 | *temp = 0; | |
143 | ||
144 | mutex_lock(&data->access_lock); | |
145 | err = regmap_read(data->regmap, reg_temp, &t1); | |
146 | if (err < 0) | |
147 | goto abort; | |
148 | t1 <<= 8; | |
149 | if (reg_temp_low) { /* 11 bit data */ | |
150 | err = regmap_read(data->regmap, reg_temp_low, &t2); | |
151 | if (err < 0) | |
152 | goto abort; | |
153 | } | |
154 | t1 |= t2 & 0xe0; | |
155 | *temp = (s16)t1 / 32 * 125; | |
156 | abort: | |
157 | mutex_unlock(&data->access_lock); | |
158 | return err; | |
159 | } | |
160 | ||
161 | static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan) | |
162 | { | |
163 | unsigned int f1, f2; | |
164 | int ret; | |
165 | ||
166 | mutex_lock(&data->access_lock); | |
167 | ret = regmap_read(data->regmap, reg_fan, &f1); | |
168 | if (ret < 0) | |
169 | goto abort; | |
170 | ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2); | |
171 | if (ret < 0) | |
172 | goto abort; | |
173 | ret = (f1 << 5) | (f2 >> 3); | |
174 | /* convert fan count to rpm */ | |
175 | if (ret == 0x1fff) /* maximum value, assume fan is stopped */ | |
176 | ret = 0; | |
177 | else if (ret) | |
178 | ret = DIV_ROUND_CLOSEST(1350000U, ret); | |
179 | abort: | |
180 | mutex_unlock(&data->access_lock); | |
181 | return ret; | |
182 | } | |
183 | ||
184 | static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low, | |
185 | u8 reg_fan_high) | |
186 | { | |
187 | unsigned int f1, f2; | |
188 | int ret; | |
189 | ||
190 | mutex_lock(&data->access_lock); | |
191 | ret = regmap_read(data->regmap, reg_fan_low, &f1); | |
192 | if (ret < 0) | |
193 | goto abort; | |
194 | ret = regmap_read(data->regmap, reg_fan_high, &f2); | |
195 | if (ret < 0) | |
196 | goto abort; | |
197 | ret = f1 | ((f2 & 0xf8) << 5); | |
198 | /* convert fan count to rpm */ | |
199 | if (ret == 0x1fff) /* maximum value, assume no limit */ | |
200 | ret = 0; | |
201 | else if (ret) | |
202 | ret = DIV_ROUND_CLOSEST(1350000U, ret); | |
203 | abort: | |
204 | mutex_unlock(&data->access_lock); | |
205 | return ret; | |
206 | } | |
207 | ||
208 | static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low, | |
209 | u8 reg_fan_high, unsigned int limit) | |
210 | { | |
211 | int err; | |
212 | ||
213 | if (limit) | |
214 | limit = DIV_ROUND_CLOSEST(1350000U, limit); | |
215 | else | |
216 | limit = 0x1fff; | |
217 | limit = clamp_val(limit, 0, 0x1fff); | |
218 | ||
219 | mutex_lock(&data->access_lock); | |
220 | err = regmap_write(data->regmap, reg_fan_low, limit & 0xff); | |
221 | if (err < 0) | |
222 | goto abort; | |
223 | ||
224 | err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5); | |
225 | abort: | |
226 | mutex_unlock(&data->access_lock); | |
227 | return err; | |
228 | } | |
229 | ||
230 | static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 }; | |
231 | ||
232 | static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index) | |
233 | { | |
234 | unsigned int v1, v2; | |
235 | int ret; | |
236 | ||
237 | mutex_lock(&data->access_lock); | |
238 | if (index == 0) { /* voltage */ | |
239 | ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1); | |
240 | if (ret < 0) | |
241 | goto abort; | |
242 | ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2); | |
243 | if (ret < 0) | |
244 | goto abort; | |
245 | ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr]; | |
246 | } else { /* limit */ | |
247 | int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; | |
248 | ||
249 | ret = regmap_read(data->regmap, | |
250 | REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1); | |
251 | if (ret < 0) | |
252 | goto abort; | |
253 | ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], | |
254 | &v2); | |
255 | if (ret < 0) | |
256 | goto abort; | |
257 | ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr]; | |
258 | } | |
259 | abort: | |
260 | mutex_unlock(&data->access_lock); | |
261 | return ret; | |
262 | } | |
263 | ||
264 | static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index, | |
9200bc4c | 265 | unsigned long voltage) |
3434f378 GR |
266 | { |
267 | int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; | |
268 | int err; | |
269 | ||
270 | voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]); | |
271 | voltage = clamp_val(voltage, 0, 0x3ff); | |
272 | ||
273 | mutex_lock(&data->access_lock); | |
274 | err = regmap_write(data->regmap, | |
275 | REG_VOLTAGE_LIMIT_LSB[index - 1][nr], | |
276 | voltage & 0xff); | |
277 | if (err < 0) | |
278 | goto abort; | |
279 | ||
280 | err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], | |
281 | 0x0300 >> shift, (voltage & 0x0300) >> shift); | |
282 | abort: | |
283 | mutex_unlock(&data->access_lock); | |
284 | return err; | |
285 | } | |
286 | ||
287 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, | |
288 | char *buf) | |
289 | { | |
290 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
291 | struct nct7802_data *data = dev_get_drvdata(dev); | |
292 | int voltage; | |
293 | ||
294 | voltage = nct7802_read_voltage(data, sattr->nr, sattr->index); | |
295 | if (voltage < 0) | |
296 | return voltage; | |
297 | ||
298 | return sprintf(buf, "%d\n", voltage); | |
299 | } | |
300 | ||
301 | static ssize_t store_in(struct device *dev, struct device_attribute *attr, | |
302 | const char *buf, size_t count) | |
303 | { | |
304 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
305 | struct nct7802_data *data = dev_get_drvdata(dev); | |
306 | int index = sattr->index; | |
307 | int nr = sattr->nr; | |
308 | unsigned long val; | |
309 | int err; | |
310 | ||
311 | err = kstrtoul(buf, 10, &val); | |
312 | if (err < 0) | |
313 | return err; | |
314 | ||
315 | err = nct7802_write_voltage(data, nr, index, val); | |
316 | return err ? : count; | |
317 | } | |
318 | ||
319 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, | |
320 | char *buf) | |
321 | { | |
322 | struct nct7802_data *data = dev_get_drvdata(dev); | |
323 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
324 | int err, temp; | |
325 | ||
326 | err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp); | |
327 | if (err < 0) | |
328 | return err; | |
329 | ||
330 | return sprintf(buf, "%d\n", temp); | |
331 | } | |
332 | ||
333 | static ssize_t store_temp(struct device *dev, struct device_attribute *attr, | |
334 | const char *buf, size_t count) | |
335 | { | |
336 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
337 | struct nct7802_data *data = dev_get_drvdata(dev); | |
338 | int nr = sattr->nr; | |
339 | long val; | |
340 | int err; | |
341 | ||
342 | err = kstrtol(buf, 10, &val); | |
343 | if (err < 0) | |
344 | return err; | |
345 | ||
346 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); | |
347 | ||
348 | err = regmap_write(data->regmap, nr, val & 0xff); | |
349 | return err ? : count; | |
350 | } | |
351 | ||
352 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, | |
353 | char *buf) | |
354 | { | |
355 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); | |
356 | struct nct7802_data *data = dev_get_drvdata(dev); | |
357 | int speed; | |
358 | ||
359 | speed = nct7802_read_fan(data, sattr->index); | |
360 | if (speed < 0) | |
361 | return speed; | |
362 | ||
363 | return sprintf(buf, "%d\n", speed); | |
364 | } | |
365 | ||
366 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, | |
367 | char *buf) | |
368 | { | |
369 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
370 | struct nct7802_data *data = dev_get_drvdata(dev); | |
371 | int speed; | |
372 | ||
373 | speed = nct7802_read_fan_min(data, sattr->nr, sattr->index); | |
374 | if (speed < 0) | |
375 | return speed; | |
376 | ||
377 | return sprintf(buf, "%d\n", speed); | |
378 | } | |
379 | ||
380 | static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr, | |
381 | const char *buf, size_t count) | |
382 | { | |
383 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
384 | struct nct7802_data *data = dev_get_drvdata(dev); | |
385 | unsigned long val; | |
386 | int err; | |
387 | ||
388 | err = kstrtoul(buf, 10, &val); | |
389 | if (err < 0) | |
390 | return err; | |
391 | ||
392 | err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val); | |
393 | return err ? : count; | |
394 | } | |
395 | ||
396 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, | |
397 | char *buf) | |
398 | { | |
399 | struct nct7802_data *data = dev_get_drvdata(dev); | |
400 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
401 | int bit = sattr->index; | |
402 | unsigned int val; | |
403 | int ret; | |
404 | ||
405 | ret = regmap_read(data->regmap, sattr->nr, &val); | |
406 | if (ret < 0) | |
407 | return ret; | |
408 | ||
409 | return sprintf(buf, "%u\n", !!(val & (1 << bit))); | |
410 | } | |
411 | ||
412 | static ssize_t | |
413 | show_beep(struct device *dev, struct device_attribute *attr, char *buf) | |
414 | { | |
415 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
416 | struct nct7802_data *data = dev_get_drvdata(dev); | |
417 | unsigned int regval; | |
418 | int err; | |
419 | ||
420 | err = regmap_read(data->regmap, sattr->nr, ®val); | |
421 | if (err) | |
422 | return err; | |
423 | ||
424 | return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index))); | |
425 | } | |
426 | ||
427 | static ssize_t | |
428 | store_beep(struct device *dev, struct device_attribute *attr, const char *buf, | |
429 | size_t count) | |
430 | { | |
431 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
432 | struct nct7802_data *data = dev_get_drvdata(dev); | |
433 | unsigned long val; | |
434 | int err; | |
435 | ||
436 | err = kstrtoul(buf, 10, &val); | |
437 | if (err < 0) | |
438 | return err; | |
439 | if (val > 1) | |
440 | return -EINVAL; | |
441 | ||
442 | err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index, | |
443 | val ? 1 << sattr->index : 0); | |
444 | return err ? : count; | |
445 | } | |
446 | ||
fcdc5739 CS |
447 | static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, |
448 | show_temp_type, store_temp_type, 0); | |
3434f378 GR |
449 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0x01, |
450 | REG_TEMP_LSB); | |
451 | static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, | |
452 | store_temp, 0x31, 0); | |
453 | static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, | |
454 | store_temp, 0x30, 0); | |
455 | static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, | |
456 | store_temp, 0x3a, 0); | |
457 | ||
fcdc5739 CS |
458 | static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, |
459 | show_temp_type, store_temp_type, 1); | |
3434f378 GR |
460 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0x02, |
461 | REG_TEMP_LSB); | |
462 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, | |
463 | store_temp, 0x33, 0); | |
464 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, | |
465 | store_temp, 0x32, 0); | |
466 | static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, | |
467 | store_temp, 0x3b, 0); | |
468 | ||
fcdc5739 CS |
469 | static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, |
470 | show_temp_type, store_temp_type, 2); | |
3434f378 GR |
471 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0x03, |
472 | REG_TEMP_LSB); | |
473 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, | |
474 | store_temp, 0x35, 0); | |
475 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, | |
476 | store_temp, 0x34, 0); | |
477 | static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, | |
478 | store_temp, 0x3c, 0); | |
479 | ||
480 | static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 0x04, 0); | |
481 | static SENSOR_DEVICE_ATTR_2(temp4_min, S_IRUGO | S_IWUSR, show_temp, | |
482 | store_temp, 0x37, 0); | |
483 | static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, | |
484 | store_temp, 0x36, 0); | |
485 | static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, | |
486 | store_temp, 0x3d, 0); | |
487 | ||
488 | static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 0x06, | |
489 | REG_TEMP_PECI_LSB); | |
490 | static SENSOR_DEVICE_ATTR_2(temp5_min, S_IRUGO | S_IWUSR, show_temp, | |
491 | store_temp, 0x39, 0); | |
492 | static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, | |
493 | store_temp, 0x38, 0); | |
494 | static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, | |
495 | store_temp, 0x3e, 0); | |
496 | ||
497 | static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 0x07, | |
498 | REG_TEMP_PECI_LSB); | |
499 | ||
500 | static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_alarm, NULL, | |
501 | 0x18, 0); | |
502 | static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_alarm, NULL, | |
503 | 0x18, 1); | |
504 | static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_alarm, NULL, | |
505 | 0x18, 2); | |
506 | static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, show_alarm, NULL, | |
507 | 0x18, 3); | |
508 | static SENSOR_DEVICE_ATTR_2(temp5_min_alarm, S_IRUGO, show_alarm, NULL, | |
509 | 0x18, 4); | |
510 | ||
511 | static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_alarm, NULL, | |
512 | 0x19, 0); | |
513 | static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_alarm, NULL, | |
514 | 0x19, 1); | |
515 | static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_alarm, NULL, | |
516 | 0x19, 2); | |
517 | static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, show_alarm, NULL, | |
518 | 0x19, 3); | |
519 | static SENSOR_DEVICE_ATTR_2(temp5_max_alarm, S_IRUGO, show_alarm, NULL, | |
520 | 0x19, 4); | |
521 | ||
522 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, | |
523 | 0x1b, 0); | |
524 | static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, | |
525 | 0x1b, 1); | |
526 | static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, | |
527 | 0x1b, 2); | |
528 | static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, | |
529 | 0x1b, 3); | |
530 | static SENSOR_DEVICE_ATTR_2(temp5_crit_alarm, S_IRUGO, show_alarm, NULL, | |
531 | 0x1b, 4); | |
532 | ||
533 | static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_alarm, NULL, 0x17, 0); | |
534 | static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_alarm, NULL, 0x17, 1); | |
535 | static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_alarm, NULL, 0x17, 2); | |
536 | ||
537 | static SENSOR_DEVICE_ATTR_2(temp1_beep, S_IRUGO | S_IWUSR, show_beep, | |
538 | store_beep, 0x5c, 0); | |
539 | static SENSOR_DEVICE_ATTR_2(temp2_beep, S_IRUGO | S_IWUSR, show_beep, | |
540 | store_beep, 0x5c, 1); | |
541 | static SENSOR_DEVICE_ATTR_2(temp3_beep, S_IRUGO | S_IWUSR, show_beep, | |
542 | store_beep, 0x5c, 2); | |
543 | static SENSOR_DEVICE_ATTR_2(temp4_beep, S_IRUGO | S_IWUSR, show_beep, | |
544 | store_beep, 0x5c, 3); | |
545 | static SENSOR_DEVICE_ATTR_2(temp5_beep, S_IRUGO | S_IWUSR, show_beep, | |
546 | store_beep, 0x5c, 4); | |
547 | static SENSOR_DEVICE_ATTR_2(temp6_beep, S_IRUGO | S_IWUSR, show_beep, | |
548 | store_beep, 0x5c, 5); | |
549 | ||
550 | static struct attribute *nct7802_temp_attrs[] = { | |
fcdc5739 | 551 | &sensor_dev_attr_temp1_type.dev_attr.attr, |
3434f378 GR |
552 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
553 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
554 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
555 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
556 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
557 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
558 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | |
559 | &sensor_dev_attr_temp1_fault.dev_attr.attr, | |
560 | &sensor_dev_attr_temp1_beep.dev_attr.attr, | |
561 | ||
fcdc5739 CS |
562 | &sensor_dev_attr_temp2_type.dev_attr.attr, /* 10 */ |
563 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
3434f378 GR |
564 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
565 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
566 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
567 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | |
568 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
569 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
570 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | |
571 | &sensor_dev_attr_temp2_beep.dev_attr.attr, | |
572 | ||
fcdc5739 CS |
573 | &sensor_dev_attr_temp3_type.dev_attr.attr, /* 20 */ |
574 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
3434f378 GR |
575 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
576 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
577 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | |
578 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, | |
579 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | |
580 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | |
581 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | |
582 | &sensor_dev_attr_temp3_beep.dev_attr.attr, | |
583 | ||
fcdc5739 | 584 | &sensor_dev_attr_temp4_input.dev_attr.attr, /* 30 */ |
3434f378 GR |
585 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
586 | &sensor_dev_attr_temp4_max.dev_attr.attr, | |
587 | &sensor_dev_attr_temp4_crit.dev_attr.attr, | |
588 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, | |
589 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, | |
590 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, | |
591 | &sensor_dev_attr_temp4_beep.dev_attr.attr, | |
592 | ||
fcdc5739 | 593 | &sensor_dev_attr_temp5_input.dev_attr.attr, /* 38 */ |
3434f378 GR |
594 | &sensor_dev_attr_temp5_min.dev_attr.attr, |
595 | &sensor_dev_attr_temp5_max.dev_attr.attr, | |
596 | &sensor_dev_attr_temp5_crit.dev_attr.attr, | |
597 | &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, | |
598 | &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, | |
599 | &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, | |
600 | &sensor_dev_attr_temp5_beep.dev_attr.attr, | |
601 | ||
fcdc5739 | 602 | &sensor_dev_attr_temp6_input.dev_attr.attr, /* 46 */ |
3434f378 GR |
603 | &sensor_dev_attr_temp6_beep.dev_attr.attr, |
604 | ||
605 | NULL | |
606 | }; | |
607 | ||
608 | static umode_t nct7802_temp_is_visible(struct kobject *kobj, | |
609 | struct attribute *attr, int index) | |
610 | { | |
611 | struct device *dev = container_of(kobj, struct device, kobj); | |
612 | struct nct7802_data *data = dev_get_drvdata(dev); | |
613 | unsigned int reg; | |
614 | int err; | |
615 | ||
616 | err = regmap_read(data->regmap, REG_MODE, ®); | |
617 | if (err < 0) | |
618 | return 0; | |
619 | ||
fcdc5739 | 620 | if (index < 10 && |
3434f378 GR |
621 | (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */ |
622 | return 0; | |
fcdc5739 CS |
623 | |
624 | if (index >= 10 && index < 20 && | |
3434f378 GR |
625 | (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */ |
626 | return 0; | |
fcdc5739 | 627 | if (index >= 20 && index < 30 && (reg & 0x30) != 0x20) /* RD3 */ |
3434f378 | 628 | return 0; |
fcdc5739 CS |
629 | |
630 | if (index >= 30 && index < 38) /* local */ | |
3434f378 GR |
631 | return attr->mode; |
632 | ||
633 | err = regmap_read(data->regmap, REG_PECI_ENABLE, ®); | |
634 | if (err < 0) | |
635 | return 0; | |
636 | ||
fcdc5739 | 637 | if (index >= 38 && index < 46 && !(reg & 0x01)) /* PECI 0 */ |
3434f378 GR |
638 | return 0; |
639 | ||
fcdc5739 | 640 | if (index >= 0x46 && (!(reg & 0x02))) /* PECI 1 */ |
3434f378 GR |
641 | return 0; |
642 | ||
643 | return attr->mode; | |
644 | } | |
645 | ||
646 | static struct attribute_group nct7802_temp_group = { | |
647 | .attrs = nct7802_temp_attrs, | |
648 | .is_visible = nct7802_temp_is_visible, | |
649 | }; | |
650 | ||
651 | static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0); | |
652 | static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
653 | 0, 1); | |
654 | static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
655 | 0, 2); | |
656 | static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 3); | |
657 | static SENSOR_DEVICE_ATTR_2(in0_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
658 | 0x5a, 3); | |
659 | ||
660 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0); | |
661 | ||
662 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0); | |
663 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
664 | 2, 1); | |
665 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
666 | 2, 2); | |
667 | static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 0); | |
668 | static SENSOR_DEVICE_ATTR_2(in2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
669 | 0x5a, 0); | |
670 | ||
671 | static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0); | |
672 | static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
673 | 3, 1); | |
674 | static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
675 | 3, 2); | |
676 | static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 1); | |
677 | static SENSOR_DEVICE_ATTR_2(in3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
678 | 0x5a, 1); | |
679 | ||
680 | static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0); | |
681 | static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
682 | 4, 1); | |
683 | static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
684 | 4, 2); | |
685 | static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 2); | |
686 | static SENSOR_DEVICE_ATTR_2(in4_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
687 | 0x5a, 2); | |
688 | ||
689 | static struct attribute *nct7802_in_attrs[] = { | |
690 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
691 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
692 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
693 | &sensor_dev_attr_in0_alarm.dev_attr.attr, | |
694 | &sensor_dev_attr_in0_beep.dev_attr.attr, | |
695 | ||
696 | &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */ | |
697 | ||
698 | &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */ | |
699 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
700 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
701 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
702 | &sensor_dev_attr_in2_beep.dev_attr.attr, | |
703 | ||
704 | &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */ | |
705 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
706 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
707 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | |
708 | &sensor_dev_attr_in3_beep.dev_attr.attr, | |
709 | ||
710 | &sensor_dev_attr_in4_input.dev_attr.attr, /* 17 */ | |
711 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
712 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
713 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | |
714 | &sensor_dev_attr_in4_beep.dev_attr.attr, | |
715 | ||
716 | NULL, | |
717 | }; | |
718 | ||
719 | static umode_t nct7802_in_is_visible(struct kobject *kobj, | |
720 | struct attribute *attr, int index) | |
721 | { | |
722 | struct device *dev = container_of(kobj, struct device, kobj); | |
723 | struct nct7802_data *data = dev_get_drvdata(dev); | |
724 | unsigned int reg; | |
725 | int err; | |
726 | ||
727 | if (index < 6) /* VCC, VCORE */ | |
728 | return attr->mode; | |
729 | ||
730 | err = regmap_read(data->regmap, REG_MODE, ®); | |
731 | if (err < 0) | |
732 | return 0; | |
733 | ||
734 | if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */ | |
735 | return 0; | |
736 | if (index >= 11 && index < 17 && (reg & 0x0c) != 0x0c) /* VSEN2 */ | |
737 | return 0; | |
738 | if (index >= 17 && (reg & 0x30) != 0x30) /* VSEN3 */ | |
739 | return 0; | |
740 | ||
741 | return attr->mode; | |
742 | } | |
743 | ||
744 | static struct attribute_group nct7802_in_group = { | |
745 | .attrs = nct7802_in_attrs, | |
746 | .is_visible = nct7802_in_is_visible, | |
747 | }; | |
748 | ||
749 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0x10); | |
750 | static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan_min, | |
751 | store_fan_min, 0x49, 0x4c); | |
752 | static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 0); | |
753 | static SENSOR_DEVICE_ATTR_2(fan1_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
754 | 0x5b, 0); | |
755 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 0x11); | |
756 | static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan_min, | |
757 | store_fan_min, 0x4a, 0x4d); | |
758 | static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 1); | |
759 | static SENSOR_DEVICE_ATTR_2(fan2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
760 | 0x5b, 1); | |
761 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 0x12); | |
762 | static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan_min, | |
763 | store_fan_min, 0x4b, 0x4e); | |
764 | static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 2); | |
765 | static SENSOR_DEVICE_ATTR_2(fan3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
766 | 0x5b, 2); | |
767 | ||
ea33597c CS |
768 | /* 7.2.91... Fan Control Output Value */ |
769 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0x60); | |
770 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0x61); | |
771 | static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0x62); | |
772 | ||
3434f378 GR |
773 | static struct attribute *nct7802_fan_attrs[] = { |
774 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
775 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
776 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | |
777 | &sensor_dev_attr_fan1_beep.dev_attr.attr, | |
778 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
779 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
780 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
781 | &sensor_dev_attr_fan2_beep.dev_attr.attr, | |
782 | &sensor_dev_attr_fan3_input.dev_attr.attr, | |
783 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
784 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | |
785 | &sensor_dev_attr_fan3_beep.dev_attr.attr, | |
786 | ||
787 | NULL | |
788 | }; | |
789 | ||
790 | static umode_t nct7802_fan_is_visible(struct kobject *kobj, | |
791 | struct attribute *attr, int index) | |
792 | { | |
793 | struct device *dev = container_of(kobj, struct device, kobj); | |
794 | struct nct7802_data *data = dev_get_drvdata(dev); | |
795 | int fan = index / 4; /* 4 attributes per fan */ | |
796 | unsigned int reg; | |
797 | int err; | |
798 | ||
799 | err = regmap_read(data->regmap, REG_FAN_ENABLE, ®); | |
800 | if (err < 0 || !(reg & (1 << fan))) | |
801 | return 0; | |
802 | ||
803 | return attr->mode; | |
804 | } | |
805 | ||
806 | static struct attribute_group nct7802_fan_group = { | |
807 | .attrs = nct7802_fan_attrs, | |
808 | .is_visible = nct7802_fan_is_visible, | |
809 | }; | |
810 | ||
ea33597c CS |
811 | static struct attribute *nct7802_pwm_attrs[] = { |
812 | &sensor_dev_attr_pwm1.dev_attr.attr, | |
813 | &sensor_dev_attr_pwm2.dev_attr.attr, | |
814 | &sensor_dev_attr_pwm3.dev_attr.attr, | |
815 | NULL | |
816 | }; | |
817 | ||
818 | static struct attribute_group nct7802_pwm_group = { | |
819 | .attrs = nct7802_pwm_attrs, | |
820 | }; | |
821 | ||
3434f378 GR |
822 | static const struct attribute_group *nct7802_groups[] = { |
823 | &nct7802_temp_group, | |
824 | &nct7802_in_group, | |
825 | &nct7802_fan_group, | |
ea33597c | 826 | &nct7802_pwm_group, |
3434f378 GR |
827 | NULL |
828 | }; | |
829 | ||
830 | static int nct7802_detect(struct i2c_client *client, | |
831 | struct i2c_board_info *info) | |
832 | { | |
833 | int reg; | |
834 | ||
835 | /* | |
836 | * Chip identification registers are only available in bank 0, | |
837 | * so only attempt chip detection if bank 0 is selected | |
838 | */ | |
839 | reg = i2c_smbus_read_byte_data(client, REG_BANK); | |
840 | if (reg != 0x00) | |
841 | return -ENODEV; | |
842 | ||
843 | reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID); | |
844 | if (reg != 0x50) | |
845 | return -ENODEV; | |
846 | ||
847 | reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID); | |
848 | if (reg != 0xc3) | |
849 | return -ENODEV; | |
850 | ||
851 | reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID); | |
852 | if (reg < 0 || (reg & 0xf0) != 0x20) | |
853 | return -ENODEV; | |
854 | ||
855 | /* Also validate lower bits of voltage and temperature registers */ | |
856 | reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB); | |
857 | if (reg < 0 || (reg & 0x1f)) | |
858 | return -ENODEV; | |
859 | ||
860 | reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB); | |
861 | if (reg < 0 || (reg & 0x3f)) | |
862 | return -ENODEV; | |
863 | ||
864 | reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW); | |
865 | if (reg < 0 || (reg & 0x3f)) | |
866 | return -ENODEV; | |
867 | ||
868 | strlcpy(info->type, "nct7802", I2C_NAME_SIZE); | |
869 | return 0; | |
870 | } | |
871 | ||
872 | static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg) | |
873 | { | |
874 | return reg != REG_BANK && reg <= 0x20; | |
875 | } | |
876 | ||
3c535bca | 877 | static const struct regmap_config nct7802_regmap_config = { |
3434f378 GR |
878 | .reg_bits = 8, |
879 | .val_bits = 8, | |
880 | .cache_type = REGCACHE_RBTREE, | |
881 | .volatile_reg = nct7802_regmap_is_volatile, | |
882 | }; | |
883 | ||
884 | static int nct7802_init_chip(struct nct7802_data *data) | |
885 | { | |
886 | int err; | |
887 | ||
888 | /* Enable ADC */ | |
889 | err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01); | |
890 | if (err) | |
891 | return err; | |
892 | ||
893 | /* Enable local temperature sensor */ | |
894 | err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40); | |
895 | if (err) | |
896 | return err; | |
897 | ||
898 | /* Enable Vcore and VCC voltage monitoring */ | |
899 | return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03); | |
900 | } | |
901 | ||
902 | static int nct7802_probe(struct i2c_client *client, | |
903 | const struct i2c_device_id *id) | |
904 | { | |
905 | struct device *dev = &client->dev; | |
906 | struct nct7802_data *data; | |
907 | struct device *hwmon_dev; | |
908 | int ret; | |
909 | ||
910 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); | |
911 | if (data == NULL) | |
912 | return -ENOMEM; | |
913 | ||
914 | data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config); | |
915 | if (IS_ERR(data->regmap)) | |
916 | return PTR_ERR(data->regmap); | |
917 | ||
918 | mutex_init(&data->access_lock); | |
919 | ||
920 | ret = nct7802_init_chip(data); | |
921 | if (ret < 0) | |
922 | return ret; | |
923 | ||
924 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, | |
925 | data, | |
926 | nct7802_groups); | |
927 | return PTR_ERR_OR_ZERO(hwmon_dev); | |
928 | } | |
929 | ||
930 | static const unsigned short nct7802_address_list[] = { | |
931 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END | |
932 | }; | |
933 | ||
934 | static const struct i2c_device_id nct7802_idtable[] = { | |
935 | { "nct7802", 0 }, | |
936 | { } | |
937 | }; | |
938 | MODULE_DEVICE_TABLE(i2c, nct7802_idtable); | |
939 | ||
940 | static struct i2c_driver nct7802_driver = { | |
941 | .class = I2C_CLASS_HWMON, | |
942 | .driver = { | |
943 | .name = DRVNAME, | |
944 | }, | |
945 | .detect = nct7802_detect, | |
946 | .probe = nct7802_probe, | |
947 | .id_table = nct7802_idtable, | |
948 | .address_list = nct7802_address_list, | |
949 | }; | |
950 | ||
951 | module_i2c_driver(nct7802_driver); | |
952 | ||
953 | MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); | |
954 | MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver"); | |
955 | MODULE_LICENSE("GPL v2"); |