st_accel_spi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics accelerometers driver
  4. *
  5. * Copyright 2012-2013 STMicroelectronics Inc.
  6. *
  7. * Denis Ciocca <[email protected]>
  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,lis3l02dq",
  66. .data = LIS3L02DQ_ACCEL_DEV_NAME,
  67. },
  68. {
  69. .compatible = "st,lng2dm-accel",
  70. .data = LNG2DM_ACCEL_DEV_NAME,
  71. },
  72. {
  73. .compatible = "st,h3lis331dl-accel",
  74. .data = H3LIS331DL_ACCEL_DEV_NAME,
  75. },
  76. {
  77. .compatible = "st,lis331dl-accel",
  78. .data = LIS331DL_ACCEL_DEV_NAME,
  79. },
  80. {
  81. .compatible = "st,lis2dw12",
  82. .data = LIS2DW12_ACCEL_DEV_NAME,
  83. },
  84. {
  85. .compatible = "st,lis3dhh",
  86. .data = LIS3DHH_ACCEL_DEV_NAME,
  87. },
  88. {
  89. .compatible = "st,lis3de",
  90. .data = LIS3DE_ACCEL_DEV_NAME,
  91. },
  92. {
  93. .compatible = "st,lis302dl",
  94. .data = LIS302DL_ACCEL_DEV_NAME,
  95. },
  96. {}
  97. };
  98. MODULE_DEVICE_TABLE(of, st_accel_of_match);
  99. static int st_accel_spi_probe(struct spi_device *spi)
  100. {
  101. const struct st_sensor_settings *settings;
  102. struct st_sensor_data *adata;
  103. struct iio_dev *indio_dev;
  104. int err;
  105. st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
  106. settings = st_accel_get_settings(spi->modalias);
  107. if (!settings) {
  108. dev_err(&spi->dev, "device name %s not recognized.\n",
  109. spi->modalias);
  110. return -ENODEV;
  111. }
  112. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
  113. if (!indio_dev)
  114. return -ENOMEM;
  115. adata = iio_priv(indio_dev);
  116. adata->sensor_settings = (struct st_sensor_settings *)settings;
  117. err = st_sensors_spi_configure(indio_dev, spi);
  118. if (err < 0)
  119. return err;
  120. err = st_sensors_power_enable(indio_dev);
  121. if (err)
  122. return err;
  123. return st_accel_common_probe(indio_dev);
  124. }
  125. static const struct spi_device_id st_accel_id_table[] = {
  126. { LIS3DH_ACCEL_DEV_NAME },
  127. { LSM330D_ACCEL_DEV_NAME },
  128. { LSM330DL_ACCEL_DEV_NAME },
  129. { LSM330DLC_ACCEL_DEV_NAME },
  130. { LIS331DLH_ACCEL_DEV_NAME },
  131. { LSM330_ACCEL_DEV_NAME },
  132. { LSM303AGR_ACCEL_DEV_NAME },
  133. { LIS2DH12_ACCEL_DEV_NAME },
  134. { LIS3L02DQ_ACCEL_DEV_NAME },
  135. { LNG2DM_ACCEL_DEV_NAME },
  136. { H3LIS331DL_ACCEL_DEV_NAME },
  137. { LIS331DL_ACCEL_DEV_NAME },
  138. { LIS3LV02DL_ACCEL_DEV_NAME },
  139. { LIS2DW12_ACCEL_DEV_NAME },
  140. { LIS3DHH_ACCEL_DEV_NAME },
  141. { LIS3DE_ACCEL_DEV_NAME },
  142. { LIS302DL_ACCEL_DEV_NAME },
  143. {},
  144. };
  145. MODULE_DEVICE_TABLE(spi, st_accel_id_table);
  146. static struct spi_driver st_accel_driver = {
  147. .driver = {
  148. .name = "st-accel-spi",
  149. .of_match_table = st_accel_of_match,
  150. },
  151. .probe = st_accel_spi_probe,
  152. .id_table = st_accel_id_table,
  153. };
  154. module_spi_driver(st_accel_driver);
  155. MODULE_AUTHOR("Denis Ciocca <[email protected]>");
  156. MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
  157. MODULE_LICENSE("GPL v2");
  158. MODULE_IMPORT_NS(IIO_ST_SENSORS);