st_accel_spi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. };
  84. MODULE_DEVICE_TABLE(of, st_accel_of_match);
  85. #else
  86. #define st_accel_of_match NULL
  87. #endif
  88. static int st_accel_spi_probe(struct spi_device *spi)
  89. {
  90. struct iio_dev *indio_dev;
  91. struct st_sensor_data *adata;
  92. int err;
  93. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
  94. if (!indio_dev)
  95. return -ENOMEM;
  96. adata = iio_priv(indio_dev);
  97. st_sensors_of_name_probe(&spi->dev, st_accel_of_match,
  98. spi->modalias, sizeof(spi->modalias));
  99. st_sensors_spi_configure(indio_dev, spi, adata);
  100. err = st_accel_common_probe(indio_dev);
  101. if (err < 0)
  102. return err;
  103. return 0;
  104. }
  105. static int st_accel_spi_remove(struct spi_device *spi)
  106. {
  107. st_accel_common_remove(spi_get_drvdata(spi));
  108. return 0;
  109. }
  110. static const struct spi_device_id st_accel_id_table[] = {
  111. { LIS3DH_ACCEL_DEV_NAME },
  112. { LSM330D_ACCEL_DEV_NAME },
  113. { LSM330DL_ACCEL_DEV_NAME },
  114. { LSM330DLC_ACCEL_DEV_NAME },
  115. { LIS331DLH_ACCEL_DEV_NAME },
  116. { LSM330_ACCEL_DEV_NAME },
  117. { LSM303AGR_ACCEL_DEV_NAME },
  118. { LIS2DH12_ACCEL_DEV_NAME },
  119. { LIS3L02DQ_ACCEL_DEV_NAME },
  120. { LNG2DM_ACCEL_DEV_NAME },
  121. { H3LIS331DL_ACCEL_DEV_NAME },
  122. { LIS331DL_ACCEL_DEV_NAME },
  123. { LIS3LV02DL_ACCEL_DEV_NAME },
  124. {},
  125. };
  126. MODULE_DEVICE_TABLE(spi, st_accel_id_table);
  127. static struct spi_driver st_accel_driver = {
  128. .driver = {
  129. .name = "st-accel-spi",
  130. .of_match_table = of_match_ptr(st_accel_of_match),
  131. },
  132. .probe = st_accel_spi_probe,
  133. .remove = st_accel_spi_remove,
  134. .id_table = st_accel_id_table,
  135. };
  136. module_spi_driver(st_accel_driver);
  137. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  138. MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
  139. MODULE_LICENSE("GPL v2");