asb100.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* $OpenBSD: asb100.c,v 1.11 2008/04/17 19:01:48 deraadt Exp $ */
  2. /*
  3. * Copyright (c) 2005 Damien Miller <djm@openbsd.org>
  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. /* Apparently the ASB100 always lives here */
  23. #define ASB100_ADDR 0x2d
  24. /* ASB100 registers */
  25. #define ASB100_TEMP3 0x17
  26. #define ASB100_TEMP3_MAX 0x18
  27. #define ASB100_TEMP3_HYST 0x19
  28. #define ASB100_VIN0 0x20
  29. #define ASB100_VIN1 0x21
  30. #define ASB100_VIN2 0x22
  31. #define ASB100_VIN3 0x23
  32. #define ASB100_VIN4 0x24
  33. #define ASB100_VIN5 0x25
  34. #define ASB100_VIN6 0x26
  35. #define ASB100_TEMP0 0x27
  36. #define ASB100_FAN0 0x28
  37. #define ASB100_FAN1 0x29
  38. #define ASB100_FAN2 0x30
  39. #define ASB100_VIN0_MIN 0x2b
  40. #define ASB100_VIN0_MAX 0x2c
  41. #define ASB100_VIN1_MIN 0x2d
  42. #define ASB100_VIN1_MAX 0x2e
  43. #define ASB100_VIN2_MIN 0x2f
  44. #define ASB100_VIN2_MAX 0x30
  45. #define ASB100_VIN3_MIN 0x31
  46. #define ASB100_VIN3_MAX 0x32
  47. #define ASB100_VIN4_MIN 0x33
  48. #define ASB100_VIN4_MAX 0x34
  49. #define ASB100_VIN5_MIN 0x35
  50. #define ASB100_VIN5_MAX 0x36
  51. #define ASB100_VIN6_MIN 0x37
  52. #define ASB100_VIN6_MAX 0x38
  53. #define ASB100_TEMP0_MAX 0x39
  54. #define ASB100_TEMP0_HYST 0x3a
  55. #define ASB100_FAN0_MIN 0x3b
  56. #define ASB100_FAN1_MIN 0x3c
  57. #define ASB100_FAN2_MIN 0x3d
  58. #define ASB100_CONFIG 0x40
  59. #define ASB100_ALARM1 0x41
  60. #define ASB100_ALARM2 0x42
  61. #define ASB100_SMIM1 0x43
  62. #define ASB100_SMIM2 0x44
  63. #define ASB100_VID_FANDIV01 0x47 /* 0-3 vid, 4-5 fan0, 6-7 fan1 */
  64. #define ASB100_I2C_ADDR 0x48
  65. #define ASB100_CHIPID 0x49
  66. #define ASB100_I2C_SUBADDR 0x4a
  67. #define ASB100_PIN_FANDIV2 0x4b /* 6-7 fan2 */
  68. #define ASB100_IRQ 0x4c
  69. #define ASB100_BANK 0x4e
  70. #define ASB100_CHIPMAN 0x4f
  71. #define ASB100_VID_CHIPID 0x58 /* 0 vid highbit, 1-7 chipid */
  72. #define ASB100_PWM 0x59 /* 0-3 duty cycle, 7 enable */
  73. /* TEMP1/2 sensors live on other chips, pointed to by the I2C_SUBADDR reg */
  74. #define ASB100_SUB1_TEMP1 0x50 /* LM75 format */
  75. #define ASB100_SUB1_TEMP1_HYST 0x53
  76. #define ASB100_SUB1_TEMP1_MAX 0x55
  77. #define ASB100_SUB2_TEMP2 0x50 /* LM75 format */
  78. #define ASB100_SUB2_TEMP2_HYST 0x53
  79. #define ASB100_SUB2_TEMP2_MAX 0x55
  80. /* Sensors */
  81. #define ASB100_SENSOR_VIN0 0
  82. #define ASB100_SENSOR_VIN1 1
  83. #define ASB100_SENSOR_VIN2 2
  84. #define ASB100_SENSOR_VIN3 3
  85. #define ASB100_SENSOR_VIN4 4
  86. #define ASB100_SENSOR_VIN5 5
  87. #define ASB100_SENSOR_VIN6 6
  88. #define ASB100_SENSOR_FAN0 7
  89. #define ASB100_SENSOR_FAN1 8
  90. #define ASB100_SENSOR_FAN2 9
  91. #define ASB100_SENSOR_TEMP0 10
  92. #define ASB100_SENSOR_TEMP1 11
  93. #define ASB100_SENSOR_TEMP2 12
  94. #define ASB100_SENSOR_TEMP3 13
  95. #define ASB100_NUM_SENSORS 14
  96. struct asbtm_softc {
  97. struct device sc_dev;
  98. i2c_tag_t sc_tag;
  99. i2c_addr_t sc_addr;
  100. struct ksensor sc_sensor[ASB100_NUM_SENSORS];
  101. struct ksensordev sc_sensordev;
  102. int sc_fanmul[3];
  103. int sc_satellite[2];
  104. };
  105. int asbtm_banksel(struct asbtm_softc *, u_int8_t, u_int8_t *);
  106. int asbtm_match(struct device *, void *, void *);
  107. void asbtm_attach(struct device *, struct device *, void *);
  108. void asbtm_refresh(void *);
  109. struct cfattach asbtm_ca = {
  110. sizeof(struct asbtm_softc), asbtm_match, asbtm_attach
  111. };
  112. struct cfdriver asbtm_cd = {
  113. NULL, "asbtm", DV_DULL
  114. };
  115. int
  116. asbtm_match(struct device *parent, void *match, void *aux)
  117. {
  118. struct i2c_attach_args *ia = aux;
  119. if (strcmp(ia->ia_name, "asb100") == 0)
  120. return (1);
  121. return (0);
  122. }
  123. int
  124. asbtm_banksel(struct asbtm_softc *sc, u_int8_t new_bank, u_int8_t *orig_bank)
  125. {
  126. u_int8_t cmd, data;
  127. new_bank &= 0xf;
  128. cmd = ASB100_BANK;
  129. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  130. &cmd, sizeof cmd, &data, sizeof data, 0))
  131. return (-1);
  132. if (orig_bank != NULL)
  133. *orig_bank = data & 0x0f;
  134. if ((data & 0xf) != new_bank) {
  135. cmd = ASB100_BANK;
  136. data = new_bank | (data & 0xf0);
  137. if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
  138. &cmd, sizeof cmd, &data, sizeof data, 0))
  139. return (-1);
  140. }
  141. return (0);
  142. }
  143. void
  144. asbtm_attach(struct device *parent, struct device *self, void *aux)
  145. {
  146. struct asbtm_softc *sc = (struct asbtm_softc *)self;
  147. struct i2c_attach_args *ia = aux;
  148. u_int8_t orig_bank, cmd, data;
  149. int i;
  150. sc->sc_tag = ia->ia_tag;
  151. sc->sc_addr = ia->ia_addr;
  152. iic_acquire_bus(sc->sc_tag, 0);
  153. if (asbtm_banksel(sc, 0, &orig_bank) == -1) {
  154. printf(": cannot get/set register bank\n");
  155. iic_release_bus(sc->sc_tag, 0);
  156. return;
  157. }
  158. cmd = ASB100_VID_FANDIV01;
  159. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  160. &cmd, sizeof cmd, &data, sizeof data, 0)) {
  161. printf(": cannot get fan01 register\n");
  162. iic_release_bus(sc->sc_tag, 0);
  163. return;
  164. }
  165. sc->sc_fanmul[0] = (1 << (data >> 4) & 0x3);
  166. sc->sc_fanmul[1] = (1 << (data >> 6) & 0x3);
  167. cmd = ASB100_PIN_FANDIV2;
  168. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  169. &cmd, sizeof cmd, &data, sizeof data, 0)) {
  170. printf(": cannot get fan2 register\n");
  171. iic_release_bus(sc->sc_tag, 0);
  172. return;
  173. }
  174. sc->sc_fanmul[0] = (1 << (data >> 6) & 0x3);
  175. cmd = ASB100_I2C_SUBADDR;
  176. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  177. &cmd, sizeof cmd, &data, sizeof data, 0)) {
  178. printf(": cannot get satellite chip address register\n");
  179. iic_release_bus(sc->sc_tag, 0);
  180. return;
  181. }
  182. /* Maybe a relative address of zero means "not present" here... */
  183. sc->sc_satellite[0] = 0x48 + (data & 0xf);
  184. sc->sc_satellite[1] = 0x48 + ((data >> 4) & 0xf);
  185. iic_ignore_addr(sc->sc_satellite[0]);
  186. iic_ignore_addr(sc->sc_satellite[1]);
  187. if (sc->sc_satellite[0] == sc->sc_satellite[1])
  188. sc->sc_satellite[1] = -1;
  189. if (asbtm_banksel(sc, orig_bank, NULL) == -1) {
  190. printf(": cannot restore saved bank %d\n", orig_bank);
  191. iic_release_bus(sc->sc_tag, 0);
  192. return;
  193. }
  194. iic_release_bus(sc->sc_tag, 0);
  195. /* Initialize sensor data. */
  196. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  197. sizeof(sc->sc_sensordev.xname));
  198. sc->sc_sensor[ASB100_SENSOR_VIN0].type = SENSOR_VOLTS_DC;
  199. sc->sc_sensor[ASB100_SENSOR_VIN1].type = SENSOR_VOLTS_DC;
  200. sc->sc_sensor[ASB100_SENSOR_VIN2].type = SENSOR_VOLTS_DC;
  201. sc->sc_sensor[ASB100_SENSOR_VIN3].type = SENSOR_VOLTS_DC;
  202. sc->sc_sensor[ASB100_SENSOR_VIN4].type = SENSOR_VOLTS_DC;
  203. sc->sc_sensor[ASB100_SENSOR_VIN5].type = SENSOR_VOLTS_DC;
  204. sc->sc_sensor[ASB100_SENSOR_VIN6].type = SENSOR_VOLTS_DC;
  205. sc->sc_sensor[ASB100_SENSOR_FAN0].type = SENSOR_FANRPM;
  206. sc->sc_sensor[ASB100_SENSOR_FAN1].type = SENSOR_FANRPM;
  207. sc->sc_sensor[ASB100_SENSOR_FAN2].type = SENSOR_FANRPM;
  208. sc->sc_sensor[ASB100_SENSOR_TEMP0].type = SENSOR_TEMP;
  209. strlcpy(sc->sc_sensor[ASB100_SENSOR_TEMP0].desc, "External",
  210. sizeof(sc->sc_sensor[ASB100_SENSOR_TEMP0].desc));
  211. sc->sc_sensor[ASB100_SENSOR_TEMP1].type = SENSOR_TEMP;
  212. strlcpy(sc->sc_sensor[ASB100_SENSOR_TEMP1].desc, "Internal",
  213. sizeof(sc->sc_sensor[ASB100_SENSOR_TEMP1].desc));
  214. sc->sc_sensor[ASB100_SENSOR_TEMP2].type = SENSOR_TEMP;
  215. strlcpy(sc->sc_sensor[ASB100_SENSOR_TEMP2].desc, "Internal",
  216. sizeof(sc->sc_sensor[ASB100_SENSOR_TEMP2].desc));
  217. if (sc->sc_satellite[1] == -1)
  218. sc->sc_sensor[ASB100_SENSOR_TEMP2].flags |= SENSOR_FINVALID;
  219. sc->sc_sensor[ASB100_SENSOR_TEMP3].type = SENSOR_TEMP;
  220. strlcpy(sc->sc_sensor[ASB100_SENSOR_TEMP3].desc, "External",
  221. sizeof(sc->sc_sensor[ASB100_SENSOR_TEMP3].desc));
  222. if (sensor_task_register(sc, asbtm_refresh, 5) == NULL) {
  223. printf(", unable to register update task\n");
  224. return;
  225. }
  226. for (i = 0; i < ASB100_NUM_SENSORS; i++)
  227. sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
  228. sensordev_install(&sc->sc_sensordev);
  229. printf("\n");
  230. }
  231. static void
  232. fanval(struct ksensor *sens, int mul, u_int8_t data)
  233. {
  234. int tmp = data * mul;
  235. if (tmp == 0)
  236. sens->flags |= SENSOR_FINVALID;
  237. else {
  238. sens->value = 1350000 / tmp;
  239. sens->flags &= ~SENSOR_FINVALID;
  240. }
  241. }
  242. void
  243. asbtm_refresh(void *arg)
  244. {
  245. struct asbtm_softc *sc = arg;
  246. u_int8_t orig_bank, cmd, data;
  247. int8_t sdata;
  248. u_int16_t sdata2;
  249. iic_acquire_bus(sc->sc_tag, 0);
  250. if (asbtm_banksel(sc, 0, &orig_bank) == -1) {
  251. printf("%s: cannot get/set register bank\n",
  252. sc->sc_dev.dv_xname);
  253. iic_release_bus(sc->sc_tag, 0);
  254. return;
  255. }
  256. cmd = ASB100_VIN0;
  257. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  258. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  259. sc->sc_sensor[ASB100_SENSOR_VIN0].value = (data * 1000000) / 16;
  260. cmd = ASB100_VIN1;
  261. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  262. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  263. sc->sc_sensor[ASB100_SENSOR_VIN1].value = (data * 1000000) / 16;
  264. cmd = ASB100_VIN2;
  265. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  266. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  267. sc->sc_sensor[ASB100_SENSOR_VIN2].value = (data * 1000000) / 16;
  268. cmd = ASB100_VIN3;
  269. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  270. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  271. sc->sc_sensor[ASB100_SENSOR_VIN3].value = (data * 1000000) / 16;
  272. cmd = ASB100_VIN4;
  273. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  274. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  275. sc->sc_sensor[ASB100_SENSOR_VIN4].value = (data * 1000000) / 16;
  276. cmd = ASB100_VIN5;
  277. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  278. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  279. sc->sc_sensor[ASB100_SENSOR_VIN5].value = (data * 1000000) / 16;
  280. cmd = ASB100_VIN6;
  281. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  282. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  283. sc->sc_sensor[ASB100_SENSOR_VIN6].value = (data * 1000000) / 16;
  284. cmd = ASB100_FAN0;
  285. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  286. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  287. fanval(&sc->sc_sensor[ASB100_SENSOR_FAN0],
  288. sc->sc_fanmul[0], data);
  289. cmd = ASB100_FAN1;
  290. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  291. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  292. fanval(&sc->sc_sensor[ASB100_SENSOR_FAN1],
  293. sc->sc_fanmul[1], data);
  294. cmd = ASB100_FAN2;
  295. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  296. &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  297. fanval(&sc->sc_sensor[ASB100_SENSOR_FAN2],
  298. sc->sc_fanmul[2], data);
  299. cmd = ASB100_TEMP0;
  300. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  301. &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0)
  302. sc->sc_sensor[ASB100_SENSOR_TEMP0].value = 273150000 +
  303. 1000000 * sdata;
  304. cmd = ASB100_TEMP3;
  305. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
  306. &cmd, sizeof cmd, &data, sizeof sdata, 0) == 0)
  307. sc->sc_sensor[ASB100_SENSOR_TEMP3].value = 273150000 +
  308. 1000000 * sdata;
  309. /* Read satellite chips for TEMP1/TEMP2 */
  310. cmd = ASB100_SUB1_TEMP1;
  311. if (sc->sc_satellite[0] != -1) {
  312. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  313. sc->sc_satellite[0], &cmd, sizeof cmd, &sdata2,
  314. sizeof sdata2, 0) == 0 && sdata2 != 0xffff) {
  315. sc->sc_sensor[ASB100_SENSOR_TEMP1].value = 273150000 +
  316. 500000 * (betoh16(sdata2) / 128);
  317. sc->sc_sensor[ASB100_SENSOR_TEMP2].flags &=
  318. ~SENSOR_FINVALID;
  319. } else {
  320. sc->sc_sensor[ASB100_SENSOR_TEMP2].flags |=
  321. SENSOR_FINVALID;
  322. }
  323. }
  324. cmd = ASB100_SUB2_TEMP2;
  325. if (sc->sc_satellite[1] != -1) {
  326. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  327. sc->sc_satellite[1], &cmd, sizeof cmd, &sdata2,
  328. sizeof sdata2, 0) == 0 && sdata2 != 0xffff) {
  329. sc->sc_sensor[ASB100_SENSOR_TEMP2].value = 273150000 +
  330. 500000 * (betoh16(sdata2) / 128);
  331. sc->sc_sensor[ASB100_SENSOR_TEMP2].flags &=
  332. ~SENSOR_FINVALID;
  333. } else {
  334. sc->sc_sensor[ASB100_SENSOR_TEMP2].flags |=
  335. SENSOR_FINVALID;
  336. }
  337. }
  338. asbtm_banksel(sc, orig_bank, NULL);
  339. iic_release_bus(sc->sc_tag, 0);
  340. }