st_magn_i2c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. .compatible = "st,lis2mdl",
  42. .data = LIS2MDL_MAGN_DEV_NAME,
  43. },
  44. {},
  45. };
  46. MODULE_DEVICE_TABLE(of, st_magn_of_match);
  47. #else
  48. #define st_magn_of_match NULL
  49. #endif
  50. static int st_magn_i2c_probe(struct i2c_client *client,
  51. const struct i2c_device_id *id)
  52. {
  53. struct iio_dev *indio_dev;
  54. struct st_sensor_data *mdata;
  55. int err;
  56. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mdata));
  57. if (!indio_dev)
  58. return -ENOMEM;
  59. mdata = iio_priv(indio_dev);
  60. st_sensors_of_name_probe(&client->dev, st_magn_of_match,
  61. client->name, sizeof(client->name));
  62. st_sensors_i2c_configure(indio_dev, client, mdata);
  63. err = st_magn_common_probe(indio_dev);
  64. if (err < 0)
  65. return err;
  66. return 0;
  67. }
  68. static int st_magn_i2c_remove(struct i2c_client *client)
  69. {
  70. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  71. st_magn_common_remove(indio_dev);
  72. return 0;
  73. }
  74. static const struct i2c_device_id st_magn_id_table[] = {
  75. { LSM303DLH_MAGN_DEV_NAME },
  76. { LSM303DLHC_MAGN_DEV_NAME },
  77. { LSM303DLM_MAGN_DEV_NAME },
  78. { LIS3MDL_MAGN_DEV_NAME },
  79. { LSM303AGR_MAGN_DEV_NAME },
  80. { LIS2MDL_MAGN_DEV_NAME },
  81. {},
  82. };
  83. MODULE_DEVICE_TABLE(i2c, st_magn_id_table);
  84. static struct i2c_driver st_magn_driver = {
  85. .driver = {
  86. .name = "st-magn-i2c",
  87. .of_match_table = of_match_ptr(st_magn_of_match),
  88. },
  89. .probe = st_magn_i2c_probe,
  90. .remove = st_magn_i2c_remove,
  91. .id_table = st_magn_id_table,
  92. };
  93. module_i2c_driver(st_magn_driver);
  94. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  95. MODULE_DESCRIPTION("STMicroelectronics magnetometers i2c driver");
  96. MODULE_LICENSE("GPL v2");