From patchwork Fri Oct 15 13:19:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Shubin X-Patchwork-Id: 1541668 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=AmV0iZx4; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=maquefel.me header.i=@maquefel.me header.a=rsa-sha256 header.s=mail header.b=Xctt7dZ/; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HW6Lr6MW4z9shx for ; Sat, 16 Oct 2021 00:19:48 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=OJp0Sp9rFbhZOXyqR60tYLE8IenkCzXb1hvMRzHWXRQ=; b=AmV0iZx4uinsUX 7CQHMzm4faPSXKg0lCWYQGiDc1un06h2lzrSpc2t0uWGnNjuLciEkacMOtHFNeujf99in94XwSogu RH/m8M2UacsR8hNWWAgCQ0pQyPPg2IhQLADVXFBfo7+lFWS1vbaZbAoedpm1Yh2Odu502vJh3oHYe MjBF/5Iq9iwyhZVjXhBBToVdOdUR91tz002MYgfQAuNpsmPRvpz56lm5Y6yulLkrHlfuFb7akjhuM nZmIH+dS/BTcpkD1FVIo9gcJ0w5A8prOhrIHAeC0ekkyHXLUypJLQQ1R2jYw9mxAuyVUQ9IrXRjqB yyCBhgPhPI3Uc6vJBE0g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mbN7f-007B88-CQ; Fri, 15 Oct 2021 13:19:43 +0000 Received: from forward501p.mail.yandex.net ([77.88.28.111]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mbN7X-007B47-GN for opensbi@lists.infradead.org; Fri, 15 Oct 2021 13:19:40 +0000 Received: from iva7-16e50705448a.qloud-c.yandex.net (iva7-16e50705448a.qloud-c.yandex.net [IPv6:2a02:6b8:c0c:3227:0:640:16e5:705]) by forward501p.mail.yandex.net (Yandex) with ESMTP id 84DDC6212EE6; Fri, 15 Oct 2021 16:19:28 +0300 (MSK) Received: from iva6-2d18925256a6.qloud-c.yandex.net (2a02:6b8:c0c:7594:0:640:2d18:9252 [2a02:6b8:c0c:7594:0:640:2d18:9252]) by iva7-16e50705448a.qloud-c.yandex.net (mxback/Yandex) with ESMTP id mQxk1Yl0we-JSDWajJF; Fri, 15 Oct 2021 16:19:28 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maquefel.me; s=mail; t=1634303968; bh=MZNUYwbQ1NqaLa+uhCRcvItwT7K+BGeEXrGHTjsOETg=; h=In-Reply-To:References:Date:Subject:To:From:Message-Id:Cc; b=Xctt7dZ/Z2/AJytrBHir+orNr6t+DhQOCOIElrhp+zkYPOgXv2AjsL1feuJSDuCNy HHeNKk8pdLGqV8bM9vbMWq7lUD0TJsJsm3ctIrUO3qe8y+I5M46XA7PAeOFySeYUVE IupXbqRx+1dGshA49W2kH4FFaYlyXfxrW4n6fm9o= Authentication-Results: iva7-16e50705448a.qloud-c.yandex.net; dkim=pass header.i=@maquefel.me Received: by iva6-2d18925256a6.qloud-c.yandex.net (smtp/Yandex) with ESMTPS id hfsYCHEyeQ-JR1G4riI; Fri, 15 Oct 2021 16:19:27 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) X-Yandex-Fwd: 2 From: Nikita Shubin To: opensbi@lists.infradead.org Cc: linux@yadro.com, anup.patel@wdc.com, Nikita Shubin Subject: [PATCH v2 2/5] lib: utils/i2c: Add generic I2C configuration library Date: Fri, 15 Oct 2021 16:19:22 +0300 Message-Id: <20211015131925.22585-3-nikita.shubin@maquefel.me> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211015131925.22585-1-nikita.shubin@maquefel.me> References: <20211015131925.22585-1-nikita.shubin@maquefel.me> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211015_061935_982807_E30B269F X-CRM114-Status: GOOD ( 16.56 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Nikita Shubin Helper library to keep track of registered I2C adapters, identified by dts offset, basic send/read functions and adapter configuration (enable, set dividers, etc...). Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [77.88.28.111 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Nikita Shubin 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 --- v1 -> v2: - switch to lists instead of array - rename send to write - use buffers instead of single byte - add single byte read/write --- include/sbi_utils/i2c/i2c.h | 99 +++++++++++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c | 85 +++++++++++++++++++++++++++++++ lib/utils/i2c/objects.mk | 10 ++++ 3 files changed, 194 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..76cdb66 --- /dev/null +++ b/include/sbi_utils/i2c/i2c.h @@ -0,0 +1,99 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 YADRO + * + * Authors: + * Nikita Shubin + */ + +#ifndef __I2C_H__ +#define __I2C_H__ + +#include +#include + +/** Representation of a I2C adapter */ +struct i2c_adapter { + /** Pointer to I2C driver owning this I2C adapter */ + void *driver; + + /** Unique 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 buffer to given address, register + * + * @return 0 on success and negative error code on failure + */ + int (*smbus_write)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg, + uint8_t *buffer, int len); + + /** + * Read buffer from given address, register + * + * @return 0 on success and negative error code on failure + */ + int (*smbus_read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg, + uint8_t *buffer, int len); + + /** List */ + struct sbi_dlist node; +}; + +static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist *node) +{ + return container_of(node, struct i2c_adapter, node); +} + +/** 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); + +/** Write to device on I2C adapter bus */ +int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr, + uint8_t reg, uint8_t *buffer, int len); + +/** Read from device on I2C adapter bus */ +int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr, + uint8_t reg, uint8_t *buffer, int len); + +/** Write single byte from device on I2C adapter bus */ +static inline int i2c_adapter_smbus_reg_write(struct i2c_adapter *ia, + uint8_t addr, uint8_t reg, uint8_t val) +{ + return i2c_adapter_smbus_write(ia, addr, reg, &val, 1); +} + +/** Read single byte from device on I2C adapter bus */ +static inline int i2c_adapter_smbus_reg_read(struct i2c_adapter *ia, + uint8_t addr, uint8_t reg, uint8_t *val) +{ + uint8_t buf; + int ret = i2c_adapter_smbus_read(ia, addr, reg, &buf, 1); + + if (ret) + return ret; + + *val = buf; + return 0; +} + +#endif diff --git a/lib/utils/i2c/i2c.c b/lib/utils/i2c/i2c.c new file mode 100644 index 0000000..d23ac91 --- /dev/null +++ b/lib/utils/i2c/i2c.c @@ -0,0 +1,85 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 YADRO + * + * Authors: + * Nikita Shubin + * + * derivate: lib/utils/gpio/gpio.c + * Authors: + * Anup Patel + */ + +#include +#include + +static SBI_LIST_HEAD(i2c_adapters_list); + +struct i2c_adapter *i2c_adapter_find(int id) +{ + struct sbi_dlist *pos; + + sbi_list_for_each(pos, &(i2c_adapters_list)) { + struct i2c_adapter *adap = to_i2c_adapter(pos); + + if (adap->id == id) + return adap; + } + + return NULL; +} + +int i2c_adapter_add(struct i2c_adapter *ia) +{ + if (!ia) + return SBI_EINVAL; + + if (i2c_adapter_find(ia->id)) + return SBI_EALREADY; + + sbi_list_add(&(ia->node), &(i2c_adapters_list)); + + return 0; +} + +void i2c_adapter_remove(struct i2c_adapter *ia) +{ + if (!ia) + return; + + sbi_list_del(&(ia->node)); +} + +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_smbus_write(struct i2c_adapter *ia, uint8_t addr, + uint8_t reg, uint8_t *buffer, int len) +{ + if (!ia) + return SBI_EINVAL; + if (!ia->smbus_write) + return SBI_ENOSYS; + + return ia->smbus_write(ia, addr, reg, buffer, len); +} + + +int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr, + uint8_t reg, uint8_t *buffer, int len) +{ + if (!ia) + return SBI_EINVAL; + if (!ia->smbus_read) + return SBI_ENOSYS; + + return ia->smbus_read(ia, addr, reg, buffer, len); +} 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 +# + +libsbiutils-objs-y += i2c/i2c.o