blob: caaa782b50d8387ac39252dc01845d910549b3b7 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Peter Korsgaard92ed1a72011-01-10 22:11:23 +01002/*
3 * I2C multiplexer using GPIO API
4 *
5 * Peter Korsgaard <peter.korsgaard@barco.com>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +01006 */
7
8#include <linux/i2c.h>
9#include <linux/i2c-mux.h>
Wolfram Sang62ea22c2018-04-19 22:00:08 +020010#include <linux/platform_data/i2c-mux-gpio.h>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010011#include <linux/platform_device.h>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010012#include <linux/module.h>
13#include <linux/slab.h>
Linus Walleijd308dfb2019-06-18 12:58:33 +020014#include <linux/bits.h>
15#include <linux/gpio/consumer.h>
16/* FIXME: stop poking around inside gpiolib */
Peter Rosin6ef91fc2016-05-04 22:15:29 +020017#include "../../gpio/gpiolib.h"
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010018
19struct gpiomux {
Jean Delvaree7065e22012-04-28 15:32:06 +020020 struct i2c_mux_gpio_platform_data data;
Linus Walleijd308dfb2019-06-18 12:58:33 +020021 int ngpios;
Peter Rosinfeff5c02016-11-24 19:19:28 +010022 struct gpio_desc **gpios;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010023};
24
Jean Delvaree7065e22012-04-28 15:32:06 +020025static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010026{
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +020027 DECLARE_BITMAP(values, BITS_PER_TYPE(val));
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010028
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +020029 values[0] = val;
Peter Rosinfeff5c02016-11-24 19:19:28 +010030
Linus Walleijd308dfb2019-06-18 12:58:33 +020031 gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010032}
33
Peter Rosinbb448142016-04-20 08:39:05 +020034static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010035{
Peter Rosinbb448142016-04-20 08:39:05 +020036 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010037
Ionut Nicu8c0ec252013-10-11 12:09:57 +020038 i2c_mux_gpio_set(mux, chan);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010039
40 return 0;
41}
42
Peter Rosinbb448142016-04-20 08:39:05 +020043static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010044{
Peter Rosinbb448142016-04-20 08:39:05 +020045 struct gpiomux *mux = i2c_mux_priv(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010046
Jean Delvaree7065e22012-04-28 15:32:06 +020047 i2c_mux_gpio_set(mux, mux->data.idle);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010048
49 return 0;
50}
51
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020052#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -050053static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020054 struct platform_device *pdev)
55{
Evan Green19eb29b2020-11-18 15:40:24 -080056 struct device *dev = &pdev->dev;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020057 struct device_node *np = pdev->dev.of_node;
58 struct device_node *adapter_np, *child;
59 struct i2c_adapter *adapter;
Linus Walleijd308dfb2019-06-18 12:58:33 +020060 unsigned *values;
61 int i = 0;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020062
63 if (!np)
64 return -ENODEV;
65
66 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
67 if (!adapter_np) {
68 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
69 return -ENODEV;
70 }
71 adapter = of_find_i2c_adapter_by_node(adapter_np);
Vladimir Zapolskiybdbf4a22015-08-26 23:59:33 +030072 of_node_put(adapter_np);
Ioan Nicu623e4ec2015-03-30 15:03:38 +020073 if (!adapter)
Ionut Nicu9937b8d2013-10-09 11:50:45 +020074 return -EPROBE_DEFER;
Ioan Nicu623e4ec2015-03-30 15:03:38 +020075
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020076 mux->data.parent = i2c_adapter_id(adapter);
77 put_device(&adapter->dev);
78
79 mux->data.n_values = of_get_child_count(np);
80
Evan Green19eb29b2020-11-18 15:40:24 -080081 values = devm_kcalloc(dev,
Kees Cooka86854d2018-06-12 14:07:58 -070082 mux->data.n_values, sizeof(*mux->data.values),
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020083 GFP_KERNEL);
84 if (!values) {
Evan Green19eb29b2020-11-18 15:40:24 -080085 dev_err(dev, "Cannot allocate values array");
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020086 return -ENOMEM;
87 }
88
89 for_each_child_of_node(np, child) {
90 of_property_read_u32(child, "reg", values + i);
91 i++;
92 }
93 mux->data.values = values;
94
95 if (of_property_read_u32(np, "idle-state", &mux->data.idle))
96 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
97
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020098 return 0;
99}
100#else
Bill Pemberton0b255e92012-11-27 15:59:38 -0500101static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200102 struct platform_device *pdev)
103{
104 return 0;
105}
106#endif
107
Bill Pemberton0b255e92012-11-27 15:59:38 -0500108static int i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100109{
Peter Rosinbb448142016-04-20 08:39:05 +0200110 struct i2c_mux_core *muxc;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100111 struct gpiomux *mux;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100112 struct i2c_adapter *parent;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200113 struct i2c_adapter *root;
Linus Walleijd308dfb2019-06-18 12:58:33 +0200114 unsigned initial_state;
115 int i, ngpios, ret;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100116
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200117 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
Peter Rosinbb448142016-04-20 08:39:05 +0200118 if (!mux)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200119 return -ENOMEM;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200120
Jingoo Han6d4028c2013-07-30 16:59:33 +0900121 if (!dev_get_platdata(&pdev->dev)) {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200122 ret = i2c_mux_gpio_probe_dt(mux, pdev);
123 if (ret < 0)
124 return ret;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900125 } else {
126 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
127 sizeof(mux->data));
128 }
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200129
Linus Walleijd308dfb2019-06-18 12:58:33 +0200130 ngpios = gpiod_count(&pdev->dev, "mux");
131 if (ngpios <= 0) {
132 dev_err(&pdev->dev, "no valid gpios provided\n");
133 return ngpios ?: -EINVAL;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200134 }
Linus Walleijd308dfb2019-06-18 12:58:33 +0200135 mux->ngpios = ngpios;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200136
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200137 parent = i2c_get_adapter(mux->data.parent);
Ioan Nicu623e4ec2015-03-30 15:03:38 +0200138 if (!parent)
Ionut Nicu9937b8d2013-10-09 11:50:45 +0200139 return -EPROBE_DEFER;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100140
Peter Rosinfeff5c02016-11-24 19:19:28 +0100141 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
Linus Walleijd308dfb2019-06-18 12:58:33 +0200142 ngpios * sizeof(*mux->gpios), 0,
Peter Rosinbb448142016-04-20 08:39:05 +0200143 i2c_mux_gpio_select, NULL);
144 if (!muxc) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100145 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +0200146 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100147 }
Peter Rosinfeff5c02016-11-24 19:19:28 +0100148 mux->gpios = muxc->priv;
Peter Rosinbb448142016-04-20 08:39:05 +0200149 muxc->priv = mux;
150
151 platform_set_drvdata(pdev, muxc);
152
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200153 root = i2c_root_adapter(&parent->dev);
154
155 muxc->mux_locked = true;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100156
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200157 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
158 initial_state = mux->data.idle;
Peter Rosinbb448142016-04-20 08:39:05 +0200159 muxc->deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100160 } else {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200161 initial_state = mux->data.values[0];
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100162 }
163
Linus Walleijd308dfb2019-06-18 12:58:33 +0200164 for (i = 0; i < ngpios; i++) {
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200165 struct device *gpio_dev;
Linus Walleijd308dfb2019-06-18 12:58:33 +0200166 struct gpio_desc *gpiod;
167 enum gpiod_flags flag;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200168
Linus Walleijd308dfb2019-06-18 12:58:33 +0200169 if (initial_state & BIT(i))
170 flag = GPIOD_OUT_HIGH;
171 else
172 flag = GPIOD_OUT_LOW;
173 gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
174 if (IS_ERR(gpiod)) {
175 ret = PTR_ERR(gpiod);
176 goto alloc_failed;
Jean Delvare7fafae62013-03-06 22:35:53 +0000177 }
178
Linus Walleijd308dfb2019-06-18 12:58:33 +0200179 mux->gpios[i] = gpiod;
Peter Rosinfeff5c02016-11-24 19:19:28 +0100180
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200181 if (!muxc->mux_locked)
182 continue;
183
Linus Walleijd308dfb2019-06-18 12:58:33 +0200184 /* FIXME: find a proper way to access the GPIO device */
185 gpio_dev = &gpiod->gdev->dev;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200186 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100187 }
188
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200189 if (muxc->mux_locked)
190 dev_info(&pdev->dev, "mux-locked i2c mux\n");
191
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200192 for (i = 0; i < mux->data.n_values; i++) {
193 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
194 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100195
Peter Rosinbb448142016-04-20 08:39:05 +0200196 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
Peter Rosin1b009002017-04-03 10:14:11 +0200197 if (ret)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100198 goto add_adapter_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100199 }
200
201 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200202 mux->data.n_values, parent->name);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100203
204 return 0;
205
206add_adapter_failed:
Peter Rosinbb448142016-04-20 08:39:05 +0200207 i2c_mux_del_adapters(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100208alloc_failed:
209 i2c_put_adapter(parent);
210
211 return ret;
212}
213
Bill Pemberton0b255e92012-11-27 15:59:38 -0500214static int i2c_mux_gpio_remove(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100215{
Peter Rosinbb448142016-04-20 08:39:05 +0200216 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100217
Peter Rosinbb448142016-04-20 08:39:05 +0200218 i2c_mux_del_adapters(muxc);
Peter Rosinbb448142016-04-20 08:39:05 +0200219 i2c_put_adapter(muxc->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100220
221 return 0;
222}
223
Bill Pemberton0b255e92012-11-27 15:59:38 -0500224static const struct of_device_id i2c_mux_gpio_of_match[] = {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200225 { .compatible = "i2c-mux-gpio", },
226 {},
227};
228MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
229
Jean Delvaree7065e22012-04-28 15:32:06 +0200230static struct platform_driver i2c_mux_gpio_driver = {
231 .probe = i2c_mux_gpio_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500232 .remove = i2c_mux_gpio_remove,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100233 .driver = {
Jean Delvaree7065e22012-04-28 15:32:06 +0200234 .name = "i2c-mux-gpio",
Sachin Kamat4e905322013-09-30 09:04:25 +0530235 .of_match_table = i2c_mux_gpio_of_match,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100236 },
237};
238
Jean Delvaree7065e22012-04-28 15:32:06 +0200239module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100240
241MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
242MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
243MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200244MODULE_ALIAS("platform:i2c-mux-gpio");