1 /*
2  * Linear Technology LTC4306 and LTC4305 I2C multiplexer/switch
3  *
4  * Copyright (C) 2017 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  *
8  * Based on: i2c-mux-pca954x.c
9  *
10  * Datasheet: http://cds.linear.com/docs/en/datasheet/4306.pdf
11  */
12 
13 #include <linux/gpio/consumer.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/i2c-mux.h>
16 #include <linux/i2c.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_device.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 
24 #define LTC4305_MAX_NCHANS 2
25 #define LTC4306_MAX_NCHANS 4
26 
27 #define LTC_REG_STATUS	0x0
28 #define LTC_REG_CONFIG	0x1
29 #define LTC_REG_MODE	0x2
30 #define LTC_REG_SWITCH	0x3
31 
32 #define LTC_DOWNSTREAM_ACCL_EN	BIT(6)
33 #define LTC_UPSTREAM_ACCL_EN	BIT(7)
34 
35 #define LTC_GPIO_ALL_INPUT	0xC0
36 #define LTC_SWITCH_MASK		0xF0
37 
38 enum ltc_type {
39 	ltc_4305,
40 	ltc_4306,
41 };
42 
43 struct chip_desc {
44 	u8 nchans;
45 	u8 num_gpios;
46 };
47 
48 struct ltc4306 {
49 	struct regmap *regmap;
50 	struct gpio_chip gpiochip;
51 	const struct chip_desc *chip;
52 };
53 
54 static const struct chip_desc chips[] = {
55 	[ltc_4305] = {
56 		.nchans = LTC4305_MAX_NCHANS,
57 	},
58 	[ltc_4306] = {
59 		.nchans = LTC4306_MAX_NCHANS,
60 		.num_gpios = 2,
61 	},
62 };
63 
64 static bool ltc4306_is_volatile_reg(struct device *dev, unsigned int reg)
65 {
66 	return (reg == LTC_REG_CONFIG) ? true : false;
67 }
68 
69 static const struct regmap_config ltc4306_regmap_config = {
70 	.reg_bits = 8,
71 	.val_bits = 8,
72 	.max_register = LTC_REG_SWITCH,
73 	.volatile_reg = ltc4306_is_volatile_reg,
74 	.cache_type = REGCACHE_FLAT,
75 };
76 
77 static int ltc4306_gpio_get(struct gpio_chip *chip, unsigned int offset)
78 {
79 	struct ltc4306 *data = gpiochip_get_data(chip);
80 	unsigned int val;
81 	int ret;
82 
83 	ret = regmap_read(data->regmap, LTC_REG_CONFIG, &val);
84 	if (ret < 0)
85 		return ret;
86 
87 	return !!(val & BIT(1 - offset));
88 }
89 
90 static void ltc4306_gpio_set(struct gpio_chip *chip, unsigned int offset,
91 			     int value)
92 {
93 	struct ltc4306 *data = gpiochip_get_data(chip);
94 
95 	regmap_update_bits(data->regmap, LTC_REG_CONFIG, BIT(5 - offset),
96 			   value ? BIT(5 - offset) : 0);
97 }
98 
99 static int ltc4306_gpio_get_direction(struct gpio_chip *chip,
100 				      unsigned int offset)
101 {
102 	struct ltc4306 *data = gpiochip_get_data(chip);
103 	unsigned int val;
104 	int ret;
105 
106 	ret = regmap_read(data->regmap, LTC_REG_MODE, &val);
107 	if (ret < 0)
108 		return ret;
109 
110 	return !!(val & BIT(7 - offset));
111 }
112 
113 static int ltc4306_gpio_direction_input(struct gpio_chip *chip,
114 					unsigned int offset)
115 {
116 	struct ltc4306 *data = gpiochip_get_data(chip);
117 
118 	return regmap_update_bits(data->regmap, LTC_REG_MODE,
119 				  BIT(7 - offset), BIT(7 - offset));
120 }
121 
122 static int ltc4306_gpio_direction_output(struct gpio_chip *chip,
123 					 unsigned int offset, int value)
124 {
125 	struct ltc4306 *data = gpiochip_get_data(chip);
126 
127 	ltc4306_gpio_set(chip, offset, value);
128 	return regmap_update_bits(data->regmap, LTC_REG_MODE,
129 				  BIT(7 - offset), 0);
130 }
131 
132 static int ltc4306_gpio_set_config(struct gpio_chip *chip,
133 				   unsigned int offset, unsigned long config)
134 {
135 	struct ltc4306 *data = gpiochip_get_data(chip);
136 	unsigned int val;
137 
138 	switch (pinconf_to_config_param(config)) {
139 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
140 		val = 0;
141 		break;
142 	case PIN_CONFIG_DRIVE_PUSH_PULL:
143 		val = BIT(4 - offset);
144 		break;
145 	default:
146 		return -ENOTSUPP;
147 	}
148 
149 	return regmap_update_bits(data->regmap, LTC_REG_MODE,
150 				  BIT(4 - offset), val);
151 }
152 
153 static int ltc4306_gpio_init(struct ltc4306 *data)
154 {
155 	struct device *dev = regmap_get_device(data->regmap);
156 
157 	if (!data->chip->num_gpios)
158 		return 0;
159 
160 	data->gpiochip.label = dev_name(dev);
161 	data->gpiochip.base = -1;
162 	data->gpiochip.ngpio = data->chip->num_gpios;
163 	data->gpiochip.parent = dev;
164 	data->gpiochip.can_sleep = true;
165 	data->gpiochip.get_direction = ltc4306_gpio_get_direction;
166 	data->gpiochip.direction_input = ltc4306_gpio_direction_input;
167 	data->gpiochip.direction_output = ltc4306_gpio_direction_output;
168 	data->gpiochip.get = ltc4306_gpio_get;
169 	data->gpiochip.set = ltc4306_gpio_set;
170 	data->gpiochip.set_config = ltc4306_gpio_set_config;
171 	data->gpiochip.owner = THIS_MODULE;
172 
173 	/* gpiolib assumes all GPIOs default input */
174 	regmap_write(data->regmap, LTC_REG_MODE, LTC_GPIO_ALL_INPUT);
175 
176 	return devm_gpiochip_add_data(dev, &data->gpiochip, data);
177 }
178 
179 static int ltc4306_select_mux(struct i2c_mux_core *muxc, u32 chan)
180 {
181 	struct ltc4306 *data = i2c_mux_priv(muxc);
182 
183 	return regmap_update_bits(data->regmap, LTC_REG_SWITCH,
184 				  LTC_SWITCH_MASK, BIT(7 - chan));
185 }
186 
187 static int ltc4306_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
188 {
189 	struct ltc4306 *data = i2c_mux_priv(muxc);
190 
191 	return regmap_update_bits(data->regmap, LTC_REG_SWITCH,
192 				  LTC_SWITCH_MASK, 0);
193 }
194 
195 static const struct i2c_device_id ltc4306_id[] = {
196 	{ "ltc4305", ltc_4305 },
197 	{ "ltc4306", ltc_4306 },
198 	{ }
199 };
200 MODULE_DEVICE_TABLE(i2c, ltc4306_id);
201 
202 static const struct of_device_id ltc4306_of_match[] = {
203 	{ .compatible = "lltc,ltc4305", .data = &chips[ltc_4305] },
204 	{ .compatible = "lltc,ltc4306", .data = &chips[ltc_4306] },
205 	{ }
206 };
207 MODULE_DEVICE_TABLE(of, ltc4306_of_match);
208 
209 static int ltc4306_probe(struct i2c_client *client)
210 {
211 	struct i2c_adapter *adap = client->adapter;
212 	const struct chip_desc *chip;
213 	struct i2c_mux_core *muxc;
214 	struct ltc4306 *data;
215 	struct gpio_desc *gpio;
216 	bool idle_disc;
217 	unsigned int val = 0;
218 	int num, ret;
219 
220 	chip = of_device_get_match_data(&client->dev);
221 
222 	if (!chip)
223 		chip = &chips[i2c_match_id(ltc4306_id, client)->driver_data];
224 
225 	idle_disc = device_property_read_bool(&client->dev,
226 					      "i2c-mux-idle-disconnect");
227 
228 	muxc = i2c_mux_alloc(adap, &client->dev,
229 			     chip->nchans, sizeof(*data),
230 			     I2C_MUX_LOCKED, ltc4306_select_mux,
231 			     idle_disc ? ltc4306_deselect_mux : NULL);
232 	if (!muxc)
233 		return -ENOMEM;
234 	data = i2c_mux_priv(muxc);
235 	data->chip = chip;
236 
237 	i2c_set_clientdata(client, muxc);
238 
239 	data->regmap = devm_regmap_init_i2c(client, &ltc4306_regmap_config);
240 	if (IS_ERR(data->regmap)) {
241 		ret = PTR_ERR(data->regmap);
242 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
243 			ret);
244 		return ret;
245 	}
246 
247 	/* Reset and enable the mux if an enable GPIO is specified. */
248 	gpio = devm_gpiod_get_optional(&client->dev, "enable", GPIOD_OUT_LOW);
249 	if (IS_ERR(gpio))
250 		return PTR_ERR(gpio);
251 
252 	if (gpio) {
253 		udelay(1);
254 		gpiod_set_value(gpio, 1);
255 	}
256 
257 	/*
258 	 * Write the mux register at addr to verify
259 	 * that the mux is in fact present. This also
260 	 * initializes the mux to disconnected state.
261 	 */
262 	if (regmap_write(data->regmap, LTC_REG_SWITCH, 0) < 0) {
263 		dev_warn(&client->dev, "probe failed\n");
264 		return -ENODEV;
265 	}
266 
267 	if (device_property_read_bool(&client->dev,
268 				      "ltc,downstream-accelerators-enable"))
269 		val |= LTC_DOWNSTREAM_ACCL_EN;
270 
271 	if (device_property_read_bool(&client->dev,
272 				      "ltc,upstream-accelerators-enable"))
273 		val |= LTC_UPSTREAM_ACCL_EN;
274 
275 	if (regmap_write(data->regmap, LTC_REG_CONFIG, val) < 0)
276 		return -ENODEV;
277 
278 	ret = ltc4306_gpio_init(data);
279 	if (ret < 0)
280 		return ret;
281 
282 	/* Now create an adapter for each channel */
283 	for (num = 0; num < chip->nchans; num++) {
284 		ret = i2c_mux_add_adapter(muxc, 0, num, 0);
285 		if (ret) {
286 			i2c_mux_del_adapters(muxc);
287 			return ret;
288 		}
289 	}
290 
291 	dev_info(&client->dev,
292 		 "registered %d multiplexed busses for I2C switch %s\n",
293 		 num, client->name);
294 
295 	return 0;
296 }
297 
298 static int ltc4306_remove(struct i2c_client *client)
299 {
300 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
301 
302 	i2c_mux_del_adapters(muxc);
303 
304 	return 0;
305 }
306 
307 static struct i2c_driver ltc4306_driver = {
308 	.driver		= {
309 		.name	= "ltc4306",
310 		.of_match_table = of_match_ptr(ltc4306_of_match),
311 	},
312 	.probe_new	= ltc4306_probe,
313 	.remove		= ltc4306_remove,
314 	.id_table	= ltc4306_id,
315 };
316 
317 module_i2c_driver(ltc4306_driver);
318 
319 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
320 MODULE_DESCRIPTION("Linear Technology LTC4306, LTC4305 I2C mux/switch driver");
321 MODULE_LICENSE("GPL v2");
322