From patchwork Thu Jul 27 14:59:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier MOYSAN X-Patchwork-Id: 127017 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp1177351vqo; Thu, 27 Jul 2023 08:24:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlFruZ2wx313rt+Y9kUUSfgbfL+Y4OvkS1fF8ZfobnC9D9/A8c33Mh0LiNrIYfUJGNaJHdZy X-Received: by 2002:a05:651c:1047:b0:2b9:c5a0:81b3 with SMTP id x7-20020a05651c104700b002b9c5a081b3mr1299597ljm.16.1690471470381; Thu, 27 Jul 2023 08:24:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690471470; cv=none; d=google.com; s=arc-20160816; b=eWD21vg/hsGibXvQpGud7HCOOj9ex4a1/wXzO7b06U00ipaJEUpnHddoKf9BfXsdoo klJ48ER1T7AxgI5/8tSwuJCz1SbeyLSxQqapf7LWVHmQQaaayrC7p0b+JtWdbuMT0hmz +pGL3rqgqllJ1doPzGs0gYr8LwAR416gjcvX4hqv/w2A7Mxd1iqQ2yNosWHClBK9lcnj qsO/HOsY2BPzHBDGGdGSKW8OSjMkxAJpSL96heLcqDy60Y2hyHhD9G4GsdY4BEQurix1 OlVenFKcQ5lrG37Q6LHa4+p5uJmEsxkH+2mSOyuLy4hOmW6UMdKlm3SF6VskJZBAwaQ5 78PQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4QX9Z5N4IBM3jR26vAY1wmUBBclNiX7yJiB7oyvXpaY=; fh=XT/Dq5D6lsUgx6jO/dIRa9Hg6+thhVHYfx9G47Z5hPM=; b=mp6s+lNLA+FsYxlSFRZnzvm48Wznu5j1/qsoSjAjO51huxjfgLp2pOQ3PNMOQgucUM F5JUq6KsezV0a20Utq8+cbX8lw0WtDeIXCq5M/R3XMvzNCNfhmMWNkdEIAZ/oFcX6skh Y3jZQ/XA3Zthau5FYEGNGsnGq4DWUaLwG1xLnVHG/SpwJVL0EmuvVS1qaQBTtNUzUk8g h2DUYblFyawK3H3b9v5U/5vJi1YxPk6aJWYZphnzPj0bA/PeUKy1YzB6PZJ8AzyAHYk9 EYB3l8Hpmf1NOkVo5oqkLJ2c3lFFr1yCWP18FNddrIg0+ZWQ/JpSACrHEVd8APtJ2i/y g8Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b="yP+N/GlB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l3-20020a170906078300b00992bbdebdbesi1074411ejc.785.2023.07.27.08.23.27; Thu, 27 Jul 2023 08:24:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b="yP+N/GlB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231882AbjG0PAQ (ORCPT + 99 others); Thu, 27 Jul 2023 11:00:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233337AbjG0PAL (ORCPT ); Thu, 27 Jul 2023 11:00:11 -0400 Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 778F3B6; Thu, 27 Jul 2023 08:00:08 -0700 (PDT) Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36RA9Ies027858; Thu, 27 Jul 2023 16:59:47 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=4QX9Z5N4IBM3jR26vAY1wmUBBclNiX7yJiB7oyvXpaY=; b=yP+N/GlBOlK1/ufli5zYvaBrvT7SWuDBVH3qGKysJWdfo0N8rAD/qVecg04R6iy+2E3W r9Mlj7FuQuXXm2I9ZMLnO/40PcE8GD0KC0G9RaS45t2Dt0CI/JFhXbAcoeHvJQygWBBe V7NVsfmePv8NnlalqwtOXyTqBZ9/hnF6hgE6r/a4XRV/hKhHnDcJIblNayU6oKAq0/oq N+rJqY+sB0XIggO2TjgLsX4f/u/JJSXjQiw+4mnXoFWRrvcwOXBMPCYrx4SpSbwAFZGo BYRTugOgCcsrwWYax+6m4YLH6QCPqK5E8jX0N7aB+o6vmykaFBl4sKhM24ticQHWZ4iz KQ== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3s3kn2b15m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 27 Jul 2023 16:59:47 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 5FD8910002A; Thu, 27 Jul 2023 16:59:47 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 579CF209721; Thu, 27 Jul 2023 16:59:47 +0200 (CEST) Received: from localhost (10.201.20.178) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 27 Jul 2023 16:59:47 +0200 From: Olivier Moysan To: Jonathan Cameron , Lars-Peter Clausen CC: Olivier Moysan , , Subject: [RFC v2 01/11] iio: introduce iio backend device Date: Thu, 27 Jul 2023 16:59:27 +0200 Message-ID: <20230727145939.1157607-2-olivier.moysan@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230727145939.1157607-1-olivier.moysan@foss.st.com> References: <20230727145939.1157607-1-olivier.moysan@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.201.20.178] X-ClientProxiedBy: EQNCAS1NODE3.st.com (10.75.129.80) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-27_07,2023-07-26_01,2023-05-22_02 X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772587812638132913 X-GMAIL-MSGID: 1772587812638132913 Add a new device type in IIO framework. This backend device does not compute channel attributes and does not expose them through sysfs, as done typically in iio-rescale frontend device. Instead, it allows to report information applying to channel attributes through callbacks. These backend devices can be cascaded to represent chained components. An IIO device configured as a consumer of a backend device can compute the channel attributes of the whole chain. Signed-off-by: Olivier Moysan --- drivers/iio/Makefile | 1 + drivers/iio/industrialio-backend.c | 107 +++++++++++++++++++++++++++++ include/linux/iio/backend.h | 56 +++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 drivers/iio/industrialio-backend.c create mode 100644 include/linux/iio/backend.h diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile index 9622347a1c1b..9b59c6ab1738 100644 --- a/drivers/iio/Makefile +++ b/drivers/iio/Makefile @@ -5,6 +5,7 @@ obj-$(CONFIG_IIO) += industrialio.o industrialio-y := industrialio-core.o industrialio-event.o inkern.o +industrialio-$(CONFIG_IIO_BACKEND) += industrialio-backend.o industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o diff --git a/drivers/iio/industrialio-backend.c b/drivers/iio/industrialio-backend.c new file mode 100644 index 000000000000..7d0625889873 --- /dev/null +++ b/drivers/iio/industrialio-backend.c @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0 +/* The industrial I/O core, backend handling functions + * + */ + +#include +#include +#include +#include +#include + +static DEFINE_IDA(iio_backend_ida); + +#define to_iio_backend(_device) container_of((_device), struct iio_backend, dev) + +static void iio_backend_release(struct device *device) +{ + struct iio_backend *backend = to_iio_backend(device); + + kfree(backend->name); + kfree(backend); +} + +static const struct device_type iio_backend_type = { + .release = iio_backend_release, + .name = "iio_backend_device", +}; + +struct iio_backend *iio_backend_alloc(struct device *parent) +{ + struct iio_backend *backend; + + backend = devm_kzalloc(parent, sizeof(*backend), GFP_KERNEL); + + backend->dev.parent = parent; + backend->dev.type = &iio_backend_type; + backend->dev.bus = &iio_bus_type; + device_initialize(&backend->dev); + + return backend; +}; +EXPORT_SYMBOL(iio_backend_alloc); + +void iio_backend_free(struct device *dev) +{ + if (dev) + put_device(dev); +} +EXPORT_SYMBOL(iio_backend_free); + +int iio_backend_register(struct iio_backend *backend) +{ + struct fwnode_handle *fwnode; + int id; + + id = ida_alloc(&iio_backend_ida, GFP_KERNEL); + if (id < 0) + return id; + backend->id = id; + + dev_set_name(&backend->dev, "backend%d", backend->id); + + if (dev_fwnode(&backend->dev)) + fwnode = dev_fwnode(&backend->dev); + else + fwnode = dev_fwnode(backend->dev.parent); + device_set_node(&backend->dev, fwnode); + + return device_add(&backend->dev); +}; +EXPORT_SYMBOL(iio_backend_register); + +void iio_backend_unregister(struct iio_backend *backend) +{ + device_del(&backend->dev); +}; +EXPORT_SYMBOL(iio_backend_unregister); + +struct iio_backend *fwnode_iio_backend_get(struct fwnode_handle *fwnode, int index) +{ + struct iio_backend *backend; + struct fwnode_reference_args iiospec; + struct device *bdev; + int ret; + + if (index < 0) + return ERR_PTR(-EINVAL); + + ret = fwnode_property_get_reference_args(fwnode, "io-backends", + "#io-backend-cells", 0, + index, &iiospec); + if (ret) + return ERR_PTR(ret); + + bdev = bus_find_device_by_fwnode(&iio_bus_type, iiospec.fwnode); + if (!bdev) { + fwnode_handle_put(iiospec.fwnode); + return ERR_PTR(-EPROBE_DEFER); + } + + backend = to_iio_backend(bdev); + + fwnode_handle_put(iiospec.fwnode); + + return backend; +}; +EXPORT_SYMBOL(fwnode_iio_backend_get); diff --git a/include/linux/iio/backend.h b/include/linux/iio/backend.h new file mode 100644 index 000000000000..d90d345a4625 --- /dev/null +++ b/include/linux/iio/backend.h @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * The industrial I/O core, backend handling functions + * + * Author: Olivier Moysan . + */ + +#ifndef _IIO_BACKEND_H_ +#define _IIO_BACKEND_H_ +#include + +struct iio_backend_ops; +struct iio_backend; + +/** + * struct iio_backend_ops - operations structure for an iio_backend + * @enable: switch on backend + * @disable: switch off backend + * @read_raw: read data from backend + **/ +struct iio_backend_ops { + int (*enable)(struct iio_backend *backend); + int (*disable)(struct iio_backend *backend); + int (*read_raw)(struct iio_backend *backend, int *val, int *val2, long mask); +}; + +/** + * truct iio_backend - industrial I/O backend device + * @ops: operations structure + * @module: owner of this driver module + * @id: unique id number + * @name: unique name + * @dev: associated device + * @priv: private data pointer + **/ +struct iio_backend { + const struct iio_backend_ops *ops; + struct module *owner; + int id; + const char *name; + struct device dev; + struct list_head list; + void *priv; +}; + +struct iio_backend *iio_backend_alloc(struct device *parent); + +void iio_backend_free(struct device *dev); + +int iio_backend_register(struct iio_backend *backend); + +void iio_backend_unregister(struct iio_backend *backend); + +struct iio_backend *fwnode_iio_backend_get(struct fwnode_handle *fwnode, int index); + +#endif /* _IIO_BACKEND_H_ */