public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Michael Walle <michael@walle.cc>
Cc: Andrew Lunn <andrew@lunn.ch>,
	Heiner Kallweit <hkallweit1@gmail.com>,
	Russell King <linux@armlinux.org.uk>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Xu Liang <lxu@maxlinear.com>, Jean Delvare <jdelvare@suse.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hwmon@vger.kernel.org
Subject: Re: [PATCH net-next] net: phy: mxl-gpy: add temperature sensor
Date: Wed, 22 Jun 2022 08:05:37 -0700	[thread overview]
Message-ID: <20220622150537.GD1861763@roeck-us.net> (raw)
In-Reply-To: <20220622141716.3517645-1-michael@walle.cc>

On Wed, Jun 22, 2022 at 04:17:16PM +0200, Michael Walle wrote:
> The GPY115 and GPY2xx PHYs contain an integrated temperature sensor. It
> accuracy is +/- 5°C. Add support for it.
> 
> Signed-off-by: Michael Walle <michael@walle.cc>

Acked-by: Guenter Roeck <linux@roeck-us.net>

> ---
>  drivers/net/phy/Kconfig   |   2 +
>  drivers/net/phy/mxl-gpy.c | 106 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 108 insertions(+)
> 
> diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
> index 9fee639ee5c8..09fa17796d4d 100644
> --- a/drivers/net/phy/Kconfig
> +++ b/drivers/net/phy/Kconfig
> @@ -216,6 +216,8 @@ config MARVELL_88X2222_PHY
>  
>  config MAXLINEAR_GPHY
>  	tristate "Maxlinear Ethernet PHYs"
> +	select POLYNOMIAL if HWMON
> +	depends on HWMON || HWMON=n
>  	help
>  	  Support for the Maxlinear GPY115, GPY211, GPY212, GPY215,
>  	  GPY241, GPY245 PHYs.
> diff --git a/drivers/net/phy/mxl-gpy.c b/drivers/net/phy/mxl-gpy.c
> index 6c4da2f9e90a..5b99acf44337 100644
> --- a/drivers/net/phy/mxl-gpy.c
> +++ b/drivers/net/phy/mxl-gpy.c
> @@ -8,7 +8,9 @@
>  
>  #include <linux/module.h>
>  #include <linux/bitfield.h>
> +#include <linux/hwmon.h>
>  #include <linux/phy.h>
> +#include <linux/polynomial.h>
>  #include <linux/netdevice.h>
>  
>  /* PHY ID */
> @@ -64,6 +66,10 @@
>  #define VSPEC1_SGMII_ANEN_ANRS	(VSPEC1_SGMII_CTRL_ANEN | \
>  				 VSPEC1_SGMII_CTRL_ANRS)
>  
> +/* Temperature sensor */
> +#define VPSPEC1_TEMP_STA	0x0E
> +#define VPSPEC1_TEMP_STA_DATA	GENMASK(9, 0)
> +
>  /* WoL */
>  #define VPSPEC2_WOL_CTL		0x0E06
>  #define VPSPEC2_WOL_AD01	0x0E08
> @@ -80,6 +86,102 @@ static const struct {
>  	{9, 0x73},
>  };
>  
> +#if IS_ENABLED(CONFIG_HWMON)
> +/* The original translation formulae of the temperature (in degrees of Celsius)
> + * are as follows:
> + *
> + *   T = -2.5761e-11*(N^4) + 9.7332e-8*(N^3) + -1.9165e-4*(N^2) +
> + *       3.0762e-1*(N^1) + -5.2156e1
> + *
> + * where [-52.156, 137.961]C and N = [0, 1023].
> + *
> + * They must be accordingly altered to be suitable for the integer arithmetics.
> + * The technique is called 'factor redistribution', which just makes sure the
> + * multiplications and divisions are made so to have a result of the operations
> + * within the integer numbers limit. In addition we need to translate the
> + * formulae to accept millidegrees of Celsius. Here what it looks like after
> + * the alterations:
> + *
> + *   T = -25761e-12*(N^4) + 97332e-9*(N^3) + -191650e-6*(N^2) +
> + *       307620e-3*(N^1) + -52156
> + *
> + * where T = [-52156, 137961]mC and N = [0, 1023].
> + */
> +static const struct polynomial poly_N_to_temp = {
> +	.terms = {
> +		{4,  -25761, 1000, 1},
> +		{3,   97332, 1000, 1},
> +		{2, -191650, 1000, 1},
> +		{1,  307620, 1000, 1},
> +		{0,  -52156,    1, 1}
> +	}
> +};
> +
> +static int gpy_hwmon_read(struct device *dev,
> +			  enum hwmon_sensor_types type,
> +			  u32 attr, int channel, long *value)
> +{
> +	struct phy_device *phydev = dev_get_drvdata(dev);
> +	int ret;
> +
> +	ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, VPSPEC1_TEMP_STA);
> +	if (ret < 0)
> +		return ret;
> +	if (!ret)
> +		return -ENODATA;
> +
> +	*value = polynomial_calc(&poly_N_to_temp,
> +				 FIELD_GET(VPSPEC1_TEMP_STA_DATA, ret));
> +
> +	return 0;
> +}
> +
> +static umode_t gpy_hwmon_is_visible(const void *data,
> +				    enum hwmon_sensor_types type,
> +				    u32 attr, int channel)
> +{
> +	return 0444;
> +}
> +
> +static const struct hwmon_channel_info *gpy_hwmon_info[] = {
> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
> +	NULL
> +};
> +
> +static const struct hwmon_ops gpy_hwmon_hwmon_ops = {
> +	.is_visible	= gpy_hwmon_is_visible,
> +	.read		= gpy_hwmon_read,
> +};
> +
> +static const struct hwmon_chip_info gpy_hwmon_chip_info = {
> +	.ops		= &gpy_hwmon_hwmon_ops,
> +	.info		= gpy_hwmon_info,
> +};
> +
> +static int gpy_hwmon_register(struct phy_device *phydev)
> +{
> +	struct device *dev = &phydev->mdio.dev;
> +	struct device *hwmon_dev;
> +	char *hwmon_name;
> +
> +	hwmon_name = devm_hwmon_sanitize_name(dev, dev_name(dev));
> +	if (IS_ERR(hwmon_name))
> +		return PTR_ERR(hwmon_name);
> +
> +	hwmon_dev = devm_hwmon_device_register_with_info(dev, hwmon_name,
> +							 phydev,
> +							 &gpy_hwmon_chip_info,
> +							 NULL);
> +
> +	return PTR_ERR_OR_ZERO(hwmon_dev);
> +}
> +#else
> +static int gpy_hwmon_register(struct phy_device *phydev)
> +{
> +	return 0;
> +}
> +#endif
> +
>  static int gpy_config_init(struct phy_device *phydev)
>  {
>  	int ret;
> @@ -109,6 +211,10 @@ static int gpy_probe(struct phy_device *phydev)
>  	if (ret < 0)
>  		return ret;
>  
> +	ret = gpy_hwmon_register(phydev);
> +	if (ret)
> +		return ret;
> +
>  	phydev_info(phydev, "Firmware Version: 0x%04X (%s)\n", ret,
>  		    (ret & PHY_FWV_REL_MASK) ? "release" : "test");
>  
> -- 
> 2.30.2
> 

  reply	other threads:[~2022-06-22 15:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-22 14:17 [PATCH net-next] net: phy: mxl-gpy: add temperature sensor Michael Walle
2022-06-22 15:05 ` Guenter Roeck [this message]
2022-06-23 12:37 ` Andrew Lunn
2022-06-24  4:00 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220622150537.GD1861763@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hkallweit1@gmail.com \
    --cc=jdelvare@suse.com \
    --cc=kuba@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=lxu@maxlinear.com \
    --cc=michael@walle.cc \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox