Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Renesas R-Car Gen3 for USB2.0 PHY driver |
| 3 | * |
Yoshihiro Shimoda | 7e0540f | 2017-10-12 15:34:45 +0900 | [diff] [blame] | 4 | * Copyright (C) 2015-2017 Renesas Electronics Corporation |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 5 | * |
| 6 | * This is based on the phy-rcar-gen2 driver: |
| 7 | * Copyright (C) 2014 Renesas Solutions Corp. |
| 8 | * Copyright (C) 2014 Cogent Embedded, Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
Chanwoo Choi | 176aa36 | 2017-09-21 12:11:24 +0900 | [diff] [blame] | 15 | #include <linux/extcon-provider.h> |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 21 | #include <linux/of_device.h> |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 22 | #include <linux/phy/phy.h> |
| 23 | #include <linux/platform_device.h> |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Yoshihiro Shimoda | 6dcfd7c | 2016-03-03 19:09:05 +0900 | [diff] [blame] | 25 | #include <linux/regulator/consumer.h> |
Yoshihiro Shimoda | 7e0540f | 2017-10-12 15:34:45 +0900 | [diff] [blame] | 26 | #include <linux/usb/of.h> |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 27 | #include <linux/workqueue.h> |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 28 | |
| 29 | /******* USB2.0 Host registers (original offset is +0x200) *******/ |
| 30 | #define USB2_INT_ENABLE 0x000 |
| 31 | #define USB2_USBCTR 0x00c |
| 32 | #define USB2_SPD_RSM_TIMSET 0x10c |
| 33 | #define USB2_OC_TIMSET 0x110 |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 34 | #define USB2_COMMCTRL 0x600 |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 35 | #define USB2_OBINTSTA 0x604 |
| 36 | #define USB2_OBINTEN 0x608 |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 37 | #define USB2_VBCTRL 0x60c |
| 38 | #define USB2_LINECTRL1 0x610 |
| 39 | #define USB2_ADPCTRL 0x630 |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 40 | |
| 41 | /* INT_ENABLE */ |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 42 | #define USB2_INT_ENABLE_UCOM_INTEN BIT(3) |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 43 | #define USB2_INT_ENABLE_USBH_INTB_EN BIT(2) |
| 44 | #define USB2_INT_ENABLE_USBH_INTA_EN BIT(1) |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 45 | #define USB2_INT_ENABLE_INIT (USB2_INT_ENABLE_UCOM_INTEN | \ |
| 46 | USB2_INT_ENABLE_USBH_INTB_EN | \ |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 47 | USB2_INT_ENABLE_USBH_INTA_EN) |
| 48 | |
| 49 | /* USBCTR */ |
| 50 | #define USB2_USBCTR_DIRPD BIT(2) |
| 51 | #define USB2_USBCTR_PLL_RST BIT(1) |
| 52 | |
| 53 | /* SPD_RSM_TIMSET */ |
| 54 | #define USB2_SPD_RSM_TIMSET_INIT 0x014e029b |
| 55 | |
| 56 | /* OC_TIMSET */ |
| 57 | #define USB2_OC_TIMSET_INIT 0x000209ab |
| 58 | |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 59 | /* COMMCTRL */ |
| 60 | #define USB2_COMMCTRL_OTG_PERI BIT(31) /* 1 = Peripheral mode */ |
| 61 | |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 62 | /* OBINTSTA and OBINTEN */ |
| 63 | #define USB2_OBINT_SESSVLDCHG BIT(12) |
| 64 | #define USB2_OBINT_IDDIGCHG BIT(11) |
| 65 | #define USB2_OBINT_BITS (USB2_OBINT_SESSVLDCHG | \ |
| 66 | USB2_OBINT_IDDIGCHG) |
| 67 | |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 68 | /* VBCTRL */ |
| 69 | #define USB2_VBCTRL_DRVVBUSSEL BIT(8) |
| 70 | |
| 71 | /* LINECTRL1 */ |
| 72 | #define USB2_LINECTRL1_DPRPD_EN BIT(19) |
| 73 | #define USB2_LINECTRL1_DP_RPD BIT(18) |
| 74 | #define USB2_LINECTRL1_DMRPD_EN BIT(17) |
| 75 | #define USB2_LINECTRL1_DM_RPD BIT(16) |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 76 | #define USB2_LINECTRL1_OPMODE_NODRV BIT(6) |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 77 | |
| 78 | /* ADPCTRL */ |
| 79 | #define USB2_ADPCTRL_OTGSESSVLD BIT(20) |
| 80 | #define USB2_ADPCTRL_IDDIG BIT(19) |
| 81 | #define USB2_ADPCTRL_IDPULLUP BIT(5) /* 1 = ID sampling is enabled */ |
| 82 | #define USB2_ADPCTRL_DRVVBUS BIT(4) |
| 83 | |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 84 | #define RCAR_GEN3_PHY_HAS_DEDICATED_PINS 1 |
| 85 | |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 86 | struct rcar_gen3_chan { |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 87 | void __iomem *base; |
Yoshihiro Shimoda | 2b38543 | 2016-04-29 14:22:25 +0530 | [diff] [blame] | 88 | struct extcon_dev *extcon; |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 89 | struct phy *phy; |
Yoshihiro Shimoda | 6dcfd7c | 2016-03-03 19:09:05 +0900 | [diff] [blame] | 90 | struct regulator *vbus; |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 91 | struct work_struct work; |
| 92 | bool extcon_host; |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 93 | bool has_otg_pins; |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 94 | }; |
| 95 | |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 96 | static void rcar_gen3_phy_usb2_work(struct work_struct *work) |
| 97 | { |
| 98 | struct rcar_gen3_chan *ch = container_of(work, struct rcar_gen3_chan, |
| 99 | work); |
| 100 | |
| 101 | if (ch->extcon_host) { |
Chanwoo Choi | c6f30a5 | 2016-12-30 13:11:28 +0900 | [diff] [blame] | 102 | extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, true); |
| 103 | extcon_set_state_sync(ch->extcon, EXTCON_USB, false); |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 104 | } else { |
Chanwoo Choi | c6f30a5 | 2016-12-30 13:11:28 +0900 | [diff] [blame] | 105 | extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, false); |
| 106 | extcon_set_state_sync(ch->extcon, EXTCON_USB, true); |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 110 | static void rcar_gen3_set_host_mode(struct rcar_gen3_chan *ch, int host) |
| 111 | { |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 112 | void __iomem *usb2_base = ch->base; |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 113 | u32 val = readl(usb2_base + USB2_COMMCTRL); |
| 114 | |
| 115 | dev_vdbg(&ch->phy->dev, "%s: %08x, %d\n", __func__, val, host); |
| 116 | if (host) |
| 117 | val &= ~USB2_COMMCTRL_OTG_PERI; |
| 118 | else |
| 119 | val |= USB2_COMMCTRL_OTG_PERI; |
| 120 | writel(val, usb2_base + USB2_COMMCTRL); |
| 121 | } |
| 122 | |
| 123 | static void rcar_gen3_set_linectrl(struct rcar_gen3_chan *ch, int dp, int dm) |
| 124 | { |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 125 | void __iomem *usb2_base = ch->base; |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 126 | u32 val = readl(usb2_base + USB2_LINECTRL1); |
| 127 | |
| 128 | dev_vdbg(&ch->phy->dev, "%s: %08x, %d, %d\n", __func__, val, dp, dm); |
| 129 | val &= ~(USB2_LINECTRL1_DP_RPD | USB2_LINECTRL1_DM_RPD); |
| 130 | if (dp) |
| 131 | val |= USB2_LINECTRL1_DP_RPD; |
| 132 | if (dm) |
| 133 | val |= USB2_LINECTRL1_DM_RPD; |
| 134 | writel(val, usb2_base + USB2_LINECTRL1); |
| 135 | } |
| 136 | |
| 137 | static void rcar_gen3_enable_vbus_ctrl(struct rcar_gen3_chan *ch, int vbus) |
| 138 | { |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 139 | void __iomem *usb2_base = ch->base; |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 140 | u32 val = readl(usb2_base + USB2_ADPCTRL); |
| 141 | |
| 142 | dev_vdbg(&ch->phy->dev, "%s: %08x, %d\n", __func__, val, vbus); |
| 143 | if (vbus) |
| 144 | val |= USB2_ADPCTRL_DRVVBUS; |
| 145 | else |
| 146 | val &= ~USB2_ADPCTRL_DRVVBUS; |
| 147 | writel(val, usb2_base + USB2_ADPCTRL); |
| 148 | } |
| 149 | |
| 150 | static void rcar_gen3_init_for_host(struct rcar_gen3_chan *ch) |
| 151 | { |
| 152 | rcar_gen3_set_linectrl(ch, 1, 1); |
| 153 | rcar_gen3_set_host_mode(ch, 1); |
| 154 | rcar_gen3_enable_vbus_ctrl(ch, 1); |
Yoshihiro Shimoda | 2b38543 | 2016-04-29 14:22:25 +0530 | [diff] [blame] | 155 | |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 156 | ch->extcon_host = true; |
| 157 | schedule_work(&ch->work); |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void rcar_gen3_init_for_peri(struct rcar_gen3_chan *ch) |
| 161 | { |
| 162 | rcar_gen3_set_linectrl(ch, 0, 1); |
| 163 | rcar_gen3_set_host_mode(ch, 0); |
| 164 | rcar_gen3_enable_vbus_ctrl(ch, 0); |
Yoshihiro Shimoda | 2b38543 | 2016-04-29 14:22:25 +0530 | [diff] [blame] | 165 | |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 166 | ch->extcon_host = false; |
| 167 | schedule_work(&ch->work); |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 168 | } |
| 169 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 170 | static void rcar_gen3_init_for_b_host(struct rcar_gen3_chan *ch) |
| 171 | { |
| 172 | void __iomem *usb2_base = ch->base; |
| 173 | u32 val; |
| 174 | |
| 175 | val = readl(usb2_base + USB2_LINECTRL1); |
| 176 | writel(val | USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1); |
| 177 | |
| 178 | rcar_gen3_set_linectrl(ch, 1, 1); |
| 179 | rcar_gen3_set_host_mode(ch, 1); |
| 180 | rcar_gen3_enable_vbus_ctrl(ch, 0); |
| 181 | |
| 182 | val = readl(usb2_base + USB2_LINECTRL1); |
| 183 | writel(val & ~USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1); |
| 184 | } |
| 185 | |
| 186 | static void rcar_gen3_init_for_a_peri(struct rcar_gen3_chan *ch) |
| 187 | { |
| 188 | rcar_gen3_set_linectrl(ch, 0, 1); |
| 189 | rcar_gen3_set_host_mode(ch, 0); |
| 190 | rcar_gen3_enable_vbus_ctrl(ch, 1); |
| 191 | } |
| 192 | |
| 193 | static void rcar_gen3_init_from_a_peri_to_a_host(struct rcar_gen3_chan *ch) |
| 194 | { |
| 195 | void __iomem *usb2_base = ch->base; |
| 196 | u32 val; |
| 197 | |
| 198 | val = readl(usb2_base + USB2_OBINTEN); |
| 199 | writel(val & ~USB2_OBINT_BITS, usb2_base + USB2_OBINTEN); |
| 200 | |
| 201 | rcar_gen3_enable_vbus_ctrl(ch, 0); |
| 202 | rcar_gen3_init_for_host(ch); |
| 203 | |
| 204 | writel(val | USB2_OBINT_BITS, usb2_base + USB2_OBINTEN); |
| 205 | } |
| 206 | |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 207 | static bool rcar_gen3_check_id(struct rcar_gen3_chan *ch) |
| 208 | { |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 209 | return !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_IDDIG); |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void rcar_gen3_device_recognition(struct rcar_gen3_chan *ch) |
| 213 | { |
Yoshihiro Shimoda | 6762925 | 2016-05-31 21:47:17 +0900 | [diff] [blame] | 214 | if (!rcar_gen3_check_id(ch)) |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 215 | rcar_gen3_init_for_host(ch); |
| 216 | else |
| 217 | rcar_gen3_init_for_peri(ch); |
| 218 | } |
| 219 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 220 | static bool rcar_gen3_is_host(struct rcar_gen3_chan *ch) |
| 221 | { |
| 222 | return !(readl(ch->base + USB2_COMMCTRL) & USB2_COMMCTRL_OTG_PERI); |
| 223 | } |
| 224 | |
Yoshihiro Shimoda | b56acc8 | 2017-10-12 15:34:46 +0900 | [diff] [blame] | 225 | static enum phy_mode rcar_gen3_get_phy_mode(struct rcar_gen3_chan *ch) |
| 226 | { |
| 227 | if (rcar_gen3_is_host(ch)) |
| 228 | return PHY_MODE_USB_HOST; |
| 229 | |
| 230 | return PHY_MODE_USB_DEVICE; |
| 231 | } |
| 232 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 233 | static ssize_t role_store(struct device *dev, struct device_attribute *attr, |
| 234 | const char *buf, size_t count) |
| 235 | { |
| 236 | struct rcar_gen3_chan *ch = dev_get_drvdata(dev); |
Yoshihiro Shimoda | b56acc8 | 2017-10-12 15:34:46 +0900 | [diff] [blame] | 237 | bool is_b_device; |
| 238 | enum phy_mode cur_mode, new_mode; |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 239 | |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 240 | if (!ch->has_otg_pins || !ch->phy->init_count) |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 241 | return -EIO; |
| 242 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 243 | if (!strncmp(buf, "host", strlen("host"))) |
Yoshihiro Shimoda | b56acc8 | 2017-10-12 15:34:46 +0900 | [diff] [blame] | 244 | new_mode = PHY_MODE_USB_HOST; |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 245 | else if (!strncmp(buf, "peripheral", strlen("peripheral"))) |
Yoshihiro Shimoda | b56acc8 | 2017-10-12 15:34:46 +0900 | [diff] [blame] | 246 | new_mode = PHY_MODE_USB_DEVICE; |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 247 | else |
| 248 | return -EINVAL; |
| 249 | |
Yoshihiro Shimoda | b56acc8 | 2017-10-12 15:34:46 +0900 | [diff] [blame] | 250 | /* is_b_device: true is B-Device. false is A-Device. */ |
| 251 | is_b_device = rcar_gen3_check_id(ch); |
| 252 | cur_mode = rcar_gen3_get_phy_mode(ch); |
| 253 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 254 | /* If current and new mode is the same, this returns the error */ |
Yoshihiro Shimoda | b56acc8 | 2017-10-12 15:34:46 +0900 | [diff] [blame] | 255 | if (cur_mode == new_mode) |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 256 | return -EINVAL; |
| 257 | |
Yoshihiro Shimoda | b56acc8 | 2017-10-12 15:34:46 +0900 | [diff] [blame] | 258 | if (new_mode == PHY_MODE_USB_HOST) { /* And is_host must be false */ |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 259 | if (!is_b_device) /* A-Peripheral */ |
| 260 | rcar_gen3_init_from_a_peri_to_a_host(ch); |
| 261 | else /* B-Peripheral */ |
| 262 | rcar_gen3_init_for_b_host(ch); |
| 263 | } else { /* And is_host must be true */ |
| 264 | if (!is_b_device) /* A-Host */ |
| 265 | rcar_gen3_init_for_a_peri(ch); |
| 266 | else /* B-Host */ |
| 267 | rcar_gen3_init_for_peri(ch); |
| 268 | } |
| 269 | |
| 270 | return count; |
| 271 | } |
| 272 | |
| 273 | static ssize_t role_show(struct device *dev, struct device_attribute *attr, |
| 274 | char *buf) |
| 275 | { |
| 276 | struct rcar_gen3_chan *ch = dev_get_drvdata(dev); |
| 277 | |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 278 | if (!ch->has_otg_pins || !ch->phy->init_count) |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 279 | return -EIO; |
| 280 | |
| 281 | return sprintf(buf, "%s\n", rcar_gen3_is_host(ch) ? "host" : |
| 282 | "peripheral"); |
| 283 | } |
| 284 | static DEVICE_ATTR_RW(role); |
| 285 | |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 286 | static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch) |
| 287 | { |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 288 | void __iomem *usb2_base = ch->base; |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 289 | u32 val; |
| 290 | |
| 291 | val = readl(usb2_base + USB2_VBCTRL); |
| 292 | writel(val | USB2_VBCTRL_DRVVBUSSEL, usb2_base + USB2_VBCTRL); |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 293 | writel(USB2_OBINT_BITS, usb2_base + USB2_OBINTSTA); |
| 294 | val = readl(usb2_base + USB2_OBINTEN); |
| 295 | writel(val | USB2_OBINT_BITS, usb2_base + USB2_OBINTEN); |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 296 | val = readl(usb2_base + USB2_ADPCTRL); |
| 297 | writel(val | USB2_ADPCTRL_IDPULLUP, usb2_base + USB2_ADPCTRL); |
| 298 | val = readl(usb2_base + USB2_LINECTRL1); |
| 299 | rcar_gen3_set_linectrl(ch, 0, 0); |
| 300 | writel(val | USB2_LINECTRL1_DPRPD_EN | USB2_LINECTRL1_DMRPD_EN, |
| 301 | usb2_base + USB2_LINECTRL1); |
| 302 | |
| 303 | rcar_gen3_device_recognition(ch); |
| 304 | } |
| 305 | |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 306 | static int rcar_gen3_phy_usb2_init(struct phy *p) |
| 307 | { |
| 308 | struct rcar_gen3_chan *channel = phy_get_drvdata(p); |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 309 | void __iomem *usb2_base = channel->base; |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 310 | |
| 311 | /* Initialize USB2 part */ |
| 312 | writel(USB2_INT_ENABLE_INIT, usb2_base + USB2_INT_ENABLE); |
| 313 | writel(USB2_SPD_RSM_TIMSET_INIT, usb2_base + USB2_SPD_RSM_TIMSET); |
| 314 | writel(USB2_OC_TIMSET_INIT, usb2_base + USB2_OC_TIMSET); |
| 315 | |
Yoshihiro Shimoda | b956401 | 2016-01-07 18:16:44 +0900 | [diff] [blame] | 316 | /* Initialize otg part */ |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 317 | if (channel->has_otg_pins) |
Yoshihiro Shimoda | 1114e2d | 2015-11-30 10:44:31 +0900 | [diff] [blame] | 318 | rcar_gen3_init_otg(channel); |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int rcar_gen3_phy_usb2_exit(struct phy *p) |
| 324 | { |
| 325 | struct rcar_gen3_chan *channel = phy_get_drvdata(p); |
| 326 | |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 327 | writel(0, channel->base + USB2_INT_ENABLE); |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int rcar_gen3_phy_usb2_power_on(struct phy *p) |
| 333 | { |
| 334 | struct rcar_gen3_chan *channel = phy_get_drvdata(p); |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 335 | void __iomem *usb2_base = channel->base; |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 336 | u32 val; |
Yoshihiro Shimoda | 6dcfd7c | 2016-03-03 19:09:05 +0900 | [diff] [blame] | 337 | int ret; |
| 338 | |
| 339 | if (channel->vbus) { |
| 340 | ret = regulator_enable(channel->vbus); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | } |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 344 | |
| 345 | val = readl(usb2_base + USB2_USBCTR); |
| 346 | val |= USB2_USBCTR_PLL_RST; |
| 347 | writel(val, usb2_base + USB2_USBCTR); |
| 348 | val &= ~USB2_USBCTR_PLL_RST; |
| 349 | writel(val, usb2_base + USB2_USBCTR); |
| 350 | |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
Yoshihiro Shimoda | 6dcfd7c | 2016-03-03 19:09:05 +0900 | [diff] [blame] | 354 | static int rcar_gen3_phy_usb2_power_off(struct phy *p) |
| 355 | { |
| 356 | struct rcar_gen3_chan *channel = phy_get_drvdata(p); |
| 357 | int ret = 0; |
| 358 | |
| 359 | if (channel->vbus) |
| 360 | ret = regulator_disable(channel->vbus); |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
Bhumika Goyal | a8df276 | 2017-01-08 16:05:56 +0530 | [diff] [blame] | 365 | static const struct phy_ops rcar_gen3_phy_usb2_ops = { |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 366 | .init = rcar_gen3_phy_usb2_init, |
| 367 | .exit = rcar_gen3_phy_usb2_exit, |
| 368 | .power_on = rcar_gen3_phy_usb2_power_on, |
Yoshihiro Shimoda | 6dcfd7c | 2016-03-03 19:09:05 +0900 | [diff] [blame] | 369 | .power_off = rcar_gen3_phy_usb2_power_off, |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 370 | .owner = THIS_MODULE, |
| 371 | }; |
| 372 | |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 373 | static irqreturn_t rcar_gen3_phy_usb2_irq(int irq, void *_ch) |
| 374 | { |
| 375 | struct rcar_gen3_chan *ch = _ch; |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 376 | void __iomem *usb2_base = ch->base; |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 377 | u32 status = readl(usb2_base + USB2_OBINTSTA); |
| 378 | irqreturn_t ret = IRQ_NONE; |
| 379 | |
| 380 | if (status & USB2_OBINT_BITS) { |
| 381 | dev_vdbg(&ch->phy->dev, "%s: %08x\n", __func__, status); |
| 382 | writel(USB2_OBINT_BITS, usb2_base + USB2_OBINTSTA); |
| 383 | rcar_gen3_device_recognition(ch); |
| 384 | ret = IRQ_HANDLED; |
| 385 | } |
| 386 | |
| 387 | return ret; |
| 388 | } |
| 389 | |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 390 | static const struct of_device_id rcar_gen3_phy_usb2_match_table[] = { |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 391 | { |
| 392 | .compatible = "renesas,usb2-phy-r8a7795", |
| 393 | .data = (void *)RCAR_GEN3_PHY_HAS_DEDICATED_PINS, |
| 394 | }, |
| 395 | { |
| 396 | .compatible = "renesas,usb2-phy-r8a7796", |
| 397 | .data = (void *)RCAR_GEN3_PHY_HAS_DEDICATED_PINS, |
| 398 | }, |
| 399 | { |
Yoshihiro Shimoda | 44e42df | 2018-03-05 14:32:44 +0900 | [diff] [blame] | 400 | .compatible = "renesas,usb2-phy-r8a77965", |
| 401 | .data = (void *)RCAR_GEN3_PHY_HAS_DEDICATED_PINS, |
| 402 | }, |
| 403 | { |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 404 | .compatible = "renesas,rcar-gen3-usb2-phy", |
| 405 | }, |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 406 | { } |
| 407 | }; |
| 408 | MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb2_match_table); |
| 409 | |
Yoshihiro Shimoda | 2b38543 | 2016-04-29 14:22:25 +0530 | [diff] [blame] | 410 | static const unsigned int rcar_gen3_phy_cable[] = { |
| 411 | EXTCON_USB, |
| 412 | EXTCON_USB_HOST, |
| 413 | EXTCON_NONE, |
| 414 | }; |
| 415 | |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 416 | static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev) |
| 417 | { |
| 418 | struct device *dev = &pdev->dev; |
| 419 | struct rcar_gen3_chan *channel; |
| 420 | struct phy_provider *provider; |
| 421 | struct resource *res; |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 422 | int irq, ret = 0; |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 423 | |
| 424 | if (!dev->of_node) { |
| 425 | dev_err(dev, "This driver needs device tree\n"); |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | |
| 429 | channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL); |
| 430 | if (!channel) |
| 431 | return -ENOMEM; |
| 432 | |
Yoshihiro Shimoda | b956401 | 2016-01-07 18:16:44 +0900 | [diff] [blame] | 433 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Yoshihiro Shimoda | 801a69c | 2016-03-03 19:09:04 +0900 | [diff] [blame] | 434 | channel->base = devm_ioremap_resource(dev, res); |
| 435 | if (IS_ERR(channel->base)) |
| 436 | return PTR_ERR(channel->base); |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 437 | |
Yoshihiro Shimoda | b956401 | 2016-01-07 18:16:44 +0900 | [diff] [blame] | 438 | /* call request_irq for OTG */ |
| 439 | irq = platform_get_irq(pdev, 0); |
| 440 | if (irq >= 0) { |
Yoshihiro Shimoda | c14f8a40 | 2016-06-27 15:36:53 +0900 | [diff] [blame] | 441 | INIT_WORK(&channel->work, rcar_gen3_phy_usb2_work); |
Yoshihiro Shimoda | b956401 | 2016-01-07 18:16:44 +0900 | [diff] [blame] | 442 | irq = devm_request_irq(dev, irq, rcar_gen3_phy_usb2_irq, |
| 443 | IRQF_SHARED, dev_name(dev), channel); |
Yoshihiro Shimoda | 9f391c5 | 2015-11-30 10:44:32 +0900 | [diff] [blame] | 444 | if (irq < 0) |
| 445 | dev_err(dev, "No irq handler (%d)\n", irq); |
Yoshihiro Shimoda | 7e0540f | 2017-10-12 15:34:45 +0900 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (of_usb_get_dr_mode_by_phy(dev->of_node, 0) == USB_DR_MODE_OTG) { |
| 449 | int ret; |
| 450 | |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 451 | channel->has_otg_pins = (uintptr_t)of_device_get_match_data(dev); |
Yoshihiro Shimoda | 2b38543 | 2016-04-29 14:22:25 +0530 | [diff] [blame] | 452 | channel->extcon = devm_extcon_dev_allocate(dev, |
| 453 | rcar_gen3_phy_cable); |
| 454 | if (IS_ERR(channel->extcon)) |
| 455 | return PTR_ERR(channel->extcon); |
| 456 | |
| 457 | ret = devm_extcon_dev_register(dev, channel->extcon); |
| 458 | if (ret < 0) { |
| 459 | dev_err(dev, "Failed to register extcon\n"); |
| 460 | return ret; |
| 461 | } |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 462 | } |
| 463 | |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 464 | /* |
| 465 | * devm_phy_create() will call pm_runtime_enable(&phy->dev); |
| 466 | * And then, phy-core will manage runtime pm for this device. |
| 467 | */ |
| 468 | pm_runtime_enable(dev); |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 469 | channel->phy = devm_phy_create(dev, NULL, &rcar_gen3_phy_usb2_ops); |
| 470 | if (IS_ERR(channel->phy)) { |
| 471 | dev_err(dev, "Failed to create USB2 PHY\n"); |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 472 | ret = PTR_ERR(channel->phy); |
| 473 | goto error; |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 474 | } |
| 475 | |
Yoshihiro Shimoda | 6dcfd7c | 2016-03-03 19:09:05 +0900 | [diff] [blame] | 476 | channel->vbus = devm_regulator_get_optional(dev, "vbus"); |
| 477 | if (IS_ERR(channel->vbus)) { |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 478 | if (PTR_ERR(channel->vbus) == -EPROBE_DEFER) { |
| 479 | ret = PTR_ERR(channel->vbus); |
| 480 | goto error; |
| 481 | } |
Yoshihiro Shimoda | 6dcfd7c | 2016-03-03 19:09:05 +0900 | [diff] [blame] | 482 | channel->vbus = NULL; |
| 483 | } |
| 484 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 485 | platform_set_drvdata(pdev, channel); |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 486 | phy_set_drvdata(channel->phy, channel); |
| 487 | |
| 488 | provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 489 | if (IS_ERR(provider)) { |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 490 | dev_err(dev, "Failed to register PHY provider\n"); |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 491 | ret = PTR_ERR(provider); |
| 492 | goto error; |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 493 | } else if (channel->has_otg_pins) { |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 494 | int ret; |
| 495 | |
| 496 | ret = device_create_file(dev, &dev_attr_role); |
| 497 | if (ret < 0) |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 498 | goto error; |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 499 | } |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 500 | |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 501 | return 0; |
| 502 | |
| 503 | error: |
| 504 | pm_runtime_disable(dev); |
| 505 | |
| 506 | return ret; |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 507 | } |
| 508 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 509 | static int rcar_gen3_phy_usb2_remove(struct platform_device *pdev) |
| 510 | { |
| 511 | struct rcar_gen3_chan *channel = platform_get_drvdata(pdev); |
| 512 | |
Yoshihiro Shimoda | 9adaaa9 | 2017-10-12 15:34:47 +0900 | [diff] [blame] | 513 | if (channel->has_otg_pins) |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 514 | device_remove_file(&pdev->dev, &dev_attr_role); |
| 515 | |
Yoshihiro Shimoda | 441a681 | 2017-03-14 08:37:40 +0900 | [diff] [blame] | 516 | pm_runtime_disable(&pdev->dev); |
| 517 | |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 518 | return 0; |
| 519 | }; |
| 520 | |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 521 | static struct platform_driver rcar_gen3_phy_usb2_driver = { |
| 522 | .driver = { |
| 523 | .name = "phy_rcar_gen3_usb2", |
| 524 | .of_match_table = rcar_gen3_phy_usb2_match_table, |
| 525 | }, |
| 526 | .probe = rcar_gen3_phy_usb2_probe, |
Yoshihiro Shimoda | 9bb8677 | 2016-11-09 11:30:25 +0900 | [diff] [blame] | 527 | .remove = rcar_gen3_phy_usb2_remove, |
Yoshihiro Shimoda | f3b5a8d | 2015-11-30 10:44:30 +0900 | [diff] [blame] | 528 | }; |
| 529 | module_platform_driver(rcar_gen3_phy_usb2_driver); |
| 530 | |
| 531 | MODULE_LICENSE("GPL v2"); |
| 532 | MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 2.0 PHY"); |
| 533 | MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>"); |