2 * Copyright (C) 2017 Broadcom
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/delay.h>
15 #include <linux/extcon-provider.h>
16 #include <linux/gpio.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/module.h>
25 #include <linux/of_address.h>
26 #include <linux/phy/phy.h>
27 #include <linux/platform_device.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <linux/workqueue.h>
32 #define ICFG_DRD_AFE 0x0
33 #define ICFG_MISC_STAT 0x18
34 #define ICFG_DRD_P0CTL 0x1C
35 #define ICFG_STRAP_CTRL 0x20
36 #define ICFG_FSM_CTRL 0x24
38 #define ICFG_DEV_BIT BIT(2)
39 #define IDM_RST_BIT BIT(0)
40 #define AFE_CORERDY_VDDC BIT(18)
41 #define PHY_PLL_RESETB BIT(15)
42 #define PHY_RESETB BIT(14)
43 #define PHY_PLL_LOCK BIT(0)
45 #define DRD_DEV_MODE BIT(20)
46 #define OHCI_OVRCUR_POL BIT(11)
47 #define ICFG_OFF_MODE BIT(6)
48 #define PLL_LOCK_RETRY 1000
53 #define DRD_HOST_MODE (BIT(2) | BIT(3))
54 #define DRD_DEVICE_MODE (BIT(4) | BIT(5))
55 #define DRD_HOST_VAL 0x803
56 #define DRD_DEV_VAL 0x807
60 struct ns2_phy_driver
{
61 void __iomem
*icfgdrd_regs
;
62 void __iomem
*idmdrd_rst_ctrl
;
63 void __iomem
*crmu_usb2_ctrl
;
64 void __iomem
*usb2h_strap_reg
;
65 struct ns2_phy_data
*data
;
66 struct extcon_dev
*edev
;
67 struct gpio_desc
*vbus_gpiod
;
68 struct gpio_desc
*id_gpiod
;
71 unsigned long debounce_jiffies
;
72 struct delayed_work wq_extcon
;
76 struct ns2_phy_driver
*driver
;
81 static const unsigned int usb_extcon_cable
[] = {
87 static inline int pll_lock_stat(u32 usb_reg
, int reg_mask
,
88 struct ns2_phy_driver
*driver
)
90 int retry
= PLL_LOCK_RETRY
;
95 val
= readl(driver
->icfgdrd_regs
+ usb_reg
);
98 } while (--retry
> 0);
103 static int ns2_drd_phy_init(struct phy
*phy
)
105 struct ns2_phy_data
*data
= phy_get_drvdata(phy
);
106 struct ns2_phy_driver
*driver
= data
->driver
;
109 val
= readl(driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
111 if (data
->new_state
== EVT_HOST
) {
112 val
&= ~DRD_DEVICE_MODE
;
113 val
|= DRD_HOST_MODE
;
115 val
&= ~DRD_HOST_MODE
;
116 val
|= DRD_DEVICE_MODE
;
118 writel(val
, driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
123 static int ns2_drd_phy_poweroff(struct phy
*phy
)
125 struct ns2_phy_data
*data
= phy_get_drvdata(phy
);
126 struct ns2_phy_driver
*driver
= data
->driver
;
129 val
= readl(driver
->crmu_usb2_ctrl
);
130 val
&= ~AFE_CORERDY_VDDC
;
131 writel(val
, driver
->crmu_usb2_ctrl
);
133 val
= readl(driver
->crmu_usb2_ctrl
);
134 val
&= ~DRD_DEV_MODE
;
135 writel(val
, driver
->crmu_usb2_ctrl
);
137 /* Disable Host and Device Mode */
138 val
= readl(driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
139 val
&= ~(DRD_HOST_MODE
| DRD_DEVICE_MODE
| ICFG_OFF_MODE
);
140 writel(val
, driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
145 static int ns2_drd_phy_poweron(struct phy
*phy
)
147 struct ns2_phy_data
*data
= phy_get_drvdata(phy
);
148 struct ns2_phy_driver
*driver
= data
->driver
;
149 u32 extcon_event
= data
->new_state
;
153 if (extcon_event
== EVT_DEVICE
) {
154 writel(DRD_DEV_VAL
, driver
->icfgdrd_regs
+ ICFG_DRD_P0CTL
);
156 val
= readl(driver
->idmdrd_rst_ctrl
);
158 writel(val
, driver
->idmdrd_rst_ctrl
);
160 val
= readl(driver
->crmu_usb2_ctrl
);
161 val
|= (AFE_CORERDY_VDDC
| DRD_DEV_MODE
);
162 writel(val
, driver
->crmu_usb2_ctrl
);
164 /* Bring PHY and PHY_PLL out of Reset */
165 val
= readl(driver
->crmu_usb2_ctrl
);
166 val
|= (PHY_PLL_RESETB
| PHY_RESETB
);
167 writel(val
, driver
->crmu_usb2_ctrl
);
169 ret
= pll_lock_stat(ICFG_MISC_STAT
, PHY_PLL_LOCK
, driver
);
171 dev_err(&phy
->dev
, "Phy PLL lock failed\n");
175 writel(DRD_HOST_VAL
, driver
->icfgdrd_regs
+ ICFG_DRD_P0CTL
);
177 val
= readl(driver
->crmu_usb2_ctrl
);
178 val
|= AFE_CORERDY_VDDC
;
179 writel(val
, driver
->crmu_usb2_ctrl
);
181 ret
= pll_lock_stat(ICFG_MISC_STAT
, PHY_PLL_LOCK
, driver
);
183 dev_err(&phy
->dev
, "Phy PLL lock failed\n");
187 val
= readl(driver
->idmdrd_rst_ctrl
);
189 writel(val
, driver
->idmdrd_rst_ctrl
);
191 /* port over current Polarity */
192 val
= readl(driver
->usb2h_strap_reg
);
193 val
|= OHCI_OVRCUR_POL
;
194 writel(val
, driver
->usb2h_strap_reg
);
200 static void connect_change(struct ns2_phy_driver
*driver
)
205 extcon_event
= driver
->data
->new_state
;
206 val
= readl(driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
208 switch (extcon_event
) {
210 val
&= ~(DRD_HOST_MODE
| DRD_DEVICE_MODE
);
211 writel(val
, driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
213 val
= (val
& ~DRD_HOST_MODE
) | DRD_DEVICE_MODE
;
214 writel(val
, driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
216 val
= readl(driver
->icfgdrd_regs
+ ICFG_DRD_P0CTL
);
218 writel(val
, driver
->icfgdrd_regs
+ ICFG_DRD_P0CTL
);
222 val
&= ~(DRD_HOST_MODE
| DRD_DEVICE_MODE
);
223 writel(val
, driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
225 val
= (val
& ~DRD_DEVICE_MODE
) | DRD_HOST_MODE
;
226 writel(val
, driver
->icfgdrd_regs
+ ICFG_FSM_CTRL
);
228 val
= readl(driver
->usb2h_strap_reg
);
229 val
|= OHCI_OVRCUR_POL
;
230 writel(val
, driver
->usb2h_strap_reg
);
232 val
= readl(driver
->icfgdrd_regs
+ ICFG_DRD_P0CTL
);
233 val
&= ~ICFG_DEV_BIT
;
234 writel(val
, driver
->icfgdrd_regs
+ ICFG_DRD_P0CTL
);
238 pr_err("Invalid extcon event\n");
243 static void extcon_work(struct work_struct
*work
)
245 struct ns2_phy_driver
*driver
;
249 driver
= container_of(to_delayed_work(work
),
250 struct ns2_phy_driver
, wq_extcon
);
252 id
= gpiod_get_value_cansleep(driver
->id_gpiod
);
253 vbus
= gpiod_get_value_cansleep(driver
->vbus_gpiod
);
255 if (!id
&& vbus
) { /* Host connected */
256 extcon_set_state_sync(driver
->edev
, EXTCON_USB_HOST
, true);
257 pr_debug("Host cable connected\n");
258 driver
->data
->new_state
= EVT_HOST
;
259 connect_change(driver
);
260 } else if (id
&& !vbus
) { /* Disconnected */
261 extcon_set_state_sync(driver
->edev
, EXTCON_USB_HOST
, false);
262 extcon_set_state_sync(driver
->edev
, EXTCON_USB
, false);
263 pr_debug("Cable disconnected\n");
264 } else if (id
&& vbus
) { /* Device connected */
265 extcon_set_state_sync(driver
->edev
, EXTCON_USB
, true);
266 pr_debug("Device cable connected\n");
267 driver
->data
->new_state
= EVT_DEVICE
;
268 connect_change(driver
);
272 static irqreturn_t
gpio_irq_handler(int irq
, void *dev_id
)
274 struct ns2_phy_driver
*driver
= dev_id
;
276 queue_delayed_work(system_power_efficient_wq
, &driver
->wq_extcon
,
277 driver
->debounce_jiffies
);
282 static struct phy_ops ops
= {
283 .init
= ns2_drd_phy_init
,
284 .power_on
= ns2_drd_phy_poweron
,
285 .power_off
= ns2_drd_phy_poweroff
,
286 .owner
= THIS_MODULE
,
289 static const struct of_device_id ns2_drd_phy_dt_ids
[] = {
290 { .compatible
= "brcm,ns2-drd-phy", },
293 MODULE_DEVICE_TABLE(of
, ns2_drd_phy_dt_ids
);
295 static int ns2_drd_phy_probe(struct platform_device
*pdev
)
297 struct phy_provider
*phy_provider
;
298 struct device
*dev
= &pdev
->dev
;
299 struct ns2_phy_driver
*driver
;
300 struct ns2_phy_data
*data
;
301 struct resource
*res
;
305 driver
= devm_kzalloc(dev
, sizeof(struct ns2_phy_driver
),
310 driver
->data
= devm_kzalloc(dev
, sizeof(struct ns2_phy_data
),
315 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "icfg");
316 driver
->icfgdrd_regs
= devm_ioremap_resource(dev
, res
);
317 if (IS_ERR(driver
->icfgdrd_regs
))
318 return PTR_ERR(driver
->icfgdrd_regs
);
320 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "rst-ctrl");
321 driver
->idmdrd_rst_ctrl
= devm_ioremap_resource(dev
, res
);
322 if (IS_ERR(driver
->idmdrd_rst_ctrl
))
323 return PTR_ERR(driver
->idmdrd_rst_ctrl
);
325 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "crmu-ctrl");
326 driver
->crmu_usb2_ctrl
= devm_ioremap_resource(dev
, res
);
327 if (IS_ERR(driver
->crmu_usb2_ctrl
))
328 return PTR_ERR(driver
->crmu_usb2_ctrl
);
330 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "usb2-strap");
331 driver
->usb2h_strap_reg
= devm_ioremap_resource(dev
, res
);
332 if (IS_ERR(driver
->usb2h_strap_reg
))
333 return PTR_ERR(driver
->usb2h_strap_reg
);
336 driver
->id_gpiod
= devm_gpiod_get(&pdev
->dev
, "id", GPIOD_IN
);
337 if (IS_ERR(driver
->id_gpiod
)) {
338 dev_err(dev
, "failed to get ID GPIO\n");
339 return PTR_ERR(driver
->id_gpiod
);
341 driver
->vbus_gpiod
= devm_gpiod_get(&pdev
->dev
, "vbus", GPIOD_IN
);
342 if (IS_ERR(driver
->vbus_gpiod
)) {
343 dev_err(dev
, "failed to get VBUS GPIO\n");
344 return PTR_ERR(driver
->vbus_gpiod
);
347 driver
->edev
= devm_extcon_dev_allocate(dev
, usb_extcon_cable
);
348 if (IS_ERR(driver
->edev
)) {
349 dev_err(dev
, "failed to allocate extcon device\n");
353 ret
= devm_extcon_dev_register(dev
, driver
->edev
);
355 dev_err(dev
, "failed to register extcon device\n");
359 ret
= gpiod_set_debounce(driver
->id_gpiod
, GPIO_DELAY
* 1000);
361 driver
->debounce_jiffies
= msecs_to_jiffies(GPIO_DELAY
);
363 INIT_DELAYED_WORK(&driver
->wq_extcon
, extcon_work
);
365 driver
->id_irq
= gpiod_to_irq(driver
->id_gpiod
);
366 if (driver
->id_irq
< 0) {
367 dev_err(dev
, "failed to get ID IRQ\n");
368 return driver
->id_irq
;
371 driver
->vbus_irq
= gpiod_to_irq(driver
->vbus_gpiod
);
372 if (driver
->vbus_irq
< 0) {
373 dev_err(dev
, "failed to get ID IRQ\n");
374 return driver
->vbus_irq
;
377 ret
= devm_request_irq(dev
, driver
->id_irq
, gpio_irq_handler
,
378 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
381 dev_err(dev
, "failed to request handler for ID IRQ\n");
385 ret
= devm_request_irq(dev
, driver
->vbus_irq
, gpio_irq_handler
,
386 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
389 dev_err(dev
, "failed to request handler for VBUS IRQ\n");
393 dev_set_drvdata(dev
, driver
);
395 /* Shutdown all ports. They can be powered up as required */
396 val
= readl(driver
->crmu_usb2_ctrl
);
397 val
&= ~(AFE_CORERDY_VDDC
| PHY_RESETB
);
398 writel(val
, driver
->crmu_usb2_ctrl
);
401 data
->phy
= devm_phy_create(dev
, dev
->of_node
, &ops
);
402 if (IS_ERR(data
->phy
)) {
403 dev_err(dev
, "Failed to create usb drd phy\n");
404 return PTR_ERR(data
->phy
);
407 data
->driver
= driver
;
408 phy_set_drvdata(data
->phy
, data
);
410 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
411 if (IS_ERR(phy_provider
)) {
412 dev_err(dev
, "Failed to register as phy provider\n");
413 return PTR_ERR(phy_provider
);
416 platform_set_drvdata(pdev
, driver
);
418 dev_info(dev
, "Registered NS2 DRD Phy device\n");
419 queue_delayed_work(system_power_efficient_wq
, &driver
->wq_extcon
,
420 driver
->debounce_jiffies
);
425 static struct platform_driver ns2_drd_phy_driver
= {
426 .probe
= ns2_drd_phy_probe
,
428 .name
= "bcm-ns2-usbphy",
429 .of_match_table
= of_match_ptr(ns2_drd_phy_dt_ids
),
432 module_platform_driver(ns2_drd_phy_driver
);
434 MODULE_ALIAS("platform:bcm-ns2-drd-phy");
435 MODULE_AUTHOR("Broadcom");
436 MODULE_DESCRIPTION("Broadcom NS2 USB2 PHY driver");
437 MODULE_LICENSE("GPL v2");