2 * Multiplexer driver for Analog Devices ADG792A/G Triple 4:1 mux
4 * Copyright (C) 2017 Axentia Technologies AB
6 * Author: Peter Rosin <peda@axentia.se>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/mux/driver.h>
17 #include <linux/property.h>
19 #define ADG792A_LDSW BIT(0)
20 #define ADG792A_RESETB BIT(1)
21 #define ADG792A_DISABLE(mux) (0x50 | (mux))
22 #define ADG792A_DISABLE_ALL (0x5f)
23 #define ADG792A_MUX(mux, state) (0xc0 | (((mux) + 1) << 2) | (state))
24 #define ADG792A_MUX_ALL(state) (0xc0 | (state))
26 static int adg792a_write_cmd(struct i2c_client
*i2c
, u8 cmd
, int reset
)
28 u8 data
= ADG792A_RESETB
| ADG792A_LDSW
;
30 /* ADG792A_RESETB is active low, the chip resets when it is zero. */
32 data
&= ~ADG792A_RESETB
;
34 return i2c_smbus_write_byte_data(i2c
, cmd
, data
);
37 static int adg792a_set(struct mux_control
*mux
, int state
)
39 struct i2c_client
*i2c
= to_i2c_client(mux
->chip
->dev
.parent
);
42 if (mux
->chip
->controllers
== 1) {
43 /* parallel mux controller operation */
44 if (state
== MUX_IDLE_DISCONNECT
)
45 cmd
= ADG792A_DISABLE_ALL
;
47 cmd
= ADG792A_MUX_ALL(state
);
49 unsigned int controller
= mux_control_get_index(mux
);
51 if (state
== MUX_IDLE_DISCONNECT
)
52 cmd
= ADG792A_DISABLE(controller
);
54 cmd
= ADG792A_MUX(controller
, state
);
57 return adg792a_write_cmd(i2c
, cmd
, 0);
60 static const struct mux_control_ops adg792a_ops
= {
64 static int adg792a_probe(struct i2c_client
*i2c
,
65 const struct i2c_device_id
*id
)
67 struct device
*dev
= &i2c
->dev
;
68 struct mux_chip
*mux_chip
;
74 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
77 ret
= device_property_read_u32(dev
, "#mux-control-cells", &cells
);
83 mux_chip
= devm_mux_chip_alloc(dev
, cells
? 3 : 1, 0);
85 return PTR_ERR(mux_chip
);
87 mux_chip
->ops
= &adg792a_ops
;
89 ret
= adg792a_write_cmd(i2c
, ADG792A_DISABLE_ALL
, 1);
93 ret
= device_property_read_u32_array(dev
, "idle-state",
95 mux_chip
->controllers
);
97 idle_state
[0] = MUX_IDLE_AS_IS
;
98 idle_state
[1] = MUX_IDLE_AS_IS
;
99 idle_state
[2] = MUX_IDLE_AS_IS
;
102 for (i
= 0; i
< mux_chip
->controllers
; ++i
) {
103 struct mux_control
*mux
= &mux_chip
->mux
[i
];
107 switch (idle_state
[i
]) {
108 case MUX_IDLE_DISCONNECT
:
111 mux
->idle_state
= idle_state
[i
];
114 dev_err(dev
, "invalid idle-state %d\n", idle_state
[i
]);
119 ret
= devm_mux_chip_register(dev
, mux_chip
);
124 dev_info(dev
, "3x single pole quadruple throw muxes registered\n");
126 dev_info(dev
, "triple pole quadruple throw mux registered\n");
131 static const struct i2c_device_id adg792a_id
[] = {
132 { .name
= "adg792a", },
133 { .name
= "adg792g", },
136 MODULE_DEVICE_TABLE(i2c
, adg792a_id
);
138 static const struct of_device_id adg792a_of_match
[] = {
139 { .compatible
= "adi,adg792a", },
140 { .compatible
= "adi,adg792g", },
143 MODULE_DEVICE_TABLE(of
, adg792a_of_match
);
145 static struct i2c_driver adg792a_driver
= {
148 .of_match_table
= of_match_ptr(adg792a_of_match
),
150 .probe
= adg792a_probe
,
151 .id_table
= adg792a_id
,
153 module_i2c_driver(adg792a_driver
);
155 MODULE_DESCRIPTION("Analog Devices ADG792A/G Triple 4:1 mux driver");
156 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
157 MODULE_LICENSE("GPL v2");