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