Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5501140imm; Tue, 31 Jul 2018 12:01:26 -0700 (PDT) X-Google-Smtp-Source: AAOMgpf6SUIIdvTv7zRfnJBAoGKgViA+JregcHObC5a+zSdch3TBwS7CEfvQR9eu3SJmft9dyT8/ X-Received: by 2002:a65:5c83:: with SMTP id a3-v6mr21797538pgt.164.1533063686249; Tue, 31 Jul 2018 12:01:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533063686; cv=none; d=google.com; s=arc-20160816; b=MwzphMQMsnTh9qxH5InHFE03bfx58ATJ9Q35giZZgFw2cuRAZODohalEKu5rBOgyxJ tPIpGo5fXhpb8iobzv/m9P7GhCARByQQASUhYOMSiFjru0LOX7tuGAKsHtz86CmtXZmF Xf4oPjKpGzfFdbLH6CHq9uhA5YmvufWXArTYwrsBfRTKa1En1MviiU7LdwPqf/RoR8Q5 U8QJHKZZwsn67vMcbdzfrHBtCext1NWv4K95mbMe11dSgVKaBJukoI2qm0D7yisKvl3i lbjzk3wBuOTCMAblGUvqhyfgJdGmfrjdb/6pEsXuErmlbJMvEdebny1/633YNXkvo5o1 78pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=Q/YRFKb3RzI0H1RpA3VS7FFvDUztiE2CUhFcLa1cepw=; b=uLXya4YS0RlJ+jj8xGNHkHqAkVQdc7LHbB6yl3CDc6bV9Co9443KRHtYXXhZwZwrrB lJpFc2RIzU6SGICH1YdH8nHrmDT5NONGF2QsHi4dU37tJBe+iitHKAvevutgkzr147z5 eLW/vf08SJBlL9U1Pl1YQeaGuWJohcd+QDArYG6eI7Dw/FXVyRM223neSO018SfWhN+S jGZwalwA0mBRN9fmzKN+Jv9vbH3m9G2F7H3IQjkyXoLiR7F8zNfIluWn7+F2OX9fUagg uHfRtddeSg30GsLdBS4OJ7bBaR2H4lGGNjxc+Tz8vR2koCc1pVYd9uBVVetTDTX9Eck5 aN4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Co+P6W3x; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q90-v6si14506595pfa.272.2018.07.31.12.01.07; Tue, 31 Jul 2018 12:01:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Co+P6W3x; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732285AbeGaUl3 (ORCPT + 99 others); Tue, 31 Jul 2018 16:41:29 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:34919 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732211AbeGaUl1 (ORCPT ); Tue, 31 Jul 2018 16:41:27 -0400 Received: by mail-pl0-f65.google.com with SMTP id w3-v6so7568018plq.2 for ; Tue, 31 Jul 2018 11:59:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q/YRFKb3RzI0H1RpA3VS7FFvDUztiE2CUhFcLa1cepw=; b=Co+P6W3x0rRIdXLxPhw37GtUJOLtELbw1BmXWuPGvPEKehgO3bw176RwaVhdSJeoTq UeUUlzE5ieYrVo4IhMQwUQJiyj4RQhSRZ0/0FwucGnrwIiLHj6egBZPsypuqicCx+3Cm OGVruMmy1k05jzDM3VdMejFzp30WxfnJ63Fnw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q/YRFKb3RzI0H1RpA3VS7FFvDUztiE2CUhFcLa1cepw=; b=JxTLsGpSkot9dCyq01W9QiHHVjjStf3kmqMM1j8LR6ZmWXo2jxtc5+A4xVYq1sMaQh 1YrOxvWSRLKdnVckLH+X44w9Oh+T17D/LT0VEMkFjiagBKmtcPj4ocV6Q8vU6llBj5xF UeWaLnpeQQEELseaiq1QfB5vtgwNrf1ckG1zuKnN8ztk6ZWCfgK3P3yIJrCPUqCn1ojq Xw/8xL0FprTCD7EkEJQ6Hl4fD7BDDwuxctLEkv85y3zdVmI0YIJpTU86h43E+cIaZ70A oNR1l5mGf//I2yt0KYXMJjeoCiGXUBi1/mteUiuDZeuHGWXE7Q0jxrHxu4LiCnTtG2BY e3gw== X-Gm-Message-State: AOUpUlGK+cEIDTRM9BJ2g3+kiQD9XwndInU5/S8y00p++Bx94u5JSnID X+ixRu3QcnSEF8AV/j62Xj8nFw== X-Received: by 2002:a17:902:184:: with SMTP id b4-v6mr21941058plb.340.1533063586567; Tue, 31 Jul 2018 11:59:46 -0700 (PDT) Received: from mka.mtv.corp.google.com ([2620:15c:202:1:b6af:f85:ed6c:ac6a]) by smtp.gmail.com with ESMTPSA id e132-v6sm27797567pfc.49.2018.07.31.11.59.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Jul 2018 11:59:45 -0700 (PDT) From: Matthias Kaehlcke To: Andy Gross , David Brown , Rob Herring , Mark Rutland , Catalin Marinas , Will Deacon , Zhang Rui , Eduardo Valentin Cc: linux-soc@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pm@vger.kernel.org, David Collins , Douglas Anderson , Stephen Boyd , Matthias Kaehlcke Subject: [PATCH v6 3/5] thermal: qcom-spmi: Use PMIC thermal stage 2 for critical trip points Date: Tue, 31 Jul 2018 11:59:15 -0700 Message-Id: <20180731185917.176074-3-mka@chromium.org> X-Mailer: git-send-email 2.18.0.345.g5c9ce644c3-goog In-Reply-To: <20180731185917.176074-1-mka@chromium.org> References: <20180731185917.176074-1-mka@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are three thermal stages defined in the PMIC: stage 1: warning stage 2: system should shut down stage 3: emergency shut down By default the PMIC assumes that the OS isn't doing anything and thus at stage 2 it does a partial PMIC shutdown and at stage 3 it kills all power. When switching between thermal stages the PMIC generates an interrupt which is handled by the driver. The partial PMIC shutdown at stage 2 can be disabled by software, which allows the OS to initiate a shutdown at stage 2 with a thermal zone configured accordingly. If a critical trip point is configured in the thermal zone the driver adjusts the stage 1-3 temperature thresholds to (closely) match the critical temperature with a stage 2 threshold (125/130/135/140 °C). If a suitable match is found the partial shutdown at stage 2 is disabled. If for some reason the system doesn't shutdown at stage 2 the emergency shutdown at stage 3 kicks in. The partial shutdown at stage 2 remains enabled in these cases: - no critical trip point defined - the temperature of the critical trip point is < 125°C - the temperature of the critical trip point is > 140°C and no ADC channel is configured (thus the OS is not notified when the critical temperature is reached) Suggested-by: Douglas Anderson Signed-off-by: Matthias Kaehlcke --- Changes in v6: - fixed condition to check if ADC is configured in qpnp_tm_update_critical_trip_temp() - changed °C in logs to C - removed needless evaluation of qpnp_tm_write() return value in qpnp_tm_update_critical_trip_temp() - move assignment of chip->initialized to true to qpnp_tm_init(), where the lock is held - call thermal_zone_device_update() after initialization is completed - split some #define + comment in two lines to avoid exceeding chars per line limit - removed extra closing parenthesis in qpnp_tm_get_temp() - remove unnecessary parentheses around conditions in qpnp_tm_update_critical_trip_temp() and qpnp_tm_get_critical_trip_temp() - fixed indentation of call devm_thermal_zone_of_sensor_register() call in qpnp_tm_probe() Changes in v5: - patch added to the series --- drivers/thermal/qcom-spmi-temp-alarm.c | 158 ++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 18 deletions(-) diff --git a/drivers/thermal/qcom-spmi-temp-alarm.c b/drivers/thermal/qcom-spmi-temp-alarm.c index ad4f3a8d6560..b2d5d5bf4a9b 100644 --- a/drivers/thermal/qcom-spmi-temp-alarm.c +++ b/drivers/thermal/qcom-spmi-temp-alarm.c @@ -23,6 +23,8 @@ #include #include +#include "thermal_core.h" + #define QPNP_TM_REG_TYPE 0x04 #define QPNP_TM_REG_SUBTYPE 0x05 #define QPNP_TM_REG_STATUS 0x08 @@ -37,9 +39,11 @@ #define STATUS_GEN2_STATE_MASK GENMASK(6, 4) #define STATUS_GEN2_STATE_SHIFT 4 -#define SHUTDOWN_CTRL1_OVERRIDE_MASK GENMASK(7, 6) +#define SHUTDOWN_CTRL1_OVERRIDE_S2 BIT(6) #define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0) +#define SHUTDOWN_CTRL1_RATE_25HZ BIT(3) + #define ALARM_CTRL_FORCE_ENABLE BIT(7) /* @@ -56,12 +60,19 @@ #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */ #define THRESH_MIN 0 +#define THRESH_MAX 3 + +/* Stage 2 Threshold Min: 125 C */ +#define STAGE2_THRESHOLD_MIN 125000 +/* Stage 2 Threshold Max: 140 C */ +#define STAGE2_THRESHOLD_MAX 140000 /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */ #define DEFAULT_TEMP 37000 struct qpnp_tm_chip { struct regmap *map; + struct device *dev; struct thermal_zone_device *tz_dev; unsigned int subtype; long temp; @@ -69,6 +80,10 @@ struct qpnp_tm_chip { unsigned int stage; unsigned int prev_stage; unsigned int base; + /* protects .thresh, .stage and chip registers */ + struct mutex lock; + bool initialized; + struct iio_channel *adc; }; @@ -125,6 +140,8 @@ static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip) unsigned int stage, stage_new, stage_old; int ret; + WARN_ON(!mutex_is_locked(&chip->lock)); + ret = qpnp_tm_get_temp_stage(chip); if (ret < 0) return ret; @@ -163,8 +180,15 @@ static int qpnp_tm_get_temp(void *data, int *temp) if (!temp) return -EINVAL; + if (!chip->initialized) { + *temp = DEFAULT_TEMP; + return 0; + } + if (!chip->adc) { + mutex_lock(&chip->lock); ret = qpnp_tm_update_temp_no_adc(chip); + mutex_unlock(&chip->lock); if (ret < 0) return ret; } else { @@ -180,8 +204,72 @@ static int qpnp_tm_get_temp(void *data, int *temp) return 0; } +static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip, + int temp) +{ + u8 reg; + bool disable_s2_shutdown = false; + + WARN_ON(!mutex_is_locked(&chip->lock)); + + /* + * Default: S2 and S3 shutdown enabled, thresholds at + * 105C/125C/145C, monitoring at 25Hz + */ + reg = SHUTDOWN_CTRL1_RATE_25HZ; + + if (temp == THERMAL_TEMP_INVALID || + temp < STAGE2_THRESHOLD_MIN) { + chip->thresh = THRESH_MIN; + goto skip; + } + + if (temp <= STAGE2_THRESHOLD_MAX) { + chip->thresh = THRESH_MAX - + ((STAGE2_THRESHOLD_MAX - temp) / + TEMP_THRESH_STEP); + disable_s2_shutdown = true; + } else { + chip->thresh = THRESH_MAX; + + if (chip->adc) + disable_s2_shutdown = true; + else + dev_warn(chip->dev, + "No ADC is configured and critical temperature is above the maximum stage 2 threshold of 140 C! Configuring stage 2 shutdown at 140 C.\n"); + } + +skip: + reg |= chip->thresh; + if (disable_s2_shutdown) + reg |= SHUTDOWN_CTRL1_OVERRIDE_S2; + + return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg); +} + +static int qpnp_tm_set_trip_temp(void *data, int trip, int temp) +{ + struct qpnp_tm_chip *chip = data; + const struct thermal_trip *trip_points; + int ret; + + trip_points = of_thermal_get_trip_points(chip->tz_dev); + if (!trip_points) + return -EINVAL; + + if (trip_points[trip].type != THERMAL_TRIP_CRITICAL) + return 0; + + mutex_lock(&chip->lock); + ret = qpnp_tm_update_critical_trip_temp(chip, temp); + mutex_unlock(&chip->lock); + + return ret; +} + static const struct thermal_zone_of_device_ops qpnp_tm_sensor_ops = { .get_temp = qpnp_tm_get_temp, + .set_trip_temp = qpnp_tm_set_trip_temp, }; static irqreturn_t qpnp_tm_isr(int irq, void *data) @@ -193,6 +281,29 @@ static irqreturn_t qpnp_tm_isr(int irq, void *data) return IRQ_HANDLED; } +static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip) +{ + int ntrips; + const struct thermal_trip *trips; + int i; + + ntrips = of_thermal_get_ntrips(chip->tz_dev); + if (ntrips <= 0) + return THERMAL_TEMP_INVALID; + + trips = of_thermal_get_trip_points(chip->tz_dev); + if (!trips) + return THERMAL_TEMP_INVALID; + + for (i = 0; i < ntrips; i++) { + if (of_thermal_is_trip_valid(chip->tz_dev, i) && + trips[i].type == THERMAL_TRIP_CRITICAL) + return trips[i].temperature; + } + + return THERMAL_TEMP_INVALID; +} + /* * This function initializes the internal temp value based on only the * current thermal stage and threshold. Setup threshold control and @@ -203,17 +314,20 @@ static int qpnp_tm_init(struct qpnp_tm_chip *chip) unsigned int stage; int ret; u8 reg = 0; + int crit_temp; + + mutex_lock(&chip->lock); ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®); if (ret < 0) - return ret; + goto out; chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK; chip->temp = DEFAULT_TEMP; ret = qpnp_tm_get_temp_stage(chip); if (ret < 0) - return ret; + goto out; chip->stage = ret; stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1 @@ -224,21 +338,19 @@ static int qpnp_tm_init(struct qpnp_tm_chip *chip) (stage - 1) * TEMP_STAGE_STEP + TEMP_THRESH_MIN; - /* - * Set threshold and disable software override of stage 2 and 3 - * shutdowns. - */ - chip->thresh = THRESH_MIN; - reg &= ~(SHUTDOWN_CTRL1_OVERRIDE_MASK | SHUTDOWN_CTRL1_THRESHOLD_MASK); - reg |= chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK; - ret = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg); + crit_temp = qpnp_tm_get_critical_trip_temp(chip); + ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp); if (ret < 0) - return ret; + goto out; /* Enable the thermal alarm PMIC module in always-on mode. */ reg = ALARM_CTRL_FORCE_ENABLE; ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg); + chip->initialized = true; + +out: + mutex_unlock(&chip->lock); return ret; } @@ -257,6 +369,9 @@ static int qpnp_tm_probe(struct platform_device *pdev) return -ENOMEM; dev_set_drvdata(&pdev->dev, chip); + chip->dev = &pdev->dev; + + mutex_init(&chip->lock); chip->map = dev_get_regmap(pdev->dev.parent, NULL); if (!chip->map) @@ -302,6 +417,18 @@ static int qpnp_tm_probe(struct platform_device *pdev) chip->subtype = subtype; + /* + * Register the sensor before initializing the hardware to be able to + * read the trip points. get_temp() returns the default temperature + * before the hardware initialization is completed. + */ + chip->tz_dev = devm_thermal_zone_of_sensor_register( + &pdev->dev, 0, chip, &qpnp_tm_sensor_ops); + if (IS_ERR(chip->tz_dev)) { + dev_err(&pdev->dev, "failed to register sensor\n"); + return PTR_ERR(chip->tz_dev); + } + ret = qpnp_tm_init(chip); if (ret < 0) { dev_err(&pdev->dev, "init failed\n"); @@ -313,12 +440,7 @@ static int qpnp_tm_probe(struct platform_device *pdev) if (ret < 0) return ret; - chip->tz_dev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, chip, - &qpnp_tm_sensor_ops); - if (IS_ERR(chip->tz_dev)) { - dev_err(&pdev->dev, "failed to register sensor\n"); - return PTR_ERR(chip->tz_dev); - } + thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); return 0; } -- 2.18.0.345.g5c9ce644c3-goog