.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * GPIO-based I2C Arbitration Using a Challenge & Response Mechanism |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2012 Google, Inc |
---|
5 | | - * |
---|
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. |
---|
9 | | - * |
---|
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. |
---|
14 | | - * |
---|
15 | 6 | */ |
---|
16 | 7 | |
---|
17 | 8 | #include <linux/delay.h> |
---|
18 | | -#include <linux/gpio.h> |
---|
| 9 | +#include <linux/gpio/consumer.h> |
---|
19 | 10 | #include <linux/kernel.h> |
---|
20 | 11 | #include <linux/i2c.h> |
---|
21 | 12 | #include <linux/i2c-mux.h> |
---|
22 | 13 | #include <linux/module.h> |
---|
23 | | -#include <linux/of_gpio.h> |
---|
24 | 14 | #include <linux/platform_device.h> |
---|
25 | 15 | #include <linux/slab.h> |
---|
26 | 16 | |
---|
.. | .. |
---|
28 | 18 | /** |
---|
29 | 19 | * struct i2c_arbitrator_data - Driver data for I2C arbitrator |
---|
30 | 20 | * |
---|
31 | | - * @our_gpio: GPIO we'll use to claim. |
---|
32 | | - * @our_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO == |
---|
33 | | - * this then consider it released. |
---|
34 | | - * @their_gpio: GPIO that the other side will use to claim. |
---|
35 | | - * @their_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO == |
---|
36 | | - * this then consider it released. |
---|
| 21 | + * @our_gpio: GPIO descriptor we'll use to claim. |
---|
| 22 | + * @their_gpio: GPIO descriptor that the other side will use to claim. |
---|
37 | 23 | * @slew_delay_us: microseconds to wait for a GPIO to go high. |
---|
38 | 24 | * @wait_retry_us: we'll attempt another claim after this many microseconds. |
---|
39 | 25 | * @wait_free_us: we'll give up after this many microseconds. |
---|
40 | 26 | */ |
---|
41 | 27 | |
---|
42 | 28 | struct i2c_arbitrator_data { |
---|
43 | | - int our_gpio; |
---|
44 | | - int our_gpio_release; |
---|
45 | | - int their_gpio; |
---|
46 | | - int their_gpio_release; |
---|
| 29 | + struct gpio_desc *our_gpio; |
---|
| 30 | + struct gpio_desc *their_gpio; |
---|
47 | 31 | unsigned int slew_delay_us; |
---|
48 | 32 | unsigned int wait_retry_us; |
---|
49 | 33 | unsigned int wait_free_us; |
---|
.. | .. |
---|
64 | 48 | stop_time = jiffies + usecs_to_jiffies(arb->wait_free_us) + 1; |
---|
65 | 49 | do { |
---|
66 | 50 | /* Indicate that we want to claim the bus */ |
---|
67 | | - gpio_set_value(arb->our_gpio, !arb->our_gpio_release); |
---|
| 51 | + gpiod_set_value(arb->our_gpio, 1); |
---|
68 | 52 | udelay(arb->slew_delay_us); |
---|
69 | 53 | |
---|
70 | 54 | /* Wait for the other master to release it */ |
---|
71 | 55 | stop_retry = jiffies + usecs_to_jiffies(arb->wait_retry_us) + 1; |
---|
72 | 56 | while (time_before(jiffies, stop_retry)) { |
---|
73 | | - int gpio_val = !!gpio_get_value(arb->their_gpio); |
---|
| 57 | + int gpio_val = gpiod_get_value(arb->their_gpio); |
---|
74 | 58 | |
---|
75 | | - if (gpio_val == arb->their_gpio_release) { |
---|
| 59 | + if (!gpio_val) { |
---|
76 | 60 | /* We got it, so return */ |
---|
77 | 61 | return 0; |
---|
78 | 62 | } |
---|
.. | .. |
---|
81 | 65 | } |
---|
82 | 66 | |
---|
83 | 67 | /* It didn't release, so give up, wait, and try again */ |
---|
84 | | - gpio_set_value(arb->our_gpio, arb->our_gpio_release); |
---|
| 68 | + gpiod_set_value(arb->our_gpio, 0); |
---|
85 | 69 | |
---|
86 | 70 | usleep_range(arb->wait_retry_us, arb->wait_retry_us * 2); |
---|
87 | 71 | } while (time_before(jiffies, stop_time)); |
---|
88 | 72 | |
---|
89 | 73 | /* Give up, release our claim */ |
---|
90 | | - gpio_set_value(arb->our_gpio, arb->our_gpio_release); |
---|
| 74 | + gpiod_set_value(arb->our_gpio, 0); |
---|
91 | 75 | udelay(arb->slew_delay_us); |
---|
92 | 76 | dev_err(muxc->dev, "Could not claim bus, timeout\n"); |
---|
93 | 77 | return -EBUSY; |
---|
.. | .. |
---|
103 | 87 | const struct i2c_arbitrator_data *arb = i2c_mux_priv(muxc); |
---|
104 | 88 | |
---|
105 | 89 | /* Release the bus and wait for the other master to notice */ |
---|
106 | | - gpio_set_value(arb->our_gpio, arb->our_gpio_release); |
---|
| 90 | + gpiod_set_value(arb->our_gpio, 0); |
---|
107 | 91 | udelay(arb->slew_delay_us); |
---|
108 | 92 | |
---|
109 | 93 | return 0; |
---|
.. | .. |
---|
116 | 100 | struct device_node *parent_np; |
---|
117 | 101 | struct i2c_mux_core *muxc; |
---|
118 | 102 | struct i2c_arbitrator_data *arb; |
---|
119 | | - enum of_gpio_flags gpio_flags; |
---|
120 | | - unsigned long out_init; |
---|
| 103 | + struct gpio_desc *dummy; |
---|
121 | 104 | int ret; |
---|
122 | 105 | |
---|
123 | 106 | /* We only support probing from device tree; no platform_data */ |
---|
.. | .. |
---|
138 | 121 | |
---|
139 | 122 | platform_set_drvdata(pdev, muxc); |
---|
140 | 123 | |
---|
141 | | - /* Request GPIOs */ |
---|
142 | | - ret = of_get_named_gpio_flags(np, "our-claim-gpio", 0, &gpio_flags); |
---|
143 | | - if (!gpio_is_valid(ret)) { |
---|
144 | | - if (ret != -EPROBE_DEFER) |
---|
145 | | - dev_err(dev, "Error getting our-claim-gpio\n"); |
---|
146 | | - return ret; |
---|
147 | | - } |
---|
148 | | - arb->our_gpio = ret; |
---|
149 | | - arb->our_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW); |
---|
150 | | - out_init = (gpio_flags & OF_GPIO_ACTIVE_LOW) ? |
---|
151 | | - GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW; |
---|
152 | | - ret = devm_gpio_request_one(dev, arb->our_gpio, out_init, |
---|
153 | | - "our-claim-gpio"); |
---|
154 | | - if (ret) { |
---|
155 | | - if (ret != -EPROBE_DEFER) |
---|
156 | | - dev_err(dev, "Error requesting our-claim-gpio\n"); |
---|
157 | | - return ret; |
---|
| 124 | + /* Request GPIOs, our GPIO as unclaimed to begin with */ |
---|
| 125 | + arb->our_gpio = devm_gpiod_get(dev, "our-claim", GPIOD_OUT_LOW); |
---|
| 126 | + if (IS_ERR(arb->our_gpio)) { |
---|
| 127 | + dev_err(dev, "could not get \"our-claim\" GPIO (%ld)\n", |
---|
| 128 | + PTR_ERR(arb->our_gpio)); |
---|
| 129 | + return PTR_ERR(arb->our_gpio); |
---|
158 | 130 | } |
---|
159 | 131 | |
---|
160 | | - ret = of_get_named_gpio_flags(np, "their-claim-gpios", 0, &gpio_flags); |
---|
161 | | - if (!gpio_is_valid(ret)) { |
---|
162 | | - if (ret != -EPROBE_DEFER) |
---|
163 | | - dev_err(dev, "Error getting their-claim-gpio\n"); |
---|
164 | | - return ret; |
---|
165 | | - } |
---|
166 | | - arb->their_gpio = ret; |
---|
167 | | - arb->their_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW); |
---|
168 | | - ret = devm_gpio_request_one(dev, arb->their_gpio, GPIOF_IN, |
---|
169 | | - "their-claim-gpio"); |
---|
170 | | - if (ret) { |
---|
171 | | - if (ret != -EPROBE_DEFER) |
---|
172 | | - dev_err(dev, "Error requesting their-claim-gpio\n"); |
---|
173 | | - return ret; |
---|
| 132 | + arb->their_gpio = devm_gpiod_get(dev, "their-claim", GPIOD_IN); |
---|
| 133 | + if (IS_ERR(arb->their_gpio)) { |
---|
| 134 | + dev_err(dev, "could not get \"their-claim\" GPIO (%ld)\n", |
---|
| 135 | + PTR_ERR(arb->their_gpio)); |
---|
| 136 | + return PTR_ERR(arb->their_gpio); |
---|
174 | 137 | } |
---|
175 | 138 | |
---|
176 | 139 | /* At the moment we only support a single two master (us + 1 other) */ |
---|
177 | | - if (gpio_is_valid(of_get_named_gpio(np, "their-claim-gpios", 1))) { |
---|
| 140 | + dummy = devm_gpiod_get_index(dev, "their-claim", 1, GPIOD_IN); |
---|
| 141 | + if (!IS_ERR(dummy)) { |
---|
178 | 142 | dev_err(dev, "Only one other master is supported\n"); |
---|
179 | 143 | return -EINVAL; |
---|
| 144 | + } else if (PTR_ERR(dummy) == -EPROBE_DEFER) { |
---|
| 145 | + return -EPROBE_DEFER; |
---|
180 | 146 | } |
---|
181 | 147 | |
---|
182 | 148 | /* Arbitration parameters */ |
---|