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 14
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_CHANNEL_INFO(pwm,
312 static const struct hwmon_ops mlxreg_fan_hwmon_hwmon_ops = {
313 .is_visible = mlxreg_fan_is_visible,
314 .read = mlxreg_fan_read,
315 .write = mlxreg_fan_write,
318 static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info = {
319 .ops = &mlxreg_fan_hwmon_hwmon_ops,
320 .info = mlxreg_fan_hwmon_info,
323 static int mlxreg_fan_get_max_state(struct thermal_cooling_device *cdev,
324 unsigned long *state)
326 *state = MLXREG_FAN_MAX_STATE;
330 static int mlxreg_fan_get_cur_state(struct thermal_cooling_device *cdev,
331 unsigned long *state)
334 struct mlxreg_fan_pwm *pwm = cdev->devdata;
335 struct mlxreg_fan *fan = pwm->fan;
339 err = regmap_read(fan->regmap, pwm->reg, ®val);
341 dev_err(fan->dev, "Failed to query PWM duty\n");
345 *state = MLXREG_FAN_PWM_DUTY2STATE(regval);
350 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
354 struct mlxreg_fan_pwm *pwm = cdev->devdata;
355 struct mlxreg_fan *fan = pwm->fan;
358 if (state > MLXREG_FAN_MAX_STATE)
361 /* Save thermal state. */
362 pwm->last_thermal_state = state;
364 state = max_t(unsigned long, state, pwm->last_hwmon_state);
365 err = regmap_write(fan->regmap, pwm->reg,
366 MLXREG_FAN_PWM_STATE2DUTY(state));
368 dev_err(fan->dev, "Failed to write PWM duty\n");
374 static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
375 .get_max_state = mlxreg_fan_get_max_state,
376 .get_cur_state = mlxreg_fan_get_cur_state,
377 .set_cur_state = mlxreg_fan_set_cur_state,
380 static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
381 struct mlxreg_core_data *data)
386 err = regmap_read(fan->regmap, data->capability, ®val);
388 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
393 return !!(regval & data->bit);
396 static int mlxreg_pwm_connect_verify(struct mlxreg_fan *fan,
397 struct mlxreg_core_data *data)
402 err = regmap_read(fan->regmap, data->reg, ®val);
404 dev_err(fan->dev, "Failed to query pwm register 0x%08x\n",
409 return regval != MLXREG_FAN_PWM_NOT_CONNECTED;
412 static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
413 struct mlxreg_core_data *data)
418 err = regmap_read(fan->regmap, data->capability, ®val);
420 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
426 * Set divider value according to the capability register, in case it
427 * contains valid value. Otherwise use default value. The purpose of
428 * this validation is to protect against the old hardware, in which
429 * this register can return zero.
431 if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIV_SCALE_MAX)
432 fan->divider = regval * MLXREG_FAN_TACHO_DIV_MIN;
437 static int mlxreg_fan_config(struct mlxreg_fan *fan,
438 struct mlxreg_core_platform_data *pdata)
440 int tacho_num = 0, tacho_avail = 0, pwm_num = 0, i;
441 struct mlxreg_core_data *data = pdata->data;
442 bool configured = false;
445 fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
446 fan->divider = MLXREG_FAN_TACHO_DIV_DEF;
447 for (i = 0; i < pdata->counter; i++, data++) {
448 if (strnstr(data->label, "tacho", sizeof(data->label))) {
449 if (tacho_num == MLXREG_FAN_MAX_TACHO) {
450 dev_err(fan->dev, "too many tacho entries: %s\n",
455 if (data->capability) {
456 err = mlxreg_fan_connect_verify(fan, data);
465 fan->tacho[tacho_num].reg = data->reg;
466 fan->tacho[tacho_num].mask = data->mask;
467 fan->tacho[tacho_num].prsnt = data->reg_prsnt;
468 fan->tacho[tacho_num++].connected = true;
470 } else if (strnstr(data->label, "pwm", sizeof(data->label))) {
471 if (pwm_num == MLXREG_FAN_MAX_TACHO) {
472 dev_err(fan->dev, "too many pwm entries: %s\n",
477 /* Validate if more then one PWM is connected. */
479 err = mlxreg_pwm_connect_verify(fan, data);
486 fan->pwm[pwm_num].reg = data->reg;
487 fan->pwm[pwm_num].connected = true;
489 } else if (strnstr(data->label, "conf", sizeof(data->label))) {
491 dev_err(fan->dev, "duplicate conf entry: %s\n",
495 /* Validate that conf parameters are not zeros. */
496 if (!data->mask && !data->bit && !data->capability) {
497 dev_err(fan->dev, "invalid conf entry params: %s\n",
501 if (data->capability) {
502 err = mlxreg_fan_speed_divider_get(fan, data);
507 fan->samples = data->mask;
509 fan->divider = data->bit;
513 dev_err(fan->dev, "invalid label: %s\n", data->label);
518 if (pdata->capability) {
522 /* Obtain the number of FAN drawers, supported by system. */
523 err = regmap_read(fan->regmap, pdata->capability, ®val);
525 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
530 drwr_avail = hweight32(regval);
531 if (!tacho_avail || !drwr_avail || tacho_avail < drwr_avail) {
532 dev_err(fan->dev, "Configuration is invalid: drawers num %d tachos num %d\n",
533 drwr_avail, tacho_avail);
537 /* Set the number of tachometers per one drawer. */
538 fan->tachos_per_drwr = tacho_avail / drwr_avail;
544 static int mlxreg_fan_cooling_config(struct device *dev, struct mlxreg_fan *fan)
548 for (i = 0; i < MLXREG_FAN_MAX_PWM; i++) {
549 struct mlxreg_fan_pwm *pwm = &fan->pwm[i];
554 pwm->cdev = devm_thermal_of_cooling_device_register(dev, NULL, mlxreg_fan_name[i],
555 pwm, &mlxreg_fan_cooling_ops);
556 if (IS_ERR(pwm->cdev)) {
557 dev_err(dev, "Failed to register cooling device\n");
558 return PTR_ERR(pwm->cdev);
561 /* Set minimal PWM speed. */
562 pwm->last_hwmon_state = MLXREG_FAN_PWM_DUTY2STATE(MLXREG_FAN_MIN_DUTY);
568 static int mlxreg_fan_probe(struct platform_device *pdev)
570 struct mlxreg_core_platform_data *pdata;
571 struct device *dev = &pdev->dev;
572 struct mlxreg_fan *fan;
576 pdata = dev_get_platdata(dev);
578 dev_err(dev, "Failed to get platform data.\n");
582 fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
587 fan->regmap = pdata->regmap;
589 err = mlxreg_fan_config(fan, pdata);
593 hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
595 &mlxreg_fan_hwmon_chip_info,
598 dev_err(dev, "Failed to register hwmon device\n");
602 if (IS_REACHABLE(CONFIG_THERMAL))
603 err = mlxreg_fan_cooling_config(dev, fan);
608 static struct platform_driver mlxreg_fan_driver = {
610 .name = "mlxreg-fan",
612 .probe = mlxreg_fan_probe,
615 module_platform_driver(mlxreg_fan_driver);
617 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
618 MODULE_DESCRIPTION("Mellanox FAN driver");
619 MODULE_LICENSE("GPL");
620 MODULE_ALIAS("platform:mlxreg-fan");