Blame view
drivers/hwmon/max31790.c
11.7 KB
c942fddf8 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
195a4b429 hwmon: Driver for... |
2 3 4 5 6 |
/* * max31790.c - Part of lm_sensors, Linux kernel modules for hardware * monitoring. * * (C) 2015 by Il Han <corone.il.han@gmail.com> |
195a4b429 hwmon: Driver for... |
7 8 9 10 |
*/ #include <linux/err.h> #include <linux/hwmon.h> |
195a4b429 hwmon: Driver for... |
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
#include <linux/i2c.h> #include <linux/init.h> #include <linux/jiffies.h> #include <linux/module.h> #include <linux/slab.h> /* MAX31790 registers */ #define MAX31790_REG_GLOBAL_CONFIG 0x00 #define MAX31790_REG_FAN_CONFIG(ch) (0x02 + (ch)) #define MAX31790_REG_FAN_DYNAMICS(ch) (0x08 + (ch)) #define MAX31790_REG_FAN_FAULT_STATUS2 0x10 #define MAX31790_REG_FAN_FAULT_STATUS1 0x11 #define MAX31790_REG_TACH_COUNT(ch) (0x18 + (ch) * 2) #define MAX31790_REG_PWM_DUTY_CYCLE(ch) (0x30 + (ch) * 2) #define MAX31790_REG_PWMOUT(ch) (0x40 + (ch) * 2) #define MAX31790_REG_TARGET_COUNT(ch) (0x50 + (ch) * 2) /* Fan Config register bits */ #define MAX31790_FAN_CFG_RPM_MODE 0x80 #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08 #define MAX31790_FAN_CFG_TACH_INPUT 0x01 /* Fan Dynamics register bits */ #define MAX31790_FAN_DYN_SR_SHIFT 5 #define MAX31790_FAN_DYN_SR_MASK 0xE0 #define SR_FROM_REG(reg) (((reg) & MAX31790_FAN_DYN_SR_MASK) \ >> MAX31790_FAN_DYN_SR_SHIFT) #define FAN_RPM_MIN 120 #define FAN_RPM_MAX 7864320 #define RPM_FROM_REG(reg, sr) (((reg) >> 4) ? \ ((60 * (sr) * 8192) / ((reg) >> 4)) : \ FAN_RPM_MAX) #define RPM_TO_REG(rpm, sr) ((60 * (sr) * 8192) / ((rpm) * 2)) #define NR_CHANNEL 6 /* * Client data (each client gets its own) */ struct max31790_data { struct i2c_client *client; struct mutex update_lock; bool valid; /* zero until following fields are valid */ unsigned long last_updated; /* in jiffies */ /* register values */ u8 fan_config[NR_CHANNEL]; u8 fan_dynamics[NR_CHANNEL]; u16 fault_status; u16 tach[NR_CHANNEL * 2]; u16 pwm[NR_CHANNEL]; u16 target_count[NR_CHANNEL]; }; static struct max31790_data *max31790_update_device(struct device *dev) { struct max31790_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; struct max31790_data *ret = data; int i; int rv; mutex_lock(&data->update_lock); if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { rv = i2c_smbus_read_byte_data(client, MAX31790_REG_FAN_FAULT_STATUS1); if (rv < 0) goto abort; data->fault_status = rv & 0x3F; rv = i2c_smbus_read_byte_data(client, MAX31790_REG_FAN_FAULT_STATUS2); if (rv < 0) goto abort; data->fault_status |= (rv & 0x3F) << 6; for (i = 0; i < NR_CHANNEL; i++) { rv = i2c_smbus_read_word_swapped(client, MAX31790_REG_TACH_COUNT(i)); if (rv < 0) goto abort; data->tach[i] = rv; if (data->fan_config[i] & MAX31790_FAN_CFG_TACH_INPUT) { rv = i2c_smbus_read_word_swapped(client, MAX31790_REG_TACH_COUNT(NR_CHANNEL + i)); if (rv < 0) goto abort; data->tach[NR_CHANNEL + i] = rv; } else { rv = i2c_smbus_read_word_swapped(client, MAX31790_REG_PWMOUT(i)); if (rv < 0) goto abort; data->pwm[i] = rv; rv = i2c_smbus_read_word_swapped(client, MAX31790_REG_TARGET_COUNT(i)); if (rv < 0) goto abort; data->target_count[i] = rv; } } data->last_updated = jiffies; data->valid = true; } goto done; abort: data->valid = false; ret = ERR_PTR(rv); done: mutex_unlock(&data->update_lock); return ret; } static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 }; static u8 get_tach_period(u8 fan_dynamics) { return tach_period[SR_FROM_REG(fan_dynamics)]; } static u8 bits_for_tach_period(int rpm) { u8 bits; if (rpm < 500) bits = 0x0; else if (rpm < 1000) bits = 0x1; else if (rpm < 2000) bits = 0x2; else if (rpm < 4000) bits = 0x3; else if (rpm < 8000) bits = 0x4; else bits = 0x5; return bits; } |
54187ff9d hwmon: (max31790)... |
161 162 |
static int max31790_read_fan(struct device *dev, u32 attr, int channel, long *val) |
195a4b429 hwmon: Driver for... |
163 |
{ |
195a4b429 hwmon: Driver for... |
164 |
struct max31790_data *data = max31790_update_device(dev); |
791432cfd hwmon: (max31790)... |
165 |
int sr, rpm; |
195a4b429 hwmon: Driver for... |
166 167 168 |
if (IS_ERR(data)) return PTR_ERR(data); |
54187ff9d hwmon: (max31790)... |
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
switch (attr) { case hwmon_fan_input: sr = get_tach_period(data->fan_dynamics[channel]); rpm = RPM_FROM_REG(data->tach[channel], sr); *val = rpm; return 0; case hwmon_fan_target: sr = get_tach_period(data->fan_dynamics[channel]); rpm = RPM_FROM_REG(data->target_count[channel], sr); *val = rpm; return 0; case hwmon_fan_fault: *val = !!(data->fault_status & (1 << channel)); return 0; default: return -EOPNOTSUPP; } |
195a4b429 hwmon: Driver for... |
186 |
} |
54187ff9d hwmon: (max31790)... |
187 188 |
static int max31790_write_fan(struct device *dev, u32 attr, int channel, long val) |
195a4b429 hwmon: Driver for... |
189 |
{ |
195a4b429 hwmon: Driver for... |
190 191 |
struct max31790_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; |
54187ff9d hwmon: (max31790)... |
192 193 |
int target_count; int err = 0; |
195a4b429 hwmon: Driver for... |
194 195 |
u8 bits; int sr; |
195a4b429 hwmon: Driver for... |
196 197 |
mutex_lock(&data->update_lock); |
54187ff9d hwmon: (max31790)... |
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
switch (attr) { case hwmon_fan_target: val = clamp_val(val, FAN_RPM_MIN, FAN_RPM_MAX); bits = bits_for_tach_period(val); data->fan_dynamics[channel] = ((data->fan_dynamics[channel] & ~MAX31790_FAN_DYN_SR_MASK) | (bits << MAX31790_FAN_DYN_SR_SHIFT)); err = i2c_smbus_write_byte_data(client, MAX31790_REG_FAN_DYNAMICS(channel), data->fan_dynamics[channel]); if (err < 0) break; sr = get_tach_period(data->fan_dynamics[channel]); target_count = RPM_TO_REG(val, sr); target_count = clamp_val(target_count, 0x1, 0x7FF); data->target_count[channel] = target_count << 5; err = i2c_smbus_write_word_swapped(client, MAX31790_REG_TARGET_COUNT(channel), data->target_count[channel]); break; default: err = -EOPNOTSUPP; break; |
195a4b429 hwmon: Driver for... |
225 |
} |
195a4b429 hwmon: Driver for... |
226 |
mutex_unlock(&data->update_lock); |
54187ff9d hwmon: (max31790)... |
227 228 |
return err; } |
195a4b429 hwmon: Driver for... |
229 |
|
54187ff9d hwmon: (max31790)... |
230 231 232 233 234 235 236 237 238 239 |
static umode_t max31790_fan_is_visible(const void *_data, u32 attr, int channel) { const struct max31790_data *data = _data; u8 fan_config = data->fan_config[channel % NR_CHANNEL]; switch (attr) { case hwmon_fan_input: case hwmon_fan_fault: if (channel < NR_CHANNEL || (fan_config & MAX31790_FAN_CFG_TACH_INPUT)) |
dc8dbb4d7 hwmon: (max31790)... |
240 |
return 0444; |
54187ff9d hwmon: (max31790)... |
241 242 243 244 |
return 0; case hwmon_fan_target: if (channel < NR_CHANNEL && !(fan_config & MAX31790_FAN_CFG_TACH_INPUT)) |
dc8dbb4d7 hwmon: (max31790)... |
245 |
return 0644; |
54187ff9d hwmon: (max31790)... |
246 247 248 249 |
return 0; default: return 0; } |
195a4b429 hwmon: Driver for... |
250 |
} |
54187ff9d hwmon: (max31790)... |
251 252 |
static int max31790_read_pwm(struct device *dev, u32 attr, int channel, long *val) |
195a4b429 hwmon: Driver for... |
253 |
{ |
195a4b429 hwmon: Driver for... |
254 |
struct max31790_data *data = max31790_update_device(dev); |
94cdc5608 hwmon: (max31790)... |
255 |
u8 fan_config; |
195a4b429 hwmon: Driver for... |
256 257 258 |
if (IS_ERR(data)) return PTR_ERR(data); |
94cdc5608 hwmon: (max31790)... |
259 |
fan_config = data->fan_config[channel]; |
54187ff9d hwmon: (max31790)... |
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
switch (attr) { case hwmon_pwm_input: *val = data->pwm[channel] >> 8; return 0; case hwmon_pwm_enable: if (fan_config & MAX31790_FAN_CFG_RPM_MODE) *val = 2; else if (fan_config & MAX31790_FAN_CFG_TACH_INPUT_EN) *val = 1; else *val = 0; return 0; default: return -EOPNOTSUPP; } |
195a4b429 hwmon: Driver for... |
275 |
} |
54187ff9d hwmon: (max31790)... |
276 277 |
static int max31790_write_pwm(struct device *dev, u32 attr, int channel, long val) |
195a4b429 hwmon: Driver for... |
278 |
{ |
195a4b429 hwmon: Driver for... |
279 280 |
struct max31790_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; |
54187ff9d hwmon: (max31790)... |
281 282 |
u8 fan_config; int err = 0; |
195a4b429 hwmon: Driver for... |
283 284 |
mutex_lock(&data->update_lock); |
54187ff9d hwmon: (max31790)... |
285 286 287 288 289 290 291 292 293 |
switch (attr) { case hwmon_pwm_input: if (val < 0 || val > 255) { err = -EINVAL; break; } data->pwm[channel] = val << 8; err = i2c_smbus_write_word_swapped(client, MAX31790_REG_PWMOUT(channel), |
dd7406dd3 hwmon: (max31790)... |
294 |
data->pwm[channel]); |
54187ff9d hwmon: (max31790)... |
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
break; case hwmon_pwm_enable: fan_config = data->fan_config[channel]; if (val == 0) { fan_config &= ~(MAX31790_FAN_CFG_TACH_INPUT_EN | MAX31790_FAN_CFG_RPM_MODE); } else if (val == 1) { fan_config = (fan_config | MAX31790_FAN_CFG_TACH_INPUT_EN) & ~MAX31790_FAN_CFG_RPM_MODE; } else if (val == 2) { fan_config |= MAX31790_FAN_CFG_TACH_INPUT_EN | MAX31790_FAN_CFG_RPM_MODE; } else { err = -EINVAL; break; } data->fan_config[channel] = fan_config; err = i2c_smbus_write_byte_data(client, MAX31790_REG_FAN_CONFIG(channel), fan_config); break; default: err = -EOPNOTSUPP; break; } |
195a4b429 hwmon: Driver for... |
321 322 |
mutex_unlock(&data->update_lock); |
54187ff9d hwmon: (max31790)... |
323 |
return err; |
195a4b429 hwmon: Driver for... |
324 |
} |
54187ff9d hwmon: (max31790)... |
325 |
static umode_t max31790_pwm_is_visible(const void *_data, u32 attr, int channel) |
195a4b429 hwmon: Driver for... |
326 |
{ |
54187ff9d hwmon: (max31790)... |
327 328 329 330 331 332 333 |
const struct max31790_data *data = _data; u8 fan_config = data->fan_config[channel]; switch (attr) { case hwmon_pwm_input: case hwmon_pwm_enable: if (!(fan_config & MAX31790_FAN_CFG_TACH_INPUT)) |
dc8dbb4d7 hwmon: (max31790)... |
334 |
return 0644; |
54187ff9d hwmon: (max31790)... |
335 336 337 338 |
return 0; default: return 0; } |
195a4b429 hwmon: Driver for... |
339 |
} |
54187ff9d hwmon: (max31790)... |
340 341 |
static int max31790_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) |
195a4b429 hwmon: Driver for... |
342 |
{ |
54187ff9d hwmon: (max31790)... |
343 344 345 346 347 |
switch (type) { case hwmon_fan: return max31790_read_fan(dev, attr, channel, val); case hwmon_pwm: return max31790_read_pwm(dev, attr, channel, val); |
195a4b429 hwmon: Driver for... |
348 |
default: |
54187ff9d hwmon: (max31790)... |
349 |
return -EOPNOTSUPP; |
195a4b429 hwmon: Driver for... |
350 |
} |
195a4b429 hwmon: Driver for... |
351 |
} |
54187ff9d hwmon: (max31790)... |
352 353 |
static int max31790_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val) |
195a4b429 hwmon: Driver for... |
354 |
{ |
54187ff9d hwmon: (max31790)... |
355 356 357 358 359 360 361 362 363 |
switch (type) { case hwmon_fan: return max31790_write_fan(dev, attr, channel, val); case hwmon_pwm: return max31790_write_pwm(dev, attr, channel, val); default: return -EOPNOTSUPP; } } |
195a4b429 hwmon: Driver for... |
364 |
|
54187ff9d hwmon: (max31790)... |
365 366 367 368 369 370 371 372 373 374 375 376 |
static umode_t max31790_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel) { switch (type) { case hwmon_fan: return max31790_fan_is_visible(data, attr, channel); case hwmon_pwm: return max31790_pwm_is_visible(data, attr, channel); default: return 0; } |
195a4b429 hwmon: Driver for... |
377 |
} |
54187ff9d hwmon: (max31790)... |
378 |
static const struct hwmon_channel_info *max31790_info[] = { |
b605e671c hwmon: (max31790)... |
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT, HWMON_F_INPUT | HWMON_F_FAULT), HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE, HWMON_PWM_INPUT | HWMON_PWM_ENABLE, HWMON_PWM_INPUT | HWMON_PWM_ENABLE, HWMON_PWM_INPUT | HWMON_PWM_ENABLE, HWMON_PWM_INPUT | HWMON_PWM_ENABLE, HWMON_PWM_INPUT | HWMON_PWM_ENABLE), |
54187ff9d hwmon: (max31790)... |
399 400 401 402 403 404 405 406 |
NULL }; static const struct hwmon_ops max31790_hwmon_ops = { .is_visible = max31790_is_visible, .read = max31790_read, .write = max31790_write, }; |
195a4b429 hwmon: Driver for... |
407 |
|
54187ff9d hwmon: (max31790)... |
408 409 410 |
static const struct hwmon_chip_info max31790_chip_info = { .ops = &max31790_hwmon_ops, .info = max31790_info, |
195a4b429 hwmon: Driver for... |
411 |
}; |
195a4b429 hwmon: Driver for... |
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
static int max31790_init_client(struct i2c_client *client, struct max31790_data *data) { int i, rv; for (i = 0; i < NR_CHANNEL; i++) { rv = i2c_smbus_read_byte_data(client, MAX31790_REG_FAN_CONFIG(i)); if (rv < 0) return rv; data->fan_config[i] = rv; rv = i2c_smbus_read_byte_data(client, MAX31790_REG_FAN_DYNAMICS(i)); if (rv < 0) return rv; data->fan_dynamics[i] = rv; } return 0; } |
674870385 hwmon: use simple... |
434 |
static int max31790_probe(struct i2c_client *client) |
195a4b429 hwmon: Driver for... |
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
{ struct i2c_adapter *adapter = client->adapter; struct device *dev = &client->dev; struct max31790_data *data; struct device *hwmon_dev; int err; if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL); if (!data) return -ENOMEM; data->client = client; mutex_init(&data->update_lock); /* * Initialize the max31790 chip */ err = max31790_init_client(client, data); if (err) return err; |
54187ff9d hwmon: (max31790)... |
459 460 461 462 |
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, &max31790_chip_info, NULL); |
195a4b429 hwmon: Driver for... |
463 464 465 466 467 468 469 470 471 472 473 474 |
return PTR_ERR_OR_ZERO(hwmon_dev); } static const struct i2c_device_id max31790_id[] = { { "max31790", 0 }, { } }; MODULE_DEVICE_TABLE(i2c, max31790_id); static struct i2c_driver max31790_driver = { .class = I2C_CLASS_HWMON, |
674870385 hwmon: use simple... |
475 |
.probe_new = max31790_probe, |
195a4b429 hwmon: Driver for... |
476 477 478 479 480 481 482 483 484 485 486 |
.driver = { .name = "max31790", }, .id_table = max31790_id, }; module_i2c_driver(max31790_driver); MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>"); MODULE_DESCRIPTION("MAX31790 sensor driver"); MODULE_LICENSE("GPL"); |