Merge tag 'v5.15-rc2' into spi-5.15
[platform/kernel/linux-rpi.git] / drivers / hwmon / mlxreg-fan.c
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
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
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)))
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,          \
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;
70  * @prsnt: present register offset;
71  */
72 struct mlxreg_fan_tacho {
73         bool connected;
74         u32 reg;
75         u32 mask;
76         u32 prsnt;
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  */
85 struct 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;
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;
102  */
103 struct 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;
109         int tachos_per_drwr;
110         int samples;
111         int divider;
112         u8 cooling_levels[MLXREG_FAN_MAX_STATE + 1];
113         struct thermal_cooling_device *cdev;
114 };
115
116 static int
117 mlxreg_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:
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
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
193 static int
194 mlxreg_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
219 static umode_t
220 mlxreg_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
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,
271                            HWMON_PWM_INPUT),
272         NULL
273 };
274
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,
279 };
280
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,
284 };
285
286 static 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
293 static 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
312 static 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;
318         u32 regval;
319         int i;
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) {
332                 state -= MLXREG_FAN_MAX_STATE;
333                 for (i = 0; i < state; i++)
334                         fan->cooling_levels[i] = state;
335                 for (i = state; i <= MLXREG_FAN_MAX_STATE; i++)
336                         fan->cooling_levels[i] = i;
337
338                 err = regmap_read(fan->regmap, fan->pwm.reg, &regval);
339                 if (err) {
340                         dev_err(fan->dev, "Failed to query PWM duty\n");
341                         return err;
342                 }
343
344                 cur_state = MLXREG_FAN_PWM_DUTY2STATE(regval);
345                 if (state < cur_state)
346                         return 0;
347
348                 state = cur_state;
349         }
350
351         if (state > MLXREG_FAN_MAX_STATE)
352                 return -EINVAL;
353
354         /* Normalize the state to the valid speed range. */
355         state = fan->cooling_levels[state];
356         err = regmap_write(fan->regmap, fan->pwm.reg,
357                            MLXREG_FAN_PWM_STATE2DUTY(state));
358         if (err) {
359                 dev_err(fan->dev, "Failed to write PWM duty\n");
360                 return err;
361         }
362         return 0;
363 }
364
365 static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
366         .get_max_state  = mlxreg_fan_get_max_state,
367         .get_cur_state  = mlxreg_fan_get_cur_state,
368         .set_cur_state  = mlxreg_fan_set_cur_state,
369 };
370
371 static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
372                                      struct mlxreg_core_data *data)
373 {
374         u32 regval;
375         int err;
376
377         err = regmap_read(fan->regmap, data->capability, &regval);
378         if (err) {
379                 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
380                         data->capability);
381                 return err;
382         }
383
384         return !!(regval & data->bit);
385 }
386
387 static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
388                                         struct mlxreg_core_data *data)
389 {
390         u32 regval;
391         int err;
392
393         err = regmap_read(fan->regmap, data->capability, &regval);
394         if (err) {
395                 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
396                         data->capability);
397                 return err;
398         }
399
400         /*
401          * Set divider value according to the capability register, in case it
402          * contains valid value. Otherwise use default value. The purpose of
403          * this validation is to protect against the old hardware, in which
404          * this register can return zero.
405          */
406         if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIV_SCALE_MAX)
407                 fan->divider = regval * MLXREG_FAN_TACHO_DIV_MIN;
408
409         return 0;
410 }
411
412 static int mlxreg_fan_config(struct mlxreg_fan *fan,
413                              struct mlxreg_core_platform_data *pdata)
414 {
415         struct mlxreg_core_data *data = pdata->data;
416         int tacho_num = 0, tacho_avail = 0, i;
417         bool configured = false;
418         int err;
419
420         fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
421         fan->divider = MLXREG_FAN_TACHO_DIV_DEF;
422         for (i = 0; i < pdata->counter; i++, data++) {
423                 if (strnstr(data->label, "tacho", sizeof(data->label))) {
424                         if (tacho_num == MLXREG_FAN_MAX_TACHO) {
425                                 dev_err(fan->dev, "too many tacho entries: %s\n",
426                                         data->label);
427                                 return -EINVAL;
428                         }
429
430                         if (data->capability) {
431                                 err = mlxreg_fan_connect_verify(fan, data);
432                                 if (err < 0)
433                                         return err;
434                                 else if (!err) {
435                                         tacho_num++;
436                                         continue;
437                                 }
438                         }
439
440                         fan->tacho[tacho_num].reg = data->reg;
441                         fan->tacho[tacho_num].mask = data->mask;
442                         fan->tacho[tacho_num].prsnt = data->reg_prsnt;
443                         fan->tacho[tacho_num++].connected = true;
444                         tacho_avail++;
445                 } else if (strnstr(data->label, "pwm", sizeof(data->label))) {
446                         if (fan->pwm.connected) {
447                                 dev_err(fan->dev, "duplicate pwm entry: %s\n",
448                                         data->label);
449                                 return -EINVAL;
450                         }
451                         fan->pwm.reg = data->reg;
452                         fan->pwm.connected = true;
453                 } else if (strnstr(data->label, "conf", sizeof(data->label))) {
454                         if (configured) {
455                                 dev_err(fan->dev, "duplicate conf entry: %s\n",
456                                         data->label);
457                                 return -EINVAL;
458                         }
459                         /* Validate that conf parameters are not zeros. */
460                         if (!data->mask && !data->bit && !data->capability) {
461                                 dev_err(fan->dev, "invalid conf entry params: %s\n",
462                                         data->label);
463                                 return -EINVAL;
464                         }
465                         if (data->capability) {
466                                 err = mlxreg_fan_speed_divider_get(fan, data);
467                                 if (err)
468                                         return err;
469                         } else {
470                                 if (data->mask)
471                                         fan->samples = data->mask;
472                                 if (data->bit)
473                                         fan->divider = data->bit;
474                         }
475                         configured = true;
476                 } else {
477                         dev_err(fan->dev, "invalid label: %s\n", data->label);
478                         return -EINVAL;
479                 }
480         }
481
482         if (pdata->capability) {
483                 int drwr_avail;
484                 u32 regval;
485
486                 /* Obtain the number of FAN drawers, supported by system. */
487                 err = regmap_read(fan->regmap, pdata->capability, &regval);
488                 if (err) {
489                         dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
490                                 pdata->capability);
491                         return err;
492                 }
493
494                 drwr_avail = hweight32(regval);
495                 if (!tacho_avail || !drwr_avail || tacho_avail < drwr_avail) {
496                         dev_err(fan->dev, "Configuration is invalid: drawers num %d tachos num %d\n",
497                                 drwr_avail, tacho_avail);
498                         return -EINVAL;
499                 }
500
501                 /* Set the number of tachometers per one drawer. */
502                 fan->tachos_per_drwr = tacho_avail / drwr_avail;
503         }
504
505         /* Init cooling levels per PWM state. */
506         for (i = 0; i < MLXREG_FAN_SPEED_MIN_LEVEL; i++)
507                 fan->cooling_levels[i] = MLXREG_FAN_SPEED_MIN_LEVEL;
508         for (i = MLXREG_FAN_SPEED_MIN_LEVEL; i <= MLXREG_FAN_MAX_STATE; i++)
509                 fan->cooling_levels[i] = i;
510
511         return 0;
512 }
513
514 static int mlxreg_fan_probe(struct platform_device *pdev)
515 {
516         struct mlxreg_core_platform_data *pdata;
517         struct device *dev = &pdev->dev;
518         struct mlxreg_fan *fan;
519         struct device *hwm;
520         int err;
521
522         pdata = dev_get_platdata(dev);
523         if (!pdata) {
524                 dev_err(dev, "Failed to get platform data.\n");
525                 return -EINVAL;
526         }
527
528         fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
529         if (!fan)
530                 return -ENOMEM;
531
532         fan->dev = dev;
533         fan->regmap = pdata->regmap;
534
535         err = mlxreg_fan_config(fan, pdata);
536         if (err)
537                 return err;
538
539         hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
540                                                    fan,
541                                                    &mlxreg_fan_hwmon_chip_info,
542                                                    NULL);
543         if (IS_ERR(hwm)) {
544                 dev_err(dev, "Failed to register hwmon device\n");
545                 return PTR_ERR(hwm);
546         }
547
548         if (IS_REACHABLE(CONFIG_THERMAL)) {
549                 fan->cdev = devm_thermal_of_cooling_device_register(dev,
550                         NULL, "mlxreg_fan", fan, &mlxreg_fan_cooling_ops);
551                 if (IS_ERR(fan->cdev)) {
552                         dev_err(dev, "Failed to register cooling device\n");
553                         return PTR_ERR(fan->cdev);
554                 }
555         }
556
557         return 0;
558 }
559
560 static struct platform_driver mlxreg_fan_driver = {
561         .driver = {
562             .name = "mlxreg-fan",
563         },
564         .probe = mlxreg_fan_probe,
565 };
566
567 module_platform_driver(mlxreg_fan_driver);
568
569 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
570 MODULE_DESCRIPTION("Mellanox FAN driver");
571 MODULE_LICENSE("GPL");
572 MODULE_ALIAS("platform:mlxreg-fan");