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