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