adm1030.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* $OpenBSD: adm1030.c,v 1.9 2008/05/01 23:02:05 deraadt 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 1030 registers */
  23. #define ADM1030_INT_TEMP 0x0a
  24. #define ADM1030_EXT_TEMP 0x0b
  25. #define ADM1030_FAN 0x08
  26. #define ADM1030_FANC 0x20
  27. #define ADM1024_FANC_VAL(x) (x >> 6)
  28. /* Sensors */
  29. #define ADMTMP_INT 0
  30. #define ADMTMP_EXT 1
  31. #define ADMTMP_FAN 2
  32. #define ADMTMP_NUM_SENSORS 3
  33. struct admtmp_softc {
  34. struct device sc_dev;
  35. i2c_tag_t sc_tag;
  36. i2c_addr_t sc_addr;
  37. int sc_fanmul;
  38. struct ksensor sc_sensor[ADMTMP_NUM_SENSORS];
  39. struct ksensordev sc_sensordev;
  40. };
  41. int admtmp_match(struct device *, void *, void *);
  42. void admtmp_attach(struct device *, struct device *, void *);
  43. void admtmp_refresh(void *);
  44. struct cfattach admtmp_ca = {
  45. sizeof(struct admtmp_softc), admtmp_match, admtmp_attach
  46. };
  47. struct cfdriver admtmp_cd = {
  48. NULL, "admtmp", DV_DULL
  49. };
  50. int
  51. admtmp_match(struct device *parent, void *match, void *aux)
  52. {
  53. struct i2c_attach_args *ia = aux;
  54. if (strcmp(ia->ia_name, "adm1030") == 0)
  55. return (1);
  56. return (0);
  57. }
  58. void
  59. admtmp_attach(struct device *parent, struct device *self, void *aux)
  60. {
  61. struct admtmp_softc *sc = (struct admtmp_softc *)self;
  62. struct i2c_attach_args *ia = aux;
  63. u_int8_t cmd, data;
  64. int i;
  65. sc->sc_tag = ia->ia_tag;
  66. sc->sc_addr = ia->ia_addr;
  67. cmd = ADM1030_FANC;
  68. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  69. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
  70. printf(": unable to read fan setting\n");
  71. return;
  72. }
  73. sc->sc_fanmul = 11250/8 * (1 << ADM1024_FANC_VAL(data)) * 60;
  74. /* Initialize sensor data. */
  75. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  76. sizeof(sc->sc_sensordev.xname));
  77. sc->sc_sensor[ADMTMP_INT].type = SENSOR_TEMP;
  78. strlcpy(sc->sc_sensor[ADMTMP_INT].desc, "Internal",
  79. sizeof(sc->sc_sensor[ADMTMP_INT].desc));
  80. sc->sc_sensor[ADMTMP_EXT].type = SENSOR_TEMP;
  81. strlcpy(sc->sc_sensor[ADMTMP_EXT].desc, "External",
  82. sizeof(sc->sc_sensor[ADMTMP_EXT].desc));
  83. sc->sc_sensor[ADMTMP_FAN].type = SENSOR_FANRPM;
  84. if (sensor_task_register(sc, admtmp_refresh, 5) == NULL) {
  85. printf(": unable to register update task\n");
  86. return;
  87. }
  88. for (i = 0; i < ADMTMP_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. admtmp_refresh(void *arg)
  95. {
  96. struct admtmp_softc *sc = arg;
  97. u_int8_t cmd, data;
  98. iic_acquire_bus(sc->sc_tag, 0);
  99. cmd = ADM1030_INT_TEMP;
  100. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  101. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  102. sc->sc_sensor[ADMTMP_INT].value = 273150000 + 1000000 * data;
  103. cmd = ADM1030_EXT_TEMP;
  104. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  105. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
  106. sc->sc_sensor[ADMTMP_EXT].value = 273150000 + 1000000 * data;
  107. cmd = ADM1030_FAN;
  108. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  109. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0) {
  110. if (data == 0)
  111. sc->sc_sensor[ADMTMP_FAN].flags |= SENSOR_FINVALID;
  112. else {
  113. sc->sc_sensor[ADMTMP_FAN].value =
  114. sc->sc_fanmul / (2 * (int)data);
  115. sc->sc_sensor[ADMTMP_FAN].flags &= ~SENSOR_FINVALID;
  116. }
  117. }
  118. iic_release_bus(sc->sc_tag, 0);
  119. }