blob: ba47b30d28fad4f3d187393a44a17bd9605f6ad4 [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
Philipp Zabel39b90042013-09-30 16:13:39 +020034#include <video/imx-ipu-v3.h>
Sascha Haueraecfbdb2012-09-21 10:07:49 +020035#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
Sascha Haueraecfbdb2012-09-21 10:07:49 +020047void ipu_srm_dp_sync_update(struct ipu_soc *ipu)
48{
49 u32 val;
50
51 val = ipu_cm_read(ipu, IPU_SRM_PRI2);
52 val |= 0x8;
53 ipu_cm_write(ipu, val, IPU_SRM_PRI2);
54}
55EXPORT_SYMBOL_GPL(ipu_srm_dp_sync_update);
56
Philipp Zabel7cb17792013-10-10 16:18:38 +020057enum ipu_color_space ipu_drm_fourcc_to_colorspace(u32 drm_fourcc)
58{
59 switch (drm_fourcc) {
Philipp Zabel0cb8b752014-12-12 13:40:14 +010060 case DRM_FORMAT_ARGB1555:
61 case DRM_FORMAT_ABGR1555:
62 case DRM_FORMAT_RGBA5551:
63 case DRM_FORMAT_BGRA5551:
Philipp Zabel7cb17792013-10-10 16:18:38 +020064 case DRM_FORMAT_RGB565:
65 case DRM_FORMAT_BGR565:
66 case DRM_FORMAT_RGB888:
67 case DRM_FORMAT_BGR888:
Lucas Stach7d2e8a22015-08-04 17:21:04 +020068 case DRM_FORMAT_ARGB4444:
Philipp Zabel7cb17792013-10-10 16:18:38 +020069 case DRM_FORMAT_XRGB8888:
70 case DRM_FORMAT_XBGR8888:
71 case DRM_FORMAT_RGBX8888:
72 case DRM_FORMAT_BGRX8888:
73 case DRM_FORMAT_ARGB8888:
74 case DRM_FORMAT_ABGR8888:
75 case DRM_FORMAT_RGBA8888:
76 case DRM_FORMAT_BGRA8888:
77 return IPUV3_COLORSPACE_RGB;
78 case DRM_FORMAT_YUYV:
79 case DRM_FORMAT_UYVY:
80 case DRM_FORMAT_YUV420:
81 case DRM_FORMAT_YVU420:
Steve Longerbeam9a34cef2014-06-25 18:05:53 -070082 case DRM_FORMAT_YUV422:
83 case DRM_FORMAT_YVU422:
84 case DRM_FORMAT_NV12:
85 case DRM_FORMAT_NV21:
86 case DRM_FORMAT_NV16:
87 case DRM_FORMAT_NV61:
Philipp Zabel7cb17792013-10-10 16:18:38 +020088 return IPUV3_COLORSPACE_YUV;
89 default:
90 return IPUV3_COLORSPACE_UNKNOWN;
91 }
92}
93EXPORT_SYMBOL_GPL(ipu_drm_fourcc_to_colorspace);
94
Sascha Haueraecfbdb2012-09-21 10:07:49 +020095enum ipu_color_space ipu_pixelformat_to_colorspace(u32 pixelformat)
96{
97 switch (pixelformat) {
98 case V4L2_PIX_FMT_YUV420:
Philipp Zabeld3e4e612012-11-12 16:29:00 +010099 case V4L2_PIX_FMT_YVU420:
Steve Longerbeam9a34cef2014-06-25 18:05:53 -0700100 case V4L2_PIX_FMT_YUV422P:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200101 case V4L2_PIX_FMT_UYVY:
Michael Olbrichc096ae12012-11-12 16:28:59 +0100102 case V4L2_PIX_FMT_YUYV:
Steve Longerbeam9a34cef2014-06-25 18:05:53 -0700103 case V4L2_PIX_FMT_NV12:
104 case V4L2_PIX_FMT_NV21:
105 case V4L2_PIX_FMT_NV16:
106 case V4L2_PIX_FMT_NV61:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200107 return IPUV3_COLORSPACE_YUV;
108 case V4L2_PIX_FMT_RGB32:
109 case V4L2_PIX_FMT_BGR32:
110 case V4L2_PIX_FMT_RGB24:
111 case V4L2_PIX_FMT_BGR24:
112 case V4L2_PIX_FMT_RGB565:
113 return IPUV3_COLORSPACE_RGB;
114 default:
115 return IPUV3_COLORSPACE_UNKNOWN;
116 }
117}
118EXPORT_SYMBOL_GPL(ipu_pixelformat_to_colorspace);
119
Steve Longerbeam4cea9402014-06-25 18:05:38 -0700120bool ipu_pixelformat_is_planar(u32 pixelformat)
121{
122 switch (pixelformat) {
123 case V4L2_PIX_FMT_YUV420:
124 case V4L2_PIX_FMT_YVU420:
Steve Longerbeam9a34cef2014-06-25 18:05:53 -0700125 case V4L2_PIX_FMT_YUV422P:
126 case V4L2_PIX_FMT_NV12:
127 case V4L2_PIX_FMT_NV21:
128 case V4L2_PIX_FMT_NV16:
129 case V4L2_PIX_FMT_NV61:
Steve Longerbeam4cea9402014-06-25 18:05:38 -0700130 return true;
131 }
132
133 return false;
134}
135EXPORT_SYMBOL_GPL(ipu_pixelformat_is_planar);
136
Steve Longerbeamae0e9702014-06-25 18:05:36 -0700137enum ipu_color_space ipu_mbus_code_to_colorspace(u32 mbus_code)
138{
139 switch (mbus_code & 0xf000) {
140 case 0x1000:
141 return IPUV3_COLORSPACE_RGB;
142 case 0x2000:
143 return IPUV3_COLORSPACE_YUV;
144 default:
145 return IPUV3_COLORSPACE_UNKNOWN;
146 }
147}
148EXPORT_SYMBOL_GPL(ipu_mbus_code_to_colorspace);
149
Steve Longerbeam6930afd2014-06-25 18:05:43 -0700150int ipu_stride_to_bytes(u32 pixel_stride, u32 pixelformat)
151{
152 switch (pixelformat) {
153 case V4L2_PIX_FMT_YUV420:
154 case V4L2_PIX_FMT_YVU420:
Steve Longerbeam9a34cef2014-06-25 18:05:53 -0700155 case V4L2_PIX_FMT_YUV422P:
156 case V4L2_PIX_FMT_NV12:
157 case V4L2_PIX_FMT_NV21:
158 case V4L2_PIX_FMT_NV16:
159 case V4L2_PIX_FMT_NV61:
Steve Longerbeam6930afd2014-06-25 18:05:43 -0700160 /*
161 * for the planar YUV formats, the stride passed to
162 * cpmem must be the stride in bytes of the Y plane.
163 * And all the planar YUV formats have an 8-bit
164 * Y component.
165 */
166 return (8 * pixel_stride) >> 3;
167 case V4L2_PIX_FMT_RGB565:
168 case V4L2_PIX_FMT_YUYV:
169 case V4L2_PIX_FMT_UYVY:
170 return (16 * pixel_stride) >> 3;
171 case V4L2_PIX_FMT_BGR24:
172 case V4L2_PIX_FMT_RGB24:
173 return (24 * pixel_stride) >> 3;
174 case V4L2_PIX_FMT_BGR32:
175 case V4L2_PIX_FMT_RGB32:
176 return (32 * pixel_stride) >> 3;
177 default:
178 break;
179 }
180
181 return -EINVAL;
182}
183EXPORT_SYMBOL_GPL(ipu_stride_to_bytes);
184
Steve Longerbeamf835f382014-06-25 18:05:37 -0700185int ipu_degrees_to_rot_mode(enum ipu_rotate_mode *mode, int degrees,
186 bool hflip, bool vflip)
187{
188 u32 r90, vf, hf;
189
190 switch (degrees) {
191 case 0:
192 vf = hf = r90 = 0;
193 break;
194 case 90:
195 vf = hf = 0;
196 r90 = 1;
197 break;
198 case 180:
199 vf = hf = 1;
200 r90 = 0;
201 break;
202 case 270:
203 vf = hf = r90 = 1;
204 break;
205 default:
206 return -EINVAL;
207 }
208
209 hf ^= (u32)hflip;
210 vf ^= (u32)vflip;
211
212 *mode = (enum ipu_rotate_mode)((r90 << 2) | (hf << 1) | vf);
213 return 0;
214}
215EXPORT_SYMBOL_GPL(ipu_degrees_to_rot_mode);
216
217int ipu_rot_mode_to_degrees(int *degrees, enum ipu_rotate_mode mode,
218 bool hflip, bool vflip)
219{
220 u32 r90, vf, hf;
221
222 r90 = ((u32)mode >> 2) & 0x1;
223 hf = ((u32)mode >> 1) & 0x1;
224 vf = ((u32)mode >> 0) & 0x1;
225 hf ^= (u32)hflip;
226 vf ^= (u32)vflip;
227
228 switch ((enum ipu_rotate_mode)((r90 << 2) | (hf << 1) | vf)) {
229 case IPU_ROTATE_NONE:
230 *degrees = 0;
231 break;
232 case IPU_ROTATE_90_RIGHT:
233 *degrees = 90;
234 break;
235 case IPU_ROTATE_180:
236 *degrees = 180;
237 break;
238 case IPU_ROTATE_90_LEFT:
239 *degrees = 270;
240 break;
241 default:
242 return -EINVAL;
243 }
244
245 return 0;
246}
247EXPORT_SYMBOL_GPL(ipu_rot_mode_to_degrees);
248
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200249struct ipuv3_channel *ipu_idmac_get(struct ipu_soc *ipu, unsigned num)
250{
251 struct ipuv3_channel *channel;
252
253 dev_dbg(ipu->dev, "%s %d\n", __func__, num);
254
255 if (num > 63)
256 return ERR_PTR(-ENODEV);
257
258 mutex_lock(&ipu->channel_lock);
259
260 channel = &ipu->channel[num];
261
262 if (channel->busy) {
263 channel = ERR_PTR(-EBUSY);
264 goto out;
265 }
266
Valentina Manea89bc5be2013-10-25 11:52:20 +0300267 channel->busy = true;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200268 channel->num = num;
269
270out:
271 mutex_unlock(&ipu->channel_lock);
272
273 return channel;
274}
275EXPORT_SYMBOL_GPL(ipu_idmac_get);
276
277void ipu_idmac_put(struct ipuv3_channel *channel)
278{
279 struct ipu_soc *ipu = channel->ipu;
280
281 dev_dbg(ipu->dev, "%s %d\n", __func__, channel->num);
282
283 mutex_lock(&ipu->channel_lock);
284
Valentina Manea89bc5be2013-10-25 11:52:20 +0300285 channel->busy = false;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200286
287 mutex_unlock(&ipu->channel_lock);
288}
289EXPORT_SYMBOL_GPL(ipu_idmac_put);
290
Steve Longerbeamaa52f572014-06-25 18:05:40 -0700291#define idma_mask(ch) (1 << ((ch) & 0x1f))
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200292
Steve Longerbeame7268c62014-06-25 18:05:42 -0700293/*
294 * This is an undocumented feature, a write one to a channel bit in
295 * IPU_CHA_CUR_BUF and IPU_CHA_TRIPLE_CUR_BUF will reset the channel's
296 * internal current buffer pointer so that transfers start from buffer
297 * 0 on the next channel enable (that's the theory anyway, the imx6 TRM
298 * only says these are read-only registers). This operation is required
299 * for channel linking to work correctly, for instance video capture
300 * pipelines that carry out image rotations will fail after the first
301 * streaming unless this function is called for each channel before
302 * re-enabling the channels.
303 */
304static void __ipu_idmac_reset_current_buffer(struct ipuv3_channel *channel)
305{
306 struct ipu_soc *ipu = channel->ipu;
307 unsigned int chno = channel->num;
308
309 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_CUR_BUF(chno));
310}
311
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200312void ipu_idmac_set_double_buffer(struct ipuv3_channel *channel,
313 bool doublebuffer)
314{
315 struct ipu_soc *ipu = channel->ipu;
316 unsigned long flags;
317 u32 reg;
318
319 spin_lock_irqsave(&ipu->lock, flags);
320
321 reg = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
322 if (doublebuffer)
323 reg |= idma_mask(channel->num);
324 else
325 reg &= ~idma_mask(channel->num);
326 ipu_cm_write(ipu, reg, IPU_CHA_DB_MODE_SEL(channel->num));
327
Steve Longerbeame7268c62014-06-25 18:05:42 -0700328 __ipu_idmac_reset_current_buffer(channel);
329
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200330 spin_unlock_irqrestore(&ipu->lock, flags);
331}
332EXPORT_SYMBOL_GPL(ipu_idmac_set_double_buffer);
333
Steve Longerbeam4fd1a072014-06-25 18:05:45 -0700334static const struct {
335 int chnum;
336 u32 reg;
337 int shift;
338} idmac_lock_en_info[] = {
339 { .chnum = 5, .reg = IDMAC_CH_LOCK_EN_1, .shift = 0, },
340 { .chnum = 11, .reg = IDMAC_CH_LOCK_EN_1, .shift = 2, },
341 { .chnum = 12, .reg = IDMAC_CH_LOCK_EN_1, .shift = 4, },
342 { .chnum = 14, .reg = IDMAC_CH_LOCK_EN_1, .shift = 6, },
343 { .chnum = 15, .reg = IDMAC_CH_LOCK_EN_1, .shift = 8, },
344 { .chnum = 20, .reg = IDMAC_CH_LOCK_EN_1, .shift = 10, },
345 { .chnum = 21, .reg = IDMAC_CH_LOCK_EN_1, .shift = 12, },
346 { .chnum = 22, .reg = IDMAC_CH_LOCK_EN_1, .shift = 14, },
347 { .chnum = 23, .reg = IDMAC_CH_LOCK_EN_1, .shift = 16, },
348 { .chnum = 27, .reg = IDMAC_CH_LOCK_EN_1, .shift = 18, },
349 { .chnum = 28, .reg = IDMAC_CH_LOCK_EN_1, .shift = 20, },
350 { .chnum = 45, .reg = IDMAC_CH_LOCK_EN_2, .shift = 0, },
351 { .chnum = 46, .reg = IDMAC_CH_LOCK_EN_2, .shift = 2, },
352 { .chnum = 47, .reg = IDMAC_CH_LOCK_EN_2, .shift = 4, },
353 { .chnum = 48, .reg = IDMAC_CH_LOCK_EN_2, .shift = 6, },
354 { .chnum = 49, .reg = IDMAC_CH_LOCK_EN_2, .shift = 8, },
355 { .chnum = 50, .reg = IDMAC_CH_LOCK_EN_2, .shift = 10, },
356};
357
358int ipu_idmac_lock_enable(struct ipuv3_channel *channel, int num_bursts)
359{
360 struct ipu_soc *ipu = channel->ipu;
361 unsigned long flags;
362 u32 bursts, regval;
363 int i;
364
365 switch (num_bursts) {
366 case 0:
367 case 1:
368 bursts = 0x00; /* locking disabled */
369 break;
370 case 2:
371 bursts = 0x01;
372 break;
373 case 4:
374 bursts = 0x02;
375 break;
376 case 8:
377 bursts = 0x03;
378 break;
379 default:
380 return -EINVAL;
381 }
382
383 for (i = 0; i < ARRAY_SIZE(idmac_lock_en_info); i++) {
384 if (channel->num == idmac_lock_en_info[i].chnum)
385 break;
386 }
387 if (i >= ARRAY_SIZE(idmac_lock_en_info))
388 return -EINVAL;
389
390 spin_lock_irqsave(&ipu->lock, flags);
391
392 regval = ipu_idmac_read(ipu, idmac_lock_en_info[i].reg);
393 regval &= ~(0x03 << idmac_lock_en_info[i].shift);
394 regval |= (bursts << idmac_lock_en_info[i].shift);
395 ipu_idmac_write(ipu, regval, idmac_lock_en_info[i].reg);
396
397 spin_unlock_irqrestore(&ipu->lock, flags);
398
399 return 0;
400}
401EXPORT_SYMBOL_GPL(ipu_idmac_lock_enable);
402
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200403int ipu_module_enable(struct ipu_soc *ipu, u32 mask)
404{
405 unsigned long lock_flags;
406 u32 val;
407
408 spin_lock_irqsave(&ipu->lock, lock_flags);
409
410 val = ipu_cm_read(ipu, IPU_DISP_GEN);
411
412 if (mask & IPU_CONF_DI0_EN)
413 val |= IPU_DI0_COUNTER_RELEASE;
414 if (mask & IPU_CONF_DI1_EN)
415 val |= IPU_DI1_COUNTER_RELEASE;
416
417 ipu_cm_write(ipu, val, IPU_DISP_GEN);
418
419 val = ipu_cm_read(ipu, IPU_CONF);
420 val |= mask;
421 ipu_cm_write(ipu, val, IPU_CONF);
422
423 spin_unlock_irqrestore(&ipu->lock, lock_flags);
424
425 return 0;
426}
427EXPORT_SYMBOL_GPL(ipu_module_enable);
428
429int ipu_module_disable(struct ipu_soc *ipu, u32 mask)
430{
431 unsigned long lock_flags;
432 u32 val;
433
434 spin_lock_irqsave(&ipu->lock, lock_flags);
435
436 val = ipu_cm_read(ipu, IPU_CONF);
437 val &= ~mask;
438 ipu_cm_write(ipu, val, IPU_CONF);
439
440 val = ipu_cm_read(ipu, IPU_DISP_GEN);
441
442 if (mask & IPU_CONF_DI0_EN)
443 val &= ~IPU_DI0_COUNTER_RELEASE;
444 if (mask & IPU_CONF_DI1_EN)
445 val &= ~IPU_DI1_COUNTER_RELEASE;
446
447 ipu_cm_write(ipu, val, IPU_DISP_GEN);
448
449 spin_unlock_irqrestore(&ipu->lock, lock_flags);
450
451 return 0;
452}
453EXPORT_SYMBOL_GPL(ipu_module_disable);
454
Philipp Zabele9046092012-05-16 17:28:29 +0200455int ipu_idmac_get_current_buffer(struct ipuv3_channel *channel)
456{
457 struct ipu_soc *ipu = channel->ipu;
458 unsigned int chno = channel->num;
459
460 return (ipu_cm_read(ipu, IPU_CHA_CUR_BUF(chno)) & idma_mask(chno)) ? 1 : 0;
461}
462EXPORT_SYMBOL_GPL(ipu_idmac_get_current_buffer);
463
Steve Longerbeamaa52f572014-06-25 18:05:40 -0700464bool ipu_idmac_buffer_is_ready(struct ipuv3_channel *channel, u32 buf_num)
465{
466 struct ipu_soc *ipu = channel->ipu;
467 unsigned long flags;
468 u32 reg = 0;
469
470 spin_lock_irqsave(&ipu->lock, flags);
471 switch (buf_num) {
472 case 0:
473 reg = ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num));
474 break;
475 case 1:
476 reg = ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num));
477 break;
478 case 2:
479 reg = ipu_cm_read(ipu, IPU_CHA_BUF2_RDY(channel->num));
480 break;
481 }
482 spin_unlock_irqrestore(&ipu->lock, flags);
483
484 return ((reg & idma_mask(channel->num)) != 0);
485}
486EXPORT_SYMBOL_GPL(ipu_idmac_buffer_is_ready);
487
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200488void ipu_idmac_select_buffer(struct ipuv3_channel *channel, u32 buf_num)
489{
490 struct ipu_soc *ipu = channel->ipu;
491 unsigned int chno = channel->num;
492 unsigned long flags;
493
494 spin_lock_irqsave(&ipu->lock, flags);
495
496 /* Mark buffer as ready. */
497 if (buf_num == 0)
498 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
499 else
500 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
501
502 spin_unlock_irqrestore(&ipu->lock, flags);
503}
504EXPORT_SYMBOL_GPL(ipu_idmac_select_buffer);
505
Steve Longerbeambce6f082014-06-25 18:05:41 -0700506void ipu_idmac_clear_buffer(struct ipuv3_channel *channel, u32 buf_num)
507{
508 struct ipu_soc *ipu = channel->ipu;
509 unsigned int chno = channel->num;
510 unsigned long flags;
511
512 spin_lock_irqsave(&ipu->lock, flags);
513
514 ipu_cm_write(ipu, 0xF0300000, IPU_GPR); /* write one to clear */
515 switch (buf_num) {
516 case 0:
517 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
518 break;
519 case 1:
520 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
521 break;
522 case 2:
523 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF2_RDY(chno));
524 break;
525 default:
526 break;
527 }
528 ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
529
530 spin_unlock_irqrestore(&ipu->lock, flags);
531}
532EXPORT_SYMBOL_GPL(ipu_idmac_clear_buffer);
533
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200534int ipu_idmac_enable_channel(struct ipuv3_channel *channel)
535{
536 struct ipu_soc *ipu = channel->ipu;
537 u32 val;
538 unsigned long flags;
539
540 spin_lock_irqsave(&ipu->lock, flags);
541
542 val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
543 val |= idma_mask(channel->num);
544 ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
545
546 spin_unlock_irqrestore(&ipu->lock, flags);
547
548 return 0;
549}
550EXPORT_SYMBOL_GPL(ipu_idmac_enable_channel);
551
Philipp Zabel17075502014-04-14 23:53:17 +0200552bool ipu_idmac_channel_busy(struct ipu_soc *ipu, unsigned int chno)
553{
554 return (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(chno)) & idma_mask(chno));
555}
556EXPORT_SYMBOL_GPL(ipu_idmac_channel_busy);
557
Sascha Hauerfb822a32013-10-10 16:18:41 +0200558int ipu_idmac_wait_busy(struct ipuv3_channel *channel, int ms)
559{
560 struct ipu_soc *ipu = channel->ipu;
561 unsigned long timeout;
562
563 timeout = jiffies + msecs_to_jiffies(ms);
564 while (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(channel->num)) &
565 idma_mask(channel->num)) {
566 if (time_after(jiffies, timeout))
567 return -ETIMEDOUT;
568 cpu_relax();
569 }
570
571 return 0;
572}
573EXPORT_SYMBOL_GPL(ipu_idmac_wait_busy);
574
Philipp Zabel17075502014-04-14 23:53:17 +0200575int ipu_wait_interrupt(struct ipu_soc *ipu, int irq, int ms)
576{
577 unsigned long timeout;
578
579 timeout = jiffies + msecs_to_jiffies(ms);
580 ipu_cm_write(ipu, BIT(irq % 32), IPU_INT_STAT(irq / 32));
581 while (!(ipu_cm_read(ipu, IPU_INT_STAT(irq / 32) & BIT(irq % 32)))) {
582 if (time_after(jiffies, timeout))
583 return -ETIMEDOUT;
584 cpu_relax();
585 }
586
587 return 0;
588}
589EXPORT_SYMBOL_GPL(ipu_wait_interrupt);
590
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200591int ipu_idmac_disable_channel(struct ipuv3_channel *channel)
592{
593 struct ipu_soc *ipu = channel->ipu;
594 u32 val;
595 unsigned long flags;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200596
597 spin_lock_irqsave(&ipu->lock, flags);
598
599 /* Disable DMA channel(s) */
600 val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
601 val &= ~idma_mask(channel->num);
602 ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
603
Steve Longerbeame7268c62014-06-25 18:05:42 -0700604 __ipu_idmac_reset_current_buffer(channel);
605
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200606 /* Set channel buffers NOT to be ready */
607 ipu_cm_write(ipu, 0xf0000000, IPU_GPR); /* write one to clear */
608
609 if (ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num)) &
610 idma_mask(channel->num)) {
611 ipu_cm_write(ipu, idma_mask(channel->num),
612 IPU_CHA_BUF0_RDY(channel->num));
613 }
614
615 if (ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num)) &
616 idma_mask(channel->num)) {
617 ipu_cm_write(ipu, idma_mask(channel->num),
618 IPU_CHA_BUF1_RDY(channel->num));
619 }
620
621 ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
622
623 /* Reset the double buffer */
624 val = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
625 val &= ~idma_mask(channel->num);
626 ipu_cm_write(ipu, val, IPU_CHA_DB_MODE_SEL(channel->num));
627
628 spin_unlock_irqrestore(&ipu->lock, flags);
629
630 return 0;
631}
632EXPORT_SYMBOL_GPL(ipu_idmac_disable_channel);
633
Steve Longerbeam2bcf5772014-06-25 18:05:44 -0700634/*
635 * The imx6 rev. D TRM says that enabling the WM feature will increase
636 * a channel's priority. Refer to Table 36-8 Calculated priority value.
637 * The sub-module that is the sink or source for the channel must enable
638 * watermark signal for this to take effect (SMFC_WM for instance).
639 */
640void ipu_idmac_enable_watermark(struct ipuv3_channel *channel, bool enable)
641{
642 struct ipu_soc *ipu = channel->ipu;
643 unsigned long flags;
644 u32 val;
645
646 spin_lock_irqsave(&ipu->lock, flags);
647
648 val = ipu_idmac_read(ipu, IDMAC_WM_EN(channel->num));
649 if (enable)
650 val |= 1 << (channel->num % 32);
651 else
652 val &= ~(1 << (channel->num % 32));
653 ipu_idmac_write(ipu, val, IDMAC_WM_EN(channel->num));
654
655 spin_unlock_irqrestore(&ipu->lock, flags);
656}
657EXPORT_SYMBOL_GPL(ipu_idmac_enable_watermark);
658
Philipp Zabel6c641552013-03-28 17:35:21 +0100659static int ipu_memory_reset(struct ipu_soc *ipu)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200660{
661 unsigned long timeout;
662
663 ipu_cm_write(ipu, 0x807FFFFF, IPU_MEM_RST);
664
665 timeout = jiffies + msecs_to_jiffies(1000);
666 while (ipu_cm_read(ipu, IPU_MEM_RST) & 0x80000000) {
667 if (time_after(jiffies, timeout))
668 return -ETIME;
669 cpu_relax();
670 }
671
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200672 return 0;
673}
674
Steve Longerbeamba079752014-06-25 18:05:30 -0700675/*
676 * Set the source mux for the given CSI. Selects either parallel or
677 * MIPI CSI2 sources.
678 */
679void ipu_set_csi_src_mux(struct ipu_soc *ipu, int csi_id, bool mipi_csi2)
680{
681 unsigned long flags;
682 u32 val, mask;
683
684 mask = (csi_id == 1) ? IPU_CONF_CSI1_DATA_SOURCE :
685 IPU_CONF_CSI0_DATA_SOURCE;
686
687 spin_lock_irqsave(&ipu->lock, flags);
688
689 val = ipu_cm_read(ipu, IPU_CONF);
690 if (mipi_csi2)
691 val |= mask;
692 else
693 val &= ~mask;
694 ipu_cm_write(ipu, val, IPU_CONF);
695
696 spin_unlock_irqrestore(&ipu->lock, flags);
697}
698EXPORT_SYMBOL_GPL(ipu_set_csi_src_mux);
699
700/*
701 * Set the source mux for the IC. Selects either CSI[01] or the VDI.
702 */
703void ipu_set_ic_src_mux(struct ipu_soc *ipu, int csi_id, bool vdi)
704{
705 unsigned long flags;
706 u32 val;
707
708 spin_lock_irqsave(&ipu->lock, flags);
709
710 val = ipu_cm_read(ipu, IPU_CONF);
711 if (vdi) {
712 val |= IPU_CONF_IC_INPUT;
713 } else {
714 val &= ~IPU_CONF_IC_INPUT;
715 if (csi_id == 1)
716 val |= IPU_CONF_CSI_SEL;
717 else
718 val &= ~IPU_CONF_CSI_SEL;
719 }
720 ipu_cm_write(ipu, val, IPU_CONF);
721
722 spin_unlock_irqrestore(&ipu->lock, flags);
723}
724EXPORT_SYMBOL_GPL(ipu_set_ic_src_mux);
725
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200726struct ipu_devtype {
727 const char *name;
728 unsigned long cm_ofs;
729 unsigned long cpmem_ofs;
730 unsigned long srm_ofs;
731 unsigned long tpm_ofs;
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700732 unsigned long csi0_ofs;
733 unsigned long csi1_ofs;
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200734 unsigned long ic_ofs;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200735 unsigned long disp0_ofs;
736 unsigned long disp1_ofs;
737 unsigned long dc_tmpl_ofs;
738 unsigned long vdi_ofs;
739 enum ipuv3_type type;
740};
741
742static struct ipu_devtype ipu_type_imx51 = {
743 .name = "IPUv3EX",
744 .cm_ofs = 0x1e000000,
745 .cpmem_ofs = 0x1f000000,
746 .srm_ofs = 0x1f040000,
747 .tpm_ofs = 0x1f060000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700748 .csi0_ofs = 0x1f030000,
749 .csi1_ofs = 0x1f038000,
Philipp Zabela49e7c02014-09-22 17:15:40 +0200750 .ic_ofs = 0x1e020000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200751 .disp0_ofs = 0x1e040000,
752 .disp1_ofs = 0x1e048000,
753 .dc_tmpl_ofs = 0x1f080000,
754 .vdi_ofs = 0x1e068000,
755 .type = IPUV3EX,
756};
757
758static struct ipu_devtype ipu_type_imx53 = {
759 .name = "IPUv3M",
760 .cm_ofs = 0x06000000,
761 .cpmem_ofs = 0x07000000,
762 .srm_ofs = 0x07040000,
763 .tpm_ofs = 0x07060000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700764 .csi0_ofs = 0x07030000,
765 .csi1_ofs = 0x07038000,
Philipp Zabela49e7c02014-09-22 17:15:40 +0200766 .ic_ofs = 0x06020000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200767 .disp0_ofs = 0x06040000,
768 .disp1_ofs = 0x06048000,
769 .dc_tmpl_ofs = 0x07080000,
770 .vdi_ofs = 0x06068000,
771 .type = IPUV3M,
772};
773
774static struct ipu_devtype ipu_type_imx6q = {
775 .name = "IPUv3H",
776 .cm_ofs = 0x00200000,
777 .cpmem_ofs = 0x00300000,
778 .srm_ofs = 0x00340000,
779 .tpm_ofs = 0x00360000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700780 .csi0_ofs = 0x00230000,
781 .csi1_ofs = 0x00238000,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200782 .ic_ofs = 0x00220000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200783 .disp0_ofs = 0x00240000,
784 .disp1_ofs = 0x00248000,
785 .dc_tmpl_ofs = 0x00380000,
786 .vdi_ofs = 0x00268000,
787 .type = IPUV3H,
788};
789
790static const struct of_device_id imx_ipu_dt_ids[] = {
791 { .compatible = "fsl,imx51-ipu", .data = &ipu_type_imx51, },
792 { .compatible = "fsl,imx53-ipu", .data = &ipu_type_imx53, },
793 { .compatible = "fsl,imx6q-ipu", .data = &ipu_type_imx6q, },
794 { /* sentinel */ }
795};
796MODULE_DEVICE_TABLE(of, imx_ipu_dt_ids);
797
798static int ipu_submodules_init(struct ipu_soc *ipu,
799 struct platform_device *pdev, unsigned long ipu_base,
800 struct clk *ipu_clk)
801{
802 char *unit;
803 int ret;
804 struct device *dev = &pdev->dev;
805 const struct ipu_devtype *devtype = ipu->devtype;
806
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700807 ret = ipu_cpmem_init(ipu, dev, ipu_base + devtype->cpmem_ofs);
808 if (ret) {
809 unit = "cpmem";
810 goto err_cpmem;
811 }
812
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700813 ret = ipu_csi_init(ipu, dev, 0, ipu_base + devtype->csi0_ofs,
814 IPU_CONF_CSI0_EN, ipu_clk);
815 if (ret) {
816 unit = "csi0";
817 goto err_csi_0;
818 }
819
820 ret = ipu_csi_init(ipu, dev, 1, ipu_base + devtype->csi1_ofs,
821 IPU_CONF_CSI1_EN, ipu_clk);
822 if (ret) {
823 unit = "csi1";
824 goto err_csi_1;
825 }
826
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200827 ret = ipu_ic_init(ipu, dev,
828 ipu_base + devtype->ic_ofs,
829 ipu_base + devtype->tpm_ofs);
830 if (ret) {
831 unit = "ic";
832 goto err_ic;
833 }
834
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200835 ret = ipu_di_init(ipu, dev, 0, ipu_base + devtype->disp0_ofs,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200836 IPU_CONF_DI0_EN, ipu_clk);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200837 if (ret) {
838 unit = "di0";
839 goto err_di_0;
840 }
841
842 ret = ipu_di_init(ipu, dev, 1, ipu_base + devtype->disp1_ofs,
843 IPU_CONF_DI1_EN, ipu_clk);
844 if (ret) {
845 unit = "di1";
846 goto err_di_1;
847 }
848
849 ret = ipu_dc_init(ipu, dev, ipu_base + devtype->cm_ofs +
850 IPU_CM_DC_REG_OFS, ipu_base + devtype->dc_tmpl_ofs);
851 if (ret) {
852 unit = "dc_template";
853 goto err_dc;
854 }
855
856 ret = ipu_dmfc_init(ipu, dev, ipu_base +
857 devtype->cm_ofs + IPU_CM_DMFC_REG_OFS, ipu_clk);
858 if (ret) {
859 unit = "dmfc";
860 goto err_dmfc;
861 }
862
863 ret = ipu_dp_init(ipu, dev, ipu_base + devtype->srm_ofs);
864 if (ret) {
865 unit = "dp";
866 goto err_dp;
867 }
868
Philipp Zabel35de9252012-05-09 16:59:01 +0200869 ret = ipu_smfc_init(ipu, dev, ipu_base +
870 devtype->cm_ofs + IPU_CM_SMFC_REG_OFS);
871 if (ret) {
872 unit = "smfc";
873 goto err_smfc;
874 }
875
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200876 return 0;
877
Philipp Zabel35de9252012-05-09 16:59:01 +0200878err_smfc:
879 ipu_dp_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200880err_dp:
881 ipu_dmfc_exit(ipu);
882err_dmfc:
883 ipu_dc_exit(ipu);
884err_dc:
885 ipu_di_exit(ipu, 1);
886err_di_1:
887 ipu_di_exit(ipu, 0);
888err_di_0:
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200889 ipu_ic_exit(ipu);
890err_ic:
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700891 ipu_csi_exit(ipu, 1);
892err_csi_1:
893 ipu_csi_exit(ipu, 0);
894err_csi_0:
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700895 ipu_cpmem_exit(ipu);
896err_cpmem:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200897 dev_err(&pdev->dev, "init %s failed with %d\n", unit, ret);
898 return ret;
899}
900
901static void ipu_irq_handle(struct ipu_soc *ipu, const int *regs, int num_regs)
902{
903 unsigned long status;
Philipp Zabelb7287662013-06-21 10:27:39 +0200904 int i, bit, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200905
906 for (i = 0; i < num_regs; i++) {
907
908 status = ipu_cm_read(ipu, IPU_INT_STAT(regs[i]));
909 status &= ipu_cm_read(ipu, IPU_INT_CTRL(regs[i]));
910
Philipp Zabelb7287662013-06-21 10:27:39 +0200911 for_each_set_bit(bit, &status, 32) {
Antoine Schweitzer-Chaput838201a2014-04-18 23:20:06 +0200912 irq = irq_linear_revmap(ipu->domain,
913 regs[i] * 32 + bit);
Philipp Zabelb7287662013-06-21 10:27:39 +0200914 if (irq)
915 generic_handle_irq(irq);
916 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200917 }
918}
919
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200920static void ipu_irq_handler(struct irq_desc *desc)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200921{
922 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
Jiang Liu4d9efdfc2015-07-13 20:39:54 +0000923 struct irq_chip *chip = irq_desc_get_chip(desc);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200924 const int int_reg[] = { 0, 1, 2, 3, 10, 11, 12, 13, 14};
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200925
926 chained_irq_enter(chip, desc);
927
928 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
929
930 chained_irq_exit(chip, desc);
931}
932
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200933static void ipu_err_irq_handler(struct irq_desc *desc)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200934{
935 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
Jiang Liu4d9efdfc2015-07-13 20:39:54 +0000936 struct irq_chip *chip = irq_desc_get_chip(desc);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200937 const int int_reg[] = { 4, 5, 8, 9};
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200938
939 chained_irq_enter(chip, desc);
940
941 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
942
943 chained_irq_exit(chip, desc);
944}
945
Philipp Zabel861a50c2014-04-14 23:53:16 +0200946int ipu_map_irq(struct ipu_soc *ipu, int irq)
947{
948 int virq;
949
950 virq = irq_linear_revmap(ipu->domain, irq);
951 if (!virq)
952 virq = irq_create_mapping(ipu->domain, irq);
953
954 return virq;
955}
956EXPORT_SYMBOL_GPL(ipu_map_irq);
957
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200958int ipu_idmac_channel_irq(struct ipu_soc *ipu, struct ipuv3_channel *channel,
959 enum ipu_channel_irq irq_type)
960{
Philipp Zabel861a50c2014-04-14 23:53:16 +0200961 return ipu_map_irq(ipu, irq_type + channel->num);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200962}
963EXPORT_SYMBOL_GPL(ipu_idmac_channel_irq);
964
965static void ipu_submodules_exit(struct ipu_soc *ipu)
966{
Philipp Zabel35de9252012-05-09 16:59:01 +0200967 ipu_smfc_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200968 ipu_dp_exit(ipu);
969 ipu_dmfc_exit(ipu);
970 ipu_dc_exit(ipu);
971 ipu_di_exit(ipu, 1);
972 ipu_di_exit(ipu, 0);
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200973 ipu_ic_exit(ipu);
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700974 ipu_csi_exit(ipu, 1);
975 ipu_csi_exit(ipu, 0);
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700976 ipu_cpmem_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200977}
978
979static int platform_remove_devices_fn(struct device *dev, void *unused)
980{
981 struct platform_device *pdev = to_platform_device(dev);
982
983 platform_device_unregister(pdev);
984
985 return 0;
986}
987
988static void platform_device_unregister_children(struct platform_device *pdev)
989{
990 device_for_each_child(&pdev->dev, NULL, platform_remove_devices_fn);
991}
992
993struct ipu_platform_reg {
994 struct ipu_client_platformdata pdata;
995 const char *name;
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200996 int reg_offset;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200997};
998
999static const struct ipu_platform_reg client_reg[] = {
1000 {
1001 .pdata = {
1002 .di = 0,
1003 .dc = 5,
1004 .dp = IPU_DP_FLOW_SYNC_BG,
1005 .dma[0] = IPUV3_CHANNEL_MEM_BG_SYNC,
Philipp Zabelb8d181e2013-10-10 16:18:45 +02001006 .dma[1] = IPUV3_CHANNEL_MEM_FG_SYNC,
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001007 },
1008 .name = "imx-ipuv3-crtc",
1009 }, {
1010 .pdata = {
1011 .di = 1,
1012 .dc = 1,
1013 .dp = -EINVAL,
1014 .dma[0] = IPUV3_CHANNEL_MEM_DC_SYNC,
1015 .dma[1] = -EINVAL,
1016 },
1017 .name = "imx-ipuv3-crtc",
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +02001018 }, {
1019 .pdata = {
1020 .csi = 0,
1021 .dma[0] = IPUV3_CHANNEL_CSI0,
1022 .dma[1] = -EINVAL,
1023 },
1024 .reg_offset = IPU_CM_CSI0_REG_OFS,
1025 .name = "imx-ipuv3-camera",
1026 }, {
1027 .pdata = {
1028 .csi = 1,
1029 .dma[0] = IPUV3_CHANNEL_CSI1,
1030 .dma[1] = -EINVAL,
1031 },
1032 .reg_offset = IPU_CM_CSI1_REG_OFS,
1033 .name = "imx-ipuv3-camera",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001034 },
1035};
1036
Russell King4ae078d2013-12-16 11:34:25 +00001037static DEFINE_MUTEX(ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001038static int ipu_client_id;
1039
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +02001040static int ipu_add_client_devices(struct ipu_soc *ipu, unsigned long ipu_base)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001041{
Russell King4ae078d2013-12-16 11:34:25 +00001042 struct device *dev = ipu->dev;
1043 unsigned i;
1044 int id, ret;
1045
1046 mutex_lock(&ipu_client_id_mutex);
1047 id = ipu_client_id;
1048 ipu_client_id += ARRAY_SIZE(client_reg);
1049 mutex_unlock(&ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001050
1051 for (i = 0; i < ARRAY_SIZE(client_reg); i++) {
1052 const struct ipu_platform_reg *reg = &client_reg[i];
Russell King4ae078d2013-12-16 11:34:25 +00001053 struct platform_device *pdev;
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +02001054 struct resource res;
Russell King4ae078d2013-12-16 11:34:25 +00001055
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +02001056 if (reg->reg_offset) {
1057 memset(&res, 0, sizeof(res));
1058 res.flags = IORESOURCE_MEM;
1059 res.start = ipu_base + ipu->devtype->cm_ofs + reg->reg_offset;
1060 res.end = res.start + PAGE_SIZE - 1;
1061 pdev = platform_device_register_resndata(dev, reg->name,
1062 id++, &res, 1, &reg->pdata, sizeof(reg->pdata));
1063 } else {
1064 pdev = platform_device_register_data(dev, reg->name,
1065 id++, &reg->pdata, sizeof(reg->pdata));
1066 }
Russell King4ae078d2013-12-16 11:34:25 +00001067
Axel Line4946cd2014-08-03 10:38:18 +08001068 if (IS_ERR(pdev)) {
1069 ret = PTR_ERR(pdev);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001070 goto err_register;
Axel Line4946cd2014-08-03 10:38:18 +08001071 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001072 }
1073
1074 return 0;
1075
1076err_register:
Russell King4ae078d2013-12-16 11:34:25 +00001077 platform_device_unregister_children(to_platform_device(dev));
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001078
1079 return ret;
1080}
1081
Philipp Zabelb7287662013-06-21 10:27:39 +02001082
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001083static int ipu_irq_init(struct ipu_soc *ipu)
1084{
Philipp Zabel379cdec2013-06-21 14:52:17 +02001085 struct irq_chip_generic *gc;
1086 struct irq_chip_type *ct;
Philipp Zabel37f85b262013-06-21 14:52:18 +02001087 unsigned long unused[IPU_NUM_IRQS / 32] = {
1088 0x400100d0, 0xffe000fd,
1089 0x400100d0, 0xffe000fd,
1090 0x400100d0, 0xffe000fd,
1091 0x4077ffff, 0xffe7e1fd,
1092 0x23fffffe, 0x8880fff0,
1093 0xf98fe7d0, 0xfff81fff,
1094 0x400100d0, 0xffe000fd,
1095 0x00000000,
1096 };
Philipp Zabel379cdec2013-06-21 14:52:17 +02001097 int ret, i;
1098
Philipp Zabelb7287662013-06-21 10:27:39 +02001099 ipu->domain = irq_domain_add_linear(ipu->dev->of_node, IPU_NUM_IRQS,
Philipp Zabel379cdec2013-06-21 14:52:17 +02001100 &irq_generic_chip_ops, ipu);
Philipp Zabelb7287662013-06-21 10:27:39 +02001101 if (!ipu->domain) {
1102 dev_err(ipu->dev, "failed to add irq domain\n");
1103 return -ENODEV;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001104 }
1105
Philipp Zabel379cdec2013-06-21 14:52:17 +02001106 ret = irq_alloc_domain_generic_chips(ipu->domain, 32, 1, "IPU",
Rob Herringca0141d2015-08-29 18:01:21 -05001107 handle_level_irq, 0, 0, 0);
Philipp Zabel379cdec2013-06-21 14:52:17 +02001108 if (ret < 0) {
1109 dev_err(ipu->dev, "failed to alloc generic irq chips\n");
1110 irq_domain_remove(ipu->domain);
1111 return ret;
1112 }
1113
Russell King510e6422015-06-16 23:29:41 +01001114 for (i = 0; i < IPU_NUM_IRQS; i += 32)
1115 ipu_cm_write(ipu, 0, IPU_INT_CTRL(i / 32));
1116
Philipp Zabel379cdec2013-06-21 14:52:17 +02001117 for (i = 0; i < IPU_NUM_IRQS; i += 32) {
1118 gc = irq_get_domain_generic_chip(ipu->domain, i);
1119 gc->reg_base = ipu->cm_reg;
Philipp Zabel37f85b262013-06-21 14:52:18 +02001120 gc->unused = unused[i / 32];
Philipp Zabel379cdec2013-06-21 14:52:17 +02001121 ct = gc->chip_types;
1122 ct->chip.irq_ack = irq_gc_ack_set_bit;
1123 ct->chip.irq_mask = irq_gc_mask_clr_bit;
1124 ct->chip.irq_unmask = irq_gc_mask_set_bit;
1125 ct->regs.ack = IPU_INT_STAT(i / 32);
1126 ct->regs.mask = IPU_INT_CTRL(i / 32);
1127 }
1128
Russell King86f5e732015-06-16 23:06:30 +01001129 irq_set_chained_handler_and_data(ipu->irq_sync, ipu_irq_handler, ipu);
1130 irq_set_chained_handler_and_data(ipu->irq_err, ipu_err_irq_handler,
1131 ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001132
1133 return 0;
1134}
1135
1136static void ipu_irq_exit(struct ipu_soc *ipu)
1137{
Philipp Zabelb7287662013-06-21 10:27:39 +02001138 int i, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001139
Russell King86f5e732015-06-16 23:06:30 +01001140 irq_set_chained_handler_and_data(ipu->irq_err, NULL, NULL);
1141 irq_set_chained_handler_and_data(ipu->irq_sync, NULL, NULL);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001142
Philipp Zabel379cdec2013-06-21 14:52:17 +02001143 /* TODO: remove irq_domain_generic_chips */
1144
Philipp Zabelb7287662013-06-21 10:27:39 +02001145 for (i = 0; i < IPU_NUM_IRQS; i++) {
1146 irq = irq_linear_revmap(ipu->domain, i);
1147 if (irq)
1148 irq_dispose_mapping(irq);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001149 }
1150
Philipp Zabelb7287662013-06-21 10:27:39 +02001151 irq_domain_remove(ipu->domain);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001152}
1153
Steve Longerbeam3feb0492014-06-25 18:05:55 -07001154void ipu_dump(struct ipu_soc *ipu)
1155{
1156 int i;
1157
1158 dev_dbg(ipu->dev, "IPU_CONF = \t0x%08X\n",
1159 ipu_cm_read(ipu, IPU_CONF));
1160 dev_dbg(ipu->dev, "IDMAC_CONF = \t0x%08X\n",
1161 ipu_idmac_read(ipu, IDMAC_CONF));
1162 dev_dbg(ipu->dev, "IDMAC_CHA_EN1 = \t0x%08X\n",
1163 ipu_idmac_read(ipu, IDMAC_CHA_EN(0)));
1164 dev_dbg(ipu->dev, "IDMAC_CHA_EN2 = \t0x%08X\n",
1165 ipu_idmac_read(ipu, IDMAC_CHA_EN(32)));
1166 dev_dbg(ipu->dev, "IDMAC_CHA_PRI1 = \t0x%08X\n",
1167 ipu_idmac_read(ipu, IDMAC_CHA_PRI(0)));
1168 dev_dbg(ipu->dev, "IDMAC_CHA_PRI2 = \t0x%08X\n",
1169 ipu_idmac_read(ipu, IDMAC_CHA_PRI(32)));
1170 dev_dbg(ipu->dev, "IDMAC_BAND_EN1 = \t0x%08X\n",
1171 ipu_idmac_read(ipu, IDMAC_BAND_EN(0)));
1172 dev_dbg(ipu->dev, "IDMAC_BAND_EN2 = \t0x%08X\n",
1173 ipu_idmac_read(ipu, IDMAC_BAND_EN(32)));
1174 dev_dbg(ipu->dev, "IPU_CHA_DB_MODE_SEL0 = \t0x%08X\n",
1175 ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(0)));
1176 dev_dbg(ipu->dev, "IPU_CHA_DB_MODE_SEL1 = \t0x%08X\n",
1177 ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(32)));
1178 dev_dbg(ipu->dev, "IPU_FS_PROC_FLOW1 = \t0x%08X\n",
1179 ipu_cm_read(ipu, IPU_FS_PROC_FLOW1));
1180 dev_dbg(ipu->dev, "IPU_FS_PROC_FLOW2 = \t0x%08X\n",
1181 ipu_cm_read(ipu, IPU_FS_PROC_FLOW2));
1182 dev_dbg(ipu->dev, "IPU_FS_PROC_FLOW3 = \t0x%08X\n",
1183 ipu_cm_read(ipu, IPU_FS_PROC_FLOW3));
1184 dev_dbg(ipu->dev, "IPU_FS_DISP_FLOW1 = \t0x%08X\n",
1185 ipu_cm_read(ipu, IPU_FS_DISP_FLOW1));
1186 for (i = 0; i < 15; i++)
1187 dev_dbg(ipu->dev, "IPU_INT_CTRL(%d) = \t%08X\n", i,
1188 ipu_cm_read(ipu, IPU_INT_CTRL(i)));
1189}
1190EXPORT_SYMBOL_GPL(ipu_dump);
1191
Bill Pembertonc4aabf82012-11-19 13:22:11 -05001192static int ipu_probe(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001193{
1194 const struct of_device_id *of_id =
1195 of_match_device(imx_ipu_dt_ids, &pdev->dev);
1196 struct ipu_soc *ipu;
1197 struct resource *res;
1198 unsigned long ipu_base;
1199 int i, ret, irq_sync, irq_err;
1200 const struct ipu_devtype *devtype;
1201
1202 devtype = of_id->data;
1203
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001204 irq_sync = platform_get_irq(pdev, 0);
1205 irq_err = platform_get_irq(pdev, 1);
1206 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1207
Fabio Estevamfd563db2012-10-24 21:36:46 -02001208 dev_dbg(&pdev->dev, "irq_sync: %d irq_err: %d\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001209 irq_sync, irq_err);
1210
1211 if (!res || irq_sync < 0 || irq_err < 0)
1212 return -ENODEV;
1213
1214 ipu_base = res->start;
1215
1216 ipu = devm_kzalloc(&pdev->dev, sizeof(*ipu), GFP_KERNEL);
1217 if (!ipu)
1218 return -ENODEV;
1219
1220 for (i = 0; i < 64; i++)
1221 ipu->channel[i].ipu = ipu;
1222 ipu->devtype = devtype;
1223 ipu->ipu_type = devtype->type;
1224
1225 spin_lock_init(&ipu->lock);
1226 mutex_init(&ipu->channel_lock);
1227
Fabio Estevamfd563db2012-10-24 21:36:46 -02001228 dev_dbg(&pdev->dev, "cm_reg: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001229 ipu_base + devtype->cm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001230 dev_dbg(&pdev->dev, "idmac: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001231 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001232 dev_dbg(&pdev->dev, "cpmem: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001233 ipu_base + devtype->cpmem_ofs);
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -07001234 dev_dbg(&pdev->dev, "csi0: 0x%08lx\n",
1235 ipu_base + devtype->csi0_ofs);
1236 dev_dbg(&pdev->dev, "csi1: 0x%08lx\n",
1237 ipu_base + devtype->csi1_ofs);
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +02001238 dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
1239 ipu_base + devtype->ic_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001240 dev_dbg(&pdev->dev, "disp0: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001241 ipu_base + devtype->disp0_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001242 dev_dbg(&pdev->dev, "disp1: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001243 ipu_base + devtype->disp1_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001244 dev_dbg(&pdev->dev, "srm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001245 ipu_base + devtype->srm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001246 dev_dbg(&pdev->dev, "tpm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001247 ipu_base + devtype->tpm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001248 dev_dbg(&pdev->dev, "dc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001249 ipu_base + devtype->cm_ofs + IPU_CM_DC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001250 dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001251 ipu_base + devtype->cm_ofs + IPU_CM_IC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001252 dev_dbg(&pdev->dev, "dmfc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001253 ipu_base + devtype->cm_ofs + IPU_CM_DMFC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001254 dev_dbg(&pdev->dev, "vdi: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001255 ipu_base + devtype->vdi_ofs);
1256
1257 ipu->cm_reg = devm_ioremap(&pdev->dev,
1258 ipu_base + devtype->cm_ofs, PAGE_SIZE);
1259 ipu->idmac_reg = devm_ioremap(&pdev->dev,
1260 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS,
1261 PAGE_SIZE);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001262
Steve Longerbeam7d2691d2014-06-25 18:05:47 -07001263 if (!ipu->cm_reg || !ipu->idmac_reg)
Fabio Estevambe798b22013-07-20 18:22:09 -03001264 return -ENOMEM;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001265
1266 ipu->clk = devm_clk_get(&pdev->dev, "bus");
1267 if (IS_ERR(ipu->clk)) {
1268 ret = PTR_ERR(ipu->clk);
1269 dev_err(&pdev->dev, "clk_get failed with %d", ret);
Fabio Estevambe798b22013-07-20 18:22:09 -03001270 return ret;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001271 }
1272
1273 platform_set_drvdata(pdev, ipu);
1274
Fabio Estevam62645a22013-07-20 18:22:10 -03001275 ret = clk_prepare_enable(ipu->clk);
1276 if (ret) {
1277 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1278 return ret;
1279 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001280
1281 ipu->dev = &pdev->dev;
1282 ipu->irq_sync = irq_sync;
1283 ipu->irq_err = irq_err;
1284
1285 ret = ipu_irq_init(ipu);
1286 if (ret)
1287 goto out_failed_irq;
1288
Philipp Zabel6c641552013-03-28 17:35:21 +01001289 ret = device_reset(&pdev->dev);
1290 if (ret) {
1291 dev_err(&pdev->dev, "failed to reset: %d\n", ret);
1292 goto out_failed_reset;
1293 }
1294 ret = ipu_memory_reset(ipu);
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001295 if (ret)
1296 goto out_failed_reset;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001297
1298 /* Set MCU_T to divide MCU access window into 2 */
1299 ipu_cm_write(ipu, 0x00400000L | (IPU_MCU_T_DEFAULT << 18),
1300 IPU_DISP_GEN);
1301
1302 ret = ipu_submodules_init(ipu, pdev, ipu_base, ipu->clk);
1303 if (ret)
1304 goto failed_submodules_init;
1305
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +02001306 ret = ipu_add_client_devices(ipu, ipu_base);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001307 if (ret) {
1308 dev_err(&pdev->dev, "adding client devices failed with %d\n",
1309 ret);
1310 goto failed_add_clients;
1311 }
1312
Fabio Estevam9c2c4382012-10-24 21:36:47 -02001313 dev_info(&pdev->dev, "%s probed\n", devtype->name);
1314
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001315 return 0;
1316
1317failed_add_clients:
1318 ipu_submodules_exit(ipu);
1319failed_submodules_init:
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001320out_failed_reset:
Philipp Zabel6c641552013-03-28 17:35:21 +01001321 ipu_irq_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001322out_failed_irq:
1323 clk_disable_unprepare(ipu->clk);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001324 return ret;
1325}
1326
Bill Pemberton8aa1be42012-11-19 13:26:38 -05001327static int ipu_remove(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001328{
1329 struct ipu_soc *ipu = platform_get_drvdata(pdev);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001330
1331 platform_device_unregister_children(pdev);
1332 ipu_submodules_exit(ipu);
1333 ipu_irq_exit(ipu);
1334
1335 clk_disable_unprepare(ipu->clk);
1336
1337 return 0;
1338}
1339
1340static struct platform_driver imx_ipu_driver = {
1341 .driver = {
1342 .name = "imx-ipuv3",
1343 .of_match_table = imx_ipu_dt_ids,
1344 },
1345 .probe = ipu_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -05001346 .remove = ipu_remove,
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001347};
1348
1349module_platform_driver(imx_ipu_driver);
1350
Fabio Estevam10f22682013-07-20 18:22:11 -03001351MODULE_ALIAS("platform:imx-ipuv3");
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001352MODULE_DESCRIPTION("i.MX IPU v3 driver");
1353MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1354MODULE_LICENSE("GPL");