Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 1 | /* |
| 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 Stuebner | 5fdbb97 | 2015-11-19 22:22:23 +0100 | [diff] [blame^] | 39 | struct rockchip_usb_phy_base { |
| 40 | struct device *dev; |
| 41 | struct regmap *reg_base; |
| 42 | }; |
| 43 | |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 44 | struct rockchip_usb_phy { |
Heiko Stuebner | 5fdbb97 | 2015-11-19 22:22:23 +0100 | [diff] [blame^] | 45 | struct rockchip_usb_phy_base *base; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 46 | unsigned int reg_offset; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 47 | struct clk *clk; |
| 48 | struct phy *phy; |
| 49 | }; |
| 50 | |
| 51 | static int rockchip_usb_phy_power(struct rockchip_usb_phy *phy, |
| 52 | bool siddq) |
| 53 | { |
Heiko Stuebner | 5fdbb97 | 2015-11-19 22:22:23 +0100 | [diff] [blame^] | 54 | return regmap_write(phy->base->reg_base, phy->reg_offset, |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 55 | SIDDQ_WRITE_ENA | (siddq ? SIDDQ_ON : SIDDQ_OFF)); |
| 56 | } |
| 57 | |
| 58 | static 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 Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static 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 Lin | 6b08e36 | 2015-03-04 09:33:32 +0800 | [diff] [blame] | 84 | if (ret) { |
| 85 | clk_disable_unprepare(phy->clk); |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 86 | return ret; |
Axel Lin | 6b08e36 | 2015-03-04 09:33:32 +0800 | [diff] [blame] | 87 | } |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Axel Lin | 4a9e5ca | 2015-07-15 15:33:51 +0800 | [diff] [blame] | 92 | static const struct phy_ops ops = { |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 93 | .power_on = rockchip_usb_phy_power_on, |
| 94 | .power_off = rockchip_usb_phy_power_off, |
| 95 | .owner = THIS_MODULE, |
| 96 | }; |
| 97 | |
Heiko Stuebner | 75d390f | 2015-11-19 22:22:22 +0100 | [diff] [blame] | 98 | static 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 Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 106 | static int rockchip_usb_phy_probe(struct platform_device *pdev) |
| 107 | { |
| 108 | struct device *dev = &pdev->dev; |
Heiko Stuebner | 5fdbb97 | 2015-11-19 22:22:23 +0100 | [diff] [blame^] | 109 | struct rockchip_usb_phy_base *phy_base; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 110 | struct rockchip_usb_phy *rk_phy; |
| 111 | struct phy_provider *phy_provider; |
| 112 | struct device_node *child; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 113 | unsigned int reg_offset; |
huang lin | 08db7e5 | 2015-07-17 15:29:25 +0800 | [diff] [blame] | 114 | int err; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 115 | |
Heiko Stuebner | 5fdbb97 | 2015-11-19 22:22:23 +0100 | [diff] [blame^] | 116 | 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 Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 124 | dev_err(&pdev->dev, "Missing rockchip,grf property\n"); |
Heiko Stuebner | 5fdbb97 | 2015-11-19 22:22:23 +0100 | [diff] [blame^] | 125 | return PTR_ERR(phy_base->reg_base); |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | for_each_available_child_of_node(dev->of_node, child) { |
| 129 | rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL); |
Julia Lawall | f6f31af | 2015-11-16 12:33:17 +0100 | [diff] [blame] | 130 | if (!rk_phy) { |
| 131 | err = -ENOMEM; |
| 132 | goto put_child; |
| 133 | } |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 134 | |
Heiko Stuebner | 5fdbb97 | 2015-11-19 22:22:23 +0100 | [diff] [blame^] | 135 | rk_phy->base = phy_base; |
| 136 | |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 137 | if (of_property_read_u32(child, "reg", ®_offset)) { |
| 138 | dev_err(dev, "missing reg property in node %s\n", |
| 139 | child->name); |
Julia Lawall | f6f31af | 2015-11-16 12:33:17 +0100 | [diff] [blame] | 140 | err = -EINVAL; |
| 141 | goto put_child; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | rk_phy->reg_offset = reg_offset; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 145 | |
Heiko Stuebner | 75d390f | 2015-11-19 22:22:22 +0100 | [diff] [blame] | 146 | err = devm_add_action(dev, rockchip_usb_phy_action, rk_phy); |
| 147 | if (err) |
| 148 | return err; |
| 149 | |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 150 | 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 Lawall | f6f31af | 2015-11-16 12:33:17 +0100 | [diff] [blame] | 157 | err = PTR_ERR(rk_phy->phy); |
| 158 | goto put_child; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 159 | } |
| 160 | phy_set_drvdata(rk_phy->phy, rk_phy); |
huang lin | 08db7e5 | 2015-07-17 15:29:25 +0800 | [diff] [blame] | 161 | |
| 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 Lawall | f6f31af | 2015-11-16 12:33:17 +0100 | [diff] [blame] | 165 | goto put_child; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); |
| 169 | return PTR_ERR_OR_ZERO(phy_provider); |
Julia Lawall | f6f31af | 2015-11-16 12:33:17 +0100 | [diff] [blame] | 170 | put_child: |
| 171 | of_node_put(child); |
| 172 | return err; |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static const struct of_device_id rockchip_usb_phy_dt_ids[] = { |
| 176 | { .compatible = "rockchip,rk3288-usb-phy" }, |
| 177 | {} |
| 178 | }; |
| 179 | |
| 180 | MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids); |
| 181 | |
| 182 | static struct platform_driver rockchip_usb_driver = { |
| 183 | .probe = rockchip_usb_phy_probe, |
| 184 | .driver = { |
| 185 | .name = "rockchip-usb-phy", |
Yunzhi Li | 64d1140 | 2014-12-12 23:07:46 +0800 | [diff] [blame] | 186 | .of_match_table = rockchip_usb_phy_dt_ids, |
| 187 | }, |
| 188 | }; |
| 189 | |
| 190 | module_platform_driver(rockchip_usb_driver); |
| 191 | |
| 192 | MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>"); |
| 193 | MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver"); |
| 194 | MODULE_LICENSE("GPL v2"); |