Received: by 10.213.65.68 with SMTP id h4csp401890imn; Tue, 27 Mar 2018 01:08:43 -0700 (PDT) X-Google-Smtp-Source: AG47ELv0KAU4yebHZWW9nK1ldiXPmu48x0cZwCvWDs+W3x5O1hs/tk11BWmMPaf6eV9hFY4ok6GW X-Received: by 2002:a17:902:51ad:: with SMTP id y42-v6mr41078854plh.314.1522138123763; Tue, 27 Mar 2018 01:08:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522138123; cv=none; d=google.com; s=arc-20160816; b=VpDvqrmmhCpHv+hwYMONBSAfoOzLb4nMXtT/42Yre4QqLD354vve5sSFut2txKFw5I VpduGArHdH4G9x7C+AZKOofM8TK+Bk6fJxfWu0nWmtVCOwPEju5S2seI2wJYNs2azVkF YTZ6Mzbu8vEPSoprDW+PoruGNHUMBsiXYWGNiQtQK8xTq4UwZOwagyTDBss/eTCf1Wfp 9VOSnADHPfxp8QwxMkjA2knzDnHeKAHxnO75jZ+O9XHIUNA9jGFHKDMdaQ2srn/c8f92 bI+uSoF4NVoaxdmqDfFDpMLhLt7Oi5sqgfDqlDpKAwfmW/gDlBDE3iGwzinXyCDYEoyf 2TVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=qxD95ULV0Jn5eD1DgT2RI/ntIx+BGLOPUuVnXO9KJqk=; b=0FutgVJz2tXWXR5H9BRBncdyjeqMY0H6XmFCe23o8l7k+PC/PewdZeX/B80OWNLkaO UvICNkh3KtU1LGjB3+no+2nErxEuFI8lXHfVZoPzRxxlwhXxE2TpxHGUPLJ2s8s0ZVDd yFWMR68HTVhbhT03/i0PXZ2oN2mj1MlN0VK41kiVa64Wje1pVp7Rto8l/maMBXrcR8BP xGI7kUNtO46RrZeURR/+TR2kGPEcy7eVQpjKGx0Ior95JF0fhsG7JobJBGc0l1uJwvMB c71RXWPY0IyDAb/39dcaqGHXYAE7Ttbs3dspoUDTvL61517B5EDEXT8bmDXtrFPCSmfL NzFA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j2-v6si858173pli.501.2018.03.27.01.08.29; Tue, 27 Mar 2018 01:08:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752334AbeC0IGq (ORCPT + 99 others); Tue, 27 Mar 2018 04:06:46 -0400 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:35158 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752197AbeC0IF5 (ORCPT ); Tue, 27 Mar 2018 04:05:57 -0400 Received: from Internal Mail-Server by MTLPINE1 (envelope-from vadimp@mellanox.com) with ESMTPS (AES256-SHA encrypted); 27 Mar 2018 10:06:49 +0200 Received: from r-mgtswh-226.mtr.labs.mlnx. (r-mgtswh-226.mtr.labs.mlnx [10.209.1.51]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id w2R85mx6029508; Tue, 27 Mar 2018 11:05:55 +0300 From: Vadim Pasternak To: dvhart@infradead.org, andy.shevchenko@gmail.com, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org, jiri@resnulli.us, michaelsh@mellanox.com, ivecera@redhat.com, Vadim Pasternak Subject: [PATCH v1 6/7] platform/mellanox: Introduce support for Mellanox register access driver Date: Tue, 27 Mar 2018 10:02:06 +0000 Message-Id: <1522144927-56512-7-git-send-email-vadimp@mellanox.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1522144927-56512-1-git-send-email-vadimp@mellanox.com> References: <1522144927-56512-1-git-send-email-vadimp@mellanox.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It introduces new Mellanox platform driver to allow access to Mellanox programmable device register space trough sysfs interface. The driver purpose is to provide sysfs interface for user space for the registers essential for system control and monitoring. The sets of registers for sysfs access are supposed to be defined per system type bases and include the registers related to system resets operation, system reset causes monitoring and some kinds of mux selection. Signed-off-by: Vadim Pasternak --- drivers/platform/mellanox/Kconfig | 11 ++ drivers/platform/mellanox/Makefile | 1 + drivers/platform/mellanox/mlxreg-io.c | 221 ++++++++++++++++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 drivers/platform/mellanox/mlxreg-io.c diff --git a/drivers/platform/mellanox/Kconfig b/drivers/platform/mellanox/Kconfig index 591bccd..7fc266e 100644 --- a/drivers/platform/mellanox/Kconfig +++ b/drivers/platform/mellanox/Kconfig @@ -23,4 +23,15 @@ config MLXREG_HOTPLUG This driver handles hot-plug events for the power suppliers, power cables and fans on the wide range Mellanox IB and Ethernet systems. +config MLXREG_IO + tristate "Mellanox platform register access driver support" + depends on REGMAP + depends on HWMON + ---help--- + This driver allows access to Mellanox programmable device register + space trough sysfs interface. The sets of registers for sysfs access + are defined per system type bases and includes the registers related + to system resets operation, system reset causes monitoring and some + kinds of mux selection. + endif # MELLANOX_PLATFORM diff --git a/drivers/platform/mellanox/Makefile b/drivers/platform/mellanox/Makefile index 7c8385e..57074d9c 100644 --- a/drivers/platform/mellanox/Makefile +++ b/drivers/platform/mellanox/Makefile @@ -4,3 +4,4 @@ # Mellanox Platform-Specific Drivers # obj-$(CONFIG_MLXREG_HOTPLUG) += mlxreg-hotplug.o +obj-$(CONFIG_MLXREG_IO) += mlxreg-io.o diff --git a/drivers/platform/mellanox/mlxreg-io.c b/drivers/platform/mellanox/mlxreg-io.c new file mode 100644 index 0000000..f573b65 --- /dev/null +++ b/drivers/platform/mellanox/mlxreg-io.c @@ -0,0 +1,221 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Mellanox register access driver + * + * Copyright (C) 2018 Mellanox Technologies + * Copyright (C) 2018 Vadim Pasternak + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Attribute parameters. */ +#define MLXREG_IO_ATT_SIZE 10 +#define MLXREG_IO_ATT_NUM 48 + +/** + * struct mlxreg_io_priv_data - driver's private data: + * + * @pdev: platform device; + * @pdata: platform data; + * @hwmon: hwmon device; + * @mlxreg_io_attr: sysfs attributes array; + * @mlxreg_io_dev_attr: sysfs sensor device attribute array; + * @group: sysfs attribute group; + * @groups: list of sysfs attribute group for hwmon registration; + */ +struct mlxreg_io_priv_data { + struct platform_device *pdev; + struct mlxreg_core_platform_data *pdata; + struct device *hwmon; + struct attribute *mlxreg_io_attr[MLXREG_IO_ATT_NUM + 1]; + struct sensor_device_attribute mlxreg_io_dev_attr[MLXREG_IO_ATT_NUM]; + struct attribute_group group; + const struct attribute_group *groups[2]; +}; + +static ssize_t +mlxreg_io_attr_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev); + int index = to_sensor_dev_attr(attr)->index; + struct mlxreg_core_data *data = priv->pdata->data + index; + u32 regval = 0; + int ret; + + ret = regmap_read(priv->pdata->regmap, data->reg, ®val); + if (ret) + goto access_error; + + if (!data->bit) + regval = !!(regval & ~data->mask); + + return sprintf(buf, "%u\n", regval); + +access_error: + return ret; +} + +static ssize_t +mlxreg_io_attr_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t len) +{ + struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev); + int index = to_sensor_dev_attr(attr)->index; + struct mlxreg_core_data *data = priv->pdata->data + index; + u32 val, regval; + int ret; + + ret = kstrtou32(buf, MLXREG_IO_ATT_SIZE, &val); + if (ret) + return ret; + + ret = regmap_read(priv->pdata->regmap, data->reg, ®val); + if (ret) + goto access_error; + + regval &= data->mask; + + val = !!val; + if (val) + regval |= ~data->mask; + else + regval &= data->mask; + + ret = regmap_write(priv->pdata->regmap, data->reg, regval); + if (ret) + goto access_error; + + return len; + +access_error: + dev_err(&priv->pdev->dev, "Bus access error\n"); + return ret; +} + +static int mlxreg_io_attr_init(struct mlxreg_io_priv_data *priv) +{ + int i; + + priv->group.attrs = devm_kzalloc(&priv->pdev->dev, + priv->pdata->counter * + sizeof(struct attribute *), + GFP_KERNEL); + if (!priv->group.attrs) + return -ENOMEM; + + for (i = 0; i < priv->pdata->counter; i++) { + priv->mlxreg_io_attr[i] = + &priv->mlxreg_io_dev_attr[i].dev_attr.attr; + + /* Set attribute name as a label. */ + priv->mlxreg_io_attr[i]->name = + devm_kasprintf(&priv->pdev->dev, GFP_KERNEL, + priv->pdata->data[i].label); + + if (!priv->mlxreg_io_attr[i]->name) { + dev_err(&priv->pdev->dev, "Memory allocation failed for sysfs attribute %d.\n", + i + 1); + return -ENOMEM; + } + + priv->mlxreg_io_dev_attr[i].dev_attr.attr.mode = + priv->pdata->data[i].mode; + switch (priv->pdata->data[i].mode) { + case 0200: + priv->mlxreg_io_dev_attr[i].dev_attr.store = + mlxreg_io_attr_store; + break; + + case 0444: + priv->mlxreg_io_dev_attr[i].dev_attr.show = + mlxreg_io_attr_show; + break; + + case 0644: + priv->mlxreg_io_dev_attr[i].dev_attr.show = + mlxreg_io_attr_show; + priv->mlxreg_io_dev_attr[i].dev_attr.store = + mlxreg_io_attr_store; + break; + + default: + dev_err(&priv->pdev->dev, "Bad access mode %u for attribute %s.\n", + priv->pdata->data[i].mode, + priv->mlxreg_io_attr[i]->name); + return -EINVAL; + } + + priv->mlxreg_io_dev_attr[i].dev_attr.attr.name = + priv->mlxreg_io_attr[i]->name; + priv->mlxreg_io_dev_attr[i].index = i; + sysfs_attr_init(&priv->mlxreg_io_dev_attr[i].dev_attr.attr); + } + + priv->group.attrs = priv->mlxreg_io_attr; + priv->groups[0] = &priv->group; + priv->groups[1] = NULL; + + return 0; +} + +static int mlxreg_io_probe(struct platform_device *pdev) +{ + struct mlxreg_io_priv_data *priv; + int err; + + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->pdata = dev_get_platdata(&pdev->dev); + if (!priv->pdata) { + dev_err(&pdev->dev, "Failed to get platform data.\n"); + return -EINVAL; + } + + priv->pdev = pdev; + + err = mlxreg_io_attr_init(priv); + if (err) { + dev_err(&priv->pdev->dev, "Failed to allocate attributes: %d\n", + err); + return err; + } + + priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, + "mlxreg_io", + priv, + priv->groups); + if (IS_ERR(priv->hwmon)) { + dev_err(&pdev->dev, "Failed to register hwmon device %ld\n", + PTR_ERR(priv->hwmon)); + return PTR_ERR(priv->hwmon); + } + + dev_set_drvdata(&pdev->dev, priv); + + return 0; +} + +static struct platform_driver mlxreg_io_driver = { + .driver = { + .name = "mlxreg-io", + }, + .probe = mlxreg_io_probe, +}; + +module_platform_driver(mlxreg_io_driver); + +MODULE_AUTHOR("Vadim Pasternak "); +MODULE_DESCRIPTION("Mellanox regmap I/O access driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:mlxreg-io"); -- 2.1.4