st_gyro_i2c.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics gyroscopes 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.h>
  15. #include <linux/iio/common/st_sensors_i2c.h>
  16. #include "st_gyro.h"
  17. static const struct of_device_id st_gyro_of_match[] = {
  18. {
  19. .compatible = "st,l3g4200d-gyro",
  20. .data = L3G4200D_GYRO_DEV_NAME,
  21. },
  22. {
  23. .compatible = "st,lsm330d-gyro",
  24. .data = LSM330D_GYRO_DEV_NAME,
  25. },
  26. {
  27. .compatible = "st,lsm330dl-gyro",
  28. .data = LSM330DL_GYRO_DEV_NAME,
  29. },
  30. {
  31. .compatible = "st,lsm330dlc-gyro",
  32. .data = LSM330DLC_GYRO_DEV_NAME,
  33. },
  34. {
  35. .compatible = "st,l3gd20-gyro",
  36. .data = L3GD20_GYRO_DEV_NAME,
  37. },
  38. {
  39. .compatible = "st,l3gd20h-gyro",
  40. .data = L3GD20H_GYRO_DEV_NAME,
  41. },
  42. {
  43. .compatible = "st,l3g4is-gyro",
  44. .data = L3G4IS_GYRO_DEV_NAME,
  45. },
  46. {
  47. .compatible = "st,lsm330-gyro",
  48. .data = LSM330_GYRO_DEV_NAME,
  49. },
  50. {
  51. .compatible = "st,lsm9ds0-gyro",
  52. .data = LSM9DS0_GYRO_DEV_NAME,
  53. },
  54. {},
  55. };
  56. MODULE_DEVICE_TABLE(of, st_gyro_of_match);
  57. static int st_gyro_i2c_probe(struct i2c_client *client)
  58. {
  59. const struct st_sensor_settings *settings;
  60. struct st_sensor_data *gdata;
  61. struct iio_dev *indio_dev;
  62. int err;
  63. st_sensors_dev_name_probe(&client->dev, client->name, sizeof(client->name));
  64. settings = st_gyro_get_settings(client->name);
  65. if (!settings) {
  66. dev_err(&client->dev, "device name %s not recognized.\n",
  67. client->name);
  68. return -ENODEV;
  69. }
  70. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*gdata));
  71. if (!indio_dev)
  72. return -ENOMEM;
  73. gdata = iio_priv(indio_dev);
  74. gdata->sensor_settings = (struct st_sensor_settings *)settings;
  75. err = st_sensors_i2c_configure(indio_dev, client);
  76. if (err < 0)
  77. return err;
  78. err = st_sensors_power_enable(indio_dev);
  79. if (err)
  80. return err;
  81. return st_gyro_common_probe(indio_dev);
  82. }
  83. static const struct i2c_device_id st_gyro_id_table[] = {
  84. { L3G4200D_GYRO_DEV_NAME },
  85. { LSM330D_GYRO_DEV_NAME },
  86. { LSM330DL_GYRO_DEV_NAME },
  87. { LSM330DLC_GYRO_DEV_NAME },
  88. { L3GD20_GYRO_DEV_NAME },
  89. { L3GD20H_GYRO_DEV_NAME },
  90. { L3G4IS_GYRO_DEV_NAME },
  91. { LSM330_GYRO_DEV_NAME },
  92. { LSM9DS0_GYRO_DEV_NAME },
  93. {},
  94. };
  95. MODULE_DEVICE_TABLE(i2c, st_gyro_id_table);
  96. static struct i2c_driver st_gyro_driver = {
  97. .driver = {
  98. .name = "st-gyro-i2c",
  99. .of_match_table = st_gyro_of_match,
  100. },
  101. .probe = st_gyro_i2c_probe,
  102. .id_table = st_gyro_id_table,
  103. };
  104. module_i2c_driver(st_gyro_driver);
  105. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  106. MODULE_DESCRIPTION("STMicroelectronics gyroscopes i2c driver");
  107. MODULE_LICENSE("GPL v2");
  108. MODULE_IMPORT_NS(IIO_ST_SENSORS);