st_magn_spi.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * STMicroelectronics magnetometers 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/spi/spi.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/common/st_sensors.h>
  16. #include <linux/iio/common/st_sensors_spi.h>
  17. #include "st_magn.h"
  18. #ifdef CONFIG_OF
  19. /*
  20. * For new single-chip sensors use <device_name> as compatible string.
  21. * For old single-chip devices keep <device_name>-magn to maintain
  22. * compatibility
  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. };
  39. MODULE_DEVICE_TABLE(of, st_magn_of_match);
  40. #else
  41. #define st_magn_of_match NULL
  42. #endif
  43. static int st_magn_spi_probe(struct spi_device *spi)
  44. {
  45. struct iio_dev *indio_dev;
  46. struct st_sensor_data *mdata;
  47. int err;
  48. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
  49. if (!indio_dev)
  50. return -ENOMEM;
  51. mdata = iio_priv(indio_dev);
  52. st_sensors_of_name_probe(&spi->dev, st_magn_of_match,
  53. spi->modalias, sizeof(spi->modalias));
  54. st_sensors_spi_configure(indio_dev, spi, mdata);
  55. err = st_magn_common_probe(indio_dev);
  56. if (err < 0)
  57. return err;
  58. return 0;
  59. }
  60. static int st_magn_spi_remove(struct spi_device *spi)
  61. {
  62. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  63. st_magn_common_remove(indio_dev);
  64. return 0;
  65. }
  66. static const struct spi_device_id st_magn_id_table[] = {
  67. { LIS3MDL_MAGN_DEV_NAME },
  68. { LSM303AGR_MAGN_DEV_NAME },
  69. { LIS2MDL_MAGN_DEV_NAME },
  70. {},
  71. };
  72. MODULE_DEVICE_TABLE(spi, st_magn_id_table);
  73. static struct spi_driver st_magn_driver = {
  74. .driver = {
  75. .name = "st-magn-spi",
  76. .of_match_table = of_match_ptr(st_magn_of_match),
  77. },
  78. .probe = st_magn_spi_probe,
  79. .remove = st_magn_spi_remove,
  80. .id_table = st_magn_id_table,
  81. };
  82. module_spi_driver(st_magn_driver);
  83. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  84. MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
  85. MODULE_LICENSE("GPL v2");