cade5681ba91107725d3979887e00145cb82f78f
[openwrt/staging/wigyori.git] /
1 From 50fe870273543a821f25e7f78ea7389183d819e7 Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.com>
3 Date: Thu, 5 May 2022 15:46:07 +0100
4 Subject: [PATCH] hwmon: emc2305: fixups for driver submitted to
5 mailing lists
6
7 The driver had a number of issues, checkpatch warnings/errors,
8 and other limitations, so fix these up to make it usable.
9
10 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
11 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
12 ---
13 drivers/hwmon/emc2305.c | 86 ++++++++++++++++++++++++-----------------
14 1 file changed, 50 insertions(+), 36 deletions(-)
15
16 --- a/drivers/hwmon/emc2305.c
17 +++ b/drivers/hwmon/emc2305.c
18 @@ -1,6 +1,6 @@
19 // SPDX-License-Identifier: GPL-2.0+
20 /*
21 - * Hardware monitoring driver for EMC2305 fan controller
22 + * Hardware monitoring driver for Microchip EMC2305 fan controller
23 *
24 * Copyright (C) 2022 Nvidia Technologies Ltd and Delta Networks, Inc.
25 */
26 @@ -17,17 +17,17 @@
27 static const unsigned short
28 emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END };
29
30 +#define EMC2305_REG_FAN_STATUS 0x24
31 +#define EMC2305_REG_FAN_STALL_STATUS 0x25
32 #define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
33 -#define EMC2305_REG_DEVICE 0xfd
34 #define EMC2305_REG_VENDOR 0xfe
35 #define EMC2305_FAN_MAX 0xff
36 #define EMC2305_FAN_MIN 0x00
37 #define EMC2305_FAN_MAX_STATE 10
38 -#define EMC2305_DEVICE 0x34
39 #define EMC2305_VENDOR 0x5d
40 #define EMC2305_REG_PRODUCT_ID 0xfd
41 #define EMC2305_TACH_REGS_UNUSE_BITS 3
42 -#define EMC2305_TACH_CNT_MULTIPLIER 0x02
43 +#define EMC2305_TACH_CNT_MULTIPLIER 1 /* Set by FAN_CFG RNGx bits */
44 #define EMC2305_PWM_MAX 5
45 #define EMC2305_PWM_CHNL_CMN 0
46
47 @@ -42,6 +42,7 @@ emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2
48 #define EMC2305_RPM_FACTOR 3932160
49
50 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
51 +#define EMC2305_REG_FAN_CFG(n) (0x32 + 0x10 * (n))
52 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
53 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
54
55 @@ -62,7 +63,8 @@ static const struct i2c_device_id emc230
56 MODULE_DEVICE_TABLE(i2c, emc2305_ids);
57
58 static const struct of_device_id emc2305_dt_ids[] = {
59 - { .compatible = "smsc,emc2305" },
60 + { .compatible = "microchip,emc2305" },
61 + { .compatible = "microchip,emc2301" },
62 { }
63 };
64 MODULE_DEVICE_TABLE(of, emc2305_dt_ids);
65 @@ -187,7 +189,7 @@ static int emc2305_set_cur_state(struct
66 struct emc2305_data *data = cdev->devdata;
67 struct i2c_client *client = data->client;
68 u8 val, i;
69 -
70 +
71 if (state > data->max_state)
72 return -EINVAL;
73
74 @@ -200,19 +202,20 @@ static int emc2305_set_cur_state(struct
75 state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
76
77 val = EMC2305_PWM_STATE2DUTY(state, data->max_state, data->pwm_max);
78 +
79 if (val > EMC2305_FAN_MAX)
80 return -EINVAL;
81
82 data->cdev_data[cdev_idx].cur_state = state;
83 if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
84 - /* Set the same PWM value in all channels
85 + /* Set the same PWM value in all channels
86 * if common PWM channel is used.
87 */
88 for (i = 0; i < data->pwm_num; i++)
89 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
90 else
91 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
92 -
93 +
94 return 0;
95 }
96
97 @@ -274,7 +277,7 @@ static int emc2305_get_tz_of(struct devi
98 struct emc2305_data *data = dev_get_drvdata(dev);
99 int ret = 0;
100
101 - /* OF parameters are optional - overwrite default setting
102 + /* OF parameters are optional - overwrite default setting
103 * if some of them are provided.
104 */
105
106 @@ -296,8 +299,8 @@ static int emc2305_get_tz_of(struct devi
107 return ret;
108 }
109
110 - /* Not defined or 0 means one thermal zone over all colling devices.
111 - * Otherwise - separted thermal zones for each PWM channel.
112 + /* Not defined or 0 means one thermal zone over all cooling devices.
113 + * Otherwise - separated thermal zones for each PWM channel.
114 */
115 if (of_find_property(np, "emc2305,pwm-channel", NULL)) {
116 ret = of_property_read_u8(np, "emc2305,pwm-channel", &data->pwm_channel);
117 @@ -313,7 +316,7 @@ static int emc2305_set_single_tz(struct
118 struct emc2305_data *data = dev_get_drvdata(dev);
119 long pwm = data->pwm_max;
120 int cdev_idx;
121 -
122 +
123 cdev_idx = (idx) ? idx - 1 : 0;
124
125 if (dev->of_node)
126 @@ -323,7 +326,7 @@ static int emc2305_set_single_tz(struct
127 &emc2305_cooling_ops);
128 else
129 data->cdev_data[cdev_idx].cdev =
130 - thermal_cooling_device_register(emc2305_fan_name[idx], data,
131 + thermal_cooling_device_register(emc2305_fan_name[idx], data,
132 &emc2305_cooling_ops);
133
134 if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
135 @@ -339,6 +342,22 @@ static int emc2305_set_single_tz(struct
136 return 0;
137 }
138
139 +static void emc2305_unset_tz(struct device *dev)
140 +{
141 + struct emc2305_data *data = dev_get_drvdata(dev);
142 + int i;
143 +
144 + /* Unregister cooling device in case they have been registered by
145 + * thermal_cooling_device_unregister(). No need for clean-up flow in case they
146 + * have been registered by devm_thermal_of_cooling_device_register()
147 + */
148 + if (!dev->of_node) {
149 + for (i = 0; i < EMC2305_PWM_MAX; i++)
150 + if (data->cdev_data[i].cdev)
151 + thermal_cooling_device_unregister(data->cdev_data[i].cdev);
152 + }
153 +}
154 +
155 static int emc2305_set_tz(struct device *dev)
156 {
157 struct emc2305_data *data = dev_get_drvdata(dev);
158 @@ -359,22 +378,6 @@ thermal_cooling_device_register_fail:
159 return ret;
160 }
161
162 -static void emc2305_unset_tz(struct device *dev)
163 -{
164 - struct emc2305_data *data = dev_get_drvdata(dev);
165 - int i;
166 -
167 - /* Unregister cooling device in case they have been registered by
168 - * thermal_cooling_device_unregister(). No need for clean-up flow in case they
169 - * have been registered by devm_thermal_of_cooling_device_register()
170 - */
171 - if (!dev->of_node) {
172 - for (i = 0; i < EMC2305_PWM_MAX; i++)
173 - if (data->cdev_data[i].cdev)
174 - thermal_cooling_device_unregister(data->cdev_data[i].cdev);
175 - }
176 -}
177 -
178 static umode_t
179 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
180 {
181 @@ -548,7 +551,7 @@ static int emc2305_probe(struct i2c_clie
182 struct i2c_adapter *adapter = client->adapter;
183 struct device *dev = &client->dev;
184 struct emc2305_data *data;
185 - int vendor, device;
186 + int vendor;
187 int ret;
188 int i;
189
190 @@ -559,10 +562,6 @@ static int emc2305_probe(struct i2c_clie
191 if (vendor != EMC2305_VENDOR)
192 return -ENODEV;
193
194 - device = i2c_smbus_read_byte_data(client, EMC2305_REG_DEVICE);
195 - if (device != EMC2305_DEVICE)
196 - return -ENODEV;
197 -
198 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
199 if (!data)
200 return -ENOMEM;
201 @@ -595,8 +594,23 @@ static int emc2305_probe(struct i2c_clie
202 return ret;
203 }
204
205 - for (i = 0; i < data->pwm_num; i++)
206 - i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i), data->pwm_min);
207 + for (i = 0; i < data->pwm_num; i++) {
208 + u8 val;
209 +
210 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
211 + data->pwm_min);
212 +
213 + val = i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_CFG(i));
214 + /* Set RNGx so minRPM=500 */
215 + val &= ~0x60;
216 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_CFG(i), val);
217 + }
218 +
219 + /* Acknowledge any existing faults. Stops the device responding on the
220 + * SMBus alert address.
221 + */
222 + i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_STALL_STATUS);
223 + i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_STATUS);
224
225 return 0;
226 }