blob: 4effe563e9e8d27a9e5c1b89818542355e1e27e3 [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{
56 struct device_node *np = pdev->dev.of_node;
57 struct device_node *adapter_np, *child;
58 struct i2c_adapter *adapter;
Linus Walleijd308dfb2019-06-18 12:58:33 +020059 unsigned *values;
60 int i = 0;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020061
62 if (!np)
63 return -ENODEV;
64
65 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
66 if (!adapter_np) {
67 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
68 return -ENODEV;
69 }
70 adapter = of_find_i2c_adapter_by_node(adapter_np);
Vladimir Zapolskiybdbf4a22015-08-26 23:59:33 +030071 of_node_put(adapter_np);
Ioan Nicu623e4ec2015-03-30 15:03:38 +020072 if (!adapter)
Ionut Nicu9937b8d2013-10-09 11:50:45 +020073 return -EPROBE_DEFER;
Ioan Nicu623e4ec2015-03-30 15:03:38 +020074
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020075 mux->data.parent = i2c_adapter_id(adapter);
76 put_device(&adapter->dev);
77
78 mux->data.n_values = of_get_child_count(np);
79
Kees Cooka86854d2018-06-12 14:07:58 -070080 values = devm_kcalloc(&pdev->dev,
81 mux->data.n_values, sizeof(*mux->data.values),
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020082 GFP_KERNEL);
83 if (!values) {
84 dev_err(&pdev->dev, "Cannot allocate values array");
85 return -ENOMEM;
86 }
87
88 for_each_child_of_node(np, child) {
89 of_property_read_u32(child, "reg", values + i);
90 i++;
91 }
92 mux->data.values = values;
93
94 if (of_property_read_u32(np, "idle-state", &mux->data.idle))
95 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
96
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020097 return 0;
98}
99#else
Bill Pemberton0b255e92012-11-27 15:59:38 -0500100static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200101 struct platform_device *pdev)
102{
103 return 0;
104}
105#endif
106
Bill Pemberton0b255e92012-11-27 15:59:38 -0500107static int i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100108{
Peter Rosinbb448142016-04-20 08:39:05 +0200109 struct i2c_mux_core *muxc;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100110 struct gpiomux *mux;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100111 struct i2c_adapter *parent;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200112 struct i2c_adapter *root;
Linus Walleijd308dfb2019-06-18 12:58:33 +0200113 unsigned initial_state;
114 int i, ngpios, ret;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100115
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200116 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
Peter Rosinbb448142016-04-20 08:39:05 +0200117 if (!mux)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200118 return -ENOMEM;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200119
Jingoo Han6d4028c2013-07-30 16:59:33 +0900120 if (!dev_get_platdata(&pdev->dev)) {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200121 ret = i2c_mux_gpio_probe_dt(mux, pdev);
122 if (ret < 0)
123 return ret;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900124 } else {
125 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
126 sizeof(mux->data));
127 }
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200128
Linus Walleijd308dfb2019-06-18 12:58:33 +0200129 ngpios = gpiod_count(&pdev->dev, "mux");
130 if (ngpios <= 0) {
131 dev_err(&pdev->dev, "no valid gpios provided\n");
132 return ngpios ?: -EINVAL;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200133 }
Linus Walleijd308dfb2019-06-18 12:58:33 +0200134 mux->ngpios = ngpios;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200135
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200136 parent = i2c_get_adapter(mux->data.parent);
Ioan Nicu623e4ec2015-03-30 15:03:38 +0200137 if (!parent)
Ionut Nicu9937b8d2013-10-09 11:50:45 +0200138 return -EPROBE_DEFER;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100139
Peter Rosinfeff5c02016-11-24 19:19:28 +0100140 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
Linus Walleijd308dfb2019-06-18 12:58:33 +0200141 ngpios * sizeof(*mux->gpios), 0,
Peter Rosinbb448142016-04-20 08:39:05 +0200142 i2c_mux_gpio_select, NULL);
143 if (!muxc) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100144 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +0200145 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100146 }
Peter Rosinfeff5c02016-11-24 19:19:28 +0100147 mux->gpios = muxc->priv;
Peter Rosinbb448142016-04-20 08:39:05 +0200148 muxc->priv = mux;
149
150 platform_set_drvdata(pdev, muxc);
151
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200152 root = i2c_root_adapter(&parent->dev);
153
154 muxc->mux_locked = true;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100155
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200156 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
157 initial_state = mux->data.idle;
Peter Rosinbb448142016-04-20 08:39:05 +0200158 muxc->deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100159 } else {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200160 initial_state = mux->data.values[0];
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100161 }
162
Linus Walleijd308dfb2019-06-18 12:58:33 +0200163 for (i = 0; i < ngpios; i++) {
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200164 struct device *gpio_dev;
Linus Walleijd308dfb2019-06-18 12:58:33 +0200165 struct gpio_desc *gpiod;
166 enum gpiod_flags flag;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200167
Linus Walleijd308dfb2019-06-18 12:58:33 +0200168 if (initial_state & BIT(i))
169 flag = GPIOD_OUT_HIGH;
170 else
171 flag = GPIOD_OUT_LOW;
172 gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
173 if (IS_ERR(gpiod)) {
174 ret = PTR_ERR(gpiod);
175 goto alloc_failed;
Jean Delvare7fafae62013-03-06 22:35:53 +0000176 }
177
Linus Walleijd308dfb2019-06-18 12:58:33 +0200178 mux->gpios[i] = gpiod;
Peter Rosinfeff5c02016-11-24 19:19:28 +0100179
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200180 if (!muxc->mux_locked)
181 continue;
182
Linus Walleijd308dfb2019-06-18 12:58:33 +0200183 /* FIXME: find a proper way to access the GPIO device */
184 gpio_dev = &gpiod->gdev->dev;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200185 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100186 }
187
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200188 if (muxc->mux_locked)
189 dev_info(&pdev->dev, "mux-locked i2c mux\n");
190
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200191 for (i = 0; i < mux->data.n_values; i++) {
192 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
193 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100194
Peter Rosinbb448142016-04-20 08:39:05 +0200195 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
Peter Rosin1b009002017-04-03 10:14:11 +0200196 if (ret)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100197 goto add_adapter_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100198 }
199
200 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200201 mux->data.n_values, parent->name);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100202
203 return 0;
204
205add_adapter_failed:
Peter Rosinbb448142016-04-20 08:39:05 +0200206 i2c_mux_del_adapters(muxc);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100207alloc_failed:
208 i2c_put_adapter(parent);
209
210 return ret;
211}
212
Bill Pemberton0b255e92012-11-27 15:59:38 -0500213static int i2c_mux_gpio_remove(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100214{
Peter Rosinbb448142016-04-20 08:39:05 +0200215 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100216
Peter Rosinbb448142016-04-20 08:39:05 +0200217 i2c_mux_del_adapters(muxc);
Peter Rosinbb448142016-04-20 08:39:05 +0200218 i2c_put_adapter(muxc->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100219
220 return 0;
221}
222
Bill Pemberton0b255e92012-11-27 15:59:38 -0500223static const struct of_device_id i2c_mux_gpio_of_match[] = {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200224 { .compatible = "i2c-mux-gpio", },
225 {},
226};
227MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
228
Jean Delvaree7065e22012-04-28 15:32:06 +0200229static struct platform_driver i2c_mux_gpio_driver = {
230 .probe = i2c_mux_gpio_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500231 .remove = i2c_mux_gpio_remove,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100232 .driver = {
Jean Delvaree7065e22012-04-28 15:32:06 +0200233 .name = "i2c-mux-gpio",
Sachin Kamat4e905322013-09-30 09:04:25 +0530234 .of_match_table = i2c_mux_gpio_of_match,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100235 },
236};
237
Jean Delvaree7065e22012-04-28 15:32:06 +0200238module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100239
240MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
241MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
242MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200243MODULE_ALIAS("platform:i2c-mux-gpio");