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 #include <linux/pwm.h>
15 #include <linux/of_device.h>
16 #include <linux/util_macros.h>
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 #define EMC2305_DEFAULT_OUTPUT 0x0
30 #define EMC2305_DEFAULT_POLARITY 0x0
31 #define EMC2305_REG_POLARITY 0x2a
32 #define EMC2305_REG_DRIVE_PWM_OUT 0x2b
33 #define EMC2305_OPEN_DRAIN 0x0
34 #define EMC2305_PUSH_PULL 0x1
35
36 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
37 DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max))
38 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
39 DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state))
40
41 /*
42 * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
43 * equal (poles * 2 + 1).
44 */
45 #define EMC2305_RPM_FACTOR 3932160
46
47 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
48 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
49 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
50
51 /* Supported base PWM frequencies */
52 static const unsigned int base_freq_table[] = { 2441, 4882, 19530, 26000 };
53
54 enum emc230x_product_id {
55 EMC2305 = 0x34,
56 EMC2303 = 0x35,
57 EMC2302 = 0x36,
58 EMC2301 = 0x37,
59 };
60
61 static const struct i2c_device_id emc2305_ids[] = {
62 { "emc2305" },
63 { "emc2303" },
64 { "emc2302" },
65 { "emc2301" },
66 { }
67 };
68 MODULE_DEVICE_TABLE(i2c, emc2305_ids);
69
70 /**
71 * struct emc2305_cdev_data - device-specific cooling device state
72 * @cdev: cooling device
73 * @cur_state: cooling current state
74 * @last_hwmon_state: last cooling state updated by hwmon subsystem
75 * @last_thermal_state: last cooling state updated by thermal subsystem
76 *
77 * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
78 * speed feature. The purpose of this feature is to provides ability to limit fan speed
79 * according to some system wise considerations, like absence of some replaceable units (PSU or
80 * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
81 * some other factors which indirectly impacts system's airflow
82 * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
83 * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
84 * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
85 * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
86 * attribute.
87 * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
88 * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
89 * speed will be just stored with no PWM update.
90 */
91 struct emc2305_cdev_data {
92 struct thermal_cooling_device *cdev;
93 unsigned int cur_state;
94 unsigned long last_hwmon_state;
95 unsigned long last_thermal_state;
96 };
97
98 /**
99 * struct emc2305_data - device-specific data
100 * @client: i2c client
101 * @hwmon_dev: hwmon device
102 * @max_state: maximum cooling state of the cooling device
103 * @pwm_num: number of PWM channels
104 * @pwm_output_mask: PWM output mask
105 * @pwm_polarity_mask: PWM polarity mask
106 * @pwm_separate: separate PWM settings for every channel
107 * @pwm_min: array of minimum PWM per channel
108 * @pwm_freq: array of PWM frequency per channel
109 * @cdev_data: array of cooling devices data
110 */
111 struct emc2305_data {
112 struct i2c_client *client;
113 struct device *hwmon_dev;
114 u8 max_state;
115 u8 pwm_num;
116 u8 pwm_output_mask;
117 u8 pwm_polarity_mask;
118 bool pwm_separate;
119 u8 pwm_min[EMC2305_PWM_MAX];
120 u16 pwm_freq[EMC2305_PWM_MAX];
121 struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
122 };
123
124 static char *emc2305_fan_name[] = {
125 "emc2305_fan",
126 "emc2305_fan1",
127 "emc2305_fan2",
128 "emc2305_fan3",
129 "emc2305_fan4",
130 "emc2305_fan5",
131 };
132
emc2305_get_max_channel(const struct emc2305_data * data)133 static int emc2305_get_max_channel(const struct emc2305_data *data)
134 {
135 return data->pwm_num;
136 }
137
emc2305_get_cdev_idx(struct thermal_cooling_device * cdev)138 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
139 {
140 struct emc2305_data *data = cdev->devdata;
141 size_t len = strlen(cdev->type);
142 int ret;
143
144 if (len <= 0)
145 return -EINVAL;
146
147 /*
148 * Returns index of cooling device 0..4 in case of separate PWM setting.
149 * Zero index is used in case of one common PWM setting.
150 * If the mode is not set as pwm_separate, all PWMs are to be bound
151 * to the common thermal zone and should work at the same speed
152 * to perform cooling for the same thermal junction.
153 * Otherwise, return specific channel that will be used in bound
154 * related PWM to the thermal zone.
155 */
156 if (!data->pwm_separate)
157 return 0;
158
159 ret = cdev->type[len - 1];
160 switch (ret) {
161 case '1' ... '5':
162 return ret - '1';
163 default:
164 break;
165 }
166 return -EINVAL;
167 }
168
emc2305_get_cur_state(struct thermal_cooling_device * cdev,unsigned long * state)169 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
170 {
171 int cdev_idx;
172 struct emc2305_data *data = cdev->devdata;
173
174 cdev_idx = emc2305_get_cdev_idx(cdev);
175 if (cdev_idx < 0)
176 return cdev_idx;
177
178 *state = data->cdev_data[cdev_idx].cur_state;
179 return 0;
180 }
181
emc2305_get_max_state(struct thermal_cooling_device * cdev,unsigned long * state)182 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
183 {
184 struct emc2305_data *data = cdev->devdata;
185 *state = data->max_state;
186 return 0;
187 }
188
__emc2305_set_cur_state(struct emc2305_data * data,int cdev_idx,unsigned long state)189 static int __emc2305_set_cur_state(struct emc2305_data *data, int cdev_idx, unsigned long state)
190 {
191 int ret;
192 struct i2c_client *client = data->client;
193 u8 val, i;
194
195 state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
196
197 val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX);
198
199 data->cdev_data[cdev_idx].cur_state = state;
200 if (data->pwm_separate) {
201 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
202 if (ret < 0)
203 return ret;
204 } else {
205 /*
206 * Set the same PWM value in all channels
207 * if common PWM channel is used.
208 */
209 for (i = 0; i < data->pwm_num; i++) {
210 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
211 if (ret < 0)
212 return ret;
213 }
214 }
215
216 return 0;
217 }
218
emc2305_set_cur_state(struct thermal_cooling_device * cdev,unsigned long state)219 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
220 {
221 int cdev_idx, ret;
222 struct emc2305_data *data = cdev->devdata;
223
224 if (state > data->max_state)
225 return -EINVAL;
226
227 cdev_idx = emc2305_get_cdev_idx(cdev);
228 if (cdev_idx < 0)
229 return cdev_idx;
230
231 /* Save thermal state. */
232 data->cdev_data[cdev_idx].last_thermal_state = state;
233 ret = __emc2305_set_cur_state(data, cdev_idx, state);
234 if (ret < 0)
235 return ret;
236
237 return 0;
238 }
239
240 static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
241 .get_max_state = emc2305_get_max_state,
242 .get_cur_state = emc2305_get_cur_state,
243 .set_cur_state = emc2305_set_cur_state,
244 };
245
emc2305_show_fault(struct device * dev,int channel)246 static int emc2305_show_fault(struct device *dev, int channel)
247 {
248 struct emc2305_data *data = dev_get_drvdata(dev);
249 struct i2c_client *client = data->client;
250 int status_reg;
251
252 status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
253 if (status_reg < 0)
254 return status_reg;
255
256 return status_reg & (1 << channel) ? 1 : 0;
257 }
258
emc2305_show_fan(struct device * dev,int channel)259 static int emc2305_show_fan(struct device *dev, int channel)
260 {
261 struct emc2305_data *data = dev_get_drvdata(dev);
262 struct i2c_client *client = data->client;
263 int ret;
264
265 ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
266 if (ret <= 0)
267 return ret;
268
269 ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
270 ret = EMC2305_RPM_FACTOR / ret;
271 if (ret <= EMC2305_TACH_RANGE_MIN)
272 return 0;
273
274 return ret * EMC2305_TACH_CNT_MULTIPLIER;
275 }
276
emc2305_show_pwm(struct device * dev,int channel)277 static int emc2305_show_pwm(struct device *dev, int channel)
278 {
279 struct emc2305_data *data = dev_get_drvdata(dev);
280 struct i2c_client *client = data->client;
281
282 return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
283 }
284
emc2305_set_pwm(struct device * dev,long val,int channel)285 static int emc2305_set_pwm(struct device *dev, long val, int channel)
286 {
287 struct emc2305_data *data = dev_get_drvdata(dev);
288 struct i2c_client *client = data->client;
289 int ret;
290
291 if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
292 return -EINVAL;
293
294 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
295 if (ret < 0)
296 return ret;
297 data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
298 EMC2305_FAN_MAX);
299 return 0;
300 }
301
emc2305_set_single_tz(struct device * dev,struct device_node * fan_node,int idx)302 static int emc2305_set_single_tz(struct device *dev, struct device_node *fan_node, int idx)
303 {
304 struct emc2305_data *data = dev_get_drvdata(dev);
305 long pwm;
306 int i, cdev_idx, ret;
307
308 cdev_idx = (idx) ? idx - 1 : 0;
309 pwm = data->pwm_min[cdev_idx];
310
311 data->cdev_data[cdev_idx].cdev =
312 devm_thermal_of_cooling_device_register(dev, fan_node,
313 emc2305_fan_name[idx], data,
314 &emc2305_cooling_ops);
315
316 if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
317 dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
318 return PTR_ERR(data->cdev_data[cdev_idx].cdev);
319 }
320
321 if (data->cdev_data[cdev_idx].cur_state > 0)
322 /* Update pwm when temperature is above trips */
323 pwm = EMC2305_PWM_STATE2DUTY(data->cdev_data[cdev_idx].cur_state,
324 data->max_state, EMC2305_FAN_MAX);
325
326 /* Set minimal PWM speed. */
327 if (data->pwm_separate) {
328 ret = emc2305_set_pwm(dev, pwm, cdev_idx);
329 if (ret < 0)
330 return ret;
331 } else {
332 for (i = 0; i < data->pwm_num; i++) {
333 ret = emc2305_set_pwm(dev, pwm, i);
334 if (ret < 0)
335 return ret;
336 }
337 }
338 data->cdev_data[cdev_idx].cur_state =
339 EMC2305_PWM_DUTY2STATE(pwm, data->max_state,
340 EMC2305_FAN_MAX);
341 data->cdev_data[cdev_idx].last_hwmon_state =
342 EMC2305_PWM_DUTY2STATE(pwm, data->max_state,
343 EMC2305_FAN_MAX);
344 return 0;
345 }
346
emc2305_set_tz(struct device * dev)347 static int emc2305_set_tz(struct device *dev)
348 {
349 struct emc2305_data *data = dev_get_drvdata(dev);
350 int i, ret;
351
352 if (!data->pwm_separate)
353 return emc2305_set_single_tz(dev, dev->of_node, 0);
354
355 for (i = 0; i < data->pwm_num; i++) {
356 ret = emc2305_set_single_tz(dev, dev->of_node, i + 1);
357 if (ret)
358 return ret;
359 }
360 return 0;
361 }
362
363 static umode_t
emc2305_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)364 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
365 {
366 int max_channel = emc2305_get_max_channel(data);
367
368 /* Don't show channels which are not physically connected. */
369 if (channel >= max_channel)
370 return 0;
371 switch (type) {
372 case hwmon_fan:
373 switch (attr) {
374 case hwmon_fan_input:
375 return 0444;
376 case hwmon_fan_fault:
377 return 0444;
378 default:
379 break;
380 }
381 break;
382 case hwmon_pwm:
383 switch (attr) {
384 case hwmon_pwm_input:
385 return 0644;
386 default:
387 break;
388 }
389 break;
390 default:
391 break;
392 }
393
394 return 0;
395 };
396
397 static int
emc2305_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)398 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
399 {
400 struct emc2305_data *data = dev_get_drvdata(dev);
401 int cdev_idx;
402
403 switch (type) {
404 case hwmon_pwm:
405 switch (attr) {
406 case hwmon_pwm_input:
407 /* If thermal is configured - handle PWM limit setting. */
408 if (IS_REACHABLE(CONFIG_THERMAL)) {
409 if (data->pwm_separate)
410 cdev_idx = channel;
411 else
412 cdev_idx = 0;
413 data->cdev_data[cdev_idx].last_hwmon_state =
414 EMC2305_PWM_DUTY2STATE(val, data->max_state,
415 EMC2305_FAN_MAX);
416 /*
417 * Update PWM only in case requested state is not less than the
418 * last thermal state.
419 */
420 if (data->cdev_data[cdev_idx].last_hwmon_state >=
421 data->cdev_data[cdev_idx].last_thermal_state)
422 return __emc2305_set_cur_state(data, cdev_idx,
423 data->cdev_data[cdev_idx].last_hwmon_state);
424 return 0;
425 }
426 return emc2305_set_pwm(dev, val, channel);
427 default:
428 break;
429 }
430 break;
431 default:
432 break;
433 }
434
435 return -EOPNOTSUPP;
436 };
437
438 static int
emc2305_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)439 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
440 {
441 int ret;
442
443 switch (type) {
444 case hwmon_fan:
445 switch (attr) {
446 case hwmon_fan_input:
447 ret = emc2305_show_fan(dev, channel);
448 if (ret < 0)
449 return ret;
450 *val = ret;
451 return 0;
452 case hwmon_fan_fault:
453 ret = emc2305_show_fault(dev, channel);
454 if (ret < 0)
455 return ret;
456 *val = ret;
457 return 0;
458 default:
459 break;
460 }
461 break;
462 case hwmon_pwm:
463 switch (attr) {
464 case hwmon_pwm_input:
465 ret = emc2305_show_pwm(dev, channel);
466 if (ret < 0)
467 return ret;
468 *val = ret;
469 return 0;
470 default:
471 break;
472 }
473 break;
474 default:
475 break;
476 }
477
478 return -EOPNOTSUPP;
479 };
480
481 static const struct hwmon_ops emc2305_ops = {
482 .is_visible = emc2305_is_visible,
483 .read = emc2305_read,
484 .write = emc2305_write,
485 };
486
487 static const struct hwmon_channel_info * const emc2305_info[] = {
488 HWMON_CHANNEL_INFO(fan,
489 HWMON_F_INPUT | HWMON_F_FAULT,
490 HWMON_F_INPUT | HWMON_F_FAULT,
491 HWMON_F_INPUT | HWMON_F_FAULT,
492 HWMON_F_INPUT | HWMON_F_FAULT,
493 HWMON_F_INPUT | HWMON_F_FAULT),
494 HWMON_CHANNEL_INFO(pwm,
495 HWMON_PWM_INPUT,
496 HWMON_PWM_INPUT,
497 HWMON_PWM_INPUT,
498 HWMON_PWM_INPUT,
499 HWMON_PWM_INPUT),
500 NULL
501 };
502
503 static const struct hwmon_chip_info emc2305_chip_info = {
504 .ops = &emc2305_ops,
505 .info = emc2305_info,
506 };
507
emc2305_identify(struct device * dev)508 static int emc2305_identify(struct device *dev)
509 {
510 struct i2c_client *client = to_i2c_client(dev);
511 struct emc2305_data *data = i2c_get_clientdata(client);
512 int ret;
513
514 ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
515 if (ret < 0)
516 return ret;
517
518 switch (ret) {
519 case EMC2305:
520 data->pwm_num = 5;
521 break;
522 case EMC2303:
523 data->pwm_num = 3;
524 break;
525 case EMC2302:
526 data->pwm_num = 2;
527 break;
528 case EMC2301:
529 data->pwm_num = 1;
530 break;
531 default:
532 return -ENODEV;
533 }
534
535 return 0;
536 }
537
emc2305_of_parse_pwm_child(struct device * dev,struct device_node * child,struct emc2305_data * data)538 static int emc2305_of_parse_pwm_child(struct device *dev,
539 struct device_node *child,
540 struct emc2305_data *data)
541 { u32 ch;
542 int ret;
543 struct of_phandle_args args;
544
545 ret = of_property_read_u32(child, "reg", &ch);
546 if (ret) {
547 dev_err(dev, "missing reg property of %pOFn\n", child);
548 return ret;
549 }
550
551 ret = of_parse_phandle_with_args(child, "pwms", "#pwm-cells", 0, &args);
552
553 if (ret)
554 return ret;
555
556 if (args.args_count > 0) {
557 data->pwm_freq[ch] = find_closest(args.args[0], base_freq_table,
558 ARRAY_SIZE(base_freq_table));
559 } else {
560 data->pwm_freq[ch] = base_freq_table[3];
561 }
562
563 if (args.args_count > 1) {
564 if (args.args[1] == PWM_POLARITY_NORMAL || args.args[1] == PWM_POLARITY_INVERSED)
565 data->pwm_polarity_mask |= args.args[1] << ch;
566 else
567 dev_err(dev, "Wrong PWM polarity config provided: %d\n", args.args[0]);
568 } else {
569 data->pwm_polarity_mask |= PWM_POLARITY_NORMAL << ch;
570 }
571
572 if (args.args_count > 2) {
573 if (args.args[2] == EMC2305_PUSH_PULL || args.args[2] <= EMC2305_OPEN_DRAIN)
574 data->pwm_output_mask |= args.args[2] << ch;
575 else
576 dev_err(dev, "Wrong PWM output config provided: %d\n", args.args[1]);
577 } else {
578 data->pwm_output_mask |= EMC2305_OPEN_DRAIN << ch;
579 }
580
581 return 0;
582 }
583
emc2305_probe_childs_from_dt(struct device * dev)584 static int emc2305_probe_childs_from_dt(struct device *dev)
585 {
586 struct emc2305_data *data = dev_get_drvdata(dev);
587 struct device_node *child;
588 int ret, count = 0;
589
590 data->pwm_output_mask = 0x0;
591 data->pwm_polarity_mask = 0x0;
592
593 for_each_child_of_node(dev->of_node, child) {
594 if (of_property_present(child, "reg")) {
595 ret = emc2305_of_parse_pwm_child(dev, child, data);
596 if (ret)
597 continue;
598 count++;
599 }
600 }
601 return count;
602 }
603
emc2305_probe(struct i2c_client * client)604 static int emc2305_probe(struct i2c_client *client)
605 {
606 struct i2c_adapter *adapter = client->adapter;
607 struct device *dev = &client->dev;
608 struct device_node *child;
609 struct emc2305_data *data;
610 struct emc2305_platform_data *pdata;
611 int vendor;
612 int ret;
613 int i;
614 int pwm_childs;
615
616 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
617 return -ENODEV;
618
619 vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
620 if (vendor != EMC2305_VENDOR)
621 return -ENODEV;
622
623 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
624 if (!data)
625 return -ENOMEM;
626
627 i2c_set_clientdata(client, data);
628 data->client = client;
629
630 ret = emc2305_identify(dev);
631 if (ret)
632 return ret;
633
634 pwm_childs = emc2305_probe_childs_from_dt(dev);
635
636 pdata = dev_get_platdata(&client->dev);
637
638 if (!pwm_childs) {
639 if (pdata) {
640 if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE)
641 return -EINVAL;
642 data->max_state = pdata->max_state;
643 /*
644 * Validate a number of active PWM channels. Note that
645 * configured number can be less than the actual maximum
646 * supported by the device.
647 */
648 if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX)
649 return -EINVAL;
650 data->pwm_num = pdata->pwm_num;
651 data->pwm_output_mask = pdata->pwm_output_mask;
652 data->pwm_polarity_mask = pdata->pwm_polarity_mask;
653 data->pwm_separate = pdata->pwm_separate;
654 for (i = 0; i < EMC2305_PWM_MAX; i++) {
655 data->pwm_min[i] = pdata->pwm_min[i];
656 data->pwm_freq[i] = pdata->pwm_freq[i];
657 }
658 } else {
659 data->max_state = EMC2305_FAN_MAX_STATE;
660 data->pwm_separate = false;
661 data->pwm_output_mask = EMC2305_DEFAULT_OUTPUT;
662 data->pwm_polarity_mask = EMC2305_DEFAULT_POLARITY;
663 for (i = 0; i < EMC2305_PWM_MAX; i++) {
664 data->pwm_min[i] = EMC2305_FAN_MIN;
665 data->pwm_freq[i] = base_freq_table[3];
666 }
667 }
668 } else {
669 data->max_state = EMC2305_FAN_MAX_STATE;
670 data->pwm_separate = false;
671 for (i = 0; i < EMC2305_PWM_MAX; i++)
672 data->pwm_min[i] = EMC2305_FAN_MIN;
673 }
674
675 data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
676 &emc2305_chip_info, NULL);
677 if (IS_ERR(data->hwmon_dev))
678 return PTR_ERR(data->hwmon_dev);
679
680 if (IS_REACHABLE(CONFIG_THERMAL)) {
681 /* Parse and check for the available PWM child nodes */
682 if (pwm_childs > 0) {
683 i = 0;
684 for_each_child_of_node(dev->of_node, child) {
685 ret = emc2305_set_single_tz(dev, child, i);
686 if (ret != 0) {
687 of_node_put(child);
688 return ret;
689 }
690 i++;
691 }
692 } else {
693 ret = emc2305_set_tz(dev);
694 if (ret != 0)
695 return ret;
696 }
697 }
698
699 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_DRIVE_PWM_OUT,
700 data->pwm_output_mask);
701 if (ret < 0)
702 dev_err(dev, "Failed to configure pwm output, using default\n");
703
704 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_POLARITY,
705 data->pwm_polarity_mask);
706 if (ret < 0)
707 dev_err(dev, "Failed to configure pwm polarity, using default\n");
708
709 for (i = 0; i < data->pwm_num; i++) {
710 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
711 data->pwm_min[i]);
712 if (ret < 0)
713 return ret;
714 }
715
716 return 0;
717 }
718
719 static const struct of_device_id of_emc2305_match_table[] = {
720 { .compatible = "microchip,emc2305", },
721 {},
722 };
723 MODULE_DEVICE_TABLE(of, of_emc2305_match_table);
724
725 static struct i2c_driver emc2305_driver = {
726 .driver = {
727 .name = "emc2305",
728 .of_match_table = of_emc2305_match_table,
729 },
730 .probe = emc2305_probe,
731 .id_table = emc2305_ids,
732 };
733
734 module_i2c_driver(emc2305_driver);
735
736 MODULE_AUTHOR("Nvidia");
737 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
738 MODULE_LICENSE("GPL");
739