···2233Required properties:44 - compatible = "i2c-gpio";55- - gpios: sda and scl gpio66-55+ - sda-gpios: gpio used for the sda signal, this should be flagged as66+ active high using open drain with (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)77+ from <dt-bindings/gpio/gpio.h> since the signal is by definition88+ open drain.99+ - scl-gpios: gpio used for the scl signal, this should be flagged as1010+ active high using open drain with (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)1111+ from <dt-bindings/gpio/gpio.h> since the signal is by definition1212+ open drain.713814Optional properties:99- - i2c-gpio,sda-open-drain: sda as open drain1010- - i2c-gpio,scl-open-drain: scl as open drain1115 - i2c-gpio,scl-output-only: scl as output only1216 - i2c-gpio,delay-us: delay between GPIO operations (may depend on each platform)1317 - i2c-gpio,timeout-ms: timeout to get data14181919+Deprecated properties, do not use in new device tree sources:2020+ - gpios: sda and scl gpio, alternative for {sda,scl}-gpios2121+ - i2c-gpio,sda-open-drain: this means that something outside of our2222+ control has put the GPIO line used for SDA into open drain mode, and2323+ that something is not the GPIO chip. It is essentially an2424+ inconsistency flag.2525+ - i2c-gpio,scl-open-drain: this means that something outside of our2626+ control has put the GPIO line used for SCL into open drain mode, and2727+ that something is not the GPIO chip. It is essentially an2828+ inconsistency flag.2929+1530Example nodes:3131+3232+#include <dt-bindings/gpio/gpio.h>16331734i2c@0 {1835 compatible = "i2c-gpio";1919- gpios = <&pioA 23 0 /* sda */2020- &pioA 24 0 /* scl */2121- >;2222- i2c-gpio,sda-open-drain;2323- i2c-gpio,scl-open-drain;3636+ sda-gpios = <&pioA 23 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>;3737+ scl-gpios = <&pioA 24 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>;2438 i2c-gpio,delay-us = <2>; /* ~100 kHz */2539 #address-cells = <1>;2640 #size-cells = <0>;
+23-18
arch/arm/mach-ep93xx/core.c
···3131#include <linux/amba/serial.h>3232#include <linux/mtd/physmap.h>3333#include <linux/i2c.h>3434-#include <linux/i2c-gpio.h>3434+#include <linux/gpio/machine.h>3535#include <linux/spi/spi.h>3636#include <linux/export.h>3737#include <linux/irqchip/arm-vic.h>···320320/*************************************************************************321321 * EP93xx i2c peripheral handling322322 *************************************************************************/323323-static struct i2c_gpio_platform_data ep93xx_i2c_data;323323+324324+/* All EP93xx devices use the same two GPIO pins for I2C bit-banging */325325+static struct gpiod_lookup_table ep93xx_i2c_gpiod_table = {326326+ .dev_id = "i2c-gpio",327327+ .table = {328328+ /* Use local offsets on gpiochip/port "G" */329329+ GPIO_LOOKUP_IDX("G", 1, NULL, 0,330330+ GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),331331+ GPIO_LOOKUP_IDX("G", 0, NULL, 1,332332+ GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),333333+ },334334+};324335325336static struct platform_device ep93xx_i2c_device = {326337 .name = "i2c-gpio",327338 .id = 0,328339 .dev = {329329- .platform_data = &ep93xx_i2c_data,340340+ .platform_data = NULL,330341 },331342};332343333344/**334345 * ep93xx_register_i2c - Register the i2c platform device.335335- * @data: platform specific i2c-gpio configuration (__initdata)336346 * @devices: platform specific i2c bus device information (__initdata)337347 * @num: the number of devices on the i2c bus338348 */339339-void __init ep93xx_register_i2c(struct i2c_gpio_platform_data *data,340340- struct i2c_board_info *devices, int num)349349+void __init ep93xx_register_i2c(struct i2c_board_info *devices, int num)341350{342351 /*343343- * Set the EEPROM interface pin drive type control.344344- * Defines the driver type for the EECLK and EEDAT pins as either345345- * open drain, which will require an external pull-up, or a normal346346- * CMOS driver.352352+ * FIXME: this just sets the two pins as non-opendrain, as no353353+ * platforms tries to do that anyway. Flag the applicable lines354354+ * as open drain in the GPIO_LOOKUP above and the driver or355355+ * gpiolib will handle open drain/open drain emulation as need356356+ * be. Right now i2c-gpio emulates open drain which is not357357+ * optimal.347358 */348348- if (data->sda_is_open_drain && data->sda_pin != EP93XX_GPIO_LINE_EEDAT)349349- pr_warning("sda != EEDAT, open drain has no effect\n");350350- if (data->scl_is_open_drain && data->scl_pin != EP93XX_GPIO_LINE_EECLK)351351- pr_warning("scl != EECLK, open drain has no effect\n");352352-353353- __raw_writel((data->sda_is_open_drain << 1) |354354- (data->scl_is_open_drain << 0),359359+ __raw_writel((0 << 1) | (0 << 0),355360 EP93XX_GPIO_EEDRIVE);356361357357- ep93xx_i2c_data = *data;358362 i2c_register_board_info(0, devices, num);363363+ gpiod_add_lookup_table(&ep93xx_i2c_gpiod_table);359364 platform_device_register(&ep93xx_i2c_device);360365}361366
···3264326432653265 if (lflags & GPIO_ACTIVE_LOW)32663266 set_bit(FLAG_ACTIVE_LOW, &desc->flags);32673267+32673268 if (lflags & GPIO_OPEN_DRAIN)32683269 set_bit(FLAG_OPEN_DRAIN, &desc->flags);32703270+ else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {32713271+ /*32723272+ * This enforces open drain mode from the consumer side.32733273+ * This is necessary for some busses like I2C, but the lookup32743274+ * should *REALLY* have specified them as open drain in the32753275+ * first place, so print a little warning here.32763276+ */32773277+ set_bit(FLAG_OPEN_DRAIN, &desc->flags);32783278+ gpiod_warn(desc,32793279+ "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");32803280+ }32813281+32693282 if (lflags & GPIO_OPEN_SOURCE)32703283 set_bit(FLAG_OPEN_SOURCE, &desc->flags);32713284 if (lflags & GPIO_SLEEP_MAY_LOOSE_VALUE)
+104-108
drivers/i2c/busses/i2c-gpio.c
···1414#include <linux/module.h>1515#include <linux/slab.h>1616#include <linux/platform_device.h>1717-#include <linux/gpio.h>1717+#include <linux/gpio/consumer.h>1818#include <linux/of.h>1919-#include <linux/of_gpio.h>20192120struct i2c_gpio_private_data {2121+ struct gpio_desc *sda;2222+ struct gpio_desc *scl;2223 struct i2c_adapter adap;2324 struct i2c_algo_bit_data bit_data;2425 struct i2c_gpio_platform_data pdata;2526};2626-2727-/* Toggle SDA by changing the direction of the pin */2828-static void i2c_gpio_setsda_dir(void *data, int state)2929-{3030- struct i2c_gpio_platform_data *pdata = data;3131-3232- if (state)3333- gpio_direction_input(pdata->sda_pin);3434- else3535- gpio_direction_output(pdata->sda_pin, 0);3636-}37273828/*3929 * Toggle SDA by changing the output value of the pin. This is only···3242 */3343static void i2c_gpio_setsda_val(void *data, int state)3444{3535- struct i2c_gpio_platform_data *pdata = data;4545+ struct i2c_gpio_private_data *priv = data;36463737- gpio_set_value(pdata->sda_pin, state);3838-}3939-4040-/* Toggle SCL by changing the direction of the pin. */4141-static void i2c_gpio_setscl_dir(void *data, int state)4242-{4343- struct i2c_gpio_platform_data *pdata = data;4444-4545- if (state)4646- gpio_direction_input(pdata->scl_pin);4747- else4848- gpio_direction_output(pdata->scl_pin, 0);4747+ gpiod_set_value(priv->sda, state);4948}50495150/*···4566 */4667static void i2c_gpio_setscl_val(void *data, int state)4768{4848- struct i2c_gpio_platform_data *pdata = data;6969+ struct i2c_gpio_private_data *priv = data;49705050- gpio_set_value(pdata->scl_pin, state);7171+ gpiod_set_value(priv->scl, state);5172}52735374static int i2c_gpio_getsda(void *data)5475{5555- struct i2c_gpio_platform_data *pdata = data;7676+ struct i2c_gpio_private_data *priv = data;56775757- return gpio_get_value(pdata->sda_pin);7878+ return gpiod_get_value(priv->sda);5879}59806081static int i2c_gpio_getscl(void *data)6182{6262- struct i2c_gpio_platform_data *pdata = data;8383+ struct i2c_gpio_private_data *priv = data;63846464- return gpio_get_value(pdata->scl_pin);6565-}6666-6767-static int of_i2c_gpio_get_pins(struct device_node *np,6868- unsigned int *sda_pin, unsigned int *scl_pin)6969-{7070- if (of_gpio_count(np) < 2)7171- return -ENODEV;7272-7373- *sda_pin = of_get_gpio(np, 0);7474- *scl_pin = of_get_gpio(np, 1);7575-7676- if (*sda_pin == -EPROBE_DEFER || *scl_pin == -EPROBE_DEFER)7777- return -EPROBE_DEFER;7878-7979- if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) {8080- pr_err("%pOF: invalid GPIO pins, sda=%d/scl=%d\n",8181- np, *sda_pin, *scl_pin);8282- return -ENODEV;8383- }8484-8585- return 0;8585+ return gpiod_get_value(priv->scl);8686}87878888static void of_i2c_gpio_get_props(struct device_node *np,···82124 of_property_read_bool(np, "i2c-gpio,scl-output-only");83125}84126127127+static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,128128+ const char *con_id,129129+ unsigned int index,130130+ enum gpiod_flags gflags)131131+{132132+ struct gpio_desc *retdesc;133133+ int ret;134134+135135+ retdesc = devm_gpiod_get(dev, con_id, gflags);136136+ if (!IS_ERR(retdesc)) {137137+ dev_dbg(dev, "got GPIO from name %s\n", con_id);138138+ return retdesc;139139+ }140140+141141+ retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);142142+ if (!IS_ERR(retdesc)) {143143+ dev_dbg(dev, "got GPIO from index %u\n", index);144144+ return retdesc;145145+ }146146+147147+ ret = PTR_ERR(retdesc);148148+149149+ /* FIXME: hack in the old code, is this really necessary? */150150+ if (ret == -EINVAL)151151+ retdesc = ERR_PTR(-EPROBE_DEFER);152152+153153+ /* This happens if the GPIO driver is not yet probed, let's defer */154154+ if (ret == -ENOENT)155155+ retdesc = ERR_PTR(-EPROBE_DEFER);156156+157157+ if (ret != -EPROBE_DEFER)158158+ dev_err(dev, "error trying to get descriptor: %d\n", ret);159159+160160+ return retdesc;161161+}162162+85163static int i2c_gpio_probe(struct platform_device *pdev)86164{87165 struct i2c_gpio_private_data *priv;88166 struct i2c_gpio_platform_data *pdata;89167 struct i2c_algo_bit_data *bit_data;90168 struct i2c_adapter *adap;9191- unsigned int sda_pin, scl_pin;169169+ struct device *dev = &pdev->dev;170170+ struct device_node *np = dev->of_node;171171+ enum gpiod_flags gflags;92172 int ret;931739494- /* First get the GPIO pins; if it fails, we'll defer the probe. */9595- if (pdev->dev.of_node) {9696- ret = of_i2c_gpio_get_pins(pdev->dev.of_node,9797- &sda_pin, &scl_pin);9898- if (ret)9999- return ret;100100- } else {101101- if (!dev_get_platdata(&pdev->dev))102102- return -ENXIO;103103- pdata = dev_get_platdata(&pdev->dev);104104- sda_pin = pdata->sda_pin;105105- scl_pin = pdata->scl_pin;106106- }107107-108108- ret = devm_gpio_request(&pdev->dev, sda_pin, "sda");109109- if (ret) {110110- if (ret == -EINVAL)111111- ret = -EPROBE_DEFER; /* Try again later */112112- return ret;113113- }114114- ret = devm_gpio_request(&pdev->dev, scl_pin, "scl");115115- if (ret) {116116- if (ret == -EINVAL)117117- ret = -EPROBE_DEFER; /* Try again later */118118- return ret;119119- }120120-121121- priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);174174+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);122175 if (!priv)123176 return -ENOMEM;177177+124178 adap = &priv->adap;125179 bit_data = &priv->bit_data;126180 pdata = &priv->pdata;127181128128- if (pdev->dev.of_node) {129129- pdata->sda_pin = sda_pin;130130- pdata->scl_pin = scl_pin;131131- of_i2c_gpio_get_props(pdev->dev.of_node, pdata);182182+ if (np) {183183+ of_i2c_gpio_get_props(np, pdata);132184 } else {133133- memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));185185+ /*186186+ * If all platform data settings are zero it is OK187187+ * to not provide any platform data from the board.188188+ */189189+ if (dev_get_platdata(dev))190190+ memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));134191 }135192136136- if (pdata->sda_is_open_drain) {137137- gpio_direction_output(pdata->sda_pin, 1);138138- bit_data->setsda = i2c_gpio_setsda_val;139139- } else {140140- gpio_direction_input(pdata->sda_pin);141141- bit_data->setsda = i2c_gpio_setsda_dir;142142- }193193+ /*194194+ * First get the GPIO pins; if it fails, we'll defer the probe.195195+ * If the SDA line is marked from platform data or device tree as196196+ * "open drain" it means something outside of our control is making197197+ * this line being handled as open drain, and we should just handle198198+ * it as any other output. Else we enforce open drain as this is199199+ * required for an I2C bus.200200+ */201201+ if (pdata->sda_is_open_drain)202202+ gflags = GPIOD_OUT_HIGH;203203+ else204204+ gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;205205+ priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);206206+ if (IS_ERR(priv->sda))207207+ return PTR_ERR(priv->sda);143208144144- if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {145145- gpio_direction_output(pdata->scl_pin, 1);146146- bit_data->setscl = i2c_gpio_setscl_val;147147- } else {148148- gpio_direction_input(pdata->scl_pin);149149- bit_data->setscl = i2c_gpio_setscl_dir;150150- }209209+ /*210210+ * If the SCL line is marked from platform data or device tree as211211+ * "open drain" it means something outside of our control is making212212+ * this line being handled as open drain, and we should just handle213213+ * it as any other output. Else we enforce open drain as this is214214+ * required for an I2C bus.215215+ */216216+ if (pdata->scl_is_open_drain)217217+ gflags = GPIOD_OUT_LOW;218218+ else219219+ gflags = GPIOD_OUT_LOW_OPEN_DRAIN;220220+ priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);221221+ if (IS_ERR(priv->scl))222222+ return PTR_ERR(priv->scl);223223+224224+ bit_data->setsda = i2c_gpio_setsda_val;225225+ bit_data->setscl = i2c_gpio_setscl_val;151226152227 if (!pdata->scl_is_output_only)153228 bit_data->getscl = i2c_gpio_getscl;···198207 else199208 bit_data->timeout = HZ / 10; /* 100 ms */200209201201- bit_data->data = pdata;210210+ bit_data->data = priv;202211203212 adap->owner = THIS_MODULE;204204- if (pdev->dev.of_node)205205- strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));213213+ if (np)214214+ strlcpy(adap->name, dev_name(dev), sizeof(adap->name));206215 else207216 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);208217209218 adap->algo_data = bit_data;210219 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;211211- adap->dev.parent = &pdev->dev;212212- adap->dev.of_node = pdev->dev.of_node;220220+ adap->dev.parent = dev;221221+ adap->dev.of_node = np;213222214223 adap->nr = pdev->id;215224 ret = i2c_bit_add_numbered_bus(adap);···218227219228 platform_set_drvdata(pdev, priv);220229221221- dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",222222- pdata->sda_pin, pdata->scl_pin,230230+ /*231231+ * FIXME: using global GPIO numbers is not helpful. If/when we232232+ * get accessors to get the actual name of the GPIO line,233233+ * from the descriptor, then provide that instead.234234+ */235235+ dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",236236+ desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),223237 pdata->scl_is_output_only224238 ? ", no clock stretching" : "");225239
···2828#define GPIOD_FLAGS_BIT_DIR_SET BIT(0)2929#define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)3030#define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)3131+#define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)31323233/**3334 * Optional flags that can be passed to one of gpiod_* to configure direction···4039 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,4140 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |4241 GPIOD_FLAGS_BIT_DIR_VAL,4242+ GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET |4343+ GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_OPEN_DRAIN,4444+ GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET |4545+ GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_DIR_VAL |4646+ GPIOD_FLAGS_BIT_OPEN_DRAIN,4347};44484549#ifdef CONFIG_GPIOLIB
-4
include/linux/i2c-gpio.h
···12121313/**1414 * struct i2c_gpio_platform_data - Platform-dependent data for i2c-gpio1515- * @sda_pin: GPIO pin ID to use for SDA1616- * @scl_pin: GPIO pin ID to use for SCL1715 * @udelay: signal toggle delay. SCL frequency is (500 / udelay) kHz1816 * @timeout: clock stretching timeout in jiffies. If the slave keeps1917 * SCL low for longer than this, the transfer will time out.···2426 * @scl_is_output_only: SCL output drivers cannot be turned off.2527 */2628struct i2c_gpio_platform_data {2727- unsigned int sda_pin;2828- unsigned int scl_pin;2929 int udelay;3030 int timeout;3131 unsigned int sda_is_open_drain:1;