From patchwork Mon Jan 3 16:33:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcello Sylvester Bauer X-Patchwork-Id: 12702663 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54A63C433FE for ; Mon, 3 Jan 2022 16:41:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231415AbiACQlk (ORCPT ); Mon, 3 Jan 2022 11:41:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230170AbiACQlk (ORCPT ); Mon, 3 Jan 2022 11:41:40 -0500 X-Greylist: delayed 415 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 03 Jan 2022 08:41:40 PST Received: from mout-y-111.mailbox.org (mout-y-111.mailbox.org [IPv6:2001:67c:2050:1::465:111]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C3A3C061784; Mon, 3 Jan 2022 08:41:40 -0800 (PST) Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:105:465:1:4:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-y-111.mailbox.org (Postfix) with ESMTPS id 4JSLvB4T1qzQlLR; Mon, 3 Jan 2022 17:35:02 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sylv.io; s=MBO0001; t=1641227702; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Gy/SaMPYaRnqLDcyWl/RE0Zx5SaX9Qnp25v+7IjRdcc=; b=IYv8pjjjq+7PPs6xmJHoJpSyoGDum3I+CTKEbVx8gruS0exHuCIanAlwMLSX2lC1lXWw7q e5alSIeGAHo2focbad/0q4VIbZf+dP2LSlH+wbVicCZkFm2SWrIT8a0JMCIAVudQRulNGh S9hs6PVgZWYr5LClCzGK9/T72CQyb2Loskf0XwJZjwXrPveWZ3YwMGpNWP6KtUmjxqkdBp XDtc8gaKLBAJRFAgYJmTGMoGL8avIpgc54OyTdGCo27HjCpe6wyqIDHlFEbpZHQj9H9s0V eExUtG0rXUNS6+vockFk5hK3bwjHCIHru8uujwrDSP1AOJ6cswxlb2Gs6/svUA== From: Marcello Sylvester Bauer To: linux-hwmon@vger.kernel.org Cc: Marcello Sylvester Bauer , Patrick Rudolph , Jean Delvare , Guenter Roeck , Liam Girdwood , Mark Brown , linux-kernel@vger.kernel.org Subject: [PATCH v1 2/4] hwmon: (max6639) Add regulator support Date: Mon, 3 Jan 2022 17:33:52 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Add regulator support for boards where the fan-supply have to be powered up before it can be used. Signed-off-by: Patrick Rudolph Signed-off-by: Marcello Sylvester Bauer --- drivers/hwmon/max6639.c | 75 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c index ccc0f047bd44..d733c35b5bcf 100644 --- a/drivers/hwmon/max6639.c +++ b/drivers/hwmon/max6639.c @@ -87,6 +87,9 @@ struct max6639_data { /* Register values initialized only once */ u8 ppr; /* Pulses per rotation 0..3 for 1..4 ppr */ u8 rpm_range; /* Index in above rpm_ranges table */ + + /* Optional regulator for FAN supply */ + struct regulator *reg; }; static struct max6639_data *max6639_update_device(struct device *dev) @@ -296,6 +299,17 @@ static ssize_t pwm_store(struct device *dev, return res; val = clamp_val(val, 0, 255); + if (data->reg) { + res = 0; + if (val == 0) + res = regulator_disable(data->reg); + else + res = regulator_enable(data->reg); + if (res) { + dev_err(dev, "Failed to operate fan supply: %d\n", res); + return res; + } + } mutex_lock(&data->update_lock); data->pwm[attr->index] = (u8)(val * 120 / 255); @@ -516,6 +530,11 @@ static int max6639_detect(struct i2c_client *client, return 0; } +static void max6639_regulator_disable(void *data) +{ + regulator_disable(data); +} + static int max6639_probe(struct i2c_client *client) { struct device *dev = &client->dev; @@ -528,6 +547,30 @@ static int max6639_probe(struct i2c_client *client) return -ENOMEM; data->client = client; + + data->reg = devm_regulator_get_optional(dev, "fan"); + if (IS_ERR(data->reg)) { + if (PTR_ERR(data->reg) != -ENODEV) { + err = (int)PTR_ERR(data->reg); + dev_warn(dev, "Failed looking up fan supply: %d\n", err); + return err; + } + data->reg = NULL; + } else { + /* Spin up fans */ + err = regulator_enable(data->reg); + if (err) { + dev_err(dev, "Failed to enable fan supply: %d\n", err); + return err; + } + err = devm_add_action_or_reset(dev, max6639_regulator_disable, + data->reg); + if (err) { + dev_err(dev, "Failed to register action: %d\n", err); + return err; + } + } + mutex_init(&data->update_lock); /* Initialize the max6639 chip */ @@ -545,23 +588,39 @@ static int max6639_probe(struct i2c_client *client) static int max6639_suspend(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); - int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); - if (data < 0) - return data; + struct max6639_data *data = dev_get_drvdata(dev); + int ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); + + if (ret < 0) + return ret; + + if (data->reg) + regulator_disable(data->reg); return i2c_smbus_write_byte_data(client, - MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY); + MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY); } static int max6639_resume(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); - int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); - if (data < 0) - return data; + struct max6639_data *data = dev_get_drvdata(dev); + int ret; + + if (data->reg) { + ret = regulator_enable(data->reg); + if (ret) { + dev_err(dev, "Failed to enable fan supply: %d\n", ret); + return ret; + } + } + + ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); + if (ret < 0) + return ret; return i2c_smbus_write_byte_data(client, - MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY); + MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY); } #endif /* CONFIG_PM_SLEEP */