blob: 211703c87fb98f3da53db6db4ec1796deeeb41a3 [file] [log] [blame]
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +05301/*
2 * phy-ti-pipe3 - PIPE3 PHY driver.
3 *
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Author: Kishon Vijay Abraham I <kishon@ti.com>
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/phy/phy.h>
23#include <linux/of.h>
24#include <linux/clk.h>
25#include <linux/err.h>
26#include <linux/io.h>
27#include <linux/pm_runtime.h>
28#include <linux/delay.h>
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +020029#include <linux/phy/omap_control_phy.h>
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053030#include <linux/of_platform.h>
31
32#define PLL_STATUS 0x00000004
33#define PLL_GO 0x00000008
34#define PLL_CONFIGURATION1 0x0000000C
35#define PLL_CONFIGURATION2 0x00000010
36#define PLL_CONFIGURATION3 0x00000014
37#define PLL_CONFIGURATION4 0x00000020
38
39#define PLL_REGM_MASK 0x001FFE00
40#define PLL_REGM_SHIFT 0x9
41#define PLL_REGM_F_MASK 0x0003FFFF
42#define PLL_REGM_F_SHIFT 0x0
43#define PLL_REGN_MASK 0x000001FE
44#define PLL_REGN_SHIFT 0x1
45#define PLL_SELFREQDCO_MASK 0x0000000E
46#define PLL_SELFREQDCO_SHIFT 0x1
47#define PLL_SD_MASK 0x0003FC00
Roger Quadros1562864f2014-03-07 11:27:09 +053048#define PLL_SD_SHIFT 10
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053049#define SET_PLL_GO 0x1
50#define PLL_TICOPWDN 0x10000
51#define PLL_LOCK 0x2
52#define PLL_IDLE 0x1
53
54/*
55 * This is an Empirical value that works, need to confirm the actual
56 * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
57 * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
58 */
59# define PLL_IDLE_TIME 100;
60
61struct pipe3_dpll_params {
62 u16 m;
63 u8 n;
64 u8 freq:3;
65 u8 sd;
66 u32 mf;
67};
68
69struct ti_pipe3 {
70 void __iomem *pll_ctrl_base;
71 struct device *dev;
72 struct device *control_dev;
73 struct clk *wkupclk;
74 struct clk *sys_clk;
Roger Quadros1562864f2014-03-07 11:27:09 +053075 struct clk *refclk;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053076};
77
78struct pipe3_dpll_map {
79 unsigned long rate;
80 struct pipe3_dpll_params params;
81};
82
83static struct pipe3_dpll_map dpll_map[] = {
84 {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
85 {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
86 {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
87 {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
88 {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
89 {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
90};
91
92static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
93{
94 return __raw_readl(addr + offset);
95}
96
97static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
98 u32 data)
99{
100 __raw_writel(data, addr + offset);
101}
102
103static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(unsigned long rate)
104{
105 int i;
106
107 for (i = 0; i < ARRAY_SIZE(dpll_map); i++) {
108 if (rate == dpll_map[i].rate)
109 return &dpll_map[i].params;
110 }
111
112 return NULL;
113}
114
115static int ti_pipe3_power_off(struct phy *x)
116{
117 struct ti_pipe3 *phy = phy_get_drvdata(x);
118 int val;
119 int timeout = PLL_IDLE_TIME;
120
121 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
122 val |= PLL_IDLE;
123 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
124
125 do {
126 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
127 if (val & PLL_TICOPWDN)
128 break;
129 udelay(5);
130 } while (--timeout);
131
132 if (!timeout) {
133 dev_err(phy->dev, "power off failed\n");
134 return -EBUSY;
135 }
136
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200137 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530138
139 return 0;
140}
141
142static int ti_pipe3_power_on(struct phy *x)
143{
144 struct ti_pipe3 *phy = phy_get_drvdata(x);
145 int val;
146 int timeout = PLL_IDLE_TIME;
147
148 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
149 val &= ~PLL_IDLE;
150 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
151
152 do {
153 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
154 if (!(val & PLL_TICOPWDN))
155 break;
156 udelay(5);
157 } while (--timeout);
158
159 if (!timeout) {
160 dev_err(phy->dev, "power on failed\n");
161 return -EBUSY;
162 }
163
164 return 0;
165}
166
167static void ti_pipe3_dpll_relock(struct ti_pipe3 *phy)
168{
169 u32 val;
170 unsigned long timeout;
171
172 ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
173
174 timeout = jiffies + msecs_to_jiffies(20);
175 do {
176 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
177 if (val & PLL_LOCK)
178 break;
179 } while (!WARN_ON(time_after(jiffies, timeout)));
180}
181
182static int ti_pipe3_dpll_lock(struct ti_pipe3 *phy)
183{
184 u32 val;
185 unsigned long rate;
186 struct pipe3_dpll_params *dpll_params;
187
188 rate = clk_get_rate(phy->sys_clk);
189 dpll_params = ti_pipe3_get_dpll_params(rate);
190 if (!dpll_params) {
191 dev_err(phy->dev,
192 "No DPLL configuration for %lu Hz SYS CLK\n", rate);
193 return -EINVAL;
194 }
195
196 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
197 val &= ~PLL_REGN_MASK;
198 val |= dpll_params->n << PLL_REGN_SHIFT;
199 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
200
201 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
202 val &= ~PLL_SELFREQDCO_MASK;
203 val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
204 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
205
206 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
207 val &= ~PLL_REGM_MASK;
208 val |= dpll_params->m << PLL_REGM_SHIFT;
209 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
210
211 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
212 val &= ~PLL_REGM_F_MASK;
213 val |= dpll_params->mf << PLL_REGM_F_SHIFT;
214 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
215
216 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
217 val &= ~PLL_SD_MASK;
218 val |= dpll_params->sd << PLL_SD_SHIFT;
219 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
220
221 ti_pipe3_dpll_relock(phy);
222
223 return 0;
224}
225
226static int ti_pipe3_init(struct phy *x)
227{
228 struct ti_pipe3 *phy = phy_get_drvdata(x);
229 int ret;
230
231 ret = ti_pipe3_dpll_lock(phy);
232 if (ret)
233 return ret;
234
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200235 omap_control_phy_power(phy->control_dev, 1);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530236
237 return 0;
238}
239
240static struct phy_ops ops = {
241 .init = ti_pipe3_init,
242 .power_on = ti_pipe3_power_on,
243 .power_off = ti_pipe3_power_off,
244 .owner = THIS_MODULE,
245};
246
247static int ti_pipe3_probe(struct platform_device *pdev)
248{
249 struct ti_pipe3 *phy;
250 struct phy *generic_phy;
251 struct phy_provider *phy_provider;
252 struct resource *res;
253 struct device_node *node = pdev->dev.of_node;
254 struct device_node *control_node;
255 struct platform_device *control_pdev;
256
257 if (!node)
258 return -EINVAL;
259
260 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
261 if (!phy) {
262 dev_err(&pdev->dev, "unable to alloc mem for TI PIPE3 PHY\n");
263 return -ENOMEM;
264 }
265
266 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll_ctrl");
267 phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res);
268 if (IS_ERR(phy->pll_ctrl_base))
269 return PTR_ERR(phy->pll_ctrl_base);
270
271 phy->dev = &pdev->dev;
272
Roger Quadros1562864f2014-03-07 11:27:09 +0530273 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530274 if (IS_ERR(phy->wkupclk)) {
Roger Quadros1562864f2014-03-07 11:27:09 +0530275 dev_err(&pdev->dev, "unable to get wkupclk\n");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530276 return PTR_ERR(phy->wkupclk);
277 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530278
Roger Quadros1562864f2014-03-07 11:27:09 +0530279 phy->refclk = devm_clk_get(phy->dev, "refclk");
280 if (IS_ERR(phy->refclk)) {
281 dev_err(&pdev->dev, "unable to get refclk\n");
282 return PTR_ERR(phy->refclk);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530283 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530284
Roger Quadros1562864f2014-03-07 11:27:09 +0530285 phy->sys_clk = devm_clk_get(phy->dev, "sysclk");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530286 if (IS_ERR(phy->sys_clk)) {
Roger Quadros1562864f2014-03-07 11:27:09 +0530287 dev_err(&pdev->dev, "unable to get sysclk\n");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530288 return -EINVAL;
289 }
290
291 control_node = of_parse_phandle(node, "ctrl-module", 0);
292 if (!control_node) {
293 dev_err(&pdev->dev, "Failed to get control device phandle\n");
294 return -EINVAL;
295 }
296
297 control_pdev = of_find_device_by_node(control_node);
298 if (!control_pdev) {
299 dev_err(&pdev->dev, "Failed to get control device\n");
300 return -EINVAL;
301 }
302
303 phy->control_dev = &control_pdev->dev;
304
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200305 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530306
307 platform_set_drvdata(pdev, phy);
308 pm_runtime_enable(phy->dev);
309
310 generic_phy = devm_phy_create(phy->dev, &ops, NULL);
311 if (IS_ERR(generic_phy))
312 return PTR_ERR(generic_phy);
313
314 phy_set_drvdata(generic_phy, phy);
315 phy_provider = devm_of_phy_provider_register(phy->dev,
316 of_phy_simple_xlate);
317 if (IS_ERR(phy_provider))
318 return PTR_ERR(phy_provider);
319
320 pm_runtime_get(&pdev->dev);
321
322 return 0;
323}
324
325static int ti_pipe3_remove(struct platform_device *pdev)
326{
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530327 if (!pm_runtime_suspended(&pdev->dev))
328 pm_runtime_put(&pdev->dev);
329 pm_runtime_disable(&pdev->dev);
330
331 return 0;
332}
333
334#ifdef CONFIG_PM_RUNTIME
335
336static int ti_pipe3_runtime_suspend(struct device *dev)
337{
338 struct ti_pipe3 *phy = dev_get_drvdata(dev);
339
Roger Quadros1562864f2014-03-07 11:27:09 +0530340 if (!IS_ERR(phy->wkupclk))
341 clk_disable_unprepare(phy->wkupclk);
342 if (!IS_ERR(phy->refclk))
343 clk_disable_unprepare(phy->refclk);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530344
345 return 0;
346}
347
348static int ti_pipe3_runtime_resume(struct device *dev)
349{
350 u32 ret = 0;
351 struct ti_pipe3 *phy = dev_get_drvdata(dev);
352
Roger Quadros1562864f2014-03-07 11:27:09 +0530353 if (!IS_ERR(phy->refclk)) {
354 ret = clk_prepare_enable(phy->refclk);
355 if (ret) {
356 dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
357 goto err1;
358 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530359 }
360
Roger Quadros1562864f2014-03-07 11:27:09 +0530361 if (!IS_ERR(phy->wkupclk)) {
362 ret = clk_prepare_enable(phy->wkupclk);
363 if (ret) {
364 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
365 goto err2;
366 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530367 }
368
369 return 0;
370
371err2:
Roger Quadros1562864f2014-03-07 11:27:09 +0530372 if (!IS_ERR(phy->refclk))
373 clk_disable_unprepare(phy->refclk);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530374
375err1:
376 return ret;
377}
378
379static const struct dev_pm_ops ti_pipe3_pm_ops = {
380 SET_RUNTIME_PM_OPS(ti_pipe3_runtime_suspend,
381 ti_pipe3_runtime_resume, NULL)
382};
383
384#define DEV_PM_OPS (&ti_pipe3_pm_ops)
385#else
386#define DEV_PM_OPS NULL
387#endif
388
389#ifdef CONFIG_OF
390static const struct of_device_id ti_pipe3_id_table[] = {
391 { .compatible = "ti,phy-usb3" },
392 { .compatible = "ti,omap-usb3" },
393 {}
394};
395MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
396#endif
397
398static struct platform_driver ti_pipe3_driver = {
399 .probe = ti_pipe3_probe,
400 .remove = ti_pipe3_remove,
401 .driver = {
402 .name = "ti-pipe3",
403 .owner = THIS_MODULE,
404 .pm = DEV_PM_OPS,
405 .of_match_table = of_match_ptr(ti_pipe3_id_table),
406 },
407};
408
409module_platform_driver(ti_pipe3_driver);
410
411MODULE_ALIAS("platform: ti_pipe3");
412MODULE_AUTHOR("Texas Instruments Inc.");
413MODULE_DESCRIPTION("TI PIPE3 phy driver");
414MODULE_LICENSE("GPL v2");