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