Commit | Line | Data |
---|---|---|
553c48cf TV |
1 | /* |
2 | * linux/drivers/video/omap2/dss/dpi.c | |
3 | * | |
4 | * Copyright (C) 2009 Nokia Corporation | |
5 | * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com> | |
6 | * | |
7 | * Some code and ideas taken from drivers/video/omap/ driver | |
8 | * by Imre Deak. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License version 2 as published by | |
12 | * the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
17 | * more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along with | |
20 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | #define DSS_SUBSYS_NAME "DPI" | |
24 | ||
25 | #include <linux/kernel.h> | |
553c48cf | 26 | #include <linux/delay.h> |
a8a35931 | 27 | #include <linux/export.h> |
8a2cfea8 | 28 | #include <linux/err.h> |
553c48cf | 29 | #include <linux/errno.h> |
8a2cfea8 TV |
30 | #include <linux/platform_device.h> |
31 | #include <linux/regulator/consumer.h> | |
13b1ba7d | 32 | #include <linux/string.h> |
2ecef246 | 33 | #include <linux/of.h> |
2daea7af | 34 | #include <linux/clk.h> |
736e60dd | 35 | #include <linux/component.h> |
553c48cf | 36 | |
a0b38cc4 | 37 | #include <video/omapdss.h> |
553c48cf TV |
38 | |
39 | #include "dss.h" | |
195e672a | 40 | #include "dss_features.h" |
553c48cf | 41 | |
acf604b7 TV |
42 | #define HSDIV_DISPC 0 |
43 | ||
630d2d0d | 44 | struct dpi_data { |
00df43b8 TV |
45 | struct platform_device *pdev; |
46 | ||
8a2cfea8 | 47 | struct regulator *vdds_dsi_reg; |
2daea7af | 48 | struct dss_pll *pll; |
5cf9a264 | 49 | |
c8a5e4e8 AT |
50 | struct mutex lock; |
51 | ||
c499144c | 52 | struct omap_video_timings timings; |
5cf9a264 | 53 | struct dss_lcd_mgr_config mgr_config; |
c6b393d4 | 54 | int data_lines; |
81b87f51 | 55 | |
1f68d9c4 | 56 | struct omap_dss_device output; |
2ecef246 TV |
57 | |
58 | bool port_initialized; | |
630d2d0d AT |
59 | }; |
60 | ||
2ac6a1aa AT |
61 | static struct dpi_data *dpi_get_data_from_dssdev(struct omap_dss_device *dssdev) |
62 | { | |
63 | return container_of(dssdev, struct dpi_data, output); | |
64 | } | |
65 | ||
80eb6751 | 66 | /* only used in non-DT mode */ |
2ac6a1aa AT |
67 | static struct dpi_data *dpi_get_data_from_pdev(struct platform_device *pdev) |
68 | { | |
69 | return dev_get_drvdata(&pdev->dev); | |
70 | } | |
553c48cf | 71 | |
2daea7af | 72 | static struct dss_pll *dpi_get_pll(enum omap_channel channel) |
a72b64b9 | 73 | { |
bd0f5cc3 TV |
74 | /* |
75 | * XXX we can't currently use DSI PLL for DPI with OMAP3, as the DSI PLL | |
76 | * would also be used for DISPC fclk. Meaning, when the DPI output is | |
77 | * disabled, DISPC clock will be disabled, and TV out will stop. | |
78 | */ | |
79 | switch (omapdss_get_version()) { | |
80 | case OMAPDSS_VER_OMAP24xx: | |
81 | case OMAPDSS_VER_OMAP34xx_ES1: | |
82 | case OMAPDSS_VER_OMAP34xx_ES3: | |
83 | case OMAPDSS_VER_OMAP3630: | |
84 | case OMAPDSS_VER_AM35xx: | |
d6279d4a | 85 | case OMAPDSS_VER_AM43xx: |
bd0f5cc3 | 86 | return NULL; |
bd0f5cc3 | 87 | |
f8ad984c TV |
88 | case OMAPDSS_VER_OMAP4430_ES1: |
89 | case OMAPDSS_VER_OMAP4430_ES2: | |
90 | case OMAPDSS_VER_OMAP4: | |
91 | switch (channel) { | |
92 | case OMAP_DSS_CHANNEL_LCD: | |
2daea7af | 93 | return dss_pll_find("dsi0"); |
f8ad984c | 94 | case OMAP_DSS_CHANNEL_LCD2: |
2daea7af | 95 | return dss_pll_find("dsi1"); |
f8ad984c TV |
96 | default: |
97 | return NULL; | |
98 | } | |
99 | ||
100 | case OMAPDSS_VER_OMAP5: | |
101 | switch (channel) { | |
102 | case OMAP_DSS_CHANNEL_LCD: | |
2daea7af | 103 | return dss_pll_find("dsi0"); |
f8ad984c | 104 | case OMAP_DSS_CHANNEL_LCD3: |
2daea7af | 105 | return dss_pll_find("dsi1"); |
f8ad984c TV |
106 | default: |
107 | return NULL; | |
108 | } | |
109 | ||
a2408154 TV |
110 | case OMAPDSS_VER_DRA7xx: |
111 | switch (channel) { | |
112 | case OMAP_DSS_CHANNEL_LCD: | |
113 | case OMAP_DSS_CHANNEL_LCD2: | |
114 | return dss_pll_find("video0"); | |
115 | case OMAP_DSS_CHANNEL_LCD3: | |
116 | return dss_pll_find("video1"); | |
117 | default: | |
118 | return NULL; | |
119 | } | |
120 | ||
0e8276ef TV |
121 | default: |
122 | return NULL; | |
123 | } | |
a72b64b9 AT |
124 | } |
125 | ||
0e8276ef | 126 | static enum omap_dss_clk_source dpi_get_alt_clk_src(enum omap_channel channel) |
7636b3b4 | 127 | { |
0e8276ef TV |
128 | switch (channel) { |
129 | case OMAP_DSS_CHANNEL_LCD: | |
130 | return OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC; | |
131 | case OMAP_DSS_CHANNEL_LCD2: | |
132 | return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC; | |
35fd32d6 TV |
133 | case OMAP_DSS_CHANNEL_LCD3: |
134 | return OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC; | |
0e8276ef TV |
135 | default: |
136 | /* this shouldn't happen */ | |
137 | WARN_ON(1); | |
138 | return OMAP_DSS_CLK_SRC_FCK; | |
139 | } | |
7636b3b4 AT |
140 | } |
141 | ||
100c8262 | 142 | struct dpi_clk_calc_ctx { |
2daea7af | 143 | struct dss_pll *pll; |
100c8262 TV |
144 | |
145 | /* inputs */ | |
146 | ||
147 | unsigned long pck_min, pck_max; | |
148 | ||
149 | /* outputs */ | |
150 | ||
2daea7af | 151 | struct dss_pll_clock_info dsi_cinfo; |
c56812fc | 152 | unsigned long fck; |
100c8262 TV |
153 | struct dispc_clock_info dispc_cinfo; |
154 | }; | |
155 | ||
156 | static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck, | |
157 | unsigned long pck, void *data) | |
158 | { | |
159 | struct dpi_clk_calc_ctx *ctx = data; | |
160 | ||
161 | /* | |
162 | * Odd dividers give us uneven duty cycle, causing problem when level | |
163 | * shifted. So skip all odd dividers when the pixel clock is on the | |
164 | * higher side. | |
165 | */ | |
72e5512a | 166 | if (ctx->pck_min >= 100000000) { |
100c8262 TV |
167 | if (lckd > 1 && lckd % 2 != 0) |
168 | return false; | |
169 | ||
170 | if (pckd > 1 && pckd % 2 != 0) | |
171 | return false; | |
172 | } | |
173 | ||
174 | ctx->dispc_cinfo.lck_div = lckd; | |
175 | ctx->dispc_cinfo.pck_div = pckd; | |
176 | ctx->dispc_cinfo.lck = lck; | |
177 | ctx->dispc_cinfo.pck = pck; | |
178 | ||
179 | return true; | |
180 | } | |
181 | ||
182 | ||
2daea7af | 183 | static bool dpi_calc_hsdiv_cb(int m_dispc, unsigned long dispc, |
100c8262 TV |
184 | void *data) |
185 | { | |
186 | struct dpi_clk_calc_ctx *ctx = data; | |
187 | ||
188 | /* | |
189 | * Odd dividers give us uneven duty cycle, causing problem when level | |
190 | * shifted. So skip all odd dividers when the pixel clock is on the | |
191 | * higher side. | |
192 | */ | |
2daea7af | 193 | if (m_dispc > 1 && m_dispc % 2 != 0 && ctx->pck_min >= 100000000) |
100c8262 TV |
194 | return false; |
195 | ||
2daea7af | 196 | ctx->dsi_cinfo.mX[HSDIV_DISPC] = m_dispc; |
acf604b7 | 197 | ctx->dsi_cinfo.clkout[HSDIV_DISPC] = dispc; |
100c8262 TV |
198 | |
199 | return dispc_div_calc(dispc, ctx->pck_min, ctx->pck_max, | |
200 | dpi_calc_dispc_cb, ctx); | |
201 | } | |
202 | ||
203 | ||
2daea7af TV |
204 | static bool dpi_calc_pll_cb(int n, int m, unsigned long fint, |
205 | unsigned long clkdco, | |
100c8262 TV |
206 | void *data) |
207 | { | |
208 | struct dpi_clk_calc_ctx *ctx = data; | |
209 | ||
2daea7af TV |
210 | ctx->dsi_cinfo.n = n; |
211 | ctx->dsi_cinfo.m = m; | |
100c8262 | 212 | ctx->dsi_cinfo.fint = fint; |
2daea7af | 213 | ctx->dsi_cinfo.clkdco = clkdco; |
100c8262 | 214 | |
2daea7af TV |
215 | return dss_pll_hsdiv_calc(ctx->pll, clkdco, |
216 | ctx->pck_min, dss_feat_get_param_max(FEAT_PARAM_DSS_FCK), | |
217 | dpi_calc_hsdiv_cb, ctx); | |
100c8262 TV |
218 | } |
219 | ||
d0f58bd3 | 220 | static bool dpi_calc_dss_cb(unsigned long fck, void *data) |
100c8262 TV |
221 | { |
222 | struct dpi_clk_calc_ctx *ctx = data; | |
223 | ||
d0f58bd3 | 224 | ctx->fck = fck; |
100c8262 TV |
225 | |
226 | return dispc_div_calc(fck, ctx->pck_min, ctx->pck_max, | |
227 | dpi_calc_dispc_cb, ctx); | |
228 | } | |
229 | ||
630d2d0d AT |
230 | static bool dpi_dsi_clk_calc(struct dpi_data *dpi, unsigned long pck, |
231 | struct dpi_clk_calc_ctx *ctx) | |
100c8262 TV |
232 | { |
233 | unsigned long clkin; | |
234 | unsigned long pll_min, pll_max; | |
235 | ||
100c8262 | 236 | memset(ctx, 0, sizeof(*ctx)); |
2daea7af | 237 | ctx->pll = dpi->pll; |
100c8262 TV |
238 | ctx->pck_min = pck - 1000; |
239 | ctx->pck_max = pck + 1000; | |
100c8262 TV |
240 | |
241 | pll_min = 0; | |
242 | pll_max = 0; | |
243 | ||
2daea7af TV |
244 | clkin = clk_get_rate(ctx->pll->clkin); |
245 | ||
246 | return dss_pll_calc(ctx->pll, clkin, | |
100c8262 TV |
247 | pll_min, pll_max, |
248 | dpi_calc_pll_cb, ctx); | |
249 | } | |
250 | ||
251 | static bool dpi_dss_clk_calc(unsigned long pck, struct dpi_clk_calc_ctx *ctx) | |
252 | { | |
253 | int i; | |
254 | ||
255 | /* | |
256 | * DSS fck gives us very few possibilities, so finding a good pixel | |
257 | * clock may not be possible. We try multiple times to find the clock, | |
258 | * each time widening the pixel clock range we look for, up to | |
2c6360fb | 259 | * +/- ~15MHz. |
100c8262 TV |
260 | */ |
261 | ||
2c6360fb | 262 | for (i = 0; i < 25; ++i) { |
100c8262 TV |
263 | bool ok; |
264 | ||
265 | memset(ctx, 0, sizeof(*ctx)); | |
266 | if (pck > 1000 * i * i * i) | |
267 | ctx->pck_min = max(pck - 1000 * i * i * i, 0lu); | |
268 | else | |
269 | ctx->pck_min = 0; | |
270 | ctx->pck_max = pck + 1000 * i * i * i; | |
271 | ||
688af02d | 272 | ok = dss_div_calc(pck, ctx->pck_min, dpi_calc_dss_cb, ctx); |
100c8262 TV |
273 | if (ok) |
274 | return ok; | |
275 | } | |
276 | ||
277 | return false; | |
278 | } | |
279 | ||
280 | ||
281 | ||
630d2d0d | 282 | static int dpi_set_dsi_clk(struct dpi_data *dpi, enum omap_channel channel, |
ff1b2cde SS |
283 | unsigned long pck_req, unsigned long *fck, int *lck_div, |
284 | int *pck_div) | |
553c48cf | 285 | { |
100c8262 | 286 | struct dpi_clk_calc_ctx ctx; |
553c48cf | 287 | int r; |
100c8262 | 288 | bool ok; |
553c48cf | 289 | |
630d2d0d | 290 | ok = dpi_dsi_clk_calc(dpi, pck_req, &ctx); |
100c8262 TV |
291 | if (!ok) |
292 | return -EINVAL; | |
553c48cf | 293 | |
2daea7af | 294 | r = dss_pll_set_config(dpi->pll, &ctx.dsi_cinfo); |
553c48cf TV |
295 | if (r) |
296 | return r; | |
297 | ||
03a0d1e8 TV |
298 | dss_select_lcd_clk_source(channel, |
299 | dpi_get_alt_clk_src(channel)); | |
553c48cf | 300 | |
630d2d0d | 301 | dpi->mgr_config.clock_info = ctx.dispc_cinfo; |
553c48cf | 302 | |
acf604b7 | 303 | *fck = ctx.dsi_cinfo.clkout[HSDIV_DISPC]; |
100c8262 TV |
304 | *lck_div = ctx.dispc_cinfo.lck_div; |
305 | *pck_div = ctx.dispc_cinfo.pck_div; | |
553c48cf TV |
306 | |
307 | return 0; | |
308 | } | |
7636b3b4 | 309 | |
630d2d0d AT |
310 | static int dpi_set_dispc_clk(struct dpi_data *dpi, unsigned long pck_req, |
311 | unsigned long *fck, int *lck_div, int *pck_div) | |
553c48cf | 312 | { |
100c8262 | 313 | struct dpi_clk_calc_ctx ctx; |
553c48cf | 314 | int r; |
100c8262 | 315 | bool ok; |
553c48cf | 316 | |
100c8262 TV |
317 | ok = dpi_dss_clk_calc(pck_req, &ctx); |
318 | if (!ok) | |
319 | return -EINVAL; | |
553c48cf | 320 | |
d0f58bd3 | 321 | r = dss_set_fck_rate(ctx.fck); |
553c48cf TV |
322 | if (r) |
323 | return r; | |
324 | ||
630d2d0d | 325 | dpi->mgr_config.clock_info = ctx.dispc_cinfo; |
553c48cf | 326 | |
d0f58bd3 | 327 | *fck = ctx.fck; |
100c8262 TV |
328 | *lck_div = ctx.dispc_cinfo.lck_div; |
329 | *pck_div = ctx.dispc_cinfo.pck_div; | |
553c48cf TV |
330 | |
331 | return 0; | |
332 | } | |
553c48cf | 333 | |
630d2d0d | 334 | static int dpi_set_mode(struct dpi_data *dpi) |
553c48cf | 335 | { |
630d2d0d AT |
336 | struct omap_dss_device *out = &dpi->output; |
337 | struct omap_overlay_manager *mgr = out->manager; | |
338 | struct omap_video_timings *t = &dpi->timings; | |
7636b3b4 AT |
339 | int lck_div = 0, pck_div = 0; |
340 | unsigned long fck = 0; | |
553c48cf | 341 | unsigned long pck; |
553c48cf TV |
342 | int r = 0; |
343 | ||
2daea7af | 344 | if (dpi->pll) |
630d2d0d | 345 | r = dpi_set_dsi_clk(dpi, mgr->id, t->pixelclock, &fck, |
6d523e7b | 346 | &lck_div, &pck_div); |
7636b3b4 | 347 | else |
630d2d0d | 348 | r = dpi_set_dispc_clk(dpi, t->pixelclock, &fck, |
6d523e7b | 349 | &lck_div, &pck_div); |
553c48cf | 350 | if (r) |
4fbafaf3 | 351 | return r; |
553c48cf | 352 | |
d8d78941 | 353 | pck = fck / lck_div / pck_div; |
553c48cf | 354 | |
d8d78941 TV |
355 | if (pck != t->pixelclock) { |
356 | DSSWARN("Could not find exact pixel clock. Requested %d Hz, got %lu Hz\n", | |
357 | t->pixelclock, pck); | |
553c48cf | 358 | |
d8d78941 | 359 | t->pixelclock = pck; |
553c48cf TV |
360 | } |
361 | ||
5d512fcd | 362 | dss_mgr_set_timings(mgr, t); |
553c48cf | 363 | |
4fbafaf3 | 364 | return 0; |
553c48cf TV |
365 | } |
366 | ||
630d2d0d | 367 | static void dpi_config_lcd_manager(struct dpi_data *dpi) |
553c48cf | 368 | { |
630d2d0d AT |
369 | struct omap_dss_device *out = &dpi->output; |
370 | struct omap_overlay_manager *mgr = out->manager; | |
371 | ||
372 | dpi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS; | |
569969d6 | 373 | |
630d2d0d AT |
374 | dpi->mgr_config.stallmode = false; |
375 | dpi->mgr_config.fifohandcheck = false; | |
5cf9a264 | 376 | |
630d2d0d | 377 | dpi->mgr_config.video_port_width = dpi->data_lines; |
5cf9a264 | 378 | |
630d2d0d | 379 | dpi->mgr_config.lcden_sig_polarity = 0; |
5cf9a264 | 380 | |
630d2d0d | 381 | dss_mgr_set_lcd_config(mgr, &dpi->mgr_config); |
553c48cf TV |
382 | } |
383 | ||
86a3efe1 | 384 | static int dpi_display_enable(struct omap_dss_device *dssdev) |
553c48cf | 385 | { |
2ac6a1aa | 386 | struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev); |
630d2d0d | 387 | struct omap_dss_device *out = &dpi->output; |
553c48cf TV |
388 | int r; |
389 | ||
630d2d0d | 390 | mutex_lock(&dpi->lock); |
c8a5e4e8 | 391 | |
630d2d0d | 392 | if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI) && !dpi->vdds_dsi_reg) { |
40410715 | 393 | DSSERR("no VDSS_DSI regulator\n"); |
c8a5e4e8 AT |
394 | r = -ENODEV; |
395 | goto err_no_reg; | |
40410715 RK |
396 | } |
397 | ||
b742648c | 398 | if (out->manager == NULL) { |
5d512fcd | 399 | DSSERR("failed to enable display: no output/manager\n"); |
c8a5e4e8 | 400 | r = -ENODEV; |
5d512fcd | 401 | goto err_no_out_mgr; |
05e1d606 TV |
402 | } |
403 | ||
195e672a | 404 | if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) { |
630d2d0d | 405 | r = regulator_enable(dpi->vdds_dsi_reg); |
8a2cfea8 | 406 | if (r) |
4fbafaf3 | 407 | goto err_reg_enable; |
8a2cfea8 TV |
408 | } |
409 | ||
4fbafaf3 | 410 | r = dispc_runtime_get(); |
553c48cf | 411 | if (r) |
4fbafaf3 TV |
412 | goto err_get_dispc; |
413 | ||
064c2a47 | 414 | r = dss_dpi_select_source(out->port_num, out->manager->id); |
de09e455 TV |
415 | if (r) |
416 | goto err_src_sel; | |
417 | ||
2daea7af TV |
418 | if (dpi->pll) { |
419 | r = dss_pll_enable(dpi->pll); | |
7636b3b4 | 420 | if (r) |
4fbafaf3 | 421 | goto err_dsi_pll_init; |
7636b3b4 AT |
422 | } |
423 | ||
630d2d0d | 424 | r = dpi_set_mode(dpi); |
553c48cf | 425 | if (r) |
4fbafaf3 | 426 | goto err_set_mode; |
553c48cf | 427 | |
630d2d0d | 428 | dpi_config_lcd_manager(dpi); |
5cf9a264 | 429 | |
553c48cf TV |
430 | mdelay(2); |
431 | ||
5d512fcd | 432 | r = dss_mgr_enable(out->manager); |
33ca237f TV |
433 | if (r) |
434 | goto err_mgr_enable; | |
553c48cf | 435 | |
630d2d0d | 436 | mutex_unlock(&dpi->lock); |
c8a5e4e8 | 437 | |
553c48cf TV |
438 | return 0; |
439 | ||
33ca237f | 440 | err_mgr_enable: |
4fbafaf3 | 441 | err_set_mode: |
2daea7af TV |
442 | if (dpi->pll) |
443 | dss_pll_disable(dpi->pll); | |
4fbafaf3 | 444 | err_dsi_pll_init: |
de09e455 | 445 | err_src_sel: |
4fbafaf3 TV |
446 | dispc_runtime_put(); |
447 | err_get_dispc: | |
195e672a | 448 | if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) |
630d2d0d | 449 | regulator_disable(dpi->vdds_dsi_reg); |
4fbafaf3 | 450 | err_reg_enable: |
5d512fcd | 451 | err_no_out_mgr: |
c8a5e4e8 | 452 | err_no_reg: |
630d2d0d | 453 | mutex_unlock(&dpi->lock); |
553c48cf TV |
454 | return r; |
455 | } | |
456 | ||
86a3efe1 | 457 | static void dpi_display_disable(struct omap_dss_device *dssdev) |
553c48cf | 458 | { |
2ac6a1aa | 459 | struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev); |
630d2d0d | 460 | struct omap_overlay_manager *mgr = dpi->output.manager; |
5d512fcd | 461 | |
630d2d0d | 462 | mutex_lock(&dpi->lock); |
c8a5e4e8 | 463 | |
5d512fcd | 464 | dss_mgr_disable(mgr); |
553c48cf | 465 | |
2daea7af | 466 | if (dpi->pll) { |
a5b8399f | 467 | dss_select_lcd_clk_source(mgr->id, OMAP_DSS_CLK_SRC_FCK); |
2daea7af | 468 | dss_pll_disable(dpi->pll); |
7636b3b4 | 469 | } |
553c48cf | 470 | |
4fbafaf3 | 471 | dispc_runtime_put(); |
553c48cf | 472 | |
195e672a | 473 | if (dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) |
630d2d0d | 474 | regulator_disable(dpi->vdds_dsi_reg); |
8a2cfea8 | 475 | |
630d2d0d | 476 | mutex_unlock(&dpi->lock); |
553c48cf | 477 | } |
553c48cf | 478 | |
86a3efe1 | 479 | static void dpi_set_timings(struct omap_dss_device *dssdev, |
c499144c | 480 | struct omap_video_timings *timings) |
553c48cf | 481 | { |
2ac6a1aa | 482 | struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev); |
630d2d0d | 483 | |
553c48cf | 484 | DSSDBG("dpi_set_timings\n"); |
c8a5e4e8 | 485 | |
630d2d0d | 486 | mutex_lock(&dpi->lock); |
c8a5e4e8 | 487 | |
630d2d0d | 488 | dpi->timings = *timings; |
c499144c | 489 | |
630d2d0d | 490 | mutex_unlock(&dpi->lock); |
553c48cf TV |
491 | } |
492 | ||
0b24edb1 TV |
493 | static void dpi_get_timings(struct omap_dss_device *dssdev, |
494 | struct omap_video_timings *timings) | |
495 | { | |
2ac6a1aa | 496 | struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev); |
630d2d0d AT |
497 | |
498 | mutex_lock(&dpi->lock); | |
0b24edb1 | 499 | |
630d2d0d | 500 | *timings = dpi->timings; |
0b24edb1 | 501 | |
630d2d0d | 502 | mutex_unlock(&dpi->lock); |
0b24edb1 TV |
503 | } |
504 | ||
86a3efe1 | 505 | static int dpi_check_timings(struct omap_dss_device *dssdev, |
553c48cf TV |
506 | struct omap_video_timings *timings) |
507 | { | |
2ac6a1aa | 508 | struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev); |
630d2d0d | 509 | struct omap_overlay_manager *mgr = dpi->output.manager; |
553c48cf TV |
510 | int lck_div, pck_div; |
511 | unsigned long fck; | |
512 | unsigned long pck; | |
100c8262 TV |
513 | struct dpi_clk_calc_ctx ctx; |
514 | bool ok; | |
553c48cf | 515 | |
2158f2c7 TV |
516 | if (timings->x_res % 8 != 0) |
517 | return -EINVAL; | |
518 | ||
8b095513 | 519 | if (mgr && !dispc_mgr_timings_ok(mgr->id, timings)) |
553c48cf TV |
520 | return -EINVAL; |
521 | ||
d8d78941 | 522 | if (timings->pixelclock == 0) |
553c48cf TV |
523 | return -EINVAL; |
524 | ||
2daea7af | 525 | if (dpi->pll) { |
630d2d0d | 526 | ok = dpi_dsi_clk_calc(dpi, timings->pixelclock, &ctx); |
100c8262 TV |
527 | if (!ok) |
528 | return -EINVAL; | |
553c48cf | 529 | |
acf604b7 | 530 | fck = ctx.dsi_cinfo.clkout[HSDIV_DISPC]; |
7636b3b4 | 531 | } else { |
d8d78941 | 532 | ok = dpi_dss_clk_calc(timings->pixelclock, &ctx); |
100c8262 TV |
533 | if (!ok) |
534 | return -EINVAL; | |
553c48cf | 535 | |
d0f58bd3 | 536 | fck = ctx.fck; |
553c48cf | 537 | } |
7636b3b4 | 538 | |
100c8262 TV |
539 | lck_div = ctx.dispc_cinfo.lck_div; |
540 | pck_div = ctx.dispc_cinfo.pck_div; | |
553c48cf | 541 | |
d8d78941 | 542 | pck = fck / lck_div / pck_div; |
553c48cf | 543 | |
d8d78941 | 544 | timings->pixelclock = pck; |
553c48cf TV |
545 | |
546 | return 0; | |
547 | } | |
553c48cf | 548 | |
86a3efe1 | 549 | static void dpi_set_data_lines(struct omap_dss_device *dssdev, int data_lines) |
c6b393d4 | 550 | { |
2ac6a1aa | 551 | struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev); |
c6b393d4 | 552 | |
630d2d0d | 553 | mutex_lock(&dpi->lock); |
c6b393d4 | 554 | |
630d2d0d AT |
555 | dpi->data_lines = data_lines; |
556 | ||
557 | mutex_unlock(&dpi->lock); | |
c6b393d4 | 558 | } |
c6b393d4 | 559 | |
2daea7af | 560 | static int dpi_verify_dsi_pll(struct dss_pll *pll) |
6061675b TV |
561 | { |
562 | int r; | |
563 | ||
564 | /* do initial setup with the PLL to see if it is operational */ | |
565 | ||
2daea7af | 566 | r = dss_pll_enable(pll); |
f76b178a | 567 | if (r) |
6061675b | 568 | return r; |
6061675b | 569 | |
2daea7af | 570 | dss_pll_disable(pll); |
6061675b TV |
571 | |
572 | return 0; | |
573 | } | |
574 | ||
630d2d0d | 575 | static int dpi_init_regulator(struct dpi_data *dpi) |
2795f646 TV |
576 | { |
577 | struct regulator *vdds_dsi; | |
578 | ||
579 | if (!dss_has_feature(FEAT_DPI_USES_VDDS_DSI)) | |
580 | return 0; | |
581 | ||
630d2d0d | 582 | if (dpi->vdds_dsi_reg) |
2795f646 TV |
583 | return 0; |
584 | ||
630d2d0d | 585 | vdds_dsi = devm_regulator_get(&dpi->pdev->dev, "vdds_dsi"); |
2795f646 | 586 | if (IS_ERR(vdds_dsi)) { |
40359a9b TV |
587 | if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER) |
588 | DSSERR("can't get VDDS_DSI regulator\n"); | |
4123de21 | 589 | return PTR_ERR(vdds_dsi); |
2795f646 TV |
590 | } |
591 | ||
630d2d0d | 592 | dpi->vdds_dsi_reg = vdds_dsi; |
2795f646 TV |
593 | |
594 | return 0; | |
595 | } | |
596 | ||
630d2d0d | 597 | static void dpi_init_pll(struct dpi_data *dpi) |
2795f646 | 598 | { |
2daea7af | 599 | struct dss_pll *pll; |
2795f646 | 600 | |
2daea7af | 601 | if (dpi->pll) |
2795f646 TV |
602 | return; |
603 | ||
2daea7af TV |
604 | pll = dpi_get_pll(dpi->output.dispc_channel); |
605 | if (!pll) | |
2795f646 TV |
606 | return; |
607 | ||
a2408154 TV |
608 | /* On DRA7 we need to set a mux to use the PLL */ |
609 | if (omapdss_get_version() == OMAPDSS_VER_DRA7xx) | |
610 | dss_ctrl_pll_set_control_mux(pll->id, dpi->output.dispc_channel); | |
611 | ||
2daea7af | 612 | if (dpi_verify_dsi_pll(pll)) { |
2795f646 TV |
613 | DSSWARN("DSI PLL not operational\n"); |
614 | return; | |
615 | } | |
616 | ||
2daea7af | 617 | dpi->pll = pll; |
2795f646 TV |
618 | } |
619 | ||
2eea5ae6 TV |
620 | /* |
621 | * Return a hardcoded channel for the DPI output. This should work for | |
622 | * current use cases, but this can be later expanded to either resolve | |
623 | * the channel in some more dynamic manner, or get the channel as a user | |
624 | * parameter. | |
625 | */ | |
f7e38fe9 | 626 | static enum omap_channel dpi_get_channel(int port_num) |
2eea5ae6 TV |
627 | { |
628 | switch (omapdss_get_version()) { | |
629 | case OMAPDSS_VER_OMAP24xx: | |
630 | case OMAPDSS_VER_OMAP34xx_ES1: | |
631 | case OMAPDSS_VER_OMAP34xx_ES3: | |
632 | case OMAPDSS_VER_OMAP3630: | |
633 | case OMAPDSS_VER_AM35xx: | |
d6279d4a | 634 | case OMAPDSS_VER_AM43xx: |
2eea5ae6 TV |
635 | return OMAP_DSS_CHANNEL_LCD; |
636 | ||
a2408154 TV |
637 | case OMAPDSS_VER_DRA7xx: |
638 | switch (port_num) { | |
639 | case 2: | |
640 | return OMAP_DSS_CHANNEL_LCD3; | |
641 | case 1: | |
642 | return OMAP_DSS_CHANNEL_LCD2; | |
643 | case 0: | |
644 | default: | |
645 | return OMAP_DSS_CHANNEL_LCD; | |
646 | } | |
647 | ||
2eea5ae6 TV |
648 | case OMAPDSS_VER_OMAP4430_ES1: |
649 | case OMAPDSS_VER_OMAP4430_ES2: | |
650 | case OMAPDSS_VER_OMAP4: | |
651 | return OMAP_DSS_CHANNEL_LCD2; | |
652 | ||
653 | case OMAPDSS_VER_OMAP5: | |
654 | return OMAP_DSS_CHANNEL_LCD3; | |
655 | ||
656 | default: | |
657 | DSSWARN("unsupported DSS version\n"); | |
658 | return OMAP_DSS_CHANNEL_LCD; | |
659 | } | |
660 | } | |
661 | ||
0b24edb1 TV |
662 | static int dpi_connect(struct omap_dss_device *dssdev, |
663 | struct omap_dss_device *dst) | |
664 | { | |
2ac6a1aa | 665 | struct dpi_data *dpi = dpi_get_data_from_dssdev(dssdev); |
0b24edb1 TV |
666 | struct omap_overlay_manager *mgr; |
667 | int r; | |
668 | ||
630d2d0d | 669 | r = dpi_init_regulator(dpi); |
0b24edb1 TV |
670 | if (r) |
671 | return r; | |
672 | ||
630d2d0d | 673 | dpi_init_pll(dpi); |
0b24edb1 TV |
674 | |
675 | mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel); | |
676 | if (!mgr) | |
677 | return -ENODEV; | |
678 | ||
679 | r = dss_mgr_connect(mgr, dssdev); | |
680 | if (r) | |
681 | return r; | |
682 | ||
683 | r = omapdss_output_set_device(dssdev, dst); | |
684 | if (r) { | |
685 | DSSERR("failed to connect output to new device: %s\n", | |
686 | dst->name); | |
687 | dss_mgr_disconnect(mgr, dssdev); | |
688 | return r; | |
689 | } | |
690 | ||
691 | return 0; | |
692 | } | |
693 | ||
694 | static void dpi_disconnect(struct omap_dss_device *dssdev, | |
695 | struct omap_dss_device *dst) | |
696 | { | |
9560dc10 | 697 | WARN_ON(dst != dssdev->dst); |
0b24edb1 | 698 | |
9560dc10 | 699 | if (dst != dssdev->dst) |
0b24edb1 TV |
700 | return; |
701 | ||
702 | omapdss_output_unset_device(dssdev); | |
703 | ||
704 | if (dssdev->manager) | |
705 | dss_mgr_disconnect(dssdev->manager, dssdev); | |
706 | } | |
707 | ||
708 | static const struct omapdss_dpi_ops dpi_ops = { | |
709 | .connect = dpi_connect, | |
710 | .disconnect = dpi_disconnect, | |
711 | ||
86a3efe1 TV |
712 | .enable = dpi_display_enable, |
713 | .disable = dpi_display_disable, | |
0b24edb1 TV |
714 | |
715 | .check_timings = dpi_check_timings, | |
86a3efe1 | 716 | .set_timings = dpi_set_timings, |
0b24edb1 TV |
717 | .get_timings = dpi_get_timings, |
718 | ||
86a3efe1 | 719 | .set_data_lines = dpi_set_data_lines, |
0b24edb1 TV |
720 | }; |
721 | ||
94cf394b | 722 | static void dpi_init_output(struct platform_device *pdev) |
81b87f51 | 723 | { |
2ac6a1aa | 724 | struct dpi_data *dpi = dpi_get_data_from_pdev(pdev); |
630d2d0d | 725 | struct omap_dss_device *out = &dpi->output; |
81b87f51 | 726 | |
1f68d9c4 | 727 | out->dev = &pdev->dev; |
81b87f51 | 728 | out->id = OMAP_DSS_OUTPUT_DPI; |
1f68d9c4 | 729 | out->output_type = OMAP_DISPLAY_TYPE_DPI; |
7286a08f | 730 | out->name = "dpi.0"; |
f7e38fe9 | 731 | out->dispc_channel = dpi_get_channel(0); |
0b24edb1 | 732 | out->ops.dpi = &dpi_ops; |
b7328e14 | 733 | out->owner = THIS_MODULE; |
81b87f51 | 734 | |
5d47dbc8 | 735 | omapdss_register_output(out); |
81b87f51 AT |
736 | } |
737 | ||
ede92695 | 738 | static void dpi_uninit_output(struct platform_device *pdev) |
81b87f51 | 739 | { |
2ac6a1aa | 740 | struct dpi_data *dpi = dpi_get_data_from_pdev(pdev); |
630d2d0d | 741 | struct omap_dss_device *out = &dpi->output; |
81b87f51 | 742 | |
5d47dbc8 | 743 | omapdss_unregister_output(out); |
81b87f51 AT |
744 | } |
745 | ||
80eb6751 AT |
746 | static void dpi_init_output_port(struct platform_device *pdev, |
747 | struct device_node *port) | |
748 | { | |
749 | struct dpi_data *dpi = port->data; | |
750 | struct omap_dss_device *out = &dpi->output; | |
f7e38fe9 AT |
751 | int r; |
752 | u32 port_num; | |
753 | ||
754 | r = of_property_read_u32(port, "reg", &port_num); | |
755 | if (r) | |
756 | port_num = 0; | |
757 | ||
758 | switch (port_num) { | |
759 | case 2: | |
760 | out->name = "dpi.2"; | |
761 | break; | |
762 | case 1: | |
763 | out->name = "dpi.1"; | |
764 | break; | |
765 | case 0: | |
766 | default: | |
767 | out->name = "dpi.0"; | |
768 | break; | |
769 | } | |
80eb6751 AT |
770 | |
771 | out->dev = &pdev->dev; | |
772 | out->id = OMAP_DSS_OUTPUT_DPI; | |
773 | out->output_type = OMAP_DISPLAY_TYPE_DPI; | |
f7e38fe9 AT |
774 | out->dispc_channel = dpi_get_channel(port_num); |
775 | out->port_num = port_num; | |
80eb6751 AT |
776 | out->ops.dpi = &dpi_ops; |
777 | out->owner = THIS_MODULE; | |
778 | ||
779 | omapdss_register_output(out); | |
780 | } | |
781 | ||
ede92695 | 782 | static void dpi_uninit_output_port(struct device_node *port) |
80eb6751 AT |
783 | { |
784 | struct dpi_data *dpi = port->data; | |
785 | struct omap_dss_device *out = &dpi->output; | |
786 | ||
787 | omapdss_unregister_output(out); | |
788 | } | |
789 | ||
736e60dd | 790 | static int dpi_bind(struct device *dev, struct device *master, void *data) |
38f3daf6 | 791 | { |
736e60dd | 792 | struct platform_device *pdev = to_platform_device(dev); |
2ac6a1aa AT |
793 | struct dpi_data *dpi; |
794 | ||
795 | dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL); | |
796 | if (!dpi) | |
797 | return -ENOMEM; | |
630d2d0d AT |
798 | |
799 | dpi->pdev = pdev; | |
00df43b8 | 800 | |
630d2d0d AT |
801 | dev_set_drvdata(&pdev->dev, dpi); |
802 | ||
803 | mutex_init(&dpi->lock); | |
c8a5e4e8 | 804 | |
81b87f51 AT |
805 | dpi_init_output(pdev); |
806 | ||
5f42f2ce TV |
807 | return 0; |
808 | } | |
809 | ||
736e60dd | 810 | static void dpi_unbind(struct device *dev, struct device *master, void *data) |
553c48cf | 811 | { |
736e60dd TV |
812 | struct platform_device *pdev = to_platform_device(dev); |
813 | ||
81b87f51 | 814 | dpi_uninit_output(pdev); |
736e60dd TV |
815 | } |
816 | ||
817 | static const struct component_ops dpi_component_ops = { | |
818 | .bind = dpi_bind, | |
819 | .unbind = dpi_unbind, | |
820 | }; | |
81b87f51 | 821 | |
736e60dd TV |
822 | static int dpi_probe(struct platform_device *pdev) |
823 | { | |
824 | return component_add(&pdev->dev, &dpi_component_ops); | |
825 | } | |
826 | ||
827 | static int dpi_remove(struct platform_device *pdev) | |
828 | { | |
829 | component_del(&pdev->dev, &dpi_component_ops); | |
a57dd4fe | 830 | return 0; |
553c48cf TV |
831 | } |
832 | ||
a57dd4fe | 833 | static struct platform_driver omap_dpi_driver = { |
736e60dd TV |
834 | .probe = dpi_probe, |
835 | .remove = dpi_remove, | |
a57dd4fe TV |
836 | .driver = { |
837 | .name = "omapdss_dpi", | |
422ccbd5 | 838 | .suppress_bind_attrs = true, |
a57dd4fe TV |
839 | }, |
840 | }; | |
841 | ||
6e7e8f06 | 842 | int __init dpi_init_platform_driver(void) |
a57dd4fe | 843 | { |
94cf394b | 844 | return platform_driver_register(&omap_dpi_driver); |
a57dd4fe TV |
845 | } |
846 | ||
ede92695 | 847 | void dpi_uninit_platform_driver(void) |
a57dd4fe TV |
848 | { |
849 | platform_driver_unregister(&omap_dpi_driver); | |
850 | } | |
2ecef246 | 851 | |
ede92695 | 852 | int dpi_init_port(struct platform_device *pdev, struct device_node *port) |
2ecef246 | 853 | { |
2ac6a1aa | 854 | struct dpi_data *dpi; |
2ecef246 TV |
855 | struct device_node *ep; |
856 | u32 datalines; | |
857 | int r; | |
858 | ||
2ac6a1aa AT |
859 | dpi = devm_kzalloc(&pdev->dev, sizeof(*dpi), GFP_KERNEL); |
860 | if (!dpi) | |
861 | return -ENOMEM; | |
862 | ||
2ecef246 TV |
863 | ep = omapdss_of_get_next_endpoint(port, NULL); |
864 | if (!ep) | |
865 | return 0; | |
866 | ||
867 | r = of_property_read_u32(ep, "data-lines", &datalines); | |
868 | if (r) { | |
869 | DSSERR("failed to parse datalines\n"); | |
870 | goto err_datalines; | |
871 | } | |
872 | ||
630d2d0d | 873 | dpi->data_lines = datalines; |
2ecef246 TV |
874 | |
875 | of_node_put(ep); | |
876 | ||
630d2d0d | 877 | dpi->pdev = pdev; |
80eb6751 | 878 | port->data = dpi; |
2ecef246 | 879 | |
630d2d0d | 880 | mutex_init(&dpi->lock); |
2ecef246 | 881 | |
80eb6751 | 882 | dpi_init_output_port(pdev, port); |
2ecef246 | 883 | |
630d2d0d | 884 | dpi->port_initialized = true; |
2ecef246 TV |
885 | |
886 | return 0; | |
887 | ||
888 | err_datalines: | |
889 | of_node_put(ep); | |
890 | ||
891 | return r; | |
892 | } | |
893 | ||
ede92695 | 894 | void dpi_uninit_port(struct device_node *port) |
2ecef246 | 895 | { |
80eb6751 | 896 | struct dpi_data *dpi = port->data; |
630d2d0d AT |
897 | |
898 | if (!dpi->port_initialized) | |
2ecef246 TV |
899 | return; |
900 | ||
80eb6751 | 901 | dpi_uninit_output_port(port); |
2ecef246 | 902 | } |