OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Xiang W <wxjstz@126.com>
To: opensbi@lists.infradead.org
Subject: [PATCH 2/4] lib: utils/gpio: Add simple FDT based I2C framework
Date: Sat, 25 Sep 2021 12:39:59 +0800	[thread overview]
Message-ID: <8f2fa486cd3d4fa1546d6b7d678dd7e223e23575.camel@126.com> (raw)
In-Reply-To: <20210924113347.3576-3-nikita.shubin@maquefel.me>

? 2021-09-24???? 14:33 +0300?Nikita Shubin???
> From: Nikita Shubin <n.shubin@yadro.com>
> 
> FDT based I2C framework on the top of I2C library.
> 
> The drivers are probed on demand by fdt_i2c_adapter_get
> function.
> 
> Signed-off-by: Nikita Shubin <n.shubin@yadro.com>
> ---
> ?include/sbi_utils/i2c/fdt_i2c.h |? 26 ++++++++
> ?lib/utils/i2c/fdt_i2c.c???????? | 107
> ++++++++++++++++++++++++++++++++
> ?lib/utils/i2c/objects.mk??????? |?? 1 +
> ?3 files changed, 134 insertions(+)
> ?create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
> ?create mode 100644 lib/utils/i2c/fdt_i2c.c
> 
> diff --git a/include/sbi_utils/i2c/fdt_i2c.h
> b/include/sbi_utils/i2c/fdt_i2c.h
> new file mode 100644
> index 0000000..1f41a0f
> --- /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 <nshubin@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..27e28a4
> --- /dev/null
> +++ b/lib/utils/i2c/fdt_i2c.c
> @@ -0,0 +1,107 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2021 YADRO
> + *
> + * Authors:
> + *?? Nikita Shubin <nshubin@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 struct fdt_i2c_adapter *fdt_i2c_adapter_driver(struct
> i2c_adapter *adapter)
> +{
> +???????int pos;
> +
> +???????if (!adapter)
> +???????????????return NULL;
> +
> +???????for (pos = 0; pos < array_size(i2c_adapter_drivers); pos++) {
> +???????????????if (adapter->driver == i2c_adapter_drivers[pos])
> +???????????????????????return i2c_adapter_drivers[pos];
> +???????}
> +
> +???????return NULL;
> +}
> +
> +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 */
Reinitializing the I2C adapter may affect other adapters that have
already been initialized. So I suggest to add a static variable,
through this control the initial operation is executed once.

Regards,
Xiang W
> +???????????????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;
> +???????struct fdt_i2c_adapter *drv;
> +
> +???????if (!fdt || (nodeoff < 0) || !out_adapter)
> +???????????????return SBI_EINVAL;
> +
> +???????rc = fdt_i2c_adapter_find(fdt, nodeoff, &adapter);
> +???????if (rc)
> +???????????????return rc;
> +
> +???????drv = fdt_i2c_adapter_driver(adapter);
> +???????if (!drv)
> +???????????????return SBI_ENOSYS;
> +
> +???????*out_adapter = adapter;
> +
> +???????return 0;
> +}
> diff --git a/lib/utils/i2c/objects.mk b/lib/utils/i2c/objects.mk
> index 16a70da..06baa65 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
> -- 
> 2.31.1
> 
> 




  reply	other threads:[~2021-09-25  4:39 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24 11:33 [PATCH 0/4] reboot SiFive Unmatched via PMIC Nikita Shubin
2021-09-24 11:33 ` [PATCH 1/4] lib: utils/i2c: Add generic I2C configuration library Nikita Shubin
2021-09-25  4:19   ` Xiang W
2021-09-28 11:07     ` Nikita Shubin
2021-09-27 15:41   ` Alexandre ghiti
2021-09-28 10:51     ` Nikita Shubin
2021-09-28 11:43       ` Alexandre Ghiti
2021-09-24 11:33 ` [PATCH 2/4] lib: utils/gpio: Add simple FDT based I2C framework Nikita Shubin
2021-09-25  4:39   ` Xiang W [this message]
2021-09-27 15:42   ` Alexandre ghiti
2021-09-24 11:33 ` [PATCH 3/4] lib: utils/gpio: Add minimal SiFive I2C driver Nikita Shubin
2021-09-27 15:42   ` Alexandre ghiti
2021-09-24 11:33 ` [PATCH 4/4] lib: utils/reset: Add generic da9063 reset driver Nikita Shubin
2021-09-27 15:42   ` Alexandre ghiti
2021-09-28 10:56     ` Nikita Shubin
2021-09-24 15:54 ` [PATCH 0/4] reboot SiFive Unmatched via PMIC Jessica Clarke
2021-09-24 16:02   ` Anup Patel
2021-09-24 16:06     ` Nikita Shubin
2021-09-24 16:09       ` Anup Patel
2021-09-24 16:20         ` Nikita Shubin
2021-09-24 16:36         ` Nikita Shubin
2021-09-25  1:10 ` Bin Meng
2021-09-25  3:56   ` Anup Patel
2021-09-25  4:45     ` Bin Meng
2021-09-25  6:34       ` Anup Patel
2021-09-25  8:11   ` Heinrich Schuchardt
2021-09-28 10:32     ` Nikita Shubin
2021-09-29 12:46     ` Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8f2fa486cd3d4fa1546d6b7d678dd7e223e23575.camel@126.com \
    --to=wxjstz@126.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox