blob: ad3fbc80e044e68e6b907d0837f6d1d7460be7b9 [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>
Roger Quadros6e738432015-01-13 14:23:19 +020031#include <linux/spinlock.h>
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053032
33#define PLL_STATUS 0x00000004
34#define PLL_GO 0x00000008
35#define PLL_CONFIGURATION1 0x0000000C
36#define PLL_CONFIGURATION2 0x00000010
37#define PLL_CONFIGURATION3 0x00000014
38#define PLL_CONFIGURATION4 0x00000020
39
40#define PLL_REGM_MASK 0x001FFE00
41#define PLL_REGM_SHIFT 0x9
42#define PLL_REGM_F_MASK 0x0003FFFF
43#define PLL_REGM_F_SHIFT 0x0
44#define PLL_REGN_MASK 0x000001FE
45#define PLL_REGN_SHIFT 0x1
46#define PLL_SELFREQDCO_MASK 0x0000000E
47#define PLL_SELFREQDCO_SHIFT 0x1
48#define PLL_SD_MASK 0x0003FC00
Roger Quadros1562864f2014-03-07 11:27:09 +053049#define PLL_SD_SHIFT 10
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053050#define SET_PLL_GO 0x1
Roger Quadros629138d2014-03-06 16:38:43 +020051#define PLL_LDOPWDN BIT(15)
52#define PLL_TICOPWDN BIT(16)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053053#define PLL_LOCK 0x2
54#define PLL_IDLE 0x1
55
56/*
57 * This is an Empirical value that works, need to confirm the actual
58 * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
59 * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
60 */
Roger Quadros629138d2014-03-06 16:38:43 +020061#define PLL_IDLE_TIME 100 /* in milliseconds */
62#define PLL_LOCK_TIME 100 /* in milliseconds */
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053063
64struct pipe3_dpll_params {
65 u16 m;
66 u8 n;
67 u8 freq:3;
68 u8 sd;
69 u32 mf;
70};
71
Roger Quadros61f54672014-03-07 11:43:39 +053072struct pipe3_dpll_map {
73 unsigned long rate;
74 struct pipe3_dpll_params params;
75};
76
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053077struct ti_pipe3 {
78 void __iomem *pll_ctrl_base;
79 struct device *dev;
80 struct device *control_dev;
81 struct clk *wkupclk;
82 struct clk *sys_clk;
Roger Quadros1562864f2014-03-07 11:27:09 +053083 struct clk *refclk;
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +053084 struct clk *div_clk;
Roger Quadros61f54672014-03-07 11:43:39 +053085 struct pipe3_dpll_map *dpll_map;
Roger Quadros6e738432015-01-13 14:23:19 +020086 bool enabled;
87 spinlock_t lock; /* serialize clock enable/disable */
Roger Quadros7f339122015-01-13 14:23:20 +020088 /* the below flag is needed specifically for SATA */
89 bool refclk_enabled;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053090};
91
Roger Quadros61f54672014-03-07 11:43:39 +053092static struct pipe3_dpll_map dpll_map_usb[] = {
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053093 {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
94 {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
95 {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
96 {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
97 {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
98 {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
Roger Quadros61f54672014-03-07 11:43:39 +053099 { }, /* Terminator */
100};
101
102static struct pipe3_dpll_map dpll_map_sata[] = {
103 {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */
104 {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */
105 {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */
106 {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */
107 {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */
108 {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */
109 { }, /* Terminator */
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530110};
111
112static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
113{
114 return __raw_readl(addr + offset);
115}
116
117static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
118 u32 data)
119{
120 __raw_writel(data, addr + offset);
121}
122
Roger Quadros61f54672014-03-07 11:43:39 +0530123static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530124{
Roger Quadros61f54672014-03-07 11:43:39 +0530125 unsigned long rate;
126 struct pipe3_dpll_map *dpll_map = phy->dpll_map;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530127
Roger Quadros61f54672014-03-07 11:43:39 +0530128 rate = clk_get_rate(phy->sys_clk);
129
130 for (; dpll_map->rate; dpll_map++) {
131 if (rate == dpll_map->rate)
132 return &dpll_map->params;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530133 }
134
Roger Quadros61f54672014-03-07 11:43:39 +0530135 dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
136
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530137 return NULL;
138}
139
140static int ti_pipe3_power_off(struct phy *x)
141{
142 struct ti_pipe3 *phy = phy_get_drvdata(x);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530143
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200144 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530145
146 return 0;
147}
148
149static int ti_pipe3_power_on(struct phy *x)
150{
151 struct ti_pipe3 *phy = phy_get_drvdata(x);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530152
Roger Quadros629138d2014-03-06 16:38:43 +0200153 omap_control_phy_power(phy->control_dev, 1);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530154
Roger Quadros629138d2014-03-06 16:38:43 +0200155 return 0;
156}
157
158static int ti_pipe3_dpll_wait_lock(struct ti_pipe3 *phy)
159{
160 u32 val;
161 unsigned long timeout;
162
163 timeout = jiffies + msecs_to_jiffies(PLL_LOCK_TIME);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530164 do {
Roger Quadros629138d2014-03-06 16:38:43 +0200165 cpu_relax();
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530166 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
Roger Quadros629138d2014-03-06 16:38:43 +0200167 if (val & PLL_LOCK)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530168 break;
Roger Quadros629138d2014-03-06 16:38:43 +0200169 } while (!time_after(jiffies, timeout));
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530170
Roger Quadros629138d2014-03-06 16:38:43 +0200171 if (!(val & PLL_LOCK)) {
172 dev_err(phy->dev, "DPLL failed to lock\n");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530173 return -EBUSY;
174 }
175
176 return 0;
177}
178
Roger Quadros629138d2014-03-06 16:38:43 +0200179static int ti_pipe3_dpll_program(struct ti_pipe3 *phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530180{
181 u32 val;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530182 struct pipe3_dpll_params *dpll_params;
183
Roger Quadros61f54672014-03-07 11:43:39 +0530184 dpll_params = ti_pipe3_get_dpll_params(phy);
185 if (!dpll_params)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530186 return -EINVAL;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530187
188 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
189 val &= ~PLL_REGN_MASK;
190 val |= dpll_params->n << PLL_REGN_SHIFT;
191 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
192
193 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
194 val &= ~PLL_SELFREQDCO_MASK;
195 val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
196 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
197
198 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
199 val &= ~PLL_REGM_MASK;
200 val |= dpll_params->m << PLL_REGM_SHIFT;
201 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
202
203 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
204 val &= ~PLL_REGM_F_MASK;
205 val |= dpll_params->mf << PLL_REGM_F_SHIFT;
206 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
207
208 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
209 val &= ~PLL_SD_MASK;
210 val |= dpll_params->sd << PLL_SD_SHIFT;
211 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
212
Roger Quadros629138d2014-03-06 16:38:43 +0200213 ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530214
Roger Quadros629138d2014-03-06 16:38:43 +0200215 return ti_pipe3_dpll_wait_lock(phy);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530216}
217
218static int ti_pipe3_init(struct phy *x)
219{
220 struct ti_pipe3 *phy = phy_get_drvdata(x);
Roger Quadros629138d2014-03-06 16:38:43 +0200221 u32 val;
222 int ret = 0;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530223
Vignesh R0bc09f92014-12-16 14:52:50 +0530224 /*
225 * Set pcie_pcs register to 0x96 for proper functioning of phy
226 * as recommended in AM572x TRM SPRUHZ6, section 18.5.2.2, table
227 * 18-1804.
228 */
Kishon Vijay Abraham If0e2cf72014-06-25 23:22:57 +0530229 if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
Vignesh R0bc09f92014-12-16 14:52:50 +0530230 omap_control_pcie_pcs(phy->control_dev, 0x96);
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530231 return 0;
Kishon Vijay Abraham If0e2cf72014-06-25 23:22:57 +0530232 }
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530233
Roger Quadros629138d2014-03-06 16:38:43 +0200234 /* Bring it out of IDLE if it is IDLE */
235 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
236 if (val & PLL_IDLE) {
237 val &= ~PLL_IDLE;
238 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
239 ret = ti_pipe3_dpll_wait_lock(phy);
240 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530241
Roger Quadros629138d2014-03-06 16:38:43 +0200242 /* Program the DPLL only if not locked */
243 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
244 if (!(val & PLL_LOCK))
245 if (ti_pipe3_dpll_program(phy))
246 return -EINVAL;
247
248 return ret;
249}
250
251static int ti_pipe3_exit(struct phy *x)
252{
253 struct ti_pipe3 *phy = phy_get_drvdata(x);
254 u32 val;
255 unsigned long timeout;
256
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530257 /* SATA DPLL can't be powered down due to Errata i783 and PCIe
258 * does not have internal DPLL
259 */
260 if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata") ||
261 of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie"))
Roger Quadros56042e42014-03-06 16:38:44 +0200262 return 0;
263
Roger Quadros629138d2014-03-06 16:38:43 +0200264 /* Put DPLL in IDLE mode */
265 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
266 val |= PLL_IDLE;
267 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
268
269 /* wait for LDO and Oscillator to power down */
270 timeout = jiffies + msecs_to_jiffies(PLL_IDLE_TIME);
271 do {
272 cpu_relax();
273 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
274 if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN))
275 break;
276 } while (!time_after(jiffies, timeout));
277
278 if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) {
279 dev_err(phy->dev, "Failed to power down: PLL_STATUS 0x%x\n",
280 val);
281 return -EBUSY;
282 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530283
284 return 0;
285}
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530286static struct phy_ops ops = {
287 .init = ti_pipe3_init,
Roger Quadros629138d2014-03-06 16:38:43 +0200288 .exit = ti_pipe3_exit,
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530289 .power_on = ti_pipe3_power_on,
290 .power_off = ti_pipe3_power_off,
291 .owner = THIS_MODULE,
292};
293
Roger Quadros61f54672014-03-07 11:43:39 +0530294static const struct of_device_id ti_pipe3_id_table[];
Roger Quadros61f54672014-03-07 11:43:39 +0530295
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530296static int ti_pipe3_probe(struct platform_device *pdev)
297{
298 struct ti_pipe3 *phy;
299 struct phy *generic_phy;
300 struct phy_provider *phy_provider;
301 struct resource *res;
302 struct device_node *node = pdev->dev.of_node;
303 struct device_node *control_node;
304 struct platform_device *control_pdev;
Roger Quadros61f54672014-03-07 11:43:39 +0530305 const struct of_device_id *match;
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530306 struct clk *clk;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530307
308 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
Peter Griffin3a4cfcb2014-08-15 13:40:11 +0100309 if (!phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530310 return -ENOMEM;
Peter Griffin3a4cfcb2014-08-15 13:40:11 +0100311
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530312 phy->dev = &pdev->dev;
Roger Quadros6e738432015-01-13 14:23:19 +0200313 spin_lock_init(&phy->lock);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530314
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530315 if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
Axel Lin298fe562015-03-05 18:20:53 +0800316 match = of_match_device(ti_pipe3_id_table, &pdev->dev);
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530317 if (!match)
318 return -EINVAL;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530319
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530320 phy->dpll_map = (struct pipe3_dpll_map *)match->data;
321 if (!phy->dpll_map) {
322 dev_err(&pdev->dev, "no DPLL data\n");
323 return -EINVAL;
324 }
325
326 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
327 "pll_ctrl");
328 phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res);
329 if (IS_ERR(phy->pll_ctrl_base))
330 return PTR_ERR(phy->pll_ctrl_base);
331
332 phy->sys_clk = devm_clk_get(phy->dev, "sysclk");
333 if (IS_ERR(phy->sys_clk)) {
334 dev_err(&pdev->dev, "unable to get sysclk\n");
335 return -EINVAL;
336 }
337 }
338
Roger Quadros7f339122015-01-13 14:23:20 +0200339 phy->refclk = devm_clk_get(phy->dev, "refclk");
340 if (IS_ERR(phy->refclk)) {
341 dev_err(&pdev->dev, "unable to get refclk\n");
342 /* older DTBs have missing refclk in SATA PHY
343 * so don't bail out in case of SATA PHY.
344 */
345 if (!of_device_is_compatible(node, "ti,phy-pipe3-sata"))
346 return PTR_ERR(phy->refclk);
347 }
348
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530349 if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
Roger Quadros9c7f0442014-03-06 16:38:42 +0200350 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
351 if (IS_ERR(phy->wkupclk)) {
352 dev_err(&pdev->dev, "unable to get wkupclk\n");
353 return PTR_ERR(phy->wkupclk);
354 }
Roger Quadros9c7f0442014-03-06 16:38:42 +0200355 } else {
356 phy->wkupclk = ERR_PTR(-ENODEV);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530357 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530358
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530359 if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530360
361 clk = devm_clk_get(phy->dev, "dpll_ref");
362 if (IS_ERR(clk)) {
363 dev_err(&pdev->dev, "unable to get dpll ref clk\n");
364 return PTR_ERR(clk);
365 }
366 clk_set_rate(clk, 1500000000);
367
368 clk = devm_clk_get(phy->dev, "dpll_ref_m2");
369 if (IS_ERR(clk)) {
370 dev_err(&pdev->dev, "unable to get dpll ref m2 clk\n");
371 return PTR_ERR(clk);
372 }
373 clk_set_rate(clk, 100000000);
374
375 clk = devm_clk_get(phy->dev, "phy-div");
376 if (IS_ERR(clk)) {
377 dev_err(&pdev->dev, "unable to get phy-div clk\n");
378 return PTR_ERR(clk);
379 }
380 clk_set_rate(clk, 100000000);
381
382 phy->div_clk = devm_clk_get(phy->dev, "div-clk");
383 if (IS_ERR(phy->div_clk)) {
384 dev_err(&pdev->dev, "unable to get div-clk\n");
385 return PTR_ERR(phy->div_clk);
386 }
387 } else {
388 phy->div_clk = ERR_PTR(-ENODEV);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530389 }
390
391 control_node = of_parse_phandle(node, "ctrl-module", 0);
392 if (!control_node) {
393 dev_err(&pdev->dev, "Failed to get control device phandle\n");
394 return -EINVAL;
395 }
396
397 control_pdev = of_find_device_by_node(control_node);
398 if (!control_pdev) {
399 dev_err(&pdev->dev, "Failed to get control device\n");
400 return -EINVAL;
401 }
402
403 phy->control_dev = &control_pdev->dev;
404
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200405 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530406
407 platform_set_drvdata(pdev, phy);
408 pm_runtime_enable(phy->dev);
409
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200410 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530411 if (IS_ERR(generic_phy))
412 return PTR_ERR(generic_phy);
413
414 phy_set_drvdata(generic_phy, phy);
415 phy_provider = devm_of_phy_provider_register(phy->dev,
416 of_phy_simple_xlate);
417 if (IS_ERR(phy_provider))
418 return PTR_ERR(phy_provider);
419
420 pm_runtime_get(&pdev->dev);
421
422 return 0;
423}
424
425static int ti_pipe3_remove(struct platform_device *pdev)
426{
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530427 if (!pm_runtime_suspended(&pdev->dev))
428 pm_runtime_put(&pdev->dev);
429 pm_runtime_disable(&pdev->dev);
430
431 return 0;
432}
433
Rafael J. Wysocki7ba37052014-12-13 00:42:12 +0100434#ifdef CONFIG_PM
Roger Quadros7f339122015-01-13 14:23:20 +0200435static int ti_pipe3_enable_refclk(struct ti_pipe3 *phy)
436{
437 if (!IS_ERR(phy->refclk) && !phy->refclk_enabled) {
438 int ret;
439
440 ret = clk_prepare_enable(phy->refclk);
441 if (ret) {
442 dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
443 return ret;
444 }
445 phy->refclk_enabled = true;
446 }
447
448 return 0;
449}
450
451static void ti_pipe3_disable_refclk(struct ti_pipe3 *phy)
452{
453 if (!IS_ERR(phy->refclk))
454 clk_disable_unprepare(phy->refclk);
455
456 phy->refclk_enabled = false;
457}
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530458
Roger Quadros6e738432015-01-13 14:23:19 +0200459static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530460{
Roger Quadros6e738432015-01-13 14:23:19 +0200461 int ret = 0;
462 unsigned long flags;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530463
Roger Quadros6e738432015-01-13 14:23:19 +0200464 spin_lock_irqsave(&phy->lock, flags);
465 if (phy->enabled)
466 goto err1;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530467
Roger Quadros7f339122015-01-13 14:23:20 +0200468 ret = ti_pipe3_enable_refclk(phy);
469 if (ret)
470 goto err1;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530471
Roger Quadros1562864f2014-03-07 11:27:09 +0530472 if (!IS_ERR(phy->wkupclk)) {
473 ret = clk_prepare_enable(phy->wkupclk);
474 if (ret) {
475 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
476 goto err2;
477 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530478 }
479
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530480 if (!IS_ERR(phy->div_clk)) {
481 ret = clk_prepare_enable(phy->div_clk);
482 if (ret) {
483 dev_err(phy->dev, "Failed to enable div_clk %d\n", ret);
484 goto err3;
485 }
486 }
Roger Quadros6e738432015-01-13 14:23:19 +0200487
488 phy->enabled = true;
489 spin_unlock_irqrestore(&phy->lock, flags);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530490 return 0;
491
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530492err3:
493 if (!IS_ERR(phy->wkupclk))
494 clk_disable_unprepare(phy->wkupclk);
495
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530496err2:
Roger Quadros1562864f2014-03-07 11:27:09 +0530497 if (!IS_ERR(phy->refclk))
498 clk_disable_unprepare(phy->refclk);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530499
Roger Quadros7f339122015-01-13 14:23:20 +0200500 ti_pipe3_disable_refclk(phy);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530501err1:
Roger Quadros6e738432015-01-13 14:23:19 +0200502 spin_unlock_irqrestore(&phy->lock, flags);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530503 return ret;
504}
505
Roger Quadros6e738432015-01-13 14:23:19 +0200506static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy)
507{
508 unsigned long flags;
509
510 spin_lock_irqsave(&phy->lock, flags);
511 if (!phy->enabled) {
512 spin_unlock_irqrestore(&phy->lock, flags);
513 return;
514 }
515
516 if (!IS_ERR(phy->wkupclk))
517 clk_disable_unprepare(phy->wkupclk);
Roger Quadros7f339122015-01-13 14:23:20 +0200518 /* Don't disable refclk for SATA PHY due to Errata i783 */
519 if (!of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata"))
520 ti_pipe3_disable_refclk(phy);
Roger Quadros6e738432015-01-13 14:23:19 +0200521 if (!IS_ERR(phy->div_clk))
522 clk_disable_unprepare(phy->div_clk);
523 phy->enabled = false;
524 spin_unlock_irqrestore(&phy->lock, flags);
525}
526
527static int ti_pipe3_runtime_suspend(struct device *dev)
528{
529 struct ti_pipe3 *phy = dev_get_drvdata(dev);
530
531 ti_pipe3_disable_clocks(phy);
532 return 0;
533}
534
535static int ti_pipe3_runtime_resume(struct device *dev)
536{
537 struct ti_pipe3 *phy = dev_get_drvdata(dev);
538 int ret = 0;
539
540 ret = ti_pipe3_enable_clocks(phy);
541 return ret;
542}
543
544static int ti_pipe3_suspend(struct device *dev)
545{
546 struct ti_pipe3 *phy = dev_get_drvdata(dev);
547
548 ti_pipe3_disable_clocks(phy);
549 return 0;
550}
551
552static int ti_pipe3_resume(struct device *dev)
553{
554 struct ti_pipe3 *phy = dev_get_drvdata(dev);
555 int ret;
556
557 ret = ti_pipe3_enable_clocks(phy);
558 if (ret)
559 return ret;
560
561 pm_runtime_disable(dev);
562 pm_runtime_set_active(dev);
563 pm_runtime_enable(dev);
564 return 0;
565}
566#endif
567
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530568static const struct dev_pm_ops ti_pipe3_pm_ops = {
569 SET_RUNTIME_PM_OPS(ti_pipe3_runtime_suspend,
570 ti_pipe3_runtime_resume, NULL)
Roger Quadros6e738432015-01-13 14:23:19 +0200571 SET_SYSTEM_SLEEP_PM_OPS(ti_pipe3_suspend, ti_pipe3_resume)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530572};
573
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530574static const struct of_device_id ti_pipe3_id_table[] = {
Roger Quadros61f54672014-03-07 11:43:39 +0530575 {
576 .compatible = "ti,phy-usb3",
577 .data = dpll_map_usb,
578 },
579 {
580 .compatible = "ti,omap-usb3",
581 .data = dpll_map_usb,
582 },
583 {
584 .compatible = "ti,phy-pipe3-sata",
585 .data = dpll_map_sata,
586 },
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530587 {
588 .compatible = "ti,phy-pipe3-pcie",
589 },
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530590 {}
591};
592MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530593
594static struct platform_driver ti_pipe3_driver = {
595 .probe = ti_pipe3_probe,
596 .remove = ti_pipe3_remove,
597 .driver = {
598 .name = "ti-pipe3",
Roger Quadros6e738432015-01-13 14:23:19 +0200599 .pm = &ti_pipe3_pm_ops,
Axel Lin298fe562015-03-05 18:20:53 +0800600 .of_match_table = ti_pipe3_id_table,
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530601 },
602};
603
604module_platform_driver(ti_pipe3_driver);
605
606MODULE_ALIAS("platform: ti_pipe3");
607MODULE_AUTHOR("Texas Instruments Inc.");
608MODULE_DESCRIPTION("TI PIPE3 phy driver");
609MODULE_LICENSE("GPL v2");