From: Guenter Roeck <linux@roeck-us.net>
To: Vadim Pasternak <vadimp@mellanox.com>
Cc: linux-hwmon@vger.kernel.org
Subject: Re: [PATCH v2 hwmon-next] hwmon: (mlxreg-fan) Add support for fan capability registers
Date: Wed, 20 Mar 2019 06:27:33 -0700 [thread overview]
Message-ID: <20190320132733.GA6565@roeck-us.net> (raw)
In-Reply-To: <20190318161028.29868-1-vadimp@mellanox.com>
On Mon, Mar 18, 2019 at 04:10:28PM +0000, Vadim Pasternak wrote:
> Add support for fan capability registers in order to distinct between
> the systems which have minor fan configuration differences. This
> reduces the amount of code used to describe such systems.
> The capability registers provides system specific information about the
> number of physically connected tachometers and system specific fan
> speed scale parameter.
> For example one system can be equipped with twelve fan tachometers,
> while the other with for example, eight or six. Or one system should
> use default fan speed divider value, while the other has a scale
> parameter defined in hardware, which should be used for divider
> setting.
> Reading this information from the capability registers allows to use the
> same fan structure for the systems with the such differences.
>
> Signed-off-by: Vadim Pasternak <vadimp@mellanox.com>
Applied to hwmon-next.
Thanks,
Guenter
> ---
> v1->v2:
> Comments pointed out by Guenter:
> - Make the defines names shorter to fit into 80 symbols.
> - Simplify mlxreg_fan_connect_verify().
> - Make more clear comment in mlxreg_fan_speed_divider_get().
> ---
> drivers/hwmon/mlxreg-fan.c | 73 ++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 68 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/hwmon/mlxreg-fan.c b/drivers/hwmon/mlxreg-fan.c
> index db8c6de0b6a0..44d4b1af1550 100644
> --- a/drivers/hwmon/mlxreg-fan.c
> +++ b/drivers/hwmon/mlxreg-fan.c
> @@ -27,7 +27,9 @@
> #define MLXREG_FAN_SPEED_MAX (MLXREG_FAN_MAX_STATE * 2)
> #define MLXREG_FAN_SPEED_MIN_LEVEL 2 /* 20 percent */
> #define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF 44
> -#define MLXREG_FAN_TACHO_DIVIDER_DEF 1132
> +#define MLXREG_FAN_TACHO_DIV_MIN 283
> +#define MLXREG_FAN_TACHO_DIV_DEF (MLXREG_FAN_TACHO_DIV_MIN * 4)
> +#define MLXREG_FAN_TACHO_DIV_SCALE_MAX 64
> /*
> * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-high.
> * The logic in a programmable device measures the time t-high by sampling the
> @@ -360,15 +362,57 @@ static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
> .set_cur_state = mlxreg_fan_set_cur_state,
> };
>
> +static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
> + struct mlxreg_core_data *data)
> +{
> + u32 regval;
> + int err;
> +
> + err = regmap_read(fan->regmap, data->capability, ®val);
> + if (err) {
> + dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
> + data->capability);
> + return err;
> + }
> +
> + return !!(regval & data->bit);
> +}
> +
> +static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
> + struct mlxreg_core_data *data)
> +{
> + u32 regval;
> + int err;
> +
> + err = regmap_read(fan->regmap, data->capability, ®val);
> + if (err) {
> + dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
> + data->capability);
> + return err;
> + }
> +
> + /*
> + * Set divider value according to the capability register, in case it
> + * contains valid value. Otherwise use default value. The purpose of
> + * this validation is to protect against the old hardware, in which
> + * this register can return zero.
> + */
> + if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIV_SCALE_MAX)
> + fan->divider = regval * MLXREG_FAN_TACHO_DIV_MIN;
> +
> + return 0;
> +}
> +
> static int mlxreg_fan_config(struct mlxreg_fan *fan,
> struct mlxreg_core_platform_data *pdata)
> {
> struct mlxreg_core_data *data = pdata->data;
> bool configured = false;
> int tacho_num = 0, i;
> + int err;
>
> fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
> - fan->divider = MLXREG_FAN_TACHO_DIVIDER_DEF;
> + fan->divider = MLXREG_FAN_TACHO_DIV_DEF;
> for (i = 0; i < pdata->counter; i++, data++) {
> if (strnstr(data->label, "tacho", sizeof(data->label))) {
> if (tacho_num == MLXREG_FAN_MAX_TACHO) {
> @@ -376,6 +420,17 @@ static int mlxreg_fan_config(struct mlxreg_fan *fan,
> data->label);
> return -EINVAL;
> }
> +
> + if (data->capability) {
> + err = mlxreg_fan_connect_verify(fan, data);
> + if (err < 0)
> + return err;
> + else if (!err) {
> + tacho_num++;
> + continue;
> + }
> + }
> +
> fan->tacho[tacho_num].reg = data->reg;
> fan->tacho[tacho_num].mask = data->mask;
> fan->tacho[tacho_num++].connected = true;
> @@ -394,13 +449,21 @@ static int mlxreg_fan_config(struct mlxreg_fan *fan,
> return -EINVAL;
> }
> /* Validate that conf parameters are not zeros. */
> - if (!data->mask || !data->bit) {
> + if (!data->mask && !data->bit && !data->capability) {
> dev_err(fan->dev, "invalid conf entry params: %s\n",
> data->label);
> return -EINVAL;
> }
> - fan->samples = data->mask;
> - fan->divider = data->bit;
> + if (data->capability) {
> + err = mlxreg_fan_speed_divider_get(fan, data);
> + if (err)
> + return err;
> + } else {
> + if (data->mask)
> + fan->samples = data->mask;
> + if (data->bit)
> + fan->divider = data->bit;
> + }
> configured = true;
> } else {
> dev_err(fan->dev, "invalid label: %s\n", data->label);
prev parent reply other threads:[~2019-03-20 13:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-18 16:10 [PATCH v2 hwmon-next] hwmon: (mlxreg-fan) Add support for fan capability registers Vadim Pasternak
2019-03-20 13:27 ` Guenter Roeck [this message]
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=20190320132733.GA6565@roeck-us.net \
--to=linux@roeck-us.net \
--cc=linux-hwmon@vger.kernel.org \
--cc=vadimp@mellanox.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;
as well as URLs for NNTP newsgroup(s).