blob: 8fb4c207f3f17ab185623fc31e5a72a579e6fdfe [file] [log] [blame]
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001/*
2 * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>
3 * Copyright (C) 2005-2009 Freescale Semiconductor, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15#include <linux/module.h>
16#include <linux/export.h>
17#include <linux/types.h>
Philipp Zabel6c641552013-03-28 17:35:21 +010018#include <linux/reset.h>
Sascha Haueraecfbdb2012-09-21 10:07:49 +020019#include <linux/platform_device.h>
20#include <linux/err.h>
21#include <linux/spinlock.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/clk.h>
26#include <linux/list.h>
27#include <linux/irq.h>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000028#include <linux/irqchip/chained_irq.h>
Philipp Zabelb7287662013-06-21 10:27:39 +020029#include <linux/irqdomain.h>
Sascha Haueraecfbdb2012-09-21 10:07:49 +020030#include <linux/of_device.h>
Sascha Haueraecfbdb2012-09-21 10:07:49 +020031
Philipp Zabel7cb17792013-10-10 16:18:38 +020032#include <drm/drm_fourcc.h>
33
Sascha Haueraecfbdb2012-09-21 10:07:49 +020034#include "imx-ipu-v3.h"
35#include "ipu-prv.h"
36
37static inline u32 ipu_cm_read(struct ipu_soc *ipu, unsigned offset)
38{
39 return readl(ipu->cm_reg + offset);
40}
41
42static inline void ipu_cm_write(struct ipu_soc *ipu, u32 value, unsigned offset)
43{
44 writel(value, ipu->cm_reg + offset);
45}
46
47static inline u32 ipu_idmac_read(struct ipu_soc *ipu, unsigned offset)
48{
49 return readl(ipu->idmac_reg + offset);
50}
51
52static inline void ipu_idmac_write(struct ipu_soc *ipu, u32 value,
53 unsigned offset)
54{
55 writel(value, ipu->idmac_reg + offset);
56}
57
58void ipu_srm_dp_sync_update(struct ipu_soc *ipu)
59{
60 u32 val;
61
62 val = ipu_cm_read(ipu, IPU_SRM_PRI2);
63 val |= 0x8;
64 ipu_cm_write(ipu, val, IPU_SRM_PRI2);
65}
66EXPORT_SYMBOL_GPL(ipu_srm_dp_sync_update);
67
68struct ipu_ch_param __iomem *ipu_get_cpmem(struct ipuv3_channel *channel)
69{
70 struct ipu_soc *ipu = channel->ipu;
71
72 return ipu->cpmem_base + channel->num;
73}
74EXPORT_SYMBOL_GPL(ipu_get_cpmem);
75
76void ipu_cpmem_set_high_priority(struct ipuv3_channel *channel)
77{
78 struct ipu_soc *ipu = channel->ipu;
79 struct ipu_ch_param __iomem *p = ipu_get_cpmem(channel);
80 u32 val;
81
82 if (ipu->ipu_type == IPUV3EX)
83 ipu_ch_param_write_field(p, IPU_FIELD_ID, 1);
84
85 val = ipu_idmac_read(ipu, IDMAC_CHA_PRI(channel->num));
86 val |= 1 << (channel->num % 32);
87 ipu_idmac_write(ipu, val, IDMAC_CHA_PRI(channel->num));
88};
89EXPORT_SYMBOL_GPL(ipu_cpmem_set_high_priority);
90
91void ipu_ch_param_write_field(struct ipu_ch_param __iomem *base, u32 wbs, u32 v)
92{
93 u32 bit = (wbs >> 8) % 160;
94 u32 size = wbs & 0xff;
95 u32 word = (wbs >> 8) / 160;
96 u32 i = bit / 32;
97 u32 ofs = bit % 32;
98 u32 mask = (1 << size) - 1;
99 u32 val;
100
101 pr_debug("%s %d %d %d\n", __func__, word, bit , size);
102
103 val = readl(&base->word[word].data[i]);
104 val &= ~(mask << ofs);
105 val |= v << ofs;
106 writel(val, &base->word[word].data[i]);
107
108 if ((bit + size - 1) / 32 > i) {
109 val = readl(&base->word[word].data[i + 1]);
110 val &= ~(mask >> (ofs ? (32 - ofs) : 0));
111 val |= v >> (ofs ? (32 - ofs) : 0);
112 writel(val, &base->word[word].data[i + 1]);
113 }
114}
115EXPORT_SYMBOL_GPL(ipu_ch_param_write_field);
116
117u32 ipu_ch_param_read_field(struct ipu_ch_param __iomem *base, u32 wbs)
118{
119 u32 bit = (wbs >> 8) % 160;
120 u32 size = wbs & 0xff;
121 u32 word = (wbs >> 8) / 160;
122 u32 i = bit / 32;
123 u32 ofs = bit % 32;
124 u32 mask = (1 << size) - 1;
125 u32 val = 0;
126
127 pr_debug("%s %d %d %d\n", __func__, word, bit , size);
128
129 val = (readl(&base->word[word].data[i]) >> ofs) & mask;
130
131 if ((bit + size - 1) / 32 > i) {
132 u32 tmp;
133 tmp = readl(&base->word[word].data[i + 1]);
134 tmp &= mask >> (ofs ? (32 - ofs) : 0);
135 val |= tmp << (ofs ? (32 - ofs) : 0);
136 }
137
138 return val;
139}
140EXPORT_SYMBOL_GPL(ipu_ch_param_read_field);
141
142int ipu_cpmem_set_format_rgb(struct ipu_ch_param __iomem *p,
Philipp Zabele56af862013-10-10 16:18:37 +0200143 const struct ipu_rgb *rgb)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200144{
145 int bpp = 0, npb = 0, ro, go, bo, to;
146
147 ro = rgb->bits_per_pixel - rgb->red.length - rgb->red.offset;
148 go = rgb->bits_per_pixel - rgb->green.length - rgb->green.offset;
149 bo = rgb->bits_per_pixel - rgb->blue.length - rgb->blue.offset;
150 to = rgb->bits_per_pixel - rgb->transp.length - rgb->transp.offset;
151
152 ipu_ch_param_write_field(p, IPU_FIELD_WID0, rgb->red.length - 1);
153 ipu_ch_param_write_field(p, IPU_FIELD_OFS0, ro);
154 ipu_ch_param_write_field(p, IPU_FIELD_WID1, rgb->green.length - 1);
155 ipu_ch_param_write_field(p, IPU_FIELD_OFS1, go);
156 ipu_ch_param_write_field(p, IPU_FIELD_WID2, rgb->blue.length - 1);
157 ipu_ch_param_write_field(p, IPU_FIELD_OFS2, bo);
158
159 if (rgb->transp.length) {
160 ipu_ch_param_write_field(p, IPU_FIELD_WID3,
161 rgb->transp.length - 1);
162 ipu_ch_param_write_field(p, IPU_FIELD_OFS3, to);
163 } else {
164 ipu_ch_param_write_field(p, IPU_FIELD_WID3, 7);
165 ipu_ch_param_write_field(p, IPU_FIELD_OFS3,
166 rgb->bits_per_pixel);
167 }
168
169 switch (rgb->bits_per_pixel) {
170 case 32:
171 bpp = 0;
172 npb = 15;
173 break;
174 case 24:
175 bpp = 1;
176 npb = 19;
177 break;
178 case 16:
179 bpp = 3;
180 npb = 31;
181 break;
182 case 8:
183 bpp = 5;
184 npb = 63;
185 break;
186 default:
187 return -EINVAL;
188 }
189 ipu_ch_param_write_field(p, IPU_FIELD_BPP, bpp);
190 ipu_ch_param_write_field(p, IPU_FIELD_NPB, npb);
191 ipu_ch_param_write_field(p, IPU_FIELD_PFS, 7); /* rgb mode */
192
193 return 0;
194}
195EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_rgb);
196
197int ipu_cpmem_set_format_passthrough(struct ipu_ch_param __iomem *p,
198 int width)
199{
200 int bpp = 0, npb = 0;
201
202 switch (width) {
203 case 32:
204 bpp = 0;
205 npb = 15;
206 break;
207 case 24:
208 bpp = 1;
209 npb = 19;
210 break;
211 case 16:
212 bpp = 3;
213 npb = 31;
214 break;
215 case 8:
216 bpp = 5;
217 npb = 63;
218 break;
219 default:
220 return -EINVAL;
221 }
222
223 ipu_ch_param_write_field(p, IPU_FIELD_BPP, bpp);
224 ipu_ch_param_write_field(p, IPU_FIELD_NPB, npb);
225 ipu_ch_param_write_field(p, IPU_FIELD_PFS, 6); /* raw mode */
226
227 return 0;
228}
229EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_passthrough);
230
Fabio Estevam6cadd882013-03-23 19:43:32 -0300231void ipu_cpmem_set_yuv_interleaved(struct ipu_ch_param __iomem *p,
232 u32 pixel_format)
Philipp Zabel0125f212012-11-12 16:29:02 +0100233{
234 switch (pixel_format) {
235 case V4L2_PIX_FMT_UYVY:
236 ipu_ch_param_write_field(p, IPU_FIELD_BPP, 3); /* bits/pixel */
237 ipu_ch_param_write_field(p, IPU_FIELD_PFS, 0xA); /* pix format */
238 ipu_ch_param_write_field(p, IPU_FIELD_NPB, 31); /* burst size */
239 break;
240 case V4L2_PIX_FMT_YUYV:
241 ipu_ch_param_write_field(p, IPU_FIELD_BPP, 3); /* bits/pixel */
242 ipu_ch_param_write_field(p, IPU_FIELD_PFS, 0x8); /* pix format */
243 ipu_ch_param_write_field(p, IPU_FIELD_NPB, 31); /* burst size */
244 break;
245 }
246}
247EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_interleaved);
248
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200249void ipu_cpmem_set_yuv_planar_full(struct ipu_ch_param __iomem *p,
250 u32 pixel_format, int stride, int u_offset, int v_offset)
251{
252 switch (pixel_format) {
253 case V4L2_PIX_FMT_YUV420:
254 ipu_ch_param_write_field(p, IPU_FIELD_SLUV, (stride / 2) - 1);
255 ipu_ch_param_write_field(p, IPU_FIELD_UBO, u_offset / 8);
256 ipu_ch_param_write_field(p, IPU_FIELD_VBO, v_offset / 8);
257 break;
Philipp Zabeld3e4e612012-11-12 16:29:00 +0100258 case V4L2_PIX_FMT_YVU420:
259 ipu_ch_param_write_field(p, IPU_FIELD_SLUV, (stride / 2) - 1);
260 ipu_ch_param_write_field(p, IPU_FIELD_UBO, v_offset / 8);
261 ipu_ch_param_write_field(p, IPU_FIELD_VBO, u_offset / 8);
262 break;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200263 }
264}
265EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_planar_full);
266
267void ipu_cpmem_set_yuv_planar(struct ipu_ch_param __iomem *p, u32 pixel_format,
268 int stride, int height)
269{
270 int u_offset, v_offset;
271 int uv_stride = 0;
272
273 switch (pixel_format) {
274 case V4L2_PIX_FMT_YUV420:
Philipp Zabeld3e4e612012-11-12 16:29:00 +0100275 case V4L2_PIX_FMT_YVU420:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200276 uv_stride = stride / 2;
277 u_offset = stride * height;
278 v_offset = u_offset + (uv_stride * height / 2);
Philipp Zabeld3e4e612012-11-12 16:29:00 +0100279 ipu_cpmem_set_yuv_planar_full(p, pixel_format, stride,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200280 u_offset, v_offset);
281 break;
282 }
283}
284EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_planar);
285
Philipp Zabele56af862013-10-10 16:18:37 +0200286static const struct ipu_rgb def_rgb_32 = {
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200287 .red = { .offset = 16, .length = 8, },
288 .green = { .offset = 8, .length = 8, },
289 .blue = { .offset = 0, .length = 8, },
290 .transp = { .offset = 24, .length = 8, },
291 .bits_per_pixel = 32,
292};
293
Philipp Zabele56af862013-10-10 16:18:37 +0200294static const struct ipu_rgb def_bgr_32 = {
Philipp Zabel7cb17792013-10-10 16:18:38 +0200295 .red = { .offset = 0, .length = 8, },
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200296 .green = { .offset = 8, .length = 8, },
Philipp Zabel7cb17792013-10-10 16:18:38 +0200297 .blue = { .offset = 16, .length = 8, },
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200298 .transp = { .offset = 24, .length = 8, },
299 .bits_per_pixel = 32,
300};
301
Philipp Zabele56af862013-10-10 16:18:37 +0200302static const struct ipu_rgb def_rgb_24 = {
Philipp Zabel7cb17792013-10-10 16:18:38 +0200303 .red = { .offset = 16, .length = 8, },
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200304 .green = { .offset = 8, .length = 8, },
Philipp Zabel7cb17792013-10-10 16:18:38 +0200305 .blue = { .offset = 0, .length = 8, },
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200306 .transp = { .offset = 0, .length = 0, },
307 .bits_per_pixel = 24,
308};
309
Philipp Zabele56af862013-10-10 16:18:37 +0200310static const struct ipu_rgb def_bgr_24 = {
Philipp Zabel7cb17792013-10-10 16:18:38 +0200311 .red = { .offset = 0, .length = 8, },
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200312 .green = { .offset = 8, .length = 8, },
Philipp Zabel7cb17792013-10-10 16:18:38 +0200313 .blue = { .offset = 16, .length = 8, },
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200314 .transp = { .offset = 0, .length = 0, },
315 .bits_per_pixel = 24,
316};
317
Philipp Zabele56af862013-10-10 16:18:37 +0200318static const struct ipu_rgb def_rgb_16 = {
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200319 .red = { .offset = 11, .length = 5, },
320 .green = { .offset = 5, .length = 6, },
321 .blue = { .offset = 0, .length = 5, },
322 .transp = { .offset = 0, .length = 0, },
323 .bits_per_pixel = 16,
324};
325
Philipp Zabel38fc7b32013-10-10 16:18:39 +0200326static const struct ipu_rgb def_bgr_16 = {
327 .red = { .offset = 0, .length = 5, },
328 .green = { .offset = 5, .length = 6, },
329 .blue = { .offset = 11, .length = 5, },
330 .transp = { .offset = 0, .length = 0, },
331 .bits_per_pixel = 16,
332};
333
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200334#define Y_OFFSET(pix, x, y) ((x) + pix->width * (y))
335#define U_OFFSET(pix, x, y) ((pix->width * pix->height) + \
336 (pix->width * (y) / 4) + (x) / 2)
337#define V_OFFSET(pix, x, y) ((pix->width * pix->height) + \
338 (pix->width * pix->height / 4) + \
339 (pix->width * (y) / 4) + (x) / 2)
340
Philipp Zabel7cb17792013-10-10 16:18:38 +0200341int ipu_cpmem_set_fmt(struct ipu_ch_param __iomem *cpmem, u32 drm_fourcc)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200342{
Philipp Zabel7cb17792013-10-10 16:18:38 +0200343 switch (drm_fourcc) {
344 case DRM_FORMAT_YUV420:
345 case DRM_FORMAT_YVU420:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200346 /* pix format */
347 ipu_ch_param_write_field(cpmem, IPU_FIELD_PFS, 2);
348 /* burst size */
349 ipu_ch_param_write_field(cpmem, IPU_FIELD_NPB, 63);
350 break;
Philipp Zabel7cb17792013-10-10 16:18:38 +0200351 case DRM_FORMAT_UYVY:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200352 /* bits/pixel */
353 ipu_ch_param_write_field(cpmem, IPU_FIELD_BPP, 3);
354 /* pix format */
355 ipu_ch_param_write_field(cpmem, IPU_FIELD_PFS, 0xA);
356 /* burst size */
357 ipu_ch_param_write_field(cpmem, IPU_FIELD_NPB, 31);
358 break;
Philipp Zabel7cb17792013-10-10 16:18:38 +0200359 case DRM_FORMAT_YUYV:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200360 /* bits/pixel */
361 ipu_ch_param_write_field(cpmem, IPU_FIELD_BPP, 3);
362 /* pix format */
363 ipu_ch_param_write_field(cpmem, IPU_FIELD_PFS, 0x8);
364 /* burst size */
365 ipu_ch_param_write_field(cpmem, IPU_FIELD_NPB, 31);
366 break;
Philipp Zabel7cb17792013-10-10 16:18:38 +0200367 case DRM_FORMAT_ABGR8888:
368 case DRM_FORMAT_XBGR8888:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200369 ipu_cpmem_set_format_rgb(cpmem, &def_bgr_32);
370 break;
Philipp Zabel7cb17792013-10-10 16:18:38 +0200371 case DRM_FORMAT_ARGB8888:
372 case DRM_FORMAT_XRGB8888:
373 ipu_cpmem_set_format_rgb(cpmem, &def_rgb_32);
374 break;
375 case DRM_FORMAT_BGR888:
376 ipu_cpmem_set_format_rgb(cpmem, &def_bgr_24);
377 break;
378 case DRM_FORMAT_RGB888:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200379 ipu_cpmem_set_format_rgb(cpmem, &def_rgb_24);
380 break;
Philipp Zabel7cb17792013-10-10 16:18:38 +0200381 case DRM_FORMAT_RGB565:
382 ipu_cpmem_set_format_rgb(cpmem, &def_rgb_16);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200383 break;
Philipp Zabel38fc7b32013-10-10 16:18:39 +0200384 case DRM_FORMAT_BGR565:
385 ipu_cpmem_set_format_rgb(cpmem, &def_bgr_16);
386 break;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200387 default:
388 return -EINVAL;
389 }
390
391 return 0;
392}
393EXPORT_SYMBOL_GPL(ipu_cpmem_set_fmt);
394
Philipp Zabel7cb17792013-10-10 16:18:38 +0200395/*
396 * The V4L2 spec defines packed RGB formats in memory byte order, which from
397 * point of view of the IPU corresponds to little-endian words with the first
398 * component in the least significant bits.
399 * The DRM pixel formats and IPU internal representation are ordered the other
400 * way around, with the first named component ordered at the most significant
401 * bits. Further, V4L2 formats are not well defined:
402 * http://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html
403 * We choose the interpretation which matches GStreamer behavior.
404 */
405static int v4l2_pix_fmt_to_drm_fourcc(u32 pixelformat)
406{
407 switch (pixelformat) {
408 case V4L2_PIX_FMT_RGB565:
409 /*
410 * Here we choose the 'corrected' interpretation of RGBP, a
411 * little-endian 16-bit word with the red component at the most
412 * significant bits:
413 * g[2:0]b[4:0] r[4:0]g[5:3] <=> [16:0] R:G:B
414 */
415 return DRM_FORMAT_RGB565;
416 case V4L2_PIX_FMT_BGR24:
417 /* B G R <=> [24:0] R:G:B */
418 return DRM_FORMAT_RGB888;
419 case V4L2_PIX_FMT_RGB24:
420 /* R G B <=> [24:0] B:G:R */
421 return DRM_FORMAT_BGR888;
422 case V4L2_PIX_FMT_BGR32:
423 /* B G R A <=> [32:0] A:B:G:R */
424 return DRM_FORMAT_XRGB8888;
425 case V4L2_PIX_FMT_RGB32:
426 /* R G B A <=> [32:0] A:B:G:R */
427 return DRM_FORMAT_XBGR8888;
428 case V4L2_PIX_FMT_UYVY:
429 return DRM_FORMAT_UYVY;
430 case V4L2_PIX_FMT_YUYV:
431 return DRM_FORMAT_YUYV;
432 case V4L2_PIX_FMT_YUV420:
433 return DRM_FORMAT_YUV420;
434 case V4L2_PIX_FMT_YVU420:
435 return DRM_FORMAT_YVU420;
436 }
437
438 return -EINVAL;
439}
440
441enum ipu_color_space ipu_drm_fourcc_to_colorspace(u32 drm_fourcc)
442{
443 switch (drm_fourcc) {
444 case DRM_FORMAT_RGB565:
445 case DRM_FORMAT_BGR565:
446 case DRM_FORMAT_RGB888:
447 case DRM_FORMAT_BGR888:
448 case DRM_FORMAT_XRGB8888:
449 case DRM_FORMAT_XBGR8888:
450 case DRM_FORMAT_RGBX8888:
451 case DRM_FORMAT_BGRX8888:
452 case DRM_FORMAT_ARGB8888:
453 case DRM_FORMAT_ABGR8888:
454 case DRM_FORMAT_RGBA8888:
455 case DRM_FORMAT_BGRA8888:
456 return IPUV3_COLORSPACE_RGB;
457 case DRM_FORMAT_YUYV:
458 case DRM_FORMAT_UYVY:
459 case DRM_FORMAT_YUV420:
460 case DRM_FORMAT_YVU420:
461 return IPUV3_COLORSPACE_YUV;
462 default:
463 return IPUV3_COLORSPACE_UNKNOWN;
464 }
465}
466EXPORT_SYMBOL_GPL(ipu_drm_fourcc_to_colorspace);
467
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200468int ipu_cpmem_set_image(struct ipu_ch_param __iomem *cpmem,
469 struct ipu_image *image)
470{
471 struct v4l2_pix_format *pix = &image->pix;
472 int y_offset, u_offset, v_offset;
473
474 pr_debug("%s: resolution: %dx%d stride: %d\n",
475 __func__, pix->width, pix->height,
476 pix->bytesperline);
477
478 ipu_cpmem_set_resolution(cpmem, image->rect.width,
479 image->rect.height);
480 ipu_cpmem_set_stride(cpmem, pix->bytesperline);
481
Philipp Zabel7cb17792013-10-10 16:18:38 +0200482 ipu_cpmem_set_fmt(cpmem, v4l2_pix_fmt_to_drm_fourcc(pix->pixelformat));
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200483
484 switch (pix->pixelformat) {
485 case V4L2_PIX_FMT_YUV420:
Philipp Zabeld3e4e612012-11-12 16:29:00 +0100486 case V4L2_PIX_FMT_YVU420:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200487 y_offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
488 u_offset = U_OFFSET(pix, image->rect.left,
489 image->rect.top) - y_offset;
490 v_offset = V_OFFSET(pix, image->rect.left,
491 image->rect.top) - y_offset;
492
493 ipu_cpmem_set_yuv_planar_full(cpmem, pix->pixelformat,
494 pix->bytesperline, u_offset, v_offset);
495 ipu_cpmem_set_buffer(cpmem, 0, image->phys + y_offset);
496 break;
497 case V4L2_PIX_FMT_UYVY:
Michael Olbrichc096ae12012-11-12 16:28:59 +0100498 case V4L2_PIX_FMT_YUYV:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200499 ipu_cpmem_set_buffer(cpmem, 0, image->phys +
500 image->rect.left * 2 +
501 image->rect.top * image->pix.bytesperline);
502 break;
503 case V4L2_PIX_FMT_RGB32:
504 case V4L2_PIX_FMT_BGR32:
505 ipu_cpmem_set_buffer(cpmem, 0, image->phys +
506 image->rect.left * 4 +
507 image->rect.top * image->pix.bytesperline);
508 break;
509 case V4L2_PIX_FMT_RGB565:
510 ipu_cpmem_set_buffer(cpmem, 0, image->phys +
511 image->rect.left * 2 +
512 image->rect.top * image->pix.bytesperline);
513 break;
514 case V4L2_PIX_FMT_RGB24:
515 case V4L2_PIX_FMT_BGR24:
516 ipu_cpmem_set_buffer(cpmem, 0, image->phys +
517 image->rect.left * 3 +
518 image->rect.top * image->pix.bytesperline);
519 break;
520 default:
521 return -EINVAL;
522 }
523
524 return 0;
525}
526EXPORT_SYMBOL_GPL(ipu_cpmem_set_image);
527
528enum ipu_color_space ipu_pixelformat_to_colorspace(u32 pixelformat)
529{
530 switch (pixelformat) {
531 case V4L2_PIX_FMT_YUV420:
Philipp Zabeld3e4e612012-11-12 16:29:00 +0100532 case V4L2_PIX_FMT_YVU420:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200533 case V4L2_PIX_FMT_UYVY:
Michael Olbrichc096ae12012-11-12 16:28:59 +0100534 case V4L2_PIX_FMT_YUYV:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200535 return IPUV3_COLORSPACE_YUV;
536 case V4L2_PIX_FMT_RGB32:
537 case V4L2_PIX_FMT_BGR32:
538 case V4L2_PIX_FMT_RGB24:
539 case V4L2_PIX_FMT_BGR24:
540 case V4L2_PIX_FMT_RGB565:
541 return IPUV3_COLORSPACE_RGB;
542 default:
543 return IPUV3_COLORSPACE_UNKNOWN;
544 }
545}
546EXPORT_SYMBOL_GPL(ipu_pixelformat_to_colorspace);
547
548struct ipuv3_channel *ipu_idmac_get(struct ipu_soc *ipu, unsigned num)
549{
550 struct ipuv3_channel *channel;
551
552 dev_dbg(ipu->dev, "%s %d\n", __func__, num);
553
554 if (num > 63)
555 return ERR_PTR(-ENODEV);
556
557 mutex_lock(&ipu->channel_lock);
558
559 channel = &ipu->channel[num];
560
561 if (channel->busy) {
562 channel = ERR_PTR(-EBUSY);
563 goto out;
564 }
565
Valentina Manea89bc5be2013-10-25 11:52:20 +0300566 channel->busy = true;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200567 channel->num = num;
568
569out:
570 mutex_unlock(&ipu->channel_lock);
571
572 return channel;
573}
574EXPORT_SYMBOL_GPL(ipu_idmac_get);
575
576void ipu_idmac_put(struct ipuv3_channel *channel)
577{
578 struct ipu_soc *ipu = channel->ipu;
579
580 dev_dbg(ipu->dev, "%s %d\n", __func__, channel->num);
581
582 mutex_lock(&ipu->channel_lock);
583
Valentina Manea89bc5be2013-10-25 11:52:20 +0300584 channel->busy = false;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200585
586 mutex_unlock(&ipu->channel_lock);
587}
588EXPORT_SYMBOL_GPL(ipu_idmac_put);
589
590#define idma_mask(ch) (1 << (ch & 0x1f))
591
592void ipu_idmac_set_double_buffer(struct ipuv3_channel *channel,
593 bool doublebuffer)
594{
595 struct ipu_soc *ipu = channel->ipu;
596 unsigned long flags;
597 u32 reg;
598
599 spin_lock_irqsave(&ipu->lock, flags);
600
601 reg = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
602 if (doublebuffer)
603 reg |= idma_mask(channel->num);
604 else
605 reg &= ~idma_mask(channel->num);
606 ipu_cm_write(ipu, reg, IPU_CHA_DB_MODE_SEL(channel->num));
607
608 spin_unlock_irqrestore(&ipu->lock, flags);
609}
610EXPORT_SYMBOL_GPL(ipu_idmac_set_double_buffer);
611
612int ipu_module_enable(struct ipu_soc *ipu, u32 mask)
613{
614 unsigned long lock_flags;
615 u32 val;
616
617 spin_lock_irqsave(&ipu->lock, lock_flags);
618
619 val = ipu_cm_read(ipu, IPU_DISP_GEN);
620
621 if (mask & IPU_CONF_DI0_EN)
622 val |= IPU_DI0_COUNTER_RELEASE;
623 if (mask & IPU_CONF_DI1_EN)
624 val |= IPU_DI1_COUNTER_RELEASE;
625
626 ipu_cm_write(ipu, val, IPU_DISP_GEN);
627
628 val = ipu_cm_read(ipu, IPU_CONF);
629 val |= mask;
630 ipu_cm_write(ipu, val, IPU_CONF);
631
632 spin_unlock_irqrestore(&ipu->lock, lock_flags);
633
634 return 0;
635}
636EXPORT_SYMBOL_GPL(ipu_module_enable);
637
638int ipu_module_disable(struct ipu_soc *ipu, u32 mask)
639{
640 unsigned long lock_flags;
641 u32 val;
642
643 spin_lock_irqsave(&ipu->lock, lock_flags);
644
645 val = ipu_cm_read(ipu, IPU_CONF);
646 val &= ~mask;
647 ipu_cm_write(ipu, val, IPU_CONF);
648
649 val = ipu_cm_read(ipu, IPU_DISP_GEN);
650
651 if (mask & IPU_CONF_DI0_EN)
652 val &= ~IPU_DI0_COUNTER_RELEASE;
653 if (mask & IPU_CONF_DI1_EN)
654 val &= ~IPU_DI1_COUNTER_RELEASE;
655
656 ipu_cm_write(ipu, val, IPU_DISP_GEN);
657
658 spin_unlock_irqrestore(&ipu->lock, lock_flags);
659
660 return 0;
661}
662EXPORT_SYMBOL_GPL(ipu_module_disable);
663
664void ipu_idmac_select_buffer(struct ipuv3_channel *channel, u32 buf_num)
665{
666 struct ipu_soc *ipu = channel->ipu;
667 unsigned int chno = channel->num;
668 unsigned long flags;
669
670 spin_lock_irqsave(&ipu->lock, flags);
671
672 /* Mark buffer as ready. */
673 if (buf_num == 0)
674 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
675 else
676 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
677
678 spin_unlock_irqrestore(&ipu->lock, flags);
679}
680EXPORT_SYMBOL_GPL(ipu_idmac_select_buffer);
681
682int ipu_idmac_enable_channel(struct ipuv3_channel *channel)
683{
684 struct ipu_soc *ipu = channel->ipu;
685 u32 val;
686 unsigned long flags;
687
688 spin_lock_irqsave(&ipu->lock, flags);
689
690 val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
691 val |= idma_mask(channel->num);
692 ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
693
694 spin_unlock_irqrestore(&ipu->lock, flags);
695
696 return 0;
697}
698EXPORT_SYMBOL_GPL(ipu_idmac_enable_channel);
699
Philipp Zabel17075502014-04-14 23:53:17 +0200700bool ipu_idmac_channel_busy(struct ipu_soc *ipu, unsigned int chno)
701{
702 return (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(chno)) & idma_mask(chno));
703}
704EXPORT_SYMBOL_GPL(ipu_idmac_channel_busy);
705
Sascha Hauerfb822a32013-10-10 16:18:41 +0200706int ipu_idmac_wait_busy(struct ipuv3_channel *channel, int ms)
707{
708 struct ipu_soc *ipu = channel->ipu;
709 unsigned long timeout;
710
711 timeout = jiffies + msecs_to_jiffies(ms);
712 while (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(channel->num)) &
713 idma_mask(channel->num)) {
714 if (time_after(jiffies, timeout))
715 return -ETIMEDOUT;
716 cpu_relax();
717 }
718
719 return 0;
720}
721EXPORT_SYMBOL_GPL(ipu_idmac_wait_busy);
722
Philipp Zabel17075502014-04-14 23:53:17 +0200723int ipu_wait_interrupt(struct ipu_soc *ipu, int irq, int ms)
724{
725 unsigned long timeout;
726
727 timeout = jiffies + msecs_to_jiffies(ms);
728 ipu_cm_write(ipu, BIT(irq % 32), IPU_INT_STAT(irq / 32));
729 while (!(ipu_cm_read(ipu, IPU_INT_STAT(irq / 32) & BIT(irq % 32)))) {
730 if (time_after(jiffies, timeout))
731 return -ETIMEDOUT;
732 cpu_relax();
733 }
734
735 return 0;
736}
737EXPORT_SYMBOL_GPL(ipu_wait_interrupt);
738
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200739int ipu_idmac_disable_channel(struct ipuv3_channel *channel)
740{
741 struct ipu_soc *ipu = channel->ipu;
742 u32 val;
743 unsigned long flags;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200744
745 spin_lock_irqsave(&ipu->lock, flags);
746
747 /* Disable DMA channel(s) */
748 val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
749 val &= ~idma_mask(channel->num);
750 ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
751
752 /* Set channel buffers NOT to be ready */
753 ipu_cm_write(ipu, 0xf0000000, IPU_GPR); /* write one to clear */
754
755 if (ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num)) &
756 idma_mask(channel->num)) {
757 ipu_cm_write(ipu, idma_mask(channel->num),
758 IPU_CHA_BUF0_RDY(channel->num));
759 }
760
761 if (ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num)) &
762 idma_mask(channel->num)) {
763 ipu_cm_write(ipu, idma_mask(channel->num),
764 IPU_CHA_BUF1_RDY(channel->num));
765 }
766
767 ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
768
769 /* Reset the double buffer */
770 val = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
771 val &= ~idma_mask(channel->num);
772 ipu_cm_write(ipu, val, IPU_CHA_DB_MODE_SEL(channel->num));
773
774 spin_unlock_irqrestore(&ipu->lock, flags);
775
776 return 0;
777}
778EXPORT_SYMBOL_GPL(ipu_idmac_disable_channel);
779
Philipp Zabel6c641552013-03-28 17:35:21 +0100780static int ipu_memory_reset(struct ipu_soc *ipu)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200781{
782 unsigned long timeout;
783
784 ipu_cm_write(ipu, 0x807FFFFF, IPU_MEM_RST);
785
786 timeout = jiffies + msecs_to_jiffies(1000);
787 while (ipu_cm_read(ipu, IPU_MEM_RST) & 0x80000000) {
788 if (time_after(jiffies, timeout))
789 return -ETIME;
790 cpu_relax();
791 }
792
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200793 return 0;
794}
795
796struct ipu_devtype {
797 const char *name;
798 unsigned long cm_ofs;
799 unsigned long cpmem_ofs;
800 unsigned long srm_ofs;
801 unsigned long tpm_ofs;
802 unsigned long disp0_ofs;
803 unsigned long disp1_ofs;
804 unsigned long dc_tmpl_ofs;
805 unsigned long vdi_ofs;
806 enum ipuv3_type type;
807};
808
809static struct ipu_devtype ipu_type_imx51 = {
810 .name = "IPUv3EX",
811 .cm_ofs = 0x1e000000,
812 .cpmem_ofs = 0x1f000000,
813 .srm_ofs = 0x1f040000,
814 .tpm_ofs = 0x1f060000,
815 .disp0_ofs = 0x1e040000,
816 .disp1_ofs = 0x1e048000,
817 .dc_tmpl_ofs = 0x1f080000,
818 .vdi_ofs = 0x1e068000,
819 .type = IPUV3EX,
820};
821
822static struct ipu_devtype ipu_type_imx53 = {
823 .name = "IPUv3M",
824 .cm_ofs = 0x06000000,
825 .cpmem_ofs = 0x07000000,
826 .srm_ofs = 0x07040000,
827 .tpm_ofs = 0x07060000,
828 .disp0_ofs = 0x06040000,
829 .disp1_ofs = 0x06048000,
830 .dc_tmpl_ofs = 0x07080000,
831 .vdi_ofs = 0x06068000,
832 .type = IPUV3M,
833};
834
835static struct ipu_devtype ipu_type_imx6q = {
836 .name = "IPUv3H",
837 .cm_ofs = 0x00200000,
838 .cpmem_ofs = 0x00300000,
839 .srm_ofs = 0x00340000,
840 .tpm_ofs = 0x00360000,
841 .disp0_ofs = 0x00240000,
842 .disp1_ofs = 0x00248000,
843 .dc_tmpl_ofs = 0x00380000,
844 .vdi_ofs = 0x00268000,
845 .type = IPUV3H,
846};
847
848static const struct of_device_id imx_ipu_dt_ids[] = {
849 { .compatible = "fsl,imx51-ipu", .data = &ipu_type_imx51, },
850 { .compatible = "fsl,imx53-ipu", .data = &ipu_type_imx53, },
851 { .compatible = "fsl,imx6q-ipu", .data = &ipu_type_imx6q, },
852 { /* sentinel */ }
853};
854MODULE_DEVICE_TABLE(of, imx_ipu_dt_ids);
855
856static int ipu_submodules_init(struct ipu_soc *ipu,
857 struct platform_device *pdev, unsigned long ipu_base,
858 struct clk *ipu_clk)
859{
860 char *unit;
861 int ret;
862 struct device *dev = &pdev->dev;
863 const struct ipu_devtype *devtype = ipu->devtype;
864
865 ret = ipu_di_init(ipu, dev, 0, ipu_base + devtype->disp0_ofs,
866 IPU_CONF_DI0_EN, ipu_clk);
867 if (ret) {
868 unit = "di0";
869 goto err_di_0;
870 }
871
872 ret = ipu_di_init(ipu, dev, 1, ipu_base + devtype->disp1_ofs,
873 IPU_CONF_DI1_EN, ipu_clk);
874 if (ret) {
875 unit = "di1";
876 goto err_di_1;
877 }
878
879 ret = ipu_dc_init(ipu, dev, ipu_base + devtype->cm_ofs +
880 IPU_CM_DC_REG_OFS, ipu_base + devtype->dc_tmpl_ofs);
881 if (ret) {
882 unit = "dc_template";
883 goto err_dc;
884 }
885
886 ret = ipu_dmfc_init(ipu, dev, ipu_base +
887 devtype->cm_ofs + IPU_CM_DMFC_REG_OFS, ipu_clk);
888 if (ret) {
889 unit = "dmfc";
890 goto err_dmfc;
891 }
892
893 ret = ipu_dp_init(ipu, dev, ipu_base + devtype->srm_ofs);
894 if (ret) {
895 unit = "dp";
896 goto err_dp;
897 }
898
899 return 0;
900
901err_dp:
902 ipu_dmfc_exit(ipu);
903err_dmfc:
904 ipu_dc_exit(ipu);
905err_dc:
906 ipu_di_exit(ipu, 1);
907err_di_1:
908 ipu_di_exit(ipu, 0);
909err_di_0:
910 dev_err(&pdev->dev, "init %s failed with %d\n", unit, ret);
911 return ret;
912}
913
914static void ipu_irq_handle(struct ipu_soc *ipu, const int *regs, int num_regs)
915{
916 unsigned long status;
Philipp Zabelb7287662013-06-21 10:27:39 +0200917 int i, bit, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200918
919 for (i = 0; i < num_regs; i++) {
920
921 status = ipu_cm_read(ipu, IPU_INT_STAT(regs[i]));
922 status &= ipu_cm_read(ipu, IPU_INT_CTRL(regs[i]));
923
Philipp Zabelb7287662013-06-21 10:27:39 +0200924 for_each_set_bit(bit, &status, 32) {
925 irq = irq_linear_revmap(ipu->domain, regs[i] * 32 + bit);
926 if (irq)
927 generic_handle_irq(irq);
928 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200929 }
930}
931
932static void ipu_irq_handler(unsigned int irq, struct irq_desc *desc)
933{
934 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
935 const int int_reg[] = { 0, 1, 2, 3, 10, 11, 12, 13, 14};
936 struct irq_chip *chip = irq_get_chip(irq);
937
938 chained_irq_enter(chip, desc);
939
940 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
941
942 chained_irq_exit(chip, desc);
943}
944
945static void ipu_err_irq_handler(unsigned int irq, struct irq_desc *desc)
946{
947 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
948 const int int_reg[] = { 4, 5, 8, 9};
949 struct irq_chip *chip = irq_get_chip(irq);
950
951 chained_irq_enter(chip, desc);
952
953 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
954
955 chained_irq_exit(chip, desc);
956}
957
Philipp Zabel861a50c2014-04-14 23:53:16 +0200958int ipu_map_irq(struct ipu_soc *ipu, int irq)
959{
960 int virq;
961
962 virq = irq_linear_revmap(ipu->domain, irq);
963 if (!virq)
964 virq = irq_create_mapping(ipu->domain, irq);
965
966 return virq;
967}
968EXPORT_SYMBOL_GPL(ipu_map_irq);
969
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200970int ipu_idmac_channel_irq(struct ipu_soc *ipu, struct ipuv3_channel *channel,
971 enum ipu_channel_irq irq_type)
972{
Philipp Zabel861a50c2014-04-14 23:53:16 +0200973 return ipu_map_irq(ipu, irq_type + channel->num);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200974}
975EXPORT_SYMBOL_GPL(ipu_idmac_channel_irq);
976
977static void ipu_submodules_exit(struct ipu_soc *ipu)
978{
979 ipu_dp_exit(ipu);
980 ipu_dmfc_exit(ipu);
981 ipu_dc_exit(ipu);
982 ipu_di_exit(ipu, 1);
983 ipu_di_exit(ipu, 0);
984}
985
986static int platform_remove_devices_fn(struct device *dev, void *unused)
987{
988 struct platform_device *pdev = to_platform_device(dev);
989
990 platform_device_unregister(pdev);
991
992 return 0;
993}
994
995static void platform_device_unregister_children(struct platform_device *pdev)
996{
997 device_for_each_child(&pdev->dev, NULL, platform_remove_devices_fn);
998}
999
1000struct ipu_platform_reg {
1001 struct ipu_client_platformdata pdata;
1002 const char *name;
1003};
1004
1005static const struct ipu_platform_reg client_reg[] = {
1006 {
1007 .pdata = {
1008 .di = 0,
1009 .dc = 5,
1010 .dp = IPU_DP_FLOW_SYNC_BG,
1011 .dma[0] = IPUV3_CHANNEL_MEM_BG_SYNC,
Philipp Zabelb8d181e2013-10-10 16:18:45 +02001012 .dma[1] = IPUV3_CHANNEL_MEM_FG_SYNC,
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001013 },
1014 .name = "imx-ipuv3-crtc",
1015 }, {
1016 .pdata = {
1017 .di = 1,
1018 .dc = 1,
1019 .dp = -EINVAL,
1020 .dma[0] = IPUV3_CHANNEL_MEM_DC_SYNC,
1021 .dma[1] = -EINVAL,
1022 },
1023 .name = "imx-ipuv3-crtc",
1024 },
1025};
1026
Russell King4ae078d2013-12-16 11:34:25 +00001027static DEFINE_MUTEX(ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001028static int ipu_client_id;
1029
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001030static int ipu_add_client_devices(struct ipu_soc *ipu)
1031{
Russell King4ae078d2013-12-16 11:34:25 +00001032 struct device *dev = ipu->dev;
1033 unsigned i;
1034 int id, ret;
1035
1036 mutex_lock(&ipu_client_id_mutex);
1037 id = ipu_client_id;
1038 ipu_client_id += ARRAY_SIZE(client_reg);
1039 mutex_unlock(&ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001040
1041 for (i = 0; i < ARRAY_SIZE(client_reg); i++) {
1042 const struct ipu_platform_reg *reg = &client_reg[i];
Russell King4ae078d2013-12-16 11:34:25 +00001043 struct platform_device *pdev;
1044
1045 pdev = platform_device_register_data(dev, reg->name,
1046 id++, &reg->pdata, sizeof(reg->pdata));
1047
1048 if (IS_ERR(pdev))
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001049 goto err_register;
1050 }
1051
1052 return 0;
1053
1054err_register:
Russell King4ae078d2013-12-16 11:34:25 +00001055 platform_device_unregister_children(to_platform_device(dev));
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001056
1057 return ret;
1058}
1059
Philipp Zabelb7287662013-06-21 10:27:39 +02001060
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001061static int ipu_irq_init(struct ipu_soc *ipu)
1062{
Philipp Zabel379cdec2013-06-21 14:52:17 +02001063 struct irq_chip_generic *gc;
1064 struct irq_chip_type *ct;
Philipp Zabel37f85b262013-06-21 14:52:18 +02001065 unsigned long unused[IPU_NUM_IRQS / 32] = {
1066 0x400100d0, 0xffe000fd,
1067 0x400100d0, 0xffe000fd,
1068 0x400100d0, 0xffe000fd,
1069 0x4077ffff, 0xffe7e1fd,
1070 0x23fffffe, 0x8880fff0,
1071 0xf98fe7d0, 0xfff81fff,
1072 0x400100d0, 0xffe000fd,
1073 0x00000000,
1074 };
Philipp Zabel379cdec2013-06-21 14:52:17 +02001075 int ret, i;
1076
Philipp Zabelb7287662013-06-21 10:27:39 +02001077 ipu->domain = irq_domain_add_linear(ipu->dev->of_node, IPU_NUM_IRQS,
Philipp Zabel379cdec2013-06-21 14:52:17 +02001078 &irq_generic_chip_ops, ipu);
Philipp Zabelb7287662013-06-21 10:27:39 +02001079 if (!ipu->domain) {
1080 dev_err(ipu->dev, "failed to add irq domain\n");
1081 return -ENODEV;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001082 }
1083
Philipp Zabel379cdec2013-06-21 14:52:17 +02001084 ret = irq_alloc_domain_generic_chips(ipu->domain, 32, 1, "IPU",
1085 handle_level_irq, 0, IRQF_VALID, 0);
1086 if (ret < 0) {
1087 dev_err(ipu->dev, "failed to alloc generic irq chips\n");
1088 irq_domain_remove(ipu->domain);
1089 return ret;
1090 }
1091
1092 for (i = 0; i < IPU_NUM_IRQS; i += 32) {
1093 gc = irq_get_domain_generic_chip(ipu->domain, i);
1094 gc->reg_base = ipu->cm_reg;
Philipp Zabel37f85b262013-06-21 14:52:18 +02001095 gc->unused = unused[i / 32];
Philipp Zabel379cdec2013-06-21 14:52:17 +02001096 ct = gc->chip_types;
1097 ct->chip.irq_ack = irq_gc_ack_set_bit;
1098 ct->chip.irq_mask = irq_gc_mask_clr_bit;
1099 ct->chip.irq_unmask = irq_gc_mask_set_bit;
1100 ct->regs.ack = IPU_INT_STAT(i / 32);
1101 ct->regs.mask = IPU_INT_CTRL(i / 32);
1102 }
1103
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001104 irq_set_chained_handler(ipu->irq_sync, ipu_irq_handler);
1105 irq_set_handler_data(ipu->irq_sync, ipu);
1106 irq_set_chained_handler(ipu->irq_err, ipu_err_irq_handler);
1107 irq_set_handler_data(ipu->irq_err, ipu);
1108
1109 return 0;
1110}
1111
1112static void ipu_irq_exit(struct ipu_soc *ipu)
1113{
Philipp Zabelb7287662013-06-21 10:27:39 +02001114 int i, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001115
1116 irq_set_chained_handler(ipu->irq_err, NULL);
1117 irq_set_handler_data(ipu->irq_err, NULL);
1118 irq_set_chained_handler(ipu->irq_sync, NULL);
1119 irq_set_handler_data(ipu->irq_sync, NULL);
1120
Philipp Zabel379cdec2013-06-21 14:52:17 +02001121 /* TODO: remove irq_domain_generic_chips */
1122
Philipp Zabelb7287662013-06-21 10:27:39 +02001123 for (i = 0; i < IPU_NUM_IRQS; i++) {
1124 irq = irq_linear_revmap(ipu->domain, i);
1125 if (irq)
1126 irq_dispose_mapping(irq);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001127 }
1128
Philipp Zabelb7287662013-06-21 10:27:39 +02001129 irq_domain_remove(ipu->domain);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001130}
1131
Bill Pembertonc4aabf82012-11-19 13:22:11 -05001132static int ipu_probe(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001133{
1134 const struct of_device_id *of_id =
1135 of_match_device(imx_ipu_dt_ids, &pdev->dev);
1136 struct ipu_soc *ipu;
1137 struct resource *res;
1138 unsigned long ipu_base;
1139 int i, ret, irq_sync, irq_err;
1140 const struct ipu_devtype *devtype;
1141
1142 devtype = of_id->data;
1143
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001144 irq_sync = platform_get_irq(pdev, 0);
1145 irq_err = platform_get_irq(pdev, 1);
1146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1147
Fabio Estevamfd563db2012-10-24 21:36:46 -02001148 dev_dbg(&pdev->dev, "irq_sync: %d irq_err: %d\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001149 irq_sync, irq_err);
1150
1151 if (!res || irq_sync < 0 || irq_err < 0)
1152 return -ENODEV;
1153
1154 ipu_base = res->start;
1155
1156 ipu = devm_kzalloc(&pdev->dev, sizeof(*ipu), GFP_KERNEL);
1157 if (!ipu)
1158 return -ENODEV;
1159
1160 for (i = 0; i < 64; i++)
1161 ipu->channel[i].ipu = ipu;
1162 ipu->devtype = devtype;
1163 ipu->ipu_type = devtype->type;
1164
1165 spin_lock_init(&ipu->lock);
1166 mutex_init(&ipu->channel_lock);
1167
Fabio Estevamfd563db2012-10-24 21:36:46 -02001168 dev_dbg(&pdev->dev, "cm_reg: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001169 ipu_base + devtype->cm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001170 dev_dbg(&pdev->dev, "idmac: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001171 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001172 dev_dbg(&pdev->dev, "cpmem: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001173 ipu_base + devtype->cpmem_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001174 dev_dbg(&pdev->dev, "disp0: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001175 ipu_base + devtype->disp0_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001176 dev_dbg(&pdev->dev, "disp1: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001177 ipu_base + devtype->disp1_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001178 dev_dbg(&pdev->dev, "srm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001179 ipu_base + devtype->srm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001180 dev_dbg(&pdev->dev, "tpm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001181 ipu_base + devtype->tpm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001182 dev_dbg(&pdev->dev, "dc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001183 ipu_base + devtype->cm_ofs + IPU_CM_DC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001184 dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001185 ipu_base + devtype->cm_ofs + IPU_CM_IC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001186 dev_dbg(&pdev->dev, "dmfc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001187 ipu_base + devtype->cm_ofs + IPU_CM_DMFC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001188 dev_dbg(&pdev->dev, "vdi: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001189 ipu_base + devtype->vdi_ofs);
1190
1191 ipu->cm_reg = devm_ioremap(&pdev->dev,
1192 ipu_base + devtype->cm_ofs, PAGE_SIZE);
1193 ipu->idmac_reg = devm_ioremap(&pdev->dev,
1194 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS,
1195 PAGE_SIZE);
1196 ipu->cpmem_base = devm_ioremap(&pdev->dev,
1197 ipu_base + devtype->cpmem_ofs, PAGE_SIZE);
1198
Fabio Estevambe798b22013-07-20 18:22:09 -03001199 if (!ipu->cm_reg || !ipu->idmac_reg || !ipu->cpmem_base)
1200 return -ENOMEM;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001201
1202 ipu->clk = devm_clk_get(&pdev->dev, "bus");
1203 if (IS_ERR(ipu->clk)) {
1204 ret = PTR_ERR(ipu->clk);
1205 dev_err(&pdev->dev, "clk_get failed with %d", ret);
Fabio Estevambe798b22013-07-20 18:22:09 -03001206 return ret;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001207 }
1208
1209 platform_set_drvdata(pdev, ipu);
1210
Fabio Estevam62645a22013-07-20 18:22:10 -03001211 ret = clk_prepare_enable(ipu->clk);
1212 if (ret) {
1213 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1214 return ret;
1215 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001216
1217 ipu->dev = &pdev->dev;
1218 ipu->irq_sync = irq_sync;
1219 ipu->irq_err = irq_err;
1220
1221 ret = ipu_irq_init(ipu);
1222 if (ret)
1223 goto out_failed_irq;
1224
Philipp Zabel6c641552013-03-28 17:35:21 +01001225 ret = device_reset(&pdev->dev);
1226 if (ret) {
1227 dev_err(&pdev->dev, "failed to reset: %d\n", ret);
1228 goto out_failed_reset;
1229 }
1230 ret = ipu_memory_reset(ipu);
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001231 if (ret)
1232 goto out_failed_reset;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001233
1234 /* Set MCU_T to divide MCU access window into 2 */
1235 ipu_cm_write(ipu, 0x00400000L | (IPU_MCU_T_DEFAULT << 18),
1236 IPU_DISP_GEN);
1237
1238 ret = ipu_submodules_init(ipu, pdev, ipu_base, ipu->clk);
1239 if (ret)
1240 goto failed_submodules_init;
1241
1242 ret = ipu_add_client_devices(ipu);
1243 if (ret) {
1244 dev_err(&pdev->dev, "adding client devices failed with %d\n",
1245 ret);
1246 goto failed_add_clients;
1247 }
1248
Fabio Estevam9c2c4382012-10-24 21:36:47 -02001249 dev_info(&pdev->dev, "%s probed\n", devtype->name);
1250
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001251 return 0;
1252
1253failed_add_clients:
1254 ipu_submodules_exit(ipu);
1255failed_submodules_init:
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001256out_failed_reset:
Philipp Zabel6c641552013-03-28 17:35:21 +01001257 ipu_irq_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001258out_failed_irq:
1259 clk_disable_unprepare(ipu->clk);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001260 return ret;
1261}
1262
Bill Pemberton8aa1be42012-11-19 13:26:38 -05001263static int ipu_remove(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001264{
1265 struct ipu_soc *ipu = platform_get_drvdata(pdev);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001266
1267 platform_device_unregister_children(pdev);
1268 ipu_submodules_exit(ipu);
1269 ipu_irq_exit(ipu);
1270
1271 clk_disable_unprepare(ipu->clk);
1272
1273 return 0;
1274}
1275
1276static struct platform_driver imx_ipu_driver = {
1277 .driver = {
1278 .name = "imx-ipuv3",
1279 .of_match_table = imx_ipu_dt_ids,
1280 },
1281 .probe = ipu_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -05001282 .remove = ipu_remove,
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001283};
1284
1285module_platform_driver(imx_ipu_driver);
1286
Fabio Estevam10f22682013-07-20 18:22:11 -03001287MODULE_ALIAS("platform:imx-ipuv3");
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001288MODULE_DESCRIPTION("i.MX IPU v3 driver");
1289MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1290MODULE_LICENSE("GPL");