From patchwork Tue Jun 26 13:34:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Matni X-Patchwork-Id: 10489127 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B1059601A0 for ; Tue, 26 Jun 2018 14:02:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 968FF28B62 for ; Tue, 26 Jun 2018 14:02:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 91BE928A95; Tue, 26 Jun 2018 14:02:52 +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=-7.9 required=2.0 tests=BAYES_00, 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 67A2528DA1 for ; Tue, 26 Jun 2018 14:01:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965566AbeFZOBL convert rfc822-to-8bit (ORCPT ); Tue, 26 Jun 2018 10:01:11 -0400 Received: from spqcsva02.exfo.com ([199.166.16.238]:53474 "EHLO SPQCSVA02.exfo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965453AbeFZOBJ (ORCPT ); Tue, 26 Jun 2018 10:01:09 -0400 X-Greylist: delayed 1594 seconds by postgrey-1.27 at vger.kernel.org; Tue, 26 Jun 2018 10:01:09 EDT Received: from SPQCSVA02.exfo.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 083BCD0007; Tue, 26 Jun 2018 09:34:35 -0400 (EDT) Received: from SPQCSVA02.exfo.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E72D4D0003; Tue, 26 Jun 2018 09:34:34 -0400 (EDT) Received: from SPQCCAS.exfo.com (unknown [10.28.36.9]) by SPQCSVA02.exfo.com (Postfix) with ESMTPS; Tue, 26 Jun 2018 09:34:34 -0400 (EDT) Received: from SPQCMBX02.exfo.com ([169.254.2.204]) by SPQCCAS02.exfo.com ([10.28.36.9]) with mapi id 14.03.0389.001; Tue, 26 Jun 2018 09:34:34 -0400 From: Gabriel Matni To: "linux-hwmon@vger.kernel.org" CC: "jdelvare@suse.com" , "linux@roeck-us.net" , "linux@arm.linux.org.uk" Subject: [PATCH] hwmon: driver for sfp/sfp+ integrated sensors Thread-Topic: [PATCH] hwmon: driver for sfp/sfp+ integrated sensors Thread-Index: AdQNUYY59sOj7GeWSo6SIBAfhI3pSA== Date: Tue, 26 Jun 2018 13:34:34 +0000 Message-ID: <3B588D51285A4A4D8D39C94212E078262A4503@SPQCMBX02.exfo.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.28.36.15] MIME-Version: 1.0 X-TM-AS-GCONF: 00 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 From: Gabriel Matni This driver supports the Small Form Factor Pluggable (SFP) Transceivers that implement the Digital Diagnostic Monitoring Interface (DDMI) as defined in SFF-8472. It currently supports temperature monitoring. Signed-off-by: Gabriel Matni --- drivers/hwmon/Kconfig | 11 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/sfpmon.c | 299 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+) create mode 100644 drivers/hwmon/sfpmon.c diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index f10840ad465c..52de64ccdcfa 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1357,6 +1357,17 @@ config SENSORS_S3C_RAW Say Y here if you want to include raw copies of all the ADC channels in sysfs. +config SENSORS_SFP + tristate "Small Form Factor Pluggable (SFP) Transceiver" + depends on I2C + help + If you say yes here you get support for the integrated sensors in + SFP/SFP+ modules that implement the Digital Diagnostic Monitoring + Interface (DDMI) as defined in SFF-8472. + + This driver can also be built as a module. If so, the module + will be called sfpmon. + config SENSORS_SIS5595 tristate "Silicon Integrated Systems Corp. SiS5595" depends on PCI diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index e7d52a36e6c4..d687b6667c1c 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -145,6 +145,7 @@ obj-$(CONFIG_SENSORS_S3C) += s3c-hwmon.o obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o +obj-$(CONFIG_SENSORS_SFP) += sfpmon.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o diff --git a/drivers/hwmon/sfpmon.c b/drivers/hwmon/sfpmon.c new file mode 100644 index 000000000000..7744577348f1 --- /dev/null +++ b/drivers/hwmon/sfpmon.c @@ -0,0 +1,299 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Hardware monitoring driver for SFP/SFP+ modules that implement the + * Digital Diagnostic Monitoring Interface (DDMI) as defined in SFF-8472. + * + */ + +#include +#include +#include +#include +#include +#include +#include + +#define DRIVER_NAME "sfpmon" + +/* id chip registers */ +#define SFF_8472_IDENTIFIER 0 +#define SFF_8472_DIAG_TYPE 92 + +/* monitoring chip registers */ +#define SFF_8472_TEMP_HIGH_ALARM 0 +#define SFF_8472_TEMP_LOW_ALARM 2 +#define SFF_8472_TEMP_HIGH_WARN 4 +#define SFF_8472_TEMP_LOW_WARN 6 +#define SFF_8472_T_SLOPE 84 +#define SFF_8472_T_OFFSET 86 +#define SFF_8472_TEMP 96 + +struct calib_info { + bool avail; + bool external; + u16 temp_slope; + s16 temp_offset; +}; + +struct sfp_data { + struct i2c_client *id_client; + struct i2c_client *ddmi_client; +}; + +static int sfp_detect(struct i2c_client *id, bool *detected) +{ + int ret; + s32 val; + + val = i2c_smbus_read_byte_data(id, SFF_8472_IDENTIFIER); + if (val < 0) { + ret = val; + *detected = false; + goto abort; + } + + *detected = (val == 3); + + ret = 0; +abort: + return ret; +} + +static int sfp_get_calib_info(struct i2c_client *id, struct i2c_client *ddmi, + struct calib_info *calib) +{ + int ret; + s32 val; + + val = i2c_smbus_read_byte_data(id, SFF_8472_DIAG_TYPE); + if (val < 0) { + ret = val; + goto abort; + } + + calib->avail = !!(val & BIT(6)); + calib->external = !!(val & BIT(4)); + + if (calib->avail && calib->external) { + u16 data; + + val = i2c_smbus_read_word_data(ddmi, SFF_8472_T_SLOPE); + if (unlikely(val < 0)) { + ret = val; + goto abort; + } + data = be16_to_cpu(val & 0xFFFF); + calib->temp_slope = *(s16 *)&data; + + val = i2c_smbus_read_word_data(ddmi, SFF_8472_T_OFFSET); + if (unlikely(val < 0)) { + ret = val; + goto abort; + } + data = be16_to_cpu(val & 0xFFFF); + calib->temp_offset = *(s16 *)&data; + } + + ret = 0; +abort: + return ret; +} + +static int sfp_get_temp(struct sfp_data *priv, u8 addr, long *temp_mC) +{ + int ret; + struct calib_info calib = {0}; + s32 temp, val; + u16 data; + bool detected; + + ret = sfp_detect(priv->id_client, &detected); + if (ret < 0) + goto abort; + + if (!detected) { + ret = -ENODEV; + goto abort; + } + + ret = sfp_get_calib_info(priv->id_client, priv->ddmi_client, &calib); + if (ret < 0) + goto abort; + + if (!calib.avail) { + ret = -ENODEV; + goto abort; + } + + val = i2c_smbus_read_word_data(priv->ddmi_client, addr); + if (unlikely(val < 0)) { + ret = val; + goto abort; + } + + data = be16_to_cpu(val & 0xFFFF); + temp = *(s16 *)&data; + + /* To get better precision we keep slope in unit of 1/256 and scale + * up the offset accordingly. We also convert to mC by multiplying + * by 1000. At the very end we divide by 256*256 (using >>16 + * operation). The first 256 is to rescale down, and the second + * 256 is because the calibration result is in unit of 1/256 + */ + if (calib.external) + *temp_mC = (((s64) calib.temp_slope * temp + + (calib.temp_offset << 8)) * 1000) >> 16; + else + *temp_mC = ((s64)temp*1000) >> 8; + +abort: + return ret; +} + +static int sfp_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *temp) +{ + struct sfp_data *priv = dev_get_drvdata(dev); + int err; + u8 reg; + + switch (attr) { + case hwmon_temp_input: + reg = SFF_8472_TEMP; + break; + case hwmon_temp_crit: + reg = SFF_8472_TEMP_HIGH_ALARM; + break; + case hwmon_temp_lcrit: + reg = SFF_8472_TEMP_LOW_ALARM; + break; + case hwmon_temp_max: + reg = SFF_8472_TEMP_HIGH_WARN; + break; + case hwmon_temp_min: + reg = SFF_8472_TEMP_LOW_WARN; + break; + default: + return -EOPNOTSUPP; + } + + err = sfp_get_temp(priv, reg, temp); + if (err < 0) + return err; + + return 0; +} + +static umode_t sfp_is_visible(const void *data, enum hwmon_sensor_types type, + u32 attr, int channel) +{ + if (type != hwmon_temp) + return 0; + + switch (attr) { + case hwmon_temp_input: + case hwmon_temp_crit: + case hwmon_temp_lcrit: + case hwmon_temp_max: + case hwmon_temp_min: + return S_IRUGO; + default: + return 0; + } +} + +static u32 ddmi_temp_config[] = { + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | HWMON_T_CRIT + | HWMON_T_LCRIT, + 0 +}; + +static const struct hwmon_channel_info ddmi_temp = { + .type = hwmon_temp, + .config = ddmi_temp_config, +}; + +static const struct hwmon_channel_info *channel_info[] = { + &ddmi_temp, + NULL +}; + +static const struct hwmon_ops sfp_hwmon_ops = { + .is_visible = sfp_is_visible, + .read = sfp_read, +}; + +static const struct hwmon_chip_info sfp_chip_info = { + .ops = &sfp_hwmon_ops, + .info = channel_info, +}; + +static int sfp_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct device *dev = &client->dev; + struct device *hwmon_dev; + struct device_node *id_node; + struct sfp_data *priv; + + if (!i2c_check_functionality(client->adapter, + I2C_FUNC_SMBUS_WORD_DATA)) { + dev_err(dev, + "adapter doesn't support SMBus word transactions\n"); + return -ENODEV; + } + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + i2c_set_clientdata(client, priv); + + priv->ddmi_client = client; + + id_node = of_parse_phandle(client->dev.of_node, "id", 0); + if (id_node) { + priv->id_client = of_find_i2c_device_by_node(id_node); + of_node_put(id_node); + } + + if (!priv->id_client) { + dev_err(dev, "ID client missing\n"); + return -ENODEV; + } + + hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, + priv, &sfp_chip_info, NULL); + + if (IS_ERR(hwmon_dev)) { + dev_err(dev, "unable to register sfp hwmon device\n"); + return PTR_ERR(hwmon_dev); + } + + return 0; +} + +static const struct i2c_device_id sfp_id[] = { + { "sfp", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, sfp_id); + +static const struct of_device_id sfp_of_match[] = { + { .compatible = "sfp,ddmi" }, + { }, +}; +MODULE_DEVICE_TABLE(of, sfp_of_match); + +static struct i2c_driver sfp_driver = { + .driver.name = DRIVER_NAME, + .driver.of_match_table = of_match_ptr(sfp_of_match), + .probe = sfp_probe, + .id_table = sfp_id, +}; + +module_i2c_driver(sfp_driver); + +MODULE_AUTHOR("Gabriel Matni "); +MODULE_DESCRIPTION("SFP/SFP+ hwmon driver"); +MODULE_LICENSE("GPL");