Skip to content

Commit 422481a

Browse files
pelwellpopcornmix
authored andcommitted
hwmon: emc2305: fixups for driver submitted to mailing lists
The driver had a number of issues, checkpatch warnings/errors, and other limitations, so fix these up to make it usable. Signed-off-by: Phil Elwell <[email protected]> Signed-off-by: Dave Stevenson <[email protected]> hwmon: emc2305: Add calls to initialise of cooling maps Commit 46ef9d4 ("hwmon: emc2305: fixups for driver submitted to mailing lists") missed adding the call to thermal_of_cooling_device_register required to configure any cooling maps for the device, hence stopping it from actually ever changing speed. Signed-off-by: Dave Stevenson <[email protected]> hwmon: emc2305: Change OF properties pwm-min & pwm-max to u8 There is no DT binding for emc2305 as mainline are still discussing how to do a generic fan binding. The 5.15 driver was reading the "emc2305," properties "cooling-levels", "pwm-max", "pwm-min", and "pwm-channel" as u8. The overlay was writing them as u16 (;) so it was working. The 6.1 driver was reading as u32, which failed as there is insufficient data. As this is all downstream only, revert to u8 to match 5.15. Signed-off-by: Dave Stevenson <[email protected]>
1 parent 60e3a9e commit 422481a

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

drivers/hwmon/emc2305.c

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
#include <linux/platform_data/emc2305.h>
1313
#include <linux/thermal.h>
1414

15+
#define EMC2305_REG_FAN_STATUS 0x24
16+
#define EMC2305_REG_FAN_STALL_STATUS 0x25
1517
#define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
1618
#define EMC2305_REG_VENDOR 0xfe
1719
#define EMC2305_FAN_MAX 0xff
1820
#define EMC2305_FAN_MIN 0x00
1921
#define EMC2305_FAN_MAX_STATE 10
20-
#define EMC2305_DEVICE 0x34
2122
#define EMC2305_VENDOR 0x5d
2223
#define EMC2305_REG_PRODUCT_ID 0xfd
2324
#define EMC2305_TACH_REGS_UNUSE_BITS 3
@@ -36,6 +37,7 @@
3637
#define EMC2305_RPM_FACTOR 3932160
3738

3839
#define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
40+
#define EMC2305_REG_FAN_CFG(n) (0x32 + 0x10 * (n))
3941
#define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
4042
#define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
4143

@@ -55,6 +57,15 @@ static const struct i2c_device_id emc2305_ids[] = {
5557
};
5658
MODULE_DEVICE_TABLE(i2c, emc2305_ids);
5759

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+
5869
/**
5970
* struct emc2305_cdev_data - device-specific cooling device state
6071
* @cdev: cooling device
@@ -100,6 +111,7 @@ struct emc2305_data {
100111
u8 pwm_num;
101112
bool pwm_separate;
102113
u8 pwm_min[EMC2305_PWM_MAX];
114+
u8 pwm_max;
103115
struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
104116
};
105117

@@ -272,7 +284,7 @@ static int emc2305_set_pwm(struct device *dev, long val, int channel)
272284
struct i2c_client *client = data->client;
273285
int ret;
274286

275-
if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
287+
if (val < data->pwm_min[channel] || val > data->pwm_max)
276288
return -EINVAL;
277289

278290
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)
283295
return 0;
284296
}
285297

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+
286341
static int emc2305_set_single_tz(struct device *dev, int idx)
287342
{
288343
struct emc2305_data *data = dev_get_drvdata(dev);
@@ -292,9 +347,17 @@ static int emc2305_set_single_tz(struct device *dev, int idx)
292347
cdev_idx = (idx) ? idx - 1 : 0;
293348
pwm = data->pwm_min[cdev_idx];
294349

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);
298361

299362
if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
300363
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)
347410
int i;
348411

349412
/* 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+
}
353418
}
354419

355420
static umode_t
@@ -571,11 +636,18 @@ static int emc2305_probe(struct i2c_client *client)
571636
data->pwm_separate = pdata->pwm_separate;
572637
for (i = 0; i < EMC2305_PWM_MAX; i++)
573638
data->pwm_min[i] = pdata->pwm_min[i];
639+
data->pwm_max = EMC2305_FAN_MAX;
574640
} else {
575641
data->max_state = EMC2305_FAN_MAX_STATE;
576642
data->pwm_separate = false;
577643
for (i = 0; i < EMC2305_PWM_MAX; i++)
578644
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+
}
579651
}
580652

581653
data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
@@ -596,6 +668,12 @@ static int emc2305_probe(struct i2c_client *client)
596668
return ret;
597669
}
598670

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+
599677
return 0;
600678
}
601679

@@ -610,6 +688,7 @@ static void emc2305_remove(struct i2c_client *client)
610688
static struct i2c_driver emc2305_driver = {
611689
.driver = {
612690
.name = "emc2305",
691+
.of_match_table = emc2305_dt_ids,
613692
},
614693
.probe = emc2305_probe,
615694
.remove = emc2305_remove,

0 commit comments

Comments
 (0)