st_accel_i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/acpi.h>
  14. #include <linux/i2c.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/common/st_sensors.h>
  17. #include <linux/iio/common/st_sensors_i2c.h>
  18. #include "st_accel.h"
  19. #ifdef CONFIG_OF
  20. static const struct of_device_id st_accel_of_match[] = {
  21. {
  22. /* An older compatible */
  23. .compatible = "st,lis3lv02d",
  24. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  25. },
  26. {
  27. .compatible = "st,lis3lv02dl-accel",
  28. .data = LIS3LV02DL_ACCEL_DEV_NAME,
  29. },
  30. {
  31. .compatible = "st,lsm303dlh-accel",
  32. .data = LSM303DLH_ACCEL_DEV_NAME,
  33. },
  34. {
  35. .compatible = "st,lsm303dlhc-accel",
  36. .data = LSM303DLHC_ACCEL_DEV_NAME,
  37. },
  38. {
  39. .compatible = "st,lis3dh-accel",
  40. .data = LIS3DH_ACCEL_DEV_NAME,
  41. },
  42. {
  43. .compatible = "st,lsm330d-accel",
  44. .data = LSM330D_ACCEL_DEV_NAME,
  45. },
  46. {
  47. .compatible = "st,lsm330dl-accel",
  48. .data = LSM330DL_ACCEL_DEV_NAME,
  49. },
  50. {
  51. .compatible = "st,lsm330dlc-accel",
  52. .data = LSM330DLC_ACCEL_DEV_NAME,
  53. },
  54. {
  55. .compatible = "st,lis331dl-accel",
  56. .data = LIS331DL_ACCEL_DEV_NAME,
  57. },
  58. {
  59. .compatible = "st,lis331dlh-accel",
  60. .data = LIS331DLH_ACCEL_DEV_NAME,
  61. },
  62. {
  63. .compatible = "st,lsm303dl-accel",
  64. .data = LSM303DL_ACCEL_DEV_NAME,
  65. },
  66. {
  67. .compatible = "st,lsm303dlm-accel",
  68. .data = LSM303DLM_ACCEL_DEV_NAME,
  69. },
  70. {
  71. .compatible = "st,lsm330-accel",
  72. .data = LSM330_ACCEL_DEV_NAME,
  73. },
  74. {
  75. .compatible = "st,lsm303agr-accel",
  76. .data = LSM303AGR_ACCEL_DEV_NAME,
  77. },
  78. {
  79. .compatible = "st,lis2dh12-accel",
  80. .data = LIS2DH12_ACCEL_DEV_NAME,
  81. },
  82. {
  83. .compatible = "st,h3lis331dl-accel",
  84. .data = H3LIS331DL_ACCEL_DEV_NAME,
  85. },
  86. {
  87. .compatible = "st,lis3l02dq",
  88. .data = LIS3L02DQ_ACCEL_DEV_NAME,
  89. },
  90. {
  91. .compatible = "st,lng2dm-accel",
  92. .data = LNG2DM_ACCEL_DEV_NAME,
  93. },
  94. {
  95. .compatible = "st,lis2dw12",
  96. .data = LIS2DW12_ACCEL_DEV_NAME,
  97. },
  98. {},
  99. };
  100. MODULE_DEVICE_TABLE(of, st_accel_of_match);
  101. #else
  102. #define st_accel_of_match NULL
  103. #endif
  104. #ifdef CONFIG_ACPI
  105. static const struct acpi_device_id st_accel_acpi_match[] = {
  106. {"SMO8A90", LNG2DM},
  107. { },
  108. };
  109. MODULE_DEVICE_TABLE(acpi, st_accel_acpi_match);
  110. #else
  111. #define st_accel_acpi_match NULL
  112. #endif
  113. static const struct i2c_device_id st_accel_id_table[] = {
  114. { LSM303DLH_ACCEL_DEV_NAME, LSM303DLH },
  115. { LSM303DLHC_ACCEL_DEV_NAME, LSM303DLHC },
  116. { LIS3DH_ACCEL_DEV_NAME, LIS3DH },
  117. { LSM330D_ACCEL_DEV_NAME, LSM330D },
  118. { LSM330DL_ACCEL_DEV_NAME, LSM330DL },
  119. { LSM330DLC_ACCEL_DEV_NAME, LSM330DLC },
  120. { LIS331DLH_ACCEL_DEV_NAME, LIS331DLH },
  121. { LSM303DL_ACCEL_DEV_NAME, LSM303DL },
  122. { LSM303DLM_ACCEL_DEV_NAME, LSM303DLM },
  123. { LSM330_ACCEL_DEV_NAME, LSM330 },
  124. { LSM303AGR_ACCEL_DEV_NAME, LSM303AGR },
  125. { LIS2DH12_ACCEL_DEV_NAME, LIS2DH12 },
  126. { LIS3L02DQ_ACCEL_DEV_NAME, LIS3L02DQ },
  127. { LNG2DM_ACCEL_DEV_NAME, LNG2DM },
  128. { H3LIS331DL_ACCEL_DEV_NAME, H3LIS331DL },
  129. { LIS331DL_ACCEL_DEV_NAME, LIS331DL },
  130. { LIS3LV02DL_ACCEL_DEV_NAME, LIS3LV02DL },
  131. { LIS2DW12_ACCEL_DEV_NAME, LIS2DW12 },
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(i2c, st_accel_id_table);
  135. static int st_accel_i2c_probe(struct i2c_client *client,
  136. const struct i2c_device_id *id)
  137. {
  138. struct iio_dev *indio_dev;
  139. struct st_sensor_data *adata;
  140. int ret;
  141. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adata));
  142. if (!indio_dev)
  143. return -ENOMEM;
  144. adata = iio_priv(indio_dev);
  145. if (client->dev.of_node) {
  146. st_sensors_of_name_probe(&client->dev, st_accel_of_match,
  147. client->name, sizeof(client->name));
  148. } else if (ACPI_HANDLE(&client->dev)) {
  149. ret = st_sensors_match_acpi_device(&client->dev);
  150. if ((ret < 0) || (ret >= ST_ACCEL_MAX))
  151. return -ENODEV;
  152. strlcpy(client->name, st_accel_id_table[ret].name,
  153. sizeof(client->name));
  154. } else if (!id)
  155. return -ENODEV;
  156. st_sensors_i2c_configure(indio_dev, client, adata);
  157. ret = st_accel_common_probe(indio_dev);
  158. if (ret < 0)
  159. return ret;
  160. return 0;
  161. }
  162. static int st_accel_i2c_remove(struct i2c_client *client)
  163. {
  164. st_accel_common_remove(i2c_get_clientdata(client));
  165. return 0;
  166. }
  167. static struct i2c_driver st_accel_driver = {
  168. .driver = {
  169. .name = "st-accel-i2c",
  170. .of_match_table = of_match_ptr(st_accel_of_match),
  171. .acpi_match_table = ACPI_PTR(st_accel_acpi_match),
  172. },
  173. .probe = st_accel_i2c_probe,
  174. .remove = st_accel_i2c_remove,
  175. .id_table = st_accel_id_table,
  176. };
  177. module_i2c_driver(st_accel_driver);
  178. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  179. MODULE_DESCRIPTION("STMicroelectronics accelerometers i2c driver");
  180. MODULE_LICENSE("GPL v2");