blob: bac415a52b7808c27ad801cd236f223cc59b2cf6 [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
Evan Green98b2b712020-11-18 15:40:25 -080052#ifdef CONFIG_ACPI
53
54static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
55 struct fwnode_handle *fwdev,
56 unsigned int *adr)
57
58{
59 unsigned long long adr64;
60 acpi_status status;
61
62 status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwdev),
63 METHOD_NAME__ADR,
64 NULL, &adr64);
65
66 if (!ACPI_SUCCESS(status)) {
67 dev_err(dev, "Cannot get address\n");
68 return -EINVAL;
69 }
70
71 *adr = adr64;
72 if (*adr != adr64) {
73 dev_err(dev, "Address out of range\n");
74 return -ERANGE;
75 }
76
77 return 0;
78}
79
80#else
81
82static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
83 struct fwnode_handle *fwdev,
84 unsigned int *adr)
85{
86 return -EINVAL;
87}
88
89#endif
90
91static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
92 struct platform_device *pdev)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020093{
Evan Green19eb29b2020-11-18 15:40:24 -080094 struct device *dev = &pdev->dev;
Evan Green98b2b712020-11-18 15:40:25 -080095 struct device_node *np = dev->of_node;
96 struct device_node *adapter_np;
97 struct i2c_adapter *adapter = NULL;
98 struct fwnode_handle *child;
Linus Walleijd308dfb2019-06-18 12:58:33 +020099 unsigned *values;
Evan Green98b2b712020-11-18 15:40:25 -0800100 int rc, i = 0;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200101
Evan Green98b2b712020-11-18 15:40:25 -0800102 if (is_of_node(dev->fwnode)) {
103 if (!np)
104 return -ENODEV;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200105
Evan Green98b2b712020-11-18 15:40:25 -0800106 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
107 if (!adapter_np) {
108 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
109 return -ENODEV;
110 }
111 adapter = of_find_i2c_adapter_by_node(adapter_np);
112 of_node_put(adapter_np);
113
114 } else if (is_acpi_node(dev->fwnode)) {
115 /*
116 * In ACPI land the mux should be a direct child of the i2c
117 * bus it muxes.
118 */
119 acpi_handle dev_handle = ACPI_HANDLE(dev->parent);
120
121 adapter = i2c_acpi_find_adapter_by_handle(dev_handle);
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200122 }
Evan Green98b2b712020-11-18 15:40:25 -0800123
Ioan Nicu623e4ec2015-03-30 15:03:38 +0200124 if (!adapter)
Ionut Nicu9937b8d2013-10-09 11:50:45 +0200125 return -EPROBE_DEFER;
Ioan Nicu623e4ec2015-03-30 15:03:38 +0200126
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200127 mux->data.parent = i2c_adapter_id(adapter);
128 put_device(&adapter->dev);
129
Evan Green98b2b712020-11-18 15:40:25 -0800130 mux->data.n_values = device_get_child_node_count(dev);
Evan Green19eb29b2020-11-18 15:40:24 -0800131 values = devm_kcalloc(dev,
Kees Cooka86854d2018-06-12 14:07:58 -0700132 mux->data.n_values, sizeof(*mux->data.values),
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200133 GFP_KERNEL);
134 if (!values) {
Evan Green19eb29b2020-11-18 15:40:24 -0800135 dev_err(dev, "Cannot allocate values array");
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200136 return -ENOMEM;
137 }
138
Evan Green98b2b712020-11-18 15:40:25 -0800139 device_for_each_child_node(dev, child) {
140 if (is_of_node(child)) {
141 fwnode_property_read_u32(child, "reg", values + i);
142
143 } else if (is_acpi_node(child)) {
144 rc = i2c_mux_gpio_get_acpi_adr(dev, child, values + i);
145 if (rc)
146 return rc;
147 }
148
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200149 i++;
150 }
151 mux->data.values = values;
152
Evan Green98b2b712020-11-18 15:40:25 -0800153 if (fwnode_property_read_u32(dev->fwnode, "idle-state", &mux->data.idle))
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200154 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
155
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200156 return 0;
157}
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200158
Bill Pemberton0b255e92012-11-27 15:59:38 -0500159static int i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100160{
Peter Rosinbb448142016-04-20 08:39:05 +0200161 struct i2c_mux_core *muxc;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100162 struct gpiomux *mux;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100163 struct i2c_adapter *parent;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200164 struct i2c_adapter *root;
Linus Walleijd308dfb2019-06-18 12:58:33 +0200165 unsigned initial_state;
166 int i, ngpios, ret;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100167
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200168 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
Peter Rosinbb448142016-04-20 08:39:05 +0200169 if (!mux)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200170 return -ENOMEM;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200171
Jingoo Han6d4028c2013-07-30 16:59:33 +0900172 if (!dev_get_platdata(&pdev->dev)) {
Evan Green98b2b712020-11-18 15:40:25 -0800173 ret = i2c_mux_gpio_probe_fw(mux, pdev);
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200174 if (ret < 0)
175 return ret;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900176 } else {
177 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
178 sizeof(mux->data));
179 }
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200180
Linus Walleijd308dfb2019-06-18 12:58:33 +0200181 ngpios = gpiod_count(&pdev->dev, "mux");
182 if (ngpios <= 0) {
183 dev_err(&pdev->dev, "no valid gpios provided\n");
184 return ngpios ?: -EINVAL;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200185 }
Linus Walleijd308dfb2019-06-18 12:58:33 +0200186 mux->ngpios = ngpios;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200187
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200188 parent = i2c_get_adapter(mux->data.parent);
Ioan Nicu623e4ec2015-03-30 15:03:38 +0200189 if (!parent)
Ionut Nicu9937b8d2013-10-09 11:50:45 +0200190 return -EPROBE_DEFER;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100191
Peter Rosinfeff5c02016-11-24 19:19:28 +0100192 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
Linus Walleijd308dfb2019-06-18 12:58:33 +0200193 ngpios * sizeof(*mux->gpios), 0,
Peter Rosinbb448142016-04-20 08:39:05 +0200194 i2c_mux_gpio_select, NULL);
195 if (!muxc) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100196 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +0200197 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100198 }
Peter Rosinfeff5c02016-11-24 19:19:28 +0100199 mux->gpios = muxc->priv;
Peter Rosinbb448142016-04-20 08:39:05 +0200200 muxc->priv = mux;
201
202 platform_set_drvdata(pdev, muxc);
203
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200204 root = i2c_root_adapter(&parent->dev);
205
206 muxc->mux_locked = true;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100207
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200208 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
209 initial_state = mux->data.idle;
Peter Rosinbb448142016-04-20 08:39:05 +0200210 muxc->deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100211 } else {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200212 initial_state = mux->data.values[0];
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100213 }
214
Linus Walleijd308dfb2019-06-18 12:58:33 +0200215 for (i = 0; i < ngpios; i++) {
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200216 struct device *gpio_dev;
Linus Walleijd308dfb2019-06-18 12:58:33 +0200217 struct gpio_desc *gpiod;
218 enum gpiod_flags flag;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200219
Linus Walleijd308dfb2019-06-18 12:58:33 +0200220 if (initial_state & BIT(i))
221 flag = GPIOD_OUT_HIGH;
222 else
223 flag = GPIOD_OUT_LOW;
224 gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
225 if (IS_ERR(gpiod)) {
226 ret = PTR_ERR(gpiod);
227 goto alloc_failed;
Jean Delvare7fafae62013-03-06 22:35:53 +0000228 }
229
Linus Walleijd308dfb2019-06-18 12:58:33 +0200230 mux->gpios[i] = gpiod;
Peter Rosinfeff5c02016-11-24 19:19:28 +0100231
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200232 if (!muxc->mux_locked)
233 continue;
234
Linus Walleijd308dfb2019-06-18 12:58:33 +0200235 /* FIXME: find a proper way to access the GPIO device */
236 gpio_dev = &gpiod->gdev->dev;
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200237 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100238 }
239
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200240 if (muxc->mux_locked)
241 dev_info(&pdev->dev, "mux-locked i2c mux\n");
242
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200243 for (i = 0; i < mux->data.n_values; i++) {
244 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
245 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100246
Peter Rosinbb448142016-04-20 08:39:05 +0200247 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
Peter Rosin1b009002017-04-03 10:14:11 +0200248 if (ret)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100249 goto add_adapter_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100250 }
251
252 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200253 mux->data.n_values, parent->name);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100254
255 return 0;
256
257add_adapter_failed:
Peter Rosinbb448142016-04-20 08:39:05 +0200258 i2c_mux_del_adapters(muxc);
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);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100268
Peter Rosinbb448142016-04-20 08:39:05 +0200269 i2c_mux_del_adapters(muxc);
Peter Rosinbb448142016-04-20 08:39:05 +0200270 i2c_put_adapter(muxc->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100271
272 return 0;
273}
274
Bill Pemberton0b255e92012-11-27 15:59:38 -0500275static const struct of_device_id i2c_mux_gpio_of_match[] = {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200276 { .compatible = "i2c-mux-gpio", },
277 {},
278};
279MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
280
Jean Delvaree7065e22012-04-28 15:32:06 +0200281static struct platform_driver i2c_mux_gpio_driver = {
282 .probe = i2c_mux_gpio_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500283 .remove = i2c_mux_gpio_remove,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100284 .driver = {
Jean Delvaree7065e22012-04-28 15:32:06 +0200285 .name = "i2c-mux-gpio",
Sachin Kamat4e905322013-09-30 09:04:25 +0530286 .of_match_table = i2c_mux_gpio_of_match,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100287 },
288};
289
Jean Delvaree7065e22012-04-28 15:32:06 +0200290module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100291
292MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
293MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
294MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200295MODULE_ALIAS("platform:i2c-mux-gpio");