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;
70 * @prsnt: present register offset;
72 struct mlxreg_fan_tacho {
80 * struct mlxreg_fan_pwm - PWM data (internal use):
82 * @connected: indicates if PWM is connected;
83 * @reg: register offset;
85 struct mlxreg_fan_pwm {
91 * struct mlxreg_fan - private data (internal use):
94 * @regmap: register map of parent device;
95 * @tacho: tachometer data;
97 * @tachos_per_drwr - number of tachometers per drawer;
98 * @samples: minimum allowed samples per pulse;
99 * @divider: divider value for tachometer RPM calculation;
100 * @cooling: cooling device levels;
101 * @cdev: cooling device;
106 struct mlxreg_core_platform_data *pdata;
107 struct mlxreg_fan_tacho tacho[MLXREG_FAN_MAX_TACHO];
108 struct mlxreg_fan_pwm pwm;
112 u8 cooling_levels[MLXREG_FAN_MAX_STATE + 1];
113 struct thermal_cooling_device *cdev;
117 mlxreg_fan_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
118 int channel, long *val)
120 struct mlxreg_fan *fan = dev_get_drvdata(dev);
121 struct mlxreg_fan_tacho *tacho;
127 tacho = &fan->tacho[channel];
129 case hwmon_fan_input:
131 * Check FAN presence: FAN related bit in presence register is one,
132 * if FAN is physically connected, zero - otherwise.
134 if (tacho->prsnt && fan->tachos_per_drwr) {
135 err = regmap_read(fan->regmap, tacho->prsnt, ®val);
140 * Map channel to presence bit - drawer can be equipped with
141 * one or few FANs, while presence is indicated per drawer.
143 if (BIT(channel / fan->tachos_per_drwr) & regval) {
144 /* FAN is not connected - return zero for FAN speed. */
150 err = regmap_read(fan->regmap, tacho->reg, ®val);
154 *val = MLXREG_FAN_GET_RPM(regval, fan->divider,
158 case hwmon_fan_fault:
159 err = regmap_read(fan->regmap, tacho->reg, ®val);
163 *val = MLXREG_FAN_GET_FAULT(regval, tacho->mask);
173 case hwmon_pwm_input:
174 err = regmap_read(fan->regmap, fan->pwm.reg, ®val);
194 mlxreg_fan_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
195 int channel, long val)
197 struct mlxreg_fan *fan = dev_get_drvdata(dev);
202 case hwmon_pwm_input:
203 if (val < MLXREG_FAN_MIN_DUTY ||
204 val > MLXREG_FAN_MAX_DUTY)
206 return regmap_write(fan->regmap, fan->pwm.reg, val);
220 mlxreg_fan_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
225 if (!(((struct mlxreg_fan *)data)->tacho[channel].connected))
229 case hwmon_fan_input:
230 case hwmon_fan_fault:
238 if (!(((struct mlxreg_fan *)data)->pwm.connected))
242 case hwmon_pwm_input:
256 static const struct hwmon_channel_info *mlxreg_fan_hwmon_info[] = {
257 HWMON_CHANNEL_INFO(fan,
258 HWMON_F_INPUT | HWMON_F_FAULT,
259 HWMON_F_INPUT | HWMON_F_FAULT,
260 HWMON_F_INPUT | HWMON_F_FAULT,
261 HWMON_F_INPUT | HWMON_F_FAULT,
262 HWMON_F_INPUT | HWMON_F_FAULT,
263 HWMON_F_INPUT | HWMON_F_FAULT,
264 HWMON_F_INPUT | HWMON_F_FAULT,
265 HWMON_F_INPUT | HWMON_F_FAULT,
266 HWMON_F_INPUT | HWMON_F_FAULT,
267 HWMON_F_INPUT | HWMON_F_FAULT,
268 HWMON_F_INPUT | HWMON_F_FAULT,
269 HWMON_F_INPUT | HWMON_F_FAULT),
270 HWMON_CHANNEL_INFO(pwm,
275 static const struct hwmon_ops mlxreg_fan_hwmon_hwmon_ops = {
276 .is_visible = mlxreg_fan_is_visible,
277 .read = mlxreg_fan_read,
278 .write = mlxreg_fan_write,
281 static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info = {
282 .ops = &mlxreg_fan_hwmon_hwmon_ops,
283 .info = mlxreg_fan_hwmon_info,
286 static int mlxreg_fan_get_max_state(struct thermal_cooling_device *cdev,
287 unsigned long *state)
289 *state = MLXREG_FAN_MAX_STATE;
293 static int mlxreg_fan_get_cur_state(struct thermal_cooling_device *cdev,
294 unsigned long *state)
297 struct mlxreg_fan *fan = cdev->devdata;
301 err = regmap_read(fan->regmap, fan->pwm.reg, ®val);
303 dev_err(fan->dev, "Failed to query PWM duty\n");
307 *state = MLXREG_FAN_PWM_DUTY2STATE(regval);
312 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
316 struct mlxreg_fan *fan = cdev->devdata;
317 unsigned long cur_state;
323 * Verify if this request is for changing allowed FAN dynamical
324 * minimum. If it is - update cooling levels accordingly and update
325 * state, if current state is below the newly requested minimum state.
326 * For example, if current state is 5, and minimal state is to be
327 * changed from 4 to 6, fan->cooling_levels[0 to 5] will be changed all
328 * from 4 to 6. And state 5 (fan->cooling_levels[4]) should be
331 if (state >= MLXREG_FAN_SPEED_MIN && state <= MLXREG_FAN_SPEED_MAX) {
333 * This is configuration change, which is only supported through sysfs.
334 * For configuration non-zero value is to be returned to avoid thermal
338 state -= MLXREG_FAN_MAX_STATE;
339 for (i = 0; i < state; i++)
340 fan->cooling_levels[i] = state;
341 for (i = state; i <= MLXREG_FAN_MAX_STATE; i++)
342 fan->cooling_levels[i] = i;
344 err = regmap_read(fan->regmap, fan->pwm.reg, ®val);
346 dev_err(fan->dev, "Failed to query PWM duty\n");
350 cur_state = MLXREG_FAN_PWM_DUTY2STATE(regval);
351 if (state < cur_state)
357 if (state > MLXREG_FAN_MAX_STATE)
360 /* Normalize the state to the valid speed range. */
361 state = fan->cooling_levels[state];
362 err = regmap_write(fan->regmap, fan->pwm.reg,
363 MLXREG_FAN_PWM_STATE2DUTY(state));
365 dev_err(fan->dev, "Failed to write PWM duty\n");
371 static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
372 .get_max_state = mlxreg_fan_get_max_state,
373 .get_cur_state = mlxreg_fan_get_cur_state,
374 .set_cur_state = mlxreg_fan_set_cur_state,
377 static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
378 struct mlxreg_core_data *data)
383 err = regmap_read(fan->regmap, data->capability, ®val);
385 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
390 return !!(regval & data->bit);
393 static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
394 struct mlxreg_core_data *data)
399 err = regmap_read(fan->regmap, data->capability, ®val);
401 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
407 * Set divider value according to the capability register, in case it
408 * contains valid value. Otherwise use default value. The purpose of
409 * this validation is to protect against the old hardware, in which
410 * this register can return zero.
412 if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIV_SCALE_MAX)
413 fan->divider = regval * MLXREG_FAN_TACHO_DIV_MIN;
418 static int mlxreg_fan_config(struct mlxreg_fan *fan,
419 struct mlxreg_core_platform_data *pdata)
421 struct mlxreg_core_data *data = pdata->data;
422 int tacho_num = 0, tacho_avail = 0, i;
423 bool configured = false;
426 fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
427 fan->divider = MLXREG_FAN_TACHO_DIV_DEF;
428 for (i = 0; i < pdata->counter; i++, data++) {
429 if (strnstr(data->label, "tacho", sizeof(data->label))) {
430 if (tacho_num == MLXREG_FAN_MAX_TACHO) {
431 dev_err(fan->dev, "too many tacho entries: %s\n",
436 if (data->capability) {
437 err = mlxreg_fan_connect_verify(fan, data);
446 fan->tacho[tacho_num].reg = data->reg;
447 fan->tacho[tacho_num].mask = data->mask;
448 fan->tacho[tacho_num].prsnt = data->reg_prsnt;
449 fan->tacho[tacho_num++].connected = true;
451 } else if (strnstr(data->label, "pwm", sizeof(data->label))) {
452 if (fan->pwm.connected) {
453 dev_err(fan->dev, "duplicate pwm entry: %s\n",
457 fan->pwm.reg = data->reg;
458 fan->pwm.connected = true;
459 } else if (strnstr(data->label, "conf", sizeof(data->label))) {
461 dev_err(fan->dev, "duplicate conf entry: %s\n",
465 /* Validate that conf parameters are not zeros. */
466 if (!data->mask && !data->bit && !data->capability) {
467 dev_err(fan->dev, "invalid conf entry params: %s\n",
471 if (data->capability) {
472 err = mlxreg_fan_speed_divider_get(fan, data);
477 fan->samples = data->mask;
479 fan->divider = data->bit;
483 dev_err(fan->dev, "invalid label: %s\n", data->label);
488 if (pdata->capability) {
492 /* Obtain the number of FAN drawers, supported by system. */
493 err = regmap_read(fan->regmap, pdata->capability, ®val);
495 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
500 drwr_avail = hweight32(regval);
501 if (!tacho_avail || !drwr_avail || tacho_avail < drwr_avail) {
502 dev_err(fan->dev, "Configuration is invalid: drawers num %d tachos num %d\n",
503 drwr_avail, tacho_avail);
507 /* Set the number of tachometers per one drawer. */
508 fan->tachos_per_drwr = tacho_avail / drwr_avail;
511 /* Init cooling levels per PWM state. */
512 for (i = 0; i < MLXREG_FAN_SPEED_MIN_LEVEL; i++)
513 fan->cooling_levels[i] = MLXREG_FAN_SPEED_MIN_LEVEL;
514 for (i = MLXREG_FAN_SPEED_MIN_LEVEL; i <= MLXREG_FAN_MAX_STATE; i++)
515 fan->cooling_levels[i] = i;
520 static int mlxreg_fan_probe(struct platform_device *pdev)
522 struct mlxreg_core_platform_data *pdata;
523 struct device *dev = &pdev->dev;
524 struct mlxreg_fan *fan;
528 pdata = dev_get_platdata(dev);
530 dev_err(dev, "Failed to get platform data.\n");
534 fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
539 fan->regmap = pdata->regmap;
541 err = mlxreg_fan_config(fan, pdata);
545 hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
547 &mlxreg_fan_hwmon_chip_info,
550 dev_err(dev, "Failed to register hwmon device\n");
554 if (IS_REACHABLE(CONFIG_THERMAL)) {
555 fan->cdev = devm_thermal_of_cooling_device_register(dev,
556 NULL, "mlxreg_fan", fan, &mlxreg_fan_cooling_ops);
557 if (IS_ERR(fan->cdev)) {
558 dev_err(dev, "Failed to register cooling device\n");
559 return PTR_ERR(fan->cdev);
566 static struct platform_driver mlxreg_fan_driver = {
568 .name = "mlxreg-fan",
570 .probe = mlxreg_fan_probe,
573 module_platform_driver(mlxreg_fan_driver);
575 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
576 MODULE_DESCRIPTION("Mellanox FAN driver");
577 MODULE_LICENSE("GPL");
578 MODULE_ALIAS("platform:mlxreg-fan");