st_gyro_spi.c 3.2 KB

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