From patchwork Fri Sep 21 00:07:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10608877 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 B425F1390 for ; Fri, 21 Sep 2018 00:08:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9575D2E004 for ; Fri, 21 Sep 2018 00:08:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 884A82E00F; Fri, 21 Sep 2018 00:08:28 +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 3AE4F2E004 for ; Fri, 21 Sep 2018 00:08:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725855AbeIUFyU (ORCPT ); Fri, 21 Sep 2018 01:54:20 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:42580 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725861AbeIUFyT (ORCPT ); Fri, 21 Sep 2018 01:54:19 -0400 Received: by mail-pl1-f194.google.com with SMTP id g23-v6so5076357plq.9; Thu, 20 Sep 2018 17:08:15 -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=vNQHJ1PVklIpmA6lmuHU4tL2czx3/Ieaf/l/Pf47rLc=; b=sCu6u161XK8hQ2Is0EDdrVLyhc0ADJEBnpy7CmJesMIgkfTQ4eKvkJ6BLUsjrkeQ6Z w9s9VGS/EdLknda3FKLz2wXctyqhSwHriE6d3XLkx9LRXMsk2vmJ1wPuQOTGcEvboMTr hcmH5hNLp5bqLkAkHu5rR8I3PgSJ4l45ZTTYqVnW0GclnOtAR9/6uOGxs8uACalSopne ekT6YU35Bf6rKWY1h/N+7jS4L0cvxbhnpPCyI1/PwIlFrtOCyaDHexgc3dVQ4qprLPpd 360kpQBL/Uw1bJ0u/nM1ZbV2vdtnchYqzeY7Jb8KmQq672mIfHuTd/irJuloSLDEC+py BnYw== 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=vNQHJ1PVklIpmA6lmuHU4tL2czx3/Ieaf/l/Pf47rLc=; b=f6uxqOU1fJTpDaN0MhtUdMu6OeXi9YYuCe+VZK25NcvHnNxPEVdaohoG3ueKN1YnYb qZGNrlfSUIGOpn5CGcfPxGFxrA4wsZqdz3Rqt6IfWOuxm5Pc+Oop8GU0Il7NAOZeo6Mv p/Sr1LZwMRnDQbPHDLlfZ1T3m29yScjqqAXcz94S6BO9wb8kOzP0bL9HG7pBhQBAgYyk 83bTnXdI8EKb9j9FV76LXUCAqqIwNOAFLV/VaxY9b/jTo+AQuZLnBjc0Hjx5NouAqy+f an5PsLgVDWyeXttaT5bgz12ToCx3zeiZjvtnqdlCcW1ttyDEo+i/P4TN+wWdDBvxNv03 RoGw== X-Gm-Message-State: APzg51AopCYZ5jaz3su66Ol7qN/9H6D0m3wWNVvnTyF85sGwGJvcnDLW v6nIzQI+cfcG7JnhauQ0Ii4= X-Google-Smtp-Source: ANB0VdaFZ1nqcwrTBoZMkOOJfa2sZhJxD3Q9gg6B0xZmj3N5uEd4Q0N6LaO7wxvAPYXqOrPArcaE4A== X-Received: by 2002:a17:902:7c8c:: with SMTP id y12-v6mr42009346pll.283.1537488494807; Thu, 20 Sep 2018 17:08:14 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id q26-v6sm43355083pfj.127.2018.09.20.17.08.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 17:08:14 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: afd@ti.com, linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] dt-bindings: hwmon: Add ina3221 documentation Date: Thu, 20 Sep 2018 17:07:52 -0700 Message-Id: <20180921000753.21846-2-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921000753.21846-1-nicoleotsuka@gmail.com> References: <20180921000753.21846-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 Texas Instruments INA3221 is a triple-channel shunt and bus voltage monitor. This patch adds a DT binding doc for it. Signed-off-by: Nicolin Chen --- .../devicetree/bindings/hwmon/ina3221.txt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/ina3221.txt diff --git a/Documentation/devicetree/bindings/hwmon/ina3221.txt b/Documentation/devicetree/bindings/hwmon/ina3221.txt new file mode 100644 index 000000000000..266c9586c9b1 --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/ina3221.txt @@ -0,0 +1,23 @@ +ina3221 properties + +Required properties: +- compatible: Must be "ti,ina3221" +- reg: I2C address + +Optional properties: + +- ti,channel1-name: +- ti,channel2-name: +- ti,channel3-name: + The names of the input sources (described in the schematics) + Set the names with "NC" to indicate not-connected channels + +Example: + +ina3221@40 { + compatible = "ti,ina3221"; + reg = <0x40>; + ti,channel1-name = "NC"; + ti,channel2-name = "VDD_5V0_EXT"; + ti,channel3-name = "VDD_19V"; +}; From patchwork Fri Sep 21 00:07:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10608875 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 682F814DA for ; Fri, 21 Sep 2018 00:08:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4A4E92E004 for ; Fri, 21 Sep 2018 00:08:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3DE382E00F; Fri, 21 Sep 2018 00:08:25 +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 A04FD2E004 for ; Fri, 21 Sep 2018 00:08:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725780AbeIUFyV (ORCPT ); Fri, 21 Sep 2018 01:54:21 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:35006 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388795AbeIUFyV (ORCPT ); Fri, 21 Sep 2018 01:54:21 -0400 Received: by mail-pf1-f193.google.com with SMTP id p12-v6so5121388pfh.2; Thu, 20 Sep 2018 17:08:16 -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=lQpG/Nl0x9DvMs9MlFGsNHInv2vOjzi+DpmvyozQgTM=; b=sHU0I/yrGQlFPtmJTv2l2kfBArXCLYKAs+aBItHMVf7Evrpeig1fpwSRv1Zp6dY2yQ 9I/hTuGAhEZXFBC3/ZScx0h2s5geXKb+pAeaXI5S4MBifZT47QpSRX25DBlPREX0KeXG Ol/qXDNhCqXuhGZHpAt3gQipvtiYvrDbu2FN9AEia0HQlPj9kJcCAmqLu30JUplty3zL sbRzisgEszGNtkQS6u5s8kls3N8Uze+NJxIZcKKfd0mkyjfmvKBsYOJkj2tfH8TJ/mIC 8i4tYYHMwfA1v+TM48xIrQrzz0yJN4+TTvUtQcMryWKJ3eXY2vvjsSVygWwRLxaq+ou0 VQCA== 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=lQpG/Nl0x9DvMs9MlFGsNHInv2vOjzi+DpmvyozQgTM=; b=Lr973p/34KlXDbuNR01gmHlkpJpv+z90stzDJ8oi4ECStyJ9fCvlZNqhgR6ihFhW9X 8qOjciBFUn2LHZYi2Dd9EtK24qy0YjjFgLhunK9WjCtegtzp+tlWHgDZ5UqW5nLGTKxV 6FwWUEPZYlfE+fskLRKfJdnJxgFIPmX7S+fAmAGJxXS3VPPOie2Q58u8oHP0jwjMIrPI hhTekgrPYYeDYa26f5hJAEL5w9unD+Xzflvj8o1VD+VGpIT1JNEjDO0g1DmYFUlAMOSc K6A6f3Wg0h1uMNmuKIwwlcCZlH241b2ZlSDL66JsZzKnp8RTLDWe+Pa/siaQY6GT83kv WJaw== X-Gm-Message-State: APzg51Dc5sRuohoar2XADGgRyTm9ZxleNqO+aEEji6mvBdKd2A2yZKuY xUPKI3oARM8ljJDlpTNM6GuB6nNE X-Google-Smtp-Source: ANB0VdYHJnyRlCD44j1x92IUY0Q3vxLJ6jS932oidZT9bbTpyjLRPedMO/y2Q9iMytZWCHotPubDAw== X-Received: by 2002:a62:808c:: with SMTP id j134-v6mr43549271pfd.120.1537488495896; Thu, 20 Sep 2018 17:08:15 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id q26-v6sm43355083pfj.127.2018.09.20.17.08.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 17:08:15 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: afd@ti.com, linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] hwmon: ina3221: Get channel names from DT node Date: Thu, 20 Sep 2018 17:07:53 -0700 Message-Id: <20180921000753.21846-3-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921000753.21846-1-nicoleotsuka@gmail.com> References: <20180921000753.21846-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 The connection of channels are usually descirbed in the schematics, which then should be indicated in DT binding as well, and further should get exposed to sysfs so as to help driver users understand what channels are really monitoring respectively. Meanwhile, channels could be left unconnected based on the hardware design. So the channel name should support NC so the driver could disable the channel accordingly. Signed-off-by: Nicolin Chen --- drivers/hwmon/ina3221.c | 88 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index e6b49500c52a..5d487e205260 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -41,9 +41,12 @@ #define INA3221_CONFIG_MODE_SHUNT BIT(1) #define INA3221_CONFIG_MODE_BUS BIT(2) #define INA3221_CONFIG_MODE_CONTINUOUS BIT(3) +#define INA3221_CONFIG_CHx_EN(x) BIT(14 - (x)) #define INA3221_RSHUNT_DEFAULT 10000 +#define INA3221_NOT_CONNECTED "NC" + enum ina3221_fields { /* Configuration */ F_RST, @@ -91,11 +94,20 @@ static const unsigned int register_channel[] = { * @regmap: Register map of the device * @fields: Register fields of the device * @shunt_resistors: Array of resistor values per channel + * @attr_group: attribute groups for sysfs node + * (leave one space at the end for NULL termination) + * @channel_name: channel names + * @enable: enable or disable channels */ struct ina3221_data { struct regmap *regmap; struct regmap_field *fields[F_MAX_FIELDS]; int shunt_resistors[INA3221_NUM_CHANNELS]; + + const struct attribute_group *attr_group[INA3221_NUM_CHANNELS + 1]; + const char *channel_name[INA3221_NUM_CHANNELS]; + + bool enable[INA3221_NUM_CHANNELS]; }; static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg, @@ -253,6 +265,24 @@ static ssize_t ina3221_show_alert(struct device *dev, return snprintf(buf, PAGE_SIZE, "%d\n", regval); } +static ssize_t ina3221_show_name(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); + struct ina3221_data *ina = dev_get_drvdata(dev); + unsigned int channel = sd_attr->index; + + return snprintf(buf, PAGE_SIZE, "%s\n", ina->channel_name[channel]); +} + +/* channel name */ +static SENSOR_DEVICE_ATTR(name1_input, 0444, + ina3221_show_name, NULL, INA3221_CHANNEL1); +static SENSOR_DEVICE_ATTR(name2_input, 0444, + ina3221_show_name, NULL, INA3221_CHANNEL2); +static SENSOR_DEVICE_ATTR(name3_input, 0444, + ina3221_show_name, NULL, INA3221_CHANNEL3); + /* bus voltage */ static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina3221_show_bus_voltage, NULL, INA3221_BUS1); @@ -317,8 +347,8 @@ static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ina3221_show_shunt_voltage, NULL, INA3221_SHUNT3); -static struct attribute *ina3221_attrs[] = { - /* channel 1 */ +static struct attribute *ina3221_channel1_attrs[] = { + &sensor_dev_attr_name1_input.dev_attr.attr, &sensor_dev_attr_in1_input.dev_attr.attr, &sensor_dev_attr_curr1_input.dev_attr.attr, &sensor_dev_attr_shunt1_resistor.dev_attr.attr, @@ -328,7 +358,11 @@ static struct attribute *ina3221_attrs[] = { &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, &sensor_dev_attr_in4_input.dev_attr.attr, - /* channel 2 */ + NULL, +}; + +static struct attribute *ina3221_channel2_attrs[] = { + &sensor_dev_attr_name2_input.dev_attr.attr, &sensor_dev_attr_in2_input.dev_attr.attr, &sensor_dev_attr_curr2_input.dev_attr.attr, &sensor_dev_attr_shunt2_resistor.dev_attr.attr, @@ -338,7 +372,11 @@ static struct attribute *ina3221_attrs[] = { &sensor_dev_attr_curr2_max_alarm.dev_attr.attr, &sensor_dev_attr_in5_input.dev_attr.attr, - /* channel 3 */ + NULL, +}; + +static struct attribute *ina3221_channel3_attrs[] = { + &sensor_dev_attr_name3_input.dev_attr.attr, &sensor_dev_attr_in3_input.dev_attr.attr, &sensor_dev_attr_curr3_input.dev_attr.attr, &sensor_dev_attr_shunt3_resistor.dev_attr.attr, @@ -350,7 +388,12 @@ static struct attribute *ina3221_attrs[] = { NULL, }; -ATTRIBUTE_GROUPS(ina3221); + +static const struct attribute_group ina3221_group[INA3221_NUM_CHANNELS] = { + { .attrs = ina3221_channel1_attrs, }, + { .attrs = ina3221_channel2_attrs, }, + { .attrs = ina3221_channel3_attrs, }, +}; static const struct regmap_range ina3221_yes_ranges[] = { regmap_reg_range(INA3221_SHUNT1, INA3221_BUS3), @@ -373,10 +416,14 @@ static const struct regmap_config ina3221_regmap_config = { static int ina3221_probe(struct i2c_client *client, const struct i2c_device_id *id) { + const struct device_node *np = client->dev.of_node; struct device *dev = &client->dev; struct ina3221_data *ina; struct device *hwmon_dev; - int i, ret; + u16 mask = 0, val = 0; + const char *str; + char prop[32]; + int i, g, ret; ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL); if (!ina) @@ -407,9 +454,34 @@ static int ina3221_probe(struct i2c_client *client, return ret; } + /* Fetch hardware information from Device Tree */ + for (i = 0, g = 0; i < INA3221_NUM_CHANNELS; i++) { + /* Fetch the channel name */ + sprintf(prop, "ti,channel%d-name", i + 1); + /* Set a default name on failure */ + if (of_property_read_string(np, prop, &str)) + str = "unknown"; + /* Ignore unconnected channels */ + if (!strcmp(str, INA3221_NOT_CONNECTED)) + continue; + /* Log connected channels */ + ina->attr_group[g++] = &ina3221_group[i]; + ina->channel_name[i] = str; + ina->enable[i] = true; + } + + /* Disable unconnected channels */ + for (i = 0; i < INA3221_NUM_CHANNELS; i++) { + mask |= INA3221_CONFIG_CHx_EN(i); + val |= ina->enable[i] ? INA3221_CONFIG_CHx_EN(i) : 0; + } + ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, val); + if (ret) + return ret; + hwmon_dev = devm_hwmon_device_register_with_groups(dev, - client->name, - ina, ina3221_groups); + client->name, ina, + ina->attr_group); if (IS_ERR(hwmon_dev)) { dev_err(dev, "Unable to register hwmon device\n"); return PTR_ERR(hwmon_dev);