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 24
16 #define MLXREG_FAN_MAX_PWM 4
17 #define MLXREG_FAN_PWM_NOT_CONNECTED 0xff
18 #define MLXREG_FAN_MAX_STATE 10
19 #define MLXREG_FAN_MIN_DUTY 51 /* 20% */
20 #define MLXREG_FAN_MAX_DUTY 255 /* 100% */
21 #define MLXREG_FAN_SPEED_MIN_LEVEL 2 /* 20 percent */
22 #define MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF 44
23 #define MLXREG_FAN_TACHO_DIV_MIN 283
24 #define MLXREG_FAN_TACHO_DIV_DEF (MLXREG_FAN_TACHO_DIV_MIN * 4)
25 #define MLXREG_FAN_TACHO_DIV_SCALE_MAX 64
27 * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-high.
28 * The logic in a programmable device measures the time t-high by sampling the
29 * tachometer every t-sample (with the default value 11.32 uS) and increment
30 * a counter (N) as long as the pulse has not change:
31 * RPM = 15 / (t-sample * (K + Regval)), where:
32 * Regval: is the value read from the programmable device register;
33 * - 0xff - represents tachometer fault;
34 * - 0xfe - represents tachometer minimum value , which is 4444 RPM;
35 * - 0x00 - represents tachometer maximum value , which is 300000 RPM;
36 * K: is 44 and it represents the minimum allowed samples per pulse;
37 * N: is equal K + Regval;
38 * In order to calculate RPM from the register value the following formula is
39 * used: RPM = 15 / ((Regval + K) * 11.32) * 10^(-6)), which in the
40 * default case is modified to:
41 * RPM = 15000000 * 100 / ((Regval + 44) * 1132);
42 * - for Regval 0x00, RPM will be 15000000 * 100 / (44 * 1132) = 30115;
43 * - for Regval 0xfe, RPM will be 15000000 * 100 / ((254 + 44) * 1132) = 4446;
44 * In common case the formula is modified to:
45 * RPM = 15000000 * 100 / ((Regval + samples) * divider).
47 #define MLXREG_FAN_GET_RPM(rval, d, s) (DIV_ROUND_CLOSEST(15000000 * 100, \
48 ((rval) + (s)) * (d)))
49 #define MLXREG_FAN_GET_FAULT(val, mask) ((val) == (mask))
50 #define MLXREG_FAN_PWM_DUTY2STATE(duty) (DIV_ROUND_CLOSEST((duty) * \
51 MLXREG_FAN_MAX_STATE, \
53 #define MLXREG_FAN_PWM_STATE2DUTY(stat) (DIV_ROUND_CLOSEST((stat) * \
54 MLXREG_FAN_MAX_DUTY, \
55 MLXREG_FAN_MAX_STATE))
60 * struct mlxreg_fan_tacho - tachometer data (internal use):
62 * @connected: indicates if tachometer is connected;
63 * @reg: register offset;
65 * @prsnt: present register offset;
67 struct mlxreg_fan_tacho
{
75 * struct mlxreg_fan_pwm - PWM data (internal use):
78 * @connected: indicates if PWM is connected;
79 * @reg: register offset;
80 * @cooling: cooling device levels;
81 * @last_hwmon_state: last cooling state set by hwmon subsystem;
82 * @last_thermal_state: last cooling state set by thermal subsystem;
83 * @cdev: cooling device;
85 struct mlxreg_fan_pwm
{
86 struct mlxreg_fan
*fan
;
89 unsigned long last_hwmon_state
;
90 unsigned long last_thermal_state
;
91 struct thermal_cooling_device
*cdev
;
95 * struct mlxreg_fan - private data (internal use):
98 * @regmap: register map of parent device;
99 * @tacho: tachometer data;
101 * @tachos_per_drwr - number of tachometers per drawer;
102 * @samples: minimum allowed samples per pulse;
103 * @divider: divider value for tachometer RPM calculation;
108 struct mlxreg_core_platform_data
*pdata
;
109 struct mlxreg_fan_tacho tacho
[MLXREG_FAN_MAX_TACHO
];
110 struct mlxreg_fan_pwm pwm
[MLXREG_FAN_MAX_PWM
];
116 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device
*cdev
,
117 unsigned long state
);
120 mlxreg_fan_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
121 int channel
, long *val
)
123 struct mlxreg_fan
*fan
= dev_get_drvdata(dev
);
124 struct mlxreg_fan_tacho
*tacho
;
125 struct mlxreg_fan_pwm
*pwm
;
131 tacho
= &fan
->tacho
[channel
];
133 case hwmon_fan_input
:
135 * Check FAN presence: FAN related bit in presence register is one,
136 * if FAN is physically connected, zero - otherwise.
138 if (tacho
->prsnt
&& fan
->tachos_per_drwr
) {
139 err
= regmap_read(fan
->regmap
, tacho
->prsnt
, ®val
);
144 * Map channel to presence bit - drawer can be equipped with
145 * one or few FANs, while presence is indicated per drawer.
147 if (BIT(channel
/ fan
->tachos_per_drwr
) & regval
) {
148 /* FAN is not connected - return zero for FAN speed. */
154 err
= regmap_read(fan
->regmap
, tacho
->reg
, ®val
);
158 if (MLXREG_FAN_GET_FAULT(regval
, tacho
->mask
)) {
159 /* FAN is broken - return zero for FAN speed. */
164 *val
= MLXREG_FAN_GET_RPM(regval
, fan
->divider
,
168 case hwmon_fan_fault
:
169 err
= regmap_read(fan
->regmap
, tacho
->reg
, ®val
);
173 *val
= MLXREG_FAN_GET_FAULT(regval
, tacho
->mask
);
182 pwm
= &fan
->pwm
[channel
];
184 case hwmon_pwm_input
:
185 err
= regmap_read(fan
->regmap
, pwm
->reg
, ®val
);
205 mlxreg_fan_write(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
206 int channel
, long val
)
208 struct mlxreg_fan
*fan
= dev_get_drvdata(dev
);
209 struct mlxreg_fan_pwm
*pwm
;
214 case hwmon_pwm_input
:
215 if (val
< MLXREG_FAN_MIN_DUTY
||
216 val
> MLXREG_FAN_MAX_DUTY
)
218 pwm
= &fan
->pwm
[channel
];
219 /* If thermal is configured - handle PWM limit setting. */
220 if (IS_REACHABLE(CONFIG_THERMAL
)) {
221 pwm
->last_hwmon_state
= MLXREG_FAN_PWM_DUTY2STATE(val
);
223 * Update PWM only in case requested state is not less than the
224 * last thermal state.
226 if (pwm
->last_hwmon_state
>= pwm
->last_thermal_state
)
227 return mlxreg_fan_set_cur_state(pwm
->cdev
,
228 pwm
->last_hwmon_state
);
231 return regmap_write(fan
->regmap
, pwm
->reg
, val
);
245 mlxreg_fan_is_visible(const void *data
, enum hwmon_sensor_types type
, u32 attr
,
250 if (!(((struct mlxreg_fan
*)data
)->tacho
[channel
].connected
))
254 case hwmon_fan_input
:
255 case hwmon_fan_fault
:
263 if (!(((struct mlxreg_fan
*)data
)->pwm
[channel
].connected
))
267 case hwmon_pwm_input
:
281 static char *mlxreg_fan_name
[] = {
288 static const struct hwmon_channel_info
* const mlxreg_fan_hwmon_info
[] = {
289 HWMON_CHANNEL_INFO(fan
,
290 HWMON_F_INPUT
| HWMON_F_FAULT
,
291 HWMON_F_INPUT
| HWMON_F_FAULT
,
292 HWMON_F_INPUT
| HWMON_F_FAULT
,
293 HWMON_F_INPUT
| HWMON_F_FAULT
,
294 HWMON_F_INPUT
| HWMON_F_FAULT
,
295 HWMON_F_INPUT
| HWMON_F_FAULT
,
296 HWMON_F_INPUT
| HWMON_F_FAULT
,
297 HWMON_F_INPUT
| HWMON_F_FAULT
,
298 HWMON_F_INPUT
| HWMON_F_FAULT
,
299 HWMON_F_INPUT
| HWMON_F_FAULT
,
300 HWMON_F_INPUT
| HWMON_F_FAULT
,
301 HWMON_F_INPUT
| HWMON_F_FAULT
,
302 HWMON_F_INPUT
| HWMON_F_FAULT
,
303 HWMON_F_INPUT
| HWMON_F_FAULT
,
304 HWMON_F_INPUT
| HWMON_F_FAULT
,
305 HWMON_F_INPUT
| HWMON_F_FAULT
,
306 HWMON_F_INPUT
| HWMON_F_FAULT
,
307 HWMON_F_INPUT
| HWMON_F_FAULT
,
308 HWMON_F_INPUT
| HWMON_F_FAULT
,
309 HWMON_F_INPUT
| HWMON_F_FAULT
,
310 HWMON_F_INPUT
| HWMON_F_FAULT
,
311 HWMON_F_INPUT
| HWMON_F_FAULT
,
312 HWMON_F_INPUT
| HWMON_F_FAULT
,
313 HWMON_F_INPUT
| HWMON_F_FAULT
),
314 HWMON_CHANNEL_INFO(pwm
,
322 static const struct hwmon_ops mlxreg_fan_hwmon_hwmon_ops
= {
323 .is_visible
= mlxreg_fan_is_visible
,
324 .read
= mlxreg_fan_read
,
325 .write
= mlxreg_fan_write
,
328 static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info
= {
329 .ops
= &mlxreg_fan_hwmon_hwmon_ops
,
330 .info
= mlxreg_fan_hwmon_info
,
333 static int mlxreg_fan_get_max_state(struct thermal_cooling_device
*cdev
,
334 unsigned long *state
)
336 *state
= MLXREG_FAN_MAX_STATE
;
340 static int mlxreg_fan_get_cur_state(struct thermal_cooling_device
*cdev
,
341 unsigned long *state
)
344 struct mlxreg_fan_pwm
*pwm
= cdev
->devdata
;
345 struct mlxreg_fan
*fan
= pwm
->fan
;
349 err
= regmap_read(fan
->regmap
, pwm
->reg
, ®val
);
351 dev_err(fan
->dev
, "Failed to query PWM duty\n");
355 *state
= MLXREG_FAN_PWM_DUTY2STATE(regval
);
360 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device
*cdev
,
364 struct mlxreg_fan_pwm
*pwm
= cdev
->devdata
;
365 struct mlxreg_fan
*fan
= pwm
->fan
;
368 if (state
> MLXREG_FAN_MAX_STATE
)
371 /* Save thermal state. */
372 pwm
->last_thermal_state
= state
;
374 state
= max_t(unsigned long, state
, pwm
->last_hwmon_state
);
375 err
= regmap_write(fan
->regmap
, pwm
->reg
,
376 MLXREG_FAN_PWM_STATE2DUTY(state
));
378 dev_err(fan
->dev
, "Failed to write PWM duty\n");
384 static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops
= {
385 .get_max_state
= mlxreg_fan_get_max_state
,
386 .get_cur_state
= mlxreg_fan_get_cur_state
,
387 .set_cur_state
= mlxreg_fan_set_cur_state
,
390 static int mlxreg_fan_connect_verify(struct mlxreg_fan
*fan
,
391 struct mlxreg_core_data
*data
)
396 err
= regmap_read(fan
->regmap
, data
->capability
, ®val
);
398 dev_err(fan
->dev
, "Failed to query capability register 0x%08x\n",
403 return !!(regval
& data
->bit
);
406 static int mlxreg_pwm_connect_verify(struct mlxreg_fan
*fan
,
407 struct mlxreg_core_data
*data
)
412 err
= regmap_read(fan
->regmap
, data
->reg
, ®val
);
414 dev_err(fan
->dev
, "Failed to query pwm register 0x%08x\n",
419 return regval
!= MLXREG_FAN_PWM_NOT_CONNECTED
;
422 static int mlxreg_fan_speed_divider_get(struct mlxreg_fan
*fan
,
423 struct mlxreg_core_data
*data
)
428 err
= regmap_read(fan
->regmap
, data
->capability
, ®val
);
430 dev_err(fan
->dev
, "Failed to query capability register 0x%08x\n",
436 * Set divider value according to the capability register, in case it
437 * contains valid value. Otherwise use default value. The purpose of
438 * this validation is to protect against the old hardware, in which
439 * this register can return zero.
441 if (regval
> 0 && regval
<= MLXREG_FAN_TACHO_DIV_SCALE_MAX
)
442 fan
->divider
= regval
* MLXREG_FAN_TACHO_DIV_MIN
;
447 static int mlxreg_fan_config(struct mlxreg_fan
*fan
,
448 struct mlxreg_core_platform_data
*pdata
)
450 int tacho_num
= 0, tacho_avail
= 0, pwm_num
= 0, i
;
451 struct mlxreg_core_data
*data
= pdata
->data
;
452 bool configured
= false;
455 fan
->samples
= MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF
;
456 fan
->divider
= MLXREG_FAN_TACHO_DIV_DEF
;
457 for (i
= 0; i
< pdata
->counter
; i
++, data
++) {
458 if (strnstr(data
->label
, "tacho", sizeof(data
->label
))) {
459 if (tacho_num
== MLXREG_FAN_MAX_TACHO
) {
460 dev_err(fan
->dev
, "too many tacho entries: %s\n",
465 if (data
->capability
) {
466 err
= mlxreg_fan_connect_verify(fan
, data
);
475 fan
->tacho
[tacho_num
].reg
= data
->reg
;
476 fan
->tacho
[tacho_num
].mask
= data
->mask
;
477 fan
->tacho
[tacho_num
].prsnt
= data
->reg_prsnt
;
478 fan
->tacho
[tacho_num
++].connected
= true;
480 } else if (strnstr(data
->label
, "pwm", sizeof(data
->label
))) {
481 if (pwm_num
== MLXREG_FAN_MAX_TACHO
) {
482 dev_err(fan
->dev
, "too many pwm entries: %s\n",
487 /* Validate if more then one PWM is connected. */
489 err
= mlxreg_pwm_connect_verify(fan
, data
);
496 fan
->pwm
[pwm_num
].reg
= data
->reg
;
497 fan
->pwm
[pwm_num
].connected
= true;
499 } else if (strnstr(data
->label
, "conf", sizeof(data
->label
))) {
501 dev_err(fan
->dev
, "duplicate conf entry: %s\n",
505 /* Validate that conf parameters are not zeros. */
506 if (!data
->mask
&& !data
->bit
&& !data
->capability
) {
507 dev_err(fan
->dev
, "invalid conf entry params: %s\n",
511 if (data
->capability
) {
512 err
= mlxreg_fan_speed_divider_get(fan
, data
);
517 fan
->samples
= data
->mask
;
519 fan
->divider
= data
->bit
;
523 dev_err(fan
->dev
, "invalid label: %s\n", data
->label
);
528 if (pdata
->capability
) {
532 /* Obtain the number of FAN drawers, supported by system. */
533 err
= regmap_read(fan
->regmap
, pdata
->capability
, ®val
);
535 dev_err(fan
->dev
, "Failed to query capability register 0x%08x\n",
540 drwr_avail
= hweight32(regval
);
541 if (!tacho_avail
|| !drwr_avail
|| tacho_avail
< drwr_avail
) {
542 dev_err(fan
->dev
, "Configuration is invalid: drawers num %d tachos num %d\n",
543 drwr_avail
, tacho_avail
);
547 /* Set the number of tachometers per one drawer. */
548 fan
->tachos_per_drwr
= tacho_avail
/ drwr_avail
;
554 static int mlxreg_fan_cooling_config(struct device
*dev
, struct mlxreg_fan
*fan
)
558 for (i
= 0; i
< MLXREG_FAN_MAX_PWM
; i
++) {
559 struct mlxreg_fan_pwm
*pwm
= &fan
->pwm
[i
];
564 pwm
->cdev
= devm_thermal_of_cooling_device_register(dev
, NULL
, mlxreg_fan_name
[i
],
565 pwm
, &mlxreg_fan_cooling_ops
);
566 if (IS_ERR(pwm
->cdev
)) {
567 dev_err(dev
, "Failed to register cooling device\n");
568 return PTR_ERR(pwm
->cdev
);
571 /* Set minimal PWM speed. */
572 pwm
->last_hwmon_state
= MLXREG_FAN_PWM_DUTY2STATE(MLXREG_FAN_MIN_DUTY
);
578 static int mlxreg_fan_probe(struct platform_device
*pdev
)
580 struct mlxreg_core_platform_data
*pdata
;
581 struct device
*dev
= &pdev
->dev
;
582 struct mlxreg_fan
*fan
;
586 pdata
= dev_get_platdata(dev
);
588 dev_err(dev
, "Failed to get platform data.\n");
592 fan
= devm_kzalloc(dev
, sizeof(*fan
), GFP_KERNEL
);
597 fan
->regmap
= pdata
->regmap
;
599 err
= mlxreg_fan_config(fan
, pdata
);
603 hwm
= devm_hwmon_device_register_with_info(dev
, "mlxreg_fan",
605 &mlxreg_fan_hwmon_chip_info
,
608 dev_err(dev
, "Failed to register hwmon device\n");
612 if (IS_REACHABLE(CONFIG_THERMAL
))
613 err
= mlxreg_fan_cooling_config(dev
, fan
);
618 static struct platform_driver mlxreg_fan_driver
= {
620 .name
= "mlxreg-fan",
622 .probe
= mlxreg_fan_probe
,
625 module_platform_driver(mlxreg_fan_driver
);
627 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
628 MODULE_DESCRIPTION("Mellanox FAN driver");
629 MODULE_LICENSE("GPL");
630 MODULE_ALIAS("platform:mlxreg-fan");