st_accel_spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics accelerometers 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/mod_devicetable.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_accel.h"
  17. /*
  18. * For new single-chip sensors use <device_name> as compatible string.
  19. * For old single-chip devices keep <device_name>-accel to maintain
  20. * compatibility
  21. */
  22. static const struct of_device_id st_accel_of_match[] = {
  23. {
  24. /* An older compatible */
  25. .compatible = "st,lis302dl-spi",
  26. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  27. },
  28. {
  29. .compatible = "st,lis3lv02dl-accel",
  30. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  31. },
  32. {
  33. .compatible = "st,lis3dh-accel",
  34. .data = LIS3DH_ACCEL_DEV_NAME,
  35. },
  36. {
  37. .compatible = "st,lsm330d-accel",
  38. .data = LSM330D_ACCEL_DEV_NAME,
  39. },
  40. {
  41. .compatible = "st,lsm330dl-accel",
  42. .data = LSM330DL_ACCEL_DEV_NAME,
  43. },
  44. {
  45. .compatible = "st,lsm330dlc-accel",
  46. .data = LSM330DLC_ACCEL_DEV_NAME,
  47. },
  48. {
  49. .compatible = "st,lis331dlh-accel",
  50. .data = LIS331DLH_ACCEL_DEV_NAME,
  51. },
  52. {
  53. .compatible = "st,lsm330-accel",
  54. .data = LSM330_ACCEL_DEV_NAME,
  55. },
  56. {
  57. .compatible = "st,lsm303agr-accel",
  58. .data = LSM303AGR_ACCEL_DEV_NAME,
  59. },
  60. {
  61. .compatible = "st,lis2dh12-accel",
  62. .data = LIS2DH12_ACCEL_DEV_NAME,
  63. },
  64. {
  65. .compatible = "st,lis2ds12",
  66. .data = LIS2DS12_ACCEL_DEV_NAME,
  67. },
  68. {
  69. .compatible = "st,lis3l02dq",
  70. .data = LIS3L02DQ_ACCEL_DEV_NAME,
  71. },
  72. {
  73. .compatible = "st,lng2dm-accel",
  74. .data = LNG2DM_ACCEL_DEV_NAME,
  75. },
  76. {
  77. .compatible = "st,h3lis331dl-accel",
  78. .data = H3LIS331DL_ACCEL_DEV_NAME,
  79. },
  80. {
  81. .compatible = "st,lis331dl-accel",
  82. .data = LIS331DL_ACCEL_DEV_NAME,
  83. },
  84. {
  85. .compatible = "st,lis2dw12",
  86. .data = LIS2DW12_ACCEL_DEV_NAME,
  87. },
  88. {
  89. .compatible = "st,lis3dhh",
  90. .data = LIS3DHH_ACCEL_DEV_NAME,
  91. },
  92. {
  93. .compatible = "st,lis3de",
  94. .data = LIS3DE_ACCEL_DEV_NAME,
  95. },
  96. {
  97. .compatible = "st,lis302dl",
  98. .data = LIS302DL_ACCEL_DEV_NAME,
  99. },
  100. {
  101. .compatible = "st,lsm303c-accel",
  102. .data = LSM303C_ACCEL_DEV_NAME,
  103. },
  104. {
  105. .compatible = "st,iis328dq",
  106. .data = IIS328DQ_ACCEL_DEV_NAME,
  107. },
  108. { }
  109. };
  110. MODULE_DEVICE_TABLE(of, st_accel_of_match);
  111. static int st_accel_spi_probe(struct spi_device *spi)
  112. {
  113. const struct st_sensor_settings *settings;
  114. struct st_sensor_data *adata;
  115. struct iio_dev *indio_dev;
  116. int err;
  117. st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
  118. settings = st_accel_get_settings(spi->modalias);
  119. if (!settings) {
  120. dev_err(&spi->dev, "device name %s not recognized.\n",
  121. spi->modalias);
  122. return -ENODEV;
  123. }
  124. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
  125. if (!indio_dev)
  126. return -ENOMEM;
  127. adata = iio_priv(indio_dev);
  128. adata->sensor_settings = (struct st_sensor_settings *)settings;
  129. err = st_sensors_spi_configure(indio_dev, spi);
  130. if (err < 0)
  131. return err;
  132. err = st_sensors_power_enable(indio_dev);
  133. if (err)
  134. return err;
  135. return st_accel_common_probe(indio_dev);
  136. }
  137. static const struct spi_device_id st_accel_id_table[] = {
  138. { LIS3DH_ACCEL_DEV_NAME },
  139. { LSM330D_ACCEL_DEV_NAME },
  140. { LSM330DL_ACCEL_DEV_NAME },
  141. { LSM330DLC_ACCEL_DEV_NAME },
  142. { LIS331DLH_ACCEL_DEV_NAME },
  143. { LSM330_ACCEL_DEV_NAME },
  144. { LSM303AGR_ACCEL_DEV_NAME },
  145. { LIS2DH12_ACCEL_DEV_NAME },
  146. { LIS2DS12_ACCEL_DEV_NAME },
  147. { LIS3L02DQ_ACCEL_DEV_NAME },
  148. { LNG2DM_ACCEL_DEV_NAME },
  149. { H3LIS331DL_ACCEL_DEV_NAME },
  150. { LIS331DL_ACCEL_DEV_NAME },
  151. { LIS3LV02DL_ACCEL_DEV_NAME },
  152. { LIS2DW12_ACCEL_DEV_NAME },
  153. { LIS3DHH_ACCEL_DEV_NAME },
  154. { LIS3DE_ACCEL_DEV_NAME },
  155. { LIS302DL_ACCEL_DEV_NAME },
  156. { LSM303C_ACCEL_DEV_NAME },
  157. { IIS328DQ_ACCEL_DEV_NAME },
  158. { }
  159. };
  160. MODULE_DEVICE_TABLE(spi, st_accel_id_table);
  161. static struct spi_driver st_accel_driver = {
  162. .driver = {
  163. .name = "st-accel-spi",
  164. .of_match_table = st_accel_of_match,
  165. },
  166. .probe = st_accel_spi_probe,
  167. .id_table = st_accel_id_table,
  168. };
  169. module_spi_driver(st_accel_driver);
  170. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  171. MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
  172. MODULE_LICENSE("GPL v2");
  173. MODULE_IMPORT_NS("IIO_ST_SENSORS");