blob: e5cf26eefa97e27532cc07e2438041093843bcc2 [file] [log] [blame]
Peter Korsgaard92ed1a72011-01-10 22:11:23 +01001/*
2 * I2C multiplexer using GPIO API
3 *
4 * Peter Korsgaard <peter.korsgaard@barco.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/i2c.h>
12#include <linux/i2c-mux.h>
Jean Delvaree7065e22012-04-28 15:32:06 +020013#include <linux/i2c-mux-gpio.h>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010014#include <linux/platform_device.h>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010015#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/gpio.h>
Peter Rosin6ef91fc2016-05-04 22:15:29 +020018#include "../../gpio/gpiolib.h"
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020019#include <linux/of_gpio.h>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010020
21struct gpiomux {
Jean Delvaree7065e22012-04-28 15:32:06 +020022 struct i2c_mux_gpio_platform_data data;
Jean Delvaree7ee5142012-10-05 22:23:54 +020023 unsigned gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010024};
25
Jean Delvaree7065e22012-04-28 15:32:06 +020026static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010027{
28 int i;
29
30 for (i = 0; i < mux->data.n_gpios; i++)
Ionut Nicu250ad592013-10-11 14:17:10 +020031 gpio_set_value_cansleep(mux->gpio_base + mux->data.gpios[i],
32 val & (1 << i));
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010033}
34
Peter Rosinbb448142016-04-20 08:39:05 +020035static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010036{
Peter Rosinbb448142016-04-20 08:39:05 +020037 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010038
Ionut Nicu8c0ec252013-10-11 12:09:57 +020039 i2c_mux_gpio_set(mux, chan);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010040
41 return 0;
42}
43
Peter Rosinbb448142016-04-20 08:39:05 +020044static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010045{
Peter Rosinbb448142016-04-20 08:39:05 +020046 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010047
Jean Delvaree7065e22012-04-28 15:32:06 +020048 i2c_mux_gpio_set(mux, mux->data.idle);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010049
50 return 0;
51}
52
Bill Pemberton0b255e92012-11-27 15:59:38 -050053static int match_gpio_chip_by_label(struct gpio_chip *chip,
Jean Delvaree7ee5142012-10-05 22:23:54 +020054 void *data)
55{
56 return !strcmp(chip->label, data);
57}
58
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020059#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -050060static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020061 struct platform_device *pdev)
62{
63 struct device_node *np = pdev->dev.of_node;
64 struct device_node *adapter_np, *child;
65 struct i2c_adapter *adapter;
66 unsigned *values, *gpios;
Ionut Nicu492ed772013-10-08 15:51:53 +020067 int i = 0, ret;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020068
69 if (!np)
70 return -ENODEV;
71
72 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
73 if (!adapter_np) {
74 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
75 return -ENODEV;
76 }
77 adapter = of_find_i2c_adapter_by_node(adapter_np);
Vladimir Zapolskiybdbf4a22015-08-26 23:59:33 +030078 of_node_put(adapter_np);
Ioan Nicu623e4ec2015-03-30 15:03:38 +020079 if (!adapter)
Ionut Nicu9937b8d2013-10-09 11:50:45 +020080 return -EPROBE_DEFER;
Ioan Nicu623e4ec2015-03-30 15:03:38 +020081
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020082 mux->data.parent = i2c_adapter_id(adapter);
83 put_device(&adapter->dev);
84
85 mux->data.n_values = of_get_child_count(np);
86
87 values = devm_kzalloc(&pdev->dev,
88 sizeof(*mux->data.values) * mux->data.n_values,
89 GFP_KERNEL);
90 if (!values) {
91 dev_err(&pdev->dev, "Cannot allocate values array");
92 return -ENOMEM;
93 }
94
95 for_each_child_of_node(np, child) {
96 of_property_read_u32(child, "reg", values + i);
97 i++;
98 }
99 mux->data.values = values;
100
101 if (of_property_read_u32(np, "idle-state", &mux->data.idle))
102 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
103
104 mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios");
105 if (mux->data.n_gpios < 0) {
106 dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n");
107 return -EINVAL;
108 }
109
110 gpios = devm_kzalloc(&pdev->dev,
111 sizeof(*mux->data.gpios) * mux->data.n_gpios, GFP_KERNEL);
112 if (!gpios) {
113 dev_err(&pdev->dev, "Cannot allocate gpios array");
114 return -ENOMEM;
115 }
116
Ionut Nicu492ed772013-10-08 15:51:53 +0200117 for (i = 0; i < mux->data.n_gpios; i++) {
118 ret = of_get_named_gpio(np, "mux-gpios", i);
119 if (ret < 0)
120 return ret;
121 gpios[i] = ret;
122 }
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200123
124 mux->data.gpios = gpios;
125
126 return 0;
127}
128#else
Bill Pemberton0b255e92012-11-27 15:59:38 -0500129static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200130 struct platform_device *pdev)
131{
132 return 0;
133}
134#endif
135
Bill Pemberton0b255e92012-11-27 15:59:38 -0500136static int i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100137{
Peter Rosinbb448142016-04-20 08:39:05 +0200138 struct i2c_mux_core *muxc;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100139 struct gpiomux *mux;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100140 struct i2c_adapter *parent;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200141 struct i2c_adapter *root;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200142 unsigned initial_state, gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100143 int i, ret;
144
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200145 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
Peter Rosinbb448142016-04-20 08:39:05 +0200146 if (!mux)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200147 return -ENOMEM;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200148
Jingoo Han6d4028c2013-07-30 16:59:33 +0900149 if (!dev_get_platdata(&pdev->dev)) {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200150 ret = i2c_mux_gpio_probe_dt(mux, pdev);
151 if (ret < 0)
152 return ret;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900153 } else {
154 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
155 sizeof(mux->data));
156 }
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200157
Jean Delvaree7ee5142012-10-05 22:23:54 +0200158 /*
159 * If a GPIO chip name is provided, the GPIO pin numbers provided are
160 * relative to its base GPIO number. Otherwise they are absolute.
161 */
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200162 if (mux->data.gpio_chip) {
Jean Delvaree7ee5142012-10-05 22:23:54 +0200163 struct gpio_chip *gpio;
164
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200165 gpio = gpiochip_find(mux->data.gpio_chip,
Jean Delvaree7ee5142012-10-05 22:23:54 +0200166 match_gpio_chip_by_label);
167 if (!gpio)
168 return -EPROBE_DEFER;
169
170 gpio_base = gpio->base;
171 } else {
172 gpio_base = 0;
173 }
174
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200175 parent = i2c_get_adapter(mux->data.parent);
Ioan Nicu623e4ec2015-03-30 15:03:38 +0200176 if (!parent)
Ionut Nicu9937b8d2013-10-09 11:50:45 +0200177 return -EPROBE_DEFER;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100178
Peter Rosinbb448142016-04-20 08:39:05 +0200179 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
180 i2c_mux_gpio_select, NULL);
181 if (!muxc) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100182 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +0200183 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100184 }
Peter Rosinbb448142016-04-20 08:39:05 +0200185 muxc->priv = mux;
186
187 platform_set_drvdata(pdev, muxc);
188
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200189 root = i2c_root_adapter(&parent->dev);
190
191 muxc->mux_locked = true;
Peter Rosinbb448142016-04-20 08:39:05 +0200192 mux->gpio_base = gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100193
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200194 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
195 initial_state = mux->data.idle;
Peter Rosinbb448142016-04-20 08:39:05 +0200196 muxc->deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100197 } else {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200198 initial_state = mux->data.values[0];
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100199 }
200
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200201 for (i = 0; i < mux->data.n_gpios; i++) {
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200202 struct device *gpio_dev;
203 struct gpio_desc *gpio_desc;
204
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200205 ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio");
Jean Delvare7fafae62013-03-06 22:35:53 +0000206 if (ret) {
207 dev_err(&pdev->dev, "Failed to request GPIO %d\n",
208 mux->data.gpios[i]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100209 goto err_request_gpio;
Jean Delvare7fafae62013-03-06 22:35:53 +0000210 }
211
212 ret = gpio_direction_output(gpio_base + mux->data.gpios[i],
213 initial_state & (1 << i));
214 if (ret) {
215 dev_err(&pdev->dev,
216 "Failed to set direction of GPIO %d to output\n",
217 mux->data.gpios[i]);
218 i++; /* gpio_request above succeeded, so must free */
219 goto err_request_gpio;
220 }
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200221
222 if (!muxc->mux_locked)
223 continue;
224
225 gpio_desc = gpio_to_desc(gpio_base + mux->data.gpios[i]);
226 gpio_dev = &gpio_desc->gdev->dev;
227 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100228 }
229
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200230 if (muxc->mux_locked)
231 dev_info(&pdev->dev, "mux-locked i2c mux\n");
232
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200233 for (i = 0; i < mux->data.n_values; i++) {
234 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
235 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100236
Peter Rosinbb448142016-04-20 08:39:05 +0200237 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
238 if (ret) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100239 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
240 goto add_adapter_failed;
241 }
242 }
243
244 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200245 mux->data.n_values, parent->name);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100246
247 return 0;
248
249add_adapter_failed:
Peter Rosinbb448142016-04-20 08:39:05 +0200250 i2c_mux_del_adapters(muxc);
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200251 i = mux->data.n_gpios;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100252err_request_gpio:
253 for (; i > 0; i--)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200254 gpio_free(gpio_base + mux->data.gpios[i - 1]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100255alloc_failed:
256 i2c_put_adapter(parent);
257
258 return ret;
259}
260
Bill Pemberton0b255e92012-11-27 15:59:38 -0500261static int i2c_mux_gpio_remove(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100262{
Peter Rosinbb448142016-04-20 08:39:05 +0200263 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
264 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100265 int i;
266
Peter Rosinbb448142016-04-20 08:39:05 +0200267 i2c_mux_del_adapters(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100268
269 for (i = 0; i < mux->data.n_gpios; i++)
Jean Delvaree7ee5142012-10-05 22:23:54 +0200270 gpio_free(mux->gpio_base + mux->data.gpios[i]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100271
Peter Rosinbb448142016-04-20 08:39:05 +0200272 i2c_put_adapter(muxc->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100273
274 return 0;
275}
276
Bill Pemberton0b255e92012-11-27 15:59:38 -0500277static const struct of_device_id i2c_mux_gpio_of_match[] = {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200278 { .compatible = "i2c-mux-gpio", },
279 {},
280};
281MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
282
Jean Delvaree7065e22012-04-28 15:32:06 +0200283static struct platform_driver i2c_mux_gpio_driver = {
284 .probe = i2c_mux_gpio_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500285 .remove = i2c_mux_gpio_remove,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100286 .driver = {
Jean Delvaree7065e22012-04-28 15:32:06 +0200287 .name = "i2c-mux-gpio",
Sachin Kamat4e905322013-09-30 09:04:25 +0530288 .of_match_table = i2c_mux_gpio_of_match,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100289 },
290};
291
Jean Delvaree7065e22012-04-28 15:32:06 +0200292module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100293
294MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
295MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
296MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200297MODULE_ALIAS("platform:i2c-mux-gpio");