2018-07-03 15:00:09 +08:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
|
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Mellanox Technologies. All rights reserved.
|
|
|
|
// Copyright (c) 2018 Vadim Pasternak <vadimp@mellanox.com>
|
|
|
|
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/hwmon.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_data/mlxreg.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/regmap.h>
|
|
|
|
#include <linux/thermal.h>
|
|
|
|
|
2021-09-17 03:47:17 +08:00
|
|
|
#define MLXREG_FAN_MAX_TACHO 14
|
2021-09-17 03:47:18 +08:00
|
|
|
#define MLXREG_FAN_MAX_PWM 4
|
|
|
|
#define MLXREG_FAN_PWM_NOT_CONNECTED 0xff
|
2018-07-03 15:00:09 +08:00
|
|
|
#define MLXREG_FAN_MAX_STATE 10
|
|
|
|
#define MLXREG_FAN_MIN_DUTY 51 /* 20% */
|
|
|
|
#define MLXREG_FAN_MAX_DUTY 255 /* 100% */
|
|
|
|
#define MLXREG_FAN_SPEED_MIN_LEVEL 2 /* 20 percent */
|
|
|
|
#define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF 44
|
2019-03-19 00:10:28 +08:00
|
|
|
#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
|
2018-07-03 15:00:09 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* tachometer every t-sample (with the default value 11.32 uS) and increment
|
|
|
|
* a counter (N) as long as the pulse has not change:
|
|
|
|
* RPM = 15 / (t-sample * (K + Regval)), where:
|
|
|
|
* Regval: is the value read from the programmable device register;
|
|
|
|
* - 0xff - represents tachometer fault;
|
|
|
|
* - 0xfe - represents tachometer minimum value , which is 4444 RPM;
|
|
|
|
* - 0x00 - represents tachometer maximum value , which is 300000 RPM;
|
|
|
|
* K: is 44 and it represents the minimum allowed samples per pulse;
|
|
|
|
* N: is equal K + Regval;
|
|
|
|
* In order to calculate RPM from the register value the following formula is
|
|
|
|
* used: RPM = 15 / ((Regval + K) * 11.32) * 10^(-6)), which in the
|
|
|
|
* default case is modified to:
|
|
|
|
* RPM = 15000000 * 100 / ((Regval + 44) * 1132);
|
|
|
|
* - for Regval 0x00, RPM will be 15000000 * 100 / (44 * 1132) = 30115;
|
|
|
|
* - for Regval 0xfe, RPM will be 15000000 * 100 / ((254 + 44) * 1132) = 4446;
|
|
|
|
* In common case the formula is modified to:
|
|
|
|
* RPM = 15000000 * 100 / ((Regval + samples) * divider).
|
|
|
|
*/
|
|
|
|
#define MLXREG_FAN_GET_RPM(rval, d, s) (DIV_ROUND_CLOSEST(15000000 * 100, \
|
|
|
|
((rval) + (s)) * (d)))
|
2018-11-21 07:16:36 +08:00
|
|
|
#define MLXREG_FAN_GET_FAULT(val, mask) ((val) == (mask))
|
2018-07-03 15:00:09 +08:00
|
|
|
#define MLXREG_FAN_PWM_DUTY2STATE(duty) (DIV_ROUND_CLOSEST((duty) * \
|
|
|
|
MLXREG_FAN_MAX_STATE, \
|
|
|
|
MLXREG_FAN_MAX_DUTY))
|
|
|
|
#define MLXREG_FAN_PWM_STATE2DUTY(stat) (DIV_ROUND_CLOSEST((stat) * \
|
|
|
|
MLXREG_FAN_MAX_DUTY, \
|
|
|
|
MLXREG_FAN_MAX_STATE))
|
|
|
|
|
2021-09-17 05:31:28 +08:00
|
|
|
struct mlxreg_fan;
|
|
|
|
|
2018-07-03 15:00:09 +08:00
|
|
|
/*
|
|
|
|
* struct mlxreg_fan_tacho - tachometer data (internal use):
|
|
|
|
*
|
|
|
|
* @connected: indicates if tachometer is connected;
|
|
|
|
* @reg: register offset;
|
|
|
|
* @mask: fault mask;
|
2021-03-23 01:22:37 +08:00
|
|
|
* @prsnt: present register offset;
|
2018-07-03 15:00:09 +08:00
|
|
|
*/
|
|
|
|
struct mlxreg_fan_tacho {
|
|
|
|
bool connected;
|
|
|
|
u32 reg;
|
|
|
|
u32 mask;
|
2021-03-23 01:22:37 +08:00
|
|
|
u32 prsnt;
|
2018-07-03 15:00:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct mlxreg_fan_pwm - PWM data (internal use):
|
|
|
|
*
|
2021-09-17 05:31:28 +08:00
|
|
|
* @fan: private data;
|
2018-07-03 15:00:09 +08:00
|
|
|
* @connected: indicates if PWM is connected;
|
|
|
|
* @reg: register offset;
|
2021-09-17 05:31:28 +08:00
|
|
|
* @cooling: cooling device levels;
|
2022-01-26 22:18:25 +08:00
|
|
|
* @last_hwmon_state: last cooling state set by hwmon subsystem;
|
|
|
|
* @last_thermal_state: last cooling state set by thermal subsystem;
|
2021-09-17 05:31:28 +08:00
|
|
|
* @cdev: cooling device;
|
2018-07-03 15:00:09 +08:00
|
|
|
*/
|
|
|
|
struct mlxreg_fan_pwm {
|
2021-09-17 05:31:28 +08:00
|
|
|
struct mlxreg_fan *fan;
|
2018-07-03 15:00:09 +08:00
|
|
|
bool connected;
|
|
|
|
u32 reg;
|
2022-01-26 22:18:25 +08:00
|
|
|
unsigned long last_hwmon_state;
|
|
|
|
unsigned long last_thermal_state;
|
2021-09-17 05:31:28 +08:00
|
|
|
struct thermal_cooling_device *cdev;
|
2018-07-03 15:00:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct mlxreg_fan - private data (internal use):
|
|
|
|
*
|
|
|
|
* @dev: basic device;
|
|
|
|
* @regmap: register map of parent device;
|
|
|
|
* @tacho: tachometer data;
|
|
|
|
* @pwm: PWM data;
|
2021-03-23 01:22:37 +08:00
|
|
|
* @tachos_per_drwr - number of tachometers per drawer;
|
2018-07-03 15:00:09 +08:00
|
|
|
* @samples: minimum allowed samples per pulse;
|
|
|
|
* @divider: divider value for tachometer RPM calculation;
|
|
|
|
*/
|
|
|
|
struct mlxreg_fan {
|
|
|
|
struct device *dev;
|
|
|
|
void *regmap;
|
|
|
|
struct mlxreg_core_platform_data *pdata;
|
|
|
|
struct mlxreg_fan_tacho tacho[MLXREG_FAN_MAX_TACHO];
|
2021-09-17 03:47:18 +08:00
|
|
|
struct mlxreg_fan_pwm pwm[MLXREG_FAN_MAX_PWM];
|
2021-03-23 01:22:37 +08:00
|
|
|
int tachos_per_drwr;
|
2018-07-03 15:00:09 +08:00
|
|
|
int samples;
|
|
|
|
int divider;
|
|
|
|
};
|
|
|
|
|
2022-01-26 22:18:25 +08:00
|
|
|
static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
|
|
|
|
unsigned long state);
|
|
|
|
|
2018-07-03 15:00:09 +08:00
|
|
|
static int
|
|
|
|
mlxreg_fan_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
|
|
|
|
int channel, long *val)
|
|
|
|
{
|
|
|
|
struct mlxreg_fan *fan = dev_get_drvdata(dev);
|
|
|
|
struct mlxreg_fan_tacho *tacho;
|
2021-09-17 03:47:18 +08:00
|
|
|
struct mlxreg_fan_pwm *pwm;
|
2018-07-03 15:00:09 +08:00
|
|
|
u32 regval;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_fan:
|
|
|
|
tacho = &fan->tacho[channel];
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_fan_input:
|
2021-03-23 01:22:37 +08:00
|
|
|
/*
|
|
|
|
* Check FAN presence: FAN related bit in presence register is one,
|
|
|
|
* if FAN is physically connected, zero - otherwise.
|
|
|
|
*/
|
|
|
|
if (tacho->prsnt && fan->tachos_per_drwr) {
|
|
|
|
err = regmap_read(fan->regmap, tacho->prsnt, ®val);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map channel to presence bit - drawer can be equipped with
|
|
|
|
* one or few FANs, while presence is indicated per drawer.
|
|
|
|
*/
|
|
|
|
if (BIT(channel / fan->tachos_per_drwr) & regval) {
|
|
|
|
/* FAN is not connected - return zero for FAN speed. */
|
|
|
|
*val = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:00:09 +08:00
|
|
|
err = regmap_read(fan->regmap, tacho->reg, ®val);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2023-02-12 22:57:30 +08:00
|
|
|
if (MLXREG_FAN_GET_FAULT(regval, tacho->mask)) {
|
|
|
|
/* FAN is broken - return zero for FAN speed. */
|
|
|
|
*val = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:00:09 +08:00
|
|
|
*val = MLXREG_FAN_GET_RPM(regval, fan->divider,
|
|
|
|
fan->samples);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case hwmon_fan_fault:
|
|
|
|
err = regmap_read(fan->regmap, tacho->reg, ®val);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
*val = MLXREG_FAN_GET_FAULT(regval, tacho->mask);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case hwmon_pwm:
|
2021-09-17 03:47:18 +08:00
|
|
|
pwm = &fan->pwm[channel];
|
2018-07-03 15:00:09 +08:00
|
|
|
switch (attr) {
|
|
|
|
case hwmon_pwm_input:
|
2021-09-17 03:47:18 +08:00
|
|
|
err = regmap_read(fan->regmap, pwm->reg, ®val);
|
2018-07-03 15:00:09 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
*val = regval;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mlxreg_fan_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
|
|
|
|
int channel, long val)
|
|
|
|
{
|
|
|
|
struct mlxreg_fan *fan = dev_get_drvdata(dev);
|
2021-09-17 03:47:18 +08:00
|
|
|
struct mlxreg_fan_pwm *pwm;
|
2018-07-03 15:00:09 +08:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_pwm:
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_pwm_input:
|
|
|
|
if (val < MLXREG_FAN_MIN_DUTY ||
|
|
|
|
val > MLXREG_FAN_MAX_DUTY)
|
|
|
|
return -EINVAL;
|
2021-09-17 03:47:18 +08:00
|
|
|
pwm = &fan->pwm[channel];
|
2022-01-26 22:18:25 +08:00
|
|
|
/* If thermal is configured - handle PWM limit setting. */
|
|
|
|
if (IS_REACHABLE(CONFIG_THERMAL)) {
|
|
|
|
pwm->last_hwmon_state = MLXREG_FAN_PWM_DUTY2STATE(val);
|
|
|
|
/*
|
|
|
|
* Update PWM only in case requested state is not less than the
|
|
|
|
* last thermal state.
|
|
|
|
*/
|
|
|
|
if (pwm->last_hwmon_state >= pwm->last_thermal_state)
|
|
|
|
return mlxreg_fan_set_cur_state(pwm->cdev,
|
|
|
|
pwm->last_hwmon_state);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-17 03:47:18 +08:00
|
|
|
return regmap_write(fan->regmap, pwm->reg, val);
|
2018-07-03 15:00:09 +08:00
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static umode_t
|
|
|
|
mlxreg_fan_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
|
|
|
|
int channel)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_fan:
|
|
|
|
if (!(((struct mlxreg_fan *)data)->tacho[channel].connected))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_fan_input:
|
|
|
|
case hwmon_fan_fault:
|
|
|
|
return 0444;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case hwmon_pwm:
|
2021-09-17 03:47:18 +08:00
|
|
|
if (!(((struct mlxreg_fan *)data)->pwm[channel].connected))
|
2018-07-03 15:00:09 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_pwm_input:
|
|
|
|
return 0644;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-26 13:35:41 +08:00
|
|
|
static char *mlxreg_fan_name[] = {
|
|
|
|
"mlxreg_fan",
|
|
|
|
"mlxreg_fan1",
|
|
|
|
"mlxreg_fan2",
|
|
|
|
"mlxreg_fan3",
|
|
|
|
};
|
|
|
|
|
2023-04-07 04:35:25 +08:00
|
|
|
static const struct hwmon_channel_info * const mlxreg_fan_hwmon_info[] = {
|
hwmon: (mlxreg-fan) Use HWMON_CHANNEL_INFO macro
The HWMON_CHANNEL_INFO macro simplifies the code, reduces the likelihood
of errors, and makes the code easier to read.
The conversion was done automatically with coccinelle. The semantic patch
used to make this change is as follows.
@r@
initializer list elements;
identifier i;
@@
-u32 i[] = {
- elements,
- 0
-};
@s@
identifier r.i,j,ty;
@@
-struct hwmon_channel_info j = {
- .type = ty,
- .config = i,
-};
@script:ocaml t@
ty << s.ty;
elements << r.elements;
shorter;
elems;
@@
shorter :=
make_ident (List.hd(List.rev (Str.split (Str.regexp "_") ty)));
elems :=
make_ident
(String.concat ","
(List.map (fun x -> Printf.sprintf "\n\t\t\t %s" x)
(Str.split (Str.regexp " , ") elements)))
@@
identifier s.j,t.shorter;
identifier t.elems;
@@
- &j
+ HWMON_CHANNEL_INFO(shorter,elems)
This patch does not introduce functional changes. Many thanks to
Julia Lawall for providing the semantic patch.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2019-04-01 01:53:50 +08:00
|
|
|
HWMON_CHANNEL_INFO(fan,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
2021-09-17 03:47:17 +08:00
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
hwmon: (mlxreg-fan) Use HWMON_CHANNEL_INFO macro
The HWMON_CHANNEL_INFO macro simplifies the code, reduces the likelihood
of errors, and makes the code easier to read.
The conversion was done automatically with coccinelle. The semantic patch
used to make this change is as follows.
@r@
initializer list elements;
identifier i;
@@
-u32 i[] = {
- elements,
- 0
-};
@s@
identifier r.i,j,ty;
@@
-struct hwmon_channel_info j = {
- .type = ty,
- .config = i,
-};
@script:ocaml t@
ty << s.ty;
elements << r.elements;
shorter;
elems;
@@
shorter :=
make_ident (List.hd(List.rev (Str.split (Str.regexp "_") ty)));
elems :=
make_ident
(String.concat ","
(List.map (fun x -> Printf.sprintf "\n\t\t\t %s" x)
(Str.split (Str.regexp " , ") elements)))
@@
identifier s.j,t.shorter;
identifier t.elems;
@@
- &j
+ HWMON_CHANNEL_INFO(shorter,elems)
This patch does not introduce functional changes. Many thanks to
Julia Lawall for providing the semantic patch.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2019-04-01 01:53:50 +08:00
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT,
|
|
|
|
HWMON_F_INPUT | HWMON_F_FAULT),
|
|
|
|
HWMON_CHANNEL_INFO(pwm,
|
2021-09-17 03:47:18 +08:00
|
|
|
HWMON_PWM_INPUT,
|
|
|
|
HWMON_PWM_INPUT,
|
|
|
|
HWMON_PWM_INPUT,
|
hwmon: (mlxreg-fan) Use HWMON_CHANNEL_INFO macro
The HWMON_CHANNEL_INFO macro simplifies the code, reduces the likelihood
of errors, and makes the code easier to read.
The conversion was done automatically with coccinelle. The semantic patch
used to make this change is as follows.
@r@
initializer list elements;
identifier i;
@@
-u32 i[] = {
- elements,
- 0
-};
@s@
identifier r.i,j,ty;
@@
-struct hwmon_channel_info j = {
- .type = ty,
- .config = i,
-};
@script:ocaml t@
ty << s.ty;
elements << r.elements;
shorter;
elems;
@@
shorter :=
make_ident (List.hd(List.rev (Str.split (Str.regexp "_") ty)));
elems :=
make_ident
(String.concat ","
(List.map (fun x -> Printf.sprintf "\n\t\t\t %s" x)
(Str.split (Str.regexp " , ") elements)))
@@
identifier s.j,t.shorter;
identifier t.elems;
@@
- &j
+ HWMON_CHANNEL_INFO(shorter,elems)
This patch does not introduce functional changes. Many thanks to
Julia Lawall for providing the semantic patch.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2019-04-01 01:53:50 +08:00
|
|
|
HWMON_PWM_INPUT),
|
2018-07-03 15:00:09 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct hwmon_ops mlxreg_fan_hwmon_hwmon_ops = {
|
|
|
|
.is_visible = mlxreg_fan_is_visible,
|
|
|
|
.read = mlxreg_fan_read,
|
|
|
|
.write = mlxreg_fan_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info = {
|
|
|
|
.ops = &mlxreg_fan_hwmon_hwmon_ops,
|
|
|
|
.info = mlxreg_fan_hwmon_info,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mlxreg_fan_get_max_state(struct thermal_cooling_device *cdev,
|
|
|
|
unsigned long *state)
|
|
|
|
{
|
|
|
|
*state = MLXREG_FAN_MAX_STATE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mlxreg_fan_get_cur_state(struct thermal_cooling_device *cdev,
|
|
|
|
unsigned long *state)
|
|
|
|
|
|
|
|
{
|
2021-09-17 05:31:28 +08:00
|
|
|
struct mlxreg_fan_pwm *pwm = cdev->devdata;
|
|
|
|
struct mlxreg_fan *fan = pwm->fan;
|
2018-07-03 15:00:09 +08:00
|
|
|
u32 regval;
|
|
|
|
int err;
|
|
|
|
|
2021-09-17 05:31:28 +08:00
|
|
|
err = regmap_read(fan->regmap, pwm->reg, ®val);
|
2018-07-03 15:00:09 +08:00
|
|
|
if (err) {
|
|
|
|
dev_err(fan->dev, "Failed to query PWM duty\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*state = MLXREG_FAN_PWM_DUTY2STATE(regval);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
|
|
|
|
unsigned long state)
|
|
|
|
|
|
|
|
{
|
2021-09-17 05:31:28 +08:00
|
|
|
struct mlxreg_fan_pwm *pwm = cdev->devdata;
|
|
|
|
struct mlxreg_fan *fan = pwm->fan;
|
2018-07-03 15:00:09 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (state > MLXREG_FAN_MAX_STATE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-01-26 22:18:25 +08:00
|
|
|
/* Save thermal state. */
|
|
|
|
pwm->last_thermal_state = state;
|
|
|
|
|
|
|
|
state = max_t(unsigned long, state, pwm->last_hwmon_state);
|
2021-09-17 05:31:28 +08:00
|
|
|
err = regmap_write(fan->regmap, pwm->reg,
|
2018-07-03 15:00:09 +08:00
|
|
|
MLXREG_FAN_PWM_STATE2DUTY(state));
|
|
|
|
if (err) {
|
|
|
|
dev_err(fan->dev, "Failed to write PWM duty\n");
|
|
|
|
return err;
|
|
|
|
}
|
2022-01-26 22:18:25 +08:00
|
|
|
return 0;
|
2018-07-03 15:00:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
|
|
|
|
.get_max_state = mlxreg_fan_get_max_state,
|
|
|
|
.get_cur_state = mlxreg_fan_get_cur_state,
|
|
|
|
.set_cur_state = mlxreg_fan_set_cur_state,
|
|
|
|
};
|
|
|
|
|
2019-03-19 00:10:28 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-17 03:47:18 +08:00
|
|
|
static int mlxreg_pwm_connect_verify(struct mlxreg_fan *fan,
|
|
|
|
struct mlxreg_core_data *data)
|
|
|
|
{
|
|
|
|
u32 regval;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = regmap_read(fan->regmap, data->reg, ®val);
|
|
|
|
if (err) {
|
|
|
|
dev_err(fan->dev, "Failed to query pwm register 0x%08x\n",
|
|
|
|
data->reg);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return regval != MLXREG_FAN_PWM_NOT_CONNECTED;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:10:28 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:00:09 +08:00
|
|
|
static int mlxreg_fan_config(struct mlxreg_fan *fan,
|
|
|
|
struct mlxreg_core_platform_data *pdata)
|
|
|
|
{
|
2021-09-17 03:47:18 +08:00
|
|
|
int tacho_num = 0, tacho_avail = 0, pwm_num = 0, i;
|
2018-07-03 15:00:09 +08:00
|
|
|
struct mlxreg_core_data *data = pdata->data;
|
|
|
|
bool configured = false;
|
2019-03-19 00:10:28 +08:00
|
|
|
int err;
|
2018-07-03 15:00:09 +08:00
|
|
|
|
|
|
|
fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
|
2019-03-19 00:10:28 +08:00
|
|
|
fan->divider = MLXREG_FAN_TACHO_DIV_DEF;
|
2018-07-03 15:00:09 +08:00
|
|
|
for (i = 0; i < pdata->counter; i++, data++) {
|
|
|
|
if (strnstr(data->label, "tacho", sizeof(data->label))) {
|
|
|
|
if (tacho_num == MLXREG_FAN_MAX_TACHO) {
|
|
|
|
dev_err(fan->dev, "too many tacho entries: %s\n",
|
|
|
|
data->label);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-03-19 00:10:28 +08:00
|
|
|
|
|
|
|
if (data->capability) {
|
|
|
|
err = mlxreg_fan_connect_verify(fan, data);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
else if (!err) {
|
|
|
|
tacho_num++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:00:09 +08:00
|
|
|
fan->tacho[tacho_num].reg = data->reg;
|
|
|
|
fan->tacho[tacho_num].mask = data->mask;
|
2021-03-23 01:22:37 +08:00
|
|
|
fan->tacho[tacho_num].prsnt = data->reg_prsnt;
|
2018-07-03 15:00:09 +08:00
|
|
|
fan->tacho[tacho_num++].connected = true;
|
2021-03-23 01:22:37 +08:00
|
|
|
tacho_avail++;
|
2018-07-03 15:00:09 +08:00
|
|
|
} else if (strnstr(data->label, "pwm", sizeof(data->label))) {
|
2021-09-17 03:47:18 +08:00
|
|
|
if (pwm_num == MLXREG_FAN_MAX_TACHO) {
|
|
|
|
dev_err(fan->dev, "too many pwm entries: %s\n",
|
2018-07-03 15:00:09 +08:00
|
|
|
data->label);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-09-17 03:47:18 +08:00
|
|
|
|
2021-09-26 13:35:40 +08:00
|
|
|
/* Validate if more then one PWM is connected. */
|
|
|
|
if (pwm_num) {
|
|
|
|
err = mlxreg_pwm_connect_verify(fan, data);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
else if (!err)
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-17 03:47:18 +08:00
|
|
|
|
|
|
|
fan->pwm[pwm_num].reg = data->reg;
|
|
|
|
fan->pwm[pwm_num].connected = true;
|
|
|
|
pwm_num++;
|
2018-07-03 15:00:09 +08:00
|
|
|
} else if (strnstr(data->label, "conf", sizeof(data->label))) {
|
|
|
|
if (configured) {
|
|
|
|
dev_err(fan->dev, "duplicate conf entry: %s\n",
|
|
|
|
data->label);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
/* Validate that conf parameters are not zeros. */
|
2019-03-19 00:10:28 +08:00
|
|
|
if (!data->mask && !data->bit && !data->capability) {
|
2018-07-03 15:00:09 +08:00
|
|
|
dev_err(fan->dev, "invalid conf entry params: %s\n",
|
|
|
|
data->label);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-03-19 00:10:28 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-07-03 15:00:09 +08:00
|
|
|
configured = true;
|
|
|
|
} else {
|
|
|
|
dev_err(fan->dev, "invalid label: %s\n", data->label);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 01:22:37 +08:00
|
|
|
if (pdata->capability) {
|
|
|
|
int drwr_avail;
|
|
|
|
u32 regval;
|
|
|
|
|
|
|
|
/* Obtain the number of FAN drawers, supported by system. */
|
|
|
|
err = regmap_read(fan->regmap, pdata->capability, ®val);
|
|
|
|
if (err) {
|
|
|
|
dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
|
|
|
|
pdata->capability);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
drwr_avail = hweight32(regval);
|
|
|
|
if (!tacho_avail || !drwr_avail || tacho_avail < drwr_avail) {
|
|
|
|
dev_err(fan->dev, "Configuration is invalid: drawers num %d tachos num %d\n",
|
|
|
|
drwr_avail, tacho_avail);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the number of tachometers per one drawer. */
|
|
|
|
fan->tachos_per_drwr = tacho_avail / drwr_avail;
|
|
|
|
}
|
|
|
|
|
2021-09-17 05:31:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mlxreg_fan_cooling_config(struct device *dev, struct mlxreg_fan *fan)
|
|
|
|
{
|
2022-01-26 22:18:25 +08:00
|
|
|
int i;
|
2021-09-17 05:31:28 +08:00
|
|
|
|
2021-09-21 02:09:21 +08:00
|
|
|
for (i = 0; i < MLXREG_FAN_MAX_PWM; i++) {
|
2021-09-17 05:31:28 +08:00
|
|
|
struct mlxreg_fan_pwm *pwm = &fan->pwm[i];
|
|
|
|
|
|
|
|
if (!pwm->connected)
|
|
|
|
continue;
|
|
|
|
pwm->fan = fan;
|
2021-09-26 13:35:41 +08:00
|
|
|
pwm->cdev = devm_thermal_of_cooling_device_register(dev, NULL, mlxreg_fan_name[i],
|
|
|
|
pwm, &mlxreg_fan_cooling_ops);
|
2021-09-17 05:31:28 +08:00
|
|
|
if (IS_ERR(pwm->cdev)) {
|
|
|
|
dev_err(dev, "Failed to register cooling device\n");
|
|
|
|
return PTR_ERR(pwm->cdev);
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:18:25 +08:00
|
|
|
/* Set minimal PWM speed. */
|
|
|
|
pwm->last_hwmon_state = MLXREG_FAN_PWM_DUTY2STATE(MLXREG_FAN_MIN_DUTY);
|
2021-09-17 05:31:28 +08:00
|
|
|
}
|
2018-07-03 15:00:09 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mlxreg_fan_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct mlxreg_core_platform_data *pdata;
|
2019-04-19 03:58:18 +08:00
|
|
|
struct device *dev = &pdev->dev;
|
2018-07-03 15:00:09 +08:00
|
|
|
struct mlxreg_fan *fan;
|
|
|
|
struct device *hwm;
|
|
|
|
int err;
|
|
|
|
|
2019-04-19 03:58:18 +08:00
|
|
|
pdata = dev_get_platdata(dev);
|
2018-07-03 15:00:09 +08:00
|
|
|
if (!pdata) {
|
2019-04-19 03:58:18 +08:00
|
|
|
dev_err(dev, "Failed to get platform data.\n");
|
2018-07-03 15:00:09 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-04-19 03:58:18 +08:00
|
|
|
fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
|
2018-07-03 15:00:09 +08:00
|
|
|
if (!fan)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-04-19 03:58:18 +08:00
|
|
|
fan->dev = dev;
|
2018-07-03 15:00:09 +08:00
|
|
|
fan->regmap = pdata->regmap;
|
|
|
|
|
|
|
|
err = mlxreg_fan_config(fan, pdata);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2019-04-19 03:58:18 +08:00
|
|
|
hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
|
2018-07-03 15:00:09 +08:00
|
|
|
fan,
|
|
|
|
&mlxreg_fan_hwmon_chip_info,
|
|
|
|
NULL);
|
|
|
|
if (IS_ERR(hwm)) {
|
2019-04-19 03:58:18 +08:00
|
|
|
dev_err(dev, "Failed to register hwmon device\n");
|
2018-07-03 15:00:09 +08:00
|
|
|
return PTR_ERR(hwm);
|
|
|
|
}
|
|
|
|
|
2021-09-17 05:31:28 +08:00
|
|
|
if (IS_REACHABLE(CONFIG_THERMAL))
|
|
|
|
err = mlxreg_fan_cooling_config(dev, fan);
|
2018-07-03 15:00:09 +08:00
|
|
|
|
2021-09-17 05:31:28 +08:00
|
|
|
return err;
|
2018-07-03 15:00:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver mlxreg_fan_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "mlxreg-fan",
|
|
|
|
},
|
|
|
|
.probe = mlxreg_fan_probe,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(mlxreg_fan_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
|
|
|
|
MODULE_DESCRIPTION("Mellanox FAN driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("platform:mlxreg-fan");
|