/* * Bitbanging I2C bus driver using the GPIO API * * Copyright (C) 2007 Atmel Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #include #include #include #include #include #include #include #include #include struct i2c_gpio_private_data { struct gpio_desc *sda; struct gpio_desc *scl; struct i2c_adapter adap; struct i2c_algo_bit_data bit_data; struct i2c_gpio_platform_data pdata; }; /* Toggle SDA by changing the direction of the pin */ static void i2c_gpio_setsda_dir(void *data, int state) { struct i2c_gpio_private_data *priv = data; /* * This is a way of saying "do not drive * me actively high" which means emulating open drain. * The right way to do this is for gpiolib to * handle this, by the function below. */ if (state) gpiod_direction_input(priv->sda); else gpiod_direction_output(priv->sda, 0); } /* * Toggle SDA by changing the output value of the pin. This is only * valid for pins configured as open drain (i.e. setting the value * high effectively turns off the output driver.) */ static void i2c_gpio_setsda_val(void *data, int state) { struct i2c_gpio_private_data *priv = data; gpiod_set_value(priv->sda, state); } /* Toggle SCL by changing the direction of the pin. */ static void i2c_gpio_setscl_dir(void *data, int state) { struct i2c_gpio_private_data *priv = data; if (state) gpiod_direction_input(priv->scl); else gpiod_direction_output(priv->scl, 0); } /* * Toggle SCL by changing the output value of the pin. This is used * for pins that are configured as open drain and for output-only * pins. The latter case will break the i2c protocol, but it will * often work in practice. */ static void i2c_gpio_setscl_val(void *data, int state) { struct i2c_gpio_private_data *priv = data; gpiod_set_value(priv->scl, state); } static int i2c_gpio_getsda(void *data) { struct i2c_gpio_private_data *priv = data; return gpiod_get_value(priv->sda); } static int i2c_gpio_getscl(void *data) { struct i2c_gpio_private_data *priv = data; return gpiod_get_value(priv->scl); } static void of_i2c_gpio_get_props(struct device_node *np, struct i2c_gpio_platform_data *pdata) { u32 reg; of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay); if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®)) pdata->timeout = msecs_to_jiffies(reg); pdata->sda_is_open_drain = of_property_read_bool(np, "i2c-gpio,sda-open-drain"); pdata->scl_is_open_drain = of_property_read_bool(np, "i2c-gpio,scl-open-drain"); pdata->scl_is_output_only = of_property_read_bool(np, "i2c-gpio,scl-output-only"); } static int i2c_gpio_probe(struct platform_device *pdev) { struct i2c_gpio_private_data *priv; struct i2c_gpio_platform_data *pdata; struct i2c_algo_bit_data *bit_data; struct i2c_adapter *adap; int ret; priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); if (!priv) return -ENOMEM; /* First get the GPIO pins; if it fails, we'll defer the probe. */ priv->sda = devm_gpiod_get_index(&pdev->dev, NULL, 0, GPIOD_OUT_HIGH); if (IS_ERR(priv->sda)) { ret = PTR_ERR(priv->sda); /* FIXME: hack in the old code, is this really necessary? */ if (ret == -EINVAL) ret = -EPROBE_DEFER; return ret; } priv->scl = devm_gpiod_get_index(&pdev->dev, NULL, 1, GPIOD_OUT_LOW); if (IS_ERR(priv->scl)) { ret = PTR_ERR(priv->scl); /* FIXME: hack in the old code, is this really necessary? */ if (ret == -EINVAL) ret = -EPROBE_DEFER; return ret; } adap = &priv->adap; bit_data = &priv->bit_data; pdata = &priv->pdata; if (pdev->dev.of_node) { of_i2c_gpio_get_props(pdev->dev.of_node, pdata); } else { /* * If all platform data settings are zero it is OK * to not provide any platform data from the board. */ if (dev_get_platdata(&pdev->dev)) memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata)); } /* * FIXME: this is a hack emulating the open drain emulation * that gpiolib can already do for us. Make all clients properly * flag their lines as open drain and get rid of this property * and the special callback. */ if (pdata->sda_is_open_drain) { gpiod_direction_output(priv->sda, 1); bit_data->setsda = i2c_gpio_setsda_val; } else { gpiod_direction_input(priv->sda); bit_data->setsda = i2c_gpio_setsda_dir; } if (pdata->scl_is_open_drain || pdata->scl_is_output_only) { gpiod_direction_output(priv->scl, 1); bit_data->setscl = i2c_gpio_setscl_val; } else { gpiod_direction_input(priv->scl); bit_data->setscl = i2c_gpio_setscl_dir; } if (!pdata->scl_is_output_only) bit_data->getscl = i2c_gpio_getscl; bit_data->getsda = i2c_gpio_getsda; if (pdata->udelay) bit_data->udelay = pdata->udelay; else if (pdata->scl_is_output_only) bit_data->udelay = 50; /* 10 kHz */ else bit_data->udelay = 5; /* 100 kHz */ if (pdata->timeout) bit_data->timeout = pdata->timeout; else bit_data->timeout = HZ / 10; /* 100 ms */ bit_data->data = priv; adap->owner = THIS_MODULE; if (pdev->dev.of_node) strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name)); else snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); adap->algo_data = bit_data; adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; adap->dev.parent = &pdev->dev; adap->dev.of_node = pdev->dev.of_node; adap->nr = pdev->id; ret = i2c_bit_add_numbered_bus(adap); if (ret) return ret; platform_set_drvdata(pdev, priv); /* * FIXME: using global GPIO numbers is not helpful. If/when we * get accessors to get the actual name of the GPIO line, * from the descriptor, then provide that instead. */ dev_info(&pdev->dev, "using lines %u (SDA) and %u (SCL%s)\n", desc_to_gpio(priv->sda), desc_to_gpio(priv->scl), pdata->scl_is_output_only ? ", no clock stretching" : ""); return 0; } static int i2c_gpio_remove(struct platform_device *pdev) { struct i2c_gpio_private_data *priv; struct i2c_adapter *adap; priv = platform_get_drvdata(pdev); adap = &priv->adap; i2c_del_adapter(adap); return 0; } #if defined(CONFIG_OF) static const struct of_device_id i2c_gpio_dt_ids[] = { { .compatible = "i2c-gpio", }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids); #endif static struct platform_driver i2c_gpio_driver = { .driver = { .name = "i2c-gpio", .of_match_table = of_match_ptr(i2c_gpio_dt_ids), }, .probe = i2c_gpio_probe, .remove = i2c_gpio_remove, }; static int __init i2c_gpio_init(void) { int ret; ret = platform_driver_register(&i2c_gpio_driver); if (ret) printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); return ret; } subsys_initcall(i2c_gpio_init); static void __exit i2c_gpio_exit(void) { platform_driver_unregister(&i2c_gpio_driver); } module_exit(i2c_gpio_exit); MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver"); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:i2c-gpio");