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 *val = MLXREG_FAN_GET_RPM(regval, fan->divider,
162 case hwmon_fan_fault:
163 err = regmap_read(fan->regmap, tacho->reg, ®val);
167 *val = MLXREG_FAN_GET_FAULT(regval, tacho->mask);
176 pwm = &fan->pwm[channel];
178 case hwmon_pwm_input:
179 err = regmap_read(fan->regmap, pwm->reg, ®val);
199 mlxreg_fan_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
200 int channel, long val)
202 struct mlxreg_fan *fan = dev_get_drvdata(dev);
203 struct mlxreg_fan_pwm *pwm;
208 case hwmon_pwm_input:
209 if (val < MLXREG_FAN_MIN_DUTY ||
210 val > MLXREG_FAN_MAX_DUTY)
212 pwm = &fan->pwm[channel];
213 /* If thermal is configured - handle PWM limit setting. */
214 if (IS_REACHABLE(CONFIG_THERMAL)) {
215 pwm->last_hwmon_state = MLXREG_FAN_PWM_DUTY2STATE(val);
217 * Update PWM only in case requested state is not less than the
218 * last thermal state.
220 if (pwm->last_hwmon_state >= pwm->last_thermal_state)
221 return mlxreg_fan_set_cur_state(pwm->cdev,
222 pwm->last_hwmon_state);
225 return regmap_write(fan->regmap, pwm->reg, val);
239 mlxreg_fan_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
244 if (!(((struct mlxreg_fan *)data)->tacho[channel].connected))
248 case hwmon_fan_input:
249 case hwmon_fan_fault:
257 if (!(((struct mlxreg_fan *)data)->pwm[channel].connected))
261 case hwmon_pwm_input:
275 static char *mlxreg_fan_name[] = {
282 static const struct hwmon_channel_info *mlxreg_fan_hwmon_info[] = {
283 HWMON_CHANNEL_INFO(fan,
284 HWMON_F_INPUT | HWMON_F_FAULT,
285 HWMON_F_INPUT | HWMON_F_FAULT,
286 HWMON_F_INPUT | HWMON_F_FAULT,
287 HWMON_F_INPUT | HWMON_F_FAULT,
288 HWMON_F_INPUT | HWMON_F_FAULT,
289 HWMON_F_INPUT | HWMON_F_FAULT,
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_CHANNEL_INFO(pwm,
306 static const struct hwmon_ops mlxreg_fan_hwmon_hwmon_ops = {
307 .is_visible = mlxreg_fan_is_visible,
308 .read = mlxreg_fan_read,
309 .write = mlxreg_fan_write,
312 static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info = {
313 .ops = &mlxreg_fan_hwmon_hwmon_ops,
314 .info = mlxreg_fan_hwmon_info,
317 static int mlxreg_fan_get_max_state(struct thermal_cooling_device *cdev,
318 unsigned long *state)
320 *state = MLXREG_FAN_MAX_STATE;
324 static int mlxreg_fan_get_cur_state(struct thermal_cooling_device *cdev,
325 unsigned long *state)
328 struct mlxreg_fan_pwm *pwm = cdev->devdata;
329 struct mlxreg_fan *fan = pwm->fan;
333 err = regmap_read(fan->regmap, pwm->reg, ®val);
335 dev_err(fan->dev, "Failed to query PWM duty\n");
339 *state = MLXREG_FAN_PWM_DUTY2STATE(regval);
344 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
348 struct mlxreg_fan_pwm *pwm = cdev->devdata;
349 struct mlxreg_fan *fan = pwm->fan;
352 if (state > MLXREG_FAN_MAX_STATE)
355 /* Save thermal state. */
356 pwm->last_thermal_state = state;
358 state = max_t(unsigned long, state, pwm->last_hwmon_state);
359 err = regmap_write(fan->regmap, pwm->reg,
360 MLXREG_FAN_PWM_STATE2DUTY(state));
362 dev_err(fan->dev, "Failed to write PWM duty\n");
368 static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
369 .get_max_state = mlxreg_fan_get_max_state,
370 .get_cur_state = mlxreg_fan_get_cur_state,
371 .set_cur_state = mlxreg_fan_set_cur_state,
374 static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
375 struct mlxreg_core_data *data)
380 err = regmap_read(fan->regmap, data->capability, ®val);
382 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
387 return !!(regval & data->bit);
390 static int mlxreg_pwm_connect_verify(struct mlxreg_fan *fan,
391 struct mlxreg_core_data *data)
396 err = regmap_read(fan->regmap, data->reg, ®val);
398 dev_err(fan->dev, "Failed to query pwm register 0x%08x\n",
403 return regval != MLXREG_FAN_PWM_NOT_CONNECTED;
406 static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
407 struct mlxreg_core_data *data)
412 err = regmap_read(fan->regmap, data->capability, ®val);
414 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
420 * Set divider value according to the capability register, in case it
421 * contains valid value. Otherwise use default value. The purpose of
422 * this validation is to protect against the old hardware, in which
423 * this register can return zero.
425 if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIV_SCALE_MAX)
426 fan->divider = regval * MLXREG_FAN_TACHO_DIV_MIN;
431 static int mlxreg_fan_config(struct mlxreg_fan *fan,
432 struct mlxreg_core_platform_data *pdata)
434 int tacho_num = 0, tacho_avail = 0, pwm_num = 0, i;
435 struct mlxreg_core_data *data = pdata->data;
436 bool configured = false;
439 fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
440 fan->divider = MLXREG_FAN_TACHO_DIV_DEF;
441 for (i = 0; i < pdata->counter; i++, data++) {
442 if (strnstr(data->label, "tacho", sizeof(data->label))) {
443 if (tacho_num == MLXREG_FAN_MAX_TACHO) {
444 dev_err(fan->dev, "too many tacho entries: %s\n",
449 if (data->capability) {
450 err = mlxreg_fan_connect_verify(fan, data);
459 fan->tacho[tacho_num].reg = data->reg;
460 fan->tacho[tacho_num].mask = data->mask;
461 fan->tacho[tacho_num].prsnt = data->reg_prsnt;
462 fan->tacho[tacho_num++].connected = true;
464 } else if (strnstr(data->label, "pwm", sizeof(data->label))) {
465 if (pwm_num == MLXREG_FAN_MAX_TACHO) {
466 dev_err(fan->dev, "too many pwm entries: %s\n",
471 /* Validate if more then one PWM is connected. */
473 err = mlxreg_pwm_connect_verify(fan, data);
480 fan->pwm[pwm_num].reg = data->reg;
481 fan->pwm[pwm_num].connected = true;
483 } else if (strnstr(data->label, "conf", sizeof(data->label))) {
485 dev_err(fan->dev, "duplicate conf entry: %s\n",
489 /* Validate that conf parameters are not zeros. */
490 if (!data->mask && !data->bit && !data->capability) {
491 dev_err(fan->dev, "invalid conf entry params: %s\n",
495 if (data->capability) {
496 err = mlxreg_fan_speed_divider_get(fan, data);
501 fan->samples = data->mask;
503 fan->divider = data->bit;
507 dev_err(fan->dev, "invalid label: %s\n", data->label);
512 if (pdata->capability) {
516 /* Obtain the number of FAN drawers, supported by system. */
517 err = regmap_read(fan->regmap, pdata->capability, ®val);
519 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
524 drwr_avail = hweight32(regval);
525 if (!tacho_avail || !drwr_avail || tacho_avail < drwr_avail) {
526 dev_err(fan->dev, "Configuration is invalid: drawers num %d tachos num %d\n",
527 drwr_avail, tacho_avail);
531 /* Set the number of tachometers per one drawer. */
532 fan->tachos_per_drwr = tacho_avail / drwr_avail;
538 static int mlxreg_fan_cooling_config(struct device *dev, struct mlxreg_fan *fan)
542 for (i = 0; i < MLXREG_FAN_MAX_PWM; i++) {
543 struct mlxreg_fan_pwm *pwm = &fan->pwm[i];
548 pwm->cdev = devm_thermal_of_cooling_device_register(dev, NULL, mlxreg_fan_name[i],
549 pwm, &mlxreg_fan_cooling_ops);
550 if (IS_ERR(pwm->cdev)) {
551 dev_err(dev, "Failed to register cooling device\n");
552 return PTR_ERR(pwm->cdev);
555 /* Set minimal PWM speed. */
556 pwm->last_hwmon_state = MLXREG_FAN_PWM_DUTY2STATE(MLXREG_FAN_MIN_DUTY);
562 static int mlxreg_fan_probe(struct platform_device *pdev)
564 struct mlxreg_core_platform_data *pdata;
565 struct device *dev = &pdev->dev;
566 struct mlxreg_fan *fan;
570 pdata = dev_get_platdata(dev);
572 dev_err(dev, "Failed to get platform data.\n");
576 fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
581 fan->regmap = pdata->regmap;
583 err = mlxreg_fan_config(fan, pdata);
587 hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
589 &mlxreg_fan_hwmon_chip_info,
592 dev_err(dev, "Failed to register hwmon device\n");
596 if (IS_REACHABLE(CONFIG_THERMAL))
597 err = mlxreg_fan_cooling_config(dev, fan);
602 static struct platform_driver mlxreg_fan_driver = {
604 .name = "mlxreg-fan",
606 .probe = mlxreg_fan_probe,
609 module_platform_driver(mlxreg_fan_driver);
611 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
612 MODULE_DESCRIPTION("Mellanox FAN driver");
613 MODULE_LICENSE("GPL");
614 MODULE_ALIAS("platform:mlxreg-fan");