12
12
#include <linux/platform_data/emc2305.h>
13
13
#include <linux/thermal.h>
14
14
15
+ #define EMC2305_REG_FAN_STATUS 0x24
16
+ #define EMC2305_REG_FAN_STALL_STATUS 0x25
15
17
#define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
16
18
#define EMC2305_REG_VENDOR 0xfe
17
19
#define EMC2305_FAN_MAX 0xff
18
20
#define EMC2305_FAN_MIN 0x00
19
21
#define EMC2305_FAN_MAX_STATE 10
20
- #define EMC2305_DEVICE 0x34
21
22
#define EMC2305_VENDOR 0x5d
22
23
#define EMC2305_REG_PRODUCT_ID 0xfd
23
24
#define EMC2305_TACH_REGS_UNUSE_BITS 3
36
37
#define EMC2305_RPM_FACTOR 3932160
37
38
38
39
#define EMC2305_REG_FAN_DRIVE (n ) (0x30 + 0x10 * (n))
40
+ #define EMC2305_REG_FAN_CFG (n ) (0x32 + 0x10 * (n))
39
41
#define EMC2305_REG_FAN_MIN_DRIVE (n ) (0x38 + 0x10 * (n))
40
42
#define EMC2305_REG_FAN_TACH (n ) (0x3e + 0x10 * (n))
41
43
@@ -55,6 +57,15 @@ static const struct i2c_device_id emc2305_ids[] = {
55
57
};
56
58
MODULE_DEVICE_TABLE (i2c , emc2305_ids );
57
59
60
+ static const struct of_device_id emc2305_dt_ids [] = {
61
+ { .compatible = "microchip,emc2305" },
62
+ { .compatible = "microchip,emc2303" },
63
+ { .compatible = "microchip,emc2302" },
64
+ { .compatible = "microchip,emc2301" },
65
+ { }
66
+ };
67
+ MODULE_DEVICE_TABLE (of , emc2305_dt_ids );
68
+
58
69
/**
59
70
* struct emc2305_cdev_data - device-specific cooling device state
60
71
* @cdev: cooling device
@@ -100,6 +111,7 @@ struct emc2305_data {
100
111
u8 pwm_num ;
101
112
bool pwm_separate ;
102
113
u8 pwm_min [EMC2305_PWM_MAX ];
114
+ u8 pwm_max ;
103
115
struct emc2305_cdev_data cdev_data [EMC2305_PWM_MAX ];
104
116
};
105
117
@@ -272,7 +284,7 @@ static int emc2305_set_pwm(struct device *dev, long val, int channel)
272
284
struct i2c_client * client = data -> client ;
273
285
int ret ;
274
286
275
- if (val < data -> pwm_min [channel ] || val > EMC2305_FAN_MAX )
287
+ if (val < data -> pwm_min [channel ] || val > data -> pwm_max )
276
288
return - EINVAL ;
277
289
278
290
ret = i2c_smbus_write_byte_data (client , EMC2305_REG_FAN_DRIVE (channel ), val );
@@ -283,6 +295,49 @@ static int emc2305_set_pwm(struct device *dev, long val, int channel)
283
295
return 0 ;
284
296
}
285
297
298
+ static int emc2305_get_tz_of (struct device * dev )
299
+ {
300
+ struct device_node * np = dev -> of_node ;
301
+ struct emc2305_data * data = dev_get_drvdata (dev );
302
+ int ret = 0 ;
303
+ u8 val ;
304
+ int i ;
305
+
306
+ /* OF parameters are optional - overwrite default setting
307
+ * if some of them are provided.
308
+ */
309
+
310
+ ret = of_property_read_u8 (np , "emc2305,cooling-levels" , & val );
311
+ if (!ret )
312
+ data -> max_state = val ;
313
+ else if (ret != - EINVAL )
314
+ return ret ;
315
+
316
+ ret = of_property_read_u8 (np , "emc2305,pwm-max" , & val );
317
+ if (!ret )
318
+ data -> pwm_max = val ;
319
+ else if (ret != - EINVAL )
320
+ return ret ;
321
+
322
+ ret = of_property_read_u8 (np , "emc2305,pwm-min" , & val );
323
+ if (!ret )
324
+ for (i = 0 ; i < EMC2305_PWM_MAX ; i ++ )
325
+ data -> pwm_min [i ] = val ;
326
+ else if (ret != - EINVAL )
327
+ return ret ;
328
+
329
+ /* Not defined or 0 means one thermal zone over all cooling devices.
330
+ * Otherwise - separated thermal zones for each PWM channel.
331
+ */
332
+ ret = of_property_read_u8 (np , "emc2305,pwm-channel" , & val );
333
+ if (!ret )
334
+ data -> pwm_separate = (val != 0 );
335
+ else if (ret != - EINVAL )
336
+ return ret ;
337
+
338
+ return 0 ;
339
+ }
340
+
286
341
static int emc2305_set_single_tz (struct device * dev , int idx )
287
342
{
288
343
struct emc2305_data * data = dev_get_drvdata (dev );
@@ -292,9 +347,17 @@ static int emc2305_set_single_tz(struct device *dev, int idx)
292
347
cdev_idx = (idx ) ? idx - 1 : 0 ;
293
348
pwm = data -> pwm_min [cdev_idx ];
294
349
295
- data -> cdev_data [cdev_idx ].cdev =
296
- thermal_cooling_device_register (emc2305_fan_name [idx ], data ,
297
- & emc2305_cooling_ops );
350
+ if (dev -> of_node )
351
+ data -> cdev_data [cdev_idx ].cdev =
352
+ devm_thermal_of_cooling_device_register (dev , dev -> of_node ,
353
+ emc2305_fan_name [idx ],
354
+ data ,
355
+ & emc2305_cooling_ops );
356
+ else
357
+ data -> cdev_data [cdev_idx ].cdev =
358
+ thermal_cooling_device_register (emc2305_fan_name [idx ],
359
+ data ,
360
+ & emc2305_cooling_ops );
298
361
299
362
if (IS_ERR (data -> cdev_data [cdev_idx ].cdev )) {
300
363
dev_err (dev , "Failed to register cooling device %s\n" , emc2305_fan_name [idx ]);
@@ -347,9 +410,11 @@ static void emc2305_unset_tz(struct device *dev)
347
410
int i ;
348
411
349
412
/* Unregister cooling device. */
350
- for (i = 0 ; i < EMC2305_PWM_MAX ; i ++ )
351
- if (data -> cdev_data [i ].cdev )
352
- thermal_cooling_device_unregister (data -> cdev_data [i ].cdev );
413
+ if (!dev -> of_node ) {
414
+ for (i = 0 ; i < EMC2305_PWM_MAX ; i ++ )
415
+ if (data -> cdev_data [i ].cdev )
416
+ thermal_cooling_device_unregister (data -> cdev_data [i ].cdev );
417
+ }
353
418
}
354
419
355
420
static umode_t
@@ -571,11 +636,18 @@ static int emc2305_probe(struct i2c_client *client)
571
636
data -> pwm_separate = pdata -> pwm_separate ;
572
637
for (i = 0 ; i < EMC2305_PWM_MAX ; i ++ )
573
638
data -> pwm_min [i ] = pdata -> pwm_min [i ];
639
+ data -> pwm_max = EMC2305_FAN_MAX ;
574
640
} else {
575
641
data -> max_state = EMC2305_FAN_MAX_STATE ;
576
642
data -> pwm_separate = false;
577
643
for (i = 0 ; i < EMC2305_PWM_MAX ; i ++ )
578
644
data -> pwm_min [i ] = EMC2305_FAN_MIN ;
645
+ data -> pwm_max = EMC2305_FAN_MAX ;
646
+ if (dev -> of_node ) {
647
+ ret = emc2305_get_tz_of (dev );
648
+ if (ret < 0 )
649
+ return ret ;
650
+ }
579
651
}
580
652
581
653
data -> hwmon_dev = devm_hwmon_device_register_with_info (dev , "emc2305" , data ,
@@ -596,6 +668,12 @@ static int emc2305_probe(struct i2c_client *client)
596
668
return ret ;
597
669
}
598
670
671
+ /* Acknowledge any existing faults. Stops the device responding on the
672
+ * SMBus alert address.
673
+ */
674
+ i2c_smbus_read_byte_data (client , EMC2305_REG_FAN_STALL_STATUS );
675
+ i2c_smbus_read_byte_data (client , EMC2305_REG_FAN_STATUS );
676
+
599
677
return 0 ;
600
678
}
601
679
@@ -610,6 +688,7 @@ static void emc2305_remove(struct i2c_client *client)
610
688
static struct i2c_driver emc2305_driver = {
611
689
.driver = {
612
690
.name = "emc2305" ,
691
+ .of_match_table = emc2305_dt_ids ,
613
692
},
614
693
.probe = emc2305_probe ,
615
694
.remove = emc2305_remove ,
0 commit comments