st_accel_i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/i2c.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/common/st_sensors_i2c.h>
  15. #include "st_accel.h"
  16. static const struct of_device_id st_accel_of_match[] = {
  17. {
  18. /* An older compatible */
  19. .compatible = "st,lis3lv02d",
  20. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  21. },
  22. {
  23. .compatible = "st,lis3lv02dl-accel",
  24. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  25. },
  26. {
  27. .compatible = "st,lsm303dlh-accel",
  28. .data = LSM303DLH_ACCEL_DEV_NAME,
  29. },
  30. {
  31. .compatible = "st,lsm303dlhc-accel",
  32. .data = LSM303DLHC_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,lis331dl-accel",
  52. .data = LIS331DL_ACCEL_DEV_NAME,
  53. },
  54. {
  55. .compatible = "st,lis331dlh-accel",
  56. .data = LIS331DLH_ACCEL_DEV_NAME,
  57. },
  58. {
  59. .compatible = "st,lsm303dl-accel",
  60. .data = LSM303DL_ACCEL_DEV_NAME,
  61. },
  62. {
  63. .compatible = "st,lsm303dlm-accel",
  64. .data = LSM303DLM_ACCEL_DEV_NAME,
  65. },
  66. {
  67. .compatible = "st,lsm330-accel",
  68. .data = LSM330_ACCEL_DEV_NAME,
  69. },
  70. {
  71. .compatible = "st,lsm303agr-accel",
  72. .data = LSM303AGR_ACCEL_DEV_NAME,
  73. },
  74. {
  75. .compatible = "st,lis2dh12-accel",
  76. .data = LIS2DH12_ACCEL_DEV_NAME,
  77. },
  78. {
  79. .compatible = "st,h3lis331dl-accel",
  80. .data = H3LIS331DL_ACCEL_DEV_NAME,
  81. },
  82. {
  83. .compatible = "st,lis3l02dq",
  84. .data = LIS3L02DQ_ACCEL_DEV_NAME,
  85. },
  86. {
  87. .compatible = "st,lng2dm-accel",
  88. .data = LNG2DM_ACCEL_DEV_NAME,
  89. },
  90. {
  91. .compatible = "st,lis2dw12",
  92. .data = LIS2DW12_ACCEL_DEV_NAME,
  93. },
  94. {
  95. .compatible = "st,lis3de",
  96. .data = LIS3DE_ACCEL_DEV_NAME,
  97. },
  98. {
  99. .compatible = "st,lis2de12",
  100. .data = LIS2DE12_ACCEL_DEV_NAME,
  101. },
  102. {
  103. .compatible = "st,lis2ds12",
  104. .data = LIS2DS12_ACCEL_DEV_NAME,
  105. },
  106. {
  107. .compatible = "st,lis2hh12",
  108. .data = LIS2HH12_ACCEL_DEV_NAME,
  109. },
  110. {
  111. .compatible = "st,lis302dl",
  112. .data = LIS302DL_ACCEL_DEV_NAME,
  113. },
  114. {
  115. .compatible = "st,lsm303c-accel",
  116. .data = LSM303C_ACCEL_DEV_NAME,
  117. },
  118. {
  119. .compatible = "silan,sc7a20",
  120. .data = SC7A20_ACCEL_DEV_NAME,
  121. },
  122. {
  123. .compatible = "st,iis328dq",
  124. .data = IIS328DQ_ACCEL_DEV_NAME,
  125. },
  126. {},
  127. };
  128. MODULE_DEVICE_TABLE(of, st_accel_of_match);
  129. static const struct acpi_device_id st_accel_acpi_match[] = {
  130. {"SMO8840", (kernel_ulong_t)LIS2DH12_ACCEL_DEV_NAME},
  131. {"SMO8A90", (kernel_ulong_t)LNG2DM_ACCEL_DEV_NAME},
  132. { },
  133. };
  134. MODULE_DEVICE_TABLE(acpi, st_accel_acpi_match);
  135. static const struct i2c_device_id st_accel_id_table[] = {
  136. { LSM303DLH_ACCEL_DEV_NAME },
  137. { LSM303DLHC_ACCEL_DEV_NAME },
  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. { LSM303DL_ACCEL_DEV_NAME },
  144. { LSM303DLM_ACCEL_DEV_NAME },
  145. { LSM330_ACCEL_DEV_NAME },
  146. { LSM303AGR_ACCEL_DEV_NAME },
  147. { LIS2DH12_ACCEL_DEV_NAME },
  148. { LIS3L02DQ_ACCEL_DEV_NAME },
  149. { LNG2DM_ACCEL_DEV_NAME },
  150. { H3LIS331DL_ACCEL_DEV_NAME },
  151. { LIS331DL_ACCEL_DEV_NAME },
  152. { LIS3LV02DL_ACCEL_DEV_NAME },
  153. { LIS2DW12_ACCEL_DEV_NAME },
  154. { LIS3DE_ACCEL_DEV_NAME },
  155. { LIS2DE12_ACCEL_DEV_NAME },
  156. { LIS2DS12_ACCEL_DEV_NAME },
  157. { LIS2HH12_ACCEL_DEV_NAME },
  158. { LIS302DL_ACCEL_DEV_NAME },
  159. { LSM303C_ACCEL_DEV_NAME },
  160. { SC7A20_ACCEL_DEV_NAME },
  161. { IIS328DQ_ACCEL_DEV_NAME },
  162. {},
  163. };
  164. MODULE_DEVICE_TABLE(i2c, st_accel_id_table);
  165. static int st_accel_i2c_probe(struct i2c_client *client)
  166. {
  167. const struct st_sensor_settings *settings;
  168. struct st_sensor_data *adata;
  169. struct iio_dev *indio_dev;
  170. int ret;
  171. st_sensors_dev_name_probe(&client->dev, client->name, sizeof(client->name));
  172. settings = st_accel_get_settings(client->name);
  173. if (!settings) {
  174. dev_err(&client->dev, "device name %s not recognized.\n",
  175. client->name);
  176. return -ENODEV;
  177. }
  178. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adata));
  179. if (!indio_dev)
  180. return -ENOMEM;
  181. adata = iio_priv(indio_dev);
  182. adata->sensor_settings = (struct st_sensor_settings *)settings;
  183. ret = st_sensors_i2c_configure(indio_dev, client);
  184. if (ret < 0)
  185. return ret;
  186. ret = st_sensors_power_enable(indio_dev);
  187. if (ret)
  188. return ret;
  189. return st_accel_common_probe(indio_dev);
  190. }
  191. static struct i2c_driver st_accel_driver = {
  192. .driver = {
  193. .name = "st-accel-i2c",
  194. .of_match_table = st_accel_of_match,
  195. .acpi_match_table = st_accel_acpi_match,
  196. },
  197. .probe = st_accel_i2c_probe,
  198. .id_table = st_accel_id_table,
  199. };
  200. module_i2c_driver(st_accel_driver);
  201. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  202. MODULE_DESCRIPTION("STMicroelectronics accelerometers i2c driver");
  203. MODULE_LICENSE("GPL v2");
  204. MODULE_IMPORT_NS(IIO_ST_SENSORS);