thmc50.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* $OpenBSD: thmc50.c,v 1.4 2007/10/17 17:14:54 deraadt Exp $ */
  2. /*
  3. * Copyright (c) 2007 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. /* THMC50 registers */
  23. #define THMC50_TEMP0 0x27
  24. #define THMC50_TEMP1 0x26
  25. #define THMC50_TEMP2 0x20
  26. /* Sensors */
  27. #define THMC_TEMP0 0
  28. #define THMC_TEMP1 1
  29. #define THMC_TEMP2 2
  30. #define THMC_NUM_SENSORS 3
  31. struct thmc_softc {
  32. struct device sc_dev;
  33. i2c_tag_t sc_tag;
  34. i2c_addr_t sc_addr;
  35. struct ksensor sc_sensor[THMC_NUM_SENSORS];
  36. struct ksensordev sc_sensordev;
  37. };
  38. int thmc_match(struct device *, void *, void *);
  39. void thmc_attach(struct device *, struct device *, void *);
  40. void thmc_refresh(void *);
  41. struct cfattach thmc_ca = {
  42. sizeof(struct thmc_softc), thmc_match, thmc_attach
  43. };
  44. struct cfdriver thmc_cd = {
  45. NULL, "thmc", DV_DULL
  46. };
  47. int
  48. thmc_match(struct device *parent, void *match, void *aux)
  49. {
  50. struct i2c_attach_args *ia = aux;
  51. if (strcmp(ia->ia_name, "thmc50") == 0 ||
  52. strcmp(ia->ia_name, "adm1022") == 0 ||
  53. strcmp(ia->ia_name, "adm1028") == 0)
  54. return (1);
  55. return (0);
  56. }
  57. void
  58. thmc_attach(struct device *parent, struct device *self, void *aux)
  59. {
  60. struct thmc_softc *sc = (struct thmc_softc *)self;
  61. struct i2c_attach_args *ia = aux;
  62. int numsensors = THMC_NUM_SENSORS;
  63. int i;
  64. sc->sc_tag = ia->ia_tag;
  65. sc->sc_addr = ia->ia_addr;
  66. printf(": %s", ia->ia_name);
  67. /* Initialize sensor data. */
  68. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  69. sizeof(sc->sc_sensordev.xname));
  70. sc->sc_sensor[THMC_TEMP0].type = SENSOR_TEMP;
  71. strlcpy(sc->sc_sensor[THMC_TEMP0].desc, "Internal",
  72. sizeof(sc->sc_sensor[THMC_TEMP0].desc));
  73. sc->sc_sensor[THMC_TEMP1].type = SENSOR_TEMP;
  74. strlcpy(sc->sc_sensor[THMC_TEMP1].desc, "External",
  75. sizeof(sc->sc_sensor[THMC_TEMP1].desc));
  76. if (strcmp(ia->ia_name, "adm1022") == 0) {
  77. /* Only the adm1022 has a THMC50_TEMP2 sensor */
  78. sc->sc_sensor[THMC_TEMP2].type = SENSOR_TEMP;
  79. strlcpy(sc->sc_sensor[THMC_TEMP2].desc, "External",
  80. sizeof(sc->sc_sensor[THMC_TEMP2].desc));
  81. } else {
  82. sc->sc_sensor[THMC_TEMP2].type = -1;
  83. numsensors--;
  84. }
  85. if (sensor_task_register(sc, thmc_refresh, 5) == NULL) {
  86. printf(", unable to register update task\n");
  87. return;
  88. }
  89. for (i = 0; i < numsensors; i++)
  90. sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
  91. sensordev_install(&sc->sc_sensordev);
  92. printf("\n");
  93. }
  94. void
  95. thmc_refresh(void *arg)
  96. {
  97. struct thmc_softc *sc = arg;
  98. u_int8_t cmd;
  99. int8_t sdata;
  100. iic_acquire_bus(sc->sc_tag, 0);
  101. cmd = THMC50_TEMP0;
  102. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  103. sc->sc_addr, &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0) {
  104. sc->sc_sensor[THMC_TEMP0].value = 273150000 + 1000000 * sdata;
  105. sc->sc_sensor[THMC_TEMP0].flags &= ~SENSOR_FINVALID;
  106. } else
  107. sc->sc_sensor[THMC_TEMP0].flags |= SENSOR_FINVALID;
  108. cmd = THMC50_TEMP1;
  109. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  110. sc->sc_addr, &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0) {
  111. sc->sc_sensor[THMC_TEMP1].value = 273150000 + 1000000 * sdata;
  112. sc->sc_sensor[THMC_TEMP1].flags &= ~SENSOR_FINVALID;
  113. } else
  114. sc->sc_sensor[THMC_TEMP1].flags |= SENSOR_FINVALID;
  115. if (sc->sc_sensor[THMC_TEMP2].type > 0) {
  116. cmd = THMC50_TEMP2;
  117. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  118. sc->sc_addr, &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0) {
  119. sc->sc_sensor[THMC_TEMP2].value = 273150000 + 1000000 * sdata;
  120. sc->sc_sensor[THMC_TEMP2].flags &= ~SENSOR_FINVALID;
  121. } else
  122. sc->sc_sensor[THMC_TEMP2].flags |= SENSOR_FINVALID;
  123. }
  124. iic_release_bus(sc->sc_tag, 0);
  125. }