serial: fix race between flush_to_ldisc and tty_open
[linux-stable.git] / drivers / mux / adg792a.c
blob12aa221ab90d53ea108f05fa68efc34455ffc60b
1 /*
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. */
31 if (reset)
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);
40 u8 cmd;
42 if (mux->chip->controllers == 1) {
43 /* parallel mux controller operation */
44 if (state == MUX_IDLE_DISCONNECT)
45 cmd = ADG792A_DISABLE_ALL;
46 else
47 cmd = ADG792A_MUX_ALL(state);
48 } else {
49 unsigned int controller = mux_control_get_index(mux);
51 if (state == MUX_IDLE_DISCONNECT)
52 cmd = ADG792A_DISABLE(controller);
53 else
54 cmd = ADG792A_MUX(controller, state);
57 return adg792a_write_cmd(i2c, cmd, 0);
60 static const struct mux_control_ops adg792a_ops = {
61 .set = adg792a_set,
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;
69 s32 idle_state[3];
70 u32 cells;
71 int ret;
72 int i;
74 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
75 return -ENODEV;
77 ret = device_property_read_u32(dev, "#mux-control-cells", &cells);
78 if (ret < 0)
79 return ret;
80 if (cells >= 2)
81 return -EINVAL;
83 mux_chip = devm_mux_chip_alloc(dev, cells ? 3 : 1, 0);
84 if (IS_ERR(mux_chip))
85 return PTR_ERR(mux_chip);
87 mux_chip->ops = &adg792a_ops;
89 ret = adg792a_write_cmd(i2c, ADG792A_DISABLE_ALL, 1);
90 if (ret < 0)
91 return ret;
93 ret = device_property_read_u32_array(dev, "idle-state",
94 (u32 *)idle_state,
95 mux_chip->controllers);
96 if (ret < 0) {
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];
105 mux->states = 4;
107 switch (idle_state[i]) {
108 case MUX_IDLE_DISCONNECT:
109 case MUX_IDLE_AS_IS:
110 case 0 ... 4:
111 mux->idle_state = idle_state[i];
112 break;
113 default:
114 dev_err(dev, "invalid idle-state %d\n", idle_state[i]);
115 return -EINVAL;
119 ret = devm_mux_chip_register(dev, mux_chip);
120 if (ret < 0)
121 return ret;
123 if (cells)
124 dev_info(dev, "3x single pole quadruple throw muxes registered\n");
125 else
126 dev_info(dev, "triple pole quadruple throw mux registered\n");
128 return 0;
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 = {
146 .driver = {
147 .name = "adg792a",
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");