Merge tag 'ovl-update-6.5-2' of git://git.kernel.org/pub/scm/linux/kernel/git/overlay...
[platform/kernel/linux-starfive.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            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
26 /*
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).
46  */
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,          \
52                                          MLXREG_FAN_MAX_DUTY))
53 #define MLXREG_FAN_PWM_STATE2DUTY(stat) (DIV_ROUND_CLOSEST((stat) *     \
54                                          MLXREG_FAN_MAX_DUTY,           \
55                                          MLXREG_FAN_MAX_STATE))
56
57 struct mlxreg_fan;
58
59 /*
60  * struct mlxreg_fan_tacho - tachometer data (internal use):
61  *
62  * @connected: indicates if tachometer is connected;
63  * @reg: register offset;
64  * @mask: fault mask;
65  * @prsnt: present register offset;
66  */
67 struct mlxreg_fan_tacho {
68         bool connected;
69         u32 reg;
70         u32 mask;
71         u32 prsnt;
72 };
73
74 /*
75  * struct mlxreg_fan_pwm - PWM data (internal use):
76  *
77  * @fan: private data;
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;
84  */
85 struct mlxreg_fan_pwm {
86         struct mlxreg_fan *fan;
87         bool connected;
88         u32 reg;
89         unsigned long last_hwmon_state;
90         unsigned long last_thermal_state;
91         struct thermal_cooling_device *cdev;
92 };
93
94 /*
95  * struct mlxreg_fan - private data (internal use):
96  *
97  * @dev: basic device;
98  * @regmap: register map of parent device;
99  * @tacho: tachometer data;
100  * @pwm: PWM 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;
104  */
105 struct mlxreg_fan {
106         struct device *dev;
107         void *regmap;
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];
111         int tachos_per_drwr;
112         int samples;
113         int divider;
114 };
115
116 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
117                                     unsigned long state);
118
119 static int
120 mlxreg_fan_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
121                 int channel, long *val)
122 {
123         struct mlxreg_fan *fan = dev_get_drvdata(dev);
124         struct mlxreg_fan_tacho *tacho;
125         struct mlxreg_fan_pwm *pwm;
126         u32 regval;
127         int err;
128
129         switch (type) {
130         case hwmon_fan:
131                 tacho = &fan->tacho[channel];
132                 switch (attr) {
133                 case hwmon_fan_input:
134                         /*
135                          * Check FAN presence: FAN related bit in presence register is one,
136                          * if FAN is physically connected, zero - otherwise.
137                          */
138                         if (tacho->prsnt && fan->tachos_per_drwr) {
139                                 err = regmap_read(fan->regmap, tacho->prsnt, &regval);
140                                 if (err)
141                                         return err;
142
143                                 /*
144                                  * Map channel to presence bit - drawer can be equipped with
145                                  * one or few FANs, while presence is indicated per drawer.
146                                  */
147                                 if (BIT(channel / fan->tachos_per_drwr) & regval) {
148                                         /* FAN is not connected - return zero for FAN speed. */
149                                         *val = 0;
150                                         return 0;
151                                 }
152                         }
153
154                         err = regmap_read(fan->regmap, tacho->reg, &regval);
155                         if (err)
156                                 return err;
157
158                         if (MLXREG_FAN_GET_FAULT(regval, tacho->mask)) {
159                                 /* FAN is broken - return zero for FAN speed. */
160                                 *val = 0;
161                                 return 0;
162                         }
163
164                         *val = MLXREG_FAN_GET_RPM(regval, fan->divider,
165                                                   fan->samples);
166                         break;
167
168                 case hwmon_fan_fault:
169                         err = regmap_read(fan->regmap, tacho->reg, &regval);
170                         if (err)
171                                 return err;
172
173                         *val = MLXREG_FAN_GET_FAULT(regval, tacho->mask);
174                         break;
175
176                 default:
177                         return -EOPNOTSUPP;
178                 }
179                 break;
180
181         case hwmon_pwm:
182                 pwm = &fan->pwm[channel];
183                 switch (attr) {
184                 case hwmon_pwm_input:
185                         err = regmap_read(fan->regmap, pwm->reg, &regval);
186                         if (err)
187                                 return err;
188
189                         *val = regval;
190                         break;
191
192                 default:
193                         return -EOPNOTSUPP;
194                 }
195                 break;
196
197         default:
198                 return -EOPNOTSUPP;
199         }
200
201         return 0;
202 }
203
204 static int
205 mlxreg_fan_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
206                  int channel, long val)
207 {
208         struct mlxreg_fan *fan = dev_get_drvdata(dev);
209         struct mlxreg_fan_pwm *pwm;
210
211         switch (type) {
212         case hwmon_pwm:
213                 switch (attr) {
214                 case hwmon_pwm_input:
215                         if (val < MLXREG_FAN_MIN_DUTY ||
216                             val > MLXREG_FAN_MAX_DUTY)
217                                 return -EINVAL;
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);
222                                 /*
223                                  * Update PWM only in case requested state is not less than the
224                                  * last thermal state.
225                                  */
226                                 if (pwm->last_hwmon_state >= pwm->last_thermal_state)
227                                         return mlxreg_fan_set_cur_state(pwm->cdev,
228                                                                         pwm->last_hwmon_state);
229                                 return 0;
230                         }
231                         return regmap_write(fan->regmap, pwm->reg, val);
232                 default:
233                         return -EOPNOTSUPP;
234                 }
235                 break;
236
237         default:
238                 return -EOPNOTSUPP;
239         }
240
241         return -EOPNOTSUPP;
242 }
243
244 static umode_t
245 mlxreg_fan_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
246                       int channel)
247 {
248         switch (type) {
249         case hwmon_fan:
250                 if (!(((struct mlxreg_fan *)data)->tacho[channel].connected))
251                         return 0;
252
253                 switch (attr) {
254                 case hwmon_fan_input:
255                 case hwmon_fan_fault:
256                         return 0444;
257                 default:
258                         break;
259                 }
260                 break;
261
262         case hwmon_pwm:
263                 if (!(((struct mlxreg_fan *)data)->pwm[channel].connected))
264                         return 0;
265
266                 switch (attr) {
267                 case hwmon_pwm_input:
268                         return 0644;
269                 default:
270                         break;
271                 }
272                 break;
273
274         default:
275                 break;
276         }
277
278         return 0;
279 }
280
281 static char *mlxreg_fan_name[] = {
282         "mlxreg_fan",
283         "mlxreg_fan1",
284         "mlxreg_fan2",
285         "mlxreg_fan3",
286 };
287
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,
305                            HWMON_PWM_INPUT,
306                            HWMON_PWM_INPUT,
307                            HWMON_PWM_INPUT,
308                            HWMON_PWM_INPUT),
309         NULL
310 };
311
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,
316 };
317
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,
321 };
322
323 static int mlxreg_fan_get_max_state(struct thermal_cooling_device *cdev,
324                                     unsigned long *state)
325 {
326         *state = MLXREG_FAN_MAX_STATE;
327         return 0;
328 }
329
330 static int mlxreg_fan_get_cur_state(struct thermal_cooling_device *cdev,
331                                     unsigned long *state)
332
333 {
334         struct mlxreg_fan_pwm *pwm = cdev->devdata;
335         struct mlxreg_fan *fan = pwm->fan;
336         u32 regval;
337         int err;
338
339         err = regmap_read(fan->regmap, pwm->reg, &regval);
340         if (err) {
341                 dev_err(fan->dev, "Failed to query PWM duty\n");
342                 return err;
343         }
344
345         *state = MLXREG_FAN_PWM_DUTY2STATE(regval);
346
347         return 0;
348 }
349
350 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
351                                     unsigned long state)
352
353 {
354         struct mlxreg_fan_pwm *pwm = cdev->devdata;
355         struct mlxreg_fan *fan = pwm->fan;
356         int err;
357
358         if (state > MLXREG_FAN_MAX_STATE)
359                 return -EINVAL;
360
361         /* Save thermal state. */
362         pwm->last_thermal_state = state;
363
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));
367         if (err) {
368                 dev_err(fan->dev, "Failed to write PWM duty\n");
369                 return err;
370         }
371         return 0;
372 }
373
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,
378 };
379
380 static int mlxreg_fan_connect_verify(struct mlxreg_fan *fan,
381                                      struct mlxreg_core_data *data)
382 {
383         u32 regval;
384         int err;
385
386         err = regmap_read(fan->regmap, data->capability, &regval);
387         if (err) {
388                 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
389                         data->capability);
390                 return err;
391         }
392
393         return !!(regval & data->bit);
394 }
395
396 static int mlxreg_pwm_connect_verify(struct mlxreg_fan *fan,
397                                      struct mlxreg_core_data *data)
398 {
399         u32 regval;
400         int err;
401
402         err = regmap_read(fan->regmap, data->reg, &regval);
403         if (err) {
404                 dev_err(fan->dev, "Failed to query pwm register 0x%08x\n",
405                         data->reg);
406                 return err;
407         }
408
409         return regval != MLXREG_FAN_PWM_NOT_CONNECTED;
410 }
411
412 static int mlxreg_fan_speed_divider_get(struct mlxreg_fan *fan,
413                                         struct mlxreg_core_data *data)
414 {
415         u32 regval;
416         int err;
417
418         err = regmap_read(fan->regmap, data->capability, &regval);
419         if (err) {
420                 dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
421                         data->capability);
422                 return err;
423         }
424
425         /*
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.
430          */
431         if (regval > 0 && regval <= MLXREG_FAN_TACHO_DIV_SCALE_MAX)
432                 fan->divider = regval * MLXREG_FAN_TACHO_DIV_MIN;
433
434         return 0;
435 }
436
437 static int mlxreg_fan_config(struct mlxreg_fan *fan,
438                              struct mlxreg_core_platform_data *pdata)
439 {
440         int tacho_num = 0, tacho_avail = 0, pwm_num = 0, i;
441         struct mlxreg_core_data *data = pdata->data;
442         bool configured = false;
443         int err;
444
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",
451                                         data->label);
452                                 return -EINVAL;
453                         }
454
455                         if (data->capability) {
456                                 err = mlxreg_fan_connect_verify(fan, data);
457                                 if (err < 0)
458                                         return err;
459                                 else if (!err) {
460                                         tacho_num++;
461                                         continue;
462                                 }
463                         }
464
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;
469                         tacho_avail++;
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",
473                                         data->label);
474                                 return -EINVAL;
475                         }
476
477                         /* Validate if more then one PWM is connected. */
478                         if (pwm_num) {
479                                 err = mlxreg_pwm_connect_verify(fan, data);
480                                 if (err < 0)
481                                         return err;
482                                 else if (!err)
483                                         continue;
484                         }
485
486                         fan->pwm[pwm_num].reg = data->reg;
487                         fan->pwm[pwm_num].connected = true;
488                         pwm_num++;
489                 } else if (strnstr(data->label, "conf", sizeof(data->label))) {
490                         if (configured) {
491                                 dev_err(fan->dev, "duplicate conf entry: %s\n",
492                                         data->label);
493                                 return -EINVAL;
494                         }
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",
498                                         data->label);
499                                 return -EINVAL;
500                         }
501                         if (data->capability) {
502                                 err = mlxreg_fan_speed_divider_get(fan, data);
503                                 if (err)
504                                         return err;
505                         } else {
506                                 if (data->mask)
507                                         fan->samples = data->mask;
508                                 if (data->bit)
509                                         fan->divider = data->bit;
510                         }
511                         configured = true;
512                 } else {
513                         dev_err(fan->dev, "invalid label: %s\n", data->label);
514                         return -EINVAL;
515                 }
516         }
517
518         if (pdata->capability) {
519                 int drwr_avail;
520                 u32 regval;
521
522                 /* Obtain the number of FAN drawers, supported by system. */
523                 err = regmap_read(fan->regmap, pdata->capability, &regval);
524                 if (err) {
525                         dev_err(fan->dev, "Failed to query capability register 0x%08x\n",
526                                 pdata->capability);
527                         return err;
528                 }
529
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);
534                         return -EINVAL;
535                 }
536
537                 /* Set the number of tachometers per one drawer. */
538                 fan->tachos_per_drwr = tacho_avail / drwr_avail;
539         }
540
541         return 0;
542 }
543
544 static int mlxreg_fan_cooling_config(struct device *dev, struct mlxreg_fan *fan)
545 {
546         int i;
547
548         for (i = 0; i < MLXREG_FAN_MAX_PWM; i++) {
549                 struct mlxreg_fan_pwm *pwm = &fan->pwm[i];
550
551                 if (!pwm->connected)
552                         continue;
553                 pwm->fan = fan;
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);
559                 }
560
561                 /* Set minimal PWM speed. */
562                 pwm->last_hwmon_state = MLXREG_FAN_PWM_DUTY2STATE(MLXREG_FAN_MIN_DUTY);
563         }
564
565         return 0;
566 }
567
568 static int mlxreg_fan_probe(struct platform_device *pdev)
569 {
570         struct mlxreg_core_platform_data *pdata;
571         struct device *dev = &pdev->dev;
572         struct mlxreg_fan *fan;
573         struct device *hwm;
574         int err;
575
576         pdata = dev_get_platdata(dev);
577         if (!pdata) {
578                 dev_err(dev, "Failed to get platform data.\n");
579                 return -EINVAL;
580         }
581
582         fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
583         if (!fan)
584                 return -ENOMEM;
585
586         fan->dev = dev;
587         fan->regmap = pdata->regmap;
588
589         err = mlxreg_fan_config(fan, pdata);
590         if (err)
591                 return err;
592
593         hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
594                                                    fan,
595                                                    &mlxreg_fan_hwmon_chip_info,
596                                                    NULL);
597         if (IS_ERR(hwm)) {
598                 dev_err(dev, "Failed to register hwmon device\n");
599                 return PTR_ERR(hwm);
600         }
601
602         if (IS_REACHABLE(CONFIG_THERMAL))
603                 err = mlxreg_fan_cooling_config(dev, fan);
604
605         return err;
606 }
607
608 static struct platform_driver mlxreg_fan_driver = {
609         .driver = {
610             .name = "mlxreg-fan",
611         },
612         .probe = mlxreg_fan_probe,
613 };
614
615 module_platform_driver(mlxreg_fan_driver);
616
617 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
618 MODULE_DESCRIPTION("Mellanox FAN driver");
619 MODULE_LICENSE("GPL");
620 MODULE_ALIAS("platform:mlxreg-fan");