^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * GPIO-based I2C Arbitration Using a Challenge & Response Mechanism
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Google, Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c-mux.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * struct i2c_arbitrator_data - Driver data for I2C arbitrator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @our_gpio: GPIO descriptor we'll use to claim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @their_gpio: GPIO descriptor that the other side will use to claim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @slew_delay_us: microseconds to wait for a GPIO to go high.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @wait_retry_us: we'll attempt another claim after this many microseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @wait_free_us: we'll give up after this many microseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct i2c_arbitrator_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct gpio_desc *our_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct gpio_desc *their_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int slew_delay_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int wait_retry_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int wait_free_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * i2c_arbitrator_select - claim the I2C bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Use the GPIO-based signalling protocol; return -EBUSY if we fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int i2c_arbitrator_select(struct i2c_mux_core *muxc, u32 chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) const struct i2c_arbitrator_data *arb = i2c_mux_priv(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long stop_retry, stop_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Start a round of trying to claim the bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) stop_time = jiffies + usecs_to_jiffies(arb->wait_free_us) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Indicate that we want to claim the bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) gpiod_set_value(arb->our_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) udelay(arb->slew_delay_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Wait for the other master to release it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) stop_retry = jiffies + usecs_to_jiffies(arb->wait_retry_us) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) while (time_before(jiffies, stop_retry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int gpio_val = gpiod_get_value(arb->their_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!gpio_val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* We got it, so return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) usleep_range(50, 200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* It didn't release, so give up, wait, and try again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) gpiod_set_value(arb->our_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) usleep_range(arb->wait_retry_us, arb->wait_retry_us * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) } while (time_before(jiffies, stop_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* Give up, release our claim */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) gpiod_set_value(arb->our_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) udelay(arb->slew_delay_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dev_err(muxc->dev, "Could not claim bus, timeout\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * i2c_arbitrator_deselect - release the I2C bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Release the I2C bus using the GPIO-based signalling protocol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int i2c_arbitrator_deselect(struct i2c_mux_core *muxc, u32 chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) const struct i2c_arbitrator_data *arb = i2c_mux_priv(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Release the bus and wait for the other master to notice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) gpiod_set_value(arb->our_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) udelay(arb->slew_delay_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int i2c_arbitrator_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct device_node *parent_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct i2c_mux_core *muxc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct i2c_arbitrator_data *arb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct gpio_desc *dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* We only support probing from device tree; no platform_data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dev_err(dev, "Cannot find device tree node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (dev_get_platdata(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dev_err(dev, "Platform data is not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) muxc = i2c_mux_alloc(NULL, dev, 1, sizeof(*arb), I2C_MUX_ARBITRATOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) i2c_arbitrator_select, i2c_arbitrator_deselect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!muxc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) arb = i2c_mux_priv(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) platform_set_drvdata(pdev, muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Request GPIOs, our GPIO as unclaimed to begin with */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) arb->our_gpio = devm_gpiod_get(dev, "our-claim", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (IS_ERR(arb->our_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dev_err(dev, "could not get \"our-claim\" GPIO (%ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) PTR_ERR(arb->our_gpio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return PTR_ERR(arb->our_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) arb->their_gpio = devm_gpiod_get(dev, "their-claim", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (IS_ERR(arb->their_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev_err(dev, "could not get \"their-claim\" GPIO (%ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) PTR_ERR(arb->their_gpio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return PTR_ERR(arb->their_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* At the moment we only support a single two master (us + 1 other) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) dummy = devm_gpiod_get_index(dev, "their-claim", 1, GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!IS_ERR(dummy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) dev_err(dev, "Only one other master is supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) } else if (PTR_ERR(dummy) == -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Arbitration parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (of_property_read_u32(np, "slew-delay-us", &arb->slew_delay_us))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) arb->slew_delay_us = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (of_property_read_u32(np, "wait-retry-us", &arb->wait_retry_us))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) arb->wait_retry_us = 3000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (of_property_read_u32(np, "wait-free-us", &arb->wait_free_us))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) arb->wait_free_us = 50000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Find our parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) parent_np = of_parse_phandle(np, "i2c-parent", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!parent_np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dev_err(dev, "Cannot parse i2c-parent\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) muxc->parent = of_get_i2c_adapter_by_node(parent_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) of_node_put(parent_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!muxc->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev_err(dev, "Cannot find parent bus\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Actually add the mux adapter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = i2c_mux_add_adapter(muxc, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) i2c_put_adapter(muxc->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int i2c_arbitrator_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) i2c_mux_del_adapters(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) i2c_put_adapter(muxc->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static const struct of_device_id i2c_arbitrator_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) { .compatible = "i2c-arb-gpio-challenge", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) MODULE_DEVICE_TABLE(of, i2c_arbitrator_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static struct platform_driver i2c_arbitrator_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .probe = i2c_arbitrator_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .remove = i2c_arbitrator_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .name = "i2c-arb-gpio-challenge",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .of_match_table = i2c_arbitrator_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) module_platform_driver(i2c_arbitrator_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) MODULE_DESCRIPTION("GPIO-based I2C Arbitration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) MODULE_ALIAS("platform:i2c-arb-gpio-challenge");