blob: 8b851f718123a1110016cfc462385b85a9da3bac [file] [log] [blame]
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +05301/*
2 * Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
3 *
Marek Szyprowski71f5c632016-03-23 12:09:18 +01004 * Copyright (C) 2013,2016 Samsung Electronics Co., Ltd.
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +05305 * 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 Kamat22fda302014-05-29 12:00:48 +053012#include <linux/err.h>
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053013#include <linux/io.h>
14#include <linux/kernel.h>
Sylwester Nawrockie4b3d382015-01-16 18:30:37 +010015#include <linux/mfd/syscon/exynos4-pmu.h>
Marek Szyprowski71f5c632016-03-23 12:09:18 +010016#include <linux/mfd/syscon/exynos5-pmu.h>
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053017#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
Marek Szyprowski97a30422016-03-23 12:09:17 +010020#include <linux/of_device.h>
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053021#include <linux/phy/phy.h>
Sylwester Nawrockie4b3d382015-01-16 18:30:37 +010022#include <linux/regmap.h>
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053023#include <linux/spinlock.h>
Sylwester Nawrockie4b3d382015-01-16 18:30:37 +010024#include <linux/mfd/syscon.h>
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053025
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053026enum exynos_mipi_phy_id {
Marek Szyprowski97a30422016-03-23 12:09:17 +010027 EXYNOS_MIPI_PHY_ID_NONE = -1,
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053028 EXYNOS_MIPI_PHY_ID_CSIS0,
29 EXYNOS_MIPI_PHY_ID_DSIM0,
30 EXYNOS_MIPI_PHY_ID_CSIS1,
31 EXYNOS_MIPI_PHY_ID_DSIM1,
Marek Szyprowski71f5c632016-03-23 12:09:18 +010032 EXYNOS_MIPI_PHY_ID_CSIS2,
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +053033 EXYNOS_MIPI_PHYS_NUM
34};
35
Marek Szyprowski97a30422016-03-23 12:09:17 +010036enum exynos_mipi_phy_regmap_id {
37 EXYNOS_MIPI_REGMAP_PMU,
Marek Szyprowski71f5c632016-03-23 12:09:18 +010038 EXYNOS_MIPI_REGMAP_DISP,
39 EXYNOS_MIPI_REGMAP_CAM0,
40 EXYNOS_MIPI_REGMAP_CAM1,
Marek Szyprowski97a30422016-03-23 12:09:17 +010041 EXYNOS_MIPI_REGMAPS_NUM
42};
43
44struct mipi_phy_device_desc {
45 int num_phys;
46 int num_regmaps;
47 const char *regmap_names[EXYNOS_MIPI_REGMAPS_NUM];
48 struct exynos_mipi_phy_desc {
49 enum exynos_mipi_phy_id coupled_phy_id;
50 u32 enable_val;
51 unsigned int enable_reg;
52 enum exynos_mipi_phy_regmap_id enable_map;
53 u32 resetn_val;
54 unsigned int resetn_reg;
55 enum exynos_mipi_phy_regmap_id resetn_map;
56 } phys[EXYNOS_MIPI_PHYS_NUM];
57};
58
59static const struct mipi_phy_device_desc s5pv210_mipi_phy = {
60 .num_regmaps = 1,
61 .regmap_names = {"syscon"},
62 .num_phys = 4,
63 .phys = {
64 {
65 /* EXYNOS_MIPI_PHY_ID_CSIS0 */
66 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM0,
67 .enable_val = EXYNOS4_MIPI_PHY_ENABLE,
68 .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
69 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
70 .resetn_val = EXYNOS4_MIPI_PHY_SRESETN,
71 .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
72 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
73 }, {
74 /* EXYNOS_MIPI_PHY_ID_DSIM0 */
75 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS0,
76 .enable_val = EXYNOS4_MIPI_PHY_ENABLE,
77 .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
78 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
79 .resetn_val = EXYNOS4_MIPI_PHY_MRESETN,
80 .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
81 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
82 }, {
83 /* EXYNOS_MIPI_PHY_ID_CSIS1 */
84 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM1,
85 .enable_val = EXYNOS4_MIPI_PHY_ENABLE,
86 .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
87 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
88 .resetn_val = EXYNOS4_MIPI_PHY_SRESETN,
89 .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
90 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
91 }, {
92 /* EXYNOS_MIPI_PHY_ID_DSIM1 */
93 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS1,
94 .enable_val = EXYNOS4_MIPI_PHY_ENABLE,
95 .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
96 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
97 .resetn_val = EXYNOS4_MIPI_PHY_MRESETN,
98 .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
99 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
100 },
101 },
102};
103
Marek Szyprowski71f5c632016-03-23 12:09:18 +0100104static const struct mipi_phy_device_desc exynos5420_mipi_phy = {
105 .num_regmaps = 1,
106 .regmap_names = {"syscon"},
107 .num_phys = 5,
108 .phys = {
109 {
110 /* EXYNOS_MIPI_PHY_ID_CSIS0 */
111 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM0,
112 .enable_val = EXYNOS5_PHY_ENABLE,
113 .enable_reg = EXYNOS5420_MIPI_PHY0_CONTROL,
114 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
115 .resetn_val = EXYNOS5_MIPI_PHY_S_RESETN,
116 .resetn_reg = EXYNOS5420_MIPI_PHY0_CONTROL,
117 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
118 }, {
119 /* EXYNOS_MIPI_PHY_ID_DSIM0 */
120 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS0,
121 .enable_val = EXYNOS5_PHY_ENABLE,
122 .enable_reg = EXYNOS5420_MIPI_PHY0_CONTROL,
123 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
124 .resetn_val = EXYNOS5_MIPI_PHY_M_RESETN,
125 .resetn_reg = EXYNOS5420_MIPI_PHY0_CONTROL,
126 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
127 }, {
128 /* EXYNOS_MIPI_PHY_ID_CSIS1 */
129 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM1,
130 .enable_val = EXYNOS5_PHY_ENABLE,
131 .enable_reg = EXYNOS5420_MIPI_PHY1_CONTROL,
132 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
133 .resetn_val = EXYNOS5_MIPI_PHY_S_RESETN,
134 .resetn_reg = EXYNOS5420_MIPI_PHY1_CONTROL,
135 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
136 }, {
137 /* EXYNOS_MIPI_PHY_ID_DSIM1 */
138 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS1,
139 .enable_val = EXYNOS5_PHY_ENABLE,
140 .enable_reg = EXYNOS5420_MIPI_PHY1_CONTROL,
141 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
142 .resetn_val = EXYNOS5_MIPI_PHY_M_RESETN,
143 .resetn_reg = EXYNOS5420_MIPI_PHY1_CONTROL,
144 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
145 }, {
146 /* EXYNOS_MIPI_PHY_ID_CSIS2 */
147 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
148 .enable_val = EXYNOS5_PHY_ENABLE,
149 .enable_reg = EXYNOS5420_MIPI_PHY2_CONTROL,
150 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
151 .resetn_val = EXYNOS5_MIPI_PHY_S_RESETN,
152 .resetn_reg = EXYNOS5420_MIPI_PHY2_CONTROL,
153 .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
154 },
155 },
156};
157
158#define EXYNOS5433_SYSREG_DISP_MIPI_PHY 0x100C
159#define EXYNOS5433_SYSREG_CAM0_MIPI_DPHY_CON 0x1014
160#define EXYNOS5433_SYSREG_CAM1_MIPI_DPHY_CON 0x1020
161
162static const struct mipi_phy_device_desc exynos5433_mipi_phy = {
163 .num_regmaps = 4,
164 .regmap_names = {
165 "samsung,pmu-syscon",
166 "samsung,disp-sysreg",
167 "samsung,cam0-sysreg",
168 "samsung,cam1-sysreg"
169 },
170 .num_phys = 5,
171 .phys = {
172 {
173 /* EXYNOS_MIPI_PHY_ID_CSIS0 */
174 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM0,
175 .enable_val = EXYNOS5_PHY_ENABLE,
176 .enable_reg = EXYNOS5433_MIPI_PHY0_CONTROL,
177 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
178 .resetn_val = BIT(0),
179 .resetn_reg = EXYNOS5433_SYSREG_CAM0_MIPI_DPHY_CON,
180 .resetn_map = EXYNOS_MIPI_REGMAP_CAM0,
181 }, {
182 /* EXYNOS_MIPI_PHY_ID_DSIM0 */
183 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS0,
184 .enable_val = EXYNOS5_PHY_ENABLE,
185 .enable_reg = EXYNOS5433_MIPI_PHY0_CONTROL,
186 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
187 .resetn_val = BIT(0),
188 .resetn_reg = EXYNOS5433_SYSREG_DISP_MIPI_PHY,
189 .resetn_map = EXYNOS_MIPI_REGMAP_DISP,
190 }, {
191 /* EXYNOS_MIPI_PHY_ID_CSIS1 */
192 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
193 .enable_val = EXYNOS5_PHY_ENABLE,
194 .enable_reg = EXYNOS5433_MIPI_PHY1_CONTROL,
195 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
196 .resetn_val = BIT(1),
197 .resetn_reg = EXYNOS5433_SYSREG_CAM0_MIPI_DPHY_CON,
198 .resetn_map = EXYNOS_MIPI_REGMAP_CAM0,
199 }, {
200 /* EXYNOS_MIPI_PHY_ID_DSIM1 */
201 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
202 .enable_val = EXYNOS5_PHY_ENABLE,
203 .enable_reg = EXYNOS5433_MIPI_PHY1_CONTROL,
204 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
205 .resetn_val = BIT(1),
206 .resetn_reg = EXYNOS5433_SYSREG_DISP_MIPI_PHY,
207 .resetn_map = EXYNOS_MIPI_REGMAP_DISP,
208 }, {
209 /* EXYNOS_MIPI_PHY_ID_CSIS2 */
210 .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
211 .enable_val = EXYNOS5_PHY_ENABLE,
212 .enable_reg = EXYNOS5433_MIPI_PHY2_CONTROL,
213 .enable_map = EXYNOS_MIPI_REGMAP_PMU,
214 .resetn_val = BIT(0),
215 .resetn_reg = EXYNOS5433_SYSREG_CAM1_MIPI_DPHY_CON,
216 .resetn_map = EXYNOS_MIPI_REGMAP_CAM1,
217 },
218 },
219};
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530220
221struct exynos_mipi_video_phy {
Marek Szyprowski97a30422016-03-23 12:09:17 +0100222 struct regmap *regmaps[EXYNOS_MIPI_REGMAPS_NUM];
223 int num_phys;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530224 struct video_phy_desc {
225 struct phy *phy;
226 unsigned int index;
Marek Szyprowski97a30422016-03-23 12:09:17 +0100227 const struct exynos_mipi_phy_desc *data;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530228 } phys[EXYNOS_MIPI_PHYS_NUM];
Sylwester Nawrockie4b3d382015-01-16 18:30:37 +0100229 spinlock_t slock;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530230};
231
Marek Szyprowski97a30422016-03-23 12:09:17 +0100232static inline int __is_running(const struct exynos_mipi_phy_desc *data,
233 struct exynos_mipi_video_phy *state)
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530234{
Marek Szyprowski97a30422016-03-23 12:09:17 +0100235 u32 val;
Arnd Bergmann06061dc2016-05-11 14:49:53 +0200236 int ret;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530237
Arnd Bergmann06061dc2016-05-11 14:49:53 +0200238 ret = regmap_read(state->regmaps[data->resetn_map], data->resetn_reg, &val);
239 if (ret)
240 return 0;
241
Marek Szyprowski97a30422016-03-23 12:09:17 +0100242 return val & data->resetn_val;
243}
244
245static int __set_phy_state(const struct exynos_mipi_phy_desc *data,
246 struct exynos_mipi_video_phy *state, unsigned int on)
247{
248 u32 val;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530249
Axel Linaf69dec2015-02-27 11:50:57 +0800250 spin_lock(&state->slock);
251
Marek Szyprowski97a30422016-03-23 12:09:17 +0100252 /* disable in PMU sysreg */
253 if (!on && data->coupled_phy_id >= 0 &&
254 !__is_running(state->phys[data->coupled_phy_id].data, state)) {
255 regmap_read(state->regmaps[data->enable_map], data->enable_reg,
256 &val);
257 val &= ~data->enable_val;
258 regmap_write(state->regmaps[data->enable_map], data->enable_reg,
259 val);
260 }
261
262 /* PHY reset */
263 regmap_read(state->regmaps[data->resetn_map], data->resetn_reg, &val);
264 val = on ? (val | data->resetn_val) : (val & ~data->resetn_val);
265 regmap_write(state->regmaps[data->resetn_map], data->resetn_reg, val);
266
267 /* enable in PMU sysreg */
268 if (on) {
269 regmap_read(state->regmaps[data->enable_map], data->enable_reg,
270 &val);
271 val |= data->enable_val;
272 regmap_write(state->regmaps[data->enable_map], data->enable_reg,
273 val);
274 }
Sylwester Nawrockie4b3d382015-01-16 18:30:37 +0100275
Axel Linaf69dec2015-02-27 11:50:57 +0800276 spin_unlock(&state->slock);
Marek Szyprowski97a30422016-03-23 12:09:17 +0100277
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530278 return 0;
279}
280
281#define to_mipi_video_phy(desc) \
Marek Szyprowski97a30422016-03-23 12:09:17 +0100282 container_of((desc), struct exynos_mipi_video_phy, phys[(desc)->index])
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530283
284static int exynos_mipi_video_phy_power_on(struct phy *phy)
285{
286 struct video_phy_desc *phy_desc = phy_get_drvdata(phy);
287 struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc);
288
Marek Szyprowski97a30422016-03-23 12:09:17 +0100289 return __set_phy_state(phy_desc->data, state, 1);
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530290}
291
292static int exynos_mipi_video_phy_power_off(struct phy *phy)
293{
294 struct video_phy_desc *phy_desc = phy_get_drvdata(phy);
295 struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc);
296
Marek Szyprowski97a30422016-03-23 12:09:17 +0100297 return __set_phy_state(phy_desc->data, state, 0);
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530298}
299
300static struct phy *exynos_mipi_video_phy_xlate(struct device *dev,
301 struct of_phandle_args *args)
302{
303 struct exynos_mipi_video_phy *state = dev_get_drvdata(dev);
304
Marek Szyprowski97a30422016-03-23 12:09:17 +0100305 if (WARN_ON(args->args[0] >= state->num_phys))
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530306 return ERR_PTR(-ENODEV);
307
308 return state->phys[args->args[0]].phy;
309}
310
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800311static const struct phy_ops exynos_mipi_video_phy_ops = {
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530312 .power_on = exynos_mipi_video_phy_power_on,
313 .power_off = exynos_mipi_video_phy_power_off,
314 .owner = THIS_MODULE,
315};
316
317static int exynos_mipi_video_phy_probe(struct platform_device *pdev)
318{
Marek Szyprowski97a30422016-03-23 12:09:17 +0100319 const struct mipi_phy_device_desc *phy_dev;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530320 struct exynos_mipi_video_phy *state;
321 struct device *dev = &pdev->dev;
Marek Szyprowski97a30422016-03-23 12:09:17 +0100322 struct device_node *np = dev->of_node;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530323 struct phy_provider *phy_provider;
324 unsigned int i;
325
Marek Szyprowski97a30422016-03-23 12:09:17 +0100326 phy_dev = of_device_get_match_data(dev);
327 if (!phy_dev)
328 return -ENODEV;
329
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530330 state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
331 if (!state)
332 return -ENOMEM;
333
Marek Szyprowski97a30422016-03-23 12:09:17 +0100334 for (i = 0; i < phy_dev->num_regmaps; i++) {
335 state->regmaps[i] = syscon_regmap_lookup_by_phandle(np,
336 phy_dev->regmap_names[i]);
337 if (IS_ERR(state->regmaps[i]))
338 return PTR_ERR(state->regmaps[i]);
339 }
340 state->num_phys = phy_dev->num_phys;
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530341 spin_lock_init(&state->slock);
342
Marek Szyprowski97a30422016-03-23 12:09:17 +0100343 dev_set_drvdata(dev, state);
344
345 for (i = 0; i < state->num_phys; i++) {
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530346 struct phy *phy = devm_phy_create(dev, NULL,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200347 &exynos_mipi_video_phy_ops);
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530348 if (IS_ERR(phy)) {
349 dev_err(dev, "failed to create PHY %d\n", i);
350 return PTR_ERR(phy);
351 }
352
353 state->phys[i].phy = phy;
354 state->phys[i].index = i;
Marek Szyprowski97a30422016-03-23 12:09:17 +0100355 state->phys[i].data = &phy_dev->phys[i];
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530356 phy_set_drvdata(phy, &state->phys[i]);
357 }
358
Kishon Vijay Abraham I64fe1892014-02-17 14:29:25 +0530359 phy_provider = devm_of_phy_provider_register(dev,
360 exynos_mipi_video_phy_xlate);
Kishon Vijay Abraham I64fe1892014-02-17 14:29:25 +0530361
Sachin Kamat22fda302014-05-29 12:00:48 +0530362 return PTR_ERR_OR_ZERO(phy_provider);
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530363}
364
365static const struct of_device_id exynos_mipi_video_phy_of_match[] = {
Marek Szyprowski97a30422016-03-23 12:09:17 +0100366 {
367 .compatible = "samsung,s5pv210-mipi-video-phy",
368 .data = &s5pv210_mipi_phy,
Marek Szyprowski71f5c632016-03-23 12:09:18 +0100369 }, {
370 .compatible = "samsung,exynos5420-mipi-video-phy",
371 .data = &exynos5420_mipi_phy,
372 }, {
373 .compatible = "samsung,exynos5433-mipi-video-phy",
374 .data = &exynos5433_mipi_phy,
Marek Szyprowski97a30422016-03-23 12:09:17 +0100375 },
376 { /* sentinel */ },
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530377};
378MODULE_DEVICE_TABLE(of, exynos_mipi_video_phy_of_match);
379
380static struct platform_driver exynos_mipi_video_phy_driver = {
381 .probe = exynos_mipi_video_phy_probe,
382 .driver = {
383 .of_match_table = exynos_mipi_video_phy_of_match,
384 .name = "exynos-mipi-video-phy",
Sylwester Nawrocki069d2e22013-10-16 21:58:10 +0530385 }
386};
387module_platform_driver(exynos_mipi_video_phy_driver);
388
389MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI CSI-2/DSI PHY driver");
390MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
391MODULE_LICENSE("GPL v2");