blob: 566a6757a33d7e53fbe24b6f7fe77b916f64b370 [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>
19
20struct gpiomux {
21 struct i2c_adapter *parent;
22 struct i2c_adapter **adap; /* child busses */
Jean Delvaree7065e22012-04-28 15:32:06 +020023 struct i2c_mux_gpio_platform_data data;
Jean Delvaree7ee5142012-10-05 22:23:54 +020024 unsigned gpio_base;
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{
29 int i;
30
31 for (i = 0; i < mux->data.n_gpios; i++)
Jean Delvaree7ee5142012-10-05 22:23:54 +020032 gpio_set_value(mux->gpio_base + mux->data.gpios[i],
33 val & (1 << i));
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010034}
35
Jean Delvaree7065e22012-04-28 15:32:06 +020036static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010037{
38 struct gpiomux *mux = data;
39
Jean Delvaree7065e22012-04-28 15:32:06 +020040 i2c_mux_gpio_set(mux, mux->data.values[chan]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010041
42 return 0;
43}
44
Jean Delvaree7065e22012-04-28 15:32:06 +020045static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010046{
47 struct gpiomux *mux = data;
48
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
Jean Delvaree7ee5142012-10-05 22:23:54 +020054static int __devinit match_gpio_chip_by_label(struct gpio_chip *chip,
55 void *data)
56{
57 return !strcmp(chip->label, data);
58}
59
Jean Delvaree7065e22012-04-28 15:32:06 +020060static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010061{
62 struct gpiomux *mux;
Jean Delvaree7065e22012-04-28 15:32:06 +020063 struct i2c_mux_gpio_platform_data *pdata;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010064 struct i2c_adapter *parent;
65 int (*deselect) (struct i2c_adapter *, void *, u32);
Jean Delvaree7ee5142012-10-05 22:23:54 +020066 unsigned initial_state, gpio_base;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010067 int i, ret;
68
69 pdata = pdev->dev.platform_data;
70 if (!pdata) {
71 dev_err(&pdev->dev, "Missing platform data\n");
72 return -ENODEV;
73 }
74
Jean Delvaree7ee5142012-10-05 22:23:54 +020075 /*
76 * If a GPIO chip name is provided, the GPIO pin numbers provided are
77 * relative to its base GPIO number. Otherwise they are absolute.
78 */
79 if (pdata->gpio_chip) {
80 struct gpio_chip *gpio;
81
82 gpio = gpiochip_find(pdata->gpio_chip,
83 match_gpio_chip_by_label);
84 if (!gpio)
85 return -EPROBE_DEFER;
86
87 gpio_base = gpio->base;
88 } else {
89 gpio_base = 0;
90 }
91
Peter Korsgaard92ed1a72011-01-10 22:11:23 +010092 parent = i2c_get_adapter(pdata->parent);
93 if (!parent) {
94 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
95 pdata->parent);
96 return -ENODEV;
97 }
98
Maxime Ripard2614a852012-10-05 22:23:53 +020099 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100100 if (!mux) {
101 ret = -ENOMEM;
102 goto alloc_failed;
103 }
104
105 mux->parent = parent;
106 mux->data = *pdata;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200107 mux->gpio_base = gpio_base;
Maxime Ripard2614a852012-10-05 22:23:53 +0200108 mux->adap = devm_kzalloc(&pdev->dev,
109 sizeof(*mux->adap) * pdata->n_values,
110 GFP_KERNEL);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100111 if (!mux->adap) {
112 ret = -ENOMEM;
Maxime Ripard2614a852012-10-05 22:23:53 +0200113 goto alloc_failed;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100114 }
115
Jean Delvaree7065e22012-04-28 15:32:06 +0200116 if (pdata->idle != I2C_MUX_GPIO_NO_IDLE) {
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100117 initial_state = pdata->idle;
Jean Delvaree7065e22012-04-28 15:32:06 +0200118 deselect = i2c_mux_gpio_deselect;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100119 } else {
120 initial_state = pdata->values[0];
121 deselect = NULL;
122 }
123
124 for (i = 0; i < pdata->n_gpios; i++) {
Jean Delvaree7ee5142012-10-05 22:23:54 +0200125 ret = gpio_request(gpio_base + pdata->gpios[i], "i2c-mux-gpio");
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100126 if (ret)
127 goto err_request_gpio;
Jean Delvaree7ee5142012-10-05 22:23:54 +0200128 gpio_direction_output(gpio_base + pdata->gpios[i],
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100129 initial_state & (1 << i));
130 }
131
132 for (i = 0; i < pdata->n_values; i++) {
133 u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0;
Jean Delvareeee543e2012-10-05 22:23:51 +0200134 unsigned int class = pdata->classes ? pdata->classes[i] : 0;
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100135
Jean Delvareeee543e2012-10-05 22:23:51 +0200136 mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
137 i, class,
Jean Delvaree7065e22012-04-28 15:32:06 +0200138 i2c_mux_gpio_select, deselect);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100139 if (!mux->adap[i]) {
140 ret = -ENODEV;
141 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
142 goto add_adapter_failed;
143 }
144 }
145
146 dev_info(&pdev->dev, "%d port mux on %s adapter\n",
147 pdata->n_values, parent->name);
148
149 platform_set_drvdata(pdev, mux);
150
151 return 0;
152
153add_adapter_failed:
154 for (; i > 0; i--)
155 i2c_del_mux_adapter(mux->adap[i - 1]);
156 i = pdata->n_gpios;
157err_request_gpio:
158 for (; i > 0; i--)
Jean Delvaree7ee5142012-10-05 22:23:54 +0200159 gpio_free(gpio_base + pdata->gpios[i - 1]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100160alloc_failed:
161 i2c_put_adapter(parent);
162
163 return ret;
164}
165
Jean Delvaree7065e22012-04-28 15:32:06 +0200166static int __devexit i2c_mux_gpio_remove(struct platform_device *pdev)
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100167{
168 struct gpiomux *mux = platform_get_drvdata(pdev);
169 int i;
170
171 for (i = 0; i < mux->data.n_values; i++)
172 i2c_del_mux_adapter(mux->adap[i]);
173
174 for (i = 0; i < mux->data.n_gpios; i++)
Jean Delvaree7ee5142012-10-05 22:23:54 +0200175 gpio_free(mux->gpio_base + mux->data.gpios[i]);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100176
177 platform_set_drvdata(pdev, NULL);
178 i2c_put_adapter(mux->parent);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100179
180 return 0;
181}
182
Jean Delvaree7065e22012-04-28 15:32:06 +0200183static struct platform_driver i2c_mux_gpio_driver = {
184 .probe = i2c_mux_gpio_probe,
185 .remove = __devexit_p(i2c_mux_gpio_remove),
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100186 .driver = {
187 .owner = THIS_MODULE,
Jean Delvaree7065e22012-04-28 15:32:06 +0200188 .name = "i2c-mux-gpio",
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100189 },
190};
191
Jean Delvaree7065e22012-04-28 15:32:06 +0200192module_platform_driver(i2c_mux_gpio_driver);
Peter Korsgaard92ed1a72011-01-10 22:11:23 +0100193
194MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
195MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
196MODULE_LICENSE("GPL");
Jean Delvaree7065e22012-04-28 15:32:06 +0200197MODULE_ALIAS("platform:i2c-mux-gpio");