Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 1 | /* |
| 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 I | 14da699 | 2014-03-06 16:38:37 +0200 | [diff] [blame] | 29 | #include <linux/phy/omap_control_phy.h> |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 30 | #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 Quadros | 1562864f | 2014-03-07 11:27:09 +0530 | [diff] [blame] | 48 | #define PLL_SD_SHIFT 10 |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 49 | #define SET_PLL_GO 0x1 |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 50 | #define PLL_LDOPWDN BIT(15) |
| 51 | #define PLL_TICOPWDN BIT(16) |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 52 | #define PLL_LOCK 0x2 |
| 53 | #define PLL_IDLE 0x1 |
| 54 | |
| 55 | /* |
| 56 | * This is an Empirical value that works, need to confirm the actual |
| 57 | * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status |
| 58 | * to be correctly reflected in the PIPE3PHY_PLL_STATUS register. |
| 59 | */ |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 60 | #define PLL_IDLE_TIME 100 /* in milliseconds */ |
| 61 | #define PLL_LOCK_TIME 100 /* in milliseconds */ |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 62 | |
| 63 | struct pipe3_dpll_params { |
| 64 | u16 m; |
| 65 | u8 n; |
| 66 | u8 freq:3; |
| 67 | u8 sd; |
| 68 | u32 mf; |
| 69 | }; |
| 70 | |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 71 | struct pipe3_dpll_map { |
| 72 | unsigned long rate; |
| 73 | struct pipe3_dpll_params params; |
| 74 | }; |
| 75 | |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 76 | struct ti_pipe3 { |
| 77 | void __iomem *pll_ctrl_base; |
| 78 | struct device *dev; |
| 79 | struct device *control_dev; |
| 80 | struct clk *wkupclk; |
| 81 | struct clk *sys_clk; |
Roger Quadros | 1562864f | 2014-03-07 11:27:09 +0530 | [diff] [blame] | 82 | struct clk *refclk; |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 83 | struct clk *div_clk; |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 84 | struct pipe3_dpll_map *dpll_map; |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 85 | u8 id; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 86 | }; |
| 87 | |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 88 | static struct pipe3_dpll_map dpll_map_usb[] = { |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 89 | {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */ |
| 90 | {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */ |
| 91 | {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */ |
| 92 | {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */ |
| 93 | {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */ |
| 94 | {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */ |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 95 | { }, /* Terminator */ |
| 96 | }; |
| 97 | |
| 98 | static struct pipe3_dpll_map dpll_map_sata[] = { |
| 99 | {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */ |
| 100 | {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */ |
| 101 | {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */ |
| 102 | {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */ |
| 103 | {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */ |
| 104 | {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */ |
| 105 | { }, /* Terminator */ |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset) |
| 109 | { |
| 110 | return __raw_readl(addr + offset); |
| 111 | } |
| 112 | |
| 113 | static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset, |
| 114 | u32 data) |
| 115 | { |
| 116 | __raw_writel(data, addr + offset); |
| 117 | } |
| 118 | |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 119 | static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy) |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 120 | { |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 121 | unsigned long rate; |
| 122 | struct pipe3_dpll_map *dpll_map = phy->dpll_map; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 123 | |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 124 | rate = clk_get_rate(phy->sys_clk); |
| 125 | |
| 126 | for (; dpll_map->rate; dpll_map++) { |
| 127 | if (rate == dpll_map->rate) |
| 128 | return &dpll_map->params; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 129 | } |
| 130 | |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 131 | dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate); |
| 132 | |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | static int ti_pipe3_power_off(struct phy *x) |
| 137 | { |
| 138 | struct ti_pipe3 *phy = phy_get_drvdata(x); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 139 | |
Kishon Vijay Abraham I | 14da699 | 2014-03-06 16:38:37 +0200 | [diff] [blame] | 140 | omap_control_phy_power(phy->control_dev, 0); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int ti_pipe3_power_on(struct phy *x) |
| 146 | { |
| 147 | struct ti_pipe3 *phy = phy_get_drvdata(x); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 148 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 149 | omap_control_phy_power(phy->control_dev, 1); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 150 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int ti_pipe3_dpll_wait_lock(struct ti_pipe3 *phy) |
| 155 | { |
| 156 | u32 val; |
| 157 | unsigned long timeout; |
| 158 | |
| 159 | timeout = jiffies + msecs_to_jiffies(PLL_LOCK_TIME); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 160 | do { |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 161 | cpu_relax(); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 162 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS); |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 163 | if (val & PLL_LOCK) |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 164 | break; |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 165 | } while (!time_after(jiffies, timeout)); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 166 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 167 | if (!(val & PLL_LOCK)) { |
| 168 | dev_err(phy->dev, "DPLL failed to lock\n"); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 169 | return -EBUSY; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 175 | static int ti_pipe3_dpll_program(struct ti_pipe3 *phy) |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 176 | { |
| 177 | u32 val; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 178 | struct pipe3_dpll_params *dpll_params; |
| 179 | |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 180 | dpll_params = ti_pipe3_get_dpll_params(phy); |
| 181 | if (!dpll_params) |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 182 | return -EINVAL; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 183 | |
| 184 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1); |
| 185 | val &= ~PLL_REGN_MASK; |
| 186 | val |= dpll_params->n << PLL_REGN_SHIFT; |
| 187 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val); |
| 188 | |
| 189 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2); |
| 190 | val &= ~PLL_SELFREQDCO_MASK; |
| 191 | val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT; |
| 192 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val); |
| 193 | |
| 194 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1); |
| 195 | val &= ~PLL_REGM_MASK; |
| 196 | val |= dpll_params->m << PLL_REGM_SHIFT; |
| 197 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val); |
| 198 | |
| 199 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4); |
| 200 | val &= ~PLL_REGM_F_MASK; |
| 201 | val |= dpll_params->mf << PLL_REGM_F_SHIFT; |
| 202 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val); |
| 203 | |
| 204 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3); |
| 205 | val &= ~PLL_SD_MASK; |
| 206 | val |= dpll_params->sd << PLL_SD_SHIFT; |
| 207 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val); |
| 208 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 209 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 210 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 211 | return ti_pipe3_dpll_wait_lock(phy); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static int ti_pipe3_init(struct phy *x) |
| 215 | { |
| 216 | struct ti_pipe3 *phy = phy_get_drvdata(x); |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 217 | u32 val; |
| 218 | int ret = 0; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 219 | |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 220 | if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) |
| 221 | return 0; |
| 222 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 223 | /* Bring it out of IDLE if it is IDLE */ |
| 224 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2); |
| 225 | if (val & PLL_IDLE) { |
| 226 | val &= ~PLL_IDLE; |
| 227 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val); |
| 228 | ret = ti_pipe3_dpll_wait_lock(phy); |
| 229 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 230 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 231 | /* Program the DPLL only if not locked */ |
| 232 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS); |
| 233 | if (!(val & PLL_LOCK)) |
| 234 | if (ti_pipe3_dpll_program(phy)) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static int ti_pipe3_exit(struct phy *x) |
| 241 | { |
| 242 | struct ti_pipe3 *phy = phy_get_drvdata(x); |
| 243 | u32 val; |
| 244 | unsigned long timeout; |
| 245 | |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 246 | /* SATA DPLL can't be powered down due to Errata i783 and PCIe |
| 247 | * does not have internal DPLL |
| 248 | */ |
| 249 | if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata") || |
| 250 | of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) |
Roger Quadros | 56042e4 | 2014-03-06 16:38:44 +0200 | [diff] [blame] | 251 | return 0; |
| 252 | |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 253 | /* Put DPLL in IDLE mode */ |
| 254 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2); |
| 255 | val |= PLL_IDLE; |
| 256 | ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val); |
| 257 | |
| 258 | /* wait for LDO and Oscillator to power down */ |
| 259 | timeout = jiffies + msecs_to_jiffies(PLL_IDLE_TIME); |
| 260 | do { |
| 261 | cpu_relax(); |
| 262 | val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS); |
| 263 | if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN)) |
| 264 | break; |
| 265 | } while (!time_after(jiffies, timeout)); |
| 266 | |
| 267 | if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) { |
| 268 | dev_err(phy->dev, "Failed to power down: PLL_STATUS 0x%x\n", |
| 269 | val); |
| 270 | return -EBUSY; |
| 271 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 275 | static struct phy_ops ops = { |
| 276 | .init = ti_pipe3_init, |
Roger Quadros | 629138d | 2014-03-06 16:38:43 +0200 | [diff] [blame] | 277 | .exit = ti_pipe3_exit, |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 278 | .power_on = ti_pipe3_power_on, |
| 279 | .power_off = ti_pipe3_power_off, |
| 280 | .owner = THIS_MODULE, |
| 281 | }; |
| 282 | |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 283 | #ifdef CONFIG_OF |
| 284 | static const struct of_device_id ti_pipe3_id_table[]; |
| 285 | #endif |
| 286 | |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 287 | static int ti_pipe3_probe(struct platform_device *pdev) |
| 288 | { |
| 289 | struct ti_pipe3 *phy; |
| 290 | struct phy *generic_phy; |
| 291 | struct phy_provider *phy_provider; |
| 292 | struct resource *res; |
| 293 | struct device_node *node = pdev->dev.of_node; |
| 294 | struct device_node *control_node; |
| 295 | struct platform_device *control_pdev; |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 296 | const struct of_device_id *match; |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 297 | struct clk *clk; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 298 | |
| 299 | phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL); |
| 300 | if (!phy) { |
| 301 | dev_err(&pdev->dev, "unable to alloc mem for TI PIPE3 PHY\n"); |
| 302 | return -ENOMEM; |
| 303 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 304 | phy->dev = &pdev->dev; |
| 305 | |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 306 | if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie")) { |
| 307 | match = of_match_device(of_match_ptr(ti_pipe3_id_table), |
| 308 | &pdev->dev); |
| 309 | if (!match) |
| 310 | return -EINVAL; |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 311 | |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 312 | phy->dpll_map = (struct pipe3_dpll_map *)match->data; |
| 313 | if (!phy->dpll_map) { |
| 314 | dev_err(&pdev->dev, "no DPLL data\n"); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | |
| 318 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 319 | "pll_ctrl"); |
| 320 | phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res); |
| 321 | if (IS_ERR(phy->pll_ctrl_base)) |
| 322 | return PTR_ERR(phy->pll_ctrl_base); |
| 323 | |
| 324 | phy->sys_clk = devm_clk_get(phy->dev, "sysclk"); |
| 325 | if (IS_ERR(phy->sys_clk)) { |
| 326 | dev_err(&pdev->dev, "unable to get sysclk\n"); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) { |
Roger Quadros | 9c7f044 | 2014-03-06 16:38:42 +0200 | [diff] [blame] | 332 | phy->wkupclk = devm_clk_get(phy->dev, "wkupclk"); |
| 333 | if (IS_ERR(phy->wkupclk)) { |
| 334 | dev_err(&pdev->dev, "unable to get wkupclk\n"); |
| 335 | return PTR_ERR(phy->wkupclk); |
| 336 | } |
| 337 | |
| 338 | phy->refclk = devm_clk_get(phy->dev, "refclk"); |
| 339 | if (IS_ERR(phy->refclk)) { |
| 340 | dev_err(&pdev->dev, "unable to get refclk\n"); |
| 341 | return PTR_ERR(phy->refclk); |
| 342 | } |
| 343 | } else { |
| 344 | phy->wkupclk = ERR_PTR(-ENODEV); |
| 345 | phy->refclk = ERR_PTR(-ENODEV); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 346 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 347 | |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 348 | if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) { |
| 349 | if (of_property_read_u8(node, "id", &phy->id) < 0) |
| 350 | phy->id = 1; |
| 351 | |
| 352 | clk = devm_clk_get(phy->dev, "dpll_ref"); |
| 353 | if (IS_ERR(clk)) { |
| 354 | dev_err(&pdev->dev, "unable to get dpll ref clk\n"); |
| 355 | return PTR_ERR(clk); |
| 356 | } |
| 357 | clk_set_rate(clk, 1500000000); |
| 358 | |
| 359 | clk = devm_clk_get(phy->dev, "dpll_ref_m2"); |
| 360 | if (IS_ERR(clk)) { |
| 361 | dev_err(&pdev->dev, "unable to get dpll ref m2 clk\n"); |
| 362 | return PTR_ERR(clk); |
| 363 | } |
| 364 | clk_set_rate(clk, 100000000); |
| 365 | |
| 366 | clk = devm_clk_get(phy->dev, "phy-div"); |
| 367 | if (IS_ERR(clk)) { |
| 368 | dev_err(&pdev->dev, "unable to get phy-div clk\n"); |
| 369 | return PTR_ERR(clk); |
| 370 | } |
| 371 | clk_set_rate(clk, 100000000); |
| 372 | |
| 373 | phy->div_clk = devm_clk_get(phy->dev, "div-clk"); |
| 374 | if (IS_ERR(phy->div_clk)) { |
| 375 | dev_err(&pdev->dev, "unable to get div-clk\n"); |
| 376 | return PTR_ERR(phy->div_clk); |
| 377 | } |
| 378 | } else { |
| 379 | phy->div_clk = ERR_PTR(-ENODEV); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | control_node = of_parse_phandle(node, "ctrl-module", 0); |
| 383 | if (!control_node) { |
| 384 | dev_err(&pdev->dev, "Failed to get control device phandle\n"); |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | |
| 388 | control_pdev = of_find_device_by_node(control_node); |
| 389 | if (!control_pdev) { |
| 390 | dev_err(&pdev->dev, "Failed to get control device\n"); |
| 391 | return -EINVAL; |
| 392 | } |
| 393 | |
| 394 | phy->control_dev = &control_pdev->dev; |
| 395 | |
Kishon Vijay Abraham I | 14da699 | 2014-03-06 16:38:37 +0200 | [diff] [blame] | 396 | omap_control_phy_power(phy->control_dev, 0); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 397 | |
| 398 | platform_set_drvdata(pdev, phy); |
| 399 | pm_runtime_enable(phy->dev); |
| 400 | |
| 401 | generic_phy = devm_phy_create(phy->dev, &ops, NULL); |
| 402 | if (IS_ERR(generic_phy)) |
| 403 | return PTR_ERR(generic_phy); |
| 404 | |
| 405 | phy_set_drvdata(generic_phy, phy); |
| 406 | phy_provider = devm_of_phy_provider_register(phy->dev, |
| 407 | of_phy_simple_xlate); |
| 408 | if (IS_ERR(phy_provider)) |
| 409 | return PTR_ERR(phy_provider); |
| 410 | |
| 411 | pm_runtime_get(&pdev->dev); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int ti_pipe3_remove(struct platform_device *pdev) |
| 417 | { |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 418 | if (!pm_runtime_suspended(&pdev->dev)) |
| 419 | pm_runtime_put(&pdev->dev); |
| 420 | pm_runtime_disable(&pdev->dev); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | #ifdef CONFIG_PM_RUNTIME |
| 426 | |
| 427 | static int ti_pipe3_runtime_suspend(struct device *dev) |
| 428 | { |
| 429 | struct ti_pipe3 *phy = dev_get_drvdata(dev); |
| 430 | |
Roger Quadros | 1562864f | 2014-03-07 11:27:09 +0530 | [diff] [blame] | 431 | if (!IS_ERR(phy->wkupclk)) |
| 432 | clk_disable_unprepare(phy->wkupclk); |
| 433 | if (!IS_ERR(phy->refclk)) |
| 434 | clk_disable_unprepare(phy->refclk); |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 435 | if (!IS_ERR(phy->div_clk)) |
| 436 | clk_disable_unprepare(phy->div_clk); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int ti_pipe3_runtime_resume(struct device *dev) |
| 442 | { |
| 443 | u32 ret = 0; |
| 444 | struct ti_pipe3 *phy = dev_get_drvdata(dev); |
| 445 | |
Roger Quadros | 1562864f | 2014-03-07 11:27:09 +0530 | [diff] [blame] | 446 | if (!IS_ERR(phy->refclk)) { |
| 447 | ret = clk_prepare_enable(phy->refclk); |
| 448 | if (ret) { |
| 449 | dev_err(phy->dev, "Failed to enable refclk %d\n", ret); |
| 450 | goto err1; |
| 451 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 452 | } |
| 453 | |
Roger Quadros | 1562864f | 2014-03-07 11:27:09 +0530 | [diff] [blame] | 454 | if (!IS_ERR(phy->wkupclk)) { |
| 455 | ret = clk_prepare_enable(phy->wkupclk); |
| 456 | if (ret) { |
| 457 | dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret); |
| 458 | goto err2; |
| 459 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 460 | } |
| 461 | |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 462 | if (!IS_ERR(phy->div_clk)) { |
| 463 | ret = clk_prepare_enable(phy->div_clk); |
| 464 | if (ret) { |
| 465 | dev_err(phy->dev, "Failed to enable div_clk %d\n", ret); |
| 466 | goto err3; |
| 467 | } |
| 468 | } |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 469 | return 0; |
| 470 | |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 471 | err3: |
| 472 | if (!IS_ERR(phy->wkupclk)) |
| 473 | clk_disable_unprepare(phy->wkupclk); |
| 474 | |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 475 | err2: |
Roger Quadros | 1562864f | 2014-03-07 11:27:09 +0530 | [diff] [blame] | 476 | if (!IS_ERR(phy->refclk)) |
| 477 | clk_disable_unprepare(phy->refclk); |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 478 | |
| 479 | err1: |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static const struct dev_pm_ops ti_pipe3_pm_ops = { |
| 484 | SET_RUNTIME_PM_OPS(ti_pipe3_runtime_suspend, |
| 485 | ti_pipe3_runtime_resume, NULL) |
| 486 | }; |
| 487 | |
| 488 | #define DEV_PM_OPS (&ti_pipe3_pm_ops) |
| 489 | #else |
| 490 | #define DEV_PM_OPS NULL |
| 491 | #endif |
| 492 | |
| 493 | #ifdef CONFIG_OF |
| 494 | static const struct of_device_id ti_pipe3_id_table[] = { |
Roger Quadros | 61f5467 | 2014-03-07 11:43:39 +0530 | [diff] [blame] | 495 | { |
| 496 | .compatible = "ti,phy-usb3", |
| 497 | .data = dpll_map_usb, |
| 498 | }, |
| 499 | { |
| 500 | .compatible = "ti,omap-usb3", |
| 501 | .data = dpll_map_usb, |
| 502 | }, |
| 503 | { |
| 504 | .compatible = "ti,phy-pipe3-sata", |
| 505 | .data = dpll_map_sata, |
| 506 | }, |
Kishon Vijay Abraham I | 99bbd48 | 2014-06-25 23:22:56 +0530 | [diff] [blame^] | 507 | { |
| 508 | .compatible = "ti,phy-pipe3-pcie", |
| 509 | }, |
Kishon Vijay Abraham I | a70143b | 2014-03-03 17:08:12 +0530 | [diff] [blame] | 510 | {} |
| 511 | }; |
| 512 | MODULE_DEVICE_TABLE(of, ti_pipe3_id_table); |
| 513 | #endif |
| 514 | |
| 515 | static struct platform_driver ti_pipe3_driver = { |
| 516 | .probe = ti_pipe3_probe, |
| 517 | .remove = ti_pipe3_remove, |
| 518 | .driver = { |
| 519 | .name = "ti-pipe3", |
| 520 | .owner = THIS_MODULE, |
| 521 | .pm = DEV_PM_OPS, |
| 522 | .of_match_table = of_match_ptr(ti_pipe3_id_table), |
| 523 | }, |
| 524 | }; |
| 525 | |
| 526 | module_platform_driver(ti_pipe3_driver); |
| 527 | |
| 528 | MODULE_ALIAS("platform: ti_pipe3"); |
| 529 | MODULE_AUTHOR("Texas Instruments Inc."); |
| 530 | MODULE_DESCRIPTION("TI PIPE3 phy driver"); |
| 531 | MODULE_LICENSE("GPL v2"); |