Merge branch '2020-06-03-misc-bugfixes'
[oweals/u-boot.git] / drivers / i2c / meson_i2c.c
index 7d06d95cf38876092b982e3e4422eff2b446ee4f..e070caf43f2a073bd5e9f4a6f5113873a3ab0b48 100644 (file)
@@ -3,10 +3,14 @@
  * (C) Copyright 2017 - Beniamino Galvani <b.galvani@gmail.com>
  */
 #include <common.h>
+#include <log.h>
 #include <asm/io.h>
 #include <clk.h>
 #include <dm.h>
 #include <i2c.h>
+#include <linux/bitops.h>
+#include <linux/delay.h>
+#include <linux/err.h>
 
 #define I2C_TIMEOUT_MS         100
 
@@ -41,7 +45,12 @@ struct i2c_regs {
        u32 tok_rdata1;
 };
 
+struct meson_i2c_data {
+       unsigned char div_factor;
+};
+
 struct meson_i2c {
+       const struct meson_i2c_data *data;
        struct clk clk;
        struct i2c_regs *regs;
        struct i2c_msg *msg;    /* Current I2C message */
@@ -229,7 +238,7 @@ static int meson_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
        if (IS_ERR_VALUE(clk_rate))
                return -EINVAL;
 
-       div = DIV_ROUND_UP(clk_rate, speed * 4);
+       div = DIV_ROUND_UP(clk_rate, speed * i2c->data->div_factor);
 
        /* clock divider has 12 bits */
        if (div >= (1 << 12)) {
@@ -253,6 +262,8 @@ static int meson_i2c_probe(struct udevice *bus)
        struct meson_i2c *i2c = dev_get_priv(bus);
        int ret;
 
+       i2c->data = (const struct meson_i2c_data *)dev_get_driver_data(bus);
+
        ret = clk_get_by_index(bus, 0, &i2c->clk);
        if (ret < 0)
                return ret;
@@ -272,11 +283,24 @@ static const struct dm_i2c_ops meson_i2c_ops = {
        .set_bus_speed = meson_i2c_set_bus_speed,
 };
 
+static const struct meson_i2c_data i2c_meson6_data = {
+       .div_factor = 4,
+};
+
+static const struct meson_i2c_data i2c_gxbb_data = {
+       .div_factor = 4,
+};
+
+static const struct meson_i2c_data i2c_axg_data = {
+       .div_factor = 3,
+};
+
 static const struct udevice_id meson_i2c_ids[] = {
-       { .compatible = "amlogic,meson6-i2c" },
-       { .compatible = "amlogic,meson-gx-i2c" },
-       { .compatible = "amlogic,meson-gxbb-i2c" },
-       { }
+       {.compatible = "amlogic,meson6-i2c", .data = (ulong)&i2c_meson6_data},
+       {.compatible = "amlogic,meson-gx-i2c", .data = (ulong)&i2c_gxbb_data},
+       {.compatible = "amlogic,meson-gxbb-i2c", .data = (ulong)&i2c_gxbb_data},
+       {.compatible = "amlogic,meson-axg-i2c", .data = (ulong)&i2c_axg_data},
+       {}
 };
 
 U_BOOT_DRIVER(i2c_meson) = {