]> Git Repo - linux.git/blame - drivers/hwmon/amc6821.c
Linux 6.14-rc3
[linux.git] / drivers / hwmon / amc6821.c
CommitLineData
74ba9207 1// SPDX-License-Identifier: GPL-2.0-or-later
b5430a04 2/*
ca3c7b63
GR
3 * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (C) 2009 T. Mertelj <[email protected]>
6 *
7 * Based on max6650.c:
8 * Copyright (C) 2007 Hans J. Koch <[email protected]>
e98ab50e
GR
9 *
10 * Conversion to regmap and with_info API:
11 * Copyright (C) 2024 Guenter Roeck <[email protected]>
ca3c7b63 12 */
b5430a04 13
a051d507
GR
14#include <linux/bitfield.h>
15#include <linux/bitops.h>
a9c2f41f 16#include <linux/bits.h>
e5cd7dd9 17#include <linux/err.h>
b5430a04
TM
18#include <linux/hwmon.h>
19#include <linux/hwmon-sysfs.h>
e5cd7dd9
GR
20#include <linux/i2c.h>
21#include <linux/init.h>
a051d507 22#include <linux/minmax.h>
e5cd7dd9 23#include <linux/module.h>
b5430a04 24#include <linux/mutex.h>
8f38236d 25#include <linux/of_platform.h>
a051d507 26#include <linux/regmap.h>
e5cd7dd9 27#include <linux/slab.h>
b5430a04 28
b5430a04
TM
29/*
30 * Addresses to scan.
31 */
32
33static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
34 0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
35
b5430a04
TM
36/*
37 * Insmod parameters
38 */
39
a6bee4a5 40static int pwminv; /*Inverted PWM output. */
a7818350 41module_param(pwminv, int, 0444);
b5430a04
TM
42
43static int init = 1; /*Power-on initialization.*/
a7818350 44module_param(init, int, 0444);
b5430a04 45
d632e829
GR
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
a051d507 54#define AMC6821_REG_TEMP_LO 0x06
d632e829
GR
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
d632e829 72#define AMC6821_REG_TACH_HLIMITL 0x12
d632e829 73#define AMC6821_REG_TACH_SETTINGL 0x1e
d632e829 74
a9c2f41f
GR
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)
b5430a04 115
a051d507
GR
116#define AMC6821_TEMP_SLOPE_MASK GENMASK(2, 0)
117#define AMC6821_TEMP_LIMIT_MASK GENMASK(7, 3)
118
b5430a04
TM
119/*
120 * Client data (each client gets its own)
ca3c7b63 121 */
b5430a04
TM
122
123struct amc6821_data {
a051d507 124 struct regmap *regmap;
b5430a04 125 struct mutex update_lock;
b5430a04
TM
126};
127
a051d507
GR
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 */
138static int amc6821_get_auto_point_temps(struct regmap *regmap, int channel, u8 *temps)
28e6274d 139{
c37d0f08
GR
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;
a051d507 147 int err;
28e6274d 148
c37d0f08 149 err = regmap_multi_reg_read(regmap, regs, regvals, 3);
a051d507
GR
150 if (err)
151 return err;
c37d0f08
GR
152 temps[0] = regvals[1];
153 temps[1] = FIELD_GET(AMC6821_TEMP_LIMIT_MASK, regvals[2]) * 4;
a051d507
GR
154
155 /* slope is 32 >> <slope bits> in °C */
c37d0f08
GR
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);
a051d507
GR
159 else
160 temps[2] = 255;
161
162 return 0;
28e6274d 163}
b5430a04 164
e98ab50e 165static int amc6821_temp_read_values(struct regmap *regmap, u32 attr, int channel, long *val)
b5430a04 166{
e98ab50e 167 int reg, err;
a051d507 168 u32 regval;
b5430a04 169
e98ab50e
GR
170 switch (attr) {
171 case hwmon_temp_input:
172 reg = channel ? AMC6821_REG_RTEMP_HI : AMC6821_REG_LTEMP_HI;
b5430a04 173 break;
e98ab50e
GR
174 case hwmon_temp_min:
175 reg = channel ? AMC6821_REG_RTEMP_LIMIT_MIN : AMC6821_REG_LTEMP_LIMIT_MIN;
b5430a04 176 break;
e98ab50e
GR
177 case hwmon_temp_max:
178 reg = channel ? AMC6821_REG_RTEMP_LIMIT_MAX : AMC6821_REG_LTEMP_LIMIT_MAX;
b5430a04 179 break;
e98ab50e
GR
180 case hwmon_temp_crit:
181 reg = channel ? AMC6821_REG_RTEMP_CRIT : AMC6821_REG_LTEMP_CRIT;
b5430a04
TM
182 break;
183 default:
e98ab50e 184 return -EOPNOTSUPP;
b5430a04 185 }
e98ab50e 186 err = regmap_read(regmap, reg, &regval);
a051d507
GR
187 if (err)
188 return err;
e98ab50e
GR
189 *val = sign_extend32(regval, 7) * 1000;
190 return 0;
b5430a04
TM
191}
192
e98ab50e
GR
193static int amc6821_read_alarms(struct regmap *regmap, enum hwmon_sensor_types type,
194 u32 attr, int channel, long *val)
b5430a04 195{
e98ab50e 196 int reg, mask, err;
a051d507 197 u32 regval;
a051d507 198
e98ab50e
GR
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, &regval);
a051d507
GR
236 if (err)
237 return err;
e98ab50e
GR
238 *val = !!(regval & mask);
239 return 0;
b5430a04
TM
240}
241
e98ab50e 242static int amc6821_temp_read(struct device *dev, u32 attr, int channel, long *val)
b5430a04 243{
a051d507 244 struct amc6821_data *data = dev_get_drvdata(dev);
a051d507 245
e98ab50e
GR
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 }
b5430a04
TM
260}
261
e98ab50e 262static int amc6821_temp_write(struct device *dev, u32 attr, int channel, long val)
b5430a04 263{
1276fae2 264 struct amc6821_data *data = dev_get_drvdata(dev);
e98ab50e 265 int reg;
b5430a04 266
e98ab50e 267 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
a051d507 268
e98ab50e
GR
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);
b5430a04
TM
283}
284
e98ab50e 285static int amc6821_pwm_read(struct device *dev, u32 attr, long *val)
b5430a04 286{
a051d507 287 struct amc6821_data *data = dev_get_drvdata(dev);
e98ab50e
GR
288 struct regmap *regmap = data->regmap;
289 u32 regval;
a051d507 290 int err;
a051d507 291
e98ab50e
GR
292 switch (attr) {
293 case hwmon_pwm_enable:
294 err = regmap_read(regmap, AMC6821_REG_CONF1, &regval);
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;
4814241a
GR
312 case hwmon_pwm_mode:
313 err = regmap_read(regmap, AMC6821_REG_CONF2, &regval);
314 if (err)
315 return err;
316 *val = !!(regval & AMC6821_CONF2_TACH_MODE);
317 return 0;
e98ab50e
GR
318 case hwmon_pwm_auto_channels_temp:
319 err = regmap_read(regmap, AMC6821_REG_CONF1, &regval);
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, &regval);
337 if (err)
338 return err;
339 *val = regval;
340 return 0;
a051d507 341 default:
e98ab50e 342 return -EOPNOTSUPP;
a051d507 343 }
b5430a04
TM
344}
345
e98ab50e 346static int amc6821_pwm_write(struct device *dev, u32 attr, long val)
b5430a04 347{
1276fae2 348 struct amc6821_data *data = dev_get_drvdata(dev);
e98ab50e 349 struct regmap *regmap = data->regmap;
a051d507 350 u32 mode;
b5430a04 351
e98ab50e
GR
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);
4814241a
GR
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;
e98ab50e
GR
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}
b5430a04 388
e98ab50e
GR
389static 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;
b5430a04 398 break;
e98ab50e
GR
399 case hwmon_fan_min:
400 reg = AMC6821_REG_TACH_LLIMITL;
b5430a04 401 break;
e98ab50e
GR
402 case hwmon_fan_max:
403 reg = AMC6821_REG_TACH_HLIMITL;
b5430a04 404 break;
e98ab50e
GR
405 case hwmon_fan_target:
406 reg = AMC6821_REG_TACH_SETTINGL;
becbd16e 407 break;
b5430a04 408 default:
e98ab50e 409 return -EOPNOTSUPP;
b5430a04 410 }
a051d507 411
e98ab50e 412 err = regmap_bulk_read(regmap, reg, regs, 2);
a051d507
GR
413 if (err)
414 return err;
415
e98ab50e
GR
416 regval = (regs[1] << 8) | regs[0];
417 *val = regval ? 6000000 / regval : 0;
418
419 return 0;
b5430a04
TM
420}
421
e98ab50e 422static int amc6821_fan_read(struct device *dev, u32 attr, long *val)
b5430a04 423{
a051d507 424 struct amc6821_data *data = dev_get_drvdata(dev);
e98ab50e
GR
425 struct regmap *regmap = data->regmap;
426 u32 regval;
a051d507
GR
427 int err;
428
e98ab50e
GR
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, &regval);
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
448static 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;
a051d507 471 break;
e98ab50e
GR
472 case hwmon_fan_max:
473 reg = AMC6821_REG_TACH_HLIMITL;
a051d507 474 break;
e98ab50e
GR
475 case hwmon_fan_target:
476 if (!val) /* no unlimited target speed */
477 return -EINVAL;
478 reg = AMC6821_REG_TACH_SETTINGL;
a051d507 479 break;
e98ab50e
GR
480 default:
481 return -EOPNOTSUPP;
a051d507
GR
482 }
483
e98ab50e
GR
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);
b5430a04
TM
491}
492
a7818350
GR
493static ssize_t temp_auto_point_temp_show(struct device *dev,
494 struct device_attribute *devattr,
495 char *buf)
b5430a04 496{
a051d507 497 struct amc6821_data *data = dev_get_drvdata(dev);
b5430a04
TM
498 int ix = to_sensor_dev_attr_2(devattr)->index;
499 int nr = to_sensor_dev_attr_2(devattr)->nr;
a051d507
GR
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);
b5430a04
TM
510}
511
a7818350
GR
512static ssize_t pwm1_auto_point_pwm_show(struct device *dev,
513 struct device_attribute *devattr,
514 char *buf)
b5430a04 515{
a051d507 516 struct amc6821_data *data = dev_get_drvdata(dev);
b5430a04 517 int ix = to_sensor_dev_attr(devattr)->index;
a051d507
GR
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);
b5430a04
TM
535}
536
a051d507
GR
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 */
545static inline int set_slope_register(struct regmap *regmap, int channel, u8 *temps)
b5430a04 546{
a051d507
GR
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;
b5430a04 555
a051d507
GR
556 dpwm = 255 - pwm;
557
558 dt = temps[2] - temps[1];
b5430a04 559 for (tmp = 4; tmp > 0; tmp--) {
a051d507 560 if (dt * (32 >> tmp) >= dpwm)
b5430a04
TM
561 break;
562 }
a051d507
GR
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);
b5430a04
TM
568}
569
a7818350
GR
570static ssize_t temp_auto_point_temp_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
b5430a04 573{
a051d507 574 struct amc6821_data *data = dev_get_drvdata(dev);
b5430a04
TM
575 int ix = to_sensor_dev_attr_2(attr)->index;
576 int nr = to_sensor_dev_attr_2(attr)->nr;
a051d507
GR
577 struct regmap *regmap = data->regmap;
578 u8 temps[3], otemps[3];
b5430a04 579 long val;
a051d507
GR
580 int ret;
581
582 ret = kstrtol(buf, 10, &val);
b5430a04
TM
583 if (ret)
584 return ret;
585
b5430a04 586 mutex_lock(&data->update_lock);
a051d507
GR
587
588 ret = amc6821_get_auto_point_temps(data->regmap, nr, temps);
589 if (ret)
590 goto unlock;
cf44819c 591
b5430a04
TM
592 switch (ix) {
593 case 0:
a051d507
GR
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;
b5430a04 605 case 1:
a051d507
GR
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);
b5430a04
TM
616 break;
617 case 2:
a051d507
GR
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);
b5430a04
TM
622 break;
623 default:
a051d507
GR
624 ret = -EINVAL;
625 break;
b5430a04 626 }
a051d507 627unlock:
b5430a04 628 mutex_unlock(&data->update_lock);
a051d507 629 return ret ? : count;
b5430a04
TM
630}
631
a7818350
GR
632static ssize_t pwm1_auto_point_pwm_store(struct device *dev,
633 struct device_attribute *attr,
634 const char *buf, size_t count)
b5430a04 635{
1276fae2 636 struct amc6821_data *data = dev_get_drvdata(dev);
a051d507
GR
637 struct regmap *regmap = data->regmap;
638 int i, ret;
af4d04b8 639 u8 val;
af4d04b8
GR
640
641 ret = kstrtou8(buf, 10, &val);
b5430a04
TM
642 if (ret)
643 return ret;
644
645 mutex_lock(&data->update_lock);
a051d507
GR
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];
b5430a04 652
a051d507
GR
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 }
660unlock:
b5430a04 661 mutex_unlock(&data->update_lock);
a051d507 662 return ret ? : count;
b5430a04
TM
663}
664
a7818350
GR
665static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm1_auto_point_pwm, 0);
666static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm1_auto_point_pwm, 1);
667static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm1_auto_point_pwm, 2);
a7818350 668static SENSOR_DEVICE_ATTR_2_RO(temp1_auto_point1_temp, temp_auto_point_temp,
a051d507 669 0, 0);
a7818350 670static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, temp_auto_point_temp,
a051d507 671 0, 1);
a7818350 672static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point3_temp, temp_auto_point_temp,
a051d507 673 0, 2);
a7818350
GR
674
675static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp_auto_point_temp,
a051d507 676 1, 0);
a7818350 677static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, temp_auto_point_temp,
a051d507 678 1, 1);
a7818350 679static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point3_temp, temp_auto_point_temp,
a051d507 680 1, 2);
b5430a04 681
b5430a04 682static struct attribute *amc6821_attrs[] = {
b5430a04
TM
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};
1276fae2 694ATTRIBUTE_GROUPS(amc6821);
b5430a04 695
e98ab50e
GR
696static 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
711static 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
726static 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) {
4814241a 761 case hwmon_pwm_mode:
e98ab50e
GR
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
775static 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,
4814241a 788 HWMON_PWM_INPUT | HWMON_PWM_ENABLE | HWMON_PWM_MODE |
e98ab50e
GR
789 HWMON_PWM_AUTO_CHANNELS_TEMP),
790 NULL
791};
792
793static const struct hwmon_ops amc6821_hwmon_ops = {
794 .is_visible = amc6821_is_visible,
795 .read = amc6821_read,
796 .write = amc6821_write,
797};
798
799static const struct hwmon_chip_info amc6821_chip_info = {
800 .ops = &amc6821_hwmon_ops,
801 .info = amc6821_info,
802};
803
b5430a04 804/* Return 0 if detection is successful, -ENODEV otherwise */
e98ab50e 805static int amc6821_detect(struct i2c_client *client, struct i2c_board_info *info)
b5430a04
TM
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
ca3c7b63
GR
829 /*
830 * Bit 7 of the address register is ignored, so we can check the
831 * ID registers again
832 */
b5430a04
TM
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);
f2f394db 843 strscpy(info->type, "amc6821", I2C_NAME_SIZE);
b5430a04
TM
844
845 return 0;
846}
847
a051d507 848static int amc6821_init_client(struct amc6821_data *data)
b5430a04 849{
a051d507
GR
850 struct regmap *regmap = data->regmap;
851 int err;
b5430a04
TM
852
853 if (init) {
a051d507
GR
854 err = regmap_set_bits(regmap, AMC6821_REG_CONF4, AMC6821_CONF4_MODE);
855 if (err)
b5430a04 856 return err;
a051d507
GR
857 err = regmap_clear_bits(regmap, AMC6821_REG_CONF3, AMC6821_CONF3_THERM_FAN_EN);
858 if (err)
b5430a04 859 return err;
a051d507
GR
860 err = regmap_clear_bits(regmap, AMC6821_REG_CONF2,
861 AMC6821_CONF2_RTFIE |
862 AMC6821_CONF2_LTOIE |
863 AMC6821_CONF2_RTOIE);
864 if (err)
b5430a04 865 return err;
b5430a04 866
a051d507
GR
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)
b5430a04 873 return err;
b5430a04
TM
874 }
875 return 0;
876}
877
a051d507
GR
878static 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
894static const struct regmap_config amc6821_regmap_config = {
895 .reg_bits = 8,
896 .val_bits = 8,
a051d507
GR
897 .volatile_reg = amc6821_volatile_reg,
898 .cache_type = REGCACHE_MAPLE,
899};
900
67487038 901static int amc6821_probe(struct i2c_client *client)
b5430a04 902{
1276fae2 903 struct device *dev = &client->dev;
28e6274d 904 struct amc6821_data *data;
1276fae2 905 struct device *hwmon_dev;
a051d507 906 struct regmap *regmap;
28e6274d 907 int err;
b5430a04 908
1276fae2 909 data = devm_kzalloc(dev, sizeof(struct amc6821_data), GFP_KERNEL);
28e6274d
AL
910 if (!data)
911 return -ENOMEM;
b5430a04 912
a051d507
GR
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;
b5430a04 918
a051d507 919 err = amc6821_init_client(data);
28e6274d
AL
920 if (err)
921 return err;
b5430a04 922
8f38236d
FB
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
e98ab50e
GR
930 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
931 data, &amc6821_chip_info,
932 amc6821_groups);
1276fae2 933 return PTR_ERR_OR_ZERO(hwmon_dev);
b5430a04
TM
934}
935
28e6274d 936static const struct i2c_device_id amc6821_id[] = {
a7e03f96 937 { "amc6821" },
28e6274d
AL
938 { }
939};
940
941MODULE_DEVICE_TABLE(i2c, amc6821_id);
942
3f003fda
JM
943static const struct of_device_id __maybe_unused amc6821_of_match[] = {
944 {
945 .compatible = "ti,amc6821",
3f003fda 946 },
8f38236d
FB
947 {
948 .compatible = "tsd,mule",
949 },
3f003fda
JM
950 { }
951};
952
953MODULE_DEVICE_TABLE(of, amc6821_of_match);
954
28e6274d
AL
955static struct i2c_driver amc6821_driver = {
956 .class = I2C_CLASS_HWMON,
957 .driver = {
958 .name = "amc6821",
3f003fda 959 .of_match_table = of_match_ptr(amc6821_of_match),
28e6274d 960 },
1975d167 961 .probe = amc6821_probe,
28e6274d
AL
962 .id_table = amc6821_id,
963 .detect = amc6821_detect,
964 .address_list = normal_i2c,
965};
966
f0967eea 967module_i2c_driver(amc6821_driver);
b5430a04
TM
968
969MODULE_LICENSE("GPL");
970MODULE_AUTHOR("T. Mertelj <[email protected]>");
971MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");
This page took 1.112436 seconds and 5 git commands to generate.