adgs1408.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/mux/driver.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 = (kernel_ulong_t)spi_get_device_match_data(spi);
  51. mux_chip = devm_mux_chip_alloc(dev, 1, 0);
  52. if (IS_ERR(mux_chip))
  53. return PTR_ERR(mux_chip);
  54. mux_chip->ops = &adgs1408_ops;
  55. ret = adgs1408_spi_reg_write(spi, ADGS1408_SW_DATA, ADGS1408_DISABLE);
  56. if (ret < 0)
  57. return ret;
  58. ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state);
  59. if (ret < 0)
  60. idle_state = MUX_IDLE_AS_IS;
  61. mux = mux_chip->mux;
  62. if (chip_id == ADGS1408)
  63. mux->states = 8;
  64. else
  65. mux->states = 4;
  66. switch (idle_state) {
  67. case MUX_IDLE_DISCONNECT:
  68. case MUX_IDLE_AS_IS:
  69. case 0 ... 7:
  70. /* adgs1409 supports only 4 states */
  71. if (idle_state < mux->states) {
  72. mux->idle_state = idle_state;
  73. break;
  74. }
  75. fallthrough;
  76. default:
  77. dev_err(dev, "invalid idle-state %d\n", idle_state);
  78. return -EINVAL;
  79. }
  80. return devm_mux_chip_register(dev, mux_chip);
  81. }
  82. static const struct spi_device_id adgs1408_spi_id[] = {
  83. { "adgs1408", ADGS1408 },
  84. { "adgs1409", ADGS1409 },
  85. { }
  86. };
  87. MODULE_DEVICE_TABLE(spi, adgs1408_spi_id);
  88. static const struct of_device_id adgs1408_of_match[] = {
  89. { .compatible = "adi,adgs1408", .data = (void *)ADGS1408, },
  90. { .compatible = "adi,adgs1409", .data = (void *)ADGS1409, },
  91. { }
  92. };
  93. MODULE_DEVICE_TABLE(of, adgs1408_of_match);
  94. static struct spi_driver adgs1408_driver = {
  95. .driver = {
  96. .name = "adgs1408",
  97. .of_match_table = adgs1408_of_match,
  98. },
  99. .probe = adgs1408_probe,
  100. .id_table = adgs1408_spi_id,
  101. };
  102. module_spi_driver(adgs1408_driver);
  103. MODULE_AUTHOR("Mircea Caprioru <mircea.caprioru@analog.com>");
  104. MODULE_DESCRIPTION("Analog Devices ADGS1408 MUX driver");
  105. MODULE_LICENSE("GPL");