Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp477120rwb; Mon, 26 Sep 2022 23:23:23 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5bhbKkoNcdNpL0gCS1DZL8QNu6nfDPtQusda+eTvKcjMkDbH6qnsxoc3OJpIl0Dpg574HT X-Received: by 2002:a17:906:fe09:b0:77a:52b3:da5d with SMTP id wy9-20020a170906fe0900b0077a52b3da5dmr20841891ejb.57.1664259803258; Mon, 26 Sep 2022 23:23:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664259803; cv=none; d=google.com; s=arc-20160816; b=ioFsLfeZXf9lcBuLnPCtmty+GBTLo2i7+PWORajZNc6YxPPJkM+ZN75eBa1Udye504 HjtygjGTWL4u7PposHObwzdJZ1TKC9Hzu5iwgUsDqa3R+9kIgbiZ5dDZNtXxCDgpad5H ILdCVQf810C9TYJDO3WmNDDM9ZCTKQwv15ojBEl9O25dLJzBWaOFAMseO1SsrttCaz/u iXOXZZM1fr0I9H21rD8+WXpMasVdXBEnXlOL2Uohv0tfTZiiK9+QVZ9lXYvJnEGzl9Yl l1MseST/pZlzgGhSvHb2CTKTBAmYXnRtZUdSZqtVDRclfgVvdhnjHoyYSXPubt+jJLqW JAvw== 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:dkim-signature:from; bh=OUwypsP7hFxWhRgDwKULqzl3aJ5f89410roECS4MZNA=; b=CMr4oUrz/pbM3Azx9yY1zJ0YWrSt5I5PzBAVluPZ7yyGS82OzNdd72T9+9FVJd9ByV 6YMYbSkTMu0Rm6eBLXRNweLQ3EnVi2lzaGZCk1C5JXy0c/iCoB0Yv+qGnN3JPvbbdBVJ jCV/VFIYSDj//W4t4nu0KO4WM5+gcNP919Q4ogW0iN9G1/2Qb8dEd/NUKQIcOJsWUrwH vSxqZu5/J3tqU5Ui52RBKImTqiQbaNvh714X98vajUm3Vl929EDiq4fPExfXuMm+Mdbs JJ4j34b3rMg118iWeEZ8cDBBOCrPFCIuIhwqKZZhz990nYlP4b8u+0B83zkDLf5jXyWW 31mA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@puri.sm header.s=comms header.b=FpoCWRm8; 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=puri.sm Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q17-20020a1709064cd100b0074299508ae9si355258ejt.992.2022.09.26.23.22.57; Mon, 26 Sep 2022 23:23:23 -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=@puri.sm header.s=comms header.b=FpoCWRm8; 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=puri.sm Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229877AbiI0GPO (ORCPT + 99 others); Tue, 27 Sep 2022 02:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229530AbiI0GPM (ORCPT ); Tue, 27 Sep 2022 02:15:12 -0400 Received: from comms.puri.sm (comms.puri.sm [159.203.221.185]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1649B7DA; Mon, 26 Sep 2022 23:15:09 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by comms.puri.sm (Postfix) with ESMTP id 9D9EFDFB07; Mon, 26 Sep 2022 23:15:08 -0700 (PDT) Received: from comms.puri.sm ([127.0.0.1]) by localhost (comms.puri.sm [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id S77rVG9D9b_h; Mon, 26 Sep 2022 23:15:07 -0700 (PDT) From: Sebastian Krzyszkowiak DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=puri.sm; s=comms; t=1664259307; bh=q0lLN8fy8Pt559Gj7K8t+/crEZ4o049mXfWGL1MvNZo=; h=From:To:Cc:Subject:Date:From; b=FpoCWRm8MeuqaeGBu27oCW7n7vVue8Ms6zVCOLlH8DbB13uj+CvElpZtjoGLSXWO2 jDU0uPUKF5HUaTC5nKRZekFg7P53py3vLwtZjb2SMDDJUD266+cJvMy66v3r5L8924 Cd/qD92oEdaRfwAJPPg4tIUyLAcQlFxegfIk7Kje4GEu3bFanKDuXSI/hFm4q5+xMn Pc4AUps2Uc/jKnnqHsRZdDqUm4BNjb0htrKLIGoJVvWEo4JMobndyrFtXpJp7dCw+Z hxiIY4BrCAlG5t94QM63faMgzzP0sjk9Tl9HHMsM3tnOAUYh+VVm75mpHqWv/EO7aP S5TddHAU1OrWQ== To: "Rafael J. Wysocki" , Daniel Lezcano , linux-pm@vger.kernel.org Cc: Sebastian Krzyszkowiak , Amit Kucheria , Zhang Rui , Andrey Smirnov , linux-kernel@vger.kernel.org, kernel@puri.sm, stable@vger.kernel.org Subject: [PATCH v3 RESEND] thermal: qoriq: Only enable sites that actually exist Date: Tue, 27 Sep 2022 08:15:01 +0200 Message-ID: <7115709.31r3eYUQgx@pliszka> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS 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 On i.MX8MQ, enabling monitoring sites that aren't connected to anything can cause unwanted side effects on some units. This seems to happen once some of these sites report out-of-range readings and results in sensor misbehavior, such as thermal zone readings getting stuck or even suddenly reporting an impossibly high value, triggering emergency shutdowns. The datasheet lists all non-existent sites as "reserved" and doesn't make any guarantees about being able to enable them at all, so let's not do that. Instead, iterate over sensor DT nodes and only enable monitoring sites that are specified there prior to registering their thermal zones. This still fixes the issue with bogus data being reported on the first reading, but doesn't introduce problems that come with reading from non-existent sites. Fixes: 45038e03d633 ("thermal: qoriq: Enable all sensors before registering them") Cc: stable@vger.kernel.org Signed-off-by: Sebastian Krzyszkowiak --- Resent <20220321170852.654094-1-sebastian.krzyszkowiak@puri.sm> v3: add cc: stable v2: augment the commit message with details on what the patch is doing --- drivers/thermal/qoriq_thermal.c | 63 ++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/drivers/thermal/qoriq_thermal.c b/drivers/thermal/qoriq_thermal.c index 73049f9bea25..ef0848849ee2 100644 --- a/drivers/thermal/qoriq_thermal.c +++ b/drivers/thermal/qoriq_thermal.c @@ -32,7 +32,6 @@ #define TMR_DISABLE 0x0 #define TMR_ME 0x80000000 #define TMR_ALPF 0x0c000000 -#define TMR_MSITE_ALL GENMASK(15, 0) #define REGS_TMTMIR 0x008 /* Temperature measurement interval Register */ #define TMTMIR_DEFAULT 0x0000000f @@ -129,33 +128,51 @@ static const struct thermal_zone_of_device_ops tmu_tz_ops = { static int qoriq_tmu_register_tmu_zone(struct device *dev, struct qoriq_tmu_data *qdata) { - int id; + int ret = 0; + struct device_node *np, *child, *sensor_np; - if (qdata->ver == TMU_VER1) { - regmap_write(qdata->regmap, REGS_TMR, - TMR_MSITE_ALL | TMR_ME | TMR_ALPF); - } else { - regmap_write(qdata->regmap, REGS_V2_TMSR, TMR_MSITE_ALL); - regmap_write(qdata->regmap, REGS_TMR, TMR_ME | TMR_ALPF_V2); - } + np = of_find_node_by_name(NULL, "thermal-zones"); + if (!np) + return -ENODEV; + + sensor_np = of_node_get(dev->of_node); - for (id = 0; id < SITES_MAX; id++) { + for_each_available_child_of_node(np, child) { struct thermal_zone_device *tzd; - struct qoriq_sensor *sensor = &qdata->sensor[id]; - int ret; + struct qoriq_sensor *sensor; + int id, site; + + ret = thermal_zone_of_get_sensor_id(child, sensor_np, &id); + + if (ret < 0) { + dev_err(dev, "failed to get valid sensor id: %d\n", ret); + of_node_put(child); + break; + } + sensor = &qdata->sensor[id]; sensor->id = id; + /* Enable monitoring */ + if (qdata->ver == TMU_VER1) { + site = 0x1 << (15 - id); + regmap_update_bits(qdata->regmap, REGS_TMR, + site | TMR_ME | TMR_ALPF, + site | TMR_ME | TMR_ALPF); + } else { + site = 0x1 << id; + regmap_update_bits(qdata->regmap, REGS_V2_TMSR, site, site); + regmap_write(qdata->regmap, REGS_TMR, TMR_ME | TMR_ALPF_V2); + } + tzd = devm_thermal_zone_of_sensor_register(dev, id, sensor, &tmu_tz_ops); - ret = PTR_ERR_OR_ZERO(tzd); - if (ret) { - if (ret == -ENODEV) - continue; - - regmap_write(qdata->regmap, REGS_TMR, TMR_DISABLE); - return ret; + if (IS_ERR(tzd)) { + ret = PTR_ERR(tzd); + dev_err(dev, "failed to register thermal zone: %d\n", ret); + of_node_put(child); + break; } if (devm_thermal_add_hwmon_sysfs(tzd)) @@ -164,7 +181,13 @@ static int qoriq_tmu_register_tmu_zone(struct device *dev, } - return 0; + of_node_put(sensor_np); + of_node_put(np); + + if (ret) + regmap_write(qdata->regmap, REGS_TMR, TMR_DISABLE); + + return ret; } static int qoriq_tmu_calibration(struct device *dev, -- 2.35.1