From patchwork Wed Oct 24 19:34:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10654901 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1CC6A14E2 for ; Wed, 24 Oct 2018 19:34:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0B52F2839C for ; Wed, 24 Oct 2018 19:34:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F3C0E2B0B9; Wed, 24 Oct 2018 19:34:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 823E72839C for ; Wed, 24 Oct 2018 19:34:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727256AbeJYEDk (ORCPT ); Thu, 25 Oct 2018 00:03:40 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:45487 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727173AbeJYEDj (ORCPT ); Thu, 25 Oct 2018 00:03:39 -0400 Received: by mail-pg1-f195.google.com with SMTP id s3-v6so2789324pga.12; Wed, 24 Oct 2018 12:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=HYBMANsFtxTvEdz9erpZJE2vyqhPlAPNtRDSmO7XKEw=; b=nMdmMVYqB1CXISq9sWKNM8DbvIcy2wCj+DBB0YEpSH/372Yl9TZQ6/8ZVUG5P9Rz9u pWwK01nVwQubX7ELXWyjViXSi278QbXUzVrxgkd9i30KlwL/vfJLPX1aZGTpyUqXp9WU SmtKZGnlApRD4JjmIK34Yca5ffpCn0HhXyyTi8BPzm807r+/9XCWoSMxCE97m83FEGKz 4EVtZmhHZ11SGz5Zdio4uq5zzjjUqNVnTlt/C8sd1xu+bBBSm+5uWqj2dFMwQKmbCbQK iCXvXHP8YjRdm7+IE5Gu6vD/CTTBt7KXwIPEboIg2ABWsNKtiZCWqG/U51tSDLsjft4Q ob3g== 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; bh=HYBMANsFtxTvEdz9erpZJE2vyqhPlAPNtRDSmO7XKEw=; b=UO5EF5xS5STE0LnCwtPOc9uHVPBm/dudOYn5SvCCL0jrbtWyW7FtWtY/kiexXnLHlC 8tH0m2RX56fa0k0GIwXqz+5m9Exk5gOCDyD8dlgn+cvjxwNgrN2XLBLJta0xQSgrQEAo RZHx1q17VppVwnUrkhb8KKBXtM3e7JBqp+ONTQe6/6YpjeVnptcTbhXEBVXWsJT1n3zO Xl6s2tVyGXTn9Kpdyr91ibrmE30JFhmAGyBbCvR7csS+HZuaqr1pfWitDE0UWJGiubT8 H3WZhooJm61LgMniWyqXk6Y9JRGHW/nTCxEjJrs7OKhfqnXNXfNzJ/JJQMhKSWVfyOwJ YaMA== X-Gm-Message-State: AGRZ1gKfruDfnJXXFA6z2WdwgZDFq5BqXrBjLtIzEwCD8wD2oGDx3wq0 NWrMhF2EdlMP5MlbTbR6ifE= X-Google-Smtp-Source: AJdET5eDR9ND4Neq7WlmKCTMGAtsS+1bgr4XOsnP00bvlec+h678RPYcgFJrw8biKoascrI3vxEkhw== X-Received: by 2002:a62:9f11:: with SMTP id g17-v6mr3867594pfe.144.1540409658027; Wed, 24 Oct 2018 12:34:18 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id r81-v6sm19921633pfa.110.2018.10.24.12.34.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Oct 2018 12:34:17 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/5] hwmon: (ina3221) Serialize sysfs ABI accesses Date: Wed, 24 Oct 2018 12:34:00 -0700 Message-Id: <20181024193402.16698-4-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181024193402.16698-1-nicoleotsuka@gmail.com> References: <20181024193402.16698-1-nicoleotsuka@gmail.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This change adds a mutex to serialize accesses of sysfs attributes. This is required when polling CVRF bit of the MASK/ENABLE register because this bit is cleared on a read of this MASK/ENABLE register or a write to CONFIG register, which means that this bit might be accidentally cleared by reading other fields like alert flags. So this patch adds a mutex lock to protect the write() and read() callbacks. The read_string() callback won't need the lock since it just returns the label without touching any hardware register. Signed-off-by: Nicolin Chen --- Changlog v1->v3: * N/A drivers/hwmon/ina3221.c | 51 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 26cdf3342d80..10e8347a3c80 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -94,12 +95,14 @@ struct ina3221_input { * @regmap: Register map of the device * @fields: Register fields of the device * @inputs: Array of channel input source specific structures + * @lock: mutex lock to serialize sysfs attribute accesses * @reg_config: Register value of INA3221_CONFIG */ struct ina3221_data { struct regmap *regmap; struct regmap_field *fields[F_MAX_FIELDS]; struct ina3221_input inputs[INA3221_NUM_CHANNELS]; + struct mutex lock; u32 reg_config; }; @@ -265,29 +268,53 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable) static int ina3221_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { + struct ina3221_data *ina = dev_get_drvdata(dev); + int ret; + + mutex_lock(&ina->lock); + switch (type) { case hwmon_in: /* 0-align channel ID */ - return ina3221_read_in(dev, attr, channel - 1, val); + ret = ina3221_read_in(dev, attr, channel - 1, val); + break; case hwmon_curr: - return ina3221_read_curr(dev, attr, channel, val); + ret = ina3221_read_curr(dev, attr, channel, val); + break; default: - return -EOPNOTSUPP; + ret = -EOPNOTSUPP; + break; } + + mutex_unlock(&ina->lock); + + return ret; } static int ina3221_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val) { + struct ina3221_data *ina = dev_get_drvdata(dev); + int ret; + + mutex_lock(&ina->lock); + switch (type) { case hwmon_in: /* 0-align channel ID */ - return ina3221_write_enable(dev, channel - 1, val); + ret = ina3221_write_enable(dev, channel - 1, val); + break; case hwmon_curr: - return ina3221_write_curr(dev, attr, channel, val); + ret = ina3221_write_curr(dev, attr, channel, val); + break; default: - return -EOPNOTSUPP; + ret = -EOPNOTSUPP; + break; } + + mutex_unlock(&ina->lock); + + return ret; } static int ina3221_read_string(struct device *dev, enum hwmon_sensor_types type, @@ -582,6 +609,7 @@ static int ina3221_probe(struct i2c_client *client, if (ret) return ret; + mutex_init(&ina->lock); dev_set_drvdata(dev, ina); hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina, @@ -589,12 +617,22 @@ static int ina3221_probe(struct i2c_client *client, ina3221_groups); if (IS_ERR(hwmon_dev)) { dev_err(dev, "Unable to register hwmon device\n"); + mutex_destroy(&ina->lock); return PTR_ERR(hwmon_dev); } return 0; } +static int ina3221_remove(struct i2c_client *client) +{ + struct ina3221_data *ina = dev_get_drvdata(&client->dev); + + mutex_destroy(&ina->lock); + + return 0; +} + static int __maybe_unused ina3221_suspend(struct device *dev) { struct ina3221_data *ina = dev_get_drvdata(dev); @@ -663,6 +701,7 @@ MODULE_DEVICE_TABLE(i2c, ina3221_ids); static struct i2c_driver ina3221_i2c_driver = { .probe = ina3221_probe, + .remove = ina3221_remove, .driver = { .name = INA3221_DRIVER_NAME, .of_match_table = ina3221_of_match_table,