adm1024.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* $OpenBSD: adm1024.c,v 1.14 2007/06/24 05:34:35 dlg Exp $ */
  2. /*
  3. * Copyright (c) 2005 Theo de Raadt
  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. /* ADM 1024 registers */
  23. #define ADM1024_V2_5 0x20
  24. #define ADM1024_Vccp 0x21
  25. #define ADM1024_Vcc 0x22
  26. #define ADM1024_V5 0x23
  27. #define ADM1024_V12 0x24
  28. #define ADM1024_Vccp2 0x25
  29. #define ADM1024_EXT_TEMP 0x26
  30. #define ADM1024_INT_TEMP 0x27
  31. #define ADM1024_FAN1 0x28
  32. #define ADM1024_FAN2 0x29
  33. #define ADM1024_STATUS2 0x42
  34. #define ADM1024_FANC 0x47
  35. #define ADM1024_STATUS2_EXT 0x40
  36. #define ADM1024_COMPANY 0x3e /* contains 0x41 */
  37. #define ADM1024_STEPPING 0x3f /* contains 0x2? */
  38. #define ADM1024_CONFIG1 0x40
  39. #define ADM1024_CONFIG1_START 0x01
  40. #define ADM1024_CONFIG1_INTCLR 0x08
  41. /* Sensors */
  42. #define ADMLC_INT 0
  43. #define ADMLC_EXT 1
  44. #define ADMLC_V2_5 2
  45. #define ADMLC_Vccp 3
  46. #define ADMLC_Vcc 4
  47. #define ADMLC_V5 5
  48. #define ADMLC_V12 6
  49. #define ADMLC_Vccp2 7
  50. #define ADMLC_FAN1 8
  51. #define ADMLC_FAN2 9
  52. #define ADMLC_NUM_SENSORS 10
  53. struct admlc_softc {
  54. struct device sc_dev;
  55. i2c_tag_t sc_tag;
  56. i2c_addr_t sc_addr;
  57. struct ksensor sc_sensor[ADMLC_NUM_SENSORS];
  58. struct ksensordev sc_sensordev;
  59. int sc_fan1mul;
  60. int sc_fan2mul;
  61. };
  62. int admlc_match(struct device *, void *, void *);
  63. void admlc_attach(struct device *, struct device *, void *);
  64. void admlc_refresh(void *);
  65. struct cfattach admlc_ca = {
  66. sizeof(struct admlc_softc), admlc_match, admlc_attach
  67. };
  68. struct cfdriver admlc_cd = {
  69. NULL, "admlc", DV_DULL
  70. };
  71. int
  72. admlc_match(struct device *parent, void *match, void *aux)
  73. {
  74. struct i2c_attach_args *ia = aux;
  75. if (strcmp(ia->ia_name, "adm1024") == 0)
  76. return (1);
  77. return (0);
  78. }
  79. void
  80. admlc_attach(struct device *parent, struct device *self, void *aux)
  81. {
  82. struct admlc_softc *sc = (struct admlc_softc *)self;
  83. struct i2c_attach_args *ia = aux;
  84. u_int8_t cmd, data, data2;
  85. int i;
  86. sc->sc_tag = ia->ia_tag;
  87. sc->sc_addr = ia->ia_addr;
  88. iic_acquire_bus(sc->sc_tag, 0);
  89. cmd = ADM1024_CONFIG1;
  90. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  91. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
  92. iic_release_bus(sc->sc_tag, 0);
  93. printf(": cannot get control register\n");
  94. return;
  95. }
  96. data2 = data | ADM1024_CONFIG1_START;
  97. data2 = data2 & ~ADM1024_CONFIG1_INTCLR;
  98. if (data != data2) {
  99. if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
  100. sc->sc_addr, &cmd, sizeof cmd, &data2, sizeof data2, 0)) {
  101. iic_release_bus(sc->sc_tag, 0);
  102. printf(": cannot set control register\n");
  103. return;
  104. }
  105. }
  106. cmd = ADM1024_FANC;
  107. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  108. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
  109. printf(", unable to read fan setting\n");
  110. return;
  111. }
  112. sc->sc_fan1mul = (1 << (data >> 4) & 0x3);
  113. sc->sc_fan2mul = (1 << (data >> 6) & 0x3);
  114. iic_release_bus(sc->sc_tag, 0);
  115. /* Initialize sensor data. */
  116. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  117. sizeof(sc->sc_sensordev.xname));
  118. sc->sc_sensor[ADMLC_INT].type = SENSOR_TEMP;
  119. strlcpy(sc->sc_sensor[ADMLC_INT].desc, "Internal",
  120. sizeof(sc->sc_sensor[ADMLC_INT].desc));
  121. sc->sc_sensor[ADMLC_EXT].type = SENSOR_TEMP;
  122. strlcpy(sc->sc_sensor[ADMLC_EXT].desc, "External",
  123. sizeof(sc->sc_sensor[ADMLC_EXT].desc));
  124. sc->sc_sensor[ADMLC_V2_5].type = SENSOR_VOLTS_DC;
  125. strlcpy(sc->sc_sensor[ADMLC_V2_5].desc, "2.5 V",
  126. sizeof(sc->sc_sensor[ADMLC_V2_5].desc));
  127. sc->sc_sensor[ADMLC_Vccp].type = SENSOR_VOLTS_DC;
  128. strlcpy(sc->sc_sensor[ADMLC_Vccp].desc, "Vccp",
  129. sizeof(sc->sc_sensor[ADMLC_Vccp].desc));
  130. sc->sc_sensor[ADMLC_Vcc].type = SENSOR_VOLTS_DC;
  131. strlcpy(sc->sc_sensor[ADMLC_Vcc].desc, "Vcc",
  132. sizeof(sc->sc_sensor[ADMLC_Vcc].desc));
  133. sc->sc_sensor[ADMLC_V5].type = SENSOR_VOLTS_DC;
  134. strlcpy(sc->sc_sensor[ADMLC_V5].desc, "5 V",
  135. sizeof(sc->sc_sensor[ADMLC_V5].desc));
  136. sc->sc_sensor[ADMLC_V12].type = SENSOR_VOLTS_DC;
  137. strlcpy(sc->sc_sensor[ADMLC_V12].desc, "12 V",
  138. sizeof(sc->sc_sensor[ADMLC_V12].desc));
  139. sc->sc_sensor[ADMLC_Vccp2].type = SENSOR_VOLTS_DC;
  140. strlcpy(sc->sc_sensor[ADMLC_Vccp2].desc, "Vccp2",
  141. sizeof(sc->sc_sensor[ADMLC_Vccp2].desc));
  142. sc->sc_sensor[ADMLC_FAN1].type = SENSOR_FANRPM;
  143. sc->sc_sensor[ADMLC_FAN2].type = SENSOR_FANRPM;
  144. if (sensor_task_register(sc, admlc_refresh, 5) == NULL) {
  145. printf(", unable to register update task\n");
  146. return;
  147. }
  148. for (i = 0; i < ADMLC_NUM_SENSORS; i++)
  149. sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
  150. sensordev_install(&sc->sc_sensordev);
  151. printf("\n");
  152. }
  153. static void
  154. fanval(struct ksensor *sens, int mul, u_int8_t data)
  155. {
  156. int tmp = data * mul;
  157. if (tmp == 0)
  158. sens->flags |= SENSOR_FINVALID;
  159. else
  160. sens->value = 1350000 / tmp;
  161. }
  162. void
  163. admlc_refresh(void *arg)
  164. {
  165. struct admlc_softc *sc = arg;
  166. u_int8_t cmd, data;
  167. int8_t sdata;
  168. iic_acquire_bus(sc->sc_tag, 0);
  169. cmd = ADM1024_INT_TEMP;
  170. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  171. sc->sc_addr, &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0)
  172. sc->sc_sensor[ADMLC_INT].value = 273150000 + 1000000 * sdata;
  173. cmd = ADM1024_EXT_TEMP;
  174. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  175. sc->sc_addr, &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0)
  176. sc->sc_sensor[ADMLC_EXT].value = 273150000 + 1000000 * sdata;
  177. cmd = ADM1024_STATUS2;
  178. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  179. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0) {
  180. if (data & ADM1024_STATUS2_EXT)
  181. sc->sc_sensor[ADMLC_EXT].flags |= SENSOR_FINVALID;
  182. else
  183. sc->sc_sensor[ADMLC_EXT].flags &= ~SENSOR_FINVALID;
  184. }
  185. cmd = ADM1024_V2_5;
  186. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  187. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  188. sc->sc_sensor[ADMLC_V2_5].value = 2500000 * data / 192;
  189. cmd = ADM1024_Vccp;
  190. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  191. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  192. sc->sc_sensor[ADMLC_Vcc].value = 2249000 * data / 192;
  193. cmd = ADM1024_Vcc;
  194. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  195. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  196. sc->sc_sensor[ADMLC_Vcc].value = 3300000 * data / 192;
  197. cmd = ADM1024_V5;
  198. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  199. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  200. sc->sc_sensor[ADMLC_V5].value = 5000000 * data / 192;
  201. cmd = ADM1024_V12;
  202. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  203. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  204. sc->sc_sensor[ADMLC_V12].value = 12000000 * data / 192;
  205. cmd = ADM1024_Vccp2;
  206. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  207. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  208. sc->sc_sensor[ADMLC_Vccp2].value = 2700000 * data / 192;
  209. cmd = ADM1024_FAN1;
  210. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  211. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  212. fanval(&sc->sc_sensor[ADMLC_FAN1], sc->sc_fan1mul, data);
  213. cmd = ADM1024_FAN2;
  214. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  215. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  216. fanval(&sc->sc_sensor[ADMLC_FAN2], sc->sc_fan2mul, data);
  217. iic_release_bus(sc->sc_tag, 0);
  218. }