1 /* 2 * Support for the FTS Systemmonitoring Chip "Teutates" 3 * 4 * Copyright (C) 2016 Fujitsu Technology Solutions GmbH, 5 * Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.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/fs.h> 20 #include <linux/hwmon.h> 21 #include <linux/hwmon-sysfs.h> 22 #include <linux/i2c.h> 23 #include <linux/init.h> 24 #include <linux/jiffies.h> 25 #include <linux/module.h> 26 #include <linux/mutex.h> 27 #include <linux/slab.h> 28 #include <linux/sysfs.h> 29 #include <linux/uaccess.h> 30 #include <linux/watchdog.h> 31 32 #define FTS_DEVICE_ID_REG 0x0000 33 #define FTS_DEVICE_REVISION_REG 0x0001 34 #define FTS_DEVICE_STATUS_REG 0x0004 35 #define FTS_SATELLITE_STATUS_REG 0x0005 36 #define FTS_EVENT_STATUS_REG 0x0006 37 #define FTS_GLOBAL_CONTROL_REG 0x0007 38 39 #define FTS_SENSOR_EVENT_REG 0x0010 40 41 #define FTS_FAN_EVENT_REG 0x0014 42 #define FTS_FAN_PRESENT_REG 0x0015 43 44 #define FTS_POWER_ON_TIME_COUNTER_A 0x007A 45 #define FTS_POWER_ON_TIME_COUNTER_B 0x007B 46 #define FTS_POWER_ON_TIME_COUNTER_C 0x007C 47 48 #define FTS_PAGE_SELECT_REG 0x007F 49 50 #define FTS_WATCHDOG_TIME_PRESET 0x000B 51 #define FTS_WATCHDOG_CONTROL 0x5081 52 53 #define FTS_NO_FAN_SENSORS 0x08 54 #define FTS_NO_TEMP_SENSORS 0x10 55 #define FTS_NO_VOLT_SENSORS 0x04 56 57 static struct i2c_device_id fts_id[] = { 58 { "ftsteutates", 0 }, 59 { } 60 }; 61 MODULE_DEVICE_TABLE(i2c, fts_id); 62 63 enum WATCHDOG_RESOLUTION { 64 seconds = 1, 65 minutes = 60 66 }; 67 68 struct fts_data { 69 struct i2c_client *client; 70 /* update sensor data lock */ 71 struct mutex update_lock; 72 /* read/write register lock */ 73 struct mutex access_lock; 74 unsigned long last_updated; /* in jiffies */ 75 struct watchdog_device wdd; 76 enum WATCHDOG_RESOLUTION resolution; 77 bool valid; /* false until following fields are valid */ 78 79 u8 volt[FTS_NO_VOLT_SENSORS]; 80 81 u8 temp_input[FTS_NO_TEMP_SENSORS]; 82 u8 temp_alarm; 83 84 u8 fan_present; 85 u8 fan_input[FTS_NO_FAN_SENSORS]; /* in rps */ 86 u8 fan_source[FTS_NO_FAN_SENSORS]; 87 u8 fan_alarm; 88 }; 89 90 #define FTS_REG_FAN_INPUT(idx) ((idx) + 0x20) 91 #define FTS_REG_FAN_SOURCE(idx) ((idx) + 0x30) 92 #define FTS_REG_FAN_CONTROL(idx) (((idx) << 16) + 0x4881) 93 94 #define FTS_REG_TEMP_INPUT(idx) ((idx) + 0x40) 95 #define FTS_REG_TEMP_CONTROL(idx) (((idx) << 16) + 0x0681) 96 97 #define FTS_REG_VOLT(idx) ((idx) + 0x18) 98 99 /*****************************************************************************/ 100 /* I2C Helper functions */ 101 /*****************************************************************************/ 102 static int fts_read_byte(struct i2c_client *client, unsigned short reg) 103 { 104 int ret; 105 unsigned char page = reg >> 8; 106 struct fts_data *data = dev_get_drvdata(&client->dev); 107 108 mutex_lock(&data->access_lock); 109 110 dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page); 111 ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page); 112 if (ret < 0) 113 goto error; 114 115 reg &= 0xFF; 116 ret = i2c_smbus_read_byte_data(client, reg); 117 dev_dbg(&client->dev, "read - reg: 0x%.02x: val: 0x%.02x\n", reg, ret); 118 119 error: 120 mutex_unlock(&data->access_lock); 121 return ret; 122 } 123 124 static int fts_write_byte(struct i2c_client *client, unsigned short reg, 125 unsigned char value) 126 { 127 int ret; 128 unsigned char page = reg >> 8; 129 struct fts_data *data = dev_get_drvdata(&client->dev); 130 131 mutex_lock(&data->access_lock); 132 133 dev_dbg(&client->dev, "page select - page: 0x%.02x\n", page); 134 ret = i2c_smbus_write_byte_data(client, FTS_PAGE_SELECT_REG, page); 135 if (ret < 0) 136 goto error; 137 138 reg &= 0xFF; 139 dev_dbg(&client->dev, 140 "write - reg: 0x%.02x: val: 0x%.02x\n", reg, value); 141 ret = i2c_smbus_write_byte_data(client, reg, value); 142 143 error: 144 mutex_unlock(&data->access_lock); 145 return ret; 146 } 147 148 /*****************************************************************************/ 149 /* Data Updater Helper function */ 150 /*****************************************************************************/ 151 static int fts_update_device(struct fts_data *data) 152 { 153 int i; 154 int err = 0; 155 156 mutex_lock(&data->update_lock); 157 if (!time_after(jiffies, data->last_updated + 2 * HZ) && data->valid) 158 goto exit; 159 160 err = fts_read_byte(data->client, FTS_DEVICE_STATUS_REG); 161 if (err < 0) 162 goto exit; 163 164 data->valid = !!(err & 0x02); /* Data not ready yet */ 165 if (unlikely(!data->valid)) { 166 err = -EAGAIN; 167 goto exit; 168 } 169 170 err = fts_read_byte(data->client, FTS_FAN_PRESENT_REG); 171 if (err < 0) 172 goto exit; 173 data->fan_present = err; 174 175 err = fts_read_byte(data->client, FTS_FAN_EVENT_REG); 176 if (err < 0) 177 goto exit; 178 data->fan_alarm = err; 179 180 for (i = 0; i < FTS_NO_FAN_SENSORS; i++) { 181 if (data->fan_present & BIT(i)) { 182 err = fts_read_byte(data->client, FTS_REG_FAN_INPUT(i)); 183 if (err < 0) 184 goto exit; 185 data->fan_input[i] = err; 186 187 err = fts_read_byte(data->client, 188 FTS_REG_FAN_SOURCE(i)); 189 if (err < 0) 190 goto exit; 191 data->fan_source[i] = err; 192 } else { 193 data->fan_input[i] = 0; 194 data->fan_source[i] = 0; 195 } 196 } 197 198 err = fts_read_byte(data->client, FTS_SENSOR_EVENT_REG); 199 if (err < 0) 200 goto exit; 201 data->temp_alarm = err; 202 203 for (i = 0; i < FTS_NO_TEMP_SENSORS; i++) { 204 err = fts_read_byte(data->client, FTS_REG_TEMP_INPUT(i)); 205 if (err < 0) 206 goto exit; 207 data->temp_input[i] = err; 208 } 209 210 for (i = 0; i < FTS_NO_VOLT_SENSORS; i++) { 211 err = fts_read_byte(data->client, FTS_REG_VOLT(i)); 212 if (err < 0) 213 goto exit; 214 data->volt[i] = err; 215 } 216 data->last_updated = jiffies; 217 err = 0; 218 exit: 219 mutex_unlock(&data->update_lock); 220 return err; 221 } 222 223 /*****************************************************************************/ 224 /* Watchdog functions */ 225 /*****************************************************************************/ 226 static int fts_wd_set_resolution(struct fts_data *data, 227 enum WATCHDOG_RESOLUTION resolution) 228 { 229 int ret; 230 231 if (data->resolution == resolution) 232 return 0; 233 234 ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL); 235 if (ret < 0) 236 return ret; 237 238 if ((resolution == seconds && ret & BIT(1)) || 239 (resolution == minutes && (ret & BIT(1)) == 0)) { 240 data->resolution = resolution; 241 return 0; 242 } 243 244 if (resolution == seconds) 245 ret |= BIT(1); 246 else 247 ret &= ~BIT(1); 248 249 ret = fts_write_byte(data->client, FTS_WATCHDOG_CONTROL, ret); 250 if (ret < 0) 251 return ret; 252 253 data->resolution = resolution; 254 return ret; 255 } 256 257 static int fts_wd_set_timeout(struct watchdog_device *wdd, unsigned int timeout) 258 { 259 struct fts_data *data; 260 enum WATCHDOG_RESOLUTION resolution = seconds; 261 int ret; 262 263 data = watchdog_get_drvdata(wdd); 264 /* switch watchdog resolution to minutes if timeout does not fit 265 * into a byte 266 */ 267 if (timeout > 0xFF) { 268 timeout = DIV_ROUND_UP(timeout, 60) * 60; 269 resolution = minutes; 270 } 271 272 ret = fts_wd_set_resolution(data, resolution); 273 if (ret < 0) 274 return ret; 275 276 wdd->timeout = timeout; 277 return 0; 278 } 279 280 static int fts_wd_start(struct watchdog_device *wdd) 281 { 282 struct fts_data *data = watchdog_get_drvdata(wdd); 283 284 return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET, 285 wdd->timeout / (u8)data->resolution); 286 } 287 288 static int fts_wd_stop(struct watchdog_device *wdd) 289 { 290 struct fts_data *data; 291 292 data = watchdog_get_drvdata(wdd); 293 return fts_write_byte(data->client, FTS_WATCHDOG_TIME_PRESET, 0); 294 } 295 296 static const struct watchdog_info fts_wd_info = { 297 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, 298 .identity = "FTS Teutates Hardware Watchdog", 299 }; 300 301 static const struct watchdog_ops fts_wd_ops = { 302 .owner = THIS_MODULE, 303 .start = fts_wd_start, 304 .stop = fts_wd_stop, 305 .set_timeout = fts_wd_set_timeout, 306 }; 307 308 static int fts_watchdog_init(struct fts_data *data) 309 { 310 int timeout, ret; 311 312 watchdog_set_drvdata(&data->wdd, data); 313 314 timeout = fts_read_byte(data->client, FTS_WATCHDOG_TIME_PRESET); 315 if (timeout < 0) 316 return timeout; 317 318 /* watchdog not running, set timeout to a default of 60 sec. */ 319 if (timeout == 0) { 320 ret = fts_wd_set_resolution(data, seconds); 321 if (ret < 0) 322 return ret; 323 data->wdd.timeout = 60; 324 } else { 325 ret = fts_read_byte(data->client, FTS_WATCHDOG_CONTROL); 326 if (ret < 0) 327 return ret; 328 329 data->resolution = ret & BIT(1) ? seconds : minutes; 330 data->wdd.timeout = timeout * (u8)data->resolution; 331 set_bit(WDOG_HW_RUNNING, &data->wdd.status); 332 } 333 334 /* Register our watchdog part */ 335 data->wdd.info = &fts_wd_info; 336 data->wdd.ops = &fts_wd_ops; 337 data->wdd.parent = &data->client->dev; 338 data->wdd.min_timeout = 1; 339 340 /* max timeout 255 minutes. */ 341 data->wdd.max_hw_heartbeat_ms = 0xFF * 60 * MSEC_PER_SEC; 342 343 return watchdog_register_device(&data->wdd); 344 } 345 346 /*****************************************************************************/ 347 /* SysFS handler functions */ 348 /*****************************************************************************/ 349 static ssize_t show_in_value(struct device *dev, 350 struct device_attribute *devattr, char *buf) 351 { 352 struct fts_data *data = dev_get_drvdata(dev); 353 int index = to_sensor_dev_attr(devattr)->index; 354 int err; 355 356 err = fts_update_device(data); 357 if (err < 0) 358 return err; 359 360 return sprintf(buf, "%u\n", data->volt[index]); 361 } 362 363 static ssize_t show_temp_value(struct device *dev, 364 struct device_attribute *devattr, char *buf) 365 { 366 struct fts_data *data = dev_get_drvdata(dev); 367 int index = to_sensor_dev_attr(devattr)->index; 368 int err; 369 370 err = fts_update_device(data); 371 if (err < 0) 372 return err; 373 374 return sprintf(buf, "%u\n", data->temp_input[index]); 375 } 376 377 static ssize_t show_temp_fault(struct device *dev, 378 struct device_attribute *devattr, char *buf) 379 { 380 struct fts_data *data = dev_get_drvdata(dev); 381 int index = to_sensor_dev_attr(devattr)->index; 382 int err; 383 384 err = fts_update_device(data); 385 if (err < 0) 386 return err; 387 388 /* 00h Temperature = Sensor Error */ 389 return sprintf(buf, "%d\n", data->temp_input[index] == 0); 390 } 391 392 static ssize_t show_temp_alarm(struct device *dev, 393 struct device_attribute *devattr, char *buf) 394 { 395 struct fts_data *data = dev_get_drvdata(dev); 396 int index = to_sensor_dev_attr(devattr)->index; 397 int err; 398 399 err = fts_update_device(data); 400 if (err < 0) 401 return err; 402 403 return sprintf(buf, "%u\n", !!(data->temp_alarm & BIT(index))); 404 } 405 406 static ssize_t 407 clear_temp_alarm(struct device *dev, struct device_attribute *devattr, 408 const char *buf, size_t count) 409 { 410 struct fts_data *data = dev_get_drvdata(dev); 411 int index = to_sensor_dev_attr(devattr)->index; 412 long ret; 413 414 ret = fts_update_device(data); 415 if (ret < 0) 416 return ret; 417 418 if (kstrtoul(buf, 10, &ret) || ret != 0) 419 return -EINVAL; 420 421 mutex_lock(&data->update_lock); 422 ret = fts_read_byte(data->client, FTS_REG_TEMP_CONTROL(index)); 423 if (ret < 0) 424 goto error; 425 426 ret = fts_write_byte(data->client, FTS_REG_TEMP_CONTROL(index), 427 ret | 0x1); 428 if (ret < 0) 429 goto error; 430 431 data->valid = false; 432 error: 433 mutex_unlock(&data->update_lock); 434 return ret; 435 } 436 437 static ssize_t show_fan_value(struct device *dev, 438 struct device_attribute *devattr, char *buf) 439 { 440 struct fts_data *data = dev_get_drvdata(dev); 441 int index = to_sensor_dev_attr(devattr)->index; 442 int err; 443 444 err = fts_update_device(data); 445 if (err < 0) 446 return err; 447 448 return sprintf(buf, "%u\n", data->fan_input[index]); 449 } 450 451 static ssize_t show_fan_source(struct device *dev, 452 struct device_attribute *devattr, char *buf) 453 { 454 struct fts_data *data = dev_get_drvdata(dev); 455 int index = to_sensor_dev_attr(devattr)->index; 456 int err; 457 458 err = fts_update_device(data); 459 if (err < 0) 460 return err; 461 462 return sprintf(buf, "%u\n", data->fan_source[index]); 463 } 464 465 static ssize_t show_fan_alarm(struct device *dev, 466 struct device_attribute *devattr, char *buf) 467 { 468 struct fts_data *data = dev_get_drvdata(dev); 469 int index = to_sensor_dev_attr(devattr)->index; 470 int err; 471 472 err = fts_update_device(data); 473 if (err < 0) 474 return err; 475 476 return sprintf(buf, "%d\n", !!(data->fan_alarm & BIT(index))); 477 } 478 479 static ssize_t 480 clear_fan_alarm(struct device *dev, struct device_attribute *devattr, 481 const char *buf, size_t count) 482 { 483 struct fts_data *data = dev_get_drvdata(dev); 484 int index = to_sensor_dev_attr(devattr)->index; 485 long ret; 486 487 ret = fts_update_device(data); 488 if (ret < 0) 489 return ret; 490 491 if (kstrtoul(buf, 10, &ret) || ret != 0) 492 return -EINVAL; 493 494 mutex_lock(&data->update_lock); 495 ret = fts_read_byte(data->client, FTS_REG_FAN_CONTROL(index)); 496 if (ret < 0) 497 goto error; 498 499 ret = fts_write_byte(data->client, FTS_REG_FAN_CONTROL(index), 500 ret | 0x1); 501 if (ret < 0) 502 goto error; 503 504 data->valid = false; 505 error: 506 mutex_unlock(&data->update_lock); 507 return ret; 508 } 509 510 /*****************************************************************************/ 511 /* SysFS structs */ 512 /*****************************************************************************/ 513 514 /* Temprature sensors */ 515 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0); 516 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1); 517 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2); 518 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_value, NULL, 3); 519 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_value, NULL, 4); 520 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_value, NULL, 5); 521 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp_value, NULL, 6); 522 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp_value, NULL, 7); 523 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp_value, NULL, 8); 524 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp_value, NULL, 9); 525 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, show_temp_value, NULL, 10); 526 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, show_temp_value, NULL, 11); 527 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, show_temp_value, NULL, 12); 528 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, show_temp_value, NULL, 13); 529 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, show_temp_value, NULL, 14); 530 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO, show_temp_value, NULL, 15); 531 532 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0); 533 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1); 534 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2); 535 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3); 536 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4); 537 static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5); 538 static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_temp_fault, NULL, 6); 539 static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_temp_fault, NULL, 7); 540 static SENSOR_DEVICE_ATTR(temp9_fault, S_IRUGO, show_temp_fault, NULL, 8); 541 static SENSOR_DEVICE_ATTR(temp10_fault, S_IRUGO, show_temp_fault, NULL, 9); 542 static SENSOR_DEVICE_ATTR(temp11_fault, S_IRUGO, show_temp_fault, NULL, 10); 543 static SENSOR_DEVICE_ATTR(temp12_fault, S_IRUGO, show_temp_fault, NULL, 11); 544 static SENSOR_DEVICE_ATTR(temp13_fault, S_IRUGO, show_temp_fault, NULL, 12); 545 static SENSOR_DEVICE_ATTR(temp14_fault, S_IRUGO, show_temp_fault, NULL, 13); 546 static SENSOR_DEVICE_ATTR(temp15_fault, S_IRUGO, show_temp_fault, NULL, 14); 547 static SENSOR_DEVICE_ATTR(temp16_fault, S_IRUGO, show_temp_fault, NULL, 15); 548 549 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 550 clear_temp_alarm, 0); 551 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 552 clear_temp_alarm, 1); 553 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 554 clear_temp_alarm, 2); 555 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 556 clear_temp_alarm, 3); 557 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 558 clear_temp_alarm, 4); 559 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 560 clear_temp_alarm, 5); 561 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 562 clear_temp_alarm, 6); 563 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 564 clear_temp_alarm, 7); 565 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 566 clear_temp_alarm, 8); 567 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 568 clear_temp_alarm, 9); 569 static SENSOR_DEVICE_ATTR(temp11_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 570 clear_temp_alarm, 10); 571 static SENSOR_DEVICE_ATTR(temp12_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 572 clear_temp_alarm, 11); 573 static SENSOR_DEVICE_ATTR(temp13_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 574 clear_temp_alarm, 12); 575 static SENSOR_DEVICE_ATTR(temp14_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 576 clear_temp_alarm, 13); 577 static SENSOR_DEVICE_ATTR(temp15_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 578 clear_temp_alarm, 14); 579 static SENSOR_DEVICE_ATTR(temp16_alarm, S_IRUGO | S_IWUSR, show_temp_alarm, 580 clear_temp_alarm, 15); 581 582 static struct attribute *fts_temp_attrs[] = { 583 &sensor_dev_attr_temp1_input.dev_attr.attr, 584 &sensor_dev_attr_temp2_input.dev_attr.attr, 585 &sensor_dev_attr_temp3_input.dev_attr.attr, 586 &sensor_dev_attr_temp4_input.dev_attr.attr, 587 &sensor_dev_attr_temp5_input.dev_attr.attr, 588 &sensor_dev_attr_temp6_input.dev_attr.attr, 589 &sensor_dev_attr_temp7_input.dev_attr.attr, 590 &sensor_dev_attr_temp8_input.dev_attr.attr, 591 &sensor_dev_attr_temp9_input.dev_attr.attr, 592 &sensor_dev_attr_temp10_input.dev_attr.attr, 593 &sensor_dev_attr_temp11_input.dev_attr.attr, 594 &sensor_dev_attr_temp12_input.dev_attr.attr, 595 &sensor_dev_attr_temp13_input.dev_attr.attr, 596 &sensor_dev_attr_temp14_input.dev_attr.attr, 597 &sensor_dev_attr_temp15_input.dev_attr.attr, 598 &sensor_dev_attr_temp16_input.dev_attr.attr, 599 600 &sensor_dev_attr_temp1_fault.dev_attr.attr, 601 &sensor_dev_attr_temp2_fault.dev_attr.attr, 602 &sensor_dev_attr_temp3_fault.dev_attr.attr, 603 &sensor_dev_attr_temp4_fault.dev_attr.attr, 604 &sensor_dev_attr_temp5_fault.dev_attr.attr, 605 &sensor_dev_attr_temp6_fault.dev_attr.attr, 606 &sensor_dev_attr_temp7_fault.dev_attr.attr, 607 &sensor_dev_attr_temp8_fault.dev_attr.attr, 608 &sensor_dev_attr_temp9_fault.dev_attr.attr, 609 &sensor_dev_attr_temp10_fault.dev_attr.attr, 610 &sensor_dev_attr_temp11_fault.dev_attr.attr, 611 &sensor_dev_attr_temp12_fault.dev_attr.attr, 612 &sensor_dev_attr_temp13_fault.dev_attr.attr, 613 &sensor_dev_attr_temp14_fault.dev_attr.attr, 614 &sensor_dev_attr_temp15_fault.dev_attr.attr, 615 &sensor_dev_attr_temp16_fault.dev_attr.attr, 616 617 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 618 &sensor_dev_attr_temp2_alarm.dev_attr.attr, 619 &sensor_dev_attr_temp3_alarm.dev_attr.attr, 620 &sensor_dev_attr_temp4_alarm.dev_attr.attr, 621 &sensor_dev_attr_temp5_alarm.dev_attr.attr, 622 &sensor_dev_attr_temp6_alarm.dev_attr.attr, 623 &sensor_dev_attr_temp7_alarm.dev_attr.attr, 624 &sensor_dev_attr_temp8_alarm.dev_attr.attr, 625 &sensor_dev_attr_temp9_alarm.dev_attr.attr, 626 &sensor_dev_attr_temp10_alarm.dev_attr.attr, 627 &sensor_dev_attr_temp11_alarm.dev_attr.attr, 628 &sensor_dev_attr_temp12_alarm.dev_attr.attr, 629 &sensor_dev_attr_temp13_alarm.dev_attr.attr, 630 &sensor_dev_attr_temp14_alarm.dev_attr.attr, 631 &sensor_dev_attr_temp15_alarm.dev_attr.attr, 632 &sensor_dev_attr_temp16_alarm.dev_attr.attr, 633 NULL 634 }; 635 636 /* Fans */ 637 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_value, NULL, 0); 638 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_value, NULL, 1); 639 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_value, NULL, 2); 640 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_value, NULL, 3); 641 static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan_value, NULL, 4); 642 static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan_value, NULL, 5); 643 static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan_value, NULL, 6); 644 static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan_value, NULL, 7); 645 646 static SENSOR_DEVICE_ATTR(fan1_source, S_IRUGO, show_fan_source, NULL, 0); 647 static SENSOR_DEVICE_ATTR(fan2_source, S_IRUGO, show_fan_source, NULL, 1); 648 static SENSOR_DEVICE_ATTR(fan3_source, S_IRUGO, show_fan_source, NULL, 2); 649 static SENSOR_DEVICE_ATTR(fan4_source, S_IRUGO, show_fan_source, NULL, 3); 650 static SENSOR_DEVICE_ATTR(fan5_source, S_IRUGO, show_fan_source, NULL, 4); 651 static SENSOR_DEVICE_ATTR(fan6_source, S_IRUGO, show_fan_source, NULL, 5); 652 static SENSOR_DEVICE_ATTR(fan7_source, S_IRUGO, show_fan_source, NULL, 6); 653 static SENSOR_DEVICE_ATTR(fan8_source, S_IRUGO, show_fan_source, NULL, 7); 654 655 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO | S_IWUSR, 656 show_fan_alarm, clear_fan_alarm, 0); 657 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO | S_IWUSR, 658 show_fan_alarm, clear_fan_alarm, 1); 659 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO | S_IWUSR, 660 show_fan_alarm, clear_fan_alarm, 2); 661 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO | S_IWUSR, 662 show_fan_alarm, clear_fan_alarm, 3); 663 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO | S_IWUSR, 664 show_fan_alarm, clear_fan_alarm, 4); 665 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO | S_IWUSR, 666 show_fan_alarm, clear_fan_alarm, 5); 667 static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO | S_IWUSR, 668 show_fan_alarm, clear_fan_alarm, 6); 669 static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO | S_IWUSR, 670 show_fan_alarm, clear_fan_alarm, 7); 671 672 static struct attribute *fts_fan_attrs[] = { 673 &sensor_dev_attr_fan1_input.dev_attr.attr, 674 &sensor_dev_attr_fan2_input.dev_attr.attr, 675 &sensor_dev_attr_fan3_input.dev_attr.attr, 676 &sensor_dev_attr_fan4_input.dev_attr.attr, 677 &sensor_dev_attr_fan5_input.dev_attr.attr, 678 &sensor_dev_attr_fan6_input.dev_attr.attr, 679 &sensor_dev_attr_fan7_input.dev_attr.attr, 680 &sensor_dev_attr_fan8_input.dev_attr.attr, 681 682 &sensor_dev_attr_fan1_source.dev_attr.attr, 683 &sensor_dev_attr_fan2_source.dev_attr.attr, 684 &sensor_dev_attr_fan3_source.dev_attr.attr, 685 &sensor_dev_attr_fan4_source.dev_attr.attr, 686 &sensor_dev_attr_fan5_source.dev_attr.attr, 687 &sensor_dev_attr_fan6_source.dev_attr.attr, 688 &sensor_dev_attr_fan7_source.dev_attr.attr, 689 &sensor_dev_attr_fan8_source.dev_attr.attr, 690 691 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 692 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 693 &sensor_dev_attr_fan3_alarm.dev_attr.attr, 694 &sensor_dev_attr_fan4_alarm.dev_attr.attr, 695 &sensor_dev_attr_fan5_alarm.dev_attr.attr, 696 &sensor_dev_attr_fan6_alarm.dev_attr.attr, 697 &sensor_dev_attr_fan7_alarm.dev_attr.attr, 698 &sensor_dev_attr_fan8_alarm.dev_attr.attr, 699 NULL 700 }; 701 702 /* Voltages */ 703 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_value, NULL, 0); 704 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_value, NULL, 1); 705 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_value, NULL, 2); 706 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_value, NULL, 3); 707 static struct attribute *fts_voltage_attrs[] = { 708 &sensor_dev_attr_in1_input.dev_attr.attr, 709 &sensor_dev_attr_in2_input.dev_attr.attr, 710 &sensor_dev_attr_in3_input.dev_attr.attr, 711 &sensor_dev_attr_in4_input.dev_attr.attr, 712 NULL 713 }; 714 715 static const struct attribute_group fts_voltage_attr_group = { 716 .attrs = fts_voltage_attrs 717 }; 718 719 static const struct attribute_group fts_temp_attr_group = { 720 .attrs = fts_temp_attrs 721 }; 722 723 static const struct attribute_group fts_fan_attr_group = { 724 .attrs = fts_fan_attrs 725 }; 726 727 static const struct attribute_group *fts_attr_groups[] = { 728 &fts_voltage_attr_group, 729 &fts_temp_attr_group, 730 &fts_fan_attr_group, 731 NULL 732 }; 733 734 /*****************************************************************************/ 735 /* Module initialization / remove functions */ 736 /*****************************************************************************/ 737 static int fts_remove(struct i2c_client *client) 738 { 739 struct fts_data *data = dev_get_drvdata(&client->dev); 740 741 watchdog_unregister_device(&data->wdd); 742 return 0; 743 } 744 745 static int fts_probe(struct i2c_client *client, const struct i2c_device_id *id) 746 { 747 u8 revision; 748 struct fts_data *data; 749 int err; 750 s8 deviceid; 751 struct device *hwmon_dev; 752 753 if (client->addr != 0x73) 754 return -ENODEV; 755 756 /* Baseboard Management Controller check */ 757 deviceid = i2c_smbus_read_byte_data(client, FTS_DEVICE_ID_REG); 758 if (deviceid > 0 && (deviceid & 0xF0) == 0x10) { 759 switch (deviceid & 0x0F) { 760 case 0x01: 761 break; 762 default: 763 dev_dbg(&client->dev, 764 "No Baseboard Management Controller\n"); 765 return -ENODEV; 766 } 767 } else { 768 dev_dbg(&client->dev, "No fujitsu board\n"); 769 return -ENODEV; 770 } 771 772 data = devm_kzalloc(&client->dev, sizeof(struct fts_data), 773 GFP_KERNEL); 774 if (!data) 775 return -ENOMEM; 776 777 mutex_init(&data->update_lock); 778 mutex_init(&data->access_lock); 779 data->client = client; 780 dev_set_drvdata(&client->dev, data); 781 782 err = i2c_smbus_read_byte_data(client, FTS_DEVICE_REVISION_REG); 783 if (err < 0) 784 return err; 785 revision = err; 786 787 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, 788 "ftsteutates", 789 data, 790 fts_attr_groups); 791 if (IS_ERR(hwmon_dev)) 792 return PTR_ERR(hwmon_dev); 793 794 err = fts_watchdog_init(data); 795 if (err) 796 return err; 797 798 dev_info(&client->dev, "Detected FTS Teutates chip, revision: %d.%d\n", 799 (revision & 0xF0) >> 4, revision & 0x0F); 800 return 0; 801 } 802 803 /*****************************************************************************/ 804 /* Module Details */ 805 /*****************************************************************************/ 806 static struct i2c_driver fts_driver = { 807 .driver = { 808 .name = "ftsteutates", 809 }, 810 .id_table = fts_id, 811 .probe = fts_probe, 812 .remove = fts_remove, 813 }; 814 815 module_i2c_driver(fts_driver); 816 817 MODULE_AUTHOR("Thilo Cestonaro <thilo.cestonaro@ts.fujitsu.com>"); 818 MODULE_DESCRIPTION("FTS Teutates driver"); 819 MODULE_LICENSE("GPL"); 820