adg792a.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Multiplexer driver for Analog Devices ADG792A/G Triple 4:1 mux
  4. *
  5. * Copyright (C) 2017 Axentia Technologies AB
  6. *
  7. * Author: Peter Rosin <peda@axentia.se>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/mux/driver.h>
  13. #include <linux/property.h>
  14. #define ADG792A_LDSW BIT(0)
  15. #define ADG792A_RESETB BIT(1)
  16. #define ADG792A_DISABLE(mux) (0x50 | (mux))
  17. #define ADG792A_DISABLE_ALL (0x5f)
  18. #define ADG792A_MUX(mux, state) (0xc0 | (((mux) + 1) << 2) | (state))
  19. #define ADG792A_MUX_ALL(state) (0xc0 | (state))
  20. static int adg792a_write_cmd(struct i2c_client *i2c, u8 cmd, int reset)
  21. {
  22. u8 data = ADG792A_RESETB | ADG792A_LDSW;
  23. /* ADG792A_RESETB is active low, the chip resets when it is zero. */
  24. if (reset)
  25. data &= ~ADG792A_RESETB;
  26. return i2c_smbus_write_byte_data(i2c, cmd, data);
  27. }
  28. static int adg792a_set(struct mux_control *mux, int state)
  29. {
  30. struct i2c_client *i2c = to_i2c_client(mux->chip->dev.parent);
  31. u8 cmd;
  32. if (mux->chip->controllers == 1) {
  33. /* parallel mux controller operation */
  34. if (state == MUX_IDLE_DISCONNECT)
  35. cmd = ADG792A_DISABLE_ALL;
  36. else
  37. cmd = ADG792A_MUX_ALL(state);
  38. } else {
  39. unsigned int controller = mux_control_get_index(mux);
  40. if (state == MUX_IDLE_DISCONNECT)
  41. cmd = ADG792A_DISABLE(controller);
  42. else
  43. cmd = ADG792A_MUX(controller, state);
  44. }
  45. return adg792a_write_cmd(i2c, cmd, 0);
  46. }
  47. static const struct mux_control_ops adg792a_ops = {
  48. .set = adg792a_set,
  49. };
  50. static int adg792a_probe(struct i2c_client *i2c)
  51. {
  52. struct device *dev = &i2c->dev;
  53. struct mux_chip *mux_chip;
  54. s32 idle_state[3];
  55. u32 cells;
  56. int ret;
  57. int i;
  58. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  59. return -ENODEV;
  60. ret = device_property_read_u32(dev, "#mux-control-cells", &cells);
  61. if (ret < 0)
  62. return ret;
  63. if (cells >= 2)
  64. return -EINVAL;
  65. mux_chip = devm_mux_chip_alloc(dev, cells ? 3 : 1, 0);
  66. if (IS_ERR(mux_chip))
  67. return PTR_ERR(mux_chip);
  68. mux_chip->ops = &adg792a_ops;
  69. ret = adg792a_write_cmd(i2c, ADG792A_DISABLE_ALL, 1);
  70. if (ret < 0)
  71. return ret;
  72. ret = device_property_read_u32_array(dev, "idle-state",
  73. (u32 *)idle_state,
  74. mux_chip->controllers);
  75. if (ret < 0) {
  76. idle_state[0] = MUX_IDLE_AS_IS;
  77. idle_state[1] = MUX_IDLE_AS_IS;
  78. idle_state[2] = MUX_IDLE_AS_IS;
  79. }
  80. for (i = 0; i < mux_chip->controllers; ++i) {
  81. struct mux_control *mux = &mux_chip->mux[i];
  82. mux->states = 4;
  83. switch (idle_state[i]) {
  84. case MUX_IDLE_DISCONNECT:
  85. case MUX_IDLE_AS_IS:
  86. case 0 ... 4:
  87. mux->idle_state = idle_state[i];
  88. break;
  89. default:
  90. dev_err(dev, "invalid idle-state %d\n", idle_state[i]);
  91. return -EINVAL;
  92. }
  93. }
  94. ret = devm_mux_chip_register(dev, mux_chip);
  95. if (ret < 0)
  96. return ret;
  97. if (cells)
  98. dev_info(dev, "3x single pole quadruple throw muxes registered\n");
  99. else
  100. dev_info(dev, "triple pole quadruple throw mux registered\n");
  101. return 0;
  102. }
  103. static const struct i2c_device_id adg792a_id[] = {
  104. { .name = "adg792a", },
  105. { .name = "adg792g", },
  106. { }
  107. };
  108. MODULE_DEVICE_TABLE(i2c, adg792a_id);
  109. static const struct of_device_id adg792a_of_match[] = {
  110. { .compatible = "adi,adg792a", },
  111. { .compatible = "adi,adg792g", },
  112. { }
  113. };
  114. MODULE_DEVICE_TABLE(of, adg792a_of_match);
  115. static struct i2c_driver adg792a_driver = {
  116. .driver = {
  117. .name = "adg792a",
  118. .of_match_table = of_match_ptr(adg792a_of_match),
  119. },
  120. .probe_new = adg792a_probe,
  121. .id_table = adg792a_id,
  122. };
  123. module_i2c_driver(adg792a_driver);
  124. MODULE_DESCRIPTION("Analog Devices ADG792A/G Triple 4:1 mux driver");
  125. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  126. MODULE_LICENSE("GPL v2");