Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. |
| 5 | * Author: Sylwester Nawrocki <s.nawrocki@samsung.com> |
| 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 version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
Sachin Kamat | 22fda30 | 2014-05-29 12:00:48 +0530 | [diff] [blame] | 12 | #include <linux/err.h> |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 13 | #include <linux/io.h> |
| 14 | #include <linux/kernel.h> |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 15 | #include <linux/mfd/syscon/exynos4-pmu.h> |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_address.h> |
| 19 | #include <linux/phy/phy.h> |
| 20 | #include <linux/platform_device.h> |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 21 | #include <linux/regmap.h> |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 23 | #include <linux/mfd/syscon.h> |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 24 | |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 25 | /* MIPI_PHYn_CONTROL reg. offset (for base address from ioremap): n = 0..1 */ |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 26 | #define EXYNOS_MIPI_PHY_CONTROL(n) ((n) * 4) |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 27 | |
| 28 | enum exynos_mipi_phy_id { |
| 29 | EXYNOS_MIPI_PHY_ID_CSIS0, |
| 30 | EXYNOS_MIPI_PHY_ID_DSIM0, |
| 31 | EXYNOS_MIPI_PHY_ID_CSIS1, |
| 32 | EXYNOS_MIPI_PHY_ID_DSIM1, |
| 33 | EXYNOS_MIPI_PHYS_NUM |
| 34 | }; |
| 35 | |
| 36 | #define is_mipi_dsim_phy_id(id) \ |
| 37 | ((id) == EXYNOS_MIPI_PHY_ID_DSIM0 || (id) == EXYNOS_MIPI_PHY_ID_DSIM1) |
| 38 | |
| 39 | struct exynos_mipi_video_phy { |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 40 | struct video_phy_desc { |
| 41 | struct phy *phy; |
| 42 | unsigned int index; |
| 43 | } phys[EXYNOS_MIPI_PHYS_NUM]; |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 44 | spinlock_t slock; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 45 | void __iomem *regs; |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 46 | struct regmap *regmap; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static int __set_phy_state(struct exynos_mipi_video_phy *state, |
| 50 | enum exynos_mipi_phy_id id, unsigned int on) |
| 51 | { |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 52 | const unsigned int offset = EXYNOS4_MIPI_PHY_CONTROL(id / 2); |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 53 | void __iomem *addr; |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 54 | u32 val, reset; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 55 | |
| 56 | if (is_mipi_dsim_phy_id(id)) |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 57 | reset = EXYNOS4_MIPI_PHY_MRESETN; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 58 | else |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 59 | reset = EXYNOS4_MIPI_PHY_SRESETN; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 60 | |
Axel Lin | af69dec | 2015-02-27 11:50:57 +0800 | [diff] [blame] | 61 | spin_lock(&state->slock); |
| 62 | |
Axel Lin | cfd565d1 | 2015-02-26 11:48:08 +0800 | [diff] [blame] | 63 | if (!IS_ERR(state->regmap)) { |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 64 | regmap_read(state->regmap, offset, &val); |
| 65 | if (on) |
| 66 | val |= reset; |
| 67 | else |
| 68 | val &= ~reset; |
| 69 | regmap_write(state->regmap, offset, val); |
| 70 | if (on) |
| 71 | val |= EXYNOS4_MIPI_PHY_ENABLE; |
| 72 | else if (!(val & EXYNOS4_MIPI_PHY_RESET_MASK)) |
| 73 | val &= ~EXYNOS4_MIPI_PHY_ENABLE; |
| 74 | regmap_write(state->regmap, offset, val); |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 75 | } else { |
| 76 | addr = state->regs + EXYNOS_MIPI_PHY_CONTROL(id / 2); |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 77 | |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 78 | val = readl(addr); |
| 79 | if (on) |
| 80 | val |= reset; |
| 81 | else |
| 82 | val &= ~reset; |
| 83 | writel(val, addr); |
| 84 | /* Clear ENABLE bit only if MRESETN, SRESETN bits are not set */ |
| 85 | if (on) |
| 86 | val |= EXYNOS4_MIPI_PHY_ENABLE; |
| 87 | else if (!(val & EXYNOS4_MIPI_PHY_RESET_MASK)) |
| 88 | val &= ~EXYNOS4_MIPI_PHY_ENABLE; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 89 | |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 90 | writel(val, addr); |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Axel Lin | af69dec | 2015-02-27 11:50:57 +0800 | [diff] [blame] | 93 | spin_unlock(&state->slock); |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | #define to_mipi_video_phy(desc) \ |
| 98 | container_of((desc), struct exynos_mipi_video_phy, phys[(desc)->index]); |
| 99 | |
| 100 | static int exynos_mipi_video_phy_power_on(struct phy *phy) |
| 101 | { |
| 102 | struct video_phy_desc *phy_desc = phy_get_drvdata(phy); |
| 103 | struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc); |
| 104 | |
| 105 | return __set_phy_state(state, phy_desc->index, 1); |
| 106 | } |
| 107 | |
| 108 | static int exynos_mipi_video_phy_power_off(struct phy *phy) |
| 109 | { |
| 110 | struct video_phy_desc *phy_desc = phy_get_drvdata(phy); |
| 111 | struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc); |
| 112 | |
Sylwester Nawrocki | e3967e7 | 2013-10-16 19:03:45 +0200 | [diff] [blame] | 113 | return __set_phy_state(state, phy_desc->index, 0); |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static struct phy *exynos_mipi_video_phy_xlate(struct device *dev, |
| 117 | struct of_phandle_args *args) |
| 118 | { |
| 119 | struct exynos_mipi_video_phy *state = dev_get_drvdata(dev); |
| 120 | |
Antoine Ténart | 98c3b32 | 2014-05-12 14:56:28 +0200 | [diff] [blame] | 121 | if (WARN_ON(args->args[0] >= EXYNOS_MIPI_PHYS_NUM)) |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 122 | return ERR_PTR(-ENODEV); |
| 123 | |
| 124 | return state->phys[args->args[0]].phy; |
| 125 | } |
| 126 | |
Axel Lin | 4a9e5ca | 2015-07-15 15:33:51 +0800 | [diff] [blame] | 127 | static const struct phy_ops exynos_mipi_video_phy_ops = { |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 128 | .power_on = exynos_mipi_video_phy_power_on, |
| 129 | .power_off = exynos_mipi_video_phy_power_off, |
| 130 | .owner = THIS_MODULE, |
| 131 | }; |
| 132 | |
| 133 | static int exynos_mipi_video_phy_probe(struct platform_device *pdev) |
| 134 | { |
| 135 | struct exynos_mipi_video_phy *state; |
| 136 | struct device *dev = &pdev->dev; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 137 | struct phy_provider *phy_provider; |
| 138 | unsigned int i; |
| 139 | |
| 140 | state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL); |
| 141 | if (!state) |
| 142 | return -ENOMEM; |
| 143 | |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 144 | state->regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon"); |
| 145 | if (IS_ERR(state->regmap)) { |
| 146 | struct resource *res; |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 147 | |
Sylwester Nawrocki | e4b3d38 | 2015-01-16 18:30:37 +0100 | [diff] [blame] | 148 | dev_info(dev, "regmap lookup failed: %ld\n", |
| 149 | PTR_ERR(state->regmap)); |
| 150 | |
| 151 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 152 | state->regs = devm_ioremap_resource(dev, res); |
| 153 | if (IS_ERR(state->regs)) |
| 154 | return PTR_ERR(state->regs); |
| 155 | } |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 156 | |
| 157 | dev_set_drvdata(dev, state); |
| 158 | spin_lock_init(&state->slock); |
| 159 | |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 160 | for (i = 0; i < EXYNOS_MIPI_PHYS_NUM; i++) { |
Kishon Vijay Abraham I | f0ed817 | 2014-07-14 15:55:02 +0530 | [diff] [blame] | 161 | struct phy *phy = devm_phy_create(dev, NULL, |
Heikki Krogerus | dbc9863 | 2014-11-19 17:28:21 +0200 | [diff] [blame] | 162 | &exynos_mipi_video_phy_ops); |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 163 | if (IS_ERR(phy)) { |
| 164 | dev_err(dev, "failed to create PHY %d\n", i); |
| 165 | return PTR_ERR(phy); |
| 166 | } |
| 167 | |
| 168 | state->phys[i].phy = phy; |
| 169 | state->phys[i].index = i; |
| 170 | phy_set_drvdata(phy, &state->phys[i]); |
| 171 | } |
| 172 | |
Kishon Vijay Abraham I | 64fe189 | 2014-02-17 14:29:25 +0530 | [diff] [blame] | 173 | phy_provider = devm_of_phy_provider_register(dev, |
| 174 | exynos_mipi_video_phy_xlate); |
Kishon Vijay Abraham I | 64fe189 | 2014-02-17 14:29:25 +0530 | [diff] [blame] | 175 | |
Sachin Kamat | 22fda30 | 2014-05-29 12:00:48 +0530 | [diff] [blame] | 176 | return PTR_ERR_OR_ZERO(phy_provider); |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static const struct of_device_id exynos_mipi_video_phy_of_match[] = { |
| 180 | { .compatible = "samsung,s5pv210-mipi-video-phy" }, |
| 181 | { }, |
| 182 | }; |
| 183 | MODULE_DEVICE_TABLE(of, exynos_mipi_video_phy_of_match); |
| 184 | |
| 185 | static struct platform_driver exynos_mipi_video_phy_driver = { |
| 186 | .probe = exynos_mipi_video_phy_probe, |
| 187 | .driver = { |
| 188 | .of_match_table = exynos_mipi_video_phy_of_match, |
| 189 | .name = "exynos-mipi-video-phy", |
Sylwester Nawrocki | 069d2e2 | 2013-10-16 21:58:10 +0530 | [diff] [blame] | 190 | } |
| 191 | }; |
| 192 | module_platform_driver(exynos_mipi_video_phy_driver); |
| 193 | |
| 194 | MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI CSI-2/DSI PHY driver"); |
| 195 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 196 | MODULE_LICENSE("GPL v2"); |