GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / phy / qualcomm / phy-qcom-usb-hsic.c
1 /**
2  * Copyright (C) 2016 Linaro Ltd
3  *
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 as
6  * published by the Free Software Foundation.
7  */
8 #include <linux/module.h>
9 #include <linux/ulpi/driver.h>
10 #include <linux/ulpi/regs.h>
11 #include <linux/phy/phy.h>
12 #include <linux/pinctrl/consumer.h>
13 #include <linux/pinctrl/pinctrl-state.h>
14 #include <linux/delay.h>
15 #include <linux/clk.h>
16
17 #define ULPI_HSIC_CFG           0x30
18 #define ULPI_HSIC_IO_CAL        0x33
19
20 struct qcom_usb_hsic_phy {
21         struct ulpi *ulpi;
22         struct phy *phy;
23         struct pinctrl *pctl;
24         struct clk *phy_clk;
25         struct clk *cal_clk;
26         struct clk *cal_sleep_clk;
27 };
28
29 static int qcom_usb_hsic_phy_power_on(struct phy *phy)
30 {
31         struct qcom_usb_hsic_phy *uphy = phy_get_drvdata(phy);
32         struct ulpi *ulpi = uphy->ulpi;
33         struct pinctrl_state *pins_default;
34         int ret;
35
36         ret = clk_prepare_enable(uphy->phy_clk);
37         if (ret)
38                 return ret;
39
40         ret = clk_prepare_enable(uphy->cal_clk);
41         if (ret)
42                 goto err_cal;
43
44         ret = clk_prepare_enable(uphy->cal_sleep_clk);
45         if (ret)
46                 goto err_sleep;
47
48         /* Set periodic calibration interval to ~2.048sec in HSIC_IO_CAL_REG */
49         ret = ulpi_write(ulpi, ULPI_HSIC_IO_CAL, 0xff);
50         if (ret)
51                 goto err_ulpi;
52
53         /* Enable periodic IO calibration in HSIC_CFG register */
54         ret = ulpi_write(ulpi, ULPI_HSIC_CFG, 0xa8);
55         if (ret)
56                 goto err_ulpi;
57
58         /* Configure pins for HSIC functionality */
59         pins_default = pinctrl_lookup_state(uphy->pctl, PINCTRL_STATE_DEFAULT);
60         if (IS_ERR(pins_default)) {
61                 ret = PTR_ERR(pins_default);
62                 goto err_ulpi;
63         }
64
65         ret = pinctrl_select_state(uphy->pctl, pins_default);
66         if (ret)
67                 goto err_ulpi;
68
69          /* Enable HSIC mode in HSIC_CFG register */
70         ret = ulpi_write(ulpi, ULPI_SET(ULPI_HSIC_CFG), 0x01);
71         if (ret)
72                 goto err_ulpi;
73
74         /* Disable auto-resume */
75         ret = ulpi_write(ulpi, ULPI_CLR(ULPI_IFC_CTRL),
76                          ULPI_IFC_CTRL_AUTORESUME);
77         if (ret)
78                 goto err_ulpi;
79
80         return ret;
81 err_ulpi:
82         clk_disable_unprepare(uphy->cal_sleep_clk);
83 err_sleep:
84         clk_disable_unprepare(uphy->cal_clk);
85 err_cal:
86         clk_disable_unprepare(uphy->phy_clk);
87         return ret;
88 }
89
90 static int qcom_usb_hsic_phy_power_off(struct phy *phy)
91 {
92         struct qcom_usb_hsic_phy *uphy = phy_get_drvdata(phy);
93
94         clk_disable_unprepare(uphy->cal_sleep_clk);
95         clk_disable_unprepare(uphy->cal_clk);
96         clk_disable_unprepare(uphy->phy_clk);
97
98         return 0;
99 }
100
101 static const struct phy_ops qcom_usb_hsic_phy_ops = {
102         .power_on = qcom_usb_hsic_phy_power_on,
103         .power_off = qcom_usb_hsic_phy_power_off,
104         .owner = THIS_MODULE,
105 };
106
107 static int qcom_usb_hsic_phy_probe(struct ulpi *ulpi)
108 {
109         struct qcom_usb_hsic_phy *uphy;
110         struct phy_provider *p;
111         struct clk *clk;
112
113         uphy = devm_kzalloc(&ulpi->dev, sizeof(*uphy), GFP_KERNEL);
114         if (!uphy)
115                 return -ENOMEM;
116         ulpi_set_drvdata(ulpi, uphy);
117
118         uphy->ulpi = ulpi;
119         uphy->pctl = devm_pinctrl_get(&ulpi->dev);
120         if (IS_ERR(uphy->pctl))
121                 return PTR_ERR(uphy->pctl);
122
123         uphy->phy_clk = clk = devm_clk_get(&ulpi->dev, "phy");
124         if (IS_ERR(clk))
125                 return PTR_ERR(clk);
126
127         uphy->cal_clk = clk = devm_clk_get(&ulpi->dev, "cal");
128         if (IS_ERR(clk))
129                 return PTR_ERR(clk);
130
131         uphy->cal_sleep_clk = clk = devm_clk_get(&ulpi->dev, "cal_sleep");
132         if (IS_ERR(clk))
133                 return PTR_ERR(clk);
134
135         uphy->phy = devm_phy_create(&ulpi->dev, ulpi->dev.of_node,
136                                     &qcom_usb_hsic_phy_ops);
137         if (IS_ERR(uphy->phy))
138                 return PTR_ERR(uphy->phy);
139         phy_set_drvdata(uphy->phy, uphy);
140
141         p = devm_of_phy_provider_register(&ulpi->dev, of_phy_simple_xlate);
142         return PTR_ERR_OR_ZERO(p);
143 }
144
145 static const struct of_device_id qcom_usb_hsic_phy_match[] = {
146         { .compatible = "qcom,usb-hsic-phy", },
147         { }
148 };
149 MODULE_DEVICE_TABLE(of, qcom_usb_hsic_phy_match);
150
151 static struct ulpi_driver qcom_usb_hsic_phy_driver = {
152         .probe = qcom_usb_hsic_phy_probe,
153         .driver = {
154                 .name = "qcom_usb_hsic_phy",
155                 .of_match_table = qcom_usb_hsic_phy_match,
156         },
157 };
158 module_ulpi_driver(qcom_usb_hsic_phy_driver);
159
160 MODULE_DESCRIPTION("Qualcomm USB HSIC phy");
161 MODULE_LICENSE("GPL v2");