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