ad7091r5.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_chan_spec ad7091r5_channels_irq[] = {
  13. AD7091R_CHANNEL(0, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
  14. AD7091R_CHANNEL(1, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
  15. AD7091R_CHANNEL(2, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
  16. AD7091R_CHANNEL(3, 12, ad7091r_events, ARRAY_SIZE(ad7091r_events)),
  17. };
  18. static const struct iio_chan_spec ad7091r5_channels_noirq[] = {
  19. AD7091R_CHANNEL(0, 12, NULL, 0),
  20. AD7091R_CHANNEL(1, 12, NULL, 0),
  21. AD7091R_CHANNEL(2, 12, NULL, 0),
  22. AD7091R_CHANNEL(3, 12, NULL, 0),
  23. };
  24. static int ad7091r5_set_mode(struct ad7091r_state *st, enum ad7091r_mode mode)
  25. {
  26. int ret, conf;
  27. switch (mode) {
  28. case AD7091R_MODE_SAMPLE:
  29. conf = 0;
  30. break;
  31. case AD7091R_MODE_COMMAND:
  32. conf = AD7091R_REG_CONF_CMD;
  33. break;
  34. case AD7091R_MODE_AUTOCYCLE:
  35. conf = AD7091R_REG_CONF_AUTO;
  36. break;
  37. default:
  38. return -EINVAL;
  39. }
  40. ret = regmap_update_bits(st->map, AD7091R_REG_CONF,
  41. AD7091R_REG_CONF_MODE_MASK, conf);
  42. if (ret)
  43. return ret;
  44. st->mode = mode;
  45. return 0;
  46. }
  47. static unsigned int ad7091r5_reg_result_chan_id(unsigned int val)
  48. {
  49. return AD7091R5_REG_RESULT_CH_ID(val);
  50. }
  51. static const struct ad7091r_chip_info ad7091r5_chip_info_irq = {
  52. .name = "ad7091r-5",
  53. .channels = ad7091r5_channels_irq,
  54. .num_channels = ARRAY_SIZE(ad7091r5_channels_irq),
  55. .vref_mV = 2500,
  56. .reg_result_chan_id = &ad7091r5_reg_result_chan_id,
  57. .set_mode = &ad7091r5_set_mode,
  58. };
  59. static const struct ad7091r_chip_info ad7091r5_chip_info_noirq = {
  60. .name = "ad7091r-5",
  61. .channels = ad7091r5_channels_noirq,
  62. .num_channels = ARRAY_SIZE(ad7091r5_channels_noirq),
  63. .vref_mV = 2500,
  64. .reg_result_chan_id = &ad7091r5_reg_result_chan_id,
  65. .set_mode = &ad7091r5_set_mode,
  66. };
  67. static const struct regmap_config ad7091r_regmap_config = {
  68. .reg_bits = 8,
  69. .val_bits = 16,
  70. .writeable_reg = ad7091r_writeable_reg,
  71. .volatile_reg = ad7091r_volatile_reg,
  72. };
  73. static void ad7091r5_regmap_init(struct ad7091r_state *st,
  74. const struct regmap_config *regmap_conf)
  75. {
  76. struct i2c_client *i2c = container_of(st->dev, struct i2c_client, dev);
  77. st->map = devm_regmap_init_i2c(i2c, regmap_conf);
  78. }
  79. static struct ad7091r_init_info ad7091r5_init_info = {
  80. .info_irq = &ad7091r5_chip_info_irq,
  81. .info_no_irq = &ad7091r5_chip_info_noirq,
  82. .regmap_config = &ad7091r_regmap_config,
  83. .init_adc_regmap = &ad7091r5_regmap_init
  84. };
  85. static int ad7091r5_i2c_probe(struct i2c_client *i2c)
  86. {
  87. const struct ad7091r_init_info *init_info;
  88. init_info = i2c_get_match_data(i2c);
  89. if (!init_info)
  90. return -EINVAL;
  91. return ad7091r_probe(&i2c->dev, init_info, i2c->irq);
  92. }
  93. static const struct of_device_id ad7091r5_dt_ids[] = {
  94. { .compatible = "adi,ad7091r5", .data = &ad7091r5_init_info },
  95. { }
  96. };
  97. MODULE_DEVICE_TABLE(of, ad7091r5_dt_ids);
  98. static const struct i2c_device_id ad7091r5_i2c_ids[] = {
  99. { "ad7091r5", (kernel_ulong_t)&ad7091r5_init_info },
  100. { }
  101. };
  102. MODULE_DEVICE_TABLE(i2c, ad7091r5_i2c_ids);
  103. static struct i2c_driver ad7091r5_driver = {
  104. .driver = {
  105. .name = "ad7091r5",
  106. .of_match_table = ad7091r5_dt_ids,
  107. },
  108. .probe = ad7091r5_i2c_probe,
  109. .id_table = ad7091r5_i2c_ids,
  110. };
  111. module_i2c_driver(ad7091r5_driver);
  112. MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
  113. MODULE_DESCRIPTION("Analog Devices AD7091R5 multi-channel ADC driver");
  114. MODULE_LICENSE("GPL v2");
  115. MODULE_IMPORT_NS(IIO_AD7091R);