st_magn_i2c.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/i2c.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/common/st_sensors.h>
  16. #include <linux/iio/common/st_sensors_i2c.h>
  17. #include "st_magn.h"
  18. #ifdef CONFIG_OF
  19. static const struct of_device_id st_magn_of_match[] = {
  20. {
  21. .compatible = "st,lsm303dlh-magn",
  22. .data = LSM303DLH_MAGN_DEV_NAME,
  23. },
  24. {
  25. .compatible = "st,lsm303dlhc-magn",
  26. .data = LSM303DLHC_MAGN_DEV_NAME,
  27. },
  28. {
  29. .compatible = "st,lsm303dlm-magn",
  30. .data = LSM303DLM_MAGN_DEV_NAME,
  31. },
  32. {
  33. .compatible = "st,lis3mdl-magn",
  34. .data = LIS3MDL_MAGN_DEV_NAME,
  35. },
  36. {
  37. .compatible = "st,lsm303agr-magn",
  38. .data = LSM303AGR_MAGN_DEV_NAME,
  39. },
  40. {},
  41. };
  42. MODULE_DEVICE_TABLE(of, st_magn_of_match);
  43. #else
  44. #define st_magn_of_match NULL
  45. #endif
  46. static int st_magn_i2c_probe(struct i2c_client *client,
  47. const struct i2c_device_id *id)
  48. {
  49. struct iio_dev *indio_dev;
  50. struct st_sensor_data *mdata;
  51. int err;
  52. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mdata));
  53. if (!indio_dev)
  54. return -ENOMEM;
  55. mdata = iio_priv(indio_dev);
  56. st_sensors_of_i2c_probe(client, st_magn_of_match);
  57. st_sensors_i2c_configure(indio_dev, client, mdata);
  58. err = st_magn_common_probe(indio_dev);
  59. if (err < 0)
  60. return err;
  61. return 0;
  62. }
  63. static int st_magn_i2c_remove(struct i2c_client *client)
  64. {
  65. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  66. st_magn_common_remove(indio_dev);
  67. return 0;
  68. }
  69. static const struct i2c_device_id st_magn_id_table[] = {
  70. { LSM303DLH_MAGN_DEV_NAME },
  71. { LSM303DLHC_MAGN_DEV_NAME },
  72. { LSM303DLM_MAGN_DEV_NAME },
  73. { LIS3MDL_MAGN_DEV_NAME },
  74. { LSM303AGR_MAGN_DEV_NAME },
  75. {},
  76. };
  77. MODULE_DEVICE_TABLE(i2c, st_magn_id_table);
  78. static struct i2c_driver st_magn_driver = {
  79. .driver = {
  80. .name = "st-magn-i2c",
  81. .of_match_table = of_match_ptr(st_magn_of_match),
  82. },
  83. .probe = st_magn_i2c_probe,
  84. .remove = st_magn_i2c_remove,
  85. .id_table = st_magn_id_table,
  86. };
  87. module_i2c_driver(st_magn_driver);
  88. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  89. MODULE_DESCRIPTION("STMicroelectronics magnetometers i2c driver");
  90. MODULE_LICENSE("GPL v2");