st_accel_spi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * STMicroelectronics accelerometers 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_accel.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>-accel to maintain
  22. * compatibility
  23. */
  24. static const struct of_device_id st_accel_of_match[] = {
  25. {
  26. /* An older compatible */
  27. .compatible = "st,lis302dl-spi",
  28. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  29. },
  30. {
  31. .compatible = "st,lis3lv02dl-accel",
  32. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  33. },
  34. {
  35. .compatible = "st,lis3dh-accel",
  36. .data = LIS3DH_ACCEL_DEV_NAME,
  37. },
  38. {
  39. .compatible = "st,lsm330d-accel",
  40. .data = LSM330D_ACCEL_DEV_NAME,
  41. },
  42. {
  43. .compatible = "st,lsm330dl-accel",
  44. .data = LSM330DL_ACCEL_DEV_NAME,
  45. },
  46. {
  47. .compatible = "st,lsm330dlc-accel",
  48. .data = LSM330DLC_ACCEL_DEV_NAME,
  49. },
  50. {
  51. .compatible = "st,lis331dlh-accel",
  52. .data = LIS331DLH_ACCEL_DEV_NAME,
  53. },
  54. {
  55. .compatible = "st,lsm330-accel",
  56. .data = LSM330_ACCEL_DEV_NAME,
  57. },
  58. {
  59. .compatible = "st,lsm303agr-accel",
  60. .data = LSM303AGR_ACCEL_DEV_NAME,
  61. },
  62. {
  63. .compatible = "st,lis2dh12-accel",
  64. .data = LIS2DH12_ACCEL_DEV_NAME,
  65. },
  66. {
  67. .compatible = "st,lis3l02dq",
  68. .data = LIS3L02DQ_ACCEL_DEV_NAME,
  69. },
  70. {
  71. .compatible = "st,lng2dm-accel",
  72. .data = LNG2DM_ACCEL_DEV_NAME,
  73. },
  74. {
  75. .compatible = "st,h3lis331dl-accel",
  76. .data = H3LIS331DL_ACCEL_DEV_NAME,
  77. },
  78. {
  79. .compatible = "st,lis331dl-accel",
  80. .data = LIS331DL_ACCEL_DEV_NAME,
  81. },
  82. {
  83. .compatible = "st,lis2dw12",
  84. .data = LIS2DW12_ACCEL_DEV_NAME,
  85. },
  86. {
  87. .compatible = "st,lis3dhh",
  88. .data = LIS3DHH_ACCEL_DEV_NAME,
  89. },
  90. {}
  91. };
  92. MODULE_DEVICE_TABLE(of, st_accel_of_match);
  93. #else
  94. #define st_accel_of_match NULL
  95. #endif
  96. static int st_accel_spi_probe(struct spi_device *spi)
  97. {
  98. struct iio_dev *indio_dev;
  99. struct st_sensor_data *adata;
  100. int err;
  101. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
  102. if (!indio_dev)
  103. return -ENOMEM;
  104. adata = iio_priv(indio_dev);
  105. st_sensors_of_name_probe(&spi->dev, st_accel_of_match,
  106. spi->modalias, sizeof(spi->modalias));
  107. st_sensors_spi_configure(indio_dev, spi, adata);
  108. err = st_accel_common_probe(indio_dev);
  109. if (err < 0)
  110. return err;
  111. return 0;
  112. }
  113. static int st_accel_spi_remove(struct spi_device *spi)
  114. {
  115. st_accel_common_remove(spi_get_drvdata(spi));
  116. return 0;
  117. }
  118. static const struct spi_device_id st_accel_id_table[] = {
  119. { LIS3DH_ACCEL_DEV_NAME },
  120. { LSM330D_ACCEL_DEV_NAME },
  121. { LSM330DL_ACCEL_DEV_NAME },
  122. { LSM330DLC_ACCEL_DEV_NAME },
  123. { LIS331DLH_ACCEL_DEV_NAME },
  124. { LSM330_ACCEL_DEV_NAME },
  125. { LSM303AGR_ACCEL_DEV_NAME },
  126. { LIS2DH12_ACCEL_DEV_NAME },
  127. { LIS3L02DQ_ACCEL_DEV_NAME },
  128. { LNG2DM_ACCEL_DEV_NAME },
  129. { H3LIS331DL_ACCEL_DEV_NAME },
  130. { LIS331DL_ACCEL_DEV_NAME },
  131. { LIS3LV02DL_ACCEL_DEV_NAME },
  132. { LIS2DW12_ACCEL_DEV_NAME },
  133. { LIS3DHH_ACCEL_DEV_NAME },
  134. {},
  135. };
  136. MODULE_DEVICE_TABLE(spi, st_accel_id_table);
  137. static struct spi_driver st_accel_driver = {
  138. .driver = {
  139. .name = "st-accel-spi",
  140. .of_match_table = of_match_ptr(st_accel_of_match),
  141. },
  142. .probe = st_accel_spi_probe,
  143. .remove = st_accel_spi_remove,
  144. .id_table = st_accel_id_table,
  145. };
  146. module_spi_driver(st_accel_driver);
  147. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  148. MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
  149. MODULE_LICENSE("GPL v2");