patches-5.15.92-rt57
[platform/kernel/linux-rpi.git] / drivers / hwmon / emc2305.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Hardware monitoring driver for Microchip EMC2305 fan controller
4  *
5  * Copyright (C) 2022 Nvidia Technologies Ltd and Delta Networks, Inc.
6  */
7
8 #include <linux/err.h>
9 #include <linux/hwmon.h>
10 #include <linux/hwmon-sysfs.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/thermal.h>
15 #include <linux/version.h>
16
17 static const unsigned short
18 emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END };
19
20 #define EMC2305_REG_FAN_STATUS          0x24
21 #define EMC2305_REG_FAN_STALL_STATUS    0x25
22 #define EMC2305_REG_DRIVE_FAIL_STATUS   0x27
23 #define EMC2305_REG_VENDOR              0xfe
24 #define EMC2305_FAN_MAX                 0xff
25 #define EMC2305_FAN_MIN                 0x00
26 #define EMC2305_FAN_MAX_STATE           10
27 #define EMC2305_VENDOR                  0x5d
28 #define EMC2305_REG_PRODUCT_ID          0xfd
29 #define EMC2305_TACH_REGS_UNUSE_BITS    3
30 #define EMC2305_TACH_CNT_MULTIPLIER     1       /* Set by FAN_CFG RNGx bits */
31 #define EMC2305_PWM_MAX                 5
32 #define EMC2305_PWM_CHNL_CMN            0
33
34 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
35         (DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max)))
36 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
37         (DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state)))
38
39 /* Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
40  * equal (poles * 2 + 1).
41  */
42 #define EMC2305_RPM_FACTOR              3932160
43
44 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
45 #define EMC2305_REG_FAN_CFG(n) (0x32 + 0x10 * (n))
46 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
47 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
48
49 enum emc230x_product_id {
50         EMC2305 = 0x34,
51         EMC2303 = 0x35,
52         EMC2302 = 0x36,
53         EMC2301 = 0x37,
54 };
55
56 static const struct i2c_device_id emc2305_ids[] = {
57         { "emc2305", 0 },
58         { "emc2303", 0 },
59         { "emc2302", 0 },
60         { "emc2301", 0 },
61         { }
62 };
63 MODULE_DEVICE_TABLE(i2c, emc2305_ids);
64
65 static const struct of_device_id emc2305_dt_ids[] = {
66         { .compatible = "microchip,emc2305" },
67         { .compatible = "microchip,emc2301" },
68         { }
69 };
70 MODULE_DEVICE_TABLE(of, emc2305_dt_ids);
71
72 /**
73  * @cdev: cooling device;
74  * @curr_state: cooling current state;
75  * @last_hwmon_state: last cooling state updated by hwmon subsystem;
76  * @last_thermal_state: last cooling state updated by thermal subsystem;
77  *
78  * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
79  * speed feature. The purpose of this feature is to provides ability to limit fan speed
80  * according to some system wise considerations, like absence of some replaceable units (PSU or
81  * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
82  * some other factors which indirectly impacts system's airflow
83  * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
84  * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
85  * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
86  * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
87  * attribute.
88  * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
89  * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
90  * speed will be just stored with no PWM update.
91  */
92 struct emc2305_cdev_data {
93         struct thermal_cooling_device *cdev;
94         unsigned int cur_state;
95         unsigned long last_hwmon_state;
96         unsigned long last_thermal_state;
97 };
98
99 /**
100  * @client: i2c client;
101  * @hwmon_dev: hwmon device;
102  * @max_state: maximum cooling state of the cooling device;
103  * @pwm_max: maximum PWM;
104  * @pwm_min: minimum PWM;
105  * @pwm_channel: maximum number of PWM channels;
106  * @cdev_data: array of cooling devices data;
107  */
108 struct emc2305_data {
109         struct i2c_client *client;
110         struct device *hwmon_dev;
111         u8 max_state;
112         u8 pwm_max;
113         u8 pwm_min;
114         u8 pwm_num;
115         u8 pwm_channel;
116         struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
117 };
118
119 static char *emc2305_fan_name[] = {
120         "emc2305_fan",
121         "emc2305_fan1",
122         "emc2305_fan2",
123         "emc2305_fan3",
124         "emc2305_fan4",
125         "emc2305_fan5",
126 };
127
128 static int emc2305_get_max_channel(struct emc2305_data *data)
129 {
130         if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
131                 return data->pwm_num;
132         else
133                 return data->pwm_channel;
134 }
135
136 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
137 {
138         struct emc2305_data *data = cdev->devdata;
139         size_t len = strlen(cdev->type);
140         int ret;
141
142         if (len <= 0)
143                 return -EINVAL;
144
145         /* Retuns index of cooling device 0..4 in case of separate PWM setting.
146          * Zero index is used in case of one common PWM setting.
147          * If the mode is set as EMC2305_PWM_CHNL_CMN, all PWMs are to be bound
148          * to the common thermal zone and should work at the same speed
149          * to perform cooling for the same thermal junction.
150          * Otherwise, return specific channel that will be used in bound
151          * related PWM to the thermal zone.
152          */
153         if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
154                 return 0;
155
156         ret = cdev->type[len - 1];
157         switch (ret) {
158         case '1' ... '5':
159                 return ret - '1';
160         default:
161                 break;
162         }
163         return -EINVAL;
164 }
165
166 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
167 {
168         int cdev_idx;
169         struct emc2305_data *data = cdev->devdata;
170
171         cdev_idx = emc2305_get_cdev_idx(cdev);
172         if (cdev_idx < 0)
173                 return cdev_idx;
174
175         *state = data->cdev_data[cdev_idx].cur_state;
176         return 0;
177 }
178
179 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
180 {
181         struct emc2305_data *data = cdev->devdata;
182         *state = data->max_state;
183         return 0;
184 }
185
186 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
187 {
188         int cdev_idx;
189         struct emc2305_data *data = cdev->devdata;
190         struct i2c_client *client = data->client;
191         u8 val, i;
192
193         if (state > data->max_state)
194                 return -EINVAL;
195
196         cdev_idx =  emc2305_get_cdev_idx(cdev);
197         if (cdev_idx < 0)
198                 return cdev_idx;
199
200         /* Save thermal state. */
201         data->cdev_data[cdev_idx].last_thermal_state = state;
202         state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
203
204         val = EMC2305_PWM_STATE2DUTY(state, data->max_state, data->pwm_max);
205
206         if (val > EMC2305_FAN_MAX)
207                 return -EINVAL;
208
209         data->cdev_data[cdev_idx].cur_state = state;
210         if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
211         /* Set the same PWM value in all channels
212          * if common PWM channel is used.
213          */
214                 for (i = 0; i < data->pwm_num; i++)
215                         i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
216         else
217                 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
218
219         return 0;
220 }
221
222 static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
223         .get_max_state = emc2305_get_max_state,
224         .get_cur_state = emc2305_get_cur_state,
225         .set_cur_state = emc2305_set_cur_state,
226 };
227
228 static int emc2305_show_fault(struct device *dev, int channel)
229 {
230         struct emc2305_data *data = dev_get_drvdata(dev);
231         struct i2c_client *client = data->client;
232         int status_reg;
233
234         status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
235         return status_reg & (1 << channel) ? 1 : 0;
236 }
237
238 static int emc2305_show_fan(struct device *dev, int channel)
239 {
240         struct emc2305_data *data = dev_get_drvdata(dev);
241         struct i2c_client *client = data->client;
242         int ret;
243
244         ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
245         if (ret < 0)
246                 return ret;
247
248         ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
249         return EMC2305_RPM_FACTOR * EMC2305_TACH_CNT_MULTIPLIER / (ret > 0 ? ret : 1);
250 }
251
252 static int emc2305_show_pwm(struct device *dev, int channel)
253 {
254         struct emc2305_data *data = dev_get_drvdata(dev);
255         struct i2c_client *client = data->client;
256
257         return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
258 }
259
260 static int emc2305_set_pwm(struct device *dev, long val, int channel)
261 {
262         struct emc2305_data *data = dev_get_drvdata(dev);
263         struct i2c_client *client = data->client;
264
265         if (val < data->pwm_min || val > data->pwm_max)
266                 return -EINVAL;
267
268         i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
269         data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
270                                                                     data->pwm_max);
271         return 0;
272 }
273
274 static int emc2305_get_tz_of(struct device *dev)
275 {
276         struct device_node *np = dev->of_node;
277         struct emc2305_data *data = dev_get_drvdata(dev);
278         int ret = 0;
279
280         /* OF parameters are optional - overwrite default setting
281          * if some of them are provided.
282          */
283
284         if (of_find_property(np, "emc2305,cooling-levels", NULL)) {
285                 ret = of_property_read_u8(np, "emc2305,cooling-levels", &data->max_state);
286                 if (ret)
287                         return ret;
288         }
289
290         if (of_find_property(np, "emc2305,pwm-max", NULL)) {
291                 ret = of_property_read_u8(np, "emc2305,pwm-max", &data->pwm_max);
292                 if (ret)
293                         return ret;
294         }
295
296         if (of_find_property(np, "emc2305,pwm-min", NULL)) {
297                 ret = of_property_read_u8(np, "emc2305,pwm-min", &data->pwm_min);
298                 if (ret)
299                         return ret;
300         }
301
302         /* Not defined or 0 means one thermal zone over all cooling devices.
303          * Otherwise - separated thermal zones for each PWM channel.
304          */
305         if (of_find_property(np, "emc2305,pwm-channel", NULL)) {
306                 ret = of_property_read_u8(np, "emc2305,pwm-channel", &data->pwm_channel);
307                 if (ret)
308                         return ret;
309         }
310
311         return ret;
312 }
313
314 static int emc2305_set_single_tz(struct device *dev, int idx)
315 {
316         struct emc2305_data *data = dev_get_drvdata(dev);
317         long pwm = data->pwm_max;
318         int cdev_idx;
319
320         cdev_idx = (idx) ? idx - 1 : 0;
321
322         if (dev->of_node)
323                 data->cdev_data[cdev_idx].cdev =
324                         devm_thermal_of_cooling_device_register(dev, dev->of_node,
325                                                                 emc2305_fan_name[idx], data,
326                                                                 &emc2305_cooling_ops);
327         else
328                 data->cdev_data[cdev_idx].cdev =
329                         thermal_cooling_device_register(emc2305_fan_name[idx], data,
330                                                         &emc2305_cooling_ops);
331
332         if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
333                 dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
334                 return PTR_ERR(data->cdev_data[cdev_idx].cdev);
335         }
336         emc2305_set_pwm(dev, pwm, cdev_idx);
337         data->cdev_data[cdev_idx].cur_state = data->max_state;
338         /* Set minimal PWM speed. */
339         data->cdev_data[cdev_idx].last_hwmon_state = EMC2305_PWM_DUTY2STATE(data->pwm_min,
340                                                                             data->max_state,
341                                                                             data->pwm_max);
342         return 0;
343 }
344
345 static void emc2305_unset_tz(struct device *dev)
346 {
347         struct emc2305_data *data = dev_get_drvdata(dev);
348         int i;
349
350         /* Unregister cooling device in case they have been registered by
351          * thermal_cooling_device_unregister(). No need for clean-up flow in case they
352          * have been registered by devm_thermal_of_cooling_device_register()
353          */
354         if (!dev->of_node) {
355                 for (i = 0; i < EMC2305_PWM_MAX; i++)
356                         if (data->cdev_data[i].cdev)
357                                 thermal_cooling_device_unregister(data->cdev_data[i].cdev);
358         }
359 }
360
361 static int emc2305_set_tz(struct device *dev)
362 {
363         struct emc2305_data *data = dev_get_drvdata(dev);
364         int i, ret;
365
366         if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
367                 return emc2305_set_single_tz(dev, 0);
368
369         for (i = 0; i < data->pwm_channel; i++) {
370                 ret = emc2305_set_single_tz(dev, i + 1);
371                 if (ret)
372                         goto thermal_cooling_device_register_fail;
373         }
374         return 0;
375
376 thermal_cooling_device_register_fail:
377         emc2305_unset_tz(dev);
378         return ret;
379 }
380
381 static umode_t
382 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
383 {
384         int max_channel = emc2305_get_max_channel((struct emc2305_data *)data);
385
386         /* Don't show channels which are not physically connected. */
387         if ((channel + 1) > max_channel)
388                 return 0;
389         switch (type) {
390         case hwmon_fan:
391                 switch (attr) {
392                 case hwmon_fan_input:
393                         return 0444;
394                 case hwmon_fan_fault:
395                         return 0444;
396                 default:
397                         break;
398                 }
399                 break;
400         case hwmon_pwm:
401                 switch (attr) {
402                 case hwmon_pwm_input:
403                         return 0644;
404                 default:
405                         break;
406                 }
407                 break;
408         default:
409                 break;
410         }
411
412         return 0;
413 };
414
415 static int
416 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
417 {
418         struct emc2305_data *data = dev_get_drvdata(dev);
419
420         switch (type) {
421         case hwmon_pwm:
422                 switch (attr) {
423                 case hwmon_pwm_input:
424                         /* If thermal is configured - handle PWM limit setting. */
425                         if (IS_REACHABLE(CONFIG_THERMAL)) {
426                                 data->cdev_data[channel].last_hwmon_state =
427                                         EMC2305_PWM_DUTY2STATE(val, data->max_state, data->pwm_max);
428                                 /* Update PWM only in case requested state is not less than the
429                                  * last thermal state.
430                                  */
431                                 if (data->cdev_data[channel].last_hwmon_state >=
432                                     data->cdev_data[channel].last_thermal_state)
433                                         return emc2305_set_cur_state(data->cdev_data[channel].cdev,
434                                                         data->cdev_data[channel].last_hwmon_state);
435                                 return 0;
436                         }
437                         return emc2305_set_pwm(dev, val, channel);
438                 default:
439                         break;
440                 }
441                 break;
442         default:
443                 break;
444         }
445
446         return -EOPNOTSUPP;
447 };
448
449 static int
450 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
451 {
452         int ret;
453
454         switch (type) {
455         case hwmon_fan:
456                 switch (attr) {
457                 case hwmon_fan_input:
458                         ret = emc2305_show_fan(dev, channel);
459                         if (ret < 0)
460                                 return ret;
461                         *val = ret;
462                         return 0;
463                 case hwmon_fan_fault:
464                         ret = emc2305_show_fault(dev, channel);
465                         if (ret < 0)
466                                 return ret;
467                         *val = ret;
468                         return 0;
469                 default:
470                         break;
471                 }
472                 break;
473         case hwmon_pwm:
474                 switch (attr) {
475                 case hwmon_pwm_input:
476                         ret = emc2305_show_pwm(dev, channel);
477                         if (ret < 0)
478                                 return ret;
479                         *val = ret;
480                         return 0;
481                 default:
482                         break;
483                 }
484                 break;
485         default:
486                 break;
487         }
488
489         return -EOPNOTSUPP;
490 };
491
492 static const struct hwmon_ops emc2305_ops = {
493         .is_visible = emc2305_is_visible,
494         .read = emc2305_read,
495         .write = emc2305_write,
496 };
497
498 static const struct hwmon_channel_info *emc2305_info[] = {
499         HWMON_CHANNEL_INFO(fan,
500                            HWMON_F_INPUT | HWMON_F_FAULT,
501                            HWMON_F_INPUT | HWMON_F_FAULT,
502                            HWMON_F_INPUT | HWMON_F_FAULT,
503                            HWMON_F_INPUT | HWMON_F_FAULT,
504                            HWMON_F_INPUT | HWMON_F_FAULT),
505         HWMON_CHANNEL_INFO(pwm,
506                            HWMON_PWM_INPUT,
507                            HWMON_PWM_INPUT,
508                            HWMON_PWM_INPUT,
509                            HWMON_PWM_INPUT,
510                            HWMON_PWM_INPUT),
511         NULL
512 };
513
514 static const struct hwmon_chip_info emc2305_chip_info = {
515         .ops = &emc2305_ops,
516         .info = emc2305_info,
517 };
518
519 static int emc2305_identify(struct device *dev)
520 {
521         struct i2c_client *client = to_i2c_client(dev);
522         struct emc2305_data *data = i2c_get_clientdata(client);
523         int ret;
524
525         ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
526         if (ret < 0)
527                 return ret;
528
529         switch (ret) {
530         case EMC2305:
531                 data->pwm_num = 5;
532                 break;
533         case EMC2303:
534                 data->pwm_num = 3;
535                 break;
536         case EMC2302:
537                 data->pwm_num = 2;
538                 break;
539         case EMC2301:
540                 data->pwm_num = 1;
541                 break;
542         default:
543                 return -EINVAL;
544         }
545
546         return 0;
547 }
548
549 static int emc2305_probe(struct i2c_client *client, const struct i2c_device_id *id)
550 {
551         struct i2c_adapter *adapter = client->adapter;
552         struct device *dev = &client->dev;
553         struct emc2305_data *data;
554         int vendor;
555         int ret;
556         int i;
557
558         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
559                 return -ENODEV;
560
561         vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
562         if (vendor != EMC2305_VENDOR)
563                 return -ENODEV;
564
565         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
566         if (!data)
567                 return -ENOMEM;
568
569         i2c_set_clientdata(client, data);
570         data->client = client;
571
572         ret = emc2305_identify(dev);
573         if (ret)
574                 return ret;
575
576         data->max_state = EMC2305_FAN_MAX_STATE;
577         data->pwm_max = EMC2305_FAN_MAX;
578         data->pwm_min = EMC2305_FAN_MIN;
579         data->pwm_channel = EMC2305_PWM_CHNL_CMN;
580         if (dev->of_node) {
581                 ret = emc2305_get_tz_of(dev);
582                 if (ret < 0)
583                         return ret;
584         }
585
586         data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
587                                                                &emc2305_chip_info, NULL);
588         if (IS_ERR(data->hwmon_dev))
589                 return PTR_ERR(data->hwmon_dev);
590
591         if (IS_REACHABLE(CONFIG_THERMAL)) {
592                 ret = emc2305_set_tz(dev);
593                 if (ret != 0)
594                         return ret;
595         }
596
597         for (i = 0; i < data->pwm_num; i++) {
598                 u8 val;
599
600                 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
601                                           data->pwm_min);
602
603                 val = i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_CFG(i));
604                 /* Set RNGx so minRPM=500 */
605                 val &= ~0x60;
606                 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_CFG(i), val);
607         }
608
609         /* Acknowledge any existing faults. Stops the device responding on the
610          * SMBus alert address.
611          */
612         i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_STALL_STATUS);
613         i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_STATUS);
614
615         return 0;
616 }
617
618 static int emc2305_remove(struct i2c_client *client)
619 {
620         struct device *dev = &client->dev;
621
622         if (IS_REACHABLE(CONFIG_THERMAL))
623                 emc2305_unset_tz(dev);
624         return 0;
625 }
626
627 static struct i2c_driver emc2305_driver = {
628         .class  = I2C_CLASS_HWMON,
629         .driver = {
630                 .name = "emc2305",
631                 .of_match_table = emc2305_dt_ids,
632         },
633         .probe    = emc2305_probe,
634         .remove   = emc2305_remove,
635         .id_table = emc2305_ids,
636         .address_list = emc2305_normal_i2c,
637 };
638
639 module_i2c_driver(emc2305_driver);
640
641 MODULE_AUTHOR("Nvidia");
642 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
643 MODULE_LICENSE("GPL");