st_gyro_i2c.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics gyroscopes driver
  4. *
  5. * Copyright 2012-2013 STMicroelectronics Inc.
  6. *
  7. * Denis Ciocca <[email protected]>
  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. const struct i2c_device_id *id)
  59. {
  60. const struct st_sensor_settings *settings;
  61. struct st_sensor_data *gdata;
  62. struct iio_dev *indio_dev;
  63. int err;
  64. st_sensors_dev_name_probe(&client->dev, client->name, sizeof(client->name));
  65. settings = st_gyro_get_settings(client->name);
  66. if (!settings) {
  67. dev_err(&client->dev, "device name %s not recognized.\n",
  68. client->name);
  69. return -ENODEV;
  70. }
  71. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*gdata));
  72. if (!indio_dev)
  73. return -ENOMEM;
  74. gdata = iio_priv(indio_dev);
  75. gdata->sensor_settings = (struct st_sensor_settings *)settings;
  76. err = st_sensors_i2c_configure(indio_dev, client);
  77. if (err < 0)
  78. return err;
  79. err = st_sensors_power_enable(indio_dev);
  80. if (err)
  81. return err;
  82. return st_gyro_common_probe(indio_dev);
  83. }
  84. static const struct i2c_device_id st_gyro_id_table[] = {
  85. { L3G4200D_GYRO_DEV_NAME },
  86. { LSM330D_GYRO_DEV_NAME },
  87. { LSM330DL_GYRO_DEV_NAME },
  88. { LSM330DLC_GYRO_DEV_NAME },
  89. { L3GD20_GYRO_DEV_NAME },
  90. { L3GD20H_GYRO_DEV_NAME },
  91. { L3G4IS_GYRO_DEV_NAME },
  92. { LSM330_GYRO_DEV_NAME },
  93. { LSM9DS0_GYRO_DEV_NAME },
  94. {},
  95. };
  96. MODULE_DEVICE_TABLE(i2c, st_gyro_id_table);
  97. static struct i2c_driver st_gyro_driver = {
  98. .driver = {
  99. .name = "st-gyro-i2c",
  100. .of_match_table = st_gyro_of_match,
  101. },
  102. .probe = st_gyro_i2c_probe,
  103. .id_table = st_gyro_id_table,
  104. };
  105. module_i2c_driver(st_gyro_driver);
  106. MODULE_AUTHOR("Denis Ciocca <[email protected]>");
  107. MODULE_DESCRIPTION("STMicroelectronics gyroscopes i2c driver");
  108. MODULE_LICENSE("GPL v2");
  109. MODULE_IMPORT_NS(IIO_ST_SENSORS);