adgs1408.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ADGS1408/ADGS1409 SPI MUX driver
  4. *
  5. * Copyright 2018 Analog Devices Inc.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/module.h>
  9. #include <linux/mux/driver.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/property.h>
  12. #include <linux/spi/spi.h>
  13. #define ADGS1408_SW_DATA (0x01)
  14. #define ADGS1408_REG_READ(reg) ((reg) | 0x80)
  15. #define ADGS1408_DISABLE (0x00)
  16. #define ADGS1408_MUX(state) (((state) << 1) | 1)
  17. enum adgs1408_chip_id {
  18. ADGS1408 = 1,
  19. ADGS1409,
  20. };
  21. static int adgs1408_spi_reg_write(struct spi_device *spi,
  22. u8 reg_addr, u8 reg_data)
  23. {
  24. u8 tx_buf[2];
  25. tx_buf[0] = reg_addr;
  26. tx_buf[1] = reg_data;
  27. return spi_write_then_read(spi, tx_buf, sizeof(tx_buf), NULL, 0);
  28. }
  29. static int adgs1408_set(struct mux_control *mux, int state)
  30. {
  31. struct spi_device *spi = to_spi_device(mux->chip->dev.parent);
  32. u8 reg;
  33. if (state == MUX_IDLE_DISCONNECT)
  34. reg = ADGS1408_DISABLE;
  35. else
  36. reg = ADGS1408_MUX(state);
  37. return adgs1408_spi_reg_write(spi, ADGS1408_SW_DATA, reg);
  38. }
  39. static const struct mux_control_ops adgs1408_ops = {
  40. .set = adgs1408_set,
  41. };
  42. static int adgs1408_probe(struct spi_device *spi)
  43. {
  44. struct device *dev = &spi->dev;
  45. enum adgs1408_chip_id chip_id;
  46. struct mux_chip *mux_chip;
  47. struct mux_control *mux;
  48. s32 idle_state;
  49. int ret;
  50. chip_id = (enum adgs1408_chip_id)of_device_get_match_data(dev);
  51. if (!chip_id)
  52. chip_id = spi_get_device_id(spi)->driver_data;
  53. mux_chip = devm_mux_chip_alloc(dev, 1, 0);
  54. if (IS_ERR(mux_chip))
  55. return PTR_ERR(mux_chip);
  56. mux_chip->ops = &adgs1408_ops;
  57. ret = adgs1408_spi_reg_write(spi, ADGS1408_SW_DATA, ADGS1408_DISABLE);
  58. if (ret < 0)
  59. return ret;
  60. ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state);
  61. if (ret < 0)
  62. idle_state = MUX_IDLE_AS_IS;
  63. mux = mux_chip->mux;
  64. if (chip_id == ADGS1408)
  65. mux->states = 8;
  66. else
  67. mux->states = 4;
  68. switch (idle_state) {
  69. case MUX_IDLE_DISCONNECT:
  70. case MUX_IDLE_AS_IS:
  71. case 0 ... 7:
  72. /* adgs1409 supports only 4 states */
  73. if (idle_state < mux->states) {
  74. mux->idle_state = idle_state;
  75. break;
  76. }
  77. /* fall through */
  78. default:
  79. dev_err(dev, "invalid idle-state %d\n", idle_state);
  80. return -EINVAL;
  81. }
  82. return devm_mux_chip_register(dev, mux_chip);
  83. }
  84. static const struct spi_device_id adgs1408_spi_id[] = {
  85. { "adgs1408", ADGS1408 },
  86. { "adgs1409", ADGS1409 },
  87. { }
  88. };
  89. MODULE_DEVICE_TABLE(spi, adgs1408_spi_id);
  90. static const struct of_device_id adgs1408_of_match[] = {
  91. { .compatible = "adi,adgs1408", .data = (void *)ADGS1408, },
  92. { .compatible = "adi,adgs1409", .data = (void *)ADGS1409, },
  93. { }
  94. };
  95. MODULE_DEVICE_TABLE(of, adgs1408_of_match);
  96. static struct spi_driver adgs1408_driver = {
  97. .driver = {
  98. .name = "adgs1408",
  99. .of_match_table = of_match_ptr(adgs1408_of_match),
  100. },
  101. .probe = adgs1408_probe,
  102. .id_table = adgs1408_spi_id,
  103. };
  104. module_spi_driver(adgs1408_driver);
  105. MODULE_AUTHOR("Mircea Caprioru <mircea.caprioru@analog.com>");
  106. MODULE_DESCRIPTION("Analog Devices ADGS1408 MUX driver");
  107. MODULE_LICENSE("GPL");