diff options
author | Nikita Shubin <n.shubin@yadro.com> | 2021-11-10 12:42:28 +0300 |
---|---|---|
committer | Anup Patel <anup@brainfault.org> | 2021-11-12 10:19:45 +0530 |
commit | 6ca6bcafb74d2dc43a5082c1b7caf6b2aa453c2d (patch) | |
tree | dfd90e44f015e50d8b570a158feee91fbe1ee0c1 | |
parent | 2c964a2e159880c921a33f51f493a3a257d4bfce (diff) |
lib: utils/i2c: Add simple FDT based I2C framework
FDT based I2C framework on the top of I2C library.
The drivers are probed on demand by fdt_i2c_adapter_get
function.
Tested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Alexandre Ghiti <alexandre.ghiti@canonical.com>
Tested-by: Alexandre Ghiti <alexandre.ghiti@canonical.com>
Signed-off-by: Nikita Shubin <n.shubin@yadro.com>
-rw-r--r-- | include/sbi_utils/i2c/fdt_i2c.h | 26 | ||||
-rw-r--r-- | lib/utils/i2c/fdt_i2c.c | 87 | ||||
-rw-r--r-- | lib/utils/i2c/objects.mk | 1 |
3 files changed, 114 insertions, 0 deletions
diff --git a/include/sbi_utils/i2c/fdt_i2c.h b/include/sbi_utils/i2c/fdt_i2c.h new file mode 100644 index 0000000..c6e3339 --- /dev/null +++ b/include/sbi_utils/i2c/fdt_i2c.h @@ -0,0 +1,26 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 YADRO + * + * Authors: + * Nikita Shubin <n.shubin@yadro.com> + */ + +#ifndef __FDT_I2C_H__ +#define __FDT_I2C_H__ + +#include <sbi_utils/i2c/i2c.h> + +/** FDT based I2C adapter driver */ +struct fdt_i2c_adapter { + const struct fdt_match *match_table; + int (*init)(void *fdt, int nodeoff, + const struct fdt_match *match); +}; + +/** Get I2C adapter identified by nodeoff */ +int fdt_i2c_adapter_get(void *fdt, int nodeoff, + struct i2c_adapter **out_adapter); + +#endif diff --git a/lib/utils/i2c/fdt_i2c.c b/lib/utils/i2c/fdt_i2c.c new file mode 100644 index 0000000..4f91649 --- /dev/null +++ b/lib/utils/i2c/fdt_i2c.c @@ -0,0 +1,87 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 YADRO + * + * Authors: + * Nikita Shubin <n.shubin@yadro.com> + * + * derivate: lib/utils/gpio/fdt_gpio.c + * Authors: + * Anup Patel <anup.patel@wdc.com> + */ + +#include <libfdt.h> +#include <sbi/sbi_error.h> +#include <sbi_utils/fdt/fdt_helper.h> +#include <sbi_utils/i2c/fdt_i2c.h> + +#include <sbi/sbi_console.h> + +static struct fdt_i2c_adapter *i2c_adapter_drivers[] = { +}; + +static int fdt_i2c_adapter_init(void *fdt, int nodeoff) +{ + int pos, rc; + struct fdt_i2c_adapter *drv; + const struct fdt_match *match; + + /* Try all I2C drivers one-by-one */ + for (pos = 0; pos < array_size(i2c_adapter_drivers); pos++) { + drv = i2c_adapter_drivers[pos]; + match = fdt_match_node(fdt, nodeoff, drv->match_table); + if (match && drv->init) { + rc = drv->init(fdt, nodeoff, match); + if (rc == SBI_ENODEV) + continue; + if (rc) + return rc; + return 0; + } + } + + return SBI_ENOSYS; +} + +static int fdt_i2c_adapter_find(void *fdt, int nodeoff, + struct i2c_adapter **out_adapter) +{ + int rc; + struct i2c_adapter *adapter = i2c_adapter_find(nodeoff); + + if (!adapter) { + /* I2C adapter not found so initialize matching driver */ + rc = fdt_i2c_adapter_init(fdt, nodeoff); + if (rc) + return rc; + + /* Try to find I2C adapter again */ + adapter = i2c_adapter_find(nodeoff); + if (!adapter) + return SBI_ENOSYS; + } + + if (out_adapter) + *out_adapter = adapter; + + return 0; +} + +int fdt_i2c_adapter_get(void *fdt, int nodeoff, + struct i2c_adapter **out_adapter) +{ + int rc; + struct i2c_adapter *adapter; + + if (!fdt || (nodeoff < 0) || !out_adapter) + return SBI_EINVAL; + + rc = fdt_i2c_adapter_find(fdt, nodeoff, &adapter); + if (rc) + return rc; + + *out_adapter = adapter; + + return 0; +} diff --git a/lib/utils/i2c/objects.mk b/lib/utils/i2c/objects.mk index 894f618..d4e1c1b 100644 --- a/lib/utils/i2c/objects.mk +++ b/lib/utils/i2c/objects.mk @@ -8,3 +8,4 @@ # libsbiutils-objs-y += i2c/i2c.o +libsbiutils-objs-y += i2c/fdt_i2c.o |