Il Han | 195a4b4 | 2015-08-30 20:44:26 +0900 | [diff] [blame] | 1 | /* |
| 2 | * max31790.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring. |
| 4 | * |
| 5 | * (C) 2015 by Il Han <corone.il.han@gmail.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/hwmon.h> |
| 20 | #include <linux/hwmon-sysfs.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | /* MAX31790 registers */ |
| 28 | #define MAX31790_REG_GLOBAL_CONFIG 0x00 |
| 29 | #define MAX31790_REG_FAN_CONFIG(ch) (0x02 + (ch)) |
| 30 | #define MAX31790_REG_FAN_DYNAMICS(ch) (0x08 + (ch)) |
| 31 | #define MAX31790_REG_FAN_FAULT_STATUS2 0x10 |
| 32 | #define MAX31790_REG_FAN_FAULT_STATUS1 0x11 |
| 33 | #define MAX31790_REG_TACH_COUNT(ch) (0x18 + (ch) * 2) |
| 34 | #define MAX31790_REG_PWM_DUTY_CYCLE(ch) (0x30 + (ch) * 2) |
| 35 | #define MAX31790_REG_PWMOUT(ch) (0x40 + (ch) * 2) |
| 36 | #define MAX31790_REG_TARGET_COUNT(ch) (0x50 + (ch) * 2) |
| 37 | |
| 38 | /* Fan Config register bits */ |
| 39 | #define MAX31790_FAN_CFG_RPM_MODE 0x80 |
| 40 | #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08 |
| 41 | #define MAX31790_FAN_CFG_TACH_INPUT 0x01 |
| 42 | |
| 43 | /* Fan Dynamics register bits */ |
| 44 | #define MAX31790_FAN_DYN_SR_SHIFT 5 |
| 45 | #define MAX31790_FAN_DYN_SR_MASK 0xE0 |
| 46 | #define SR_FROM_REG(reg) (((reg) & MAX31790_FAN_DYN_SR_MASK) \ |
| 47 | >> MAX31790_FAN_DYN_SR_SHIFT) |
| 48 | |
| 49 | #define FAN_RPM_MIN 120 |
| 50 | #define FAN_RPM_MAX 7864320 |
| 51 | |
| 52 | #define RPM_FROM_REG(reg, sr) (((reg) >> 4) ? \ |
| 53 | ((60 * (sr) * 8192) / ((reg) >> 4)) : \ |
| 54 | FAN_RPM_MAX) |
| 55 | #define RPM_TO_REG(rpm, sr) ((60 * (sr) * 8192) / ((rpm) * 2)) |
| 56 | |
| 57 | #define NR_CHANNEL 6 |
| 58 | |
| 59 | /* |
| 60 | * Client data (each client gets its own) |
| 61 | */ |
| 62 | struct max31790_data { |
| 63 | struct i2c_client *client; |
| 64 | struct mutex update_lock; |
| 65 | bool valid; /* zero until following fields are valid */ |
| 66 | unsigned long last_updated; /* in jiffies */ |
| 67 | |
| 68 | /* register values */ |
| 69 | u8 fan_config[NR_CHANNEL]; |
| 70 | u8 fan_dynamics[NR_CHANNEL]; |
| 71 | u16 fault_status; |
| 72 | u16 tach[NR_CHANNEL * 2]; |
| 73 | u16 pwm[NR_CHANNEL]; |
| 74 | u16 target_count[NR_CHANNEL]; |
| 75 | }; |
| 76 | |
| 77 | static struct max31790_data *max31790_update_device(struct device *dev) |
| 78 | { |
| 79 | struct max31790_data *data = dev_get_drvdata(dev); |
| 80 | struct i2c_client *client = data->client; |
| 81 | struct max31790_data *ret = data; |
| 82 | int i; |
| 83 | int rv; |
| 84 | |
| 85 | mutex_lock(&data->update_lock); |
| 86 | |
| 87 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 88 | rv = i2c_smbus_read_byte_data(client, |
| 89 | MAX31790_REG_FAN_FAULT_STATUS1); |
| 90 | if (rv < 0) |
| 91 | goto abort; |
| 92 | data->fault_status = rv & 0x3F; |
| 93 | |
| 94 | rv = i2c_smbus_read_byte_data(client, |
| 95 | MAX31790_REG_FAN_FAULT_STATUS2); |
| 96 | if (rv < 0) |
| 97 | goto abort; |
| 98 | data->fault_status |= (rv & 0x3F) << 6; |
| 99 | |
| 100 | for (i = 0; i < NR_CHANNEL; i++) { |
| 101 | rv = i2c_smbus_read_word_swapped(client, |
| 102 | MAX31790_REG_TACH_COUNT(i)); |
| 103 | if (rv < 0) |
| 104 | goto abort; |
| 105 | data->tach[i] = rv; |
| 106 | |
| 107 | if (data->fan_config[i] |
| 108 | & MAX31790_FAN_CFG_TACH_INPUT) { |
| 109 | rv = i2c_smbus_read_word_swapped(client, |
| 110 | MAX31790_REG_TACH_COUNT(NR_CHANNEL |
| 111 | + i)); |
| 112 | if (rv < 0) |
| 113 | goto abort; |
| 114 | data->tach[NR_CHANNEL + i] = rv; |
| 115 | } else { |
| 116 | rv = i2c_smbus_read_word_swapped(client, |
| 117 | MAX31790_REG_PWMOUT(i)); |
| 118 | if (rv < 0) |
| 119 | goto abort; |
| 120 | data->pwm[i] = rv; |
| 121 | |
| 122 | rv = i2c_smbus_read_word_swapped(client, |
| 123 | MAX31790_REG_TARGET_COUNT(i)); |
| 124 | if (rv < 0) |
| 125 | goto abort; |
| 126 | data->target_count[i] = rv; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | data->last_updated = jiffies; |
| 131 | data->valid = true; |
| 132 | } |
| 133 | goto done; |
| 134 | |
| 135 | abort: |
| 136 | data->valid = false; |
| 137 | ret = ERR_PTR(rv); |
| 138 | |
| 139 | done: |
| 140 | mutex_unlock(&data->update_lock); |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 }; |
| 146 | |
| 147 | static u8 get_tach_period(u8 fan_dynamics) |
| 148 | { |
| 149 | return tach_period[SR_FROM_REG(fan_dynamics)]; |
| 150 | } |
| 151 | |
| 152 | static u8 bits_for_tach_period(int rpm) |
| 153 | { |
| 154 | u8 bits; |
| 155 | |
| 156 | if (rpm < 500) |
| 157 | bits = 0x0; |
| 158 | else if (rpm < 1000) |
| 159 | bits = 0x1; |
| 160 | else if (rpm < 2000) |
| 161 | bits = 0x2; |
| 162 | else if (rpm < 4000) |
| 163 | bits = 0x3; |
| 164 | else if (rpm < 8000) |
| 165 | bits = 0x4; |
| 166 | else |
| 167 | bits = 0x5; |
| 168 | |
| 169 | return bits; |
| 170 | } |
| 171 | |
| 172 | static ssize_t get_fan(struct device *dev, |
| 173 | struct device_attribute *devattr, char *buf) |
| 174 | { |
| 175 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 176 | struct max31790_data *data = max31790_update_device(dev); |
Sudip Mukherjee | 791432c | 2015-09-16 19:32:59 +0530 | [diff] [blame] | 177 | int sr, rpm; |
Il Han | 195a4b4 | 2015-08-30 20:44:26 +0900 | [diff] [blame] | 178 | |
| 179 | if (IS_ERR(data)) |
| 180 | return PTR_ERR(data); |
| 181 | |
Sudip Mukherjee | 791432c | 2015-09-16 19:32:59 +0530 | [diff] [blame] | 182 | sr = get_tach_period(data->fan_dynamics[attr->index]); |
Il Han | 195a4b4 | 2015-08-30 20:44:26 +0900 | [diff] [blame] | 183 | rpm = RPM_FROM_REG(data->tach[attr->index], sr); |
| 184 | |
| 185 | return sprintf(buf, "%d\n", rpm); |
| 186 | } |
| 187 | |
| 188 | static ssize_t get_fan_target(struct device *dev, |
| 189 | struct device_attribute *devattr, char *buf) |
| 190 | { |
| 191 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 192 | struct max31790_data *data = max31790_update_device(dev); |
Sudip Mukherjee | 791432c | 2015-09-16 19:32:59 +0530 | [diff] [blame] | 193 | int sr, rpm; |
Il Han | 195a4b4 | 2015-08-30 20:44:26 +0900 | [diff] [blame] | 194 | |
| 195 | if (IS_ERR(data)) |
| 196 | return PTR_ERR(data); |
| 197 | |
Sudip Mukherjee | 791432c | 2015-09-16 19:32:59 +0530 | [diff] [blame] | 198 | sr = get_tach_period(data->fan_dynamics[attr->index]); |
Il Han | 195a4b4 | 2015-08-30 20:44:26 +0900 | [diff] [blame] | 199 | rpm = RPM_FROM_REG(data->target_count[attr->index], sr); |
| 200 | |
| 201 | return sprintf(buf, "%d\n", rpm); |
| 202 | } |
| 203 | |
| 204 | static ssize_t set_fan_target(struct device *dev, |
| 205 | struct device_attribute *devattr, |
| 206 | const char *buf, size_t count) |
| 207 | { |
| 208 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 209 | struct max31790_data *data = dev_get_drvdata(dev); |
| 210 | struct i2c_client *client = data->client; |
| 211 | u8 bits; |
| 212 | int sr; |
| 213 | int target_count; |
| 214 | unsigned long rpm; |
| 215 | int err; |
| 216 | |
| 217 | err = kstrtoul(buf, 10, &rpm); |
| 218 | if (err) |
| 219 | return err; |
| 220 | |
| 221 | mutex_lock(&data->update_lock); |
| 222 | |
| 223 | rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX); |
| 224 | bits = bits_for_tach_period(rpm); |
| 225 | data->fan_dynamics[attr->index] = |
| 226 | ((data->fan_dynamics[attr->index] |
| 227 | & ~MAX31790_FAN_DYN_SR_MASK) |
| 228 | | (bits << MAX31790_FAN_DYN_SR_SHIFT)); |
| 229 | err = i2c_smbus_write_byte_data(client, |
| 230 | MAX31790_REG_FAN_DYNAMICS(attr->index), |
| 231 | data->fan_dynamics[attr->index]); |
| 232 | |
| 233 | if (err < 0) { |
| 234 | mutex_unlock(&data->update_lock); |
| 235 | return err; |
| 236 | } |
| 237 | |
| 238 | sr = get_tach_period(data->fan_dynamics[attr->index]); |
| 239 | target_count = RPM_TO_REG(rpm, sr); |
| 240 | target_count = clamp_val(target_count, 0x1, 0x7FF); |
| 241 | |
| 242 | data->target_count[attr->index] = target_count << 5; |
| 243 | |
| 244 | err = i2c_smbus_write_word_swapped(client, |
| 245 | MAX31790_REG_TARGET_COUNT(attr->index), |
| 246 | data->target_count[attr->index]); |
| 247 | |
| 248 | mutex_unlock(&data->update_lock); |
| 249 | |
| 250 | if (err < 0) |
| 251 | return err; |
| 252 | |
| 253 | return count; |
| 254 | } |
| 255 | |
| 256 | static ssize_t get_pwm(struct device *dev, |
| 257 | struct device_attribute *devattr, char *buf) |
| 258 | { |
| 259 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 260 | struct max31790_data *data = max31790_update_device(dev); |
| 261 | int pwm; |
| 262 | |
| 263 | if (IS_ERR(data)) |
| 264 | return PTR_ERR(data); |
| 265 | |
| 266 | pwm = data->pwm[attr->index] >> 8; |
| 267 | |
| 268 | return sprintf(buf, "%d\n", pwm); |
| 269 | } |
| 270 | |
| 271 | static ssize_t set_pwm(struct device *dev, |
| 272 | struct device_attribute *devattr, |
| 273 | const char *buf, size_t count) |
| 274 | { |
| 275 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 276 | struct max31790_data *data = dev_get_drvdata(dev); |
| 277 | struct i2c_client *client = data->client; |
| 278 | unsigned long pwm; |
| 279 | int err; |
| 280 | |
| 281 | err = kstrtoul(buf, 10, &pwm); |
| 282 | if (err) |
| 283 | return err; |
| 284 | |
| 285 | if (pwm > 255) |
| 286 | return -EINVAL; |
| 287 | |
| 288 | mutex_lock(&data->update_lock); |
| 289 | |
| 290 | data->pwm[attr->index] = pwm << 8; |
| 291 | err = i2c_smbus_write_word_swapped(client, |
| 292 | MAX31790_REG_PWMOUT(attr->index), |
| 293 | data->pwm[attr->index]); |
| 294 | |
| 295 | mutex_unlock(&data->update_lock); |
| 296 | |
| 297 | if (err < 0) |
| 298 | return err; |
| 299 | |
| 300 | return count; |
| 301 | } |
| 302 | |
| 303 | static ssize_t get_pwm_enable(struct device *dev, |
| 304 | struct device_attribute *devattr, char *buf) |
| 305 | { |
| 306 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 307 | struct max31790_data *data = max31790_update_device(dev); |
| 308 | int mode; |
| 309 | |
| 310 | if (IS_ERR(data)) |
| 311 | return PTR_ERR(data); |
| 312 | |
| 313 | if (data->fan_config[attr->index] & MAX31790_FAN_CFG_RPM_MODE) |
| 314 | mode = 2; |
| 315 | else if (data->fan_config[attr->index] & MAX31790_FAN_CFG_TACH_INPUT_EN) |
| 316 | mode = 1; |
| 317 | else |
| 318 | mode = 0; |
| 319 | |
| 320 | return sprintf(buf, "%d\n", mode); |
| 321 | } |
| 322 | |
| 323 | static ssize_t set_pwm_enable(struct device *dev, |
| 324 | struct device_attribute *devattr, |
| 325 | const char *buf, size_t count) |
| 326 | { |
| 327 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 328 | struct max31790_data *data = dev_get_drvdata(dev); |
| 329 | struct i2c_client *client = data->client; |
| 330 | unsigned long mode; |
| 331 | int err; |
| 332 | |
| 333 | err = kstrtoul(buf, 10, &mode); |
| 334 | if (err) |
| 335 | return err; |
| 336 | |
| 337 | switch (mode) { |
| 338 | case 0: |
| 339 | data->fan_config[attr->index] = |
| 340 | data->fan_config[attr->index] |
| 341 | & ~(MAX31790_FAN_CFG_TACH_INPUT_EN |
| 342 | | MAX31790_FAN_CFG_RPM_MODE); |
| 343 | break; |
| 344 | case 1: |
| 345 | data->fan_config[attr->index] = |
| 346 | (data->fan_config[attr->index] |
| 347 | | MAX31790_FAN_CFG_TACH_INPUT_EN) |
| 348 | & ~MAX31790_FAN_CFG_RPM_MODE; |
| 349 | break; |
| 350 | case 2: |
| 351 | data->fan_config[attr->index] = |
| 352 | data->fan_config[attr->index] |
| 353 | | MAX31790_FAN_CFG_TACH_INPUT_EN |
| 354 | | MAX31790_FAN_CFG_RPM_MODE; |
| 355 | break; |
| 356 | default: |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | |
| 360 | mutex_lock(&data->update_lock); |
| 361 | |
| 362 | err = i2c_smbus_write_byte_data(client, |
| 363 | MAX31790_REG_FAN_CONFIG(attr->index), |
| 364 | data->fan_config[attr->index]); |
| 365 | |
| 366 | mutex_unlock(&data->update_lock); |
| 367 | |
| 368 | if (err < 0) |
| 369 | return err; |
| 370 | |
| 371 | return count; |
| 372 | } |
| 373 | |
| 374 | static ssize_t get_fan_fault(struct device *dev, |
| 375 | struct device_attribute *devattr, char *buf) |
| 376 | { |
| 377 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 378 | struct max31790_data *data = max31790_update_device(dev); |
| 379 | int fault; |
| 380 | |
| 381 | if (IS_ERR(data)) |
| 382 | return PTR_ERR(data); |
| 383 | |
| 384 | fault = !!(data->fault_status & (1 << attr->index)); |
| 385 | |
| 386 | return sprintf(buf, "%d\n", fault); |
| 387 | } |
| 388 | |
| 389 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0); |
| 390 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1); |
| 391 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2); |
| 392 | static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3); |
| 393 | static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, get_fan, NULL, 4); |
| 394 | static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, get_fan, NULL, 5); |
| 395 | |
| 396 | static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_fault, NULL, 0); |
| 397 | static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, get_fan_fault, NULL, 1); |
| 398 | static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, get_fan_fault, NULL, 2); |
| 399 | static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, get_fan_fault, NULL, 3); |
| 400 | static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, get_fan_fault, NULL, 4); |
| 401 | static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, get_fan_fault, NULL, 5); |
| 402 | |
| 403 | static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, get_fan, NULL, 6); |
| 404 | static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, get_fan, NULL, 7); |
| 405 | static SENSOR_DEVICE_ATTR(fan9_input, S_IRUGO, get_fan, NULL, 8); |
| 406 | static SENSOR_DEVICE_ATTR(fan10_input, S_IRUGO, get_fan, NULL, 9); |
| 407 | static SENSOR_DEVICE_ATTR(fan11_input, S_IRUGO, get_fan, NULL, 10); |
| 408 | static SENSOR_DEVICE_ATTR(fan12_input, S_IRUGO, get_fan, NULL, 11); |
| 409 | |
| 410 | static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, get_fan_fault, NULL, 6); |
| 411 | static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, get_fan_fault, NULL, 7); |
| 412 | static SENSOR_DEVICE_ATTR(fan9_fault, S_IRUGO, get_fan_fault, NULL, 8); |
| 413 | static SENSOR_DEVICE_ATTR(fan10_fault, S_IRUGO, get_fan_fault, NULL, 9); |
| 414 | static SENSOR_DEVICE_ATTR(fan11_fault, S_IRUGO, get_fan_fault, NULL, 10); |
| 415 | static SENSOR_DEVICE_ATTR(fan12_fault, S_IRUGO, get_fan_fault, NULL, 11); |
| 416 | |
| 417 | static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, |
| 418 | get_fan_target, set_fan_target, 0); |
| 419 | static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO, |
| 420 | get_fan_target, set_fan_target, 1); |
| 421 | static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO, |
| 422 | get_fan_target, set_fan_target, 2); |
| 423 | static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO, |
| 424 | get_fan_target, set_fan_target, 3); |
| 425 | static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO, |
| 426 | get_fan_target, set_fan_target, 4); |
| 427 | static SENSOR_DEVICE_ATTR(fan6_target, S_IWUSR | S_IRUGO, |
| 428 | get_fan_target, set_fan_target, 5); |
| 429 | |
| 430 | static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0); |
| 431 | static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1); |
| 432 | static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2); |
| 433 | static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3); |
| 434 | static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 4); |
| 435 | static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 5); |
| 436 | |
| 437 | static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, |
| 438 | get_pwm_enable, set_pwm_enable, 0); |
| 439 | static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, |
| 440 | get_pwm_enable, set_pwm_enable, 1); |
| 441 | static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, |
| 442 | get_pwm_enable, set_pwm_enable, 2); |
| 443 | static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, |
| 444 | get_pwm_enable, set_pwm_enable, 3); |
| 445 | static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO, |
| 446 | get_pwm_enable, set_pwm_enable, 4); |
| 447 | static SENSOR_DEVICE_ATTR(pwm6_enable, S_IWUSR | S_IRUGO, |
| 448 | get_pwm_enable, set_pwm_enable, 5); |
| 449 | |
| 450 | static struct attribute *max31790_attrs[] = { |
| 451 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 452 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 453 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 454 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 455 | &sensor_dev_attr_fan5_input.dev_attr.attr, |
| 456 | &sensor_dev_attr_fan6_input.dev_attr.attr, |
| 457 | |
| 458 | &sensor_dev_attr_fan1_fault.dev_attr.attr, |
| 459 | &sensor_dev_attr_fan2_fault.dev_attr.attr, |
| 460 | &sensor_dev_attr_fan3_fault.dev_attr.attr, |
| 461 | &sensor_dev_attr_fan4_fault.dev_attr.attr, |
| 462 | &sensor_dev_attr_fan5_fault.dev_attr.attr, |
| 463 | &sensor_dev_attr_fan6_fault.dev_attr.attr, |
| 464 | |
| 465 | &sensor_dev_attr_fan7_input.dev_attr.attr, |
| 466 | &sensor_dev_attr_fan8_input.dev_attr.attr, |
| 467 | &sensor_dev_attr_fan9_input.dev_attr.attr, |
| 468 | &sensor_dev_attr_fan10_input.dev_attr.attr, |
| 469 | &sensor_dev_attr_fan11_input.dev_attr.attr, |
| 470 | &sensor_dev_attr_fan12_input.dev_attr.attr, |
| 471 | |
| 472 | &sensor_dev_attr_fan7_fault.dev_attr.attr, |
| 473 | &sensor_dev_attr_fan8_fault.dev_attr.attr, |
| 474 | &sensor_dev_attr_fan9_fault.dev_attr.attr, |
| 475 | &sensor_dev_attr_fan10_fault.dev_attr.attr, |
| 476 | &sensor_dev_attr_fan11_fault.dev_attr.attr, |
| 477 | &sensor_dev_attr_fan12_fault.dev_attr.attr, |
| 478 | |
| 479 | &sensor_dev_attr_fan1_target.dev_attr.attr, |
| 480 | &sensor_dev_attr_fan2_target.dev_attr.attr, |
| 481 | &sensor_dev_attr_fan3_target.dev_attr.attr, |
| 482 | &sensor_dev_attr_fan4_target.dev_attr.attr, |
| 483 | &sensor_dev_attr_fan5_target.dev_attr.attr, |
| 484 | &sensor_dev_attr_fan6_target.dev_attr.attr, |
| 485 | |
| 486 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 487 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 488 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 489 | &sensor_dev_attr_pwm4.dev_attr.attr, |
| 490 | &sensor_dev_attr_pwm5.dev_attr.attr, |
| 491 | &sensor_dev_attr_pwm6.dev_attr.attr, |
| 492 | |
| 493 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 494 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 495 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 496 | &sensor_dev_attr_pwm4_enable.dev_attr.attr, |
| 497 | &sensor_dev_attr_pwm5_enable.dev_attr.attr, |
| 498 | &sensor_dev_attr_pwm6_enable.dev_attr.attr, |
| 499 | NULL |
| 500 | }; |
| 501 | |
| 502 | static umode_t max31790_attrs_visible(struct kobject *kobj, |
| 503 | struct attribute *a, int n) |
| 504 | { |
| 505 | struct device *dev = container_of(kobj, struct device, kobj); |
| 506 | struct max31790_data *data = dev_get_drvdata(dev); |
| 507 | struct device_attribute *devattr = |
| 508 | container_of(a, struct device_attribute, attr); |
| 509 | int index = to_sensor_dev_attr(devattr)->index % NR_CHANNEL; |
| 510 | u8 fan_config; |
| 511 | |
| 512 | fan_config = data->fan_config[index]; |
| 513 | |
| 514 | if (n >= NR_CHANNEL * 2 && n < NR_CHANNEL * 4 && |
| 515 | !(fan_config & MAX31790_FAN_CFG_TACH_INPUT)) |
| 516 | return 0; |
| 517 | if (n >= NR_CHANNEL * 4 && (fan_config & MAX31790_FAN_CFG_TACH_INPUT)) |
| 518 | return 0; |
| 519 | |
| 520 | return a->mode; |
| 521 | } |
| 522 | |
| 523 | static const struct attribute_group max31790_group = { |
| 524 | .attrs = max31790_attrs, |
| 525 | .is_visible = max31790_attrs_visible, |
| 526 | }; |
| 527 | __ATTRIBUTE_GROUPS(max31790); |
| 528 | |
| 529 | static int max31790_init_client(struct i2c_client *client, |
| 530 | struct max31790_data *data) |
| 531 | { |
| 532 | int i, rv; |
| 533 | |
| 534 | for (i = 0; i < NR_CHANNEL; i++) { |
| 535 | rv = i2c_smbus_read_byte_data(client, |
| 536 | MAX31790_REG_FAN_CONFIG(i)); |
| 537 | if (rv < 0) |
| 538 | return rv; |
| 539 | data->fan_config[i] = rv; |
| 540 | |
| 541 | rv = i2c_smbus_read_byte_data(client, |
| 542 | MAX31790_REG_FAN_DYNAMICS(i)); |
| 543 | if (rv < 0) |
| 544 | return rv; |
| 545 | data->fan_dynamics[i] = rv; |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static int max31790_probe(struct i2c_client *client, |
| 552 | const struct i2c_device_id *id) |
| 553 | { |
| 554 | struct i2c_adapter *adapter = client->adapter; |
| 555 | struct device *dev = &client->dev; |
| 556 | struct max31790_data *data; |
| 557 | struct device *hwmon_dev; |
| 558 | int err; |
| 559 | |
| 560 | if (!i2c_check_functionality(adapter, |
| 561 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) |
| 562 | return -ENODEV; |
| 563 | |
| 564 | data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL); |
| 565 | if (!data) |
| 566 | return -ENOMEM; |
| 567 | |
| 568 | data->client = client; |
| 569 | mutex_init(&data->update_lock); |
| 570 | |
| 571 | /* |
| 572 | * Initialize the max31790 chip |
| 573 | */ |
| 574 | err = max31790_init_client(client, data); |
| 575 | if (err) |
| 576 | return err; |
| 577 | |
| 578 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, |
| 579 | client->name, data, max31790_groups); |
| 580 | |
| 581 | return PTR_ERR_OR_ZERO(hwmon_dev); |
| 582 | } |
| 583 | |
| 584 | static const struct i2c_device_id max31790_id[] = { |
| 585 | { "max31790", 0 }, |
| 586 | { } |
| 587 | }; |
| 588 | MODULE_DEVICE_TABLE(i2c, max31790_id); |
| 589 | |
| 590 | static struct i2c_driver max31790_driver = { |
| 591 | .class = I2C_CLASS_HWMON, |
| 592 | .probe = max31790_probe, |
| 593 | .driver = { |
| 594 | .name = "max31790", |
| 595 | }, |
| 596 | .id_table = max31790_id, |
| 597 | }; |
| 598 | |
| 599 | module_i2c_driver(max31790_driver); |
| 600 | |
| 601 | MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>"); |
| 602 | MODULE_DESCRIPTION("MAX31790 sensor driver"); |
| 603 | MODULE_LICENSE("GPL"); |