adg792a.c 3.7 KB

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