rt2800: initialize BBP_R104 on proper subroutines
[linux/fpc-iii.git] / drivers / i2c / muxes / i2c-arb-gpio-challenge.c
blob210b6f7b9028af89036341b02de8f7251a7ca996
1 /*
2 * GPIO-based I2C Arbitration Using a Challenge & Response Mechanism
4 * Copyright (C) 2012 Google, Inc
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/i2c-mux.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/of_i2c.h>
25 #include <linux/of_gpio.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
30 /**
31 * struct i2c_arbitrator_data - Driver data for I2C arbitrator
33 * @parent: Parent adapter
34 * @child: Child bus
35 * @our_gpio: GPIO we'll use to claim.
36 * @our_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
37 * this then consider it released.
38 * @their_gpio: GPIO that the other side will use to claim.
39 * @their_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
40 * this then consider it released.
41 * @slew_delay_us: microseconds to wait for a GPIO to go high.
42 * @wait_retry_us: we'll attempt another claim after this many microseconds.
43 * @wait_free_us: we'll give up after this many microseconds.
46 struct i2c_arbitrator_data {
47 struct i2c_adapter *parent;
48 struct i2c_adapter *child;
49 int our_gpio;
50 int our_gpio_release;
51 int their_gpio;
52 int their_gpio_release;
53 unsigned int slew_delay_us;
54 unsigned int wait_retry_us;
55 unsigned int wait_free_us;
59 /**
60 * i2c_arbitrator_select - claim the I2C bus
62 * Use the GPIO-based signalling protocol; return -EBUSY if we fail.
64 static int i2c_arbitrator_select(struct i2c_adapter *adap, void *data, u32 chan)
66 const struct i2c_arbitrator_data *arb = data;
67 unsigned long stop_retry, stop_time;
69 /* Start a round of trying to claim the bus */
70 stop_time = jiffies + usecs_to_jiffies(arb->wait_free_us) + 1;
71 do {
72 /* Indicate that we want to claim the bus */
73 gpio_set_value(arb->our_gpio, !arb->our_gpio_release);
74 udelay(arb->slew_delay_us);
76 /* Wait for the other master to release it */
77 stop_retry = jiffies + usecs_to_jiffies(arb->wait_retry_us) + 1;
78 while (time_before(jiffies, stop_retry)) {
79 int gpio_val = !!gpio_get_value(arb->their_gpio);
81 if (gpio_val == arb->their_gpio_release) {
82 /* We got it, so return */
83 return 0;
86 usleep_range(50, 200);
89 /* It didn't release, so give up, wait, and try again */
90 gpio_set_value(arb->our_gpio, arb->our_gpio_release);
92 usleep_range(arb->wait_retry_us, arb->wait_retry_us * 2);
93 } while (time_before(jiffies, stop_time));
95 /* Give up, release our claim */
96 gpio_set_value(arb->our_gpio, arb->our_gpio_release);
97 udelay(arb->slew_delay_us);
98 dev_err(&adap->dev, "Could not claim bus, timeout\n");
99 return -EBUSY;
103 * i2c_arbitrator_deselect - release the I2C bus
105 * Release the I2C bus using the GPIO-based signalling protocol.
107 static int i2c_arbitrator_deselect(struct i2c_adapter *adap, void *data,
108 u32 chan)
110 const struct i2c_arbitrator_data *arb = data;
112 /* Release the bus and wait for the other master to notice */
113 gpio_set_value(arb->our_gpio, arb->our_gpio_release);
114 udelay(arb->slew_delay_us);
116 return 0;
119 static int i2c_arbitrator_probe(struct platform_device *pdev)
121 struct device *dev = &pdev->dev;
122 struct device_node *np = dev->of_node;
123 struct device_node *parent_np;
124 struct i2c_arbitrator_data *arb;
125 enum of_gpio_flags gpio_flags;
126 unsigned long out_init;
127 int ret;
129 /* We only support probing from device tree; no platform_data */
130 if (!np) {
131 dev_err(dev, "Cannot find device tree node\n");
132 return -ENODEV;
134 if (dev->platform_data) {
135 dev_err(dev, "Platform data is not supported\n");
136 return -EINVAL;
139 arb = devm_kzalloc(dev, sizeof(*arb), GFP_KERNEL);
140 if (!arb) {
141 dev_err(dev, "Cannot allocate i2c_arbitrator_data\n");
142 return -ENOMEM;
144 platform_set_drvdata(pdev, arb);
146 /* Request GPIOs */
147 ret = of_get_named_gpio_flags(np, "our-claim-gpio", 0, &gpio_flags);
148 if (!gpio_is_valid(ret)) {
149 if (ret != -EPROBE_DEFER)
150 dev_err(dev, "Error getting our-claim-gpio\n");
151 return ret;
153 arb->our_gpio = ret;
154 arb->our_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW);
155 out_init = (gpio_flags & OF_GPIO_ACTIVE_LOW) ?
156 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
157 ret = devm_gpio_request_one(dev, arb->our_gpio, out_init,
158 "our-claim-gpio");
159 if (ret) {
160 if (ret != -EPROBE_DEFER)
161 dev_err(dev, "Error requesting our-claim-gpio\n");
162 return ret;
165 ret = of_get_named_gpio_flags(np, "their-claim-gpios", 0, &gpio_flags);
166 if (!gpio_is_valid(ret)) {
167 if (ret != -EPROBE_DEFER)
168 dev_err(dev, "Error getting their-claim-gpio\n");
169 return ret;
171 arb->their_gpio = ret;
172 arb->their_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW);
173 ret = devm_gpio_request_one(dev, arb->their_gpio, GPIOF_IN,
174 "their-claim-gpio");
175 if (ret) {
176 if (ret != -EPROBE_DEFER)
177 dev_err(dev, "Error requesting their-claim-gpio\n");
178 return ret;
181 /* At the moment we only support a single two master (us + 1 other) */
182 if (gpio_is_valid(of_get_named_gpio(np, "their-claim-gpios", 1))) {
183 dev_err(dev, "Only one other master is supported\n");
184 return -EINVAL;
187 /* Arbitration parameters */
188 if (of_property_read_u32(np, "slew-delay-us", &arb->slew_delay_us))
189 arb->slew_delay_us = 10;
190 if (of_property_read_u32(np, "wait-retry-us", &arb->wait_retry_us))
191 arb->wait_retry_us = 3000;
192 if (of_property_read_u32(np, "wait-free-us", &arb->wait_free_us))
193 arb->wait_free_us = 50000;
195 /* Find our parent */
196 parent_np = of_parse_phandle(np, "i2c-parent", 0);
197 if (!parent_np) {
198 dev_err(dev, "Cannot parse i2c-parent\n");
199 return -EINVAL;
201 arb->parent = of_find_i2c_adapter_by_node(parent_np);
202 if (!arb->parent) {
203 dev_err(dev, "Cannot find parent bus\n");
204 return -EINVAL;
207 /* Actually add the mux adapter */
208 arb->child = i2c_add_mux_adapter(arb->parent, dev, arb, 0, 0, 0,
209 i2c_arbitrator_select,
210 i2c_arbitrator_deselect);
211 if (!arb->child) {
212 dev_err(dev, "Failed to add adapter\n");
213 ret = -ENODEV;
214 i2c_put_adapter(arb->parent);
217 return ret;
220 static int i2c_arbitrator_remove(struct platform_device *pdev)
222 struct i2c_arbitrator_data *arb = platform_get_drvdata(pdev);
224 i2c_del_mux_adapter(arb->child);
225 i2c_put_adapter(arb->parent);
227 return 0;
230 static const struct of_device_id i2c_arbitrator_of_match[] = {
231 { .compatible = "i2c-arb-gpio-challenge", },
234 MODULE_DEVICE_TABLE(of, i2c_arbitrator_of_match);
236 static struct platform_driver i2c_arbitrator_driver = {
237 .probe = i2c_arbitrator_probe,
238 .remove = i2c_arbitrator_remove,
239 .driver = {
240 .owner = THIS_MODULE,
241 .name = "i2c-arb-gpio-challenge",
242 .of_match_table = of_match_ptr(i2c_arbitrator_of_match),
246 module_platform_driver(i2c_arbitrator_driver);
248 MODULE_DESCRIPTION("GPIO-based I2C Arbitration");
249 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
250 MODULE_LICENSE("GPL v2");
251 MODULE_ALIAS("platform:i2c-arb-gpio-challenge");