1 /* $NetBSD: emcfan.c,v 1.2 2025/07/08 18:15:03 gutteridge Exp $ */ 2 3 /* 4 * Copyright (c) 2025 Brad Spencer <brad (at) anduin.eldar.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/cdefs.h> 20 __KERNEL_RCSID(0, "$NetBSD: emcfan.c,v 1.2 2025/07/08 18:15:03 gutteridge Exp $"); 21 22 /* 23 * Driver for the EMC-210x and EMC-230x fan controllers on a 24 * I2C bus. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/kernel.h> 30 #include <sys/device.h> 31 #include <sys/module.h> 32 #include <sys/conf.h> 33 #include <sys/sysctl.h> 34 #include <sys/mutex.h> 35 #include <sys/condvar.h> 36 #include <sys/kthread.h> 37 #include <sys/pool.h> 38 #include <sys/kmem.h> 39 40 #include <dev/sysmon/sysmonvar.h> 41 #include <dev/i2c/i2cvar.h> 42 #include <dev/i2c/emcfanreg.h> 43 #include <dev/i2c/emcfanvar.h> 44 #include <dev/i2c/emcfaninfo.h> 45 46 static int emcfan_poke(i2c_tag_t, i2c_addr_t, bool); 47 static int emcfan_match(device_t, cfdata_t, void *); 48 static void emcfan_attach(device_t, device_t, void *); 49 static int emcfan_detach(device_t, int); 50 static void emcfan_refresh(struct sysmon_envsys *, envsys_data_t *); 51 static int emcfan_activate(device_t, enum devact); 52 static int emcfan_verify_sysctl(SYSCTLFN_ARGS); 53 static void emcfan_attach_gpio(struct emcfan_sc *, uint8_t); 54 55 #define EMCFAN_DEBUG 56 #ifdef EMCFAN_DEBUG 57 #define DPRINTF(s, l, x) \ 58 do { \ 59 if (l <= s->sc_emcfandebug) \ 60 printf x; \ 61 } while (/*CONSTCOND*/0) 62 #else 63 #define DPRINTF(s, l, x) 64 #endif 65 66 CFATTACH_DECL_NEW(emcfan, sizeof(struct emcfan_sc), 67 emcfan_match, emcfan_attach, emcfan_detach, emcfan_activate); 68 69 extern struct cfdriver emcfan_cd; 70 71 static dev_type_open(emcfanopen); 72 static dev_type_read(emcfanread); 73 static dev_type_write(emcfanwrite); 74 static dev_type_close(emcfanclose); 75 const struct cdevsw emcfan_cdevsw = { 76 .d_open = emcfanopen, 77 .d_close = emcfanclose, 78 .d_read = emcfanread, 79 .d_write = emcfanwrite, 80 .d_ioctl = noioctl, 81 .d_stop = nostop, 82 .d_tty = notty, 83 .d_poll = nopoll, 84 .d_mmap = nommap, 85 .d_kqfilter = nokqfilter, 86 .d_discard = nodiscard, 87 .d_flag = D_OTHER 88 }; 89 90 static bool 91 emcfan_reg_is_real(struct emcfan_sc *sc, uint8_t reg) 92 { 93 int segment; 94 uint64_t index; 95 96 segment = reg / 64; 97 index = reg % 64; 98 99 DPRINTF(sc, 10, ("%s: void check 1: reg=%02x, segment=%d, index=%jd, sc_info_info=%d\n", __func__, reg, 100 segment, index, sc->sc_info_index)); 101 DPRINTF(sc, 10, ("%s: void check 2: register_void=%jx, shift=%jx\n", __func__, 102 emcfan_chip_infos[sc->sc_info_index].register_void[segment], ((uint64_t)1 << index))); 103 104 return(emcfan_chip_infos[sc->sc_info_index].register_void[segment] & ((uint64_t)1 << index)); 105 } 106 107 static int 108 emcfan_read_registerr(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, 109 uint8_t *res) 110 { 111 int error = 0; 112 113 error = iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, ®, 1, res, 1, 0); 114 115 return error; 116 } 117 118 static int 119 emcfan_read_register(struct emcfan_sc *sc, uint8_t reg, uint8_t *res) 120 { 121 if (emcfan_reg_is_real(sc,reg)) 122 return(emcfan_read_registerr(sc->sc_tag, sc->sc_addr, reg, res)); 123 else 124 *res = EMCFAN_VOID_READ; 125 return 0; 126 } 127 128 static int 129 emcfan_write_registerr(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, 130 uint8_t value) 131 { 132 int error = 0; 133 134 error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, ®, 1, &value, 1, 0); 135 136 return error; 137 } 138 139 static int 140 emcfan_write_register(struct emcfan_sc *sc, uint8_t reg, uint8_t value) 141 { 142 if (emcfan_reg_is_real(sc,reg)) 143 return(emcfan_write_registerr(sc->sc_tag, sc->sc_addr, reg, value)); 144 else 145 return EACCES; 146 } 147 148 static int 149 emcfan_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug) 150 { 151 int error; 152 uint8_t res; 153 154 error = emcfan_read_registerr(tag, addr, EMCFAN_MANUFACTURER_ID, &res); 155 if (matchdebug) { 156 printf("poke X 1: %d %d\n", addr, error); 157 } 158 159 /* Ok.. something was there, but the ID did not match what was expected. 160 * We get away with doing this because the poke is just getting the Manufacturer 161 * ID, which is a fixed value. 162 */ 163 164 if (!error) { 165 if (res != EMCFAN_VALID_MANUFACTURER_ID) 166 error = EIO; 167 } 168 169 return error; 170 } 171 172 static bool 173 emcfan_check_i2c_addr(i2c_addr_t addr) 174 { 175 bool r = false; 176 177 for(int i = 0;i < __arraycount(emcfan_typical_addrs); i++) 178 if (addr == emcfan_typical_addrs[i]) { 179 r = true; 180 break; 181 } 182 183 return(r); 184 } 185 186 static int 187 emcfan_match(device_t parent, cfdata_t match, void *aux) 188 { 189 struct i2c_attach_args *ia = aux; 190 int error, match_result; 191 const bool matchdebug = false; 192 193 if (iic_use_direct_match(ia, match, NULL, &match_result)) 194 return match_result; 195 196 if (matchdebug) { 197 printf("Looking at ia_addr: %x\n",ia->ia_addr); 198 } 199 200 /* Look to see if there is a device indirectly */ 201 202 if (! emcfan_check_i2c_addr(ia->ia_addr)) 203 return 0; 204 205 /* 206 * Check to see if something is really at this i2c address. 207 * This will keep phantom devices from appearing 208 */ 209 if (iic_acquire_bus(ia->ia_tag, 0) != 0) { 210 if (matchdebug) 211 printf("in match acquire bus failed\n"); 212 return 0; 213 } 214 215 error = emcfan_poke(ia->ia_tag, ia->ia_addr, matchdebug); 216 iic_release_bus(ia->ia_tag, 0); 217 218 return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0; 219 } 220 221 static int 222 emcfan_find_info(uint8_t product) 223 { 224 for(int i = 0;i < __arraycount(emcfan_chip_infos); i++) 225 if (product == emcfan_chip_infos[i].product_id) 226 return(i); 227 228 return(-1); 229 } 230 231 static const char * 232 emcfan_product_to_name(uint8_t info_index) 233 { 234 KASSERT(info_index >= 0); 235 236 return(emcfan_chip_infos[info_index].name); 237 } 238 239 int 240 emcfan_verify_sysctl(SYSCTLFN_ARGS) 241 { 242 int error, t; 243 struct sysctlnode node; 244 245 node = *rnode; 246 t = *(int *)rnode->sysctl_data; 247 node.sysctl_data = &t; 248 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 249 if (error || newp == NULL) 250 return error; 251 252 if (t < 0) 253 return EINVAL; 254 255 *(int *)rnode->sysctl_data = t; 256 257 return 0; 258 } 259 260 static int 261 emcfan_sysctl_init(struct emcfan_sc *sc) 262 { 263 int error; 264 const struct sysctlnode *cnode; 265 int sysctlroot_num; 266 char pole_name[8]; 267 268 if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode, 269 0, CTLTYPE_NODE, device_xname(sc->sc_dev), 270 SYSCTL_DESCR("emcfan controls"), NULL, 0, NULL, 0, CTL_HW, 271 CTL_CREATE, CTL_EOL)) != 0) 272 return error; 273 274 sysctlroot_num = cnode->sysctl_num; 275 276 #ifdef EMCFAN_DEBUG 277 if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode, 278 CTLFLAG_READWRITE, CTLTYPE_INT, "debug", 279 SYSCTL_DESCR("Debug level"), emcfan_verify_sysctl, 0, 280 &sc->sc_emcfandebug, 0, CTL_HW, sysctlroot_num, CTL_CREATE, 281 CTL_EOL)) != 0) 282 return error; 283 284 #endif 285 286 if (emcfan_chip_infos[sc->sc_info_index].family == EMCFAN_FAMILY_230X || 287 emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_1 || 288 emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_24 || 289 emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2104 || 290 emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2106) { 291 for(int i=0;i < emcfan_chip_infos[sc->sc_info_index].num_tachs;i++) { 292 snprintf(pole_name,sizeof(pole_name),"poles%d",i+1); 293 if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode, 294 CTLFLAG_READWRITE, CTLTYPE_INT, pole_name, 295 SYSCTL_DESCR("Number of poles"), emcfan_verify_sysctl, 0, 296 &sc->sc_num_poles[i], 0, CTL_HW, sysctlroot_num, CTL_CREATE, 297 CTL_EOL)) != 0) 298 return error; 299 } 300 } 301 if (emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_1 || 302 emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_24) { 303 if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode, 304 CTLFLAG_READWRITE, CTLTYPE_INT, "ftach", 305 SYSCTL_DESCR("ftach frequency"), emcfan_verify_sysctl, 0, 306 &sc->sc_ftach, 0, CTL_HW, sysctlroot_num, CTL_CREATE, 307 CTL_EOL)) != 0) 308 return error; 309 } 310 if (emcfan_chip_infos[sc->sc_info_index].vin4_temp_zone) { 311 if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode, 312 CTLFLAG_READWRITE, CTLTYPE_BOOL, "vin4", 313 SYSCTL_DESCR("Use VIN4 pin as a temperature sensor input"), NULL, 0, 314 &sc->sc_vin4_temp, 0, CTL_HW, sysctlroot_num, CTL_CREATE, 315 CTL_EOL)) != 0) 316 return error; 317 } 318 319 return 0; 320 } 321 322 static void 323 emcfan_attach(device_t parent, device_t self, void *aux) 324 { 325 struct emcfan_sc *sc; 326 struct i2c_attach_args *ia; 327 uint8_t product_id, revision; 328 int error; 329 330 ia = aux; 331 sc = device_private(self); 332 333 sc->sc_dev = self; 334 sc->sc_tag = ia->ia_tag; 335 sc->sc_addr = ia->ia_addr; 336 sc->sc_opened = false; 337 sc->sc_dying = false; 338 sc->sc_ftach = 32000; 339 sc->sc_vin4_temp = false; 340 for(int i=0;i < EMCFAN_NUM_FANS;i++) 341 sc->sc_num_poles[i] = 2; 342 sc->sc_emcfandebug = 0; 343 344 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE); 345 346 aprint_normal("\n"); 347 348 if ((sc->sc_sme = sysmon_envsys_create()) == NULL) { 349 aprint_error_dev(self, 350 "Unable to create sysmon structure\n"); 351 sc->sc_sme = NULL; 352 return; 353 } 354 355 error = iic_acquire_bus(sc->sc_tag, 0); 356 if (error) { 357 aprint_error_dev(self, "Could not acquire iic bus: %d\n", 358 error); 359 goto out; 360 } 361 362 error = emcfan_read_registerr(sc->sc_tag, sc->sc_addr, EMCFAN_PRODUCT_ID, &product_id); 363 if (error != 0) { 364 aprint_error_dev(self, "Could not get the product id\n"); 365 } else { 366 error = emcfan_read_registerr(sc->sc_tag, sc->sc_addr, EMCFAN_REVISION, &revision); 367 if (error != 0) { 368 aprint_error_dev(self, "Could not get the revision of the chip\n"); 369 } 370 } 371 372 iic_release_bus(sc->sc_tag, 0); 373 if (error != 0) { 374 aprint_error_dev(self, "Unable to setup device\n"); 375 goto out; 376 } 377 378 sc->sc_info_index = emcfan_find_info(product_id); 379 if (sc->sc_info_index < 0) { 380 aprint_error_dev(self, "Unknown product id: %02x\n",product_id); 381 goto out; 382 } 383 384 if ((error = emcfan_sysctl_init(sc)) != 0) { 385 sc->sc_emcfanlog = NULL; 386 aprint_error_dev(self, "Can't setup sysctl tree (%d)\n", error); 387 goto out; 388 } 389 390 for(int i=0;i < EMCFAN_NUM_SENSORS;i++) 391 sc->sc_sensor_instances[i].sc_i_member = -1; 392 393 int sensor_instance = 0; 394 /* Set up the tachs */ 395 for(int i = 0;i < emcfan_chip_infos[sc->sc_info_index].num_tachs && 396 sensor_instance < EMCFAN_NUM_SENSORS; 397 i++) { 398 snprintf(sc->sc_sensors[sensor_instance].desc, 399 sizeof(sc->sc_sensors[sensor_instance].desc), 400 "FAN %d",i+1); 401 402 DPRINTF(sc, 2, ("%s: TACH registering fan sensor %d (%s)\n", __func__, 403 sensor_instance, sc->sc_sensors[sensor_instance].desc)); 404 405 sc->sc_sensor_instances[sensor_instance].sc_i_flags = 0; 406 sc->sc_sensor_instances[sensor_instance].sc_i_member = i + 1; 407 sc->sc_sensors[sensor_instance].units = ENVSYS_SFANRPM; 408 sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID; 409 410 error = sysmon_envsys_sensor_attach(sc->sc_sme, 411 &sc->sc_sensors[sensor_instance]); 412 if (error) { 413 aprint_error_dev(self, 414 "Unable to attach sensor %d: %d\n", i, error); 415 goto out; 416 } 417 418 sc->sc_sensor_instances[sensor_instance].sc_i_envnum = 419 sc->sc_sensors[sensor_instance].sensor; 420 421 DPRINTF(sc, 2, ("%s: TACH recorded sensor instance number %d->%d\n", __func__, 422 sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum)); 423 424 sensor_instance++; 425 } 426 427 /* Set up internal temperature sensor */ 428 if (emcfan_chip_infos[sc->sc_info_index].internal_temp_zone) { 429 snprintf(sc->sc_sensors[sensor_instance].desc, 430 sizeof(sc->sc_sensors[sensor_instance].desc), 431 "internal temperature"); 432 433 DPRINTF(sc, 2, ("%s: IT registering internal temperature sensor %d (%s)\n", __func__, 434 sensor_instance, sc->sc_sensors[sensor_instance].desc)); 435 436 sc->sc_sensor_instances[sensor_instance].sc_i_flags = EMCFAN_INTERNAL_TEMP; 437 sc->sc_sensor_instances[sensor_instance].sc_i_member = 1; 438 sc->sc_sensors[sensor_instance].units = ENVSYS_STEMP; 439 sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID; 440 441 error = sysmon_envsys_sensor_attach(sc->sc_sme, 442 &sc->sc_sensors[sensor_instance]); 443 if (error) { 444 aprint_error_dev(self, 445 "Unable to attach internal sensor: %d\n", error); 446 goto out; 447 } 448 449 sc->sc_sensor_instances[sensor_instance].sc_i_envnum = 450 sc->sc_sensors[sensor_instance].sensor; 451 452 DPRINTF(sc, 2, ("%s: IT recorded sensor instance number %d->%d\n", __func__, 453 sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum)); 454 455 sensor_instance++; 456 } 457 458 /* Set up VIN4 temperature sensor */ 459 if (emcfan_chip_infos[sc->sc_info_index].vin4_temp_zone) { 460 snprintf(sc->sc_sensors[sensor_instance].desc, 461 sizeof(sc->sc_sensors[sensor_instance].desc), 462 "VIN4 temperature"); 463 464 DPRINTF(sc, 2, ("%s: registering VIN4 temperature sensor %d (%s)\n", __func__, 465 sensor_instance, sc->sc_sensors[sensor_instance].desc)); 466 467 sc->sc_sensor_instances[sensor_instance].sc_i_flags = EMCFAN_VIN4_TEMP; 468 sc->sc_sensor_instances[sensor_instance].sc_i_member = 1; 469 sc->sc_sensors[sensor_instance].units = ENVSYS_STEMP; 470 sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID; 471 472 error = sysmon_envsys_sensor_attach(sc->sc_sme, 473 &sc->sc_sensors[sensor_instance]); 474 if (error) { 475 aprint_error_dev(self, 476 "Unable to attach VIN4 sensor: %d\n", error); 477 goto out; 478 } 479 480 sc->sc_sensor_instances[sensor_instance].sc_i_envnum = 481 sc->sc_sensors[sensor_instance].sensor; 482 483 DPRINTF(sc, 2, ("%s: VIN4 recorded sensor instance number %d->%d\n", __func__, 484 sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum)); 485 486 sensor_instance++; 487 } 488 489 /* Set up external temperature sensors */ 490 for(int i = 0;i < emcfan_chip_infos[sc->sc_info_index].num_external_temp_zones && 491 sensor_instance < EMCFAN_NUM_SENSORS; 492 i++) { 493 snprintf(sc->sc_sensors[sensor_instance].desc, 494 sizeof(sc->sc_sensors[sensor_instance].desc), 495 "temperature zone %d",i+1); 496 497 DPRINTF(sc, 2, ("%s: ET registering fan sensor %d (%s)\n", __func__, 498 sensor_instance, sc->sc_sensors[sensor_instance].desc)); 499 500 sc->sc_sensor_instances[sensor_instance].sc_i_flags = 0; 501 sc->sc_sensor_instances[sensor_instance].sc_i_member = i + 1; 502 sc->sc_sensors[sensor_instance].units = ENVSYS_STEMP; 503 sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID; 504 505 error = sysmon_envsys_sensor_attach(sc->sc_sme, 506 &sc->sc_sensors[sensor_instance]); 507 if (error) { 508 aprint_error_dev(self, 509 "Unable to attach sensor %d: %d\n", i, error); 510 goto out; 511 } 512 513 sc->sc_sensor_instances[sensor_instance].sc_i_envnum = 514 sc->sc_sensors[sensor_instance].sensor; 515 516 DPRINTF(sc, 2, ("%s: ET recorded sensor instance number %d->%d\n", __func__, 517 sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum)); 518 519 sensor_instance++; 520 } 521 522 sc->sc_sme->sme_name = device_xname(sc->sc_dev); 523 sc->sc_sme->sme_cookie = sc; 524 sc->sc_sme->sme_refresh = emcfan_refresh; 525 526 if (sysmon_envsys_register(sc->sc_sme)) { 527 aprint_error_dev(self, "unable to register with sysmon\n"); 528 sysmon_envsys_destroy(sc->sc_sme); 529 sc->sc_sme = NULL; 530 return; 531 } 532 533 aprint_normal_dev(self, "Microchip Technology %s fan controller, " 534 "Revision: %02x\n", 535 emcfan_product_to_name(sc->sc_info_index), 536 revision); 537 int e = emcfan_chip_infos[sc->sc_info_index].num_external_temp_zones; 538 if (emcfan_chip_infos[sc->sc_info_index].vin4_temp_zone) 539 e++; 540 aprint_normal_dev(self, "Fans: %d, Tachometers: %d, Internal Temperature: %s, External Sensors: %d, GPIO: %s\n", 541 emcfan_chip_infos[sc->sc_info_index].num_fans, 542 emcfan_chip_infos[sc->sc_info_index].num_tachs, 543 (emcfan_chip_infos[sc->sc_info_index].internal_temp_zone) ? "Yes" : "No", 544 e, 545 (emcfan_chip_infos[sc->sc_info_index].num_gpio_pins > 0) ? "Yes" : "No"); 546 547 if (emcfan_chip_infos[sc->sc_info_index].num_gpio_pins > 0) 548 emcfan_attach_gpio(sc, product_id); 549 return; 550 out: 551 sysmon_envsys_destroy(sc->sc_sme); 552 sc->sc_sme = NULL; 553 } 554 555 /* The EMC-2101 is quite a bit different than the other EMC fan controllers. 556 * Handle it differently. 557 */ 558 559 static void 560 emcfan_refresh_2101_tach(struct sysmon_envsys *sme, envsys_data_t *edata, int instance) 561 { 562 struct emcfan_sc *sc = sme->sme_cookie; 563 564 int error; 565 uint8_t tach_high_reg; 566 uint8_t tach_low_reg; 567 uint8_t tach_high; 568 uint8_t tach_low; 569 570 switch(sc->sc_sensor_instances[instance].sc_i_member) { 571 case 1: 572 tach_high_reg = EMCFAN_2101_TACH_HIGH; 573 tach_low_reg = EMCFAN_2101_TACH_LOW; 574 break; 575 default: 576 panic("A 2101 can not have more than one tach\n"); 577 break; 578 }; 579 580 DPRINTF(sc, 2, ("%s: dev=%s, instance=%d, sc_i_member=%d, tach_high_reg=0x%02X, tach_low_reg=0x%02X\n", __func__, 581 device_xname(sc->sc_dev), instance, 582 sc->sc_sensor_instances[instance].sc_i_member, 583 tach_high_reg, tach_low_reg)); 584 585 error = iic_acquire_bus(sc->sc_tag, 0); 586 if (error) { 587 device_printf(sc->sc_dev,"%s: could not acquire I2C bus: %d\n",__func__, error); 588 return; 589 } 590 591 /* There is a interlock thing with the low and high bytes. Read the 592 * low byte first. 593 */ 594 595 error = emcfan_read_register(sc, tach_low_reg, &tach_low); 596 if (error) { 597 device_printf(sc->sc_dev,"%s: could not read tach low register: %d\n",__func__, error); 598 iic_release_bus(sc->sc_tag, 0); 599 return; 600 } 601 error = emcfan_read_register(sc, tach_high_reg, &tach_high); 602 if (error) { 603 device_printf(sc->sc_dev,"%s: could not read tach high register: %d\n",__func__, error); 604 iic_release_bus(sc->sc_tag, 0); 605 return; 606 } 607 608 iic_release_bus(sc->sc_tag, 0); 609 610 uint16_t count; 611 count = tach_high << 8; 612 count |= tach_low; 613 614 DPRINTF(sc, 2, ("%s: instance=%d, tach_high=%d 0x%02X, tach_low=%d 0x%02X, count=%d\n", __func__, 615 instance, tach_high, tach_high, tach_low, tach_low, count)); 616 617 /* 0xffff indicates that the fan is not present, stopped / stalled 618 * or below the RPM that can be measured or the chip is not configured 619 * to read tach signals on the pin, but is being used for an alert 620 */ 621 622 if (count == 0xffff) 623 return; 624 625 /* The formula is: 626 * 627 * rpm = 5400000 / count 628 * 629 */ 630 631 uint64_t irpm; 632 633 irpm = 5400000 / count; 634 635 edata->value_cur = (uint32_t) irpm; 636 edata->state = ENVSYS_SVALID; 637 } 638 639 static void 640 emcfan_refresh_210_346_230x_tach(int product_family, uint8_t product_id, 641 struct sysmon_envsys *sme, envsys_data_t *edata, int instance) 642 { 643 struct emcfan_sc *sc = sme->sme_cookie; 644 645 int error; 646 uint8_t tach_high_reg; 647 uint8_t tach_low_reg; 648 uint8_t fan_config_reg; 649 uint8_t chip_config; 650 uint8_t fan_config; 651 uint8_t tach_high; 652 uint8_t tach_low; 653 int ftach = 32000; 654 int edges; 655 int poles; 656 int m; 657 658 if (product_family == EMCFAN_FAMILY_210X) { 659 switch(sc->sc_sensor_instances[instance].sc_i_member) { 660 case 1: 661 fan_config_reg = EMCFAN_210_346_CONFIG_1; 662 tach_high_reg = EMCFAN_210_346_TACH_1_HIGH; 663 tach_low_reg = EMCFAN_210_346_TACH_1_LOW; 664 break; 665 case 2: 666 fan_config_reg = EMCFAN_210_346_CONFIG_2; 667 tach_high_reg = EMCFAN_210_346_TACH_2_HIGH; 668 tach_low_reg = EMCFAN_210_346_TACH_2_LOW; 669 break; 670 default: 671 panic("210X family do not know how to deal with member: %d\n", 672 sc->sc_sensor_instances[instance].sc_i_member); 673 break; 674 }; 675 } else { 676 switch(sc->sc_sensor_instances[instance].sc_i_member) { 677 case 1: 678 fan_config_reg = EMCFAN_230X_CONFIG_1; 679 tach_high_reg = EMCFAN_230X_TACH_1_HIGH; 680 tach_low_reg = EMCFAN_230X_TACH_1_LOW; 681 break; 682 case 2: 683 fan_config_reg = EMCFAN_230X_CONFIG_2; 684 tach_high_reg = EMCFAN_230X_TACH_2_HIGH; 685 tach_low_reg = EMCFAN_230X_TACH_2_LOW; 686 break; 687 case 3: 688 fan_config_reg = EMCFAN_230X_CONFIG_3; 689 tach_high_reg = EMCFAN_230X_TACH_3_HIGH; 690 tach_low_reg = EMCFAN_230X_TACH_3_LOW; 691 break; 692 case 4: 693 fan_config_reg = EMCFAN_230X_CONFIG_4; 694 tach_high_reg = EMCFAN_230X_TACH_4_HIGH; 695 tach_low_reg = EMCFAN_230X_TACH_4_LOW; 696 break; 697 case 5: 698 fan_config_reg = EMCFAN_230X_CONFIG_5; 699 tach_high_reg = EMCFAN_230X_TACH_5_HIGH; 700 tach_low_reg = EMCFAN_230X_TACH_5_LOW; 701 break; 702 default: 703 panic("230X family do not know how to deal with member: %d\n", 704 sc->sc_sensor_instances[instance].sc_i_member); 705 break; 706 }; 707 } 708 709 DPRINTF(sc, 2, ("%s: dev=%s, instance=%d, sc_i_member=%d, fan_config_reg=0x%02X, tach_high_reg=0x%02X, tach_low_reg=0x%02X\n", __func__, 710 device_xname(sc->sc_dev), instance, 711 sc->sc_sensor_instances[instance].sc_i_member, 712 fan_config_reg, tach_high_reg, tach_low_reg)); 713 714 error = iic_acquire_bus(sc->sc_tag, 0); 715 if (error) { 716 device_printf(sc->sc_dev,"%s: could not acquire I2C bus: %d\n",__func__, error); 717 return; 718 } 719 720 if (product_id == EMCFAN_PRODUCT_2103_1 || 721 product_id == EMCFAN_PRODUCT_2103_24) { 722 ftach = sc->sc_ftach; 723 } else { 724 chip_config = 0x00; 725 if (product_family == EMCFAN_FAMILY_230X) { 726 error = emcfan_read_register(sc, EMCFAN_CHIP_CONFIG, &chip_config); 727 } else { 728 if (product_id == EMCFAN_PRODUCT_2104 || 729 product_id == EMCFAN_PRODUCT_2106) { 730 error = emcfan_read_register(sc, EMCFAN_MUX_PINS, &chip_config); 731 } 732 } 733 if (error) { 734 device_printf(sc->sc_dev,"%s: could not read chip config: %d\n",__func__, error); 735 iic_release_bus(sc->sc_tag, 0); 736 return; 737 } 738 739 /* Figure out if there is an external clock involved */ 740 if (product_family == EMCFAN_FAMILY_230X) { 741 if (chip_config & 0x02) 742 ftach = 32000; 743 else 744 if (chip_config & 0x01) 745 ftach = 32768; 746 else 747 ftach = 32000; 748 } else { 749 if (product_id == EMCFAN_PRODUCT_2104 || 750 product_id == EMCFAN_PRODUCT_2106) { 751 if (chip_config & 0x01) 752 ftach = 32768; 753 else 754 ftach = 32000; 755 } 756 } 757 758 } 759 760 error = emcfan_read_register(sc, fan_config_reg, &fan_config); 761 if (error) { 762 device_printf(sc->sc_dev,"%s: could not read fan config: %d\n",__func__, error); 763 iic_release_bus(sc->sc_tag, 0); 764 return; 765 } 766 767 /* There is a interlock thing with the low and high bytes. Read the 768 * low byte first. 769 */ 770 771 error = emcfan_read_register(sc, tach_low_reg, &tach_low); 772 if (error) { 773 device_printf(sc->sc_dev,"%s: could not read tach low register: %d\n",__func__, error); 774 iic_release_bus(sc->sc_tag, 0); 775 return; 776 } 777 error = emcfan_read_register(sc, tach_high_reg, &tach_high); 778 if (error) { 779 device_printf(sc->sc_dev,"%s: could not read tach high register: %d\n",__func__, error); 780 iic_release_bus(sc->sc_tag, 0); 781 return; 782 } 783 784 iic_release_bus(sc->sc_tag, 0); 785 786 /* Return early if the fan is stalled or not hooked up. It might be better to look at 787 * the stalled fan status register, but that works differently depending on which chip 788 * you are looking at. 789 */ 790 791 if (product_family == EMCFAN_FAMILY_210X) { 792 /* The datasheet is not at all clear as to what will be set in the low byte of the tach 793 * 0xc0, 0xe0 and 0xf0 all seem to depend on the minimum expected rpm and 0xf8 appears 794 * to mean that the fan is stalled in some way. 795 * 796 * Further to confuse matters, some chips may be able to adjust what invalid means. 797 * See the fan config register (0x4A) on the EMC2101 for an example of that. We check 798 * tach_low here just in case these chips can do that too. 799 */ 800 if (tach_high == 0xff && 801 (tach_low == 0xc0 || tach_low == 0xe0 || 802 tach_low == 0xf0 || tach_low == 0xf8 || 803 tach_low == 0xff)) 804 return; 805 } else { 806 /* The datasheet for the 230X family was a little clearer. In that one, if the high byte is 807 * 0xff the tach reading is invalid. 808 */ 809 if (tach_high == 0xff) 810 return; 811 } 812 813 /* Extract the M value, also known as the tach multiplier */ 814 m = fan_config & 0b01100000; 815 m = m >> 5; 816 817 DPRINTF(sc, 2, ("%s: fan_config=%d 0x%02X, raw m=%d 0x%02X\n", 818 __func__, fan_config, fan_config, m, m)); 819 820 m = (1 << m); 821 822 /* Extract the number of configured edges */ 823 edges = fan_config & 0b00011000; 824 edges = edges >> 3; 825 826 DPRINTF(sc, 2, ("%s: fan_config=%d 0x%02X, raw edges=%d 0x%02X\n", 827 __func__, fan_config, fan_config, edges, edges)); 828 829 edges = ((edges + 1) * 2) + 1; 830 831 /* Calculate the tach count, which needs to use bit weights */ 832 int count = 0; 833 count = (tach_high << 5) | tach_low; 834 835 /* The number of poles is a sysctl setting */ 836 poles = sc->sc_num_poles[sc->sc_sensor_instances[instance].sc_i_member - 1]; 837 838 DPRINTF(sc, 2, ("%s: instance=%d, ftach=%d, m=%d, edges=%d, poles=%d, tach_high=%d 0x%02X, tach_low=%d 0x%02X, count=%d\n", __func__, 839 instance, ftach, m, edges, poles, tach_high, tach_high, tach_low, tach_low, count)); 840 841 /* The formula is: 842 * 843 * rpm = 1/poles * ((edges - 1) / count * 1/m) * ftach * 60 844 * 845 * ftach is either 32.000khz or 32.768khz 846 * 847 */ 848 849 int64_t irpm; 850 int ip1, ip2; 851 int64_t ip3; 852 853 ip1 = 10000 / poles; 854 /* 855 printf("poles: %d ; ip1: %d\n",poles,ip1); 856 */ 857 ip2 = 10000 / m; 858 /* 859 printf("m: %d ; ip2: %d\n",m,ip2); 860 */ 861 ip2 = count * ip2; 862 /* 863 printf("count: %d ; ip2: %d\n",count,ip2); 864 */ 865 ip3 = (int64_t)((edges - 1) * (int64_t)100000000000) / (int64_t)ip2; 866 /* 867 printf("edges: %d ; ip3: %d\n",edges,ip3); 868 */ 869 870 irpm = (ip1 * ip3 * ftach * 60) / 100000000000; 871 872 edata->value_cur = (uint32_t) irpm; 873 edata->state = ENVSYS_SVALID; 874 } 875 876 /* These two tables are taken from Appendix A in the 2104 and 2106 datasheet. 877 * The index into the array is the ADC value and the value of the array is a 878 * precomputed kelvin1000 (i.e celcius to kelvin * 1000) temperature. 879 * 880 * There are unusual holes as not all of the ADC values are present in the 881 * *center* of the table these were made into xx.5 temperature values. 882 * 883 * Another quirk is that the table in the datasheets have multiple temperatures 884 * for a particular ADC. This behavior seems more common on the edges of the 885 * table and may make sense. What these tables do, is just take the first 886 * temperature for any ADC value. 887 * 888 */ 889 890 #define EMCFAN_VIN_NO_TEMP -1 891 892 static const int32_t emcfan_vin_temps[] = { 893 EMCFAN_VIN_NO_TEMP, 894 EMCFAN_VIN_NO_TEMP, 895 EMCFAN_VIN_NO_TEMP, 896 EMCFAN_VIN_NO_TEMP, 897 EMCFAN_VIN_NO_TEMP, 898 EMCFAN_VIN_NO_TEMP, 899 EMCFAN_VIN_NO_TEMP, 900 EMCFAN_VIN_NO_TEMP, 901 EMCFAN_VIN_NO_TEMP, 902 EMCFAN_VIN_NO_TEMP, 903 EMCFAN_VIN_NO_TEMP, 904 EMCFAN_VIN_NO_TEMP, 905 EMCFAN_VIN_NO_TEMP, 906 EMCFAN_VIN_NO_TEMP, 907 EMCFAN_VIN_NO_TEMP, 908 EMCFAN_VIN_NO_TEMP, 909 EMCFAN_VIN_NO_TEMP, 910 EMCFAN_VIN_NO_TEMP, 911 EMCFAN_VIN_NO_TEMP, 912 EMCFAN_VIN_NO_TEMP, 913 EMCFAN_VIN_NO_TEMP, 914 EMCFAN_VIN_NO_TEMP, 915 EMCFAN_VIN_NO_TEMP, 916 EMCFAN_VIN_NO_TEMP, 917 EMCFAN_VIN_NO_TEMP, 918 EMCFAN_VIN_NO_TEMP, 919 EMCFAN_VIN_NO_TEMP, 920 EMCFAN_VIN_NO_TEMP, 921 EMCFAN_VIN_NO_TEMP, 922 EMCFAN_VIN_NO_TEMP, 923 EMCFAN_VIN_NO_TEMP, 924 EMCFAN_VIN_NO_TEMP, 925 463150, 926 461150, 927 459150, 928 457150, 929 455150, 930 453150, 931 451150, 932 450150, 933 448150, 934 446150, 935 445150, 936 443150, 937 441150, 938 440150, 939 438150, 940 437150, 941 435150, 942 434150, 943 433150, 944 431150, 945 430150, 946 429150, 947 427150, 948 426150, 949 425150, 950 424150, 951 423150, 952 421150, 953 420150, 954 419150, 955 418150, 956 417150, 957 416150, 958 415150, 959 414150, 960 413150, 961 412150, 962 411150, 963 410150, 964 409150, 965 408150, 966 407150, 967 406150, 968 405150, 969 404150, 970 403150, 971 402150, 972 398150, 973 397150, 974 396150, 975 395150, 976 394650, 977 394150, 978 393150, 979 392150, 980 391650, 981 391150, 982 390150, 983 389150, 984 388650, 985 388150, 986 387150, 987 386650, 988 386150, 989 385150, 990 384150, 991 383650, 992 383150, 993 382150, 994 381650, 995 381150, 996 380150, 997 379650, 998 379150, 999 378150, 1000 377650, 1001 377150, 1002 376650, 1003 376150, 1004 375150, 1005 374650, 1006 374150, 1007 373150, 1008 372650, 1009 372150, 1010 371650, 1011 371150, 1012 370150, 1013 369650, 1014 369150, 1015 368650, 1016 368150, 1017 367150, 1018 366650, 1019 366150, 1020 365650, 1021 365150, 1022 364150, 1023 363650, 1024 363150, 1025 362650, 1026 362150, 1027 361650, 1028 361150, 1029 360150, 1030 359650, 1031 359150, 1032 358150, 1033 357650, 1034 357150, 1035 356650, 1036 356150, 1037 355650, 1038 355150, 1039 354150, 1040 353650, 1041 353150, 1042 352650, 1043 352150, 1044 351650, 1045 351150, 1046 350650, 1047 350150, 1048 349150, 1049 348650, 1050 348150, 1051 347650, 1052 347150, 1053 346150, 1054 345650, 1055 345150, 1056 344650, 1057 344150, 1058 343650, 1059 343150, 1060 342150, 1061 341650, 1062 341150, 1063 340650, 1064 340150, 1065 339150, 1066 338650, 1067 338150, 1068 337650, 1069 337150, 1070 336150, 1071 335650, 1072 335150, 1073 334650, 1074 334150, 1075 333150, 1076 332650, 1077 332150, 1078 331150, 1079 330650, 1080 330150, 1081 329650, 1082 329150, 1083 328150, 1084 327650, 1085 327150, 1086 326150, 1087 325650, 1088 325150, 1089 324150, 1090 323650, 1091 323150, 1092 322150, 1093 321150, 1094 320650, 1095 320150, 1096 319150, 1097 318650, 1098 318150, 1099 317150, 1100 316150, 1101 315150, 1102 314650, 1103 314150, 1104 313150, 1105 312150, 1106 311150, 1107 310650, 1108 310150, 1109 309150, 1110 308150, 1111 307150, 1112 306150, 1113 305150, 1114 304150, 1115 303150, 1116 302150, 1117 301150, 1118 300150, 1119 299150, 1120 298150, 1121 297150, 1122 296150, 1123 295150, 1124 293150, 1125 292150, 1126 291150, 1127 290150, 1128 288150, 1129 287150, 1130 285150, 1131 283150, 1132 282150, 1133 280150, 1134 278150, 1135 276150, 1136 273150, 1137 271150, 1138 268150, 1139 265150, 1140 262150, 1141 259150, 1142 255150, 1143 250150, 1144 244150, 1145 236150, 1146 229150, 1147 228150, 1148 EMCFAN_VIN_NO_TEMP 1149 }; 1150 1151 static const int32_t emcfan_vin_temps_i[] = { 1152 228150, 1153 229150, 1154 236150, 1155 244150, 1156 250150, 1157 255150, 1158 259150, 1159 262150, 1160 265150, 1161 268150, 1162 271150, 1163 273150, 1164 276150, 1165 278150, 1166 280150, 1167 281150, 1168 283150, 1169 285150, 1170 286150, 1171 288150, 1172 289150, 1173 291150, 1174 292150, 1175 293150, 1176 295150, 1177 296150, 1178 297150, 1179 298150, 1180 299150, 1181 300150, 1182 301150, 1183 302150, 1184 303150, 1185 304150, 1186 305150, 1187 306150, 1188 307150, 1189 308150, 1190 309150, 1191 310150, 1192 310650, 1193 311150, 1194 312150, 1195 313150, 1196 314150, 1197 314650, 1198 315150, 1199 316150, 1200 317150, 1201 317650, 1202 318150, 1203 319150, 1204 320150, 1205 320650, 1206 321150, 1207 322150, 1208 323150, 1209 323650, 1210 324150, 1211 325150, 1212 325650, 1213 326150, 1214 327150, 1215 327650, 1216 328150, 1217 329150, 1218 329650, 1219 330150, 1220 330650, 1221 331150, 1222 332150, 1223 332650, 1224 333150, 1225 334150, 1226 334650, 1227 335150, 1228 335650, 1229 336150, 1230 337150, 1231 337650, 1232 338150, 1233 338650, 1234 339150, 1235 340150, 1236 340650, 1237 341150, 1238 341650, 1239 342150, 1240 343150, 1241 343650, 1242 344150, 1243 344650, 1244 345150, 1245 345650, 1246 346150, 1247 347150, 1248 347650, 1249 348150, 1250 348650, 1251 349150, 1252 350150, 1253 350650, 1254 351150, 1255 351650, 1256 352150, 1257 352650, 1258 353150, 1259 353650, 1260 354150, 1261 355150, 1262 355650, 1263 356150, 1264 356650, 1265 357150, 1266 357650, 1267 358150, 1268 359150, 1269 359650, 1270 360150, 1271 360650, 1272 361150, 1273 362150, 1274 362650, 1275 363150, 1276 363650, 1277 364150, 1278 365150, 1279 365650, 1280 366150, 1281 366650, 1282 367150, 1283 368150, 1284 368650, 1285 369150, 1286 369650, 1287 370150, 1288 371150, 1289 371650, 1290 372150, 1291 372650, 1292 373150, 1293 374150, 1294 374650, 1295 375150, 1296 376150, 1297 376650, 1298 377150, 1299 377650, 1300 378150, 1301 379150, 1302 379650, 1303 380150, 1304 381150, 1305 381650, 1306 382150, 1307 383150, 1308 383650, 1309 384150, 1310 385150, 1311 386150, 1312 386650, 1313 387150, 1314 388150, 1315 388650, 1316 389150, 1317 390150, 1318 391150, 1319 391650, 1320 392150, 1321 393150, 1322 394150, 1323 394650, 1324 395150, 1325 396150, 1326 397150, 1327 398150, 1328 402150, 1329 403150, 1330 404150, 1331 405150, 1332 406150, 1333 407150, 1334 408150, 1335 409150, 1336 410150, 1337 411150, 1338 412150, 1339 413150, 1340 414150, 1341 415150, 1342 416150, 1343 417150, 1344 418150, 1345 419150, 1346 420150, 1347 421150, 1348 423150, 1349 424150, 1350 425150, 1351 426150, 1352 427150, 1353 429150, 1354 430150, 1355 431150, 1356 433150, 1357 434150, 1358 435150, 1359 437150, 1360 438150, 1361 440150, 1362 441150, 1363 443150, 1364 445150, 1365 446150, 1366 448150, 1367 450150, 1368 451150, 1369 453150, 1370 455150, 1371 457150, 1372 459150, 1373 461150, 1374 463150, 1375 EMCFAN_VIN_NO_TEMP, 1376 EMCFAN_VIN_NO_TEMP, 1377 EMCFAN_VIN_NO_TEMP, 1378 EMCFAN_VIN_NO_TEMP, 1379 EMCFAN_VIN_NO_TEMP, 1380 EMCFAN_VIN_NO_TEMP, 1381 EMCFAN_VIN_NO_TEMP, 1382 EMCFAN_VIN_NO_TEMP, 1383 EMCFAN_VIN_NO_TEMP, 1384 EMCFAN_VIN_NO_TEMP, 1385 EMCFAN_VIN_NO_TEMP, 1386 EMCFAN_VIN_NO_TEMP, 1387 EMCFAN_VIN_NO_TEMP, 1388 EMCFAN_VIN_NO_TEMP, 1389 EMCFAN_VIN_NO_TEMP, 1390 EMCFAN_VIN_NO_TEMP, 1391 EMCFAN_VIN_NO_TEMP, 1392 EMCFAN_VIN_NO_TEMP, 1393 EMCFAN_VIN_NO_TEMP, 1394 EMCFAN_VIN_NO_TEMP, 1395 EMCFAN_VIN_NO_TEMP, 1396 EMCFAN_VIN_NO_TEMP, 1397 EMCFAN_VIN_NO_TEMP, 1398 EMCFAN_VIN_NO_TEMP, 1399 EMCFAN_VIN_NO_TEMP, 1400 EMCFAN_VIN_NO_TEMP, 1401 EMCFAN_VIN_NO_TEMP, 1402 EMCFAN_VIN_NO_TEMP, 1403 EMCFAN_VIN_NO_TEMP, 1404 EMCFAN_VIN_NO_TEMP, 1405 EMCFAN_VIN_NO_TEMP, 1406 EMCFAN_VIN_NO_TEMP, 1407 EMCFAN_VIN_NO_TEMP 1408 }; 1409 1410 static void 1411 emcfan_refresh_temp(int product_family, uint8_t product_id, 1412 struct sysmon_envsys *sme, envsys_data_t *edata, int instance) 1413 { 1414 struct emcfan_sc *sc = sme->sme_cookie; 1415 1416 int error; 1417 uint8_t temp_config; 1418 uint8_t raw_temp_config_3; 1419 uint8_t temp_config_3; 1420 uint8_t temp_high; 1421 uint8_t temp_low; 1422 uint8_t external_temp_high_reg; 1423 uint8_t external_temp_low_reg; 1424 bool is_internal = false; 1425 bool is_vin4 = false; 1426 bool using_apd = false; 1427 bool using_vin = false; 1428 bool inverted = false; 1429 1430 is_internal = sc->sc_sensor_instances[instance].sc_i_flags & EMCFAN_INTERNAL_TEMP; 1431 is_vin4 = sc->sc_sensor_instances[instance].sc_i_flags & EMCFAN_VIN4_TEMP; 1432 1433 error = iic_acquire_bus(sc->sc_tag, 0); 1434 if (error) { 1435 device_printf(sc->sc_dev,"%s: could not acquire I2C bus: %d\n",__func__, error); 1436 return; 1437 } 1438 1439 if (is_internal) { 1440 /* There might be a data interlock thing going on with the high and low 1441 * registers, to make sure, read the high one first. This works in the 1442 * opposite of the tach. 1443 */ 1444 error = emcfan_read_register(sc, EMCFAN_INTERNAL_TEMP_HIGH, &temp_high); 1445 if (error) { 1446 device_printf(sc->sc_dev,"%s: could not read internal temp high: %d\n",__func__, error); 1447 iic_release_bus(sc->sc_tag, 0); 1448 return; 1449 } 1450 /* The 2101 does not have fractions on the internal temperature sensor */ 1451 if (product_id == EMCFAN_PRODUCT_2101) { 1452 temp_low = 0; 1453 } else { 1454 error = emcfan_read_register(sc, EMCFAN_INTERNAL_TEMP_LOW, &temp_low); 1455 if (error) { 1456 device_printf(sc->sc_dev,"%s: could not read internal temp low: %d\n",__func__, error); 1457 iic_release_bus(sc->sc_tag, 0); 1458 return; 1459 } 1460 } 1461 } else { 1462 if (is_vin4) { 1463 if (sc->sc_vin4_temp) { 1464 using_vin = true; 1465 1466 error = emcfan_read_register(sc, EMCFAN_CHIP_CONFIG, &temp_config); 1467 if (error) { 1468 device_printf(sc->sc_dev,"%s: could not read chip config register: %d\n",__func__, error); 1469 iic_release_bus(sc->sc_tag, 0); 1470 return; 1471 } 1472 inverted = temp_config & 0x80; 1473 1474 error = emcfan_read_register(sc, EMCFAN_VIN4_VOLTAGE, &temp_high); 1475 if (error) { 1476 device_printf(sc->sc_dev,"%s: could not read external temp high: %d\n",__func__, error); 1477 iic_release_bus(sc->sc_tag, 0); 1478 return; 1479 } 1480 } else { 1481 iic_release_bus(sc->sc_tag, 0); 1482 return; 1483 } 1484 } else { 1485 /* The 2101 has its external sensor on a different set of registers 1486 * than the rest. 1487 */ 1488 if (product_id == EMCFAN_PRODUCT_2101) { 1489 error = emcfan_read_register(sc, EMCFAN_2101_EXTERNAL_TEMP_LOW, &temp_low); 1490 if (error) { 1491 device_printf(sc->sc_dev,"%s: could not read external temp low: %d\n",__func__, error); 1492 iic_release_bus(sc->sc_tag, 0); 1493 return; 1494 } 1495 error = emcfan_read_register(sc, EMCFAN_2101_EXTERNAL_TEMP_HIGH, &temp_high); 1496 if (error) { 1497 device_printf(sc->sc_dev,"%s: could not read external temp high: %d\n",__func__, error); 1498 iic_release_bus(sc->sc_tag, 0); 1499 return; 1500 } 1501 } else { 1502 switch(sc->sc_sensor_instances[instance].sc_i_member) { 1503 case 1: 1504 external_temp_high_reg = EMCFAN_EXTERNAL_1_TEMP_HIGH; 1505 external_temp_low_reg = EMCFAN_EXTERNAL_1_TEMP_LOW; 1506 break; 1507 case 2: 1508 external_temp_high_reg = EMCFAN_EXTERNAL_2_TEMP_HIGH; 1509 external_temp_low_reg = EMCFAN_EXTERNAL_2_TEMP_LOW; 1510 break; 1511 case 3: 1512 external_temp_high_reg = EMCFAN_EXTERNAL_3_TEMP_HIGH; 1513 external_temp_low_reg = EMCFAN_EXTERNAL_3_TEMP_LOW; 1514 break; 1515 case 4: 1516 external_temp_high_reg = EMCFAN_EXTERNAL_4_TEMP_HIGH; 1517 external_temp_low_reg = EMCFAN_EXTERNAL_4_TEMP_LOW; 1518 break; 1519 default: 1520 panic("Unknown member: %d\n", 1521 sc->sc_sensor_instances[instance].sc_i_member); 1522 break; 1523 }; 1524 1525 /* The 2103-2, 2103-4, 2104 and 2106 can use APD mode. This is a method 1526 * of using two sensors in parallel on a single set of pins. The way one 1527 * wires this up is in the datasheets for the chip. 1528 */ 1529 1530 if (product_id == EMCFAN_PRODUCT_2103_24 || 1531 product_id == EMCFAN_PRODUCT_2104 || 1532 product_id == EMCFAN_PRODUCT_2106) { 1533 error = emcfan_read_register(sc, EMCFAN_CHIP_CONFIG, &temp_config); 1534 if (error) { 1535 device_printf(sc->sc_dev,"%s: could not read chip config register: %d\n",__func__, error); 1536 iic_release_bus(sc->sc_tag, 0); 1537 return; 1538 } 1539 1540 using_apd = temp_config & 0x01; 1541 } 1542 1543 /* The 2104, 2105 and 2106 has some other special abilities, such as being 1544 * able to use thermistors. 1545 */ 1546 1547 if (product_id == EMCFAN_PRODUCT_2104 || 1548 product_id == EMCFAN_PRODUCT_2106) { 1549 error = emcfan_read_register(sc, EMCFAN_TEMP_CONFIG_3, &raw_temp_config_3); 1550 if (error) { 1551 device_printf(sc->sc_dev,"%s: could not read temperature config register: %d\n",__func__, error); 1552 iic_release_bus(sc->sc_tag, 0); 1553 return; 1554 } 1555 switch(sc->sc_sensor_instances[instance].sc_i_member) { 1556 case 1: 1557 temp_config_3 = raw_temp_config_3 & 0x03; 1558 break; 1559 case 2: 1560 temp_config_3 = raw_temp_config_3 >> 2; 1561 temp_config_3 = temp_config_3 & 0x03; 1562 break; 1563 case 3: 1564 temp_config_3 = raw_temp_config_3 >> 4; 1565 temp_config_3 = temp_config_3 & 0x03; 1566 break; 1567 default: 1568 temp_config_3 = 0; 1569 break; 1570 }; 1571 1572 using_vin = temp_config_3 & 0x02; 1573 inverted = temp_config_3 & 0x01; 1574 1575 1576 /* There is a strange situation if sensor 3 is being used as a VIN 1577 * sensor, then sensor 4 is not available at all. Note that this 1578 * sensor 4 is *NOT* the sensor that might be attached to the VIN4 1579 * pin. 1580 */ 1581 1582 if (sc->sc_sensor_instances[instance].sc_i_member == 4 && 1583 raw_temp_config_3 & 0x20) { 1584 iic_release_bus(sc->sc_tag, 0); 1585 return; 1586 } 1587 } 1588 1589 if (product_id == EMCFAN_PRODUCT_2103_24) { 1590 /* The anti-parallel mode, apd, must be enabled before sensor 3 will 1591 * be available. 1592 */ 1593 if (!using_apd && 1594 sc->sc_sensor_instances[instance].sc_i_member == 3) { 1595 iic_release_bus(sc->sc_tag, 0); 1596 return; 1597 } 1598 } 1599 1600 if (product_id == EMCFAN_PRODUCT_2104 || 1601 product_id == EMCFAN_PRODUCT_2106) { 1602 /* The anti-parallel mode, apd, must be enabled before sensor 4 will 1603 * be available. This, of course, might conflict if sensor 3 is a VIN 1604 * sensor. Don't do that.... 1605 */ 1606 if (!using_apd && 1607 sc->sc_sensor_instances[instance].sc_i_member == 4) { 1608 iic_release_bus(sc->sc_tag, 0); 1609 return; 1610 } 1611 } 1612 1613 /* There is a data interlock thing going on with the high and low 1614 * registers, but it works the opposite from the tach. 1615 */ 1616 1617 error = emcfan_read_register(sc, external_temp_high_reg, &temp_high); 1618 if (error) { 1619 device_printf(sc->sc_dev,"%s: could not read external temp high: %d\n",__func__, error); 1620 iic_release_bus(sc->sc_tag, 0); 1621 return; 1622 } 1623 if (using_vin) { 1624 temp_low = 0; 1625 } else { 1626 error = emcfan_read_register(sc, external_temp_low_reg, &temp_low); 1627 if (error) { 1628 device_printf(sc->sc_dev,"%s: could not read external temp low: %d\n",__func__, error); 1629 iic_release_bus(sc->sc_tag, 0); 1630 return; 1631 } 1632 } 1633 } 1634 } 1635 } 1636 1637 iic_release_bus(sc->sc_tag, 0); 1638 1639 /* It appears that on the 2101, if the high byte is 0x7f and the low byte is 0,\ 1640 * then there is a fault or no sensor. 1641 */ 1642 1643 if (product_id == EMCFAN_PRODUCT_2101 && 1644 !is_internal) { 1645 if (temp_high == 0x7f && 1646 temp_low == 0) { 1647 return; 1648 } 1649 } 1650 1651 /* For everyone else, if the external sensor read 0x80 on the high byte and 1652 * the fraction is 0, then there is a fault or no sensor. 1653 */ 1654 1655 if (!is_internal && !using_vin) { 1656 if (temp_high == 0x80 && 1657 (temp_low >> 5) == 0x00) { 1658 return; 1659 } 1660 } 1661 1662 int32_t kelvin1000 = 0; 1663 int32_t frac = 0; 1664 uint8_t tl; 1665 1666 if (!using_vin) { 1667 kelvin1000 = (int8_t)temp_high * 1000; 1668 tl = temp_low >> 5; 1669 if (temp_high & 0x80) { 1670 tl = (~tl) & 0x07; 1671 tl++; 1672 } 1673 frac = 125 * tl; 1674 if (temp_high & 0x80) { 1675 kelvin1000 -= frac; 1676 } else { 1677 kelvin1000 += frac; 1678 } 1679 kelvin1000 += 273150; 1680 } else { 1681 int32_t vin1000 = EMCFAN_VIN_NO_TEMP; 1682 1683 if (inverted) { 1684 if (emcfan_vin_temps_i[temp_high] != EMCFAN_VIN_NO_TEMP) { 1685 vin1000 = emcfan_vin_temps_i[temp_high]; 1686 } 1687 } else { 1688 if (emcfan_vin_temps[temp_high] != EMCFAN_VIN_NO_TEMP) { 1689 vin1000 = emcfan_vin_temps[temp_high]; 1690 } 1691 } 1692 1693 if (vin1000 != EMCFAN_VIN_NO_TEMP) 1694 kelvin1000 = vin1000; 1695 else 1696 return; 1697 } 1698 1699 edata->value_cur = (uint32_t) kelvin1000 * 1000; 1700 edata->state = ENVSYS_SVALID; 1701 } 1702 1703 static void 1704 emcfan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 1705 { 1706 struct emcfan_sc *sc = sme->sme_cookie; 1707 int instance = -1; 1708 1709 /* Hunt down the instance for this sensor. 1710 * 1711 * The type is held in sc_sensors, but the actual hardware 1712 * instance is held in sc_sensor_instances in the member 1713 * field. It would be nice if the edata structure had a 1714 * field that could be used as an opaque value. 1715 */ 1716 for(int i = 0;i < EMCFAN_NUM_SENSORS;i++) 1717 if (sc->sc_sensor_instances[i].sc_i_envnum == edata->sensor) { 1718 instance = i; 1719 break; 1720 } 1721 1722 KASSERT(instance > -1); 1723 1724 DPRINTF(sc, 2, ("%s: using sensor instance %d\n", __func__, 1725 instance)); 1726 1727 edata->state = ENVSYS_SINVALID; 1728 1729 /* Unlike the manner of the refresh functions in other drivers, this 1730 * one will select the sensor based upon the type and instance. 1731 * Due to the fact that the order will vary depending on which 1732 * chip you are using. 1733 */ 1734 1735 switch(edata->units) { 1736 case ENVSYS_SFANRPM: 1737 switch(emcfan_chip_infos[sc->sc_info_index].family) { 1738 case EMCFAN_FAMILY_210X: 1739 switch(emcfan_chip_infos[sc->sc_info_index].product_id) { 1740 case EMCFAN_PRODUCT_2101: 1741 emcfan_refresh_2101_tach(sme, edata, instance); 1742 break; 1743 /* 2103, 2104 and 2106 use nearly the same algorithm as the 230x family */ 1744 default: 1745 emcfan_refresh_210_346_230x_tach(emcfan_chip_infos[sc->sc_info_index].family, 1746 emcfan_chip_infos[sc->sc_info_index].product_id, 1747 sme, edata, instance); 1748 break; 1749 }; 1750 break; 1751 case EMCFAN_FAMILY_230X: 1752 emcfan_refresh_210_346_230x_tach(emcfan_chip_infos[sc->sc_info_index].family, 1753 emcfan_chip_infos[sc->sc_info_index].product_id, 1754 sme, edata, instance); 1755 break; 1756 default: 1757 panic("Unknown family: %d\n",emcfan_chip_infos[sc->sc_info_index].family); 1758 break; 1759 } 1760 break; 1761 case ENVSYS_STEMP: 1762 emcfan_refresh_temp(emcfan_chip_infos[sc->sc_info_index].family, 1763 emcfan_chip_infos[sc->sc_info_index].product_id, 1764 sme, edata, instance); 1765 break; 1766 default: 1767 panic("Unknown edata units value: %d\n",edata->units); 1768 break; 1769 }; 1770 } 1771 1772 static int 1773 emcfanopen(dev_t dev, int flags, int fmt, struct lwp *l) 1774 { 1775 struct emcfan_sc *sc; 1776 1777 sc = device_lookup_private(&emcfan_cd, minor(dev)); 1778 if (!sc) 1779 return ENXIO; 1780 1781 if (sc->sc_opened) 1782 return EBUSY; 1783 1784 mutex_enter(&sc->sc_mutex); 1785 sc->sc_opened = true; 1786 mutex_exit(&sc->sc_mutex); 1787 1788 return 0; 1789 } 1790 1791 static int 1792 emcfanread(dev_t dev, struct uio *uio, int flags) 1793 { 1794 struct emcfan_sc *sc; 1795 int error; 1796 1797 if ((sc = device_lookup_private(&emcfan_cd, minor(dev))) == NULL) 1798 return ENXIO; 1799 1800 /* We do not make this an error. There is nothing wrong with running 1801 * off the end here, just return EOF. 1802 */ 1803 if (uio->uio_offset > 0xff) 1804 return 0; 1805 1806 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) 1807 return error; 1808 1809 while (uio->uio_resid && 1810 uio->uio_offset <= 0xff && 1811 !sc->sc_dying) { 1812 uint8_t buf; 1813 int reg_addr = uio->uio_offset; 1814 1815 if ((error = emcfan_read_register(sc, reg_addr, &buf)) != 0) { 1816 iic_release_bus(sc->sc_tag, 0); 1817 aprint_error_dev(sc->sc_dev, 1818 "%s: read failed at 0x%02x: %d\n", 1819 __func__, reg_addr, error); 1820 return error; 1821 } 1822 1823 if (sc->sc_dying) 1824 break; 1825 1826 if ((error = uiomove(&buf, 1, uio)) != 0) { 1827 iic_release_bus(sc->sc_tag, 0); 1828 return error; 1829 } 1830 } 1831 1832 iic_release_bus(sc->sc_tag, 0); 1833 1834 if (sc->sc_dying) { 1835 return EIO; 1836 } 1837 1838 return 0; 1839 } 1840 1841 static int 1842 emcfanwrite(dev_t dev, struct uio *uio, int flags) 1843 { 1844 struct emcfan_sc *sc; 1845 int error; 1846 1847 if ((sc = device_lookup_private(&emcfan_cd, minor(dev))) == NULL) 1848 return ENXIO; 1849 1850 /* Same thing as read, this is not considered an error */ 1851 if (uio->uio_offset > 0xff) 1852 return 0; 1853 1854 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) 1855 return error; 1856 1857 while (uio->uio_resid && 1858 uio->uio_offset <= 0xff && 1859 !sc->sc_dying) { 1860 uint8_t buf; 1861 int reg_addr = uio->uio_offset; 1862 1863 if ((error = uiomove(&buf, 1, uio)) != 0) 1864 break; 1865 1866 if (sc->sc_dying) 1867 break; 1868 1869 if ((error = emcfan_write_register(sc, (uint8_t)reg_addr, buf)) != 0) { 1870 iic_release_bus(sc->sc_tag, 0); 1871 aprint_error_dev(sc->sc_dev, 1872 "%s: write failed at 0x%02x: %d\n", 1873 __func__, reg_addr, error); 1874 return error; 1875 } 1876 } 1877 1878 iic_release_bus(sc->sc_tag, 0); 1879 1880 if (sc->sc_dying) { 1881 return EIO; 1882 } 1883 1884 return error; 1885 } 1886 1887 static int 1888 emcfanclose(dev_t dev, int flags, int fmt, struct lwp *l) 1889 { 1890 struct emcfan_sc *sc; 1891 1892 sc = device_lookup_private(&emcfan_cd, minor(dev)); 1893 1894 mutex_enter(&sc->sc_mutex); 1895 sc->sc_opened = false; 1896 mutex_exit(&sc->sc_mutex); 1897 1898 return(0); 1899 } 1900 1901 static int 1902 emcfan_detach(device_t self, int flags) 1903 { 1904 int err; 1905 struct emcfan_sc *sc; 1906 1907 sc = device_private(self); 1908 1909 mutex_enter(&sc->sc_mutex); 1910 sc->sc_dying = true; 1911 1912 err = config_detach_children(self, flags); 1913 if (err) 1914 return err; 1915 1916 /* Remove the sysctl tree */ 1917 if (sc->sc_emcfanlog != NULL) 1918 sysctl_teardown(&sc->sc_emcfanlog); 1919 1920 /* Remove the sensors */ 1921 if (sc->sc_sme != NULL) { 1922 sysmon_envsys_unregister(sc->sc_sme); 1923 sc->sc_sme = NULL; 1924 } 1925 1926 mutex_exit(&sc->sc_mutex); 1927 1928 mutex_destroy(&sc->sc_mutex); 1929 1930 return 0; 1931 } 1932 1933 int 1934 emcfan_activate(device_t self, enum devact act) 1935 { 1936 struct emcfan_sc *sc = device_private(self); 1937 1938 switch (act) { 1939 case DVACT_DEACTIVATE: 1940 sc->sc_dying = true; 1941 return 0; 1942 default: 1943 return EOPNOTSUPP; 1944 } 1945 } 1946 1947 /* --- GPIO --- */ 1948 1949 static int 1950 emcfan_current_gpio_flags(struct emcfan_sc *sc, 1951 uint8_t product_id, 1952 int pin) 1953 { 1954 int error = 0; 1955 int f = 0; 1956 uint8_t mux_reg = 0; 1957 uint8_t dir_reg; 1958 uint8_t out_config; 1959 uint8_t pin_mask; 1960 uint8_t pin_maska1; 1961 uint8_t pin_maska2; 1962 1963 error = iic_acquire_bus(sc->sc_tag, 0); 1964 if (error) { 1965 return 0; 1966 } 1967 1968 if (product_id != EMCFAN_PRODUCT_2103_24) { 1969 error = emcfan_read_register(sc, EMCFAN_MUX_PINS, &mux_reg); 1970 if (error != 0) { 1971 return 0; 1972 } 1973 } 1974 error = emcfan_read_register(sc, EMCFAN_DIR_PINS, &dir_reg); 1975 if (error != 0) { 1976 return 0; 1977 } 1978 error = emcfan_read_register(sc, EMCFAN_OUTPUT_PIN_CONFIG, &out_config); 1979 if (error != 0) { 1980 return 0; 1981 } 1982 iic_release_bus(sc->sc_tag, 0); 1983 1984 pin_mask = 1 << pin; 1985 1986 if (product_id != EMCFAN_PRODUCT_2103_24) { 1987 if (pin <= 4) { 1988 if (pin <= 2) { 1989 if ((mux_reg & pin_mask) == 0) 1990 f = GPIO_PIN_ALT0; 1991 } else { 1992 if (pin == 3) { 1993 pin_maska1 = 0x08; 1994 pin_maska2 = 0x10; 1995 } else { 1996 pin_maska1 = 0x20; 1997 pin_maska2 = 0x40; 1998 } 1999 if (mux_reg & pin_maska1 && 2000 mux_reg & pin_maska2) { 2001 f = GPIO_PIN_ALT1; 2002 } else { 2003 if (((mux_reg & pin_maska1) == 0) && 2004 ((mux_reg & pin_maska2) == 0)) { 2005 f = GPIO_PIN_ALT0; 2006 } 2007 } 2008 } 2009 } 2010 } 2011 2012 if (f == 0) { 2013 if (dir_reg & pin_mask) { 2014 f = GPIO_PIN_OUTPUT; 2015 } else { 2016 f = GPIO_PIN_INPUT; 2017 } 2018 2019 if (out_config & pin_mask) { 2020 f |= GPIO_PIN_PUSHPULL; 2021 } else { 2022 f |= GPIO_PIN_OPENDRAIN; 2023 } 2024 } 2025 2026 return f; 2027 } 2028 2029 static int 2030 emcfan_gpio_pin_read(void *arg, int pin) 2031 { 2032 struct emcfan_sc *sc = arg; 2033 int error = 0; 2034 int r = GPIO_PIN_LOW; 2035 uint8_t input_reg; 2036 uint8_t pin_mask; 2037 2038 error = iic_acquire_bus(sc->sc_tag, 0); 2039 if (!error) { 2040 error = emcfan_read_register(sc, EMCFAN_PINS_INPUT, &input_reg); 2041 if (!error) { 2042 pin_mask = 1 << pin; 2043 if (input_reg & pin_mask) 2044 r = GPIO_PIN_HIGH; 2045 } 2046 2047 } 2048 iic_release_bus(sc->sc_tag, 0); 2049 2050 return r; 2051 } 2052 2053 static void 2054 emcfan_gpio_pin_write(void *arg, int pin, int value) 2055 { 2056 struct emcfan_sc *sc = arg; 2057 int error = 0; 2058 uint8_t output_reg; 2059 uint8_t pin_mask; 2060 2061 error = iic_acquire_bus(sc->sc_tag, 0); 2062 if (!error) { 2063 error = emcfan_read_register(sc, EMCFAN_PINS_OUTPUT, &output_reg); 2064 if (!error) { 2065 pin_mask = 1 << pin; 2066 2067 if (value == 0) { 2068 pin_mask = ~pin_mask; 2069 output_reg &= pin_mask; 2070 } else { 2071 output_reg |= pin_mask; 2072 } 2073 emcfan_write_register(sc, EMCFAN_PINS_OUTPUT, output_reg); 2074 } 2075 2076 } 2077 iic_release_bus(sc->sc_tag, 0); 2078 } 2079 2080 static void 2081 emcfan_gpio_pin_ctl(void *arg, int pin, int flags) 2082 { 2083 struct emcfan_sc *sc = arg; 2084 int error = 0; 2085 uint8_t product_id = emcfan_chip_infos[sc->sc_info_index].product_id; 2086 uint8_t pin_mask = 1 << pin; 2087 uint8_t pin_maska1; 2088 uint8_t pin_maska2; 2089 uint8_t mux_reg = 0; 2090 uint8_t dir_reg; 2091 uint8_t out_config; 2092 2093 error = iic_acquire_bus(sc->sc_tag, 0); 2094 if (error) { 2095 return; 2096 } 2097 2098 if (product_id != EMCFAN_PRODUCT_2103_24) { 2099 error = emcfan_read_register(sc, EMCFAN_MUX_PINS, &mux_reg); 2100 if (error != 0) { 2101 return; 2102 } 2103 } 2104 error = emcfan_read_register(sc, EMCFAN_DIR_PINS, &dir_reg); 2105 if (error != 0) { 2106 return; 2107 } 2108 error = emcfan_read_register(sc, EMCFAN_OUTPUT_PIN_CONFIG, &out_config); 2109 if (error != 0) { 2110 return; 2111 } 2112 iic_release_bus(sc->sc_tag, 0); 2113 2114 if (flags & GPIO_PIN_ALT0 || 2115 flags & GPIO_PIN_ALT1) { 2116 if (product_id != EMCFAN_PRODUCT_2103_24) { 2117 if (pin <= 4) { 2118 if (pin <= 2) { 2119 mux_reg &= ~pin_mask; 2120 } else { 2121 if (pin == 3) { 2122 pin_maska2 = 0x18; 2123 } else { 2124 pin_maska2 = 0x60; 2125 } 2126 if (flags & GPIO_PIN_ALT0) { 2127 mux_reg &= ~pin_maska2; 2128 } else { 2129 if (flags & GPIO_PIN_ALT1) { 2130 mux_reg |= pin_maska2; 2131 } 2132 } 2133 } 2134 emcfan_write_register(sc, EMCFAN_MUX_PINS, mux_reg); 2135 } 2136 } 2137 } else { 2138 if (product_id != EMCFAN_PRODUCT_2103_24) { 2139 if (pin <= 4) { 2140 if (pin <= 2) { 2141 mux_reg |= pin_mask; 2142 } else { 2143 if (pin == 3) { 2144 pin_maska1 = 0x08; 2145 pin_maska2 = 0x18; 2146 } else { 2147 pin_maska1 = 0x20; 2148 pin_maska2 = 0x60; 2149 } 2150 mux_reg &= ~pin_maska2; 2151 mux_reg |= pin_maska1; 2152 } 2153 emcfan_write_register(sc, EMCFAN_MUX_PINS, mux_reg); 2154 } 2155 } 2156 2157 if (flags & GPIO_PIN_OPENDRAIN) { 2158 out_config &= ~pin_mask; 2159 } else { 2160 if (flags & GPIO_PIN_PUSHPULL) 2161 out_config |= pin_mask; 2162 } 2163 emcfan_write_register(sc, EMCFAN_OUTPUT_PIN_CONFIG, out_config); 2164 2165 if (flags & GPIO_PIN_INPUT) { 2166 dir_reg &= ~pin_mask; 2167 } else { 2168 if (flags & GPIO_PIN_OUTPUT) 2169 dir_reg |= pin_mask; 2170 } 2171 emcfan_write_register(sc, EMCFAN_DIR_PINS, dir_reg); 2172 } 2173 } 2174 2175 static void 2176 emcfan_attach_gpio(struct emcfan_sc *sc, uint8_t product_id) 2177 { 2178 struct gpiobus_attach_args gba; 2179 2180 for(int i = 0; i < emcfan_chip_infos[sc->sc_info_index].num_gpio_pins;i++) { 2181 sc->sc_gpio_pins[i].pin_num = i; 2182 sc->sc_gpio_pins[i].pin_caps = emcfan_chip_infos[sc->sc_info_index].gpio_pin_ability[i]; 2183 sc->sc_gpio_pins[i].pin_flags = emcfan_current_gpio_flags(sc, emcfan_chip_infos[sc->sc_info_index].product_id, i); 2184 sc->sc_gpio_pins[i].pin_intrcaps = 0; 2185 strncpy(sc->sc_gpio_pins[i].pin_defname, 2186 emcfan_chip_infos[sc->sc_info_index].gpio_names[i], 2187 strlen(emcfan_chip_infos[sc->sc_info_index].gpio_names[i]) + 1); 2188 } 2189 2190 sc->sc_gpio_gc.gp_cookie = sc; 2191 sc->sc_gpio_gc.gp_pin_read = emcfan_gpio_pin_read; 2192 sc->sc_gpio_gc.gp_pin_write = emcfan_gpio_pin_write; 2193 sc->sc_gpio_gc.gp_pin_ctl = emcfan_gpio_pin_ctl; 2194 2195 gba.gba_gc = &sc->sc_gpio_gc; 2196 gba.gba_pins = sc->sc_gpio_pins; 2197 gba.gba_npins = emcfan_chip_infos[sc->sc_info_index].num_gpio_pins; 2198 2199 sc->sc_gpio_dev = config_found(sc->sc_dev, &gba, gpiobus_print, CFARGS(.iattr = "gpiobus")); 2200 2201 return; 2202 } 2203 2204 MODULE(MODULE_CLASS_DRIVER, emcfan, "iic,sysmon_envsys,gpio"); 2205 2206 #ifdef _MODULE 2207 #include "ioconf.c" 2208 #endif 2209 2210 static int 2211 emcfan_modcmd(modcmd_t cmd, void *opaque) 2212 { 2213 int error; 2214 #ifdef _MODULE 2215 int bmaj = -1, cmaj = -1; 2216 #endif 2217 2218 switch (cmd) { 2219 case MODULE_CMD_INIT: 2220 #ifdef _MODULE 2221 error = devsw_attach("emcfan", NULL, &bmaj, 2222 &emcfan_cdevsw, &cmaj); 2223 if (error) { 2224 aprint_error("%s: unable to attach devsw\n", 2225 emcfan_cd.cd_name); 2226 return error; 2227 } 2228 2229 error = config_init_component(cfdriver_ioconf_emcfan, 2230 cfattach_ioconf_emcfan, cfdata_ioconf_emcfan); 2231 if (error) { 2232 aprint_error("%s: unable to init component\n", 2233 emcfan_cd.cd_name); 2234 devsw_detach(NULL, &emcfan_cdevsw); 2235 } 2236 return error; 2237 #else 2238 return 0; 2239 #endif 2240 case MODULE_CMD_FINI: 2241 #ifdef _MODULE 2242 error = config_fini_component(cfdriver_ioconf_emcfan, 2243 cfattach_ioconf_emcfan, cfdata_ioconf_emcfan); 2244 devsw_detach(NULL, &emcfan_cdevsw); 2245 return error; 2246 #else 2247 return 0; 2248 #endif 2249 default: 2250 return ENOTTY; 2251 } 2252 } 2253