Merge tag 'spi-fix-v5.15-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/brooni...
[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            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         int i, config = 0;
319         u32 regval;
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                 /*
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;
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)
352                         return config;
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         }
368         return config;
369 }
370
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,
375 };
376
377 static 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
393 static 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
418 static int mlxreg_fan_config(struct mlxreg_fan *fan,
419                              struct mlxreg_core_platform_data *pdata)
420 {
421         struct mlxreg_core_data *data = pdata->data;
422         int tacho_num = 0, tacho_avail = 0, i;
423         bool configured = false;
424         int err;
425
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",
432                                         data->label);
433                                 return -EINVAL;
434                         }
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
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;
450                         tacho_avail++;
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. */
466                         if (!data->mask && !data->bit && !data->capability) {
467                                 dev_err(fan->dev, "invalid conf entry params: %s\n",
468                                         data->label);
469                                 return -EINVAL;
470                         }
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                         }
481                         configured = true;
482                 } else {
483                         dev_err(fan->dev, "invalid label: %s\n", data->label);
484                         return -EINVAL;
485                 }
486         }
487
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
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
520 static int mlxreg_fan_probe(struct platform_device *pdev)
521 {
522         struct mlxreg_core_platform_data *pdata;
523         struct device *dev = &pdev->dev;
524         struct mlxreg_fan *fan;
525         struct device *hwm;
526         int err;
527
528         pdata = dev_get_platdata(dev);
529         if (!pdata) {
530                 dev_err(dev, "Failed to get platform data.\n");
531                 return -EINVAL;
532         }
533
534         fan = devm_kzalloc(dev, sizeof(*fan), GFP_KERNEL);
535         if (!fan)
536                 return -ENOMEM;
537
538         fan->dev = dev;
539         fan->regmap = pdata->regmap;
540
541         err = mlxreg_fan_config(fan, pdata);
542         if (err)
543                 return err;
544
545         hwm = devm_hwmon_device_register_with_info(dev, "mlxreg_fan",
546                                                    fan,
547                                                    &mlxreg_fan_hwmon_chip_info,
548                                                    NULL);
549         if (IS_ERR(hwm)) {
550                 dev_err(dev, "Failed to register hwmon device\n");
551                 return PTR_ERR(hwm);
552         }
553
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);
560                 }
561         }
562
563         return 0;
564 }
565
566 static struct platform_driver mlxreg_fan_driver = {
567         .driver = {
568             .name = "mlxreg-fan",
569         },
570         .probe = mlxreg_fan_probe,
571 };
572
573 module_platform_driver(mlxreg_fan_driver);
574
575 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
576 MODULE_DESCRIPTION("Mellanox FAN driver");
577 MODULE_LICENSE("GPL");
578 MODULE_ALIAS("platform:mlxreg-fan");