blob: 2b4802a7497d88c6cdb2549999c7f6fb4e0b152a [file] [log] [blame]
Yunzhi Li64d11402014-12-12 23:07:46 +08001/*
2 * Rockchip usb PHY driver
3 *
4 * Copyright (C) 2014 Yunzhi Li <lyz@rock-chips.com>
5 * Copyright (C) 2014 ROCKCHIP, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/clk.h>
18#include <linux/io.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/mutex.h>
22#include <linux/of.h>
23#include <linux/of_address.h>
24#include <linux/phy/phy.h>
25#include <linux/platform_device.h>
26#include <linux/regulator/consumer.h>
27#include <linux/reset.h>
28#include <linux/regmap.h>
29#include <linux/mfd/syscon.h>
30
31/*
32 * The higher 16-bit of this register is used for write protection
33 * only if BIT(13 + 16) set to 1 the BIT(13) can be written.
34 */
35#define SIDDQ_WRITE_ENA BIT(29)
36#define SIDDQ_ON BIT(13)
37#define SIDDQ_OFF (0 << 13)
38
Heiko Stuebner5fdbb972015-11-19 22:22:23 +010039struct rockchip_usb_phy_base {
40 struct device *dev;
41 struct regmap *reg_base;
42};
43
Yunzhi Li64d11402014-12-12 23:07:46 +080044struct rockchip_usb_phy {
Heiko Stuebner5fdbb972015-11-19 22:22:23 +010045 struct rockchip_usb_phy_base *base;
Yunzhi Li64d11402014-12-12 23:07:46 +080046 unsigned int reg_offset;
Yunzhi Li64d11402014-12-12 23:07:46 +080047 struct clk *clk;
48 struct phy *phy;
49};
50
51static int rockchip_usb_phy_power(struct rockchip_usb_phy *phy,
52 bool siddq)
53{
Heiko Stuebner5fdbb972015-11-19 22:22:23 +010054 return regmap_write(phy->base->reg_base, phy->reg_offset,
Yunzhi Li64d11402014-12-12 23:07:46 +080055 SIDDQ_WRITE_ENA | (siddq ? SIDDQ_ON : SIDDQ_OFF));
56}
57
58static int rockchip_usb_phy_power_off(struct phy *_phy)
59{
60 struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
61 int ret = 0;
62
63 /* Power down usb phy analog blocks by set siddq 1 */
64 ret = rockchip_usb_phy_power(phy, 1);
65 if (ret)
66 return ret;
67
68 clk_disable_unprepare(phy->clk);
Yunzhi Li64d11402014-12-12 23:07:46 +080069
70 return 0;
71}
72
73static int rockchip_usb_phy_power_on(struct phy *_phy)
74{
75 struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
76 int ret = 0;
77
78 ret = clk_prepare_enable(phy->clk);
79 if (ret)
80 return ret;
81
82 /* Power up usb phy analog blocks by set siddq 0 */
83 ret = rockchip_usb_phy_power(phy, 0);
Axel Lin6b08e362015-03-04 09:33:32 +080084 if (ret) {
85 clk_disable_unprepare(phy->clk);
Yunzhi Li64d11402014-12-12 23:07:46 +080086 return ret;
Axel Lin6b08e362015-03-04 09:33:32 +080087 }
Yunzhi Li64d11402014-12-12 23:07:46 +080088
89 return 0;
90}
91
Axel Lin4a9e5ca2015-07-15 15:33:51 +080092static const struct phy_ops ops = {
Yunzhi Li64d11402014-12-12 23:07:46 +080093 .power_on = rockchip_usb_phy_power_on,
94 .power_off = rockchip_usb_phy_power_off,
95 .owner = THIS_MODULE,
96};
97
Heiko Stuebner75d390f2015-11-19 22:22:22 +010098static void rockchip_usb_phy_action(void *data)
99{
100 struct rockchip_usb_phy *rk_phy = data;
101
102 if (rk_phy->clk)
103 clk_put(rk_phy->clk);
104}
105
Yunzhi Li64d11402014-12-12 23:07:46 +0800106static int rockchip_usb_phy_probe(struct platform_device *pdev)
107{
108 struct device *dev = &pdev->dev;
Heiko Stuebner5fdbb972015-11-19 22:22:23 +0100109 struct rockchip_usb_phy_base *phy_base;
Yunzhi Li64d11402014-12-12 23:07:46 +0800110 struct rockchip_usb_phy *rk_phy;
111 struct phy_provider *phy_provider;
112 struct device_node *child;
Yunzhi Li64d11402014-12-12 23:07:46 +0800113 unsigned int reg_offset;
huang lin08db7e52015-07-17 15:29:25 +0800114 int err;
Yunzhi Li64d11402014-12-12 23:07:46 +0800115
Heiko Stuebner5fdbb972015-11-19 22:22:23 +0100116 phy_base = devm_kzalloc(dev, sizeof(*phy_base), GFP_KERNEL);
117 if (!phy_base)
118 return -ENOMEM;
119
120 phy_base->dev = dev;
121 phy_base->reg_base = syscon_regmap_lookup_by_phandle(dev->of_node,
122 "rockchip,grf");
123 if (IS_ERR(phy_base->reg_base)) {
Yunzhi Li64d11402014-12-12 23:07:46 +0800124 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
Heiko Stuebner5fdbb972015-11-19 22:22:23 +0100125 return PTR_ERR(phy_base->reg_base);
Yunzhi Li64d11402014-12-12 23:07:46 +0800126 }
127
128 for_each_available_child_of_node(dev->of_node, child) {
129 rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
Julia Lawallf6f31af2015-11-16 12:33:17 +0100130 if (!rk_phy) {
131 err = -ENOMEM;
132 goto put_child;
133 }
Yunzhi Li64d11402014-12-12 23:07:46 +0800134
Heiko Stuebner5fdbb972015-11-19 22:22:23 +0100135 rk_phy->base = phy_base;
136
Yunzhi Li64d11402014-12-12 23:07:46 +0800137 if (of_property_read_u32(child, "reg", &reg_offset)) {
138 dev_err(dev, "missing reg property in node %s\n",
139 child->name);
Julia Lawallf6f31af2015-11-16 12:33:17 +0100140 err = -EINVAL;
141 goto put_child;
Yunzhi Li64d11402014-12-12 23:07:46 +0800142 }
143
144 rk_phy->reg_offset = reg_offset;
Yunzhi Li64d11402014-12-12 23:07:46 +0800145
Heiko Stuebner75d390f2015-11-19 22:22:22 +0100146 err = devm_add_action(dev, rockchip_usb_phy_action, rk_phy);
147 if (err)
148 return err;
149
Yunzhi Li64d11402014-12-12 23:07:46 +0800150 rk_phy->clk = of_clk_get_by_name(child, "phyclk");
151 if (IS_ERR(rk_phy->clk))
152 rk_phy->clk = NULL;
153
154 rk_phy->phy = devm_phy_create(dev, child, &ops);
155 if (IS_ERR(rk_phy->phy)) {
156 dev_err(dev, "failed to create PHY\n");
Julia Lawallf6f31af2015-11-16 12:33:17 +0100157 err = PTR_ERR(rk_phy->phy);
158 goto put_child;
Yunzhi Li64d11402014-12-12 23:07:46 +0800159 }
160 phy_set_drvdata(rk_phy->phy, rk_phy);
huang lin08db7e52015-07-17 15:29:25 +0800161
162 /* only power up usb phy when it use, so disable it when init*/
163 err = rockchip_usb_phy_power(rk_phy, 1);
164 if (err)
Julia Lawallf6f31af2015-11-16 12:33:17 +0100165 goto put_child;
Yunzhi Li64d11402014-12-12 23:07:46 +0800166 }
167
168 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
169 return PTR_ERR_OR_ZERO(phy_provider);
Julia Lawallf6f31af2015-11-16 12:33:17 +0100170put_child:
171 of_node_put(child);
172 return err;
Yunzhi Li64d11402014-12-12 23:07:46 +0800173}
174
175static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
176 { .compatible = "rockchip,rk3288-usb-phy" },
177 {}
178};
179
180MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
181
182static struct platform_driver rockchip_usb_driver = {
183 .probe = rockchip_usb_phy_probe,
184 .driver = {
185 .name = "rockchip-usb-phy",
Yunzhi Li64d11402014-12-12 23:07:46 +0800186 .of_match_table = rockchip_usb_phy_dt_ids,
187 },
188};
189
190module_platform_driver(rockchip_usb_driver);
191
192MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
193MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
194MODULE_LICENSE("GPL v2");