st_magn_spi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics magnetometers 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/slab.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. };
  43. MODULE_DEVICE_TABLE(of, st_magn_of_match);
  44. static int st_magn_spi_probe(struct spi_device *spi)
  45. {
  46. const struct st_sensor_settings *settings;
  47. struct st_sensor_data *mdata;
  48. struct iio_dev *indio_dev;
  49. int err;
  50. st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
  51. settings = st_magn_get_settings(spi->modalias);
  52. if (!settings) {
  53. dev_err(&spi->dev, "device name %s not recognized.\n",
  54. spi->modalias);
  55. return -ENODEV;
  56. }
  57. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
  58. if (!indio_dev)
  59. return -ENOMEM;
  60. mdata = iio_priv(indio_dev);
  61. mdata->sensor_settings = (struct st_sensor_settings *)settings;
  62. err = st_sensors_spi_configure(indio_dev, spi);
  63. if (err < 0)
  64. return err;
  65. err = st_sensors_power_enable(indio_dev);
  66. if (err)
  67. return err;
  68. err = st_magn_common_probe(indio_dev);
  69. if (err < 0)
  70. goto st_magn_power_off;
  71. return 0;
  72. st_magn_power_off:
  73. st_sensors_power_disable(indio_dev);
  74. return err;
  75. }
  76. static int st_magn_spi_remove(struct spi_device *spi)
  77. {
  78. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  79. st_magn_common_remove(indio_dev);
  80. st_sensors_power_disable(indio_dev);
  81. return 0;
  82. }
  83. static const struct spi_device_id st_magn_id_table[] = {
  84. { LIS3MDL_MAGN_DEV_NAME },
  85. { LSM303AGR_MAGN_DEV_NAME },
  86. { LIS2MDL_MAGN_DEV_NAME },
  87. { LSM9DS1_MAGN_DEV_NAME },
  88. {},
  89. };
  90. MODULE_DEVICE_TABLE(spi, st_magn_id_table);
  91. static struct spi_driver st_magn_driver = {
  92. .driver = {
  93. .name = "st-magn-spi",
  94. .of_match_table = st_magn_of_match,
  95. },
  96. .probe = st_magn_spi_probe,
  97. .remove = st_magn_spi_remove,
  98. .id_table = st_magn_id_table,
  99. };
  100. module_spi_driver(st_magn_driver);
  101. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  102. MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
  103. MODULE_LICENSE("GPL v2");