blob: 6a206e8d58f4e3f2985a00e287ea7a5103a86024 [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>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/gpio.h>
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020019#include <linux/of_i2c.h>
20#include <linux/of_gpio.h>
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010021
22struct gpiomux {
23 struct i2c_adapter *parent;
24 struct i2c_adapter **adap; /* child busses */
Jean Delvaree7065e22012-04-28 15:32:06 +020025 struct i2c_mux_gpio_platform_data data;
Jean Delvaree7ee5142012-10-05 22:23:54 +020026 unsigned gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010027};
28
Jean Delvaree7065e22012-04-28 15:32:06 +020029static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010030{
31 int i;
32
33 for (i = 0; i < mux->data.n_gpios; i++)
Jean Delvaree7ee5142012-10-05 22:23:54 +020034 gpio_set_value(mux->gpio_base + mux->data.gpios[i],
35 val & (1 << i));
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010036}
37
Jean Delvaree7065e22012-04-28 15:32:06 +020038static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010039{
40 struct gpiomux *mux = data;
41
Jean Delvaree7065e22012-04-28 15:32:06 +020042 i2c_mux_gpio_set(mux, mux->data.values[chan]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010043
44 return 0;
45}
46
Jean Delvaree7065e22012-04-28 15:32:06 +020047static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010048{
49 struct gpiomux *mux = data;
50
Jean Delvaree7065e22012-04-28 15:32:06 +020051 i2c_mux_gpio_set(mux, mux->data.idle);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010052
53 return 0;
54}
55
Bill Pemberton0b255e92012-11-27 15:59:38 -050056static int match_gpio_chip_by_label(struct gpio_chip *chip,
Jean Delvaree7ee5142012-10-05 22:23:54 +020057 void *data)
58{
59 return !strcmp(chip->label, data);
60}
61
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020062#ifdef CONFIG_OF
Bill Pemberton0b255e92012-11-27 15:59:38 -050063static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +020064 struct platform_device *pdev)
65{
66 struct device_node *np = pdev->dev.of_node;
67 struct device_node *adapter_np, *child;
68 struct i2c_adapter *adapter;
69 unsigned *values, *gpios;
70 int i = 0;
71
72 if (!np)
73 return -ENODEV;
74
75 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
76 if (!adapter_np) {
77 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
78 return -ENODEV;
79 }
80 adapter = of_find_i2c_adapter_by_node(adapter_np);
81 if (!adapter) {
82 dev_err(&pdev->dev, "Cannot find parent bus\n");
83 return -ENODEV;
84 }
85 mux->data.parent = i2c_adapter_id(adapter);
86 put_device(&adapter->dev);
87
88 mux->data.n_values = of_get_child_count(np);
89
90 values = devm_kzalloc(&pdev->dev,
91 sizeof(*mux->data.values) * mux->data.n_values,
92 GFP_KERNEL);
93 if (!values) {
94 dev_err(&pdev->dev, "Cannot allocate values array");
95 return -ENOMEM;
96 }
97
98 for_each_child_of_node(np, child) {
99 of_property_read_u32(child, "reg", values + i);
100 i++;
101 }
102 mux->data.values = values;
103
104 if (of_property_read_u32(np, "idle-state", &mux->data.idle))
105 mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
106
107 mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios");
108 if (mux->data.n_gpios < 0) {
109 dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n");
110 return -EINVAL;
111 }
112
113 gpios = devm_kzalloc(&pdev->dev,
114 sizeof(*mux->data.gpios) * mux->data.n_gpios, GFP_KERNEL);
115 if (!gpios) {
116 dev_err(&pdev->dev, "Cannot allocate gpios array");
117 return -ENOMEM;
118 }
119
120 for (i = 0; i < mux->data.n_gpios; i++)
121 gpios[i] = of_get_named_gpio(np, "mux-gpios", i);
122
123 mux->data.gpios = gpios;
124
125 return 0;
126}
127#else
Bill Pemberton0b255e92012-11-27 15:59:38 -0500128static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200129 struct platform_device *pdev)
130{
131 return 0;
132}
133#endif
134
Bill Pemberton0b255e92012-11-27 15:59:38 -0500135static int i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100136{
137 struct gpiomux *mux;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100138 struct i2c_adapter *parent;
139 int (*deselect) (struct i2c_adapter *, void *, u32);
Jean Delvaree7ee5142012-10-05 22:23:54 +0200140 unsigned initial_state, gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100141 int i, ret;
142
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200143 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
144 if (!mux) {
145 dev_err(&pdev->dev, "Cannot allocate gpiomux structure");
146 return -ENOMEM;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100147 }
148
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200149 platform_set_drvdata(pdev, mux);
150
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);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100178 if (!parent) {
179 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200180 mux->data.parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100181 return -ENODEV;
182 }
183
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100184 mux->parent = parent;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200185 mux->gpio_base = gpio_base;
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200186
Maxime Ripard2614a852012-10-05 22:23:53 +0200187 mux->adap = devm_kzalloc(&pdev->dev,
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200188 sizeof(*mux->adap) * mux->data.n_values,
Maxime Ripard2614a852012-10-05 22:23:53 +0200189 GFP_KERNEL);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100190 if (!mux->adap) {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200191 dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure");
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100192 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +0200193 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100194 }
195
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200196 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
197 initial_state = mux->data.idle;
Jean Delvaree7065e22012-04-28 15:32:06 +0200198 deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100199 } else {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200200 initial_state = mux->data.values[0];
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100201 deselect = NULL;
202 }
203
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200204 for (i = 0; i < mux->data.n_gpios; i++) {
205 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 Korsgaard92ed1a72011-01-10 22:11:23 +0100221 }
222
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200223 for (i = 0; i < mux->data.n_values; i++) {
224 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
225 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100226
Jean Delvareeee543e2012-10-05 22:23:51 +0200227 mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
228 i, class,
Jean Delvaree7065e22012-04-28 15:32:06 +0200229 i2c_mux_gpio_select, deselect);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100230 if (!mux->adap[i]) {
231 ret = -ENODEV;
232 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
233 goto add_adapter_failed;
234 }
235 }
236
237 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200238 mux->data.n_values, parent->name);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100239
240 return 0;
241
242add_adapter_failed:
243 for (; i > 0; i--)
244 i2c_del_mux_adapter(mux->adap[i - 1]);
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200245 i = mux->data.n_gpios;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100246err_request_gpio:
247 for (; i > 0; i--)
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200248 gpio_free(gpio_base + mux->data.gpios[i - 1]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100249alloc_failed:
250 i2c_put_adapter(parent);
251
252 return ret;
253}
254
Bill Pemberton0b255e92012-11-27 15:59:38 -0500255static int i2c_mux_gpio_remove(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100256{
257 struct gpiomux *mux = platform_get_drvdata(pdev);
258 int i;
259
260 for (i = 0; i < mux->data.n_values; i++)
261 i2c_del_mux_adapter(mux->adap[i]);
262
263 for (i = 0; i < mux->data.n_gpios; i++)
Jean Delvaree7ee5142012-10-05 22:23:54 +0200264 gpio_free(mux->gpio_base + mux->data.gpios[i]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100265
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100266 i2c_put_adapter(mux->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100267
268 return 0;
269}
270
Bill Pemberton0b255e92012-11-27 15:59:38 -0500271static const struct of_device_id i2c_mux_gpio_of_match[] = {
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200272 { .compatible = "i2c-mux-gpio", },
273 {},
274};
275MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
276
Jean Delvaree7065e22012-04-28 15:32:06 +0200277static struct platform_driver i2c_mux_gpio_driver = {
278 .probe = i2c_mux_gpio_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500279 .remove = i2c_mux_gpio_remove,
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100280 .driver = {
281 .owner = THIS_MODULE,
Jean Delvaree7065e22012-04-28 15:32:06 +0200282 .name = "i2c-mux-gpio",
Maxime Ripard9b7a0c42012-10-25 18:23:53 +0200283 .of_match_table = of_match_ptr(i2c_mux_gpio_of_match),
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100284 },
285};
286
Jean Delvaree7065e22012-04-28 15:32:06 +0200287module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100288
289MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
290MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
291MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200292MODULE_ALIAS("platform:i2c-mux-gpio");