asc7621.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* $OpenBSD: asc7621.c,v 1.4 2007/10/31 20:46:17 cnst Exp $ */
  2. /*
  3. * Copyright (c) 2007 Mike Belopuhov
  4. * Copyright (c) 2007 Theo de Raadt
  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. #include <sys/param.h>
  19. #include <sys/systm.h>
  20. #include <sys/device.h>
  21. #include <sys/sensors.h>
  22. #include <dev/i2c/i2cvar.h>
  23. /* ASC7621 registers */
  24. #define ASC7621_PECI 0x40 /* Check for PECI monitoring */
  25. #define ASC7621_PECI_MASK 0x10 /* 00010000 */
  26. #define ASC7621_LEGACY 0x36 /* Check for legacy mode */
  27. #define ASC7621_LEGACY_MASK 0x10 /* 00010000 */
  28. #define ASC7621_TEMP1H 0x25 /* Zone 1 Temperature (MS Byte) */
  29. #define ASC7621_TEMP1L 0x10 /* Zone 1 Temperature (LS Byte) */
  30. #define ASC7621_TEMP2H 0x26 /* Zone 2 Temperature (MS Byte) */
  31. #define ASC7621_TEMP2L 0x15 /* Zone 2 Temperature (LS Byte) */
  32. #define ASC7621_TEMP3H 0x27 /* Zone 3 Temperature (MS Byte) */
  33. #define ASC7621_TEMP3L 0x16 /* Zone 3 Temperature (LS Byte) */
  34. #define ASC7621_TEMP4H 0x33 /* Zone 4 Temperature (MS Byte) */
  35. #define ASC7621_TEMP4L 0x17 /* Zone 4 Temperature (LS Byte) */
  36. #define ASC7621_TEMP_NA 0x80 /* Not plugged */
  37. #define ASC7621_IN1_VH 0x20 /* 2.5V (MS Byte) */
  38. #define ASC7621_IN1_VL 0x13 /* 2.5V (LS Byte) */
  39. #define ASC7621_IN2_VH 0x21 /* VCCP (MS Byte) */
  40. #define ASC7621_IN2_VL 0x18 /* VCCP (LS Byte) */
  41. #define ASC7621_IN3_VH 0x22 /* 3.3V (MS Byte) */
  42. #define ASC7621_IN3_VL 0x11 /* 2.3V (LS Byte) */
  43. #define ASC7621_IN4_VH 0x23 /* 5V (MS Byte) */
  44. #define ASC7621_IN4_VL 0x12 /* 5V (LS Byte) */
  45. #define ASC7621_IN5_VH 0x24 /* 12V (MS Byte) */
  46. #define ASC7621_IN5_VL 0x14 /* 12V (LS Byte) */
  47. #define ASC7621_TACH1H 0x29 /* Tachometer 1 (MS Byte) */
  48. #define ASC7621_TACH1L 0x28 /* Tachometer 1 (LS Byte) */
  49. #define ASC7621_TACH2H 0x2b /* Tachometer 2 (MS Byte) */
  50. #define ASC7621_TACH2L 0x2a /* Tachometer 2 (LS Byte) */
  51. #define ASC7621_TACH3H 0x2d /* Tachometer 3 (MS Byte) */
  52. #define ASC7621_TACH3L 0x2c /* Tachometer 3 (LS Byte) */
  53. #define ASC7621_TACH4H 0x2f /* Tachometer 4 (MS Byte) */
  54. #define ASC7621_TACH4L 0x2e /* Tachometer 4 (LS Byte) */
  55. /* Sensors */
  56. #define ADL_TEMP1 0
  57. #define ADL_TEMP2 1
  58. #define ADL_TEMP3 2
  59. #define ADL_TEMP4 3
  60. #define ADL_IN1_V 4
  61. #define ADL_IN2_V 5
  62. #define ADL_IN3_V 6
  63. #define ADL_IN4_V 7
  64. #define ADL_IN5_V 8
  65. #define ADL_TACH1 9
  66. #define ADL_TACH2 10
  67. #define ADL_TACH3 11
  68. #define ADL_TACH4 12
  69. #define ADL_NUM_SENSORS 13
  70. struct {
  71. char sensor;
  72. u_int8_t hreg; /* MS-byte register */
  73. u_int8_t lreg; /* LS-byte register */
  74. char *name;
  75. u_short mVscale;
  76. u_short tempscale; /* else a fan */
  77. } adl_worklist[] = {
  78. { ADL_TEMP1, ASC7621_TEMP1H, ASC7621_TEMP1L, "CPU", 0, 1 },
  79. { ADL_TEMP2, ASC7621_TEMP2H, ASC7621_TEMP2L, "CPU", 0, 1 },
  80. { ADL_TEMP3, ASC7621_TEMP3H, ASC7621_TEMP3L, "Internal", 0, 1 },
  81. { ADL_TEMP4, ASC7621_TEMP4H, ASC7621_TEMP4L, "External", 0, 1 },
  82. { ADL_IN1_V, ASC7621_IN1_VH, ASC7621_IN1_VL, "+1.5V", 2500, 0 },
  83. { ADL_IN2_V, ASC7621_IN2_VH, ASC7621_IN2_VL, "Vccp", 2250, 0 },
  84. { ADL_IN3_V, ASC7621_IN3_VH, ASC7621_IN3_VL, "+3.3V", 3300, 0 },
  85. { ADL_IN4_V, ASC7621_IN4_VH, ASC7621_IN4_VL, "+5V", 5000, 0 },
  86. { ADL_IN5_V, ASC7621_IN5_VH, ASC7621_IN5_VL, "+12V", 12000, 0 },
  87. { ADL_TACH1, ASC7621_TACH1L, ASC7621_TACH1H, "", 0, 0 },
  88. { ADL_TACH2, ASC7621_TACH2L, ASC7621_TACH2H, "", 0, 0 },
  89. { ADL_TACH3, ASC7621_TACH3L, ASC7621_TACH3H, "", 0, 0 },
  90. { ADL_TACH4, ASC7621_TACH4L, ASC7621_TACH4H, "", 0, 0 }
  91. };
  92. struct adl_softc {
  93. struct device sc_dev;
  94. i2c_tag_t sc_tag;
  95. i2c_addr_t sc_addr;
  96. u_int8_t sc_conf;
  97. struct ksensor sc_sensor[ADL_NUM_SENSORS];
  98. struct ksensordev sc_sensordev;
  99. };
  100. #if 0
  101. static int peci_enabled;
  102. static int legacy_mode;
  103. #endif
  104. int adl_match(struct device *, void *, void *);
  105. void adl_attach(struct device *, struct device *, void *);
  106. void adl_refresh(void *);
  107. struct cfattach adl_ca = {
  108. sizeof(struct adl_softc), adl_match, adl_attach
  109. };
  110. struct cfdriver adl_cd = {
  111. NULL, "adl", DV_DULL
  112. };
  113. int
  114. adl_match(struct device *parent, void *match, void *aux)
  115. {
  116. struct i2c_attach_args *ia = aux;
  117. if (strcmp(ia->ia_name, "asc7621") == 0)
  118. return (1);
  119. return (0);
  120. }
  121. void
  122. adl_attach(struct device *parent, struct device *self, void *aux)
  123. {
  124. struct adl_softc *sc = (struct adl_softc *)self;
  125. struct i2c_attach_args *ia = aux;
  126. u_int8_t cmd, data;
  127. int i;
  128. sc->sc_tag = ia->ia_tag;
  129. sc->sc_addr = ia->ia_addr;
  130. printf(": %s", ia->ia_name);
  131. /* Initialize sensor data. */
  132. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  133. sizeof(sc->sc_sensordev.xname));
  134. /* Check for PECI mode */
  135. cmd = ASC7621_PECI;
  136. (void)iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  137. &cmd, sizeof(cmd), &data, sizeof(data), 0);
  138. if (data & ASC7621_PECI_MASK)
  139. printf(", PECI enabled\n");
  140. #if 0
  141. /* Check for legacy mode */
  142. cmd = ASC7621_LEGACY;
  143. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  144. &cmd, sizeof(cmd), &data, sizeof(data), 0)) {
  145. printf(", unable to read PECI configuration register");
  146. }
  147. if (data & ASC7621_LEGACY_MASK)
  148. legacy_mode = 1;
  149. #endif
  150. if (sensor_task_register(sc, adl_refresh, 5) == NULL) {
  151. printf(", unable to register update task\n");
  152. return;
  153. }
  154. for (i = 0; i < ADL_NUM_SENSORS; i++) {
  155. if (adl_worklist[i].tempscale)
  156. sc->sc_sensor[i].type = SENSOR_TEMP;
  157. else if (adl_worklist[i].mVscale)
  158. sc->sc_sensor[i].type = SENSOR_VOLTS_DC;
  159. else
  160. sc->sc_sensor[i].type = SENSOR_FANRPM;
  161. strlcpy(sc->sc_sensor[i].desc, adl_worklist[i].name,
  162. sizeof(sc->sc_sensor[i].desc));
  163. sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
  164. }
  165. sensordev_install(&sc->sc_sensordev);
  166. printf("\n");
  167. }
  168. void
  169. adl_refresh(void *arg)
  170. {
  171. struct adl_softc *sc = arg;
  172. int64_t temp, volt;
  173. u_int8_t hdata, ldata, hreg, lreg;
  174. u_int16_t fan;
  175. int i;
  176. iic_acquire_bus(sc->sc_tag, 0);
  177. for (i = 0; i < sizeof adl_worklist / sizeof(adl_worklist[0]); i++) {
  178. hreg = adl_worklist[i].hreg;
  179. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  180. sc->sc_addr, &hreg, sizeof hreg, &hdata, sizeof hdata, 0)) {
  181. sc->sc_sensor[i].flags |= SENSOR_FINVALID;
  182. continue;
  183. }
  184. lreg = adl_worklist[i].lreg;
  185. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  186. sc->sc_addr, &lreg, sizeof lreg, &ldata, sizeof ldata, 0)) {
  187. sc->sc_sensor[i].flags |= SENSOR_FINVALID;
  188. continue;
  189. }
  190. sc->sc_sensor[i].flags &= ~SENSOR_FINVALID;
  191. if (adl_worklist[i].tempscale) {
  192. if (hdata == ASC7621_TEMP_NA)
  193. sc->sc_sensor[i].flags |= SENSOR_FINVALID;
  194. else {
  195. /*
  196. * 10-bit two's complement integer in
  197. * steps of 0.25
  198. */
  199. temp = ((hdata << 8 | ldata)) >> (16 - 10);
  200. temp = temp * 250000 + 273150000;
  201. sc->sc_sensor[i].value = temp;
  202. }
  203. } else if (adl_worklist[i].mVscale) {
  204. volt = ((hdata << 8 | ldata)) >> (16 - 10);
  205. volt = volt * adl_worklist[i].mVscale / (192 << 2);
  206. sc->sc_sensor[i].value = volt * 1000;
  207. } else {
  208. /*
  209. * Inversed to ensure that the LS byte will be read
  210. * before MS byte.
  211. */
  212. fan = hdata + (ldata << 8);
  213. if (fan == 0 || fan == 0xffff)
  214. sc->sc_sensor[i].flags |= SENSOR_FINVALID;
  215. else
  216. sc->sc_sensor[i].value = (90000 * 60) / fan;
  217. }
  218. }
  219. iic_release_bus(sc->sc_tag, 0);
  220. }