1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 // Copyright (c) 2018 Mellanox Technologies. All rights reserved.
4 // Copyright (c) 2018 Vadim Pasternak <vadimp@mellanox.com>
6 #include <linux/bitops.h>
7 #include <linux/device.h>
8 #include <linux/hwmon.h>
9 #include <linux/module.h>
10 #include <linux/platform_data/mlxreg.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/thermal.h>
15 #define MLXREG_FAN_MAX_TACHO 12
16 #define MLXREG_FAN_MAX_STATE 10
17 #define MLXREG_FAN_MIN_DUTY 51 /* 20% */
18 #define MLXREG_FAN_MAX_DUTY 255 /* 100% */
20 * Minimum and maximum FAN allowed speed in percent: from 20% to 100%. Values
21 * MLXREG_FAN_MAX_STATE + x, where x is between 2 and 10 are used for
22 * setting FAN speed dynamic minimum. For example, if value is set to 14 (40%)
23 * cooling levels vector will be set to 4, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10 to
24 * introduce PWM speed in percent: 40, 40, 40, 40, 40, 50, 60. 70, 80, 90, 100.
26 #define MLXREG_FAN_SPEED_MIN (MLXREG_FAN_MAX_STATE + 2)
27 #define MLXREG_FAN_SPEED_MAX (MLXREG_FAN_MAX_STATE * 2)
28 #define MLXREG_FAN_SPEED_MIN_LEVEL 2 /* 20 percent */
29 #define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF 44
30 #define MLXREG_FAN_TACHO_DIV_MIN 283
31 #define MLXREG_FAN_TACHO_DIV_DEF (MLXREG_FAN_TACHO_DIV_MIN * 4)
32 #define MLXREG_FAN_TACHO_DIV_SCALE_MAX 64
34 * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-high.
35 * The logic in a programmable device measures the time t-high by sampling the
36 * tachometer every t-sample (with the default value 11.32 uS) and increment
37 * a counter (N) as long as the pulse has not change:
38 * RPM = 15 / (t-sample * (K + Regval)), where:
39 * Regval: is the value read from the programmable device register;
40 * - 0xff - represents tachometer fault;
41 * - 0xfe - represents tachometer minimum value , which is 4444 RPM;
42 * - 0x00 - represents tachometer maximum value , which is 300000 RPM;
43 * K: is 44 and it represents the minimum allowed samples per pulse;
44 * N: is equal K + Regval;
45 * In order to calculate RPM from the register value the following formula is
46 * used: RPM = 15 / ((Regval + K) * 11.32) * 10^(-6)), which in the
47 * default case is modified to:
48 * RPM = 15000000 * 100 / ((Regval + 44) * 1132);
49 * - for Regval 0x00, RPM will be 15000000 * 100 / (44 * 1132) = 30115;
50 * - for Regval 0xfe, RPM will be 15000000 * 100 / ((254 + 44) * 1132) = 4446;
51 * In common case the formula is modified to:
52 * RPM = 15000000 * 100 / ((Regval + samples) * divider).
54 #define MLXREG_FAN_GET_RPM(rval, d, s) (DIV_ROUND_CLOSEST(15000000 * 100, \
55 ((rval) + (s)) * (d)))
56 #define MLXREG_FAN_GET_FAULT(val, mask) ((val) == (mask))
57 #define MLXREG_FAN_PWM_DUTY2STATE(duty) (DIV_ROUND_CLOSEST((duty) * \
58 MLXREG_FAN_MAX_STATE, \
60 #define MLXREG_FAN_PWM_STATE2DUTY(stat) (DIV_ROUND_CLOSEST((stat) * \
61 MLXREG_FAN_MAX_DUTY, \
62 MLXREG_FAN_MAX_STATE))
65 * struct mlxreg_fan_tacho - tachometer data (internal use):
67 * @connected: indicates if tachometer is connected;
68 * @reg: register offset;
71 struct mlxreg_fan_tacho
{
78 * struct mlxreg_fan_pwm - PWM data (internal use):
80 * @connected: indicates if PWM is connected;
81 * @reg: register offset;
83 struct mlxreg_fan_pwm
{
89 * struct mlxreg_fan - private data (internal use):
92 * @regmap: register map of parent device;
93 * @tacho: tachometer data;
95 * @samples: minimum allowed samples per pulse;
96 * @divider: divider value for tachometer RPM calculation;
97 * @cooling: cooling device levels;
98 * @cdev: cooling device;
103 struct mlxreg_core_platform_data
*pdata
;
104 struct mlxreg_fan_tacho tacho
[MLXREG_FAN_MAX_TACHO
];
105 struct mlxreg_fan_pwm pwm
;
108 u8 cooling_levels
[MLXREG_FAN_MAX_STATE
+ 1];
109 struct thermal_cooling_device
*cdev
;
113 mlxreg_fan_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
114 int channel
, long *val
)
116 struct mlxreg_fan
*fan
= dev_get_drvdata(dev
);
117 struct mlxreg_fan_tacho
*tacho
;
123 tacho
= &fan
->tacho
[channel
];
125 case hwmon_fan_input
:
126 err
= regmap_read(fan
->regmap
, tacho
->reg
, ®val
);
130 *val
= MLXREG_FAN_GET_RPM(regval
, fan
->divider
,
134 case hwmon_fan_fault
:
135 err
= regmap_read(fan
->regmap
, tacho
->reg
, ®val
);
139 *val
= MLXREG_FAN_GET_FAULT(regval
, tacho
->mask
);
149 case hwmon_pwm_input
:
150 err
= regmap_read(fan
->regmap
, fan
->pwm
.reg
, ®val
);
170 mlxreg_fan_write(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
171 int channel
, long val
)
173 struct mlxreg_fan
*fan
= dev_get_drvdata(dev
);
178 case hwmon_pwm_input
:
179 if (val
< MLXREG_FAN_MIN_DUTY
||
180 val
> MLXREG_FAN_MAX_DUTY
)
182 return regmap_write(fan
->regmap
, fan
->pwm
.reg
, val
);
196 mlxreg_fan_is_visible(const void *data
, enum hwmon_sensor_types type
, u32 attr
,
201 if (!(((struct mlxreg_fan
*)data
)->tacho
[channel
].connected
))
205 case hwmon_fan_input
:
206 case hwmon_fan_fault
:
214 if (!(((struct mlxreg_fan
*)data
)->pwm
.connected
))
218 case hwmon_pwm_input
:
232 static const struct hwmon_channel_info
*mlxreg_fan_hwmon_info
[] = {
233 HWMON_CHANNEL_INFO(fan
,
234 HWMON_F_INPUT
| HWMON_F_FAULT
,
235 HWMON_F_INPUT
| HWMON_F_FAULT
,
236 HWMON_F_INPUT
| HWMON_F_FAULT
,
237 HWMON_F_INPUT
| HWMON_F_FAULT
,
238 HWMON_F_INPUT
| HWMON_F_FAULT
,
239 HWMON_F_INPUT
| HWMON_F_FAULT
,
240 HWMON_F_INPUT
| HWMON_F_FAULT
,
241 HWMON_F_INPUT
| HWMON_F_FAULT
,
242 HWMON_F_INPUT
| HWMON_F_FAULT
,
243 HWMON_F_INPUT
| HWMON_F_FAULT
,
244 HWMON_F_INPUT
| HWMON_F_FAULT
,
245 HWMON_F_INPUT
| HWMON_F_FAULT
),
246 HWMON_CHANNEL_INFO(pwm
,
251 static const struct hwmon_ops mlxreg_fan_hwmon_hwmon_ops
= {
252 .is_visible
= mlxreg_fan_is_visible
,
253 .read
= mlxreg_fan_read
,
254 .write
= mlxreg_fan_write
,
257 static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info
= {
258 .ops
= &mlxreg_fan_hwmon_hwmon_ops
,
259 .info
= mlxreg_fan_hwmon_info
,
262 static int mlxreg_fan_get_max_state(struct thermal_cooling_device
*cdev
,
263 unsigned long *state
)
265 *state
= MLXREG_FAN_MAX_STATE
;
269 static int mlxreg_fan_get_cur_state(struct thermal_cooling_device
*cdev
,
270 unsigned long *state
)
273 struct mlxreg_fan
*fan
= cdev
->devdata
;
277 err
= regmap_read(fan
->regmap
, fan
->pwm
.reg
, ®val
);
279 dev_err(fan
->dev
, "Failed to query PWM duty\n");
283 *state
= MLXREG_FAN_PWM_DUTY2STATE(regval
);
288 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device
*cdev
,
292 struct mlxreg_fan
*fan
= cdev
->devdata
;
293 unsigned long cur_state
;
299 * Verify if this request is for changing allowed FAN dynamical
300 * minimum. If it is - update cooling levels accordingly and update
301 * state, if current state is below the newly requested minimum state.
302 * For example, if current state is 5, and minimal state is to be
303 * changed from 4 to 6, fan->cooling_levels[0 to 5] will be changed all
304 * from 4 to 6. And state 5 (fan->cooling_levels[4]) should be
307 if (state
>= MLXREG_FAN_SPEED_MIN
&& state
<= MLXREG_FAN_SPEED_MAX
) {
308 state
-= MLXREG_FAN_MAX_STATE
;
309 for (i
= 0; i
< state
; i
++)
310 fan
->cooling_levels
[i
] = state
;
311 for (i
= state
; i
<= MLXREG_FAN_MAX_STATE
; i
++)
312 fan
->cooling_levels
[i
] = i
;
314 err
= regmap_read(fan
->regmap
, fan
->pwm
.reg
, ®val
);
316 dev_err(fan
->dev
, "Failed to query PWM duty\n");
320 cur_state
= MLXREG_FAN_PWM_DUTY2STATE(regval
);
321 if (state
< cur_state
)
327 if (state
> MLXREG_FAN_MAX_STATE
)
330 /* Normalize the state to the valid speed range. */
331 state
= fan
->cooling_levels
[state
];
332 err
= regmap_write(fan
->regmap
, fan
->pwm
.reg
,
333 MLXREG_FAN_PWM_STATE2DUTY(state
));
335 dev_err(fan
->dev
, "Failed to write PWM duty\n");
341 static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops
= {
342 .get_max_state
= mlxreg_fan_get_max_state
,
343 .get_cur_state
= mlxreg_fan_get_cur_state
,
344 .set_cur_state
= mlxreg_fan_set_cur_state
,
347 static int mlxreg_fan_connect_verify(struct mlxreg_fan
*fan
,
348 struct mlxreg_core_data
*data
)
353 err
= regmap_read(fan
->regmap
, data
->capability
, ®val
);
355 dev_err(fan
->dev
, "Failed to query capability register 0x%08x\n",
360 return !!(regval
& data
->bit
);
363 static int mlxreg_fan_speed_divider_get(struct mlxreg_fan
*fan
,
364 struct mlxreg_core_data
*data
)
369 err
= regmap_read(fan
->regmap
, data
->capability
, ®val
);
371 dev_err(fan
->dev
, "Failed to query capability register 0x%08x\n",
377 * Set divider value according to the capability register, in case it
378 * contains valid value. Otherwise use default value. The purpose of
379 * this validation is to protect against the old hardware, in which
380 * this register can return zero.
382 if (regval
> 0 && regval
<= MLXREG_FAN_TACHO_DIV_SCALE_MAX
)
383 fan
->divider
= regval
* MLXREG_FAN_TACHO_DIV_MIN
;
388 static int mlxreg_fan_config(struct mlxreg_fan
*fan
,
389 struct mlxreg_core_platform_data
*pdata
)
391 struct mlxreg_core_data
*data
= pdata
->data
;
392 bool configured
= false;
393 int tacho_num
= 0, i
;
396 fan
->samples
= MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF
;
397 fan
->divider
= MLXREG_FAN_TACHO_DIV_DEF
;
398 for (i
= 0; i
< pdata
->counter
; i
++, data
++) {
399 if (strnstr(data
->label
, "tacho", sizeof(data
->label
))) {
400 if (tacho_num
== MLXREG_FAN_MAX_TACHO
) {
401 dev_err(fan
->dev
, "too many tacho entries: %s\n",
406 if (data
->capability
) {
407 err
= mlxreg_fan_connect_verify(fan
, data
);
416 fan
->tacho
[tacho_num
].reg
= data
->reg
;
417 fan
->tacho
[tacho_num
].mask
= data
->mask
;
418 fan
->tacho
[tacho_num
++].connected
= true;
419 } else if (strnstr(data
->label
, "pwm", sizeof(data
->label
))) {
420 if (fan
->pwm
.connected
) {
421 dev_err(fan
->dev
, "duplicate pwm entry: %s\n",
425 fan
->pwm
.reg
= data
->reg
;
426 fan
->pwm
.connected
= true;
427 } else if (strnstr(data
->label
, "conf", sizeof(data
->label
))) {
429 dev_err(fan
->dev
, "duplicate conf entry: %s\n",
433 /* Validate that conf parameters are not zeros. */
434 if (!data
->mask
&& !data
->bit
&& !data
->capability
) {
435 dev_err(fan
->dev
, "invalid conf entry params: %s\n",
439 if (data
->capability
) {
440 err
= mlxreg_fan_speed_divider_get(fan
, data
);
445 fan
->samples
= data
->mask
;
447 fan
->divider
= data
->bit
;
451 dev_err(fan
->dev
, "invalid label: %s\n", data
->label
);
456 /* Init cooling levels per PWM state. */
457 for (i
= 0; i
< MLXREG_FAN_SPEED_MIN_LEVEL
; i
++)
458 fan
->cooling_levels
[i
] = MLXREG_FAN_SPEED_MIN_LEVEL
;
459 for (i
= MLXREG_FAN_SPEED_MIN_LEVEL
; i
<= MLXREG_FAN_MAX_STATE
; i
++)
460 fan
->cooling_levels
[i
] = i
;
465 static int mlxreg_fan_probe(struct platform_device
*pdev
)
467 struct mlxreg_core_platform_data
*pdata
;
468 struct device
*dev
= &pdev
->dev
;
469 struct mlxreg_fan
*fan
;
473 pdata
= dev_get_platdata(dev
);
475 dev_err(dev
, "Failed to get platform data.\n");
479 fan
= devm_kzalloc(dev
, sizeof(*fan
), GFP_KERNEL
);
484 fan
->regmap
= pdata
->regmap
;
486 err
= mlxreg_fan_config(fan
, pdata
);
490 hwm
= devm_hwmon_device_register_with_info(dev
, "mlxreg_fan",
492 &mlxreg_fan_hwmon_chip_info
,
495 dev_err(dev
, "Failed to register hwmon device\n");
499 if (IS_REACHABLE(CONFIG_THERMAL
)) {
500 fan
->cdev
= devm_thermal_of_cooling_device_register(dev
,
501 NULL
, "mlxreg_fan", fan
, &mlxreg_fan_cooling_ops
);
502 if (IS_ERR(fan
->cdev
)) {
503 dev_err(dev
, "Failed to register cooling device\n");
504 return PTR_ERR(fan
->cdev
);
511 static struct platform_driver mlxreg_fan_driver
= {
513 .name
= "mlxreg-fan",
515 .probe
= mlxreg_fan_probe
,
518 module_platform_driver(mlxreg_fan_driver
);
520 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
521 MODULE_DESCRIPTION("Mellanox FAN driver");
522 MODULE_LICENSE("GPL");
523 MODULE_ALIAS("platform:mlxreg-fan");