eventpoll.h: add missing epoll event masks
[linux/fpc-iii.git] / drivers / usb / phy / phy-qcom-8x16-usb.c
blob579587d972177338e97ca7ece02fabbc60fdcf55
1 /*
2 * Copyright (c) 2015, Linaro Limited
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/extcon.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/io.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <linux/reboot.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/reset.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/usb/ulpi.h>
31 #define HSPHY_AHBBURST 0x0090
32 #define HSPHY_AHBMODE 0x0098
33 #define HSPHY_GENCONFIG 0x009c
34 #define HSPHY_GENCONFIG_2 0x00a0
36 #define HSPHY_USBCMD 0x0140
37 #define HSPHY_ULPI_VIEWPORT 0x0170
38 #define HSPHY_CTRL 0x0240
40 #define HSPHY_TXFIFO_IDLE_FORCE_DIS BIT(4)
41 #define HSPHY_SESS_VLD_CTRL_EN BIT(7)
42 #define HSPHY_POR_ASSERT BIT(0)
43 #define HSPHY_RETEN BIT(1)
45 #define HSPHY_SESS_VLD_CTRL BIT(25)
47 #define ULPI_PWR_CLK_MNG_REG 0x88
48 #define ULPI_PWR_OTG_COMP_DISABLE BIT(0)
50 #define ULPI_MISC_A 0x96
51 #define ULPI_MISC_A_VBUSVLDEXTSEL BIT(1)
52 #define ULPI_MISC_A_VBUSVLDEXT BIT(0)
54 #define HSPHY_3P3_MIN 3050000 /* uV */
55 #define HSPHY_3P3_MAX 3300000 /* uV */
57 #define HSPHY_1P8_MIN 1800000 /* uV */
58 #define HSPHY_1P8_MAX 1800000 /* uV */
60 #define HSPHY_VDD_MIN 5
61 #define HSPHY_VDD_MAX 7
63 struct phy_8x16 {
64 struct usb_phy phy;
65 void __iomem *regs;
66 struct clk *core_clk;
67 struct clk *iface_clk;
68 struct regulator *v3p3;
69 struct regulator *v1p8;
70 struct regulator *vdd;
72 struct reset_control *phy_reset;
74 struct extcon_dev *vbus_edev;
75 struct notifier_block vbus_notify;
77 struct gpio_desc *switch_gpio;
78 struct notifier_block reboot_notify;
81 static int phy_8x16_regulators_enable(struct phy_8x16 *qphy)
83 int ret;
85 ret = regulator_set_voltage(qphy->vdd, HSPHY_VDD_MIN, HSPHY_VDD_MAX);
86 if (ret)
87 return ret;
89 ret = regulator_enable(qphy->vdd);
90 if (ret)
91 return ret;
93 ret = regulator_set_voltage(qphy->v3p3, HSPHY_3P3_MIN, HSPHY_3P3_MAX);
94 if (ret)
95 goto off_vdd;
97 ret = regulator_enable(qphy->v3p3);
98 if (ret)
99 goto off_vdd;
101 ret = regulator_set_voltage(qphy->v1p8, HSPHY_1P8_MIN, HSPHY_1P8_MAX);
102 if (ret)
103 goto off_3p3;
105 ret = regulator_enable(qphy->v1p8);
106 if (ret)
107 goto off_3p3;
109 return 0;
111 off_3p3:
112 regulator_disable(qphy->v3p3);
113 off_vdd:
114 regulator_disable(qphy->vdd);
116 return ret;
119 static void phy_8x16_regulators_disable(struct phy_8x16 *qphy)
121 regulator_disable(qphy->v1p8);
122 regulator_disable(qphy->v3p3);
123 regulator_disable(qphy->vdd);
126 static int phy_8x16_notify_connect(struct usb_phy *phy,
127 enum usb_device_speed speed)
129 struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
130 u32 val;
132 val = ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT;
133 usb_phy_io_write(&qphy->phy, val, ULPI_SET(ULPI_MISC_A));
135 val = readl(qphy->regs + HSPHY_USBCMD);
136 val |= HSPHY_SESS_VLD_CTRL;
137 writel(val, qphy->regs + HSPHY_USBCMD);
139 return 0;
142 static int phy_8x16_notify_disconnect(struct usb_phy *phy,
143 enum usb_device_speed speed)
145 struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
146 u32 val;
148 val = ULPI_MISC_A_VBUSVLDEXT | ULPI_MISC_A_VBUSVLDEXTSEL;
149 usb_phy_io_write(&qphy->phy, val, ULPI_CLR(ULPI_MISC_A));
151 val = readl(qphy->regs + HSPHY_USBCMD);
152 val &= ~HSPHY_SESS_VLD_CTRL;
153 writel(val, qphy->regs + HSPHY_USBCMD);
155 return 0;
158 static int phy_8x16_vbus_on(struct phy_8x16 *qphy)
160 phy_8x16_notify_connect(&qphy->phy, USB_SPEED_UNKNOWN);
162 /* Switch D+/D- lines to Device connector */
163 gpiod_set_value_cansleep(qphy->switch_gpio, 0);
165 return 0;
168 static int phy_8x16_vbus_off(struct phy_8x16 *qphy)
170 phy_8x16_notify_disconnect(&qphy->phy, USB_SPEED_UNKNOWN);
172 /* Switch D+/D- lines to USB HUB */
173 gpiod_set_value_cansleep(qphy->switch_gpio, 1);
175 return 0;
178 static int phy_8x16_vbus_notify(struct notifier_block *nb, unsigned long event,
179 void *ptr)
181 struct phy_8x16 *qphy = container_of(nb, struct phy_8x16, vbus_notify);
183 if (event)
184 phy_8x16_vbus_on(qphy);
185 else
186 phy_8x16_vbus_off(qphy);
188 return NOTIFY_DONE;
191 static int phy_8x16_init(struct usb_phy *phy)
193 struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
194 u32 val, init[] = {0x44, 0x6B, 0x24, 0x13};
195 u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
196 int idx, state;
198 for (idx = 0; idx < ARRAY_SIZE(init); idx++)
199 usb_phy_io_write(phy, init[idx], addr + idx);
201 reset_control_reset(qphy->phy_reset);
203 /* Assert USB HSPHY_POR */
204 val = readl(qphy->regs + HSPHY_CTRL);
205 val |= HSPHY_POR_ASSERT;
206 writel(val, qphy->regs + HSPHY_CTRL);
209 * wait for minimum 10 microseconds as suggested in HPG.
210 * Use a slightly larger value since the exact value didn't
211 * work 100% of the time.
213 usleep_range(12, 15);
215 /* Deassert USB HSPHY_POR */
216 val = readl(qphy->regs + HSPHY_CTRL);
217 val &= ~HSPHY_POR_ASSERT;
218 writel(val, qphy->regs + HSPHY_CTRL);
220 usleep_range(10, 15);
222 writel(0x00, qphy->regs + HSPHY_AHBBURST);
223 writel(0x08, qphy->regs + HSPHY_AHBMODE);
225 /* workaround for rx buffer collision issue */
226 val = readl(qphy->regs + HSPHY_GENCONFIG);
227 val &= ~HSPHY_TXFIFO_IDLE_FORCE_DIS;
228 writel(val, qphy->regs + HSPHY_GENCONFIG);
230 val = readl(qphy->regs + HSPHY_GENCONFIG_2);
231 val |= HSPHY_SESS_VLD_CTRL_EN;
232 writel(val, qphy->regs + HSPHY_GENCONFIG_2);
234 val = ULPI_PWR_OTG_COMP_DISABLE;
235 usb_phy_io_write(phy, val, ULPI_SET(ULPI_PWR_CLK_MNG_REG));
237 state = extcon_get_cable_state_(qphy->vbus_edev, EXTCON_USB);
238 if (state)
239 phy_8x16_vbus_on(qphy);
240 else
241 phy_8x16_vbus_off(qphy);
243 val = usb_phy_io_read(&qphy->phy, ULPI_FUNC_CTRL);
244 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
245 val |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
246 usb_phy_io_write(&qphy->phy, val, ULPI_FUNC_CTRL);
248 return 0;
251 static void phy_8x16_shutdown(struct usb_phy *phy)
253 u32 val;
255 /* Put the controller in non-driving mode */
256 val = usb_phy_io_read(phy, ULPI_FUNC_CTRL);
257 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
258 val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
259 usb_phy_io_write(phy, val, ULPI_FUNC_CTRL);
262 static int phy_8x16_read_devicetree(struct phy_8x16 *qphy)
264 struct regulator_bulk_data regs[3];
265 struct device *dev = qphy->phy.dev;
266 int ret;
268 qphy->core_clk = devm_clk_get(dev, "core");
269 if (IS_ERR(qphy->core_clk))
270 return PTR_ERR(qphy->core_clk);
272 qphy->iface_clk = devm_clk_get(dev, "iface");
273 if (IS_ERR(qphy->iface_clk))
274 return PTR_ERR(qphy->iface_clk);
276 regs[0].supply = "v3p3";
277 regs[1].supply = "v1p8";
278 regs[2].supply = "vddcx";
280 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(regs), regs);
281 if (ret)
282 return ret;
284 qphy->v3p3 = regs[0].consumer;
285 qphy->v1p8 = regs[1].consumer;
286 qphy->vdd = regs[2].consumer;
288 qphy->phy_reset = devm_reset_control_get(dev, "phy");
289 if (IS_ERR(qphy->phy_reset))
290 return PTR_ERR(qphy->phy_reset);
292 qphy->switch_gpio = devm_gpiod_get_optional(dev, "switch",
293 GPIOD_OUT_LOW);
294 if (IS_ERR(qphy->switch_gpio))
295 return PTR_ERR(qphy->switch_gpio);
297 return 0;
300 static int phy_8x16_reboot_notify(struct notifier_block *this,
301 unsigned long code, void *unused)
303 struct phy_8x16 *qphy;
305 qphy = container_of(this, struct phy_8x16, reboot_notify);
308 * Ensure that D+/D- lines are routed to uB connector, so
309 * we could load bootloader/kernel at next reboot_notify
311 gpiod_set_value_cansleep(qphy->switch_gpio, 0);
312 return NOTIFY_DONE;
315 static int phy_8x16_probe(struct platform_device *pdev)
317 struct phy_8x16 *qphy;
318 struct resource *res;
319 struct usb_phy *phy;
320 int ret;
322 qphy = devm_kzalloc(&pdev->dev, sizeof(*qphy), GFP_KERNEL);
323 if (!qphy)
324 return -ENOMEM;
326 platform_set_drvdata(pdev, qphy);
328 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
329 if (!res)
330 return -EINVAL;
332 qphy->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
333 if (!qphy->regs)
334 return -ENOMEM;
336 phy = &qphy->phy;
337 phy->dev = &pdev->dev;
338 phy->label = dev_name(&pdev->dev);
339 phy->init = phy_8x16_init;
340 phy->shutdown = phy_8x16_shutdown;
341 phy->notify_connect = phy_8x16_notify_connect;
342 phy->notify_disconnect = phy_8x16_notify_disconnect;
343 phy->io_priv = qphy->regs + HSPHY_ULPI_VIEWPORT;
344 phy->io_ops = &ulpi_viewport_access_ops;
345 phy->type = USB_PHY_TYPE_USB2;
347 ret = phy_8x16_read_devicetree(qphy);
348 if (ret < 0)
349 return ret;
351 qphy->vbus_edev = extcon_get_edev_by_phandle(phy->dev, 0);
352 if (IS_ERR(qphy->vbus_edev))
353 return PTR_ERR(qphy->vbus_edev);
355 ret = clk_set_rate(qphy->core_clk, INT_MAX);
356 if (ret < 0)
357 dev_dbg(phy->dev, "Can't boost core clock\n");
359 ret = clk_prepare_enable(qphy->core_clk);
360 if (ret < 0)
361 return ret;
363 ret = clk_prepare_enable(qphy->iface_clk);
364 if (ret < 0)
365 goto off_core;
367 ret = phy_8x16_regulators_enable(qphy);
368 if (0 && ret)
369 goto off_clks;
371 qphy->vbus_notify.notifier_call = phy_8x16_vbus_notify;
372 ret = extcon_register_notifier(qphy->vbus_edev, EXTCON_USB,
373 &qphy->vbus_notify);
374 if (ret < 0)
375 goto off_power;
377 ret = usb_add_phy_dev(&qphy->phy);
378 if (ret)
379 goto off_extcon;
381 qphy->reboot_notify.notifier_call = phy_8x16_reboot_notify;
382 register_reboot_notifier(&qphy->reboot_notify);
384 return 0;
386 off_extcon:
387 extcon_unregister_notifier(qphy->vbus_edev, EXTCON_USB,
388 &qphy->vbus_notify);
389 off_power:
390 phy_8x16_regulators_disable(qphy);
391 off_clks:
392 clk_disable_unprepare(qphy->iface_clk);
393 off_core:
394 clk_disable_unprepare(qphy->core_clk);
395 return ret;
398 static int phy_8x16_remove(struct platform_device *pdev)
400 struct phy_8x16 *qphy = platform_get_drvdata(pdev);
402 unregister_reboot_notifier(&qphy->reboot_notify);
403 extcon_unregister_notifier(qphy->vbus_edev, EXTCON_USB,
404 &qphy->vbus_notify);
407 * Ensure that D+/D- lines are routed to uB connector, so
408 * we could load bootloader/kernel at next reboot_notify
410 gpiod_set_value_cansleep(qphy->switch_gpio, 0);
412 usb_remove_phy(&qphy->phy);
414 clk_disable_unprepare(qphy->iface_clk);
415 clk_disable_unprepare(qphy->core_clk);
416 phy_8x16_regulators_disable(qphy);
417 return 0;
420 static const struct of_device_id phy_8x16_dt_match[] = {
421 { .compatible = "qcom,usb-8x16-phy" },
424 MODULE_DEVICE_TABLE(of, phy_8x16_dt_match);
426 static struct platform_driver phy_8x16_driver = {
427 .probe = phy_8x16_probe,
428 .remove = phy_8x16_remove,
429 .driver = {
430 .name = "phy-qcom-8x16-usb",
431 .of_match_table = phy_8x16_dt_match,
434 module_platform_driver(phy_8x16_driver);
436 MODULE_LICENSE("GPL v2");
437 MODULE_DESCRIPTION("Qualcomm APQ8016/MSM8916 chipsets USB transceiver driver");