Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp151433rwd; Wed, 31 May 2023 20:54:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7aY5xErS5waNVbXBRUtLslVX1h8w1cKHNv/6ZXC9C+/zW+YxSg6CmIckC94c9esB4y0Axy X-Received: by 2002:a17:902:aa86:b0:1b0:2d08:eb51 with SMTP id d6-20020a170902aa8600b001b02d08eb51mr7140559plr.12.1685591659557; Wed, 31 May 2023 20:54:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685591659; cv=none; d=google.com; s=arc-20160816; b=ZJdVW15DP41P5scFXNjkurijoVKuAU7Lla8q90rlTpLwi+asza1a46kVLrTUH7LhYB zmKvi5LcKjBinRxdCVzSgukfOen901mlbD0PJd+GpOLisGmMrcTrMmy0wQBNUtXq779l Qkzy8rEQcTnfA9++wr5mwKcKsZixh4vBV16UepPjVo57aDf1cck81vYZiVlXXkji4etC Itgb+Gulst7AAnlEtgKINARIlrJuf5ICMOgpal5DJQ7umITpxWEIfqshY4jlNvKA/zrf Xu8DPf5GYohI3uPyOA7MgMAj/F+78KC4OCngrvTlXNSdCWyXotUakCrqVt6BFGFms/RP Bwyw== 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 :message-id:date:subject:cc:to:from; bh=Fx5krmBTfhn479GAz/ljhcC4B8i/eLwI1GOcBphYU2I=; b=UA/k/0ANftHFWjKq+6oOys6fktP63CBjUzEekqblLUuJ36iAgxdJiFdoPx0jpbGhE4 v4cbriqDvEc8kB6ffa+6nkp7Zk6WJxmHzwcbb6o5Aw1noGXvLL/CmZ/74C1wpShbHah4 ZpOvPpyYbYW1faxXsKgKM7u/rc0Hz2Bam2G4O9mYzC4d7PeikdlgsKkDdRq4I16o5nq8 kxGHQc11+fbUPEt051DRAxMvhJuDTHZMP3LTj97zq8inKwstk4v8VyAN8IFuyEAzPVHV jQFg4qA+dvUqVZtzovmJo2oTJK73/7O4PPUnnq8y8AvX2LwjsaD06k+Ax5zL/EVPnoCo AyPA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=rock-chips.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id km3-20020a17090327c300b001aaf1734d1dsi1957218plb.241.2023.05.31.20.54.06; Wed, 31 May 2023 20:54:19 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=rock-chips.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231171AbjFADkV (ORCPT + 99 others); Wed, 31 May 2023 23:40:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229527AbjFADkS (ORCPT ); Wed, 31 May 2023 23:40:18 -0400 X-Greylist: delayed 345 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 31 May 2023 20:40:13 PDT Received: from mail-m11878.qiye.163.com (mail-m11878.qiye.163.com [115.236.118.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF1A9128; Wed, 31 May 2023 20:40:13 -0700 (PDT) Received: from localhost.localdomain (unknown [58.22.7.114]) by mail-m11878.qiye.163.com (Hmail) with ESMTPA id F1464740464; Thu, 1 Jun 2023 11:34:24 +0800 (CST) From: David Wu To: wsa@kernel.org, linux-i2c@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Zhang aihui , David Wu Subject: [PATCH] i2c: Devices which have some i2c addr can work in same i2c bus Date: Thu, 1 Jun 2023 11:34:23 +0800 Message-Id: <20230601033423.172021-1-david.wu@rock-chips.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFDSUNOT01LS0k3V1ktWUFJV1kPCRoVCBIfWUFZQk5IGFZMQ0MfTx1NSx5OHx1VEwETFh oSFyQUDg9ZV1kYEgtZQVlOQ1VJSVVMVUpKT1lXWRYaDxIVHRRZQVlPS0hVSkpLQ0hJVUpLS1VLWQ Y+ X-HM-Tid: 0a8875055a412eb4kusnf1464740464 X-HM-MType: 1 X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6Ny46Szo6Lz1DIjhLGRcsHz8x EEkwCw5VSlVKTUNOTkJLT01OTU5IVTMWGhIXVR8aDRIfVQwOOwkUGBBWGBMSCwhVGBQWRVlXWRIL WUFZTkNVSUlVTFVKSk9ZV1kIAVlBT0xDSTcG X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Zhang aihui If i2c slave devices don't work at the same time, which have the same i2c addr, it would register two devices, can make them working. Change-Id: I1bfb7783924b08bdc6e12bf47c2de01bdac7c2e2 Signed-off-by: Zhang aihui Signed-off-by: David Wu --- drivers/i2c/i2c-core-base.c | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index ae3af738b03f..53a8141e6238 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -62,6 +62,7 @@ static DEFINE_MUTEX(core_lock); static DEFINE_IDR(i2c_adapter_idr); +static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr); static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key); @@ -849,7 +850,8 @@ static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter, static void i2c_dev_set_name(struct i2c_adapter *adap, struct i2c_client *client, - struct i2c_board_info const *info) + struct i2c_board_info const *info, + int status) { struct acpi_device *adev = ACPI_COMPANION(&client->dev); @@ -863,8 +865,12 @@ static void i2c_dev_set_name(struct i2c_adapter *adap, return; } - dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), - i2c_encode_flags_to_addr(client)); + if (status == 0) + dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), + i2c_encode_flags_to_addr(client)); + else + dev_set_name(&client->dev, "%d-%04x-%01x", i2c_adapter_id(adap), + i2c_encode_flags_to_addr(client), status); } int i2c_dev_irq_from_resources(const struct resource *resources, @@ -940,9 +946,11 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf } /* Check for address business */ - status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client)); + status = i2c_check_addr_ex(adap, i2c_encode_flags_to_addr(client)); if (status) - goto out_err; + dev_err(&adap->dev, + "%d i2c clients have been registered at 0x%02x", + status, client->addr); client->dev.parent = &client->adapter->dev; client->dev.bus = &i2c_bus_type; @@ -951,7 +959,7 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf client->dev.fwnode = info->fwnode; device_enable_async_suspend(&client->dev); - i2c_dev_set_name(adap, client, info); + i2c_dev_set_name(adap, client, info, status); if (info->swnode) { status = device_add_software_node(&client->dev, info->swnode); @@ -976,10 +984,6 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf device_remove_software_node(&client->dev); out_err_put_of_node: of_node_put(info->of_node); -out_err: - dev_err(&adap->dev, - "Failed to register i2c client %s at 0x%02x (%d)\n", - client->name, client->addr, status); out_err_silent: kfree(client); return ERR_PTR(status); @@ -1988,6 +1992,33 @@ EXPORT_SYMBOL(i2c_del_driver); /* ------------------------------------------------------------------------- */ +struct i2c_addr_cnt { + int addr; + int cnt; +}; + +static int __i2c_check_addr_ex(struct device *dev, void *addrp) +{ + struct i2c_client *client = i2c_verify_client(dev); + struct i2c_addr_cnt *addrinfo = (struct i2c_addr_cnt *)addrp; + int addr = addrinfo->addr; + + if (client && client->addr == addr) + addrinfo->cnt++; + + return 0; +} + +static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr) +{ + struct i2c_addr_cnt addrinfo; + + addrinfo.addr = addr; + addrinfo.cnt = 0; + device_for_each_child(&adapter->dev, &addrinfo, __i2c_check_addr_ex); + return addrinfo.cnt; +} + struct i2c_cmd_arg { unsigned cmd; void *arg; -- 2.25.1