1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADGS1408/ADGS1409 SPI MUX driver
5 * Copyright 2018 Analog Devices Inc.
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>
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)
20 enum adgs1408_chip_id
{
25 static int adgs1408_spi_reg_write(struct spi_device
*spi
,
26 u8 reg_addr
, u8 reg_data
)
33 return spi_write_then_read(spi
, tx_buf
, sizeof(tx_buf
), NULL
, 0);
36 static int adgs1408_set(struct mux_control
*mux
, int state
)
38 struct spi_device
*spi
= to_spi_device(mux
->chip
->dev
.parent
);
41 if (state
== MUX_IDLE_DISCONNECT
)
42 reg
= ADGS1408_DISABLE
;
44 reg
= ADGS1408_MUX(state
);
46 return adgs1408_spi_reg_write(spi
, ADGS1408_SW_DATA
, reg
);
49 static const struct mux_control_ops adgs1408_ops
= {
53 static int adgs1408_probe(struct spi_device
*spi
)
55 struct device
*dev
= &spi
->dev
;
56 enum adgs1408_chip_id chip_id
;
57 struct mux_chip
*mux_chip
;
58 struct mux_control
*mux
;
62 chip_id
= (enum adgs1408_chip_id
)device_get_match_data(dev
);
64 chip_id
= spi_get_device_id(spi
)->driver_data
;
66 mux_chip
= devm_mux_chip_alloc(dev
, 1, 0);
68 return PTR_ERR(mux_chip
);
70 mux_chip
->ops
= &adgs1408_ops
;
72 ret
= adgs1408_spi_reg_write(spi
, ADGS1408_SW_DATA
, ADGS1408_DISABLE
);
76 ret
= device_property_read_u32(dev
, "idle-state", (u32
*)&idle_state
);
78 idle_state
= MUX_IDLE_AS_IS
;
82 if (chip_id
== ADGS1408
)
88 case MUX_IDLE_DISCONNECT
:
91 /* adgs1409 supports only 4 states */
92 if (idle_state
< mux
->states
) {
93 mux
->idle_state
= idle_state
;
98 dev_err(dev
, "invalid idle-state %d\n", idle_state
);
102 return devm_mux_chip_register(dev
, mux_chip
);
105 static const struct spi_device_id adgs1408_spi_id
[] = {
106 { "adgs1408", ADGS1408
},
107 { "adgs1409", ADGS1409
},
110 MODULE_DEVICE_TABLE(spi
, adgs1408_spi_id
);
112 static const struct of_device_id adgs1408_of_match
[] = {
113 { .compatible
= "adi,adgs1408", .data
= (void *)ADGS1408
, },
114 { .compatible
= "adi,adgs1409", .data
= (void *)ADGS1409
, },
117 MODULE_DEVICE_TABLE(of
, adgs1408_of_match
);
119 static struct spi_driver adgs1408_driver
= {
122 .of_match_table
= adgs1408_of_match
,
124 .probe
= adgs1408_probe
,
125 .id_table
= adgs1408_spi_id
,
127 module_spi_driver(adgs1408_driver
);
129 MODULE_AUTHOR("Mircea Caprioru <mircea.caprioru@analog.com>");
130 MODULE_DESCRIPTION("Analog Devices ADGS1408 MUX driver");
131 MODULE_LICENSE("GPL");