GNU Linux-libre 4.19.263-gnu1
[releases.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_DIVIDER_DEF            1132
31 /*
32  * FAN datasheet defines the formula for RPM calculations as RPM = 15/t-high.
33  * The logic in a programmable device measures the time t-high by sampling the
34  * tachometer every t-sample (with the default value 11.32 uS) and increment
35  * a counter (N) as long as the pulse has not change:
36  * RPM = 15 / (t-sample * (K + Regval)), where:
37  * Regval: is the value read from the programmable device register;
38  *  - 0xff - represents tachometer fault;
39  *  - 0xfe - represents tachometer minimum value , which is 4444 RPM;
40  *  - 0x00 - represents tachometer maximum value , which is 300000 RPM;
41  * K: is 44 and it represents the minimum allowed samples per pulse;
42  * N: is equal K + Regval;
43  * In order to calculate RPM from the register value the following formula is
44  * used: RPM = 15 / ((Regval + K) * 11.32) * 10^(-6)), which in  the
45  * default case is modified to:
46  * RPM = 15000000 * 100 / ((Regval + 44) * 1132);
47  * - for Regval 0x00, RPM will be 15000000 * 100 / (44 * 1132) = 30115;
48  * - for Regval 0xfe, RPM will be 15000000 * 100 / ((254 + 44) * 1132) = 4446;
49  * In common case the formula is modified to:
50  * RPM = 15000000 * 100 / ((Regval + samples) * divider).
51  */
52 #define MLXREG_FAN_GET_RPM(rval, d, s)  (DIV_ROUND_CLOSEST(15000000 * 100, \
53                                          ((rval) + (s)) * (d)))
54 #define MLXREG_FAN_GET_FAULT(val, mask) (!((val) ^ (mask)))
55 #define MLXREG_FAN_PWM_DUTY2STATE(duty) (DIV_ROUND_CLOSEST((duty) *     \
56                                          MLXREG_FAN_MAX_STATE,          \
57                                          MLXREG_FAN_MAX_DUTY))
58 #define MLXREG_FAN_PWM_STATE2DUTY(stat) (DIV_ROUND_CLOSEST((stat) *     \
59                                          MLXREG_FAN_MAX_DUTY,           \
60                                          MLXREG_FAN_MAX_STATE))
61
62 /*
63  * struct mlxreg_fan_tacho - tachometer data (internal use):
64  *
65  * @connected: indicates if tachometer is connected;
66  * @reg: register offset;
67  * @mask: fault mask;
68  */
69 struct mlxreg_fan_tacho {
70         bool connected;
71         u32 reg;
72         u32 mask;
73 };
74
75 /*
76  * struct mlxreg_fan_pwm - PWM data (internal use):
77  *
78  * @connected: indicates if PWM is connected;
79  * @reg: register offset;
80  */
81 struct mlxreg_fan_pwm {
82         bool connected;
83         u32 reg;
84 };
85
86 /*
87  * struct mlxreg_fan - private data (internal use):
88  *
89  * @dev: basic device;
90  * @regmap: register map of parent device;
91  * @tacho: tachometer data;
92  * @pwm: PWM data;
93  * @samples: minimum allowed samples per pulse;
94  * @divider: divider value for tachometer RPM calculation;
95  * @cooling: cooling device levels;
96  * @cdev: cooling device;
97  */
98 struct mlxreg_fan {
99         struct device *dev;
100         void *regmap;
101         struct mlxreg_core_platform_data *pdata;
102         struct mlxreg_fan_tacho tacho[MLXREG_FAN_MAX_TACHO];
103         struct mlxreg_fan_pwm pwm;
104         int samples;
105         int divider;
106         u8 cooling_levels[MLXREG_FAN_MAX_STATE + 1];
107         struct thermal_cooling_device *cdev;
108 };
109
110 static int
111 mlxreg_fan_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
112                 int channel, long *val)
113 {
114         struct mlxreg_fan *fan = dev_get_drvdata(dev);
115         struct mlxreg_fan_tacho *tacho;
116         u32 regval;
117         int err;
118
119         switch (type) {
120         case hwmon_fan:
121                 tacho = &fan->tacho[channel];
122                 switch (attr) {
123                 case hwmon_fan_input:
124                         err = regmap_read(fan->regmap, tacho->reg, &regval);
125                         if (err)
126                                 return err;
127
128                         *val = MLXREG_FAN_GET_RPM(regval, fan->divider,
129                                                   fan->samples);
130                         break;
131
132                 case hwmon_fan_fault:
133                         err = regmap_read(fan->regmap, tacho->reg, &regval);
134                         if (err)
135                                 return err;
136
137                         *val = MLXREG_FAN_GET_FAULT(regval, tacho->mask);
138                         break;
139
140                 default:
141                         return -EOPNOTSUPP;
142                 }
143                 break;
144
145         case hwmon_pwm:
146                 switch (attr) {
147                 case hwmon_pwm_input:
148                         err = regmap_read(fan->regmap, fan->pwm.reg, &regval);
149                         if (err)
150                                 return err;
151
152                         *val = regval;
153                         break;
154
155                 default:
156                         return -EOPNOTSUPP;
157                 }
158                 break;
159
160         default:
161                 return -EOPNOTSUPP;
162         }
163
164         return 0;
165 }
166
167 static int
168 mlxreg_fan_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
169                  int channel, long val)
170 {
171         struct mlxreg_fan *fan = dev_get_drvdata(dev);
172
173         switch (type) {
174         case hwmon_pwm:
175                 switch (attr) {
176                 case hwmon_pwm_input:
177                         if (val < MLXREG_FAN_MIN_DUTY ||
178                             val > MLXREG_FAN_MAX_DUTY)
179                                 return -EINVAL;
180                         return regmap_write(fan->regmap, fan->pwm.reg, val);
181                 default:
182                         return -EOPNOTSUPP;
183                 }
184                 break;
185
186         default:
187                 return -EOPNOTSUPP;
188         }
189
190         return -EOPNOTSUPP;
191 }
192
193 static umode_t
194 mlxreg_fan_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
195                       int channel)
196 {
197         switch (type) {
198         case hwmon_fan:
199                 if (!(((struct mlxreg_fan *)data)->tacho[channel].connected))
200                         return 0;
201
202                 switch (attr) {
203                 case hwmon_fan_input:
204                 case hwmon_fan_fault:
205                         return 0444;
206                 default:
207                         break;
208                 }
209                 break;
210
211         case hwmon_pwm:
212                 if (!(((struct mlxreg_fan *)data)->pwm.connected))
213                         return 0;
214
215                 switch (attr) {
216                 case hwmon_pwm_input:
217                         return 0644;
218                 default:
219                         break;
220                 }
221                 break;
222
223         default:
224                 break;
225         }
226
227         return 0;
228 }
229
230 static const u32 mlxreg_fan_hwmon_fan_config[] = {
231         HWMON_F_INPUT | HWMON_F_FAULT,
232         HWMON_F_INPUT | HWMON_F_FAULT,
233         HWMON_F_INPUT | HWMON_F_FAULT,
234         HWMON_F_INPUT | HWMON_F_FAULT,
235         HWMON_F_INPUT | HWMON_F_FAULT,
236         HWMON_F_INPUT | HWMON_F_FAULT,
237         HWMON_F_INPUT | HWMON_F_FAULT,
238         HWMON_F_INPUT | HWMON_F_FAULT,
239         HWMON_F_INPUT | HWMON_F_FAULT,
240         HWMON_F_INPUT | HWMON_F_FAULT,
241         HWMON_F_INPUT | HWMON_F_FAULT,
242         HWMON_F_INPUT | HWMON_F_FAULT,
243         0
244 };
245
246 static const struct hwmon_channel_info mlxreg_fan_hwmon_fan = {
247         .type = hwmon_fan,
248         .config = mlxreg_fan_hwmon_fan_config,
249 };
250
251 static const u32 mlxreg_fan_hwmon_pwm_config[] = {
252         HWMON_PWM_INPUT,
253         0
254 };
255
256 static const struct hwmon_channel_info mlxreg_fan_hwmon_pwm = {
257         .type = hwmon_pwm,
258         .config = mlxreg_fan_hwmon_pwm_config,
259 };
260
261 static const struct hwmon_channel_info *mlxreg_fan_hwmon_info[] = {
262         &mlxreg_fan_hwmon_fan,
263         &mlxreg_fan_hwmon_pwm,
264         NULL
265 };
266
267 static const struct hwmon_ops mlxreg_fan_hwmon_hwmon_ops = {
268         .is_visible = mlxreg_fan_is_visible,
269         .read = mlxreg_fan_read,
270         .write = mlxreg_fan_write,
271 };
272
273 static const struct hwmon_chip_info mlxreg_fan_hwmon_chip_info = {
274         .ops = &mlxreg_fan_hwmon_hwmon_ops,
275         .info = mlxreg_fan_hwmon_info,
276 };
277
278 static int mlxreg_fan_get_max_state(struct thermal_cooling_device *cdev,
279                                     unsigned long *state)
280 {
281         *state = MLXREG_FAN_MAX_STATE;
282         return 0;
283 }
284
285 static int mlxreg_fan_get_cur_state(struct thermal_cooling_device *cdev,
286                                     unsigned long *state)
287
288 {
289         struct mlxreg_fan *fan = cdev->devdata;
290         u32 regval;
291         int err;
292
293         err = regmap_read(fan->regmap, fan->pwm.reg, &regval);
294         if (err) {
295                 dev_err(fan->dev, "Failed to query PWM duty\n");
296                 return err;
297         }
298
299         *state = MLXREG_FAN_PWM_DUTY2STATE(regval);
300
301         return 0;
302 }
303
304 static int mlxreg_fan_set_cur_state(struct thermal_cooling_device *cdev,
305                                     unsigned long state)
306
307 {
308         struct mlxreg_fan *fan = cdev->devdata;
309         unsigned long cur_state;
310         int i, config = 0;
311         u32 regval;
312         int err;
313
314         /*
315          * Verify if this request is for changing allowed FAN dynamical
316          * minimum. If it is - update cooling levels accordingly and update
317          * state, if current state is below the newly requested minimum state.
318          * For example, if current state is 5, and minimal state is to be
319          * changed from 4 to 6, fan->cooling_levels[0 to 5] will be changed all
320          * from 4 to 6. And state 5 (fan->cooling_levels[4]) should be
321          * overwritten.
322          */
323         if (state >= MLXREG_FAN_SPEED_MIN && state <= MLXREG_FAN_SPEED_MAX) {
324                 /*
325                  * This is configuration change, which is only supported through sysfs.
326                  * For configuration non-zero value is to be returned to avoid thermal
327                  * statistics update.
328                  */
329                 config = 1;
330                 state -= MLXREG_FAN_MAX_STATE;
331                 for (i = 0; i < state; i++)
332                         fan->cooling_levels[i] = state;
333                 for (i = state; i <= MLXREG_FAN_MAX_STATE; i++)
334                         fan->cooling_levels[i] = i;
335
336                 err = regmap_read(fan->regmap, fan->pwm.reg, &regval);
337                 if (err) {
338                         dev_err(fan->dev, "Failed to query PWM duty\n");
339                         return err;
340                 }
341
342                 cur_state = MLXREG_FAN_PWM_DUTY2STATE(regval);
343                 if (state < cur_state)
344                         return config;
345
346                 state = cur_state;
347         }
348
349         if (state > MLXREG_FAN_MAX_STATE)
350                 return -EINVAL;
351
352         /* Normalize the state to the valid speed range. */
353         state = fan->cooling_levels[state];
354         err = regmap_write(fan->regmap, fan->pwm.reg,
355                            MLXREG_FAN_PWM_STATE2DUTY(state));
356         if (err) {
357                 dev_err(fan->dev, "Failed to write PWM duty\n");
358                 return err;
359         }
360         return config;
361 }
362
363 static const struct thermal_cooling_device_ops mlxreg_fan_cooling_ops = {
364         .get_max_state  = mlxreg_fan_get_max_state,
365         .get_cur_state  = mlxreg_fan_get_cur_state,
366         .set_cur_state  = mlxreg_fan_set_cur_state,
367 };
368
369 static int mlxreg_fan_config(struct mlxreg_fan *fan,
370                              struct mlxreg_core_platform_data *pdata)
371 {
372         struct mlxreg_core_data *data = pdata->data;
373         bool configured = false;
374         int tacho_num = 0, i;
375
376         fan->samples = MLXREG_FAN_TACHO_SAMPLES_PER_PULSE_DEF;
377         fan->divider = MLXREG_FAN_TACHO_DIVIDER_DEF;
378         for (i = 0; i < pdata->counter; i++, data++) {
379                 if (strnstr(data->label, "tacho", sizeof(data->label))) {
380                         if (tacho_num == MLXREG_FAN_MAX_TACHO) {
381                                 dev_err(fan->dev, "too many tacho entries: %s\n",
382                                         data->label);
383                                 return -EINVAL;
384                         }
385                         fan->tacho[tacho_num].reg = data->reg;
386                         fan->tacho[tacho_num].mask = data->mask;
387                         fan->tacho[tacho_num++].connected = true;
388                 } else if (strnstr(data->label, "pwm", sizeof(data->label))) {
389                         if (fan->pwm.connected) {
390                                 dev_err(fan->dev, "duplicate pwm entry: %s\n",
391                                         data->label);
392                                 return -EINVAL;
393                         }
394                         fan->pwm.reg = data->reg;
395                         fan->pwm.connected = true;
396                 } else if (strnstr(data->label, "conf", sizeof(data->label))) {
397                         if (configured) {
398                                 dev_err(fan->dev, "duplicate conf entry: %s\n",
399                                         data->label);
400                                 return -EINVAL;
401                         }
402                         /* Validate that conf parameters are not zeros. */
403                         if (!data->mask || !data->bit) {
404                                 dev_err(fan->dev, "invalid conf entry params: %s\n",
405                                         data->label);
406                                 return -EINVAL;
407                         }
408                         fan->samples = data->mask;
409                         fan->divider = data->bit;
410                         configured = true;
411                 } else {
412                         dev_err(fan->dev, "invalid label: %s\n", data->label);
413                         return -EINVAL;
414                 }
415         }
416
417         /* Init cooling levels per PWM state. */
418         for (i = 0; i < MLXREG_FAN_SPEED_MIN_LEVEL; i++)
419                 fan->cooling_levels[i] = MLXREG_FAN_SPEED_MIN_LEVEL;
420         for (i = MLXREG_FAN_SPEED_MIN_LEVEL; i <= MLXREG_FAN_MAX_STATE; i++)
421                 fan->cooling_levels[i] = i;
422
423         return 0;
424 }
425
426 static int mlxreg_fan_probe(struct platform_device *pdev)
427 {
428         struct mlxreg_core_platform_data *pdata;
429         struct mlxreg_fan *fan;
430         struct device *hwm;
431         int err;
432
433         pdata = dev_get_platdata(&pdev->dev);
434         if (!pdata) {
435                 dev_err(&pdev->dev, "Failed to get platform data.\n");
436                 return -EINVAL;
437         }
438
439         fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
440         if (!fan)
441                 return -ENOMEM;
442
443         fan->dev = &pdev->dev;
444         fan->regmap = pdata->regmap;
445         platform_set_drvdata(pdev, fan);
446
447         err = mlxreg_fan_config(fan, pdata);
448         if (err)
449                 return err;
450
451         hwm = devm_hwmon_device_register_with_info(&pdev->dev, "mlxreg_fan",
452                                                    fan,
453                                                    &mlxreg_fan_hwmon_chip_info,
454                                                    NULL);
455         if (IS_ERR(hwm)) {
456                 dev_err(&pdev->dev, "Failed to register hwmon device\n");
457                 return PTR_ERR(hwm);
458         }
459
460         if (IS_REACHABLE(CONFIG_THERMAL)) {
461                 fan->cdev = thermal_cooling_device_register("mlxreg_fan", fan,
462                                                 &mlxreg_fan_cooling_ops);
463                 if (IS_ERR(fan->cdev)) {
464                         dev_err(&pdev->dev, "Failed to register cooling device\n");
465                         return PTR_ERR(fan->cdev);
466                 }
467         }
468
469         return 0;
470 }
471
472 static int mlxreg_fan_remove(struct platform_device *pdev)
473 {
474         struct mlxreg_fan *fan = platform_get_drvdata(pdev);
475
476         if (IS_REACHABLE(CONFIG_THERMAL))
477                 thermal_cooling_device_unregister(fan->cdev);
478
479         return 0;
480 }
481
482 static struct platform_driver mlxreg_fan_driver = {
483         .driver = {
484             .name = "mlxreg-fan",
485         },
486         .probe = mlxreg_fan_probe,
487         .remove = mlxreg_fan_remove,
488 };
489
490 module_platform_driver(mlxreg_fan_driver);
491
492 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
493 MODULE_DESCRIPTION("Mellanox FAN driver");
494 MODULE_LICENSE("GPL");
495 MODULE_ALIAS("platform:mlxreg-fan");