ad7091r5.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AD7091R5 Analog to Digital converter driver
  4. *
  5. * Copyright 2014-2019 Analog Devices Inc.
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/iio/iio.h>
  9. #include <linux/module.h>
  10. #include <linux/regmap.h>
  11. #include "ad7091r-base.h"
  12. static const struct iio_event_spec ad7091r5_events[] = {
  13. {
  14. .type = IIO_EV_TYPE_THRESH,
  15. .dir = IIO_EV_DIR_RISING,
  16. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  17. BIT(IIO_EV_INFO_ENABLE),
  18. },
  19. {
  20. .type = IIO_EV_TYPE_THRESH,
  21. .dir = IIO_EV_DIR_FALLING,
  22. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  23. BIT(IIO_EV_INFO_ENABLE),
  24. },
  25. {
  26. .type = IIO_EV_TYPE_THRESH,
  27. .dir = IIO_EV_DIR_EITHER,
  28. .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
  29. },
  30. };
  31. #define AD7091R_CHANNEL(idx, bits, ev, num_ev) { \
  32. .type = IIO_VOLTAGE, \
  33. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  34. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  35. .indexed = 1, \
  36. .channel = idx, \
  37. .event_spec = ev, \
  38. .num_event_specs = num_ev, \
  39. .scan_type.storagebits = 16, \
  40. .scan_type.realbits = bits, \
  41. }
  42. static const struct iio_chan_spec ad7091r5_channels_irq[] = {
  43. AD7091R_CHANNEL(0, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
  44. AD7091R_CHANNEL(1, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
  45. AD7091R_CHANNEL(2, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
  46. AD7091R_CHANNEL(3, 12, ad7091r5_events, ARRAY_SIZE(ad7091r5_events)),
  47. };
  48. static const struct iio_chan_spec ad7091r5_channels_noirq[] = {
  49. AD7091R_CHANNEL(0, 12, NULL, 0),
  50. AD7091R_CHANNEL(1, 12, NULL, 0),
  51. AD7091R_CHANNEL(2, 12, NULL, 0),
  52. AD7091R_CHANNEL(3, 12, NULL, 0),
  53. };
  54. static const struct ad7091r_chip_info ad7091r5_chip_info_irq = {
  55. .channels = ad7091r5_channels_irq,
  56. .num_channels = ARRAY_SIZE(ad7091r5_channels_irq),
  57. .vref_mV = 2500,
  58. };
  59. static const struct ad7091r_chip_info ad7091r5_chip_info_noirq = {
  60. .channels = ad7091r5_channels_noirq,
  61. .num_channels = ARRAY_SIZE(ad7091r5_channels_noirq),
  62. .vref_mV = 2500,
  63. };
  64. static int ad7091r5_i2c_probe(struct i2c_client *i2c,
  65. const struct i2c_device_id *id)
  66. {
  67. const struct ad7091r_chip_info *chip_info;
  68. struct regmap *map = devm_regmap_init_i2c(i2c, &ad7091r_regmap_config);
  69. if (IS_ERR(map))
  70. return PTR_ERR(map);
  71. if (i2c->irq)
  72. chip_info = &ad7091r5_chip_info_irq;
  73. else
  74. chip_info = &ad7091r5_chip_info_noirq;
  75. return ad7091r_probe(&i2c->dev, id->name, chip_info, map, i2c->irq);
  76. }
  77. static const struct of_device_id ad7091r5_dt_ids[] = {
  78. { .compatible = "adi,ad7091r5" },
  79. {},
  80. };
  81. MODULE_DEVICE_TABLE(of, ad7091r5_dt_ids);
  82. static const struct i2c_device_id ad7091r5_i2c_ids[] = {
  83. {"ad7091r5", 0},
  84. {}
  85. };
  86. MODULE_DEVICE_TABLE(i2c, ad7091r5_i2c_ids);
  87. static struct i2c_driver ad7091r5_driver = {
  88. .driver = {
  89. .name = "ad7091r5",
  90. .of_match_table = ad7091r5_dt_ids,
  91. },
  92. .probe = ad7091r5_i2c_probe,
  93. .id_table = ad7091r5_i2c_ids,
  94. };
  95. module_i2c_driver(ad7091r5_driver);
  96. MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
  97. MODULE_DESCRIPTION("Analog Devices AD7091R5 multi-channel ADC driver");
  98. MODULE_LICENSE("GPL v2");