st_magn_spi.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics magnetometers 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/spi/spi.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/common/st_sensors.h>
  15. #include <linux/iio/common/st_sensors_spi.h>
  16. #include "st_magn.h"
  17. /*
  18. * For new single-chip sensors use <device_name> as compatible string.
  19. * For old single-chip devices keep <device_name>-magn to maintain
  20. * compatibility
  21. * For multi-chip devices, use <device_name>-magn to distinguish which
  22. * capability is being used
  23. */
  24. static const struct of_device_id st_magn_of_match[] = {
  25. {
  26. .compatible = "st,lis3mdl-magn",
  27. .data = LIS3MDL_MAGN_DEV_NAME,
  28. },
  29. {
  30. .compatible = "st,lsm303agr-magn",
  31. .data = LSM303AGR_MAGN_DEV_NAME,
  32. },
  33. {
  34. .compatible = "st,lis2mdl",
  35. .data = LIS2MDL_MAGN_DEV_NAME,
  36. },
  37. {
  38. .compatible = "st,lsm9ds1-magn",
  39. .data = LSM9DS1_MAGN_DEV_NAME,
  40. },
  41. {
  42. .compatible = "st,iis2mdc",
  43. .data = IIS2MDC_MAGN_DEV_NAME,
  44. },
  45. {}
  46. };
  47. MODULE_DEVICE_TABLE(of, st_magn_of_match);
  48. static int st_magn_spi_probe(struct spi_device *spi)
  49. {
  50. const struct st_sensor_settings *settings;
  51. struct st_sensor_data *mdata;
  52. struct iio_dev *indio_dev;
  53. int err;
  54. st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
  55. settings = st_magn_get_settings(spi->modalias);
  56. if (!settings) {
  57. dev_err(&spi->dev, "device name %s not recognized.\n",
  58. spi->modalias);
  59. return -ENODEV;
  60. }
  61. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
  62. if (!indio_dev)
  63. return -ENOMEM;
  64. mdata = iio_priv(indio_dev);
  65. mdata->sensor_settings = (struct st_sensor_settings *)settings;
  66. err = st_sensors_spi_configure(indio_dev, spi);
  67. if (err < 0)
  68. return err;
  69. err = st_sensors_power_enable(indio_dev);
  70. if (err)
  71. return err;
  72. return st_magn_common_probe(indio_dev);
  73. }
  74. static const struct spi_device_id st_magn_id_table[] = {
  75. { LIS3MDL_MAGN_DEV_NAME },
  76. { LSM303AGR_MAGN_DEV_NAME },
  77. { LIS2MDL_MAGN_DEV_NAME },
  78. { LSM9DS1_MAGN_DEV_NAME },
  79. { IIS2MDC_MAGN_DEV_NAME },
  80. {},
  81. };
  82. MODULE_DEVICE_TABLE(spi, st_magn_id_table);
  83. static struct spi_driver st_magn_driver = {
  84. .driver = {
  85. .name = "st-magn-spi",
  86. .of_match_table = st_magn_of_match,
  87. },
  88. .probe = st_magn_spi_probe,
  89. .id_table = st_magn_id_table,
  90. };
  91. module_spi_driver(st_magn_driver);
  92. MODULE_AUTHOR("Denis Ciocca <[email protected]>");
  93. MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
  94. MODULE_LICENSE("GPL v2");
  95. MODULE_IMPORT_NS(IIO_ST_SENSORS);