1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
6 *
7 * Based on max6650.c:
8 * Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
9 *
10 * Conversion to regmap and with_info API:
11 * Copyright (C) 2024 Guenter Roeck <linux@roeck-us.net>
12 */
13
14 #include <linux/bitfield.h>
15 #include <linux/bitops.h>
16 #include <linux/bits.h>
17 #include <linux/err.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/minmax.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/of_platform.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28
29 /*
30 * Addresses to scan.
31 */
32
33 static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
34 0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
35
36 /*
37 * Insmod parameters
38 */
39
40 static int pwminv; /*Inverted PWM output. */
41 module_param(pwminv, int, 0444);
42
43 static int init = 1; /*Power-on initialization.*/
44 module_param(init, int, 0444);
45
46 #define AMC6821_REG_DEV_ID 0x3D
47 #define AMC6821_REG_COMP_ID 0x3E
48 #define AMC6821_REG_CONF1 0x00
49 #define AMC6821_REG_CONF2 0x01
50 #define AMC6821_REG_CONF3 0x3F
51 #define AMC6821_REG_CONF4 0x04
52 #define AMC6821_REG_STAT1 0x02
53 #define AMC6821_REG_STAT2 0x03
54 #define AMC6821_REG_TEMP_LO 0x06
55 #define AMC6821_REG_TDATA_LOW 0x08
56 #define AMC6821_REG_TDATA_HI 0x09
57 #define AMC6821_REG_LTEMP_HI 0x0A
58 #define AMC6821_REG_RTEMP_HI 0x0B
59 #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
60 #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
61 #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
62 #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
63 #define AMC6821_REG_LTEMP_CRIT 0x1B
64 #define AMC6821_REG_RTEMP_CRIT 0x1D
65 #define AMC6821_REG_PSV_TEMP 0x1C
66 #define AMC6821_REG_DCY 0x22
67 #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
68 #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
69 #define AMC6821_REG_DCY_LOW_TEMP 0x21
70
71 #define AMC6821_REG_TACH_LLIMITL 0x10
72 #define AMC6821_REG_TACH_HLIMITL 0x12
73 #define AMC6821_REG_TACH_SETTINGL 0x1e
74
75 #define AMC6821_CONF1_START BIT(0)
76 #define AMC6821_CONF1_FAN_INT_EN BIT(1)
77 #define AMC6821_CONF1_FANIE BIT(2)
78 #define AMC6821_CONF1_PWMINV BIT(3)
79 #define AMC6821_CONF1_FAN_FAULT_EN BIT(4)
80 #define AMC6821_CONF1_FDRC0 BIT(5)
81 #define AMC6821_CONF1_FDRC1 BIT(6)
82 #define AMC6821_CONF1_THERMOVIE BIT(7)
83
84 #define AMC6821_CONF2_PWM_EN BIT(0)
85 #define AMC6821_CONF2_TACH_MODE BIT(1)
86 #define AMC6821_CONF2_TACH_EN BIT(2)
87 #define AMC6821_CONF2_RTFIE BIT(3)
88 #define AMC6821_CONF2_LTOIE BIT(4)
89 #define AMC6821_CONF2_RTOIE BIT(5)
90 #define AMC6821_CONF2_PSVIE BIT(6)
91 #define AMC6821_CONF2_RST BIT(7)
92
93 #define AMC6821_CONF3_THERM_FAN_EN BIT(7)
94 #define AMC6821_CONF3_REV_MASK GENMASK(3, 0)
95
96 #define AMC6821_CONF4_OVREN BIT(4)
97 #define AMC6821_CONF4_TACH_FAST BIT(5)
98 #define AMC6821_CONF4_PSPR BIT(6)
99 #define AMC6821_CONF4_MODE BIT(7)
100
101 #define AMC6821_STAT1_RPM_ALARM BIT(0)
102 #define AMC6821_STAT1_FANS BIT(1)
103 #define AMC6821_STAT1_RTH BIT(2)
104 #define AMC6821_STAT1_RTL BIT(3)
105 #define AMC6821_STAT1_R_THERM BIT(4)
106 #define AMC6821_STAT1_RTF BIT(5)
107 #define AMC6821_STAT1_LTH BIT(6)
108 #define AMC6821_STAT1_LTL BIT(7)
109
110 #define AMC6821_STAT2_RTC BIT(3)
111 #define AMC6821_STAT2_LTC BIT(4)
112 #define AMC6821_STAT2_LPSV BIT(5)
113 #define AMC6821_STAT2_L_THERM BIT(6)
114 #define AMC6821_STAT2_THERM_IN BIT(7)
115
116 #define AMC6821_TEMP_SLOPE_MASK GENMASK(2, 0)
117 #define AMC6821_TEMP_LIMIT_MASK GENMASK(7, 3)
118
119 /*
120 * Client data (each client gets its own)
121 */
122
123 struct amc6821_data {
124 struct regmap *regmap;
125 struct mutex update_lock;
126 };
127
128 /*
129 * Return 0 on success or negative error code.
130 *
131 * temps returns set of three temperatures, in °C:
132 * temps[0]: Passive cooling temperature, applies to both channels
133 * temps[1]: Low temperature, start slope calculations
134 * temps[2]: High temperature
135 *
136 * Channel 0: local, channel 1: remote.
137 */
amc6821_get_auto_point_temps(struct regmap * regmap,int channel,u8 * temps)138 static int amc6821_get_auto_point_temps(struct regmap *regmap, int channel, u8 *temps)
139 {
140 u32 regs[] = {
141 AMC6821_REG_DCY_LOW_TEMP,
142 AMC6821_REG_PSV_TEMP,
143 channel ? AMC6821_REG_RTEMP_FAN_CTRL : AMC6821_REG_LTEMP_FAN_CTRL
144 };
145 u8 regvals[3];
146 int slope;
147 int err;
148
149 err = regmap_multi_reg_read(regmap, regs, regvals, 3);
150 if (err)
151 return err;
152 temps[0] = regvals[1];
153 temps[1] = FIELD_GET(AMC6821_TEMP_LIMIT_MASK, regvals[2]) * 4;
154
155 /* slope is 32 >> <slope bits> in °C */
156 slope = 32 >> FIELD_GET(AMC6821_TEMP_SLOPE_MASK, regvals[2]);
157 if (slope)
158 temps[2] = temps[1] + DIV_ROUND_CLOSEST(255 - regvals[0], slope);
159 else
160 temps[2] = 255;
161
162 return 0;
163 }
164
amc6821_temp_read_values(struct regmap * regmap,u32 attr,int channel,long * val)165 static int amc6821_temp_read_values(struct regmap *regmap, u32 attr, int channel, long *val)
166 {
167 int reg, err;
168 u32 regval;
169
170 switch (attr) {
171 case hwmon_temp_input:
172 reg = channel ? AMC6821_REG_RTEMP_HI : AMC6821_REG_LTEMP_HI;
173 break;
174 case hwmon_temp_min:
175 reg = channel ? AMC6821_REG_RTEMP_LIMIT_MIN : AMC6821_REG_LTEMP_LIMIT_MIN;
176 break;
177 case hwmon_temp_max:
178 reg = channel ? AMC6821_REG_RTEMP_LIMIT_MAX : AMC6821_REG_LTEMP_LIMIT_MAX;
179 break;
180 case hwmon_temp_crit:
181 reg = channel ? AMC6821_REG_RTEMP_CRIT : AMC6821_REG_LTEMP_CRIT;
182 break;
183 default:
184 return -EOPNOTSUPP;
185 }
186 err = regmap_read(regmap, reg, ®val);
187 if (err)
188 return err;
189 *val = sign_extend32(regval, 7) * 1000;
190 return 0;
191 }
192
amc6821_read_alarms(struct regmap * regmap,enum hwmon_sensor_types type,u32 attr,int channel,long * val)193 static int amc6821_read_alarms(struct regmap *regmap, enum hwmon_sensor_types type,
194 u32 attr, int channel, long *val)
195 {
196 int reg, mask, err;
197 u32 regval;
198
199 switch (type) {
200 case hwmon_temp:
201 switch (attr) {
202 case hwmon_temp_min_alarm:
203 reg = AMC6821_REG_STAT1;
204 mask = channel ? AMC6821_STAT1_RTL : AMC6821_STAT1_LTL;
205 break;
206 case hwmon_temp_max_alarm:
207 reg = AMC6821_REG_STAT1;
208 mask = channel ? AMC6821_STAT1_RTH : AMC6821_STAT1_LTH;
209 break;
210 case hwmon_temp_crit_alarm:
211 reg = AMC6821_REG_STAT2;
212 mask = channel ? AMC6821_STAT2_RTC : AMC6821_STAT2_LTC;
213 break;
214 case hwmon_temp_fault:
215 reg = AMC6821_REG_STAT1;
216 mask = AMC6821_STAT1_RTF;
217 break;
218 default:
219 return -EOPNOTSUPP;
220 }
221 break;
222 case hwmon_fan:
223 switch (attr) {
224 case hwmon_fan_fault:
225 reg = AMC6821_REG_STAT1;
226 mask = AMC6821_STAT1_FANS;
227 break;
228 default:
229 return -EOPNOTSUPP;
230 }
231 break;
232 default:
233 return -EOPNOTSUPP;
234 }
235 err = regmap_read(regmap, reg, ®val);
236 if (err)
237 return err;
238 *val = !!(regval & mask);
239 return 0;
240 }
241
amc6821_temp_read(struct device * dev,u32 attr,int channel,long * val)242 static int amc6821_temp_read(struct device *dev, u32 attr, int channel, long *val)
243 {
244 struct amc6821_data *data = dev_get_drvdata(dev);
245
246 switch (attr) {
247 case hwmon_temp_input:
248 case hwmon_temp_min:
249 case hwmon_temp_max:
250 case hwmon_temp_crit:
251 return amc6821_temp_read_values(data->regmap, attr, channel, val);
252 case hwmon_temp_min_alarm:
253 case hwmon_temp_max_alarm:
254 case hwmon_temp_crit_alarm:
255 case hwmon_temp_fault:
256 return amc6821_read_alarms(data->regmap, hwmon_temp, attr, channel, val);
257 default:
258 return -EOPNOTSUPP;
259 }
260 }
261
amc6821_temp_write(struct device * dev,u32 attr,int channel,long val)262 static int amc6821_temp_write(struct device *dev, u32 attr, int channel, long val)
263 {
264 struct amc6821_data *data = dev_get_drvdata(dev);
265 int reg;
266
267 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
268
269 switch (attr) {
270 case hwmon_temp_min:
271 reg = channel ? AMC6821_REG_RTEMP_LIMIT_MIN : AMC6821_REG_LTEMP_LIMIT_MIN;
272 break;
273 case hwmon_temp_max:
274 reg = channel ? AMC6821_REG_RTEMP_LIMIT_MAX : AMC6821_REG_LTEMP_LIMIT_MAX;
275 break;
276 case hwmon_temp_crit:
277 reg = channel ? AMC6821_REG_RTEMP_CRIT : AMC6821_REG_LTEMP_CRIT;
278 break;
279 default:
280 return -EOPNOTSUPP;
281 }
282 return regmap_write(data->regmap, reg, val);
283 }
284
amc6821_pwm_read(struct device * dev,u32 attr,long * val)285 static int amc6821_pwm_read(struct device *dev, u32 attr, long *val)
286 {
287 struct amc6821_data *data = dev_get_drvdata(dev);
288 struct regmap *regmap = data->regmap;
289 u32 regval;
290 int err;
291
292 switch (attr) {
293 case hwmon_pwm_enable:
294 err = regmap_read(regmap, AMC6821_REG_CONF1, ®val);
295 if (err)
296 return err;
297 switch (regval & (AMC6821_CONF1_FDRC0 | AMC6821_CONF1_FDRC1)) {
298 case 0:
299 *val = 1; /* manual */
300 break;
301 case AMC6821_CONF1_FDRC0:
302 *val = 4; /* target rpm (fan1_target) controlled */
303 break;
304 case AMC6821_CONF1_FDRC1:
305 *val = 2; /* remote temp controlled */
306 break;
307 default:
308 *val = 3; /* max(local, remote) temp controlled */
309 break;
310 }
311 return 0;
312 case hwmon_pwm_mode:
313 err = regmap_read(regmap, AMC6821_REG_CONF2, ®val);
314 if (err)
315 return err;
316 *val = !!(regval & AMC6821_CONF2_TACH_MODE);
317 return 0;
318 case hwmon_pwm_auto_channels_temp:
319 err = regmap_read(regmap, AMC6821_REG_CONF1, ®val);
320 if (err)
321 return err;
322 switch (regval & (AMC6821_CONF1_FDRC0 | AMC6821_CONF1_FDRC1)) {
323 case 0:
324 case AMC6821_CONF1_FDRC0:
325 *val = 0; /* manual or target rpm controlled */
326 break;
327 case AMC6821_CONF1_FDRC1:
328 *val = 2; /* remote temp controlled */
329 break;
330 default:
331 *val = 3; /* max(local, remote) temp controlled */
332 break;
333 }
334 return 0;
335 case hwmon_pwm_input:
336 err = regmap_read(regmap, AMC6821_REG_DCY, ®val);
337 if (err)
338 return err;
339 *val = regval;
340 return 0;
341 default:
342 return -EOPNOTSUPP;
343 }
344 }
345
amc6821_pwm_write(struct device * dev,u32 attr,long val)346 static int amc6821_pwm_write(struct device *dev, u32 attr, long val)
347 {
348 struct amc6821_data *data = dev_get_drvdata(dev);
349 struct regmap *regmap = data->regmap;
350 u32 mode;
351
352 switch (attr) {
353 case hwmon_pwm_enable:
354 switch (val) {
355 case 1:
356 mode = 0;
357 break;
358 case 2:
359 mode = AMC6821_CONF1_FDRC1;
360 break;
361 case 3:
362 mode = AMC6821_CONF1_FDRC0 | AMC6821_CONF1_FDRC1;
363 break;
364 case 4:
365 mode = AMC6821_CONF1_FDRC0;
366 break;
367 default:
368 return -EINVAL;
369 }
370 return regmap_update_bits(regmap, AMC6821_REG_CONF1,
371 AMC6821_CONF1_FDRC0 | AMC6821_CONF1_FDRC1,
372 mode);
373 case hwmon_pwm_mode:
374 if (val < 0 || val > 1)
375 return -EINVAL;
376 return regmap_update_bits(regmap, AMC6821_REG_CONF2,
377 AMC6821_CONF2_TACH_MODE,
378 val ? AMC6821_CONF2_TACH_MODE : 0);
379 break;
380 case hwmon_pwm_input:
381 if (val < 0 || val > 255)
382 return -EINVAL;
383 return regmap_write(regmap, AMC6821_REG_DCY, val);
384 default:
385 return -EOPNOTSUPP;
386 }
387 }
388
amc6821_fan_read_rpm(struct regmap * regmap,u32 attr,long * val)389 static int amc6821_fan_read_rpm(struct regmap *regmap, u32 attr, long *val)
390 {
391 int reg, err;
392 u8 regs[2];
393 u32 regval;
394
395 switch (attr) {
396 case hwmon_fan_input:
397 reg = AMC6821_REG_TDATA_LOW;
398 break;
399 case hwmon_fan_min:
400 reg = AMC6821_REG_TACH_LLIMITL;
401 break;
402 case hwmon_fan_max:
403 reg = AMC6821_REG_TACH_HLIMITL;
404 break;
405 case hwmon_fan_target:
406 reg = AMC6821_REG_TACH_SETTINGL;
407 break;
408 default:
409 return -EOPNOTSUPP;
410 }
411
412 err = regmap_bulk_read(regmap, reg, regs, 2);
413 if (err)
414 return err;
415
416 regval = (regs[1] << 8) | regs[0];
417 *val = regval ? 6000000 / regval : 0;
418
419 return 0;
420 }
421
amc6821_fan_read(struct device * dev,u32 attr,long * val)422 static int amc6821_fan_read(struct device *dev, u32 attr, long *val)
423 {
424 struct amc6821_data *data = dev_get_drvdata(dev);
425 struct regmap *regmap = data->regmap;
426 u32 regval;
427 int err;
428
429 switch (attr) {
430 case hwmon_fan_input:
431 case hwmon_fan_min:
432 case hwmon_fan_max:
433 case hwmon_fan_target:
434 return amc6821_fan_read_rpm(regmap, attr, val);
435 case hwmon_fan_fault:
436 return amc6821_read_alarms(regmap, hwmon_fan, attr, 0, val);
437 case hwmon_fan_pulses:
438 err = regmap_read(regmap, AMC6821_REG_CONF4, ®val);
439 if (err)
440 return err;
441 *val = (regval & AMC6821_CONF4_PSPR) ? 4 : 2;
442 return 0;
443 default:
444 return -EOPNOTSUPP;
445 }
446 }
447
amc6821_fan_write(struct device * dev,u32 attr,long val)448 static int amc6821_fan_write(struct device *dev, u32 attr, long val)
449 {
450 struct amc6821_data *data = dev_get_drvdata(dev);
451 struct regmap *regmap = data->regmap;
452 u8 regs[2];
453 int reg;
454
455 if (attr == hwmon_fan_pulses) {
456 if (val != 2 && val != 4)
457 return -EINVAL;
458 return regmap_update_bits(regmap, AMC6821_REG_CONF4,
459 AMC6821_CONF4_PSPR,
460 val == 4 ? AMC6821_CONF4_PSPR : 0);
461 }
462
463 if (val < 0)
464 return -EINVAL;
465
466 switch (attr) {
467 case hwmon_fan_min:
468 if (!val) /* no unlimited minimum speed */
469 return -EINVAL;
470 reg = AMC6821_REG_TACH_LLIMITL;
471 break;
472 case hwmon_fan_max:
473 reg = AMC6821_REG_TACH_HLIMITL;
474 break;
475 case hwmon_fan_target:
476 if (!val) /* no unlimited target speed */
477 return -EINVAL;
478 reg = AMC6821_REG_TACH_SETTINGL;
479 break;
480 default:
481 return -EOPNOTSUPP;
482 }
483
484 val = val ? 6000000 / clamp_val(val, 1, 6000000) : 0;
485 val = clamp_val(val, 0, 0xffff);
486
487 regs[0] = val & 0xff;
488 regs[1] = val >> 8;
489
490 return regmap_bulk_write(data->regmap, reg, regs, 2);
491 }
492
temp_auto_point_temp_show(struct device * dev,struct device_attribute * devattr,char * buf)493 static ssize_t temp_auto_point_temp_show(struct device *dev,
494 struct device_attribute *devattr,
495 char *buf)
496 {
497 struct amc6821_data *data = dev_get_drvdata(dev);
498 int ix = to_sensor_dev_attr_2(devattr)->index;
499 int nr = to_sensor_dev_attr_2(devattr)->nr;
500 u8 temps[3];
501 int err;
502
503 mutex_lock(&data->update_lock);
504 err = amc6821_get_auto_point_temps(data->regmap, nr, temps);
505 mutex_unlock(&data->update_lock);
506 if (err)
507 return err;
508
509 return sysfs_emit(buf, "%d\n", temps[ix] * 1000);
510 }
511
pwm1_auto_point_pwm_show(struct device * dev,struct device_attribute * devattr,char * buf)512 static ssize_t pwm1_auto_point_pwm_show(struct device *dev,
513 struct device_attribute *devattr,
514 char *buf)
515 {
516 struct amc6821_data *data = dev_get_drvdata(dev);
517 int ix = to_sensor_dev_attr(devattr)->index;
518 u32 val;
519 int err;
520
521 switch (ix) {
522 case 0:
523 val = 0;
524 break;
525 case 1:
526 err = regmap_read(data->regmap, AMC6821_REG_DCY_LOW_TEMP, &val);
527 if (err)
528 return err;
529 break;
530 default:
531 val = 255;
532 break;
533 }
534 return sysfs_emit(buf, "%d\n", val);
535 }
536
537 /*
538 * Set TEMP[0-4] (low temperature) and SLP[0-2] (slope) of local or remote
539 * TEMP-FAN control register.
540 *
541 * Return 0 on success or negative error code.
542 *
543 * Channel 0: local, channel 1: remote
544 */
set_slope_register(struct regmap * regmap,int channel,u8 * temps)545 static inline int set_slope_register(struct regmap *regmap, int channel, u8 *temps)
546 {
547 u8 regval = FIELD_PREP(AMC6821_TEMP_LIMIT_MASK, temps[1] / 4);
548 u8 tmp, dpwm;
549 int err, dt;
550 u32 pwm;
551
552 err = regmap_read(regmap, AMC6821_REG_DCY_LOW_TEMP, &pwm);
553 if (err)
554 return err;
555
556 dpwm = 255 - pwm;
557
558 dt = temps[2] - temps[1];
559 for (tmp = 4; tmp > 0; tmp--) {
560 if (dt * (32 >> tmp) >= dpwm)
561 break;
562 }
563 regval |= FIELD_PREP(AMC6821_TEMP_SLOPE_MASK, tmp);
564
565 return regmap_write(regmap,
566 channel ? AMC6821_REG_RTEMP_FAN_CTRL : AMC6821_REG_LTEMP_FAN_CTRL,
567 regval);
568 }
569
temp_auto_point_temp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)570 static ssize_t temp_auto_point_temp_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
573 {
574 struct amc6821_data *data = dev_get_drvdata(dev);
575 int ix = to_sensor_dev_attr_2(attr)->index;
576 int nr = to_sensor_dev_attr_2(attr)->nr;
577 struct regmap *regmap = data->regmap;
578 u8 temps[3], otemps[3];
579 long val;
580 int ret;
581
582 ret = kstrtol(buf, 10, &val);
583 if (ret)
584 return ret;
585
586 mutex_lock(&data->update_lock);
587
588 ret = amc6821_get_auto_point_temps(data->regmap, nr, temps);
589 if (ret)
590 goto unlock;
591
592 switch (ix) {
593 case 0:
594 /*
595 * Passive cooling temperature. Range limit against low limit
596 * of both channels.
597 */
598 ret = amc6821_get_auto_point_temps(data->regmap, 1 - nr, otemps);
599 if (ret)
600 goto unlock;
601 val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 63000), 1000);
602 val = clamp_val(val, 0, min(temps[1], otemps[1]));
603 ret = regmap_write(regmap, AMC6821_REG_PSV_TEMP, val);
604 break;
605 case 1:
606 /*
607 * Low limit; must be between passive and high limit,
608 * and not exceed 124. Step size is 4 degrees C.
609 */
610 val = clamp_val(val, DIV_ROUND_UP(temps[0], 4) * 4000, 124000);
611 temps[1] = DIV_ROUND_CLOSEST(val, 4000) * 4;
612 val = temps[1] / 4;
613 /* Auto-adjust high limit if necessary */
614 temps[2] = clamp_val(temps[2], temps[1] + 1, 255);
615 ret = set_slope_register(regmap, nr, temps);
616 break;
617 case 2:
618 /* high limit, must be higher than low limit */
619 val = clamp_val(val, (temps[1] + 1) * 1000, 255000);
620 temps[2] = DIV_ROUND_CLOSEST(val, 1000);
621 ret = set_slope_register(regmap, nr, temps);
622 break;
623 default:
624 ret = -EINVAL;
625 break;
626 }
627 unlock:
628 mutex_unlock(&data->update_lock);
629 return ret ? : count;
630 }
631
pwm1_auto_point_pwm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)632 static ssize_t pwm1_auto_point_pwm_store(struct device *dev,
633 struct device_attribute *attr,
634 const char *buf, size_t count)
635 {
636 struct amc6821_data *data = dev_get_drvdata(dev);
637 struct regmap *regmap = data->regmap;
638 int i, ret;
639 u8 val;
640
641 ret = kstrtou8(buf, 10, &val);
642 if (ret)
643 return ret;
644
645 mutex_lock(&data->update_lock);
646 ret = regmap_write(regmap, AMC6821_REG_DCY_LOW_TEMP, val);
647 if (ret)
648 goto unlock;
649
650 for (i = 0; i < 2; i++) {
651 u8 temps[3];
652
653 ret = amc6821_get_auto_point_temps(regmap, i, temps);
654 if (ret)
655 break;
656 ret = set_slope_register(regmap, i, temps);
657 if (ret)
658 break;
659 }
660 unlock:
661 mutex_unlock(&data->update_lock);
662 return ret ? : count;
663 }
664
665 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm1_auto_point_pwm, 0);
666 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm1_auto_point_pwm, 1);
667 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm1_auto_point_pwm, 2);
668 static SENSOR_DEVICE_ATTR_2_RO(temp1_auto_point1_temp, temp_auto_point_temp,
669 0, 0);
670 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, temp_auto_point_temp,
671 0, 1);
672 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point3_temp, temp_auto_point_temp,
673 0, 2);
674
675 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp_auto_point_temp,
676 1, 0);
677 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, temp_auto_point_temp,
678 1, 1);
679 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point3_temp, temp_auto_point_temp,
680 1, 2);
681
682 static struct attribute *amc6821_attrs[] = {
683 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
684 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
685 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
686 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
687 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
688 &sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
689 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
690 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
691 &sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
692 NULL
693 };
694 ATTRIBUTE_GROUPS(amc6821);
695
amc6821_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)696 static int amc6821_read(struct device *dev, enum hwmon_sensor_types type,
697 u32 attr, int channel, long *val)
698 {
699 switch (type) {
700 case hwmon_temp:
701 return amc6821_temp_read(dev, attr, channel, val);
702 case hwmon_fan:
703 return amc6821_fan_read(dev, attr, val);
704 case hwmon_pwm:
705 return amc6821_pwm_read(dev, attr, val);
706 default:
707 return -EOPNOTSUPP;
708 }
709 }
710
amc6821_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)711 static int amc6821_write(struct device *dev, enum hwmon_sensor_types type,
712 u32 attr, int channel, long val)
713 {
714 switch (type) {
715 case hwmon_temp:
716 return amc6821_temp_write(dev, attr, channel, val);
717 case hwmon_fan:
718 return amc6821_fan_write(dev, attr, val);
719 case hwmon_pwm:
720 return amc6821_pwm_write(dev, attr, val);
721 default:
722 return -EOPNOTSUPP;
723 }
724 }
725
amc6821_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)726 static umode_t amc6821_is_visible(const void *data,
727 enum hwmon_sensor_types type,
728 u32 attr, int channel)
729 {
730 switch (type) {
731 case hwmon_temp:
732 switch (attr) {
733 case hwmon_temp_input:
734 case hwmon_temp_min_alarm:
735 case hwmon_temp_max_alarm:
736 case hwmon_temp_crit_alarm:
737 case hwmon_temp_fault:
738 return 0444;
739 case hwmon_temp_min:
740 case hwmon_temp_max:
741 case hwmon_temp_crit:
742 return 0644;
743 default:
744 return 0;
745 }
746 case hwmon_fan:
747 switch (attr) {
748 case hwmon_fan_input:
749 case hwmon_fan_fault:
750 return 0444;
751 case hwmon_fan_pulses:
752 case hwmon_fan_min:
753 case hwmon_fan_max:
754 case hwmon_fan_target:
755 return 0644;
756 default:
757 return 0;
758 }
759 case hwmon_pwm:
760 switch (attr) {
761 case hwmon_pwm_mode:
762 case hwmon_pwm_enable:
763 case hwmon_pwm_input:
764 return 0644;
765 case hwmon_pwm_auto_channels_temp:
766 return 0444;
767 default:
768 return 0;
769 }
770 default:
771 return 0;
772 }
773 }
774
775 static const struct hwmon_channel_info * const amc6821_info[] = {
776 HWMON_CHANNEL_INFO(temp,
777 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
778 HWMON_T_CRIT | HWMON_T_MIN_ALARM |
779 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM,
780 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
781 HWMON_T_CRIT | HWMON_T_MIN_ALARM |
782 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM |
783 HWMON_T_FAULT),
784 HWMON_CHANNEL_INFO(fan,
785 HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX |
786 HWMON_F_TARGET | HWMON_F_PULSES | HWMON_F_FAULT),
787 HWMON_CHANNEL_INFO(pwm,
788 HWMON_PWM_INPUT | HWMON_PWM_ENABLE | HWMON_PWM_MODE |
789 HWMON_PWM_AUTO_CHANNELS_TEMP),
790 NULL
791 };
792
793 static const struct hwmon_ops amc6821_hwmon_ops = {
794 .is_visible = amc6821_is_visible,
795 .read = amc6821_read,
796 .write = amc6821_write,
797 };
798
799 static const struct hwmon_chip_info amc6821_chip_info = {
800 .ops = &amc6821_hwmon_ops,
801 .info = amc6821_info,
802 };
803
804 /* Return 0 if detection is successful, -ENODEV otherwise */
amc6821_detect(struct i2c_client * client,struct i2c_board_info * info)805 static int amc6821_detect(struct i2c_client *client, struct i2c_board_info *info)
806 {
807 struct i2c_adapter *adapter = client->adapter;
808 int address = client->addr;
809 int dev_id, comp_id;
810
811 dev_dbg(&adapter->dev, "amc6821_detect called.\n");
812
813 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
814 dev_dbg(&adapter->dev,
815 "amc6821: I2C bus doesn't support byte mode, "
816 "skipping.\n");
817 return -ENODEV;
818 }
819
820 dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
821 comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
822 if (dev_id != 0x21 || comp_id != 0x49) {
823 dev_dbg(&adapter->dev,
824 "amc6821: detection failed at 0x%02x.\n",
825 address);
826 return -ENODEV;
827 }
828
829 /*
830 * Bit 7 of the address register is ignored, so we can check the
831 * ID registers again
832 */
833 dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
834 comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
835 if (dev_id != 0x21 || comp_id != 0x49) {
836 dev_dbg(&adapter->dev,
837 "amc6821: detection failed at 0x%02x.\n",
838 address);
839 return -ENODEV;
840 }
841
842 dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
843 strscpy(info->type, "amc6821", I2C_NAME_SIZE);
844
845 return 0;
846 }
847
amc6821_init_client(struct amc6821_data * data)848 static int amc6821_init_client(struct amc6821_data *data)
849 {
850 struct regmap *regmap = data->regmap;
851 int err;
852
853 if (init) {
854 err = regmap_set_bits(regmap, AMC6821_REG_CONF4, AMC6821_CONF4_MODE);
855 if (err)
856 return err;
857 err = regmap_clear_bits(regmap, AMC6821_REG_CONF3, AMC6821_CONF3_THERM_FAN_EN);
858 if (err)
859 return err;
860 err = regmap_clear_bits(regmap, AMC6821_REG_CONF2,
861 AMC6821_CONF2_RTFIE |
862 AMC6821_CONF2_LTOIE |
863 AMC6821_CONF2_RTOIE);
864 if (err)
865 return err;
866
867 err = regmap_update_bits(regmap, AMC6821_REG_CONF1,
868 AMC6821_CONF1_THERMOVIE | AMC6821_CONF1_FANIE |
869 AMC6821_CONF1_START | AMC6821_CONF1_PWMINV,
870 AMC6821_CONF1_START |
871 (pwminv ? AMC6821_CONF1_PWMINV : 0));
872 if (err)
873 return err;
874 }
875 return 0;
876 }
877
amc6821_volatile_reg(struct device * dev,unsigned int reg)878 static bool amc6821_volatile_reg(struct device *dev, unsigned int reg)
879 {
880 switch (reg) {
881 case AMC6821_REG_STAT1:
882 case AMC6821_REG_STAT2:
883 case AMC6821_REG_TEMP_LO:
884 case AMC6821_REG_TDATA_LOW:
885 case AMC6821_REG_LTEMP_HI:
886 case AMC6821_REG_RTEMP_HI:
887 case AMC6821_REG_TDATA_HI:
888 return true;
889 default:
890 return false;
891 }
892 }
893
894 static const struct regmap_config amc6821_regmap_config = {
895 .reg_bits = 8,
896 .val_bits = 8,
897 .volatile_reg = amc6821_volatile_reg,
898 .cache_type = REGCACHE_MAPLE,
899 };
900
amc6821_probe(struct i2c_client * client)901 static int amc6821_probe(struct i2c_client *client)
902 {
903 struct device *dev = &client->dev;
904 struct amc6821_data *data;
905 struct device *hwmon_dev;
906 struct regmap *regmap;
907 int err;
908
909 data = devm_kzalloc(dev, sizeof(struct amc6821_data), GFP_KERNEL);
910 if (!data)
911 return -ENOMEM;
912
913 regmap = devm_regmap_init_i2c(client, &amc6821_regmap_config);
914 if (IS_ERR(regmap))
915 return dev_err_probe(dev, PTR_ERR(regmap),
916 "Failed to initialize regmap\n");
917 data->regmap = regmap;
918
919 err = amc6821_init_client(data);
920 if (err)
921 return err;
922
923 if (of_device_is_compatible(dev->of_node, "tsd,mule")) {
924 err = devm_of_platform_populate(dev);
925 if (err)
926 return dev_err_probe(dev, err,
927 "Failed to create sub-devices\n");
928 }
929
930 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
931 data, &amc6821_chip_info,
932 amc6821_groups);
933 return PTR_ERR_OR_ZERO(hwmon_dev);
934 }
935
936 static const struct i2c_device_id amc6821_id[] = {
937 { "amc6821" },
938 { }
939 };
940
941 MODULE_DEVICE_TABLE(i2c, amc6821_id);
942
943 static const struct of_device_id __maybe_unused amc6821_of_match[] = {
944 {
945 .compatible = "ti,amc6821",
946 },
947 {
948 .compatible = "tsd,mule",
949 },
950 { }
951 };
952
953 MODULE_DEVICE_TABLE(of, amc6821_of_match);
954
955 static struct i2c_driver amc6821_driver = {
956 .class = I2C_CLASS_HWMON,
957 .driver = {
958 .name = "amc6821",
959 .of_match_table = of_match_ptr(amc6821_of_match),
960 },
961 .probe = amc6821_probe,
962 .id_table = amc6821_id,
963 .detect = amc6821_detect,
964 .address_list = normal_i2c,
965 };
966
967 module_i2c_driver(amc6821_driver);
968
969 MODULE_LICENSE("GPL");
970 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
971 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");
972