Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp325807pxb; Mon, 7 Feb 2022 12:17:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJx9nDjBfyc1rPURK26Kc4jqdcv4Q8dasNJH/Jmk1zwWjMpO4jtgXTmL1voWGWYuOGZXcNyG X-Received: by 2002:a17:906:4dd5:: with SMTP id f21mr1083003ejw.474.1644265066849; Mon, 07 Feb 2022 12:17:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644265066; cv=none; d=google.com; s=arc-20160816; b=NIJ2kk+WiNJs7kbwg1EOaz8oZ5OlRnwD3fzKoGKo5U9XekBbtvgbW7djRdMVIrL5No Mt+aI0si8/McfFF4PH/YtxOiynj4eZUWONuMuxhZQrjfZCh6Ui7rZL4cTOgXI0OiHfKL vsb5xQ8FY5AL1LTbejOICsNRm75nttgd2SNi172p6px/Mp2XPY/w1FK7hHISq13afqom D7Fk38ioGBaidioeGlRCMDGmpxEF5FYcTv1dQmUglpDhcuKnPKnO6Iul7RpJKOzD7bB0 joFryDdLgZ/PUnnA2zKOIur+OqM98EE1VptdiwLwStODC77IcYqb7LiFdaBFnrdQBz4M hcWw== 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; bh=PM9urVhPmMFafxM+iDNJ1CdcW/IofGAhjQHHzY9RhRM=; b=bbAg5mNHFvu15dZfh1wrWKrlLykkhaRUB/gO1TBCqFetP55PD/jbA5QnSANjFeHDWN KuuNrf7Myu3QAkx6Rk4p9pPCLNK/m+dQUG7/IpTjxPIHmpSpxUAHDKyNSE6mdgGGI/HG xooY8bzjMsGhpY6dPn2JgaNNpeEhBwPWdaF/l1wJSCeyK+/+5kgmr2htdoiRMzZKFp8h SNrqdHqWNPllxBqPDIvPwIETorbuyeyumHTSRYCuJmlrWf/RK4OBYi59JK6byyP1+Bd1 w9WO0kfcBVChNnRW+eoULY70BriG7fHwN3T7VtFScAzIuyIJnTNgJ0Yb79W+ca1l95gV RqBw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hc30si8042360ejc.253.2022.02.07.12.17.22; Mon, 07 Feb 2022 12:17:46 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241387AbiBFN7O (ORCPT + 99 others); Sun, 6 Feb 2022 08:59:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242164AbiBFN6s (ORCPT ); Sun, 6 Feb 2022 08:58:48 -0500 Received: from hostingweb31-40.netsons.net (hostingweb31-40.netsons.net [89.40.174.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A74EC0401E3; Sun, 6 Feb 2022 05:58:38 -0800 (PST) Received: from [77.244.183.192] (port=63680 helo=melee.fritz.box) by hostingweb31.netsons.net with esmtpa (Exim 4.94.2) (envelope-from ) id 1nGgD1-00059v-Kg; Sun, 06 Feb 2022 12:59:59 +0100 From: Luca Ceresoli To: linux-media@vger.kernel.org, linux-i2c@vger.kernel.org Cc: Luca Ceresoli , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Rob Herring , Mark Rutland , Wolfram Sang , Sakari Ailus , Hans Verkuil , Laurent Pinchart , Kieran Bingham , Jacopo Mondi , Vladimir Zapolskiy , Peter Rosin , Mauro Carvalho Chehab , Tomi Valkeinen , matti.vaittinen@fi.rohmeurope.com Subject: [RFCv3 1/6] i2c: core: let adapters be notified of client attach/detach Date: Sun, 6 Feb 2022 12:59:34 +0100 Message-Id: <20220206115939.3091265-2-luca@lucaceresoli.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220206115939.3091265-1-luca@lucaceresoli.net> References: <20220206115939.3091265-1-luca@lucaceresoli.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hostingweb31.netsons.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lucaceresoli.net X-Get-Message-Sender-Via: hostingweb31.netsons.net: authenticated_id: luca+lucaceresoli.net/only user confirmed/virtual account not confirmed X-Authenticated-Sender: hostingweb31.netsons.net: luca@lucaceresoli.net X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, 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 An adapter might need to know when a new device is about to be added. This will soon bee needed to implement an "I2C address translator" (ATR for short), a device that propagates I2C transactions with a different slave address (an "alias" address). An ATR driver needs to know when a slave is being added to find a suitable alias and program the device translation map. Add an attach/detach callback pair to allow adapter drivers to be notified of clients being added and removed. Signed-off-by: Luca Ceresoli --- Changes RFCv2 -> RFCv3: - rebase on current master Changes RFCv1 -> RFCv2: - Document struct i2c_attach_operations --- drivers/i2c/i2c-core-base.c | 18 +++++++++++++++++- include/linux/i2c.h | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 2c59dd748a49..aea1e1b552b0 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -966,15 +966,23 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf } } + if (adap->attach_ops && + adap->attach_ops->attach_client && + adap->attach_ops->attach_client(adap, info, client) != 0) + goto out_remove_swnode; + status = device_register(&client->dev); if (status) - goto out_remove_swnode; + goto out_detach_client; dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", client->name, dev_name(&client->dev)); return client; +out_detach_client: + if (adap->attach_ops && adap->attach_ops->detach_client) + adap->attach_ops->detach_client(adap, client); out_remove_swnode: device_remove_software_node(&client->dev); out_err_put_of_node: @@ -996,9 +1004,17 @@ EXPORT_SYMBOL_GPL(i2c_new_client_device); */ void i2c_unregister_device(struct i2c_client *client) { + struct i2c_adapter *adap; + if (IS_ERR_OR_NULL(client)) return; + adap = client->adapter; + + if (adap->attach_ops && + adap->attach_ops->detach_client) + adap->attach_ops->detach_client(adap, client); + if (client->dev.of_node) { of_node_clear_flag(client->dev.of_node, OF_POPULATED); of_node_put(client->dev.of_node); diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 7d4f52ceb7b5..aadd71e0533c 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -587,6 +587,21 @@ struct i2c_lock_operations { void (*unlock_bus)(struct i2c_adapter *adapter, unsigned int flags); }; +/** + * struct i2c_attach_operations - callbacks to notify client attach/detach + * @attach_client: Notify of new client being attached + * @detach_client: Notify of new client being detached + * + * Both ops are optional. + */ +struct i2c_attach_operations { + int (*attach_client)(struct i2c_adapter *adapter, + const struct i2c_board_info *info, + const struct i2c_client *client); + void (*detach_client)(struct i2c_adapter *adapter, + const struct i2c_client *client); +}; + /** * struct i2c_timings - I2C timing information * @bus_freq_hz: the bus frequency in Hz @@ -729,6 +744,7 @@ struct i2c_adapter { /* data fields that are valid for all devices */ const struct i2c_lock_operations *lock_ops; + const struct i2c_attach_operations *attach_ops; struct rt_mutex bus_lock; struct rt_mutex mux_lock; -- 2.25.1