forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/usb/misc/usb3503.c
....@@ -7,11 +7,10 @@
77
88 #include <linux/clk.h>
99 #include <linux/i2c.h>
10
-#include <linux/gpio.h>
10
+#include <linux/gpio/consumer.h>
1111 #include <linux/delay.h>
1212 #include <linux/slab.h>
1313 #include <linux/module.h>
14
-#include <linux/of_gpio.h>
1514 #include <linux/platform_device.h>
1615 #include <linux/platform_data/usb3503.h>
1716 #include <linux/regmap.h>
....@@ -47,19 +46,19 @@
4746 struct device *dev;
4847 struct clk *clk;
4948 u8 port_off_mask;
50
- int gpio_intn;
51
- int gpio_reset;
52
- int gpio_connect;
49
+ struct gpio_desc *intn;
50
+ struct gpio_desc *reset;
51
+ struct gpio_desc *connect;
5352 bool secondary_ref_clk;
5453 };
5554
5655 static int usb3503_reset(struct usb3503 *hub, int state)
5756 {
58
- if (!state && gpio_is_valid(hub->gpio_connect))
59
- gpio_set_value_cansleep(hub->gpio_connect, 0);
57
+ if (!state && hub->connect)
58
+ gpiod_set_value_cansleep(hub->connect, 0);
6059
61
- if (gpio_is_valid(hub->gpio_reset))
62
- gpio_set_value_cansleep(hub->gpio_reset, state);
60
+ if (hub->reset)
61
+ gpiod_set_value_cansleep(hub->reset, !state);
6362
6463 /* Wait T_HUBINIT == 4ms for hub logic to stabilize */
6564 if (state)
....@@ -115,8 +114,8 @@
115114 }
116115 }
117116
118
- if (gpio_is_valid(hub->gpio_connect))
119
- gpio_set_value_cansleep(hub->gpio_connect, 1);
117
+ if (hub->connect)
118
+ gpiod_set_value_cansleep(hub->connect, 1);
120119
121120 hub->mode = USB3503_MODE_HUB;
122121 dev_info(dev, "switched to HUB mode\n");
....@@ -163,16 +162,13 @@
163162 int err;
164163 u32 mode = USB3503_MODE_HUB;
165164 const u32 *property;
165
+ enum gpiod_flags flags;
166166 int len;
167167
168168 if (pdata) {
169169 hub->port_off_mask = pdata->port_off_mask;
170
- hub->gpio_intn = pdata->gpio_intn;
171
- hub->gpio_connect = pdata->gpio_connect;
172
- hub->gpio_reset = pdata->gpio_reset;
173170 hub->mode = pdata->initial_mode;
174171 } else if (np) {
175
- struct clk *clk;
176172 u32 rate = 0;
177173 hub->port_off_mask = 0;
178174
....@@ -198,32 +194,27 @@
198194 }
199195 }
200196
201
- clk = devm_clk_get(dev, "refclk");
202
- if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) {
197
+ hub->clk = devm_clk_get_optional(dev, "refclk");
198
+ if (IS_ERR(hub->clk)) {
203199 dev_err(dev, "unable to request refclk (%ld)\n",
204
- PTR_ERR(clk));
205
- return PTR_ERR(clk);
200
+ PTR_ERR(hub->clk));
201
+ return PTR_ERR(hub->clk);
206202 }
207203
208
- if (!IS_ERR(clk)) {
209
- hub->clk = clk;
210
-
211
- if (rate != 0) {
212
- err = clk_set_rate(hub->clk, rate);
213
- if (err) {
214
- dev_err(dev,
215
- "unable to set reference clock rate to %d\n",
216
- (int) rate);
217
- return err;
218
- }
219
- }
220
-
221
- err = clk_prepare_enable(hub->clk);
204
+ if (rate != 0) {
205
+ err = clk_set_rate(hub->clk, rate);
222206 if (err) {
223207 dev_err(dev,
224
- "unable to enable reference clock\n");
208
+ "unable to set reference clock rate to %d\n",
209
+ (int)rate);
225210 return err;
226211 }
212
+ }
213
+
214
+ err = clk_prepare_enable(hub->clk);
215
+ if (err) {
216
+ dev_err(dev, "unable to enable reference clock\n");
217
+ return err;
227218 }
228219
229220 property = of_get_property(np, "disabled-ports", &len);
....@@ -236,58 +227,37 @@
236227 }
237228 }
238229
239
- hub->gpio_intn = of_get_named_gpio(np, "intn-gpios", 0);
240
- if (hub->gpio_intn == -EPROBE_DEFER)
241
- return -EPROBE_DEFER;
242
- hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0);
243
- if (hub->gpio_connect == -EPROBE_DEFER)
244
- return -EPROBE_DEFER;
245
- hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
246
- if (hub->gpio_reset == -EPROBE_DEFER)
247
- return -EPROBE_DEFER;
248230 of_property_read_u32(np, "initial-mode", &mode);
249231 hub->mode = mode;
250232 }
251233
252
- if (hub->port_off_mask && !hub->regmap)
253
- dev_err(dev, "Ports disabled with no control interface\n");
234
+ if (hub->secondary_ref_clk)
235
+ flags = GPIOD_OUT_LOW;
236
+ else
237
+ flags = GPIOD_OUT_HIGH;
238
+ hub->intn = devm_gpiod_get_optional(dev, "intn", flags);
239
+ if (IS_ERR(hub->intn))
240
+ return PTR_ERR(hub->intn);
241
+ if (hub->intn)
242
+ gpiod_set_consumer_name(hub->intn, "usb3503 intn");
254243
255
- if (gpio_is_valid(hub->gpio_intn)) {
256
- int val = hub->secondary_ref_clk ? GPIOF_OUT_INIT_LOW :
257
- GPIOF_OUT_INIT_HIGH;
258
- err = devm_gpio_request_one(dev, hub->gpio_intn, val,
259
- "usb3503 intn");
260
- if (err) {
261
- dev_err(dev,
262
- "unable to request GPIO %d as interrupt pin (%d)\n",
263
- hub->gpio_intn, err);
264
- return err;
265
- }
266
- }
244
+ hub->connect = devm_gpiod_get_optional(dev, "connect", GPIOD_OUT_LOW);
245
+ if (IS_ERR(hub->connect))
246
+ return PTR_ERR(hub->connect);
247
+ if (hub->connect)
248
+ gpiod_set_consumer_name(hub->connect, "usb3503 connect");
267249
268
- if (gpio_is_valid(hub->gpio_connect)) {
269
- err = devm_gpio_request_one(dev, hub->gpio_connect,
270
- GPIOF_OUT_INIT_LOW, "usb3503 connect");
271
- if (err) {
272
- dev_err(dev,
273
- "unable to request GPIO %d as connect pin (%d)\n",
274
- hub->gpio_connect, err);
275
- return err;
276
- }
277
- }
278
-
279
- if (gpio_is_valid(hub->gpio_reset)) {
280
- err = devm_gpio_request_one(dev, hub->gpio_reset,
281
- GPIOF_OUT_INIT_LOW, "usb3503 reset");
250
+ hub->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
251
+ if (IS_ERR(hub->reset))
252
+ return PTR_ERR(hub->reset);
253
+ if (hub->reset) {
282254 /* Datasheet defines a hardware reset to be at least 100us */
283255 usleep_range(100, 10000);
284
- if (err) {
285
- dev_err(dev,
286
- "unable to request GPIO %d as reset pin (%d)\n",
287
- hub->gpio_reset, err);
288
- return err;
289
- }
256
+ gpiod_set_consumer_name(hub->reset, "usb3503 reset");
290257 }
258
+
259
+ if (hub->port_off_mask && !hub->regmap)
260
+ dev_err(dev, "Ports disabled with no control interface\n");
291261
292262 usb3503_switch_mode(hub, hub->mode);
293263
....@@ -324,8 +294,7 @@
324294 struct usb3503 *hub;
325295
326296 hub = i2c_get_clientdata(i2c);
327
- if (hub->clk)
328
- clk_disable_unprepare(hub->clk);
297
+ clk_disable_unprepare(hub->clk);
329298
330299 return 0;
331300 }
....@@ -348,42 +317,56 @@
348317 struct usb3503 *hub;
349318
350319 hub = platform_get_drvdata(pdev);
351
- if (hub->clk)
352
- clk_disable_unprepare(hub->clk);
320
+ clk_disable_unprepare(hub->clk);
353321
354322 return 0;
355323 }
356324
357
-#ifdef CONFIG_PM_SLEEP
358
-static int usb3503_i2c_suspend(struct device *dev)
325
+static int __maybe_unused usb3503_suspend(struct usb3503 *hub)
359326 {
360
- struct i2c_client *client = to_i2c_client(dev);
361
- struct usb3503 *hub = i2c_get_clientdata(client);
362
-
363327 usb3503_switch_mode(hub, USB3503_MODE_STANDBY);
364
-
365
- if (hub->clk)
366
- clk_disable_unprepare(hub->clk);
328
+ clk_disable_unprepare(hub->clk);
367329
368330 return 0;
369331 }
370332
371
-static int usb3503_i2c_resume(struct device *dev)
333
+static int __maybe_unused usb3503_resume(struct usb3503 *hub)
372334 {
373
- struct i2c_client *client = to_i2c_client(dev);
374
- struct usb3503 *hub = i2c_get_clientdata(client);
375
-
376
- if (hub->clk)
377
- clk_prepare_enable(hub->clk);
378
-
335
+ clk_prepare_enable(hub->clk);
379336 usb3503_switch_mode(hub, hub->mode);
380337
381338 return 0;
382339 }
383
-#endif
340
+
341
+static int __maybe_unused usb3503_i2c_suspend(struct device *dev)
342
+{
343
+ struct i2c_client *client = to_i2c_client(dev);
344
+
345
+ return usb3503_suspend(i2c_get_clientdata(client));
346
+}
347
+
348
+static int __maybe_unused usb3503_i2c_resume(struct device *dev)
349
+{
350
+ struct i2c_client *client = to_i2c_client(dev);
351
+
352
+ return usb3503_resume(i2c_get_clientdata(client));
353
+}
354
+
355
+static int __maybe_unused usb3503_platform_suspend(struct device *dev)
356
+{
357
+ return usb3503_suspend(dev_get_drvdata(dev));
358
+}
359
+
360
+static int __maybe_unused usb3503_platform_resume(struct device *dev)
361
+{
362
+ return usb3503_resume(dev_get_drvdata(dev));
363
+}
384364
385365 static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops, usb3503_i2c_suspend,
386366 usb3503_i2c_resume);
367
+
368
+static SIMPLE_DEV_PM_OPS(usb3503_platform_pm_ops, usb3503_platform_suspend,
369
+ usb3503_platform_resume);
387370
388371 static const struct i2c_device_id usb3503_id[] = {
389372 { USB3503_I2C_NAME, 0 },
....@@ -403,7 +386,7 @@
403386 static struct i2c_driver usb3503_i2c_driver = {
404387 .driver = {
405388 .name = USB3503_I2C_NAME,
406
- .pm = &usb3503_i2c_pm_ops,
389
+ .pm = pm_ptr(&usb3503_i2c_pm_ops),
407390 .of_match_table = of_match_ptr(usb3503_of_match),
408391 },
409392 .probe = usb3503_i2c_probe,
....@@ -415,6 +398,7 @@
415398 .driver = {
416399 .name = USB3503_I2C_NAME,
417400 .of_match_table = of_match_ptr(usb3503_of_match),
401
+ .pm = pm_ptr(&usb3503_platform_pm_ops),
418402 },
419403 .probe = usb3503_platform_probe,
420404 .remove = usb3503_platform_remove,