1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
15 DECLARE_GLOBAL_DATA_PTR;
17 struct i2c_arbitrator_priv {
18 struct gpio_desc ap_claim;
19 struct gpio_desc ec_claim;
25 int i2c_arbitrator_deselect(struct udevice *mux, struct udevice *bus,
28 struct i2c_arbitrator_priv *priv = dev_get_priv(mux);
31 debug("%s: %s\n", __func__, mux->name);
32 ret = dm_gpio_set_value(&priv->ap_claim, 0);
33 udelay(priv->slew_delay_us);
38 int i2c_arbitrator_select(struct udevice *mux, struct udevice *bus,
41 struct i2c_arbitrator_priv *priv = dev_get_priv(mux);
45 debug("%s: %s\n", __func__, mux->name);
46 /* Start a round of trying to claim the bus */
52 /* Indicate that we want to claim the bus */
53 ret = dm_gpio_set_value(&priv->ap_claim, 1);
56 udelay(priv->slew_delay_us);
58 /* Wait for the EC to release it */
59 start_retry = get_timer(0);
60 while (get_timer(start_retry) < priv->wait_retry_ms) {
61 ret = dm_gpio_get_value(&priv->ec_claim);
65 /* We got it, so return */
73 /* It didn't release, so give up, wait, and try again */
74 ret = dm_gpio_set_value(&priv->ap_claim, 0);
78 mdelay(priv->wait_retry_ms);
79 } while (get_timer(start) < priv->wait_free_ms);
81 /* Give up, release our claim */
82 printf("I2C: Could not claim bus, timeout %lu\n", get_timer(start));
89 static int i2c_arbitrator_probe(struct udevice *dev)
91 struct i2c_arbitrator_priv *priv = dev_get_priv(dev);
92 const void *blob = gd->fdt_blob;
93 int node = dev_of_offset(dev);
96 debug("%s: %s\n", __func__, dev->name);
97 priv->slew_delay_us = fdtdec_get_int(blob, node, "slew-delay-us", 0);
98 priv->wait_retry_ms = fdtdec_get_int(blob, node, "wait-retry-us", 0) /
100 priv->wait_free_ms = fdtdec_get_int(blob, node, "wait-free-us", 0) /
102 ret = gpio_request_by_name(dev, "our-claim-gpio", 0, &priv->ap_claim,
106 ret = gpio_request_by_name(dev, "their-claim-gpios", 0, &priv->ec_claim,
114 dm_gpio_free(dev, &priv->ap_claim);
116 debug("%s: ret=%d\n", __func__, ret);
120 static int i2c_arbitrator_remove(struct udevice *dev)
122 struct i2c_arbitrator_priv *priv = dev_get_priv(dev);
124 dm_gpio_free(dev, &priv->ap_claim);
125 dm_gpio_free(dev, &priv->ec_claim);
130 static const struct i2c_mux_ops i2c_arbitrator_ops = {
131 .select = i2c_arbitrator_select,
132 .deselect = i2c_arbitrator_deselect,
135 static const struct udevice_id i2c_arbitrator_ids[] = {
136 { .compatible = "i2c-arb-gpio-challenge" },
140 U_BOOT_DRIVER(i2c_arbitrator) = {
141 .name = "i2c_arbitrator",
142 .id = UCLASS_I2C_MUX,
143 .of_match = i2c_arbitrator_ids,
144 .probe = i2c_arbitrator_probe,
145 .remove = i2c_arbitrator_remove,
146 .ops = &i2c_arbitrator_ops,
147 .priv_auto_alloc_size = sizeof(struct i2c_arbitrator_priv),