Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1241941rdh; Fri, 27 Oct 2023 08:29:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGlL6c8JryfxMNIK2ymx1K6R2dUdxssUASQ3gZgr2O8Bfas1zN6g2X2K655CCrue4d8rn8p X-Received: by 2002:a25:30d:0:b0:da0:4f22:4eda with SMTP id 13-20020a25030d000000b00da04f224edamr2738366ybd.36.1698420552561; Fri, 27 Oct 2023 08:29:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698420552; cv=none; d=google.com; s=arc-20160816; b=jLtzekgqgdHxrwmTwm472zBXFzL5zSoDQHgqgpOP+ais0jcLsK+Z7Yf5HGrO+eVUjV TVYAKMSwdii/cPI2bZ188ibm04benTK5uxDqgKes1E+xZyv/Rw5XnfhvsGvgJFYnf3e8 MwEcdQ2DK5OqadBsCudcibaQlO2e56R9/pUovpLz8UKNjb4OsNOJC2SYSURXQ4Wul/na yjZ1GxU0BUY3j2OisquIMrpEy7J3J8c/ZB1SgPqlVWAIoreJuq0vOEI2nID8VBMi+FkD 8x9iB/uEfm0YM4HLeyE422zJ6Wvh9FFxErTRAhoKOkG5g68/47JAvNOilI5NxN9zcfmf O69g== 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:dkim-signature; bh=GXUmekLw1GEFnBbnbx9xiufl4bE1wbCiB6K9UiIw9hY=; fh=rtbxTZERLuuPzxUIrI9qbFkc7YAqYsZWrLv0VJdgAQU=; b=LQJgeTy4GGT1hXIxDiWnwZASnNlur8d9ZPYiZpGx9NRYeSEfspZ6IFQDsNeUmTTi7U oMfUSfbSnKNbrOno/RVM269Ba1DcWl0ew9Y2EQAfb/2WB6EnPj8V7NYOwqa3TeD4sCO2 BzoyYKCudWMnnJ3W7+EuPhTxoHiFDeJ9mPM/gGJg8InTGv6BSBFOH+bzVARDOolfqB6z WY38JtjN6Kj2RiJhgMiE9Mk4aFkYl211Bv77E55rOr0xW93mV8ZeAa+ULOQPbxi0v/ld 4wA5RUPgn/PFdYWoNS+cZrpsuwb4nSG6xbTp4xqtndPq8/CiQvlae44E2Jj60+lYN/Lc vwQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@9elements.com header.s=google header.b=OlbMcgqb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=9elements.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id p68-20020a25d847000000b00d9111959e93si3409017ybg.497.2023.10.27.08.29.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 08:29:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@9elements.com header.s=google header.b=OlbMcgqb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=9elements.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id A32BA832A65B; Fri, 27 Oct 2023 08:29:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346270AbjJ0P2l (ORCPT + 99 others); Fri, 27 Oct 2023 11:28:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235117AbjJ0P2k (ORCPT ); Fri, 27 Oct 2023 11:28:40 -0400 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [IPv6:2a00:1450:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DA7818A for ; Fri, 27 Oct 2023 08:28:37 -0700 (PDT) Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2c50cd16f3bso30994001fa.2 for ; Fri, 27 Oct 2023 08:28:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; t=1698420515; x=1699025315; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=GXUmekLw1GEFnBbnbx9xiufl4bE1wbCiB6K9UiIw9hY=; b=OlbMcgqbh9KDvhDSF2SUUn2kTEG67Vmq5a9kte9IVj9PFZvfhLeFsKncbF6/+Jjs8U KZFxUf2KE+KJyV16k6HG7JYqY6BgJlcYSfbjhrAwY4vWGcTZdIopmzf2p08Yq2ke2zmZ YUjRq+edna3NcrKZ7yp2gYkzdpGyIpGIw6q9L3YvbiNoUozd8YtgG3tEFbX5PMUnS2sA f5EpFne4UOgdt+qAhLPN6erMHGEqeuqMEwaK2SMXlX1GnromhnmwWQPoVzxaymGJ9vKw LfF1nvp9kwJ8gNN3mFWMobM1lVn/WOCnNZbK28HmfgEAbq1MXxRajQyN6+I4vWjQgpCx s0EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698420515; x=1699025315; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GXUmekLw1GEFnBbnbx9xiufl4bE1wbCiB6K9UiIw9hY=; b=QaFRMBBgC7N0UJYythSxcxqlnQfX2sSHjUNB6dko/mjD3biry2jCcjKD6zb9eqIiWl N6QeEXDbP0AhkF83Cej8P8UsB7oa8occvicJWQi3dAL7M8rqLwAL4ziaag05GvfHALJN ODJtOmJJ05KTzNUoqfsL6ZcnJkouJsJI5PKf+ExnAsrzuQhTphC4q/apPfUO6j3IOLEt atKeL7O+cf/fB9ZVz3u6hMnx+TWsLlmHU+eLE7Vs3W/ZE5xsSOONeMMh2C0Y5tSnW5dE SQxewV6ZRmy5DZPTNbM9OHZjRD7ODgpE4QY/SfVeShtacCYYBe0ngE+eY31KqmYEfErJ v6HA== X-Gm-Message-State: AOJu0YzUssZhXvMmzFdPMYa78lfgrh/5TS0sdQoRIpqDHm1fBePwAbnD wVZzR5pzvCBRUEpY1mn7I7iFmQ== X-Received: by 2002:a05:651c:b9f:b0:2c5:623:aa01 with SMTP id bg31-20020a05651c0b9f00b002c50623aa01mr2018337ljb.49.1698420514939; Fri, 27 Oct 2023 08:28:34 -0700 (PDT) Received: from stroh80.sec.9e.network (ip-078-094-000-051.um19.pools.vodafone-ip.de. [78.94.0.51]) by smtp.gmail.com with ESMTPSA id q17-20020a05600c46d100b0040836519dd9sm1902688wmo.25.2023.10.27.08.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 08:28:34 -0700 (PDT) From: Naresh Solanki To: Liam Girdwood , Mark Brown , Jean Delvare , Guenter Roeck Cc: Naresh Solanki , linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org Subject: [PATCH v6 1/2] regulator (max5970): Add hwmon support Date: Fri, 27 Oct 2023 15:28:28 +0000 Message-ID: <20231027152830.1269895-1-naresh.solanki@9elements.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Fri, 27 Oct 2023 08:29:07 -0700 (PDT) Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage and current monitoring. This feature is seamlessly integrated through the hwmon subsystem. Signed-off-by: Naresh Solanki --- Change in V6: - Remove unrelated change - Add necessary return value check - Remove unneeded NULL check Changes in V5: - Simplify memory allocation for rdevs Changes in V4: - Use IS_REACHABLE - Use rdevs array for hwmon ops. - Remove duplicate i2c_set_clientdata Changes in V3: - Update signed-off - Add break - Update hwmon dev register name to max5970 - Remove changes in Kconfig. Changes in V2: - default case added for switch statement - Add dependency on HWMON --- drivers/regulator/max5970-regulator.c | 144 +++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 1 deletion(-) diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c index b56a174cde3d..56cc56ae63b7 100644 --- a/drivers/regulator/max5970-regulator.c +++ b/drivers/regulator/max5970-regulator.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,132 @@ enum max597x_regulator_id { MAX597X_SW1, }; +static int max5970_read_adc(struct regmap *regmap, int reg, long *val) +{ + u8 reg_data[2]; + int ret; + + ret = regmap_bulk_read(regmap, reg, ®_data[0], 2); + if (ret < 0) + return ret; + + *val = (reg_data[0] << 2) | (reg_data[1] & 3); + + return 0; +} + +static int max5970_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *val) +{ + struct regulator_dev **rdevs = dev_get_drvdata(dev); + struct max5970_regulator *ddata = rdev_get_drvdata(rdevs[channel]); + struct regmap *regmap = ddata->regmap; + int ret; + + switch (type) { + case hwmon_curr: + switch (attr) { + case hwmon_curr_input: + ret = max5970_read_adc(regmap, MAX5970_REG_CURRENT_H(channel), val); + if (ret < 0) + return ret; + /* + * Calculate current from ADC value, IRNG range & shunt resistor value. + * ddata->irng holds the voltage corresponding to the maximum value the + * 10-bit ADC can measure. + * To obtain the output, multiply the ADC value by the IRNG range (in + * millivolts) and then divide it by the maximum value of the 10-bit ADC. + */ + *val = (*val * ddata->irng) >> 10; + /* Convert the voltage meansurement across shunt resistor to current */ + *val = (*val * 1000) / ddata->shunt_micro_ohms; + return 0; + default: + return -EOPNOTSUPP; + } + + case hwmon_in: + switch (attr) { + case hwmon_in_input: + ret = max5970_read_adc(regmap, MAX5970_REG_VOLTAGE_H(channel), val); + if (ret < 0) + return ret; + /* + * Calculate voltage from ADC value and MON range. + * ddata->mon_rng holds the voltage corresponding to the maximum value the + * 10-bit ADC can measure. + * To obtain the output, multiply the ADC value by the MON range (in + * microvolts) and then divide it by the maximum value of the 10-bit ADC. + */ + *val = mul_u64_u32_shr(*val, ddata->mon_rng, 10); + /* uV to mV */ + *val = *val / 1000; + return 0; + default: + return -EOPNOTSUPP; + } + default: + return -EOPNOTSUPP; + } +} + +static umode_t max5970_is_visible(const void *data, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + struct regulator_dev **rdevs = (struct regulator_dev **)data; + struct max5970_regulator *ddata; + + if (channel >= MAX5970_NUM_SWITCHES || !rdevs[channel]) + return 0; + + ddata = rdev_get_drvdata(rdevs[channel]); + + if (channel >= ddata->num_switches) + return 0; + + switch (type) { + case hwmon_in: + switch (attr) { + case hwmon_in_input: + return 0444; + default: + break; + } + break; + case hwmon_curr: + switch (attr) { + case hwmon_curr_input: + /* Current measurement requires knowledge of the shunt resistor value. */ + if (ddata->shunt_micro_ohms) + return 0444; + break; + default: + break; + } + break; + default: + break; + } + return 0; +} + +static const struct hwmon_ops max5970_hwmon_ops = { + .is_visible = max5970_is_visible, + .read = max5970_read, +}; + +static const struct hwmon_channel_info *max5970_info[] = { + HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT), + HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT, HWMON_C_INPUT), + NULL +}; + +static const struct hwmon_chip_info max5970_chip_info = { + .ops = &max5970_hwmon_ops, + .info = max5970_info, +}; + static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity) { int ret, reg; @@ -431,7 +558,8 @@ static int max597x_regulator_probe(struct platform_device *pdev) struct i2c_client *i2c = to_i2c_client(pdev->dev.parent); struct regulator_config config = { }; struct regulator_dev *rdev; - struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES]; + struct regulator_dev **rdevs = NULL; + struct device *hwmon_dev; int num_switches; int ret, i; @@ -442,6 +570,11 @@ static int max597x_regulator_probe(struct platform_device *pdev) if (!max597x) return -ENOMEM; + rdevs = devm_kcalloc(&i2c->dev, MAX5970_NUM_SWITCHES, sizeof(struct regulator_dev *), + GFP_KERNEL); + if (!rdevs) + return -ENOMEM; + i2c_set_clientdata(i2c, max597x); if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5978")) @@ -485,6 +618,15 @@ static int max597x_regulator_probe(struct platform_device *pdev) max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms; } + if (IS_REACHABLE(CONFIG_HWMON)) { + hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970", rdevs, + &max5970_chip_info, NULL); + if (IS_ERR(hwmon_dev)) { + return dev_err_probe(&i2c->dev, PTR_ERR(hwmon_dev), + "Unable to register hwmon device\n"); + } + } + if (i2c->irq) { ret = max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches, base-commit: e95d9ba0eb4ef2f9f7fa0451c3ad4f77c685538d -- 2.41.0