blob: d835857bb09416a364d782436d60befc329f2e40 [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>
Wolfram Sang62ea22c2018-04-19 22:00:08 +020013#include <linux/platform_data/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 Rosinfeff5c02016-11-24 19:19:28 +010024 struct gpio_desc **gpios;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010025};
26
Jean Delvaree7065e22012-04-28 15:32:06 +020027static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010028{
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +020029 DECLARE_BITMAP(values, BITS_PER_TYPE(val));
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010030
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +020031 values[0] = val;
Peter Rosinfeff5c02016-11-24 19:19:28 +010032
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +020033 gpiod_set_array_value_cansleep(mux->data.n_gpios, mux->gpios, values);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010034}
35
Peter Rosinbb448142016-04-20 08:39:05 +020036static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010037{
Peter Rosinbb448142016-04-20 08:39:05 +020038 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010039
Ionut Nicu8c0ec252013-10-11 12:09:57 +020040 i2c_mux_gpio_set(mux, chan);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010041
42 return 0;
43}
44
Peter Rosinbb448142016-04-20 08:39:05 +020045static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010046{
Peter Rosinbb448142016-04-20 08:39:05 +020047 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010048
Jean Delvaree7065e22012-04-28 15:32:06 +020049 i2c_mux_gpio_set(mux, mux->data.idle);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010050
51 return 0;
52}
53
Bill Pemberton0b255e92012-11-27 15:59:38 -050054static int match_gpio_chip_by_label(struct gpio_chip *chip,
Jean Delvaree7ee5142012-10-05 22:23:54 +020055 void *data)
56{
57 return !strcmp(chip->label, data);
58}
59
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020060#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -050061static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020062 struct platform_device *pdev)
63{
64 struct device_node *np = pdev->dev.of_node;
65 struct device_node *adapter_np, *child;
66 struct i2c_adapter *adapter;
67 unsigned *values, *gpios;
Ionut Nicu492ed772013-10-08 15:51:53 +020068 int i = 0, ret;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020069
70 if (!np)
71 return -ENODEV;
72
73 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
74 if (!adapter_np) {
75 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
76 return -ENODEV;
77 }
78 adapter = of_find_i2c_adapter_by_node(adapter_np);
Vladimir Zapolskiybdbf4a22015-08-26 23:59:33 +030079 of_node_put(adapter_np);
Ioan Nicu623e4ec2015-03-30 15:03:38 +020080 if (!adapter)
Ionut Nicu9937b8d2013-10-09 11:50:45 +020081 return -EPROBE_DEFER;
Ioan Nicu623e4ec2015-03-30 15:03:38 +020082
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020083 mux->data.parent = i2c_adapter_id(adapter);
84 put_device(&adapter->dev);
85
86 mux->data.n_values = of_get_child_count(np);
87
Kees Cooka86854d2018-06-12 14:07:58 -070088 values = devm_kcalloc(&pdev->dev,
89 mux->data.n_values, sizeof(*mux->data.values),
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020090 GFP_KERNEL);
91 if (!values) {
92 dev_err(&pdev->dev, "Cannot allocate values array");
93 return -ENOMEM;
94 }
95
96 for_each_child_of_node(np, child) {
97 of_property_read_u32(child, "reg", values + i);
98 i++;
99 }
100 mux->data.values = values;
101
102 if (of_property_read_u32(np, "idle-state", &mux->data.idle))
103 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
104
105 mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios");
106 if (mux->data.n_gpios < 0) {
107 dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n");
108 return -EINVAL;
109 }
110
Kees Cooka86854d2018-06-12 14:07:58 -0700111 gpios = devm_kcalloc(&pdev->dev,
112 mux->data.n_gpios, sizeof(*mux->data.gpios),
113 GFP_KERNEL);
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200114 if (!gpios) {
115 dev_err(&pdev->dev, "Cannot allocate gpios array");
116 return -ENOMEM;
117 }
118
Ionut Nicu492ed772013-10-08 15:51:53 +0200119 for (i = 0; i < mux->data.n_gpios; i++) {
120 ret = of_get_named_gpio(np, "mux-gpios", i);
121 if (ret < 0)
122 return ret;
123 gpios[i] = ret;
124 }
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200125
126 mux->data.gpios = gpios;
127
128 return 0;
129}
130#else
Bill Pemberton0b255e92012-11-27 15:59:38 -0500131static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200132 struct platform_device *pdev)
133{
134 return 0;
135}
136#endif
137
Bill Pemberton0b255e92012-11-27 15:59:38 -0500138static int i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100139{
Peter Rosinbb448142016-04-20 08:39:05 +0200140 struct i2c_mux_core *muxc;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100141 struct gpiomux *mux;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100142 struct i2c_adapter *parent;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200143 struct i2c_adapter *root;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200144 unsigned initial_state, gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100145 int i, ret;
146
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200147 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
Peter Rosinbb448142016-04-20 08:39:05 +0200148 if (!mux)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200149 return -ENOMEM;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200150
Jingoo Han6d4028c2013-07-30 16:59:33 +0900151 if (!dev_get_platdata(&pdev->dev)) {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200152 ret = i2c_mux_gpio_probe_dt(mux, pdev);
153 if (ret < 0)
154 return ret;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900155 } else {
156 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
157 sizeof(mux->data));
158 }
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200159
Jean Delvaree7ee5142012-10-05 22:23:54 +0200160 /*
161 * If a GPIO chip name is provided, the GPIO pin numbers provided are
162 * relative to its base GPIO number. Otherwise they are absolute.
163 */
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200164 if (mux->data.gpio_chip) {
Jean Delvaree7ee5142012-10-05 22:23:54 +0200165 struct gpio_chip *gpio;
166
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200167 gpio = gpiochip_find(mux->data.gpio_chip,
Jean Delvaree7ee5142012-10-05 22:23:54 +0200168 match_gpio_chip_by_label);
169 if (!gpio)
170 return -EPROBE_DEFER;
171
172 gpio_base = gpio->base;
173 } else {
174 gpio_base = 0;
175 }
176
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200177 parent = i2c_get_adapter(mux->data.parent);
Ioan Nicu623e4ec2015-03-30 15:03:38 +0200178 if (!parent)
Ionut Nicu9937b8d2013-10-09 11:50:45 +0200179 return -EPROBE_DEFER;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100180
Peter Rosinfeff5c02016-11-24 19:19:28 +0100181 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200182 mux->data.n_gpios * sizeof(*mux->gpios), 0,
Peter Rosinbb448142016-04-20 08:39:05 +0200183 i2c_mux_gpio_select, NULL);
184 if (!muxc) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100185 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +0200186 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100187 }
Peter Rosinfeff5c02016-11-24 19:19:28 +0100188 mux->gpios = muxc->priv;
Peter Rosinbb448142016-04-20 08:39:05 +0200189 muxc->priv = mux;
190
191 platform_set_drvdata(pdev, muxc);
192
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200193 root = i2c_root_adapter(&parent->dev);
194
195 muxc->mux_locked = true;
Peter Rosinbb448142016-04-20 08:39:05 +0200196 mux->gpio_base = gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100197
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200198 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
199 initial_state = mux->data.idle;
Peter Rosinbb448142016-04-20 08:39:05 +0200200 muxc->deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100201 } else {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200202 initial_state = mux->data.values[0];
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100203 }
204
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200205 for (i = 0; i < mux->data.n_gpios; i++) {
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200206 struct device *gpio_dev;
207 struct gpio_desc *gpio_desc;
208
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200209 ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio");
Jean Delvare7fafae62013-03-06 22:35:53 +0000210 if (ret) {
211 dev_err(&pdev->dev, "Failed to request GPIO %d\n",
212 mux->data.gpios[i]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100213 goto err_request_gpio;
Jean Delvare7fafae62013-03-06 22:35:53 +0000214 }
215
216 ret = gpio_direction_output(gpio_base + mux->data.gpios[i],
217 initial_state & (1 << i));
218 if (ret) {
219 dev_err(&pdev->dev,
220 "Failed to set direction of GPIO %d to output\n",
221 mux->data.gpios[i]);
222 i++; /* gpio_request above succeeded, so must free */
223 goto err_request_gpio;
224 }
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200225
Peter Rosinfeff5c02016-11-24 19:19:28 +0100226 gpio_desc = gpio_to_desc(gpio_base + mux->data.gpios[i]);
227 mux->gpios[i] = gpio_desc;
228
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200229 if (!muxc->mux_locked)
230 continue;
231
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200232 gpio_dev = &gpio_desc->gdev->dev;
233 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100234 }
235
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200236 if (muxc->mux_locked)
237 dev_info(&pdev->dev, "mux-locked i2c mux\n");
238
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200239 for (i = 0; i < mux->data.n_values; i++) {
240 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
241 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100242
Peter Rosinbb448142016-04-20 08:39:05 +0200243 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
Peter Rosin1b009002017-04-03 10:14:11 +0200244 if (ret)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100245 goto add_adapter_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100246 }
247
248 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200249 mux->data.n_values, parent->name);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100250
251 return 0;
252
253add_adapter_failed:
Peter Rosinbb448142016-04-20 08:39:05 +0200254 i2c_mux_del_adapters(muxc);
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200255 i = mux->data.n_gpios;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100256err_request_gpio:
257 for (; i > 0; i--)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200258 gpio_free(gpio_base + mux->data.gpios[i - 1]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100259alloc_failed:
260 i2c_put_adapter(parent);
261
262 return ret;
263}
264
Bill Pemberton0b255e92012-11-27 15:59:38 -0500265static int i2c_mux_gpio_remove(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100266{
Peter Rosinbb448142016-04-20 08:39:05 +0200267 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
268 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100269 int i;
270
Peter Rosinbb448142016-04-20 08:39:05 +0200271 i2c_mux_del_adapters(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100272
273 for (i = 0; i < mux->data.n_gpios; i++)
Jean Delvaree7ee5142012-10-05 22:23:54 +0200274 gpio_free(mux->gpio_base + mux->data.gpios[i]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100275
Peter Rosinbb448142016-04-20 08:39:05 +0200276 i2c_put_adapter(muxc->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100277
278 return 0;
279}
280
Bill Pemberton0b255e92012-11-27 15:59:38 -0500281static const struct of_device_id i2c_mux_gpio_of_match[] = {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200282 { .compatible = "i2c-mux-gpio", },
283 {},
284};
285MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
286
Jean Delvaree7065e22012-04-28 15:32:06 +0200287static struct platform_driver i2c_mux_gpio_driver = {
288 .probe = i2c_mux_gpio_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500289 .remove = i2c_mux_gpio_remove,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100290 .driver = {
Jean Delvaree7065e22012-04-28 15:32:06 +0200291 .name = "i2c-mux-gpio",
Sachin Kamat4e905322013-09-30 09:04:25 +0530292 .of_match_table = i2c_mux_gpio_of_match,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100293 },
294};
295
Jean Delvaree7065e22012-04-28 15:32:06 +0200296module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100297
298MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
299MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
300MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200301MODULE_ALIAS("platform:i2c-mux-gpio");