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