st_gyro_spi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * STMicroelectronics gyroscopes driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <denis.ciocca@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/common/st_sensors.h>
  16. #include <linux/iio/common/st_sensors_spi.h>
  17. #include "st_gyro.h"
  18. #ifdef CONFIG_OF
  19. /*
  20. * For new single-chip sensors use <device_name> as compatible string.
  21. * For old single-chip devices keep <device_name>-gyro to maintain
  22. * compatibility
  23. */
  24. static const struct of_device_id st_gyro_of_match[] = {
  25. {
  26. .compatible = "st,l3g4200d-gyro",
  27. .data = L3G4200D_GYRO_DEV_NAME,
  28. },
  29. {
  30. .compatible = "st,lsm330d-gyro",
  31. .data = LSM330D_GYRO_DEV_NAME,
  32. },
  33. {
  34. .compatible = "st,lsm330dl-gyro",
  35. .data = LSM330DL_GYRO_DEV_NAME,
  36. },
  37. {
  38. .compatible = "st,lsm330dlc-gyro",
  39. .data = LSM330DLC_GYRO_DEV_NAME,
  40. },
  41. {
  42. .compatible = "st,l3gd20-gyro",
  43. .data = L3GD20_GYRO_DEV_NAME,
  44. },
  45. {
  46. .compatible = "st,l3gd20h-gyro",
  47. .data = L3GD20H_GYRO_DEV_NAME,
  48. },
  49. {
  50. .compatible = "st,l3g4is-gyro",
  51. .data = L3G4IS_GYRO_DEV_NAME,
  52. },
  53. {
  54. .compatible = "st,lsm330-gyro",
  55. .data = LSM330_GYRO_DEV_NAME,
  56. },
  57. {
  58. .compatible = "st,lsm9ds0-gyro",
  59. .data = LSM9DS0_GYRO_DEV_NAME,
  60. },
  61. {},
  62. };
  63. MODULE_DEVICE_TABLE(of, st_gyro_of_match);
  64. #else
  65. #define st_gyro_of_match NULL
  66. #endif
  67. static int st_gyro_spi_probe(struct spi_device *spi)
  68. {
  69. struct iio_dev *indio_dev;
  70. struct st_sensor_data *gdata;
  71. int err;
  72. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*gdata));
  73. if (!indio_dev)
  74. return -ENOMEM;
  75. gdata = iio_priv(indio_dev);
  76. st_sensors_of_name_probe(&spi->dev, st_gyro_of_match,
  77. spi->modalias, sizeof(spi->modalias));
  78. st_sensors_spi_configure(indio_dev, spi, gdata);
  79. err = st_gyro_common_probe(indio_dev);
  80. if (err < 0)
  81. return err;
  82. return 0;
  83. }
  84. static int st_gyro_spi_remove(struct spi_device *spi)
  85. {
  86. st_gyro_common_remove(spi_get_drvdata(spi));
  87. return 0;
  88. }
  89. static const struct spi_device_id st_gyro_id_table[] = {
  90. { L3G4200D_GYRO_DEV_NAME },
  91. { LSM330D_GYRO_DEV_NAME },
  92. { LSM330DL_GYRO_DEV_NAME },
  93. { LSM330DLC_GYRO_DEV_NAME },
  94. { L3GD20_GYRO_DEV_NAME },
  95. { L3GD20H_GYRO_DEV_NAME },
  96. { L3G4IS_GYRO_DEV_NAME },
  97. { LSM330_GYRO_DEV_NAME },
  98. { LSM9DS0_GYRO_DEV_NAME },
  99. {},
  100. };
  101. MODULE_DEVICE_TABLE(spi, st_gyro_id_table);
  102. static struct spi_driver st_gyro_driver = {
  103. .driver = {
  104. .name = "st-gyro-spi",
  105. .of_match_table = of_match_ptr(st_gyro_of_match),
  106. },
  107. .probe = st_gyro_spi_probe,
  108. .remove = st_gyro_spi_remove,
  109. .id_table = st_gyro_id_table,
  110. };
  111. module_spi_driver(st_gyro_driver);
  112. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  113. MODULE_DESCRIPTION("STMicroelectronics gyroscopes spi driver");
  114. MODULE_LICENSE("GPL v2");