lis331dl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* $OpenBSD: lis331dl.c,v 1.1 2009/08/12 14:51:20 cnst Exp $ */
  2. /*
  3. * Copyright (c) 2009 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
  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. /*
  23. * STMicroelectronics LIS331DL
  24. * MEMS motion sensor
  25. * http://www.stm.com/stonline/products/literature/ds/13951.pdf
  26. * April 2008
  27. */
  28. /* 3-axis accelerometer */
  29. #define LISA_NUM_AXIS 3
  30. static const struct {
  31. const char *name;
  32. const uint8_t reg;
  33. } lisa_axis[LISA_NUM_AXIS] = {
  34. { "OUT_X", 0x29 },
  35. { "OUT_Y", 0x2b },
  36. { "OUT_Z", 0x2d }
  37. };
  38. struct lisa_softc {
  39. struct device sc_dev;
  40. i2c_tag_t sc_tag;
  41. i2c_addr_t sc_addr;
  42. struct ksensor sc_sensors[LISA_NUM_AXIS];
  43. struct ksensordev sc_sensordev;
  44. };
  45. int lisa_match(struct device *, void *, void *);
  46. void lisa_attach(struct device *, struct device *, void *);
  47. void lisa_refresh(void *);
  48. uint8_t lisa_readreg(struct lisa_softc *, uint8_t);
  49. void lisa_writereg(struct lisa_softc *, uint8_t, uint8_t);
  50. struct cfattach lisa_ca = {
  51. sizeof(struct lisa_softc), lisa_match, lisa_attach
  52. };
  53. struct cfdriver lisa_cd = {
  54. NULL, "lisa", DV_DULL
  55. };
  56. int
  57. lisa_match(struct device *parent, void *match, void *aux)
  58. {
  59. struct i2c_attach_args *ia = aux;
  60. if (strcmp(ia->ia_name, "lis331dl") == 0)
  61. return 1;
  62. return 0;
  63. }
  64. void
  65. lisa_attach(struct device *parent, struct device *self, void *aux)
  66. {
  67. struct lisa_softc *sc = (struct lisa_softc *)self;
  68. struct i2c_attach_args *ia = aux;
  69. int i;
  70. sc->sc_tag = ia->ia_tag;
  71. sc->sc_addr = ia->ia_addr;
  72. printf(": %s", ia->ia_name);
  73. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  74. sizeof(sc->sc_sensordev.xname));
  75. for (i = 0; i < LISA_NUM_AXIS; i++) {
  76. strlcpy(sc->sc_sensors[i].desc, lisa_axis[i].name,
  77. sizeof(sc->sc_sensors[i].desc));
  78. sc->sc_sensors[i].type = SENSOR_INTEGER;
  79. sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[i]);
  80. }
  81. if (sensor_task_register(sc, lisa_refresh, 1) == NULL) {
  82. printf(", unable to register update task\n");
  83. return;
  84. }
  85. sensordev_install(&sc->sc_sensordev);
  86. printf("\n");
  87. }
  88. void
  89. lisa_refresh(void *arg)
  90. {
  91. struct lisa_softc *sc = arg;
  92. struct ksensor *s = sc->sc_sensors;
  93. int i;
  94. iic_acquire_bus(sc->sc_tag, 0);
  95. for (i = 0; i < LISA_NUM_AXIS; i++)
  96. s[i].value = (int8_t)lisa_readreg(sc, lisa_axis[i].reg);
  97. iic_release_bus(sc->sc_tag, 0);
  98. }
  99. uint8_t
  100. lisa_readreg(struct lisa_softc *sc, uint8_t reg)
  101. {
  102. uint8_t data;
  103. iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  104. sc->sc_addr, &reg, sizeof reg, &data, sizeof data, 0);
  105. return data;
  106. }
  107. void
  108. lisa_writereg(struct lisa_softc *sc, uint8_t reg, uint8_t data)
  109. {
  110. iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
  111. sc->sc_addr, &reg, sizeof reg, &data, sizeof data, 0);
  112. }