From patchwork Mon Jan 23 06:40:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13111689 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 B1CC3C38142 for ; Mon, 23 Jan 2023 06:40:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230365AbjAWGkd (ORCPT ); Mon, 23 Jan 2023 01:40:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbjAWGkc (ORCPT ); Mon, 23 Jan 2023 01:40:32 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2230DBF4 for ; Sun, 22 Jan 2023 22:40:30 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id m5-20020a05600c4f4500b003db03b2559eso7737232wmq.5 for ; Sun, 22 Jan 2023 22:40:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=MjXpKA4JxoE+HGRIALy1sJ9Tu9aDnL5aCNpUki8vVSA=; b=PD4DhGvyQlMosWSqNvtIA94x91f/g4ZCjum1Bvtbpz5vdftz7uoYdm9wfnn10J0Cr3 bDdqKZiPHnUv0Wr523BMCIDaiHDGV/wAFRGOoRsYNvROfZKB0MQhVtW+o9lMfLewy3ZC kCCv/LModEn9KXp8SeBCFjL3RXGD7vrewT1EPrsFinjzWezRw+A0ZJcDycyjsY8c25Wd 67mDdjnAnCxzPPkrFrHLHt4jVwjUfHSavsATkEC8how8xrzWgeKb7zYos52z5L2CDcL/ BkKkC4aci8Z7Jm/xlmf5G920phTtUj09YLKcBOrBImZuxqfPKM6nbIjocTJTgbGsjiP+ aj2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=MjXpKA4JxoE+HGRIALy1sJ9Tu9aDnL5aCNpUki8vVSA=; b=QsLHM7HnbQDTRkW3iijoct9lp+1XUaFQImvS1p10wmVvy3FEO5XsAqwTO8nkDAR5xJ G57q9ruLpv9pmxiHbONbhbv1FXnDIzqgHsjjrTU93Uln8mGO1uLHo+XYg+NwZa3TTno3 UcOihcBSJhNJmk2/25emD8BqZSi8GJDSmzfaucMJ3FFMNSB6DfI8CbUCfz4n3rGpxDuu IobSMdFKViDMxZ35Joe4mpYFmSo4TFzYezHaK1D6dTommXn0DsMfc064z3OqmTJT5O6d gtCqGRdKVSTcY1rLANEikZ13k06dDcmhevNagcQLwMsS/LR4kDoPKca3ObTFfr4Vpa5D HvJQ== X-Gm-Message-State: AFqh2kpcKX2FCHYIKITtxm5GgkcNikV0msYgGvcvBe+LvtJPC1Tt+IZt tHNYb0c/lkm6vaaohXZaUZcmTx7ESJFluRjh2+w= X-Google-Smtp-Source: AMrXdXu+18/1OqjCXE9wZk08tLBTTt7yAAghn/6qG7PgZaJWKlSv89RGQ3AU0I12q5VuWtqu2LW2yg== X-Received: by 2002:a05:600c:2206:b0:3d2:2a72:2577 with SMTP id z6-20020a05600c220600b003d22a722577mr22708887wml.27.1674456029235; Sun, 22 Jan 2023 22:40:29 -0800 (PST) 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 o4-20020a1c7504000000b003da28dfdedcsm11439373wmc.5.2023.01.22.22.40.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Jan 2023 22:40:28 -0800 (PST) From: Naresh Solanki X-Google-Original-From: Naresh Solanki To: Guenter Roeck , Jean Delvare Cc: linux-hwmon@vger.kernel.org, Naresh Solanki , linux-kernel@vger.kernel.org Subject: [PATCH 1/4] hwmon: (pmbus/core): Generalize pmbus status flag map Date: Mon, 23 Jan 2023 07:40:18 +0100 Message-Id: <20230123064021.2657670-1-Naresh.Solanki@9elements.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The PMBus status flag map(pmbus_regulator_status_flag_map) is moved outside of the regulator #if block and the associated variable/struct name updated to reflect as generic PMBus status. This will make the PMBus status flag map more versatile and easier to incorporate into different contexts and functions. Signed-off-by: Naresh Solanki --- drivers/hwmon/pmbus/pmbus_core.c | 94 ++++++++++++++++---------------- 1 file changed, 47 insertions(+), 47 deletions(-) base-commit: 4d891f76a30d3be4194a805c5e4277786140ef05 diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 95e95783972a..1b70cf3be313 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -2692,6 +2692,49 @@ static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, return 0; } +/* A PMBus status flag and the corresponding REGULATOR_ERROR_* flag */ +struct pmbus_status_assoc { + int pflag, rflag; +}; + +/* PMBus->regulator bit mappings for a PMBus status register */ +struct pmbus_status_category { + int func; + int reg; + const struct pmbus_status_assoc *bits; /* zero-terminated */ +}; + +static const struct pmbus_status_category __maybe_unused pmbus_status_flag_map[] = { + { + .func = PMBUS_HAVE_STATUS_VOUT, + .reg = PMBUS_STATUS_VOUT, + .bits = (const struct pmbus_status_assoc[]) { + { PB_VOLTAGE_UV_WARNING, REGULATOR_ERROR_UNDER_VOLTAGE_WARN }, + { PB_VOLTAGE_UV_FAULT, REGULATOR_ERROR_UNDER_VOLTAGE }, + { PB_VOLTAGE_OV_WARNING, REGULATOR_ERROR_OVER_VOLTAGE_WARN }, + { PB_VOLTAGE_OV_FAULT, REGULATOR_ERROR_REGULATION_OUT }, + { }, + }, + }, { + .func = PMBUS_HAVE_STATUS_IOUT, + .reg = PMBUS_STATUS_IOUT, + .bits = (const struct pmbus_status_assoc[]) { + { PB_IOUT_OC_WARNING, REGULATOR_ERROR_OVER_CURRENT_WARN }, + { PB_IOUT_OC_FAULT, REGULATOR_ERROR_OVER_CURRENT }, + { PB_IOUT_OC_LV_FAULT, REGULATOR_ERROR_OVER_CURRENT }, + { }, + }, + }, { + .func = PMBUS_HAVE_STATUS_TEMP, + .reg = PMBUS_STATUS_TEMPERATURE, + .bits = (const struct pmbus_status_assoc[]) { + { PB_TEMP_OT_WARNING, REGULATOR_ERROR_OVER_TEMP_WARN }, + { PB_TEMP_OT_FAULT, REGULATOR_ERROR_OVER_TEMP }, + { }, + }, + }, +}; + #if IS_ENABLED(CONFIG_REGULATOR) static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) { @@ -2738,54 +2781,11 @@ static int pmbus_regulator_disable(struct regulator_dev *rdev) return _pmbus_regulator_on_off(rdev, 0); } -/* A PMBus status flag and the corresponding REGULATOR_ERROR_* flag */ -struct pmbus_regulator_status_assoc { - int pflag, rflag; -}; - -/* PMBus->regulator bit mappings for a PMBus status register */ -struct pmbus_regulator_status_category { - int func; - int reg; - const struct pmbus_regulator_status_assoc *bits; /* zero-terminated */ -}; - -static const struct pmbus_regulator_status_category pmbus_regulator_flag_map[] = { - { - .func = PMBUS_HAVE_STATUS_VOUT, - .reg = PMBUS_STATUS_VOUT, - .bits = (const struct pmbus_regulator_status_assoc[]) { - { PB_VOLTAGE_UV_WARNING, REGULATOR_ERROR_UNDER_VOLTAGE_WARN }, - { PB_VOLTAGE_UV_FAULT, REGULATOR_ERROR_UNDER_VOLTAGE }, - { PB_VOLTAGE_OV_WARNING, REGULATOR_ERROR_OVER_VOLTAGE_WARN }, - { PB_VOLTAGE_OV_FAULT, REGULATOR_ERROR_REGULATION_OUT }, - { }, - }, - }, { - .func = PMBUS_HAVE_STATUS_IOUT, - .reg = PMBUS_STATUS_IOUT, - .bits = (const struct pmbus_regulator_status_assoc[]) { - { PB_IOUT_OC_WARNING, REGULATOR_ERROR_OVER_CURRENT_WARN }, - { PB_IOUT_OC_FAULT, REGULATOR_ERROR_OVER_CURRENT }, - { PB_IOUT_OC_LV_FAULT, REGULATOR_ERROR_OVER_CURRENT }, - { }, - }, - }, { - .func = PMBUS_HAVE_STATUS_TEMP, - .reg = PMBUS_STATUS_TEMPERATURE, - .bits = (const struct pmbus_regulator_status_assoc[]) { - { PB_TEMP_OT_WARNING, REGULATOR_ERROR_OVER_TEMP_WARN }, - { PB_TEMP_OT_FAULT, REGULATOR_ERROR_OVER_TEMP }, - { }, - }, - }, -}; - static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) { int i, status; - const struct pmbus_regulator_status_category *cat; - const struct pmbus_regulator_status_assoc *bit; + const struct pmbus_status_category *cat; + const struct pmbus_status_assoc *bit; struct device *dev = rdev_get_dev(rdev); struct i2c_client *client = to_i2c_client(dev->parent); struct pmbus_data *data = i2c_get_clientdata(client); @@ -2796,8 +2796,8 @@ static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned mutex_lock(&data->update_lock); - for (i = 0; i < ARRAY_SIZE(pmbus_regulator_flag_map); i++) { - cat = &pmbus_regulator_flag_map[i]; + for (i = 0; i < ARRAY_SIZE(pmbus_status_flag_map); i++) { + cat = &pmbus_status_flag_map[i]; if (!(func & cat->func)) continue; From patchwork Mon Jan 23 06:40:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13111690 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 805C3C38142 for ; Mon, 23 Jan 2023 06:40:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230489AbjAWGkg (ORCPT ); Mon, 23 Jan 2023 01:40:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230368AbjAWGkf (ORCPT ); Mon, 23 Jan 2023 01:40:35 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AFD416AD3 for ; Sun, 22 Jan 2023 22:40:34 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id m15so8179948wms.4 for ; Sun, 22 Jan 2023 22:40:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=86T6EDiTMrf7zBUXyMzRIZen/96Xp0PrhB5D+bfRINk=; b=NMzS1fZ6k9qFucPKXuTEABW9tjwaDSTWmR8DT0pF6H0H1kMpb9rwWQiQz7EaqjrFGJ uBwC0HklMb95ut5E3i66PIMFmBHeviWvDjlVDZvgcULqOJo8pecx2IzbRh0M91fogAOd sJ9w/z5etp0jhMUF6F1ldTNBAVPAa1UEQcuWWPPWqZTO/Ot/i3QUstfLUJNm/XXDS06t EG9xxcZhu6vJAI8YwG9RpRBBjox4m/XeXUJDSm0oECIjIb7g9vmxd2+J2ZiLWZEza9xR yfklba1vHd+GJM4f595XmopziY3lyOBy2QHaAq9SK2WbtMyFWyhI6dz7/FBAtrebXKYr 0m3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=86T6EDiTMrf7zBUXyMzRIZen/96Xp0PrhB5D+bfRINk=; b=jBWUAVUGGzXl+qPOj8lRzPuJQH8BKRg6Je7N7DBu7hUBqw2Wv446o5e6GIZPboHs68 K4HfUPdVTmcjQJt9T88ZVYiXOPTOsqpTig8+1thl2tvrN2nUVFs8UhxzRSK2zP+BwY/+ 7gMK1tHO3UMzzGPvDWt/lOqTn2oCpKneZeoS02TDuFdRQV9Y6b7NXOUhiEONKO0wlTwN uFE0dELUaY8I3fVcX4xDb3R2yuDHXMY89cROvQUOcTAZO6cVjZZM9k7Ket2q9pU6GKpr JWSRaSBsBLOEpo7vH4aKX0IzQUHEKC+2mG6DnDcz3/f/8xp6KEWA7dAU+FQRTumRjojZ EiKw== X-Gm-Message-State: AFqh2krg9bZtqx1r1O/GdZnR+9sCWFjAgeePE/5BVqnz43Czho1Brhoq qQfPJnlZwNlDD+hOiIKbl3Y8sW2VpzMDXyDsV/U= X-Google-Smtp-Source: AMrXdXtbRgY4Pq0dCdkFyzPdwV+gBaPXn5spLKjeaGOTSWUS5qFfwCWYhLr0iSH4scTyS8zPvbj3Eg== X-Received: by 2002:a05:600c:3583:b0:3d9:719a:8f7d with SMTP id p3-20020a05600c358300b003d9719a8f7dmr22155580wmq.35.1674456032636; Sun, 22 Jan 2023 22:40:32 -0800 (PST) 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 o4-20020a1c7504000000b003da28dfdedcsm11439373wmc.5.2023.01.22.22.40.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Jan 2023 22:40:32 -0800 (PST) From: Naresh Solanki X-Google-Original-From: Naresh Solanki To: Guenter Roeck , Jean Delvare Cc: linux-hwmon@vger.kernel.org, Naresh Solanki , linux-kernel@vger.kernel.org Subject: [PATCH 2/4] hwmon: (pmbus/core) Generalise pmbus get status Date: Mon, 23 Jan 2023 07:40:19 +0100 Message-Id: <20230123064021.2657670-2-Naresh.Solanki@9elements.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230123064021.2657670-1-Naresh.Solanki@9elements.com> References: <20230123064021.2657670-1-Naresh.Solanki@9elements.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Add function pmbus get status that can be used to get both pmbus specific status & regulator status Signed-off-by: Naresh Solanki --- drivers/hwmon/pmbus/pmbus_core.c | 148 +++++++++++++++++-------------- 1 file changed, 82 insertions(+), 66 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 1b70cf3be313..12b662b91306 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -2735,64 +2735,16 @@ static const struct pmbus_status_category __maybe_unused pmbus_status_flag_map[] }, }; -#if IS_ENABLED(CONFIG_REGULATOR) -static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) -{ - struct device *dev = rdev_get_dev(rdev); - struct i2c_client *client = to_i2c_client(dev->parent); - struct pmbus_data *data = i2c_get_clientdata(client); - u8 page = rdev_get_id(rdev); - int ret; - mutex_lock(&data->update_lock); - ret = _pmbus_read_byte_data(client, page, PMBUS_OPERATION); - mutex_unlock(&data->update_lock); - - if (ret < 0) - return ret; - - return !!(ret & PB_OPERATION_CONTROL_ON); -} - -static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) +static int pmbus_get_flags(struct pmbus_data *data, u8 page, unsigned int *error) { - struct device *dev = rdev_get_dev(rdev); - struct i2c_client *client = to_i2c_client(dev->parent); - struct pmbus_data *data = i2c_get_clientdata(client); - u8 page = rdev_get_id(rdev); - int ret; - - mutex_lock(&data->update_lock); - ret = pmbus_update_byte_data(client, page, PMBUS_OPERATION, - PB_OPERATION_CONTROL_ON, - enable ? PB_OPERATION_CONTROL_ON : 0); - mutex_unlock(&data->update_lock); - - return ret; -} - -static int pmbus_regulator_enable(struct regulator_dev *rdev) -{ - return _pmbus_regulator_on_off(rdev, 1); -} - -static int pmbus_regulator_disable(struct regulator_dev *rdev) -{ - return _pmbus_regulator_on_off(rdev, 0); -} - -static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) -{ - int i, status; const struct pmbus_status_category *cat; const struct pmbus_status_assoc *bit; - struct device *dev = rdev_get_dev(rdev); - struct i2c_client *client = to_i2c_client(dev->parent); - struct pmbus_data *data = i2c_get_clientdata(client); - u8 page = rdev_get_id(rdev); + struct i2c_client *client = to_i2c_client(data->dev); int func = data->info->func[page]; + int i, status, ret; - *flags = 0; + *error = 0; mutex_lock(&data->update_lock); @@ -2803,14 +2755,15 @@ static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned status = _pmbus_read_byte_data(client, page, cat->reg); if (status < 0) { - mutex_unlock(&data->update_lock); - return status; + ret = status; + goto unlock; } for (bit = cat->bits; bit->pflag; bit++) { if (status & bit->pflag) - *flags |= bit->rflag; + *error |= bit->rflag; } + } /* @@ -2823,36 +2776,99 @@ static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned * REGULATOR_ERROR__WARN. */ status = pmbus_get_status(client, page, PMBUS_STATUS_WORD); - mutex_unlock(&data->update_lock); - if (status < 0) - return status; - if (pmbus_regulator_is_enabled(rdev)) { + if (status < 0) { + ret = status; + goto unlock; + } + + ret = _pmbus_read_byte_data(client, page, PMBUS_OPERATION); + if (ret < 0) + goto unlock; + + if (ret & PB_OPERATION_CONTROL_ON) { if (status & PB_STATUS_OFF) - *flags |= REGULATOR_ERROR_FAIL; + *error |= REGULATOR_ERROR_FAIL; if (status & PB_STATUS_POWER_GOOD_N) - *flags |= REGULATOR_ERROR_REGULATION_OUT; + *error |= REGULATOR_ERROR_REGULATION_OUT; } /* * Unlike most other status bits, PB_STATUS_{IOUT_OC,VOUT_OV} are * defined strictly as fault indicators (not warnings). */ if (status & PB_STATUS_IOUT_OC) - *flags |= REGULATOR_ERROR_OVER_CURRENT; + *error |= REGULATOR_ERROR_OVER_CURRENT; if (status & PB_STATUS_VOUT_OV) - *flags |= REGULATOR_ERROR_REGULATION_OUT; + *error |= REGULATOR_ERROR_REGULATION_OUT; /* * If we haven't discovered any thermal faults or warnings via * PMBUS_STATUS_TEMPERATURE, map PB_STATUS_TEMPERATURE to a warning as * a (conservative) best-effort interpretation. */ - if (!(*flags & (REGULATOR_ERROR_OVER_TEMP | REGULATOR_ERROR_OVER_TEMP_WARN)) && + if (!(*error & (REGULATOR_ERROR_OVER_TEMP | REGULATOR_ERROR_OVER_TEMP_WARN)) && (status & PB_STATUS_TEMPERATURE)) - *flags |= REGULATOR_ERROR_OVER_TEMP_WARN; + *error |= REGULATOR_ERROR_OVER_TEMP_WARN; - return 0; +unlock: + mutex_unlock(&data->update_lock); + return ret; +} + +#if IS_ENABLED(CONFIG_REGULATOR) +static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) +{ + struct device *dev = rdev_get_dev(rdev); + struct i2c_client *client = to_i2c_client(dev->parent); + struct pmbus_data *data = i2c_get_clientdata(client); + u8 page = rdev_get_id(rdev); + int ret; + + mutex_lock(&data->update_lock); + ret = _pmbus_read_byte_data(client, page, PMBUS_OPERATION); + mutex_unlock(&data->update_lock); + + if (ret < 0) + return ret; + + return !!(ret & PB_OPERATION_CONTROL_ON); +} + +static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) +{ + struct device *dev = rdev_get_dev(rdev); + struct i2c_client *client = to_i2c_client(dev->parent); + struct pmbus_data *data = i2c_get_clientdata(client); + u8 page = rdev_get_id(rdev); + int ret; + + mutex_lock(&data->update_lock); + ret = pmbus_update_byte_data(client, page, PMBUS_OPERATION, + PB_OPERATION_CONTROL_ON, + enable ? PB_OPERATION_CONTROL_ON : 0); + mutex_unlock(&data->update_lock); + + return ret; +} + +static int pmbus_regulator_enable(struct regulator_dev *rdev) +{ + return _pmbus_regulator_on_off(rdev, 1); +} + +static int pmbus_regulator_disable(struct regulator_dev *rdev) +{ + return _pmbus_regulator_on_off(rdev, 0); +} + +static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) +{ + struct device *dev = rdev_get_dev(rdev); + struct i2c_client *client = to_i2c_client(dev->parent); + struct pmbus_data *data = i2c_get_clientdata(client); + + return pmbus_get_flags(data, rdev_get_id(rdev), flags); } static int pmbus_regulator_get_status(struct regulator_dev *rdev) From patchwork Mon Jan 23 06:40:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13111691 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 32558C38142 for ; Mon, 23 Jan 2023 06:40:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230520AbjAWGkn (ORCPT ); Mon, 23 Jan 2023 01:40:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231130AbjAWGkk (ORCPT ); Mon, 23 Jan 2023 01:40:40 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 492701969B for ; Sun, 22 Jan 2023 22:40:39 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id l41-20020a05600c1d2900b003daf986faaeso7751561wms.3 for ; Sun, 22 Jan 2023 22:40:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ov8fh9l0ZAHhGTGOwGz76sDwDHqWLrH8WR2EMR4pMDQ=; b=DfdZ2UPqZULiwcA4DFLfmlYFHU6xX3ia8PNzX5s0J2gqiyduukYElFPytQ7n2GBl36 0TEuhHWpNTjCLK2Dl3MCoUzA/IjnrFdASXJCpSf9zqUQlC5JocsnrkZcHVtNQ3ZWfd+/ bLhpcSkopc/FMlfiQy6cStMx6f3cIWUrr1hw8HWAQ1NoXQiJFByvNTMzF8LS+7BcQd1E san0VNw19vgR6MIuo26h9OLg+MmGzWHnJ0oyYmsrHlU7dwf06TgPJaMv7k5x41eYNlEC d2Kh2GI5wXu3w1UM1589wd7TP2fTD9zAYNtlsKPIoo+Yuk4NlJYVtFuOZ/I28YliZI33 9CrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ov8fh9l0ZAHhGTGOwGz76sDwDHqWLrH8WR2EMR4pMDQ=; b=DL0+e8HJNtcAeiM2AZ0WaoGLMOSYFq85L9GYvXOuM2S/nr07D6GoxqoH+CmJPXWvEw ZJuVX0a5AqQFAdYOMHVicxRFqux/Odj3DT4piyWH+ZhBYr2CIcse9Hs16XrzepyUO2ox ad5xWJTpnP6vwc3nz7CNBuWO5iodR7SqGsim/xx2vN1DrY/jD7yWAGeRaAZ0HeAIAw58 axM1AJkCG4/YisOULJfkoVyTQf37Fs6HlheJK2BkSXO2UnX54n5FfhAzlGuGTUOAg/Dg CXoeqC5iRlg8dtThHV0C+SvQqOGKj8qL4R+jtxrp9Qq3FgsJoTjKu62bNqChlVs+W6of WOIg== X-Gm-Message-State: AFqh2krSTpcCFsSXESnLHyNUW7MmnstoZV4FOlqLGbF7SJtiO+Lm+k+I 7FEHuBgMFh3y0ix1WP2zmIzowA== X-Google-Smtp-Source: AMrXdXtgsRE0R1LbuUE4LfJWulGjK9X5QZrCEgK+IAbPJuMo5AIjBNizMCblX7+JlNSe3JuzVHGZ1g== X-Received: by 2002:a05:600c:3d14:b0:3da:f793:fff6 with SMTP id bh20-20020a05600c3d1400b003daf793fff6mr22662762wmb.16.1674456037843; Sun, 22 Jan 2023 22:40:37 -0800 (PST) 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 o4-20020a1c7504000000b003da28dfdedcsm11439373wmc.5.2023.01.22.22.40.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Jan 2023 22:40:37 -0800 (PST) From: Naresh Solanki X-Google-Original-From: Naresh Solanki To: Guenter Roeck , Jean Delvare Cc: linux-hwmon@vger.kernel.org, Patrick Rudolph , Naresh Solanki , linux-kernel@vger.kernel.org Subject: [PATCH 3/4] hwmon: (pmbus/core): Add interrupt support Date: Mon, 23 Jan 2023 07:40:20 +0100 Message-Id: <20230123064021.2657670-3-Naresh.Solanki@9elements.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230123064021.2657670-1-Naresh.Solanki@9elements.com> References: <20230123064021.2657670-1-Naresh.Solanki@9elements.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org From: Patrick Rudolph Implement PMBUS irq handler. Signed-off-by: Patrick Rudolph Signed-off-by: Naresh Solanki --- drivers/hwmon/pmbus/pmbus.h | 2 +- drivers/hwmon/pmbus/pmbus_core.c | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/drivers/hwmon/pmbus/pmbus.h b/drivers/hwmon/pmbus/pmbus.h index 713ea7915425..11e84e141126 100644 --- a/drivers/hwmon/pmbus/pmbus.h +++ b/drivers/hwmon/pmbus/pmbus.h @@ -26,7 +26,7 @@ enum pmbus_regs { PMBUS_CAPABILITY = 0x19, PMBUS_QUERY = 0x1A, - + PMBUS_SMBALERT_MASK = 0x1B, PMBUS_VOUT_MODE = 0x20, PMBUS_VOUT_COMMAND = 0x21, PMBUS_VOUT_TRIM = 0x22, diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 12b662b91306..3c1c68e93943 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -3095,6 +3095,85 @@ static int pmbus_regulator_register(struct pmbus_data *data) } #endif +static int pmbus_write_smbalert_mask(struct i2c_client *client, u8 page, u8 reg, u8 val) +{ + int err; + + err = pmbus_check_word_register(client, page, reg | (val << 8)); + if (err) + return err; + + return pmbus_write_word_data(client, page, PMBUS_SMBALERT_MASK, reg | (val << 8)); +} + +static irqreturn_t pmbus_fault_handler(int irq, void *pdata) +{ + struct pmbus_data *data = pdata; + struct i2c_client *client = to_i2c_client(data->dev); + int i, status; + + mutex_lock(&data->update_lock); + for (i = 0; i < data->info->pages; i++) { + status = pmbus_read_status_word(client, i); + if (status < 0) { + mutex_unlock(&data->update_lock); + return status; + } + + if (status & ~(PB_STATUS_OFF | PB_STATUS_BUSY | PB_STATUS_POWER_GOOD_N)) + pmbus_clear_fault_page(client, i); + } + mutex_unlock(&data->update_lock); + + return IRQ_HANDLED; +} + +static int pmbus_irq_setup(struct i2c_client *client, struct pmbus_data *data) +{ + struct device *dev = &client->dev; + const struct pmbus_status_category *cat; + const struct pmbus_status_assoc *bit; + int i, j, err, ret, func; + u8 mask; + u8 misc_status[] = {PMBUS_STATUS_CML, PMBUS_STATUS_OTHER, PMBUS_STATUS_MFR_SPECIFIC, + PMBUS_STATUS_FAN_12, PMBUS_STATUS_FAN_34}; + + for (i = 0; i < data->info->pages; i++) { + func = data->info->func[i]; + + for (j = 0; j < ARRAY_SIZE(pmbus_status_flag_map); j++) { + cat = &pmbus_status_flag_map[j]; + if (!(func & cat->func)) + continue; + mask = 0; + for (bit = cat->bits; bit->pflag; bit++) + mask |= bit->pflag; + + err = pmbus_write_smbalert_mask(client, i, cat->reg, ~mask); + if (err) + dev_err_once(dev, "Failed to set smbalert for reg 0x%02x\n", + cat->reg); + } + + for (j = 0; j < ARRAY_SIZE(misc_status); j++) { + err = pmbus_write_smbalert_mask(client, i, misc_status[j], 0xff); + if (err) + dev_err_once(dev, "Failed to set smbalert for reg 0x%02x\n", + misc_status[j]); + } + } + + /* Register notifiers - can fail if IRQ is not given */ + ret = devm_request_threaded_irq(dev, client->irq, NULL, pmbus_fault_handler, 0, + "pmbus-irq", data); + if (ret) { + dev_warn(dev, "IRQ disabled %d\n", ret); + return ret; + } + + return 0; +} + static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */ #if IS_ENABLED(CONFIG_DEBUG_FS) @@ -3457,6 +3536,12 @@ int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info) if (ret) return ret; + if (client->irq > 0) { + ret = pmbus_irq_setup(client, data); + if (ret) + return ret; + } + ret = pmbus_init_debugfs(client, data); if (ret) dev_warn(dev, "Failed to register debugfs\n"); From patchwork Mon Jan 23 06:40:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 13111692 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 75D51C05027 for ; Mon, 23 Jan 2023 06:40:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231147AbjAWGkp (ORCPT ); Mon, 23 Jan 2023 01:40:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231150AbjAWGko (ORCPT ); Mon, 23 Jan 2023 01:40:44 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D27441ABF4 for ; Sun, 22 Jan 2023 22:40:41 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id j17so8214077wms.0 for ; Sun, 22 Jan 2023 22:40:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c+JF7XPB91qXux0PSB/u2LOK9F1btKF5KMGFh4JlLz4=; b=dYc41iPRe9mkv72Tn+NSiJD45gWuyAZwg8HfTDI4PDsqvCUMNTbGShut3I9O74si+A AFxDygpnmG4p9ExU+AJoM9T9SFnQlKsKFnol/JCZg0p1aVQvE5K3+YQfBflr7OpCzjaB FC0+sQ1ZQ/dfzk7OPCc9e5jdSg7Rn+4D7c0qh7D+d4SMg1ep+WC9IYYeb7LfpVlDHVpW ru4Rx9em/OXqAAaIQviIUg9xbX8pbIiUXczuHZeSlDQV5Li1hTQHQWsKcc8iyjTRTsLs 1joZnpFitWK0kNfI1GyohlGsFJdXO4J2UbUzQLG5NfJcSl22rwOlbIIQjpX1TCJ7v3Pk RMCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c+JF7XPB91qXux0PSB/u2LOK9F1btKF5KMGFh4JlLz4=; b=XkjvQT/VOUb/bx/dDOINonvc9fwpHXgJaut1M0vEtRK9VltuZXW8+WN4682uSJX/4i 5IJDWgUZN8lVGI45mJ1O/GSfBmhmFW3Epzq+Iak/dFWGVZDpGJ2akh+icbyeEYN0ERQ9 Ge3p6CKJSc2hHWtSHRpO/5YZfGA0ioLpQgZ7EN+3c+Q6dJ2nw6g8say6kWja1UgEyKPm l8QaF9MqnJt0rKQq9RK1Hv6L1tGMeUxnSgCBYexiLUk8zh9QTd9d81u8ESosiPEtPAhB yIsDM6+fefeGGUKbNhHB+5P8kcWLJtkOqaFMRROar7v7frVcX23SyYvalZukYUt1e+AW l5Xg== X-Gm-Message-State: AFqh2kqSkgmhts6X6hqCI5lazyDBxdP19Iy51p7oHvdxoE5GIQ8RcYBM /HaVOzyYMcJW6RB/c7ievrSu1g== X-Google-Smtp-Source: AMrXdXu0fUtce20OFIj3HMtvfjV5CYnrL2JWHE0FAgqFynclPuOpHmHfA0Hk/kTeUJ5zHDfcC/wNAg== X-Received: by 2002:a05:600c:1d0b:b0:3db:88f:996d with SMTP id l11-20020a05600c1d0b00b003db088f996dmr21949295wms.36.1674456039531; Sun, 22 Jan 2023 22:40:39 -0800 (PST) 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 o4-20020a1c7504000000b003da28dfdedcsm11439373wmc.5.2023.01.22.22.40.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Jan 2023 22:40:39 -0800 (PST) From: Naresh Solanki X-Google-Original-From: Naresh Solanki To: Guenter Roeck , Jean Delvare Cc: linux-hwmon@vger.kernel.org, Naresh Solanki , linux-kernel@vger.kernel.org Subject: [PATCH 4/4] hwmon: (pmbus/core): Notify hwmon events Date: Mon, 23 Jan 2023 07:40:21 +0100 Message-Id: <20230123064021.2657670-4-Naresh.Solanki@9elements.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230123064021.2657670-1-Naresh.Solanki@9elements.com> References: <20230123064021.2657670-1-Naresh.Solanki@9elements.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Notify hwmon events using the pmbus irq handler. Signed-off-by: Naresh Solanki --- drivers/hwmon/pmbus/pmbus_core.c | 45 ++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 3c1c68e93943..cbea92d3a1e6 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -2735,8 +2735,35 @@ static const struct pmbus_status_category __maybe_unused pmbus_status_flag_map[] }, }; +#define to_dev_attr(_dev_attr) \ + container_of(_dev_attr, struct device_attribute, attr) -static int pmbus_get_flags(struct pmbus_data *data, u8 page, unsigned int *error) +static void pmbus_notify(struct pmbus_data *data, int page, int reg, int flags) +{ + int i; + + for (i = 0; i < data->num_attributes; i++) { + struct device_attribute *da = to_dev_attr(data->group.attrs[i]); + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int index = attr->index; + u16 smask = pb_index_to_mask(index); + u8 spage = pb_index_to_page(index); + u16 sreg = pb_index_to_reg(index); + + if (reg == sreg && page == spage && (smask & flags)) { + dev_dbg(data->dev, "sysfs notify: %s", da->attr.name); + sysfs_notify(&data->dev->kobj, NULL, da->attr.name); + kobject_uevent(&data->dev->kobj, KOBJ_CHANGE); + flags &= ~smask; + } + + if (!flags) + break; + } +} + +static int pmbus_get_flags(struct pmbus_data *data, u8 page, unsigned int *error, + bool notify) { const struct pmbus_status_category *cat; const struct pmbus_status_assoc *bit; @@ -2764,6 +2791,8 @@ static int pmbus_get_flags(struct pmbus_data *data, u8 page, unsigned int *error *error |= bit->rflag; } + if (notify && status) + pmbus_notify(data, page, cat->reg, status); } /* @@ -2868,7 +2897,7 @@ static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned struct i2c_client *client = to_i2c_client(dev->parent); struct pmbus_data *data = i2c_get_clientdata(client); - return pmbus_get_flags(data, rdev_get_id(rdev), flags); + return pmbus_get_flags(data, rdev_get_id(rdev), flags, false); } static int pmbus_regulator_get_status(struct regulator_dev *rdev) @@ -3110,10 +3139,14 @@ static irqreturn_t pmbus_fault_handler(int irq, void *pdata) { struct pmbus_data *data = pdata; struct i2c_client *client = to_i2c_client(data->dev); - int i, status; + int i, status, ret; - mutex_lock(&data->update_lock); for (i = 0; i < data->info->pages; i++) { + ret = pmbus_get_flags(data, i, &status, true); + if (ret) + return ret; + + mutex_lock(&data->update_lock); status = pmbus_read_status_word(client, i); if (status < 0) { mutex_unlock(&data->update_lock); @@ -3122,8 +3155,10 @@ static irqreturn_t pmbus_fault_handler(int irq, void *pdata) if (status & ~(PB_STATUS_OFF | PB_STATUS_BUSY | PB_STATUS_POWER_GOOD_N)) pmbus_clear_fault_page(client, i); + + mutex_unlock(&data->update_lock); } - mutex_unlock(&data->update_lock); + return IRQ_HANDLED; }