1 // SPDX-License-Identifier: GPL-2.0
3 * Multiplexer driver for Analog Devices ADG792A/G Triple 4:1 mux
5 * Copyright (C) 2017 Axentia Technologies AB
7 * Author: Peter Rosin <peda@axentia.se>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/mux/driver.h>
14 #include <linux/property.h>
16 #define ADG792A_LDSW BIT(0)
17 #define ADG792A_RESETB BIT(1)
18 #define ADG792A_DISABLE(mux) (0x50 | (mux))
19 #define ADG792A_DISABLE_ALL (0x5f)
20 #define ADG792A_MUX(mux, state) (0xc0 | (((mux) + 1) << 2) | (state))
21 #define ADG792A_MUX_ALL(state) (0xc0 | (state))
23 static int adg792a_write_cmd(struct i2c_client
*i2c
, u8 cmd
, int reset
)
25 u8 data
= ADG792A_RESETB
| ADG792A_LDSW
;
27 /* ADG792A_RESETB is active low, the chip resets when it is zero. */
29 data
&= ~ADG792A_RESETB
;
31 return i2c_smbus_write_byte_data(i2c
, cmd
, data
);
34 static int adg792a_set(struct mux_control
*mux
, int state
)
36 struct i2c_client
*i2c
= to_i2c_client(mux
->chip
->dev
.parent
);
39 if (mux
->chip
->controllers
== 1) {
40 /* parallel mux controller operation */
41 if (state
== MUX_IDLE_DISCONNECT
)
42 cmd
= ADG792A_DISABLE_ALL
;
44 cmd
= ADG792A_MUX_ALL(state
);
46 unsigned int controller
= mux_control_get_index(mux
);
48 if (state
== MUX_IDLE_DISCONNECT
)
49 cmd
= ADG792A_DISABLE(controller
);
51 cmd
= ADG792A_MUX(controller
, state
);
54 return adg792a_write_cmd(i2c
, cmd
, 0);
57 static const struct mux_control_ops adg792a_ops
= {
61 static int adg792a_probe(struct i2c_client
*i2c
,
62 const struct i2c_device_id
*id
)
64 struct device
*dev
= &i2c
->dev
;
65 struct mux_chip
*mux_chip
;
71 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
74 ret
= device_property_read_u32(dev
, "#mux-control-cells", &cells
);
80 mux_chip
= devm_mux_chip_alloc(dev
, cells
? 3 : 1, 0);
82 return PTR_ERR(mux_chip
);
84 mux_chip
->ops
= &adg792a_ops
;
86 ret
= adg792a_write_cmd(i2c
, ADG792A_DISABLE_ALL
, 1);
90 ret
= device_property_read_u32_array(dev
, "idle-state",
92 mux_chip
->controllers
);
94 idle_state
[0] = MUX_IDLE_AS_IS
;
95 idle_state
[1] = MUX_IDLE_AS_IS
;
96 idle_state
[2] = MUX_IDLE_AS_IS
;
99 for (i
= 0; i
< mux_chip
->controllers
; ++i
) {
100 struct mux_control
*mux
= &mux_chip
->mux
[i
];
104 switch (idle_state
[i
]) {
105 case MUX_IDLE_DISCONNECT
:
108 mux
->idle_state
= idle_state
[i
];
111 dev_err(dev
, "invalid idle-state %d\n", idle_state
[i
]);
116 ret
= devm_mux_chip_register(dev
, mux_chip
);
121 dev_info(dev
, "3x single pole quadruple throw muxes registered\n");
123 dev_info(dev
, "triple pole quadruple throw mux registered\n");
128 static const struct i2c_device_id adg792a_id
[] = {
129 { .name
= "adg792a", },
130 { .name
= "adg792g", },
133 MODULE_DEVICE_TABLE(i2c
, adg792a_id
);
135 static const struct of_device_id adg792a_of_match
[] = {
136 { .compatible
= "adi,adg792a", },
137 { .compatible
= "adi,adg792g", },
140 MODULE_DEVICE_TABLE(of
, adg792a_of_match
);
142 static struct i2c_driver adg792a_driver
= {
145 .of_match_table
= of_match_ptr(adg792a_of_match
),
147 .probe
= adg792a_probe
,
148 .id_table
= adg792a_id
,
150 module_i2c_driver(adg792a_driver
);
152 MODULE_DESCRIPTION("Analog Devices ADG792A/G Triple 4:1 mux driver");
153 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
154 MODULE_LICENSE("GPL v2");