]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/hwmon/mlxreg-fan.c
Merge tag 'efi-urgent-for-v5.15' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-jammy-kernel.git] / drivers / hwmon / mlxreg-fan.c
CommitLineData
65afb4c8
VP
1// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2//
3// Copyright (c) 2018 Mellanox Technologies. All rights reserved.
4// Copyright (c) 2018 Vadim Pasternak <vadimp@mellanox.com>
5
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>
14
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% */
19/*
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.
25 */
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
b429ebc8
VP
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
65afb4c8
VP
33/*
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).
53 */
54#define MLXREG_FAN_GET_RPM(rval, d, s) (DIV_ROUND_CLOSEST(15000000 * 100, \
55 ((rval) + (s)) * (d)))
3f9ffa5c 56#define MLXREG_FAN_GET_FAULT(val, mask) ((val) == (mask))
65afb4c8
VP
57#define MLXREG_FAN_PWM_DUTY2STATE(duty) (DIV_ROUND_CLOSEST((duty) * \
58 MLXREG_FAN_MAX_STATE, \
59 MLXREG_FAN_MAX_DUTY))
60#define MLXREG_FAN_PWM_STATE2DUTY(stat) (DIV_ROUND_CLOSEST((stat) * \
61 MLXREG_FAN_MAX_DUTY, \
62 MLXREG_FAN_MAX_STATE))
63
64/*
65 * struct mlxreg_fan_tacho - tachometer data (internal use):
66 *
67 * @connected: indicates if tachometer is connected;
68 * @reg: register offset;
69 * @mask: fault mask;
f7bf7eb2 70 * @prsnt: present register offset;
65afb4c8
VP
71 */
72struct mlxreg_fan_tacho {
73 bool connected;
74 u32 reg;
75 u32 mask;
f7bf7eb2 76 u32 prsnt;
65afb4c8
VP
77};
78
79/*
80 * struct mlxreg_fan_pwm - PWM data (internal use):
81 *
82 * @connected: indicates if PWM is connected;
83 * @reg: register offset;
84 */
85struct mlxreg_fan_pwm {
86 bool connected;
87 u32 reg;
88};
89
90/*
91 * struct mlxreg_fan - private data (internal use):
92 *
93 * @dev: basic device;
94 * @regmap: register map of parent device;
95 * @tacho: tachometer data;
96 * @pwm: PWM data;
f7bf7eb2 97 * @tachos_per_drwr - number of tachometers per drawer;
65afb4c8
VP
98 * @samples: minimum allowed samples per pulse;
99 * @divider: divider value for tachometer RPM calculation;
100 * @cooling: cooling device levels;
101 * @cdev: cooling device;
102 */
103struct mlxreg_fan {
104 struct device *dev;
105 void *regmap;
106 struct mlxreg_core_platform_data *pdata;
107 struct mlxreg_fan_tacho tacho[MLXREG_FAN_MAX_TACHO];
108 struct mlxreg_fan_pwm pwm;
f7bf7eb2 109 int tachos_per_drwr;
65afb4c8
VP
110 int samples;
111 int divider;
112 u8 cooling_levels[MLXREG_FAN_MAX_STATE + 1];
113 struct thermal_cooling_device *cdev;
114};
115
116static int
117mlxreg_fan_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
118 int channel, long *val)
119{
120 struct mlxreg_fan *fan = dev_get_drvdata(dev);
121 struct mlxreg_fan_tacho *tacho;
122 u32 regval;
123 int err;
124
125 switch (type) {
126 case hwmon_fan:
127 tacho = &fan->tacho[channel];
128 switch (attr) {
129 case hwmon_fan_input:
f7bf7eb2
VP
130 /*
131 * Check FAN presence: FAN related bit in presence register is one,
132 * if FAN is physically connected, zero - otherwise.
133 */
134 if (tacho->prsnt && fan->tachos_per_drwr) {
135 err = regmap_read(fan->regmap, tacho->prsnt, &regval);
136 if (err)
137 return err;
138
139 /*
140 * Map channel to presence bit - drawer can be equipped with
141 * one or few FANs, while presence is indicated per drawer.
142 */
143 if (BIT(channel / fan->tachos_per_drwr) & regval) {
144 /* FAN is not connected - return zero for FAN speed. */
145 *val = 0;
146 return 0;
147 }
148 }
149
65afb4c8
VP
150 err = regmap_read(fan->regmap, tacho->reg, &regval);
151 if (err)
152 return err;
153
154 *val = MLXREG_FAN_GET_RPM(regval, fan->divider,
155 fan->samples);
156 break;
157
158 case hwmon_fan_fault:
159 err = regmap_read(fan->regmap, tacho->reg, &regval);
160 if (err)
161 return err;
162
163 *val = MLXREG_FAN_GET_FAULT(regval, tacho->mask);
164 break;
165
166 default:
167 return -EOPNOTSUPP;
168 }
169 break;
170
171 case hwmon_pwm:
172 switch (attr) {
173 case hwmon_pwm_input:
174 err = regmap_read(fan->regmap, fan->pwm.reg, &regval);
175 if (err)
176 return err;
177
178 *val = regval;
179 break;
180
181 default:
182 return -EOPNOTSUPP;
183 }
184 break;
185
186 default:
187 return -EOPNOTSUPP;
188 }
189
190 return 0;
191}
192
193static int
194mlxreg_fan_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
195 int channel, long val)
196{
197 struct mlxreg_fan *fan = dev_get_drvdata(dev);
198
199 switch (type) {
200 case hwmon_pwm:
201 switch (attr) {
202 case hwmon_pwm_input:
203 if (val < MLXREG_FAN_MIN_DUTY ||
204 val > MLXREG_FAN_MAX_DUTY)
205 return -EINVAL;
206 return regmap_write(fan->regmap, fan->pwm.reg, val);
207 default:
208 return -EOPNOTSUPP;
209 }
210 break;
211
212 default:
213 return -EOPNOTSUPP;
214 }
215
216 return -EOPNOTSUPP;
217}
218
219static umode_t
220mlxreg_fan_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
221 int channel)
222{
223 switch (type) {
224 case hwmon_fan:
225 if (!(((struct mlxreg_fan *)data)->tacho[channel].connected))
226 return 0;
227
228 switch (attr) {
229 case hwmon_fan_input:
230 case hwmon_fan_fault:
231 return 0444;
232 default:
233 break;
234 }
235 break;
236
237 case hwmon_pwm:
238 if (!(((struct mlxreg_fan *)data)->pwm.connected))
239 return 0;
240
241 switch (attr) {
242 case hwmon_pwm_input:
243 return 0644;
244 default:
245 break;
246 }
247 break;
248
249 default:
250 break;
251 }
252
253 return 0;
254}
255
65afb4c8 256static const struct hwmon_channel_info *mlxreg_fan_hwmon_info[] = {
725dcf08
GR
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,
271 HWMON_PWM_INPUT),
65afb4c8
VP
272 NULL
273};
274
275static 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,
279};
280
281static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info = {
282 .ops = &mlxreg_fan_hwmon_hwmon_ops,
283 .info = mlxreg_fan_hwmon_info,
284};
285
286static int mlxreg_fan_get_max_state(struct thermal_cooling_device *cdev,
287 unsigned long *state)
288{
289 *state = MLXREG_FAN_MAX_STATE;
290 return 0;
291}
292
293static int mlxreg_fan_get_cur_state(struct thermal_cooling_device *cdev,
294 unsigned long *state)
295
296{
297 struct mlxreg_fan *fan = cdev->devdata;
298 u32 regval;
299 int err;
300
301 err = regmap_read(fan->regmap, fan->pwm.reg, &regval);
302 if (err) {
303 dev_err(fan->dev, "Failed to query PWM duty\n");
304 return err;
305 }
306
307 *state = MLXREG_FAN_PWM_DUTY2STATE(regval);
308
309 return 0;
310}
311
312static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
313 unsigned long state)
314
315{
316 struct mlxreg_fan *fan = cdev->devdata;
317 unsigned long cur_state;
e6fab7af 318 int i, config = 0;
65afb4c8 319 u32 regval;
65afb4c8
VP
320 int err;
321
322 /*
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
329 * overwritten.
330 */
331 if (state >= MLXREG_FAN_SPEED_MIN && state <= MLXREG_FAN_SPEED_MAX) {
e6fab7af
VP
332 /*
333 * This is configuration change, which is only supported through sysfs.
334 * For configuration non-zero value is to be returned to avoid thermal
335 * statistics update.
336 */
337 config = 1;
65afb4c8
VP
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;
343
344 err = regmap_read(fan->regmap, fan->pwm.reg, &regval);
345 if (err) {
346 dev_err(fan->dev, "Failed to query PWM duty\n");
347 return err;
348 }
349
350 cur_state = MLXREG_FAN_PWM_DUTY2STATE(regval);
351 if (state < cur_state)
e6fab7af 352 return config;
65afb4c8
VP
353
354 state = cur_state;
355 }
356
357 if (state > MLXREG_FAN_MAX_STATE)
358 return -EINVAL;
359
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));
364 if (err) {
365 dev_err(fan->dev, "Failed to write PWM duty\n");
366 return err;
367 }
e6fab7af 368 return config;
65afb4c8
VP
369}
370
371static 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,
375};
376
b429ebc8
VP
377static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
378 struct mlxreg_core_data *data)
379{
380 u32 regval;
381 int err;
382
383 err = regmap_read(fan->regmap, data->capability, &regval);
384 if (err) {
385 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
386 data->capability);
387 return err;
388 }
389
390 return !!(regval & data->bit);
391}
392
393static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
394 struct mlxreg_core_data *data)
395{
396 u32 regval;
397 int err;
398
399 err = regmap_read(fan->regmap, data->capability, &regval);
400 if (err) {
401 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
402 data->capability);
403 return err;
404 }
405
406 /*
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.
411 */
412 if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIV_SCALE_MAX)
413 fan->divider = regval * MLXREG_FAN_TACHO_DIV_MIN;
414
415 return 0;
416}
417
65afb4c8
VP
418static int mlxreg_fan_config(struct mlxreg_fan *fan,
419 struct mlxreg_core_platform_data *pdata)
420{
421 struct mlxreg_core_data *data = pdata->data;
f7bf7eb2 422 int tacho_num = 0, tacho_avail = 0, i;
65afb4c8 423 bool configured = false;
b429ebc8 424 int err;
65afb4c8
VP
425
426 fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
b429ebc8 427 fan->divider = MLXREG_FAN_TACHO_DIV_DEF;
65afb4c8
VP
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",
432 data->label);
433 return -EINVAL;
434 }
b429ebc8
VP
435
436 if (data->capability) {
437 err = mlxreg_fan_connect_verify(fan, data);
438 if (err < 0)
439 return err;
440 else if (!err) {
441 tacho_num++;
442 continue;
443 }
444 }
445
65afb4c8
VP
446 fan->tacho[tacho_num].reg = data->reg;
447 fan->tacho[tacho_num].mask = data->mask;
f7bf7eb2 448 fan->tacho[tacho_num].prsnt = data->reg_prsnt;
65afb4c8 449 fan->tacho[tacho_num++].connected = true;
f7bf7eb2 450 tacho_avail++;
65afb4c8
VP
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",
454 data->label);
455 return -EINVAL;
456 }
457 fan->pwm.reg = data->reg;
458 fan->pwm.connected = true;
459 } else if (strnstr(data->label, "conf", sizeof(data->label))) {
460 if (configured) {
461 dev_err(fan->dev, "duplicate conf entry: %s\n",
462 data->label);
463 return -EINVAL;
464 }
465 /* Validate that conf parameters are not zeros. */
b429ebc8 466 if (!data->mask && !data->bit && !data->capability) {
65afb4c8
VP
467 dev_err(fan->dev, "invalid conf entry params: %s\n",
468 data->label);
469 return -EINVAL;
470 }
b429ebc8
VP
471 if (data->capability) {
472 err = mlxreg_fan_speed_divider_get(fan, data);
473 if (err)
474 return err;
475 } else {
476 if (data->mask)
477 fan->samples = data->mask;
478 if (data->bit)
479 fan->divider = data->bit;
480 }
65afb4c8
VP
481 configured = true;
482 } else {
483 dev_err(fan->dev, "invalid label: %s\n", data->label);
484 return -EINVAL;
485 }
486 }
487
f7bf7eb2
VP
488 if (pdata->capability) {
489 int drwr_avail;
490 u32 regval;
491
492 /* Obtain the number of FAN drawers, supported by system. */
493 err = regmap_read(fan->regmap, pdata->capability, &regval);
494 if (err) {
495 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
496 pdata->capability);
497 return err;
498 }
499
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);
504 return -EINVAL;
505 }
506
507 /* Set the number of tachometers per one drawer. */
508 fan->tachos_per_drwr = tacho_avail / drwr_avail;
509 }
510
65afb4c8
VP
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;
516
517 return 0;
518}
519
520static int mlxreg_fan_probe(struct platform_device *pdev)
521{
522 struct mlxreg_core_platform_data *pdata;
9ebe010e 523 struct device *dev = &pdev->dev;
65afb4c8
VP
524 struct mlxreg_fan *fan;
525 struct device *hwm;
526 int err;
527
9ebe010e 528 pdata = dev_get_platdata(dev);
65afb4c8 529 if (!pdata) {
9ebe010e 530 dev_err(dev, "Failed to get platform data.\n");
65afb4c8
VP
531 return -EINVAL;
532 }
533
9ebe010e 534 fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
65afb4c8
VP
535 if (!fan)
536 return -ENOMEM;
537
9ebe010e 538 fan->dev = dev;
65afb4c8 539 fan->regmap = pdata->regmap;
65afb4c8
VP
540
541 err = mlxreg_fan_config(fan, pdata);
542 if (err)
543 return err;
544
9ebe010e 545 hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
65afb4c8
VP
546 fan,
547 &mlxreg_fan_hwmon_chip_info,
548 NULL);
549 if (IS_ERR(hwm)) {
9ebe010e 550 dev_err(dev, "Failed to register hwmon device\n");
65afb4c8
VP
551 return PTR_ERR(hwm);
552 }
553
554 if (IS_REACHABLE(CONFIG_THERMAL)) {
9ebe010e
GR
555 fan->cdev = devm_thermal_of_cooling_device_register(dev,
556 NULL, "mlxreg_fan", fan, &mlxreg_fan_cooling_ops);
65afb4c8 557 if (IS_ERR(fan->cdev)) {
9ebe010e 558 dev_err(dev, "Failed to register cooling device\n");
65afb4c8
VP
559 return PTR_ERR(fan->cdev);
560 }
561 }
562
563 return 0;
564}
565
65afb4c8
VP
566static struct platform_driver mlxreg_fan_driver = {
567 .driver = {
568 .name = "mlxreg-fan",
569 },
570 .probe = mlxreg_fan_probe,
65afb4c8
VP
571};
572
573module_platform_driver(mlxreg_fan_driver);
574
575MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
576MODULE_DESCRIPTION("Mellanox FAN driver");
577MODULE_LICENSE("GPL");
578MODULE_ALIAS("platform:mlxreg-fan");