fcu.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* $OpenBSD: fcu.c,v 1.7 2007/06/24 05:34:35 dlg Exp $ */
  2. /*
  3. * Copyright (c) 2005 Mark Kettenis
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/systm.h>
  19. #include <sys/device.h>
  20. #include <sys/sensors.h>
  21. #include <dev/i2c/i2cvar.h>
  22. /* FCU registers */
  23. #define FCU_FAN_FAIL 0x0b /* fans states in bits 0<1-6>7 */
  24. #define FCU_FAN_ACTIVE 0x0d
  25. #define FCU_FANREAD(x) 0x11 + (x)*2
  26. #define FCU_FANSET(x) 0x10 + (x)*2
  27. #define FCU_PWM_FAIL 0x2b
  28. #define FCU_PWM_ACTIVE 0x2d
  29. #define FCU_PWMREAD(x) 0x30 + (x)*2
  30. /* Sensors */
  31. #define FCU_RPM1 0
  32. #define FCU_RPM2 1
  33. #define FCU_RPM3 2
  34. #define FCU_RPM4 3
  35. #define FCU_RPM5 4
  36. #define FCU_RPM6 5
  37. #define FCU_FANS 6
  38. #define FCU_PWM1 6
  39. #define FCU_PWM2 7
  40. #define FCU_PWMS 2
  41. #define FCU_NUM_SENSORS 8
  42. struct fcu_softc {
  43. struct device sc_dev;
  44. i2c_tag_t sc_tag;
  45. i2c_addr_t sc_addr;
  46. struct ksensor sc_sensor[FCU_NUM_SENSORS];
  47. struct ksensordev sc_sensordev;
  48. };
  49. int fcu_match(struct device *, void *, void *);
  50. void fcu_attach(struct device *, struct device *, void *);
  51. void fcu_refresh(void *);
  52. struct cfattach fcu_ca = {
  53. sizeof(struct fcu_softc), fcu_match, fcu_attach
  54. };
  55. struct cfdriver fcu_cd = {
  56. NULL, "fcu", DV_DULL
  57. };
  58. int
  59. fcu_match(struct device *parent, void *match, void *aux)
  60. {
  61. struct i2c_attach_args *ia = aux;
  62. if (strcmp(ia->ia_name, "fcu") == 0)
  63. return (1);
  64. return (0);
  65. }
  66. void
  67. fcu_attach(struct device *parent, struct device *self, void *aux)
  68. {
  69. struct fcu_softc *sc = (struct fcu_softc *)self;
  70. struct i2c_attach_args *ia = aux;
  71. int i;
  72. sc->sc_tag = ia->ia_tag;
  73. sc->sc_addr = ia->ia_addr;
  74. /* Initialize sensor data. */
  75. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  76. sizeof(sc->sc_sensordev.xname));
  77. for (i = 0; i < FCU_FANS; i++)
  78. sc->sc_sensor[i].type = SENSOR_FANRPM;
  79. for (i = 0; i < FCU_PWMS; i++) {
  80. sc->sc_sensor[FCU_PWM1 + i].type = SENSOR_PERCENT;
  81. strlcpy(sc->sc_sensor[FCU_PWM1 + i].desc, "PWM",
  82. sizeof(sc->sc_sensor[FCU_PWM1 + i].desc));
  83. }
  84. if (sensor_task_register(sc, fcu_refresh, 5) == NULL) {
  85. printf(", unable to register update task\n");
  86. return;
  87. }
  88. for (i = 0; i < FCU_NUM_SENSORS; i++)
  89. sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
  90. sensordev_install(&sc->sc_sensordev);
  91. printf("\n");
  92. }
  93. void
  94. fcu_refresh(void *arg)
  95. {
  96. struct fcu_softc *sc = arg;
  97. u_int8_t cmd, fail, fan[2], active;
  98. int i;
  99. iic_acquire_bus(sc->sc_tag, 0);
  100. cmd = FCU_FAN_FAIL;
  101. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  102. sc->sc_addr, &cmd, sizeof cmd, &fail, sizeof fail, 0))
  103. goto abort;
  104. cmd = FCU_FAN_ACTIVE;
  105. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  106. sc->sc_addr, &cmd, sizeof cmd, &active, sizeof active, 0))
  107. goto abort;
  108. fail &= active;
  109. for (i = 0; i < FCU_FANS; i++) {
  110. if (fail & (1 << (i + 1)))
  111. sc->sc_sensor[i].flags |= SENSOR_FINVALID;
  112. else
  113. sc->sc_sensor[i].flags &= ~SENSOR_FINVALID;
  114. }
  115. cmd = FCU_PWM_FAIL;
  116. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  117. sc->sc_addr, &cmd, sizeof cmd, &fail, sizeof fail, 0))
  118. goto abort;
  119. cmd = FCU_PWM_ACTIVE;
  120. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  121. sc->sc_addr, &cmd, sizeof cmd, &active, sizeof active, 0))
  122. goto abort;
  123. fail &= active;
  124. for (i = 0; i < FCU_PWMS; i++) {
  125. if (fail & (1 << (i + 1)))
  126. sc->sc_sensor[FCU_PWMS + i].flags |= SENSOR_FINVALID;
  127. else
  128. sc->sc_sensor[FCU_PWMS + i].flags &= ~SENSOR_FINVALID;
  129. }
  130. for (i = 0; i < FCU_FANS; i++) {
  131. cmd = FCU_FANREAD(i + 1);
  132. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  133. sc->sc_addr, &cmd, sizeof cmd, &fan, sizeof fan, 0)) {
  134. sc->sc_sensor[FCU_RPM1 + i].flags |= SENSOR_FINVALID;
  135. continue;
  136. }
  137. sc->sc_sensor[FCU_RPM1 + i].value = (fan[0] << 5) | (fan[1] >> 3);
  138. }
  139. for (i = 0; i < FCU_PWMS; i++) {
  140. cmd = FCU_PWMREAD(i + 1);
  141. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  142. sc->sc_addr, &cmd, sizeof cmd, &fan, sizeof fan, 0)) {
  143. sc->sc_sensor[FCU_PWM1 + i].flags |= SENSOR_FINVALID;
  144. continue;
  145. }
  146. sc->sc_sensor[FCU_PWM1 + i].value = (fan[0] * 100 * 1000) / 255;
  147. }
  148. abort:
  149. iic_release_bus(sc->sc_tag, 0);
  150. }