Linux 4.16.11
[linux/fpc-iii.git] / drivers / mux / adg792a.c
blob6a8725cf3d717cce9539e950f6ad9124d57e6d34
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
8 */
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. */
28 if (reset)
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);
37 u8 cmd;
39 if (mux->chip->controllers == 1) {
40 /* parallel mux controller operation */
41 if (state == MUX_IDLE_DISCONNECT)
42 cmd = ADG792A_DISABLE_ALL;
43 else
44 cmd = ADG792A_MUX_ALL(state);
45 } else {
46 unsigned int controller = mux_control_get_index(mux);
48 if (state == MUX_IDLE_DISCONNECT)
49 cmd = ADG792A_DISABLE(controller);
50 else
51 cmd = ADG792A_MUX(controller, state);
54 return adg792a_write_cmd(i2c, cmd, 0);
57 static const struct mux_control_ops adg792a_ops = {
58 .set = adg792a_set,
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;
66 s32 idle_state[3];
67 u32 cells;
68 int ret;
69 int i;
71 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
72 return -ENODEV;
74 ret = device_property_read_u32(dev, "#mux-control-cells", &cells);
75 if (ret < 0)
76 return ret;
77 if (cells >= 2)
78 return -EINVAL;
80 mux_chip = devm_mux_chip_alloc(dev, cells ? 3 : 1, 0);
81 if (IS_ERR(mux_chip))
82 return PTR_ERR(mux_chip);
84 mux_chip->ops = &adg792a_ops;
86 ret = adg792a_write_cmd(i2c, ADG792A_DISABLE_ALL, 1);
87 if (ret < 0)
88 return ret;
90 ret = device_property_read_u32_array(dev, "idle-state",
91 (u32 *)idle_state,
92 mux_chip->controllers);
93 if (ret < 0) {
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];
102 mux->states = 4;
104 switch (idle_state[i]) {
105 case MUX_IDLE_DISCONNECT:
106 case MUX_IDLE_AS_IS:
107 case 0 ... 4:
108 mux->idle_state = idle_state[i];
109 break;
110 default:
111 dev_err(dev, "invalid idle-state %d\n", idle_state[i]);
112 return -EINVAL;
116 ret = devm_mux_chip_register(dev, mux_chip);
117 if (ret < 0)
118 return ret;
120 if (cells)
121 dev_info(dev, "3x single pole quadruple throw muxes registered\n");
122 else
123 dev_info(dev, "triple pole quadruple throw mux registered\n");
125 return 0;
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 = {
143 .driver = {
144 .name = "adg792a",
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");