1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
13 DECLARE_GLOBAL_DATA_PTR;
15 struct i2c_arbitrator_priv {
16 struct gpio_desc ap_claim;
17 struct gpio_desc ec_claim;
23 int i2c_arbitrator_deselect(struct udevice *mux, struct udevice *bus,
26 struct i2c_arbitrator_priv *priv = dev_get_priv(mux);
29 debug("%s: %s\n", __func__, mux->name);
30 ret = dm_gpio_set_value(&priv->ap_claim, 0);
31 udelay(priv->slew_delay_us);
36 int i2c_arbitrator_select(struct udevice *mux, struct udevice *bus,
39 struct i2c_arbitrator_priv *priv = dev_get_priv(mux);
43 debug("%s: %s\n", __func__, mux->name);
44 /* Start a round of trying to claim the bus */
50 /* Indicate that we want to claim the bus */
51 ret = dm_gpio_set_value(&priv->ap_claim, 1);
54 udelay(priv->slew_delay_us);
56 /* Wait for the EC to release it */
57 start_retry = get_timer(0);
58 while (get_timer(start_retry) < priv->wait_retry_ms) {
59 ret = dm_gpio_get_value(&priv->ec_claim);
63 /* We got it, so return */
71 /* It didn't release, so give up, wait, and try again */
72 ret = dm_gpio_set_value(&priv->ap_claim, 0);
76 mdelay(priv->wait_retry_ms);
77 } while (get_timer(start) < priv->wait_free_ms);
79 /* Give up, release our claim */
80 printf("I2C: Could not claim bus, timeout %lu\n", get_timer(start));
87 static int i2c_arbitrator_probe(struct udevice *dev)
89 struct i2c_arbitrator_priv *priv = dev_get_priv(dev);
90 const void *blob = gd->fdt_blob;
91 int node = dev_of_offset(dev);
94 debug("%s: %s\n", __func__, dev->name);
95 priv->slew_delay_us = fdtdec_get_int(blob, node, "slew-delay-us", 0);
96 priv->wait_retry_ms = fdtdec_get_int(blob, node, "wait-retry-us", 0) /
98 priv->wait_free_ms = fdtdec_get_int(blob, node, "wait-free-us", 0) /
100 ret = gpio_request_by_name(dev, "our-claim-gpio", 0, &priv->ap_claim,
104 ret = gpio_request_by_name(dev, "their-claim-gpios", 0, &priv->ec_claim,
112 dm_gpio_free(dev, &priv->ap_claim);
114 debug("%s: ret=%d\n", __func__, ret);
118 static int i2c_arbitrator_remove(struct udevice *dev)
120 struct i2c_arbitrator_priv *priv = dev_get_priv(dev);
122 dm_gpio_free(dev, &priv->ap_claim);
123 dm_gpio_free(dev, &priv->ec_claim);
128 static const struct i2c_mux_ops i2c_arbitrator_ops = {
129 .select = i2c_arbitrator_select,
130 .deselect = i2c_arbitrator_deselect,
133 static const struct udevice_id i2c_arbitrator_ids[] = {
134 { .compatible = "i2c-arb-gpio-challenge" },
138 U_BOOT_DRIVER(i2c_arbitrator) = {
139 .name = "i2c_arbitrator",
140 .id = UCLASS_I2C_MUX,
141 .of_match = i2c_arbitrator_ids,
142 .probe = i2c_arbitrator_probe,
143 .remove = i2c_arbitrator_remove,
144 .ops = &i2c_arbitrator_ops,
145 .priv_auto_alloc_size = sizeof(struct i2c_arbitrator_priv),