phy: rockchip-inno-usb2: add a new driver for Rockchip usb2phy
[firefly-linux-kernel-4.4.55.git] / drivers / phy / phy-rockchip-inno-usb2.c
1 /*
2  * Rockchip USB2.0 PHY with Innosilicon IP block driver
3  *
4  * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/clk.h>
17 #include <linux/clk-provider.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/jiffies.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/of.h>
27 #include <linux/of_address.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_platform.h>
30 #include <linux/phy/phy.h>
31 #include <linux/platform_device.h>
32 #include <linux/regmap.h>
33 #include <linux/mfd/syscon.h>
34
35 #define BIT_WRITEABLE_SHIFT     16
36 #define SCHEDULE_DELAY  (60 * HZ)
37
38 enum rockchip_usb2phy_port_id {
39         USB2PHY_PORT_OTG,
40         USB2PHY_PORT_HOST,
41         USB2PHY_NUM_PORTS,
42 };
43
44 enum rockchip_usb2phy_host_state {
45         PHY_STATE_HS_ONLINE     = 0,
46         PHY_STATE_DISCONNECT    = 1,
47         PHY_STATE_HS_CONNECT    = 2,
48         PHY_STATE_FS_CONNECT    = 4,
49 };
50
51 struct usb2phy_reg {
52         unsigned int    offset;
53         unsigned int    bitend;
54         unsigned int    bitstart;
55         unsigned int    disable;
56         unsigned int    enable;
57 };
58
59 /**
60  * struct rockchip_usb2phy_port_cfg: usb-phy port configuration.
61  * @phy_sus: phy suspend register.
62  * @ls_det_en: linestate detection enable register.
63  * @ls_det_st: linestate detection state register.
64  * @ls_det_clr: linestate detection clear register.
65  * @utmi_ls: utmi linestate state register.
66  * @utmi_hstdet: utmi host disconnect register.
67  */
68 struct rockchip_usb2phy_port_cfg {
69         struct usb2phy_reg      phy_sus;
70         struct usb2phy_reg      ls_det_en;
71         struct usb2phy_reg      ls_det_st;
72         struct usb2phy_reg      ls_det_clr;
73         struct usb2phy_reg      utmi_ls;
74         struct usb2phy_reg      utmi_hstdet;
75 };
76
77 /**
78  * struct rockchip_usb2phy_cfg: usb-phy configuration.
79  * @num_ports: specify how many ports that the phy has.
80  * @clkout_ctl: keep on/turn off output clk of phy.
81  */
82 struct rockchip_usb2phy_cfg {
83         unsigned int    num_ports;
84         struct usb2phy_reg      clkout_ctl;
85         const struct rockchip_usb2phy_port_cfg  *port_cfgs;
86 };
87
88 /**
89  * struct rockchip_usb2phy_port: usb-phy port data.
90  * @port_id: flag for otg port or host port.
91  * @suspended: phy suspended flag.
92  * @ls_irq: IRQ number assigned for linestate detection.
93  * @mutex: for register updating in sm_work.
94  * @sm_work: OTG state machine work.
95  * @phy_cfg: port register configuration, assigned by driver data.
96  */
97 struct rockchip_usb2phy_port {
98         struct phy      *phy;
99         unsigned int    port_id;
100         bool            suspended;
101         int             ls_irq;
102         struct mutex    mutex;
103         struct          delayed_work sm_work;
104         const struct    rockchip_usb2phy_port_cfg *port_cfg;
105 };
106
107 /**
108  * struct rockchip_usb2phy: usb2.0 phy driver data.
109  * @grf: General Register Files regmap.
110  * @clk480m: clock struct of phy output clk.
111  * @clk_hw: clock struct of phy output clk management.
112  * @vbus_host_gpio: host VBUS direction output.
113  * @phy_cfg: phy register configuration, assigned by driver data.
114  * @ports: phy port instance.
115  */
116 struct rockchip_usb2phy {
117         struct device   *dev;
118         struct regmap   *grf;
119         struct clk      *clk480m;
120         struct clk_hw   clk480m_hw;
121         struct gpio_desc        *vbus_host_gpio;
122         const struct rockchip_usb2phy_cfg       *phy_cfg;
123         struct rockchip_usb2phy_port    ports[USB2PHY_NUM_PORTS];
124 };
125
126 static inline int property_enable(struct rockchip_usb2phy *rphy,
127                            const struct usb2phy_reg *reg, bool en)
128 {
129         unsigned int val, mask, tmp;
130
131         tmp = en ? reg->enable : reg->disable;
132         mask = GENMASK(reg->bitend, reg->bitstart);
133         val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
134
135         return regmap_write(rphy->grf, reg->offset, val);
136 }
137
138 static inline bool property_enabled(struct rockchip_usb2phy *rphy,
139                                     const struct usb2phy_reg *reg)
140 {
141         int ret;
142         unsigned int tmp, orig;
143         unsigned int mask = GENMASK(reg->bitend, reg->bitstart);
144
145         ret = regmap_read(rphy->grf, reg->offset, &orig);
146         if (ret)
147                 return false;
148
149         tmp = (orig & mask) >> reg->bitstart;
150         return tmp == reg->enable;
151 }
152
153 static int rockchip_usb2phy_clk480m_enable(struct clk_hw *hw)
154 {
155         struct rockchip_usb2phy *rphy =
156                 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
157         int ret = 0;
158
159         /* turn on 480m clk output if it is off */
160         if (!property_enabled(rphy, &rphy->phy_cfg->clkout_ctl)) {
161                 ret = property_enable(rphy, &rphy->phy_cfg->clkout_ctl, true);
162                 if (ret)
163                         return ret;
164
165                 /* waitting for the clk become stable */
166                 mdelay(1);
167         }
168
169         return ret;
170 }
171
172 static void rockchip_usb2phy_clk480m_disable(struct clk_hw *hw)
173 {
174         struct rockchip_usb2phy *rphy =
175                 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
176         int index;
177
178         /* make sure all ports in suspended mode */
179         for (index = 0; index != rphy->phy_cfg->num_ports; index++)
180                 if (!rphy->ports[index].suspended)
181                         return;
182
183         /* turn off 480m clk output */
184         property_enable(rphy, &rphy->phy_cfg->clkout_ctl, false);
185 }
186
187 static int rockchip_usb2phy_clk480m_enabled(struct clk_hw *hw)
188 {
189         struct rockchip_usb2phy *rphy =
190                 container_of(hw, struct rockchip_usb2phy, clk480m_hw);
191
192         return property_enabled(rphy, &rphy->phy_cfg->clkout_ctl);
193 }
194
195 static unsigned long
196 rockchip_usb2phy_clk480m_recalc_rate(struct clk_hw *hw,
197                                      unsigned long parent_rate)
198 {
199         return 480000000;
200 }
201
202 static const struct clk_ops rockchip_usb2phy_clkout_ops = {
203         .enable = rockchip_usb2phy_clk480m_enable,
204         .disable = rockchip_usb2phy_clk480m_disable,
205         .is_enabled = rockchip_usb2phy_clk480m_enabled,
206         .recalc_rate = rockchip_usb2phy_clk480m_recalc_rate,
207 };
208
209 static struct clk *
210 rockchip_usb2phy_clk480m_register(struct rockchip_usb2phy *rphy)
211 {
212         struct device_node *node = rphy->dev->of_node;
213         struct clk *clk;
214         struct clk_init_data init;
215
216         init.name = "clk_usbphy_480m";
217         init.ops = &rockchip_usb2phy_clkout_ops;
218         init.flags = CLK_IS_ROOT;
219         init.parent_names = NULL;
220         init.num_parents = 0;
221         rphy->clk480m_hw.init = &init;
222
223         /* optional override of the clockname */
224         of_property_read_string(node, "clock-output-names", &init.name);
225
226         /* register the clock */
227         clk = clk_register(rphy->dev, &rphy->clk480m_hw);
228         if (!IS_ERR(clk))
229                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
230
231         return clk;
232 }
233
234 static int rockchip_usb2phy_init(struct phy *phy)
235 {
236         struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
237         struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
238         int ret = 0;
239
240         if (rport->port_id == USB2PHY_PORT_HOST) {
241                 /* clear linestate and enable linestate detect irq */
242                 mutex_lock(&rport->mutex);
243
244                 ret = property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
245                 if (ret) {
246                         mutex_unlock(&rport->mutex);
247                         return ret;
248                 }
249
250                 ret = property_enable(rphy, &rport->port_cfg->ls_det_en, true);
251                 if (ret) {
252                         mutex_unlock(&rport->mutex);
253                         return ret;
254                 }
255
256                 mutex_unlock(&rport->mutex);
257                 schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
258         }
259
260         return ret;
261 }
262
263 static int rockchip_usb2phy_resume(struct phy *phy)
264 {
265         struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
266         struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
267         int ret;
268
269         dev_dbg(&rport->phy->dev, "port resume\n");
270
271         ret = clk_prepare_enable(rphy->clk480m);
272         if (ret)
273                 return ret;
274
275         ret = property_enable(rphy, &rport->port_cfg->phy_sus, false);
276         if (ret)
277                 return ret;
278
279         rport->suspended = false;
280         return 0;
281 }
282
283 static int rockchip_usb2phy_suspend(struct phy *phy)
284 {
285         struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
286         struct rockchip_usb2phy *rphy = dev_get_drvdata(phy->dev.parent);
287         int ret;
288
289         dev_dbg(&rport->phy->dev, "port suspend\n");
290
291         ret = property_enable(rphy, &rport->port_cfg->phy_sus, true);
292         if (ret)
293                 return ret;
294
295         rport->suspended = true;
296         clk_disable_unprepare(rphy->clk480m);
297         return 0;
298 }
299
300 static int rockchip_usb2phy_exit(struct phy *phy)
301 {
302         struct rockchip_usb2phy_port *rport = phy_get_drvdata(phy);
303
304         if (rport->port_id == USB2PHY_PORT_HOST)
305                 cancel_delayed_work_sync(&rport->sm_work);
306
307         return 0;
308 }
309
310 static const struct phy_ops rockchip_usb2phy_ops = {
311         .init           = rockchip_usb2phy_init,
312         .exit           = rockchip_usb2phy_exit,
313         .power_on       = rockchip_usb2phy_resume,
314         .power_off      = rockchip_usb2phy_suspend,
315         .owner          = THIS_MODULE,
316 };
317
318 /*
319  * The function manage host-phy port state and suspend/resume phy port
320  * to save power.
321  *
322  * we rely on utmi_linestate and utmi_hostdisconnect to identify whether
323  * FS/HS is disconnect or not. Besides, we do not need care it is FS
324  * disconnected or HS disconnected, actually, we just only need get the
325  * device is disconnected at last through rearm the delayed work,
326  * to suspend the phy port in _PHY_STATE_DISCONNECT_ case.
327  *
328  * NOTE: It will invoke some clk related APIs, so do not invoke it from
329  * interrupt context.
330  */
331 static void rockchip_usb2phy_sm_work(struct work_struct *work)
332 {
333         struct rockchip_usb2phy_port *rport =
334                 container_of(work, struct rockchip_usb2phy_port, sm_work.work);
335         struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
336         unsigned int sh = rport->port_cfg->utmi_hstdet.bitend -
337                           rport->port_cfg->utmi_hstdet.bitstart + 1;
338         unsigned int ul, uhd, state;
339         unsigned int ul_mask, uhd_mask;
340         int ret;
341
342         mutex_lock(&rport->mutex);
343
344         ret = regmap_read(rphy->grf, rport->port_cfg->utmi_ls.offset, &ul);
345         if (ret < 0)
346                 goto next_schedule;
347
348         ret = regmap_read(rphy->grf, rport->port_cfg->utmi_hstdet.offset,
349                           &uhd);
350         if (ret < 0)
351                 goto next_schedule;
352
353         uhd_mask = GENMASK(rport->port_cfg->utmi_hstdet.bitend,
354                            rport->port_cfg->utmi_hstdet.bitstart);
355         ul_mask = GENMASK(rport->port_cfg->utmi_ls.bitend,
356                           rport->port_cfg->utmi_ls.bitstart);
357
358         /* stitch on utmi_ls and utmi_hstdet as phy state */
359         state = ((uhd & uhd_mask) >> rport->port_cfg->utmi_hstdet.bitstart) |
360                 (((ul & ul_mask) >> rport->port_cfg->utmi_ls.bitstart) << sh);
361
362         switch (state) {
363         case PHY_STATE_HS_ONLINE:
364                 dev_dbg(&rport->phy->dev, "HS online\n");
365                 break;
366         case PHY_STATE_FS_CONNECT:
367                 /*
368                  * For FS device, the online state share with connect state
369                  * from utmi_ls and utmi_hstdet register, so we distinguish
370                  * them via suspended flag.
371                  */
372                 if (!rport->suspended) {
373                         dev_dbg(&rport->phy->dev, "FS online\n");
374                         break;
375                 }
376                 /* fall through */
377         case PHY_STATE_HS_CONNECT:
378                 if (rport->suspended) {
379                         dev_dbg(&rport->phy->dev, "HS/FS connected\n");
380                         rockchip_usb2phy_resume(rport->phy);
381                         rport->suspended = false;
382                 }
383                 break;
384         case PHY_STATE_DISCONNECT:
385                 if (!rport->suspended) {
386                         dev_dbg(&rport->phy->dev, "HS/FS disconnected\n");
387                         rockchip_usb2phy_suspend(rport->phy);
388                         rport->suspended = true;
389                 }
390
391                 /*
392                  * activate the linestate detection to get the next device
393                  * plug-in irq.
394                  */
395                 property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
396                 property_enable(rphy, &rport->port_cfg->ls_det_en, true);
397
398                 /*
399                  * we don't need to rearm the delayed work when the phy port
400                  * is suspended.
401                  */
402                 mutex_unlock(&rport->mutex);
403                 return;
404         default:
405                 dev_dbg(&rport->phy->dev, "unknown phy state\n");
406                 break;
407         }
408
409 next_schedule:
410         mutex_unlock(&rport->mutex);
411         schedule_delayed_work(&rport->sm_work, SCHEDULE_DELAY);
412 }
413
414 static irqreturn_t rockchip_usb2phy_linestate_irq(int irq, void *data)
415 {
416         struct rockchip_usb2phy_port *rport = data;
417         struct rockchip_usb2phy *rphy = dev_get_drvdata(rport->phy->dev.parent);
418
419         if (!property_enabled(rphy, &rport->port_cfg->ls_det_st))
420                 return IRQ_NONE;
421
422         mutex_lock(&rport->mutex);
423
424         /* disable linestate detect irq and clear its status */
425         property_enable(rphy, &rport->port_cfg->ls_det_en, false);
426         property_enable(rphy, &rport->port_cfg->ls_det_clr, true);
427
428         mutex_unlock(&rport->mutex);
429
430         /*
431          * In this case for host phy port, a new device is plugged in,
432          * meanwhile, if the phy port is suspended, we need rearm the work to
433          * resume it and mange its states; otherwise, we do nothing about that.
434          */
435         if (rport->suspended && rport->port_id == USB2PHY_PORT_HOST)
436                 rockchip_usb2phy_sm_work(&rport->sm_work.work);
437
438         return IRQ_HANDLED;
439 }
440
441 static int rockchip_usb2phy_host_port_init(struct rockchip_usb2phy *rphy,
442                                            struct rockchip_usb2phy_port *rport,
443                                            struct device_node *child_np)
444 {
445         int ret = 0;
446
447         rport->port_id = USB2PHY_PORT_HOST;
448         mutex_init(&rport->mutex);
449         INIT_DELAYED_WORK(&rport->sm_work, rockchip_usb2phy_sm_work);
450
451         rport->ls_irq = of_irq_get_byname(child_np, "linestate");
452         if (rport->ls_irq < 0) {
453                 dev_err(rphy->dev, "no linestate irq provided\n");
454                 return rport->ls_irq;
455         }
456
457         ret = devm_request_threaded_irq(rphy->dev, rport->ls_irq, NULL,
458                                         rockchip_usb2phy_linestate_irq,
459                                         IRQF_ONESHOT,
460                                         "rockchip_usb2phy", rport);
461         if (ret) {
462                 dev_err(rphy->dev, "failed to request irq handle\n");
463                 return ret;
464         }
465
466         return ret;
467 }
468
469 static int rockchip_usb2phy_probe(struct platform_device *pdev)
470 {
471         struct device *dev = &pdev->dev;
472         struct device_node *np = dev->of_node;
473         struct device_node *child_np;
474         struct phy_provider *provider;
475         struct rockchip_usb2phy *rphy;
476         const struct of_device_id *match;
477         int index, ret;
478
479         rphy = devm_kzalloc(dev, sizeof(*rphy), GFP_KERNEL);
480         if (!rphy)
481                 return -ENOMEM;
482
483         match = of_match_device(dev->driver->of_match_table, dev);
484         if (!match || !match->data) {
485                 dev_err(dev, "phy configs are not assigned!\n");
486                 return -EINVAL;
487         }
488
489         if (!dev->parent || !dev->parent->of_node)
490                 return -ENOMEM;
491
492         rphy->grf = syscon_node_to_regmap(dev->parent->of_node);
493         if (IS_ERR(rphy->grf))
494                 return PTR_ERR(rphy->grf);
495
496         rphy->dev = dev;
497         rphy->phy_cfg = match->data;
498         platform_set_drvdata(pdev, rphy);
499
500         rphy->clk480m = rockchip_usb2phy_clk480m_register(rphy);
501         if (IS_ERR(rphy->clk480m))
502                 return PTR_ERR(rphy->clk480m);
503
504         rphy->vbus_host_gpio =
505                 devm_gpiod_get_optional(dev, "vbus_host", GPIOD_OUT_HIGH);
506         if (!rphy->vbus_host_gpio)
507                 dev_info(dev, "host_vbus is not assigned!\n");
508         else if (IS_ERR(rphy->vbus_host_gpio))
509                 return PTR_ERR(rphy->vbus_host_gpio);
510
511         index = 0;
512         for_each_child_of_node(np, child_np) {
513                 struct rockchip_usb2phy_port *rport = &rphy->ports[index];
514                 struct phy *phy;
515
516                 phy = devm_phy_create(dev, child_np, &rockchip_usb2phy_ops);
517                 if (IS_ERR(phy)) {
518                         dev_err(dev, "failed to create phy\n");
519                         ret = PTR_ERR(phy);
520                         goto put_child;
521                 }
522
523                 rport->phy = phy;
524                 rport->port_cfg = &rphy->phy_cfg->port_cfgs[index];
525
526                 /* initialize otg/host port separately */
527                 if (!of_node_cmp(child_np->name, "host-port")) {
528                         ret = rockchip_usb2phy_host_port_init(rphy, rport,
529                                                               child_np);
530                         if (ret)
531                                 goto put_child;
532                 }
533
534                 phy_set_drvdata(rport->phy, rport);
535                 index++;
536         }
537
538         provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
539         return PTR_ERR_OR_ZERO(provider);
540
541 put_child:
542         of_node_put(child_np);
543         of_clk_del_provider(np);
544         clk_unregister(rphy->clk480m);
545         return ret;
546 }
547
548 static const struct rockchip_usb2phy_cfg rk3366_phy_cfgs = {
549         .num_ports      = 1,
550         .clkout_ctl     = { 0x0724, 15, 15, 1, 0 },
551         .port_cfgs      = (struct rockchip_usb2phy_port_cfg[]) {
552                 {
553                         .phy_sus        = { 0x0728, 15, 0, 0, 0x1d1 },
554                         .ls_det_en      = { 0x0680, 4, 4, 0, 1 },
555                         .ls_det_st      = { 0x0690, 4, 4, 0, 1 },
556                         .ls_det_clr     = { 0x06a0, 4, 4, 0, 1 },
557                         .utmi_ls        = { 0x049c, 14, 13, 0, 1 },
558                         .utmi_hstdet    = { 0x049c, 12, 12, 0, 1 }
559                 },
560                 { /* sentinel */ }
561         },
562 };
563
564 static const struct of_device_id rockchip_usb2phy_dt_match[] = {
565         { .compatible = "rockchip,rk3366-usb2phy", .data = &rk3366_phy_cfgs },
566         {}
567 };
568 MODULE_DEVICE_TABLE(of, rockchip_usb2phy_dt_match);
569
570 static struct platform_driver rockchip_usb2phy_driver = {
571         .probe          = rockchip_usb2phy_probe,
572         .driver         = {
573                 .name   = "rockchip-usb2phy",
574                 .of_match_table = rockchip_usb2phy_dt_match,
575         },
576 };
577 module_platform_driver(rockchip_usb2phy_driver);
578
579 MODULE_AUTHOR("Frank Wang <frank.wang@rock-chips.com>");
580 MODULE_DESCRIPTION("Rockchip USB2.0 PHY driver");
581 MODULE_LICENSE("GPL v2");