From: Xiang W <wxjstz@126.com>
To: opensbi@lists.infradead.org
Subject: [PATCH 1/4] lib: utils/i2c: Add generic I2C configuration library
Date: Sat, 25 Sep 2021 12:19:43 +0800 [thread overview]
Message-ID: <bd47f16d6e6a80b26a57d10a18ff2372b8620987.camel@126.com> (raw)
In-Reply-To: <20210924113347.3576-2-nikita.shubin@maquefel.me>
? 2021-09-24???? 14:33 +0300?Nikita Shubin???
> From: Nikita Shubin <n.shubin@yadro.com>
>
> Helper library to keep track of registered I2C adapters,
> identified by dts offset, basic send/read functions and
> adapter configuration (enable, set dividers, etc...).
>
> Signed-off-by: Nikita Shubin <n.shubin@yadro.com>
> ---
> ?include/sbi_utils/i2c/i2c.h |? 65 +++++++++++++++++++++++
> ?lib/utils/i2c/i2c.c???????? | 102
> ++++++++++++++++++++++++++++++++++++
> ?lib/utils/i2c/objects.mk??? |? 10 ++++
> ?3 files changed, 177 insertions(+)
> ?create mode 100644 include/sbi_utils/i2c/i2c.h
> ?create mode 100644 lib/utils/i2c/i2c.c
> ?create mode 100644 lib/utils/i2c/objects.mk
>
> diff --git a/include/sbi_utils/i2c/i2c.h
> b/include/sbi_utils/i2c/i2c.h
> new file mode 100644
> index 0000000..86dd582
> --- /dev/null
> +++ b/include/sbi_utils/i2c/i2c.h
> @@ -0,0 +1,65 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2021 YADRO
> + *
> + * Authors:
> + *?? Nikita Shubin <nshubin@yadro.com>
> + */
> +
> +#ifndef __I2C_H__
> +#define __I2C_H__
> +
> +#include <sbi/sbi_types.h>
> +
> +/** Representation of a I2C adapter */
> +struct i2c_adapter {
I suggest adding an object sbi_dlist to form a linked list
> +???????/** Pointer to I2C driver owning this I2C adapter */
> +???????void *driver;
> +
> +???????/** Uniquie ID of the I2C adapter assigned by the driver */
> +???????int id;
> +
> +???????/**
> +??????? * Configure I2C adapter
> +??????? *
> +??????? * Enable, set dividers, etc...
> +??????? *
> +??????? * @return 0 on success and negative error code on failure
> +??????? */
> +???????int (*configure)(struct i2c_adapter *ia);
> +
> +???????/**
> +??????? * Send byte to given address, register
> +??????? *
> +??????? * @return 0 on success and negative error code on failure
> +??????? */
> +???????int (*send)(struct i2c_adapter *ia, uint8_t addr, uint8_t
> reg, uint8_t value);
> +
> +???????/**
> +??????? * Read byte from given address, register
> +??????? *
> +??????? * @return 0 on success and negative error code on failure
> +??????? */
> +???????int (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t
> reg, uint8_t *value);
> +};
> +
> +/** Find a registered I2C adapter */
> +struct i2c_adapter *i2c_adapter_find(int id);
> +
> +/** Register I2C adapter */
> +int i2c_adapter_add(struct i2c_adapter *ia);
> +
> +/** Un-register I2C adapter */
> +void i2c_adapter_remove(struct i2c_adapter *ia);
> +
> +/** Configure I2C adapter prior to send/read */
> +int i2c_adapter_configure(struct i2c_adapter *ia);
> +
> +/** Send to device on I2C adapter bus */
> +int i2c_adapter_send(struct i2c_adapter *ia, uint8_t addr, uint8_t
> reg, uint8_t value);
> +
> +/** Read from device on I2C adapter bus */
> +int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t
> reg, uint8_t *value);
> +
> +#endif
> diff --git a/lib/utils/i2c/i2c.c b/lib/utils/i2c/i2c.c
> new file mode 100644
> index 0000000..9bcb129
> --- /dev/null
> +++ b/lib/utils/i2c/i2c.c
> @@ -0,0 +1,102 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2021 YADRO
> + *
> + * Authors:
> + *?? Nikita Shubin <nshubin@yadro.com>
> + *
> + * derivate: lib/utils/gpio/gpio.c
> + * Authors:
> + *?? Anup Patel <anup.patel@wdc.com>
> + */
> +
> +#include <sbi/sbi_error.h>
> +#include <sbi_utils/i2c/i2c.h>
> +
> +#define I2C_ADAPTER_MAX????????????????16
If it is implemented through a linked list, there will be no limit on
the number
Regards,
Xiang W
> +
> +static struct i2c_adapter *i2c_array[I2C_ADAPTER_MAX];
> +
> +struct i2c_adapter *i2c_adapter_find(int id)
> +{
> +???????unsigned int i;
> +???????struct i2c_adapter *ret = NULL;
> +
> +???????for (i = 0; i < I2C_ADAPTER_MAX; i++) {
> +???????????????if (i2c_array[i] && i2c_array[i]->id == id) {
> +???????????????????????ret = i2c_array[i];
> +???????????????????????break;
> +???????????????}
> +???????}
> +
> +???????return ret;
> +}
> +
> +int i2c_adapter_add(struct i2c_adapter *ia)
> +{
> +???????int i, ret = SBI_ENOSPC;
> +
> +???????if (!ia)
> +???????????????return SBI_EINVAL;
> +???????if (i2c_adapter_find(ia->id))
> +???????????????return SBI_EALREADY;
> +
> +???????for (i = 0; i < I2C_ADAPTER_MAX; i++) {
> +???????????????if (!i2c_array[i]) {
> +???????????????????????i2c_array[i] = ia;
> +???????????????????????ret = 0;
> +???????????????????????break;
> +???????????????}
> +???????}
> +
> +???????return ret;
> +}
> +
> +void i2c_adapter_remove(struct i2c_adapter *ia)
> +{
> +???????int i;
> +
> +???????if (!ia)
> +???????????????return;
> +
> +???????for (i = 0; i < I2C_ADAPTER_MAX; i++) {
> +???????????????if (i2c_array[i] == ia) {
> +???????????????????????i2c_array[i] = NULL;
> +???????????????????????break;
> +???????????????}
> +???????}
> +}
> +
> +int i2c_adapter_configure(struct i2c_adapter *ia)
> +{
> +???????if (!ia)
> +???????????????return SBI_EINVAL;
> +???????if (!ia->configure)
> +???????????????return 0;
> +
> +???????return ia->configure(ia);
> +}
> +
> +int i2c_adapter_send(struct i2c_adapter *ia, uint8_t addr,
> +??????????????????? uint8_t reg, uint8_t value)
> +{
> +???????if (!ia)
> +???????????????return SBI_EINVAL;
> +???????if (!ia->send)
> +???????????????return SBI_ENOSYS;
> +
> +???????return ia->send(ia, addr, reg, value);
> +}
> +
> +
> +int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,
> +??????????????????? uint8_t reg, uint8_t *value)
> +{
> +???????if (!ia)
> +???????????????return SBI_EINVAL;
> +???????if (!ia->read)
> +???????????????return SBI_ENOSYS;
> +
> +???????return ia->read(ia, addr, reg, value);
> +}
> diff --git a/lib/utils/i2c/objects.mk b/lib/utils/i2c/objects.mk
> new file mode 100644
> index 0000000..16a70da
> --- /dev/null
> +++ b/lib/utils/i2c/objects.mk
> @@ -0,0 +1,10 @@
> +#
> +# SPDX-License-Identifier: BSD-2-Clause
> +#
> +# Copyright (c) 2021 YADRO
> +#
> +# Authors:
> +#?? Nikita Shubin <nshubin@yadro.com>
> +#
> +
> +libsbiutils-objs-y += i2c/i2c.o
> --
> 2.31.1
>
>
next prev parent reply other threads:[~2021-09-25 4:19 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 [this message]
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
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=bd47f16d6e6a80b26a57d10a18ff2372b8620987.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