blob: e542d5e8a9e17eb4d85bb1360d03f54359f70ce2 [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) {
60 case DRM_FORMAT_RGB565:
61 case DRM_FORMAT_BGR565:
62 case DRM_FORMAT_RGB888:
63 case DRM_FORMAT_BGR888:
64 case DRM_FORMAT_XRGB8888:
65 case DRM_FORMAT_XBGR8888:
66 case DRM_FORMAT_RGBX8888:
67 case DRM_FORMAT_BGRX8888:
68 case DRM_FORMAT_ARGB8888:
69 case DRM_FORMAT_ABGR8888:
70 case DRM_FORMAT_RGBA8888:
71 case DRM_FORMAT_BGRA8888:
72 return IPUV3_COLORSPACE_RGB;
73 case DRM_FORMAT_YUYV:
74 case DRM_FORMAT_UYVY:
75 case DRM_FORMAT_YUV420:
76 case DRM_FORMAT_YVU420:
77 return IPUV3_COLORSPACE_YUV;
78 default:
79 return IPUV3_COLORSPACE_UNKNOWN;
80 }
81}
82EXPORT_SYMBOL_GPL(ipu_drm_fourcc_to_colorspace);
83
Sascha Haueraecfbdb2012-09-21 10:07:49 +020084enum ipu_color_space ipu_pixelformat_to_colorspace(u32 pixelformat)
85{
86 switch (pixelformat) {
87 case V4L2_PIX_FMT_YUV420:
Philipp Zabeld3e4e612012-11-12 16:29:00 +010088 case V4L2_PIX_FMT_YVU420:
Sascha Haueraecfbdb2012-09-21 10:07:49 +020089 case V4L2_PIX_FMT_UYVY:
Michael Olbrichc096ae12012-11-12 16:28:59 +010090 case V4L2_PIX_FMT_YUYV:
Sascha Haueraecfbdb2012-09-21 10:07:49 +020091 return IPUV3_COLORSPACE_YUV;
92 case V4L2_PIX_FMT_RGB32:
93 case V4L2_PIX_FMT_BGR32:
94 case V4L2_PIX_FMT_RGB24:
95 case V4L2_PIX_FMT_BGR24:
96 case V4L2_PIX_FMT_RGB565:
97 return IPUV3_COLORSPACE_RGB;
98 default:
99 return IPUV3_COLORSPACE_UNKNOWN;
100 }
101}
102EXPORT_SYMBOL_GPL(ipu_pixelformat_to_colorspace);
103
Steve Longerbeam4cea9402014-06-25 18:05:38 -0700104bool ipu_pixelformat_is_planar(u32 pixelformat)
105{
106 switch (pixelformat) {
107 case V4L2_PIX_FMT_YUV420:
108 case V4L2_PIX_FMT_YVU420:
109 return true;
110 }
111
112 return false;
113}
114EXPORT_SYMBOL_GPL(ipu_pixelformat_is_planar);
115
Steve Longerbeamae0e9702014-06-25 18:05:36 -0700116enum ipu_color_space ipu_mbus_code_to_colorspace(u32 mbus_code)
117{
118 switch (mbus_code & 0xf000) {
119 case 0x1000:
120 return IPUV3_COLORSPACE_RGB;
121 case 0x2000:
122 return IPUV3_COLORSPACE_YUV;
123 default:
124 return IPUV3_COLORSPACE_UNKNOWN;
125 }
126}
127EXPORT_SYMBOL_GPL(ipu_mbus_code_to_colorspace);
128
Steve Longerbeam6930afd2014-06-25 18:05:43 -0700129int ipu_stride_to_bytes(u32 pixel_stride, u32 pixelformat)
130{
131 switch (pixelformat) {
132 case V4L2_PIX_FMT_YUV420:
133 case V4L2_PIX_FMT_YVU420:
134 /*
135 * for the planar YUV formats, the stride passed to
136 * cpmem must be the stride in bytes of the Y plane.
137 * And all the planar YUV formats have an 8-bit
138 * Y component.
139 */
140 return (8 * pixel_stride) >> 3;
141 case V4L2_PIX_FMT_RGB565:
142 case V4L2_PIX_FMT_YUYV:
143 case V4L2_PIX_FMT_UYVY:
144 return (16 * pixel_stride) >> 3;
145 case V4L2_PIX_FMT_BGR24:
146 case V4L2_PIX_FMT_RGB24:
147 return (24 * pixel_stride) >> 3;
148 case V4L2_PIX_FMT_BGR32:
149 case V4L2_PIX_FMT_RGB32:
150 return (32 * pixel_stride) >> 3;
151 default:
152 break;
153 }
154
155 return -EINVAL;
156}
157EXPORT_SYMBOL_GPL(ipu_stride_to_bytes);
158
Steve Longerbeamf835f382014-06-25 18:05:37 -0700159int ipu_degrees_to_rot_mode(enum ipu_rotate_mode *mode, int degrees,
160 bool hflip, bool vflip)
161{
162 u32 r90, vf, hf;
163
164 switch (degrees) {
165 case 0:
166 vf = hf = r90 = 0;
167 break;
168 case 90:
169 vf = hf = 0;
170 r90 = 1;
171 break;
172 case 180:
173 vf = hf = 1;
174 r90 = 0;
175 break;
176 case 270:
177 vf = hf = r90 = 1;
178 break;
179 default:
180 return -EINVAL;
181 }
182
183 hf ^= (u32)hflip;
184 vf ^= (u32)vflip;
185
186 *mode = (enum ipu_rotate_mode)((r90 << 2) | (hf << 1) | vf);
187 return 0;
188}
189EXPORT_SYMBOL_GPL(ipu_degrees_to_rot_mode);
190
191int ipu_rot_mode_to_degrees(int *degrees, enum ipu_rotate_mode mode,
192 bool hflip, bool vflip)
193{
194 u32 r90, vf, hf;
195
196 r90 = ((u32)mode >> 2) & 0x1;
197 hf = ((u32)mode >> 1) & 0x1;
198 vf = ((u32)mode >> 0) & 0x1;
199 hf ^= (u32)hflip;
200 vf ^= (u32)vflip;
201
202 switch ((enum ipu_rotate_mode)((r90 << 2) | (hf << 1) | vf)) {
203 case IPU_ROTATE_NONE:
204 *degrees = 0;
205 break;
206 case IPU_ROTATE_90_RIGHT:
207 *degrees = 90;
208 break;
209 case IPU_ROTATE_180:
210 *degrees = 180;
211 break;
212 case IPU_ROTATE_90_LEFT:
213 *degrees = 270;
214 break;
215 default:
216 return -EINVAL;
217 }
218
219 return 0;
220}
221EXPORT_SYMBOL_GPL(ipu_rot_mode_to_degrees);
222
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200223struct ipuv3_channel *ipu_idmac_get(struct ipu_soc *ipu, unsigned num)
224{
225 struct ipuv3_channel *channel;
226
227 dev_dbg(ipu->dev, "%s %d\n", __func__, num);
228
229 if (num > 63)
230 return ERR_PTR(-ENODEV);
231
232 mutex_lock(&ipu->channel_lock);
233
234 channel = &ipu->channel[num];
235
236 if (channel->busy) {
237 channel = ERR_PTR(-EBUSY);
238 goto out;
239 }
240
Valentina Manea89bc5be2013-10-25 11:52:20 +0300241 channel->busy = true;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200242 channel->num = num;
243
244out:
245 mutex_unlock(&ipu->channel_lock);
246
247 return channel;
248}
249EXPORT_SYMBOL_GPL(ipu_idmac_get);
250
251void ipu_idmac_put(struct ipuv3_channel *channel)
252{
253 struct ipu_soc *ipu = channel->ipu;
254
255 dev_dbg(ipu->dev, "%s %d\n", __func__, channel->num);
256
257 mutex_lock(&ipu->channel_lock);
258
Valentina Manea89bc5be2013-10-25 11:52:20 +0300259 channel->busy = false;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200260
261 mutex_unlock(&ipu->channel_lock);
262}
263EXPORT_SYMBOL_GPL(ipu_idmac_put);
264
Steve Longerbeamaa52f572014-06-25 18:05:40 -0700265#define idma_mask(ch) (1 << ((ch) & 0x1f))
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200266
Steve Longerbeame7268c62014-06-25 18:05:42 -0700267/*
268 * This is an undocumented feature, a write one to a channel bit in
269 * IPU_CHA_CUR_BUF and IPU_CHA_TRIPLE_CUR_BUF will reset the channel's
270 * internal current buffer pointer so that transfers start from buffer
271 * 0 on the next channel enable (that's the theory anyway, the imx6 TRM
272 * only says these are read-only registers). This operation is required
273 * for channel linking to work correctly, for instance video capture
274 * pipelines that carry out image rotations will fail after the first
275 * streaming unless this function is called for each channel before
276 * re-enabling the channels.
277 */
278static void __ipu_idmac_reset_current_buffer(struct ipuv3_channel *channel)
279{
280 struct ipu_soc *ipu = channel->ipu;
281 unsigned int chno = channel->num;
282
283 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_CUR_BUF(chno));
284}
285
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200286void ipu_idmac_set_double_buffer(struct ipuv3_channel *channel,
287 bool doublebuffer)
288{
289 struct ipu_soc *ipu = channel->ipu;
290 unsigned long flags;
291 u32 reg;
292
293 spin_lock_irqsave(&ipu->lock, flags);
294
295 reg = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
296 if (doublebuffer)
297 reg |= idma_mask(channel->num);
298 else
299 reg &= ~idma_mask(channel->num);
300 ipu_cm_write(ipu, reg, IPU_CHA_DB_MODE_SEL(channel->num));
301
Steve Longerbeame7268c62014-06-25 18:05:42 -0700302 __ipu_idmac_reset_current_buffer(channel);
303
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200304 spin_unlock_irqrestore(&ipu->lock, flags);
305}
306EXPORT_SYMBOL_GPL(ipu_idmac_set_double_buffer);
307
308int ipu_module_enable(struct ipu_soc *ipu, u32 mask)
309{
310 unsigned long lock_flags;
311 u32 val;
312
313 spin_lock_irqsave(&ipu->lock, lock_flags);
314
315 val = ipu_cm_read(ipu, IPU_DISP_GEN);
316
317 if (mask & IPU_CONF_DI0_EN)
318 val |= IPU_DI0_COUNTER_RELEASE;
319 if (mask & IPU_CONF_DI1_EN)
320 val |= IPU_DI1_COUNTER_RELEASE;
321
322 ipu_cm_write(ipu, val, IPU_DISP_GEN);
323
324 val = ipu_cm_read(ipu, IPU_CONF);
325 val |= mask;
326 ipu_cm_write(ipu, val, IPU_CONF);
327
328 spin_unlock_irqrestore(&ipu->lock, lock_flags);
329
330 return 0;
331}
332EXPORT_SYMBOL_GPL(ipu_module_enable);
333
334int ipu_module_disable(struct ipu_soc *ipu, u32 mask)
335{
336 unsigned long lock_flags;
337 u32 val;
338
339 spin_lock_irqsave(&ipu->lock, lock_flags);
340
341 val = ipu_cm_read(ipu, IPU_CONF);
342 val &= ~mask;
343 ipu_cm_write(ipu, val, IPU_CONF);
344
345 val = ipu_cm_read(ipu, IPU_DISP_GEN);
346
347 if (mask & IPU_CONF_DI0_EN)
348 val &= ~IPU_DI0_COUNTER_RELEASE;
349 if (mask & IPU_CONF_DI1_EN)
350 val &= ~IPU_DI1_COUNTER_RELEASE;
351
352 ipu_cm_write(ipu, val, IPU_DISP_GEN);
353
354 spin_unlock_irqrestore(&ipu->lock, lock_flags);
355
356 return 0;
357}
358EXPORT_SYMBOL_GPL(ipu_module_disable);
359
Philipp Zabele9046092012-05-16 17:28:29 +0200360int ipu_idmac_get_current_buffer(struct ipuv3_channel *channel)
361{
362 struct ipu_soc *ipu = channel->ipu;
363 unsigned int chno = channel->num;
364
365 return (ipu_cm_read(ipu, IPU_CHA_CUR_BUF(chno)) & idma_mask(chno)) ? 1 : 0;
366}
367EXPORT_SYMBOL_GPL(ipu_idmac_get_current_buffer);
368
Steve Longerbeamaa52f572014-06-25 18:05:40 -0700369bool ipu_idmac_buffer_is_ready(struct ipuv3_channel *channel, u32 buf_num)
370{
371 struct ipu_soc *ipu = channel->ipu;
372 unsigned long flags;
373 u32 reg = 0;
374
375 spin_lock_irqsave(&ipu->lock, flags);
376 switch (buf_num) {
377 case 0:
378 reg = ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num));
379 break;
380 case 1:
381 reg = ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num));
382 break;
383 case 2:
384 reg = ipu_cm_read(ipu, IPU_CHA_BUF2_RDY(channel->num));
385 break;
386 }
387 spin_unlock_irqrestore(&ipu->lock, flags);
388
389 return ((reg & idma_mask(channel->num)) != 0);
390}
391EXPORT_SYMBOL_GPL(ipu_idmac_buffer_is_ready);
392
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200393void ipu_idmac_select_buffer(struct ipuv3_channel *channel, u32 buf_num)
394{
395 struct ipu_soc *ipu = channel->ipu;
396 unsigned int chno = channel->num;
397 unsigned long flags;
398
399 spin_lock_irqsave(&ipu->lock, flags);
400
401 /* Mark buffer as ready. */
402 if (buf_num == 0)
403 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
404 else
405 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
406
407 spin_unlock_irqrestore(&ipu->lock, flags);
408}
409EXPORT_SYMBOL_GPL(ipu_idmac_select_buffer);
410
Steve Longerbeambce6f082014-06-25 18:05:41 -0700411void ipu_idmac_clear_buffer(struct ipuv3_channel *channel, u32 buf_num)
412{
413 struct ipu_soc *ipu = channel->ipu;
414 unsigned int chno = channel->num;
415 unsigned long flags;
416
417 spin_lock_irqsave(&ipu->lock, flags);
418
419 ipu_cm_write(ipu, 0xF0300000, IPU_GPR); /* write one to clear */
420 switch (buf_num) {
421 case 0:
422 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
423 break;
424 case 1:
425 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
426 break;
427 case 2:
428 ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF2_RDY(chno));
429 break;
430 default:
431 break;
432 }
433 ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
434
435 spin_unlock_irqrestore(&ipu->lock, flags);
436}
437EXPORT_SYMBOL_GPL(ipu_idmac_clear_buffer);
438
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200439int ipu_idmac_enable_channel(struct ipuv3_channel *channel)
440{
441 struct ipu_soc *ipu = channel->ipu;
442 u32 val;
443 unsigned long flags;
444
445 spin_lock_irqsave(&ipu->lock, flags);
446
447 val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
448 val |= idma_mask(channel->num);
449 ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
450
451 spin_unlock_irqrestore(&ipu->lock, flags);
452
453 return 0;
454}
455EXPORT_SYMBOL_GPL(ipu_idmac_enable_channel);
456
Philipp Zabel17075502014-04-14 23:53:17 +0200457bool ipu_idmac_channel_busy(struct ipu_soc *ipu, unsigned int chno)
458{
459 return (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(chno)) & idma_mask(chno));
460}
461EXPORT_SYMBOL_GPL(ipu_idmac_channel_busy);
462
Sascha Hauerfb822a32013-10-10 16:18:41 +0200463int ipu_idmac_wait_busy(struct ipuv3_channel *channel, int ms)
464{
465 struct ipu_soc *ipu = channel->ipu;
466 unsigned long timeout;
467
468 timeout = jiffies + msecs_to_jiffies(ms);
469 while (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(channel->num)) &
470 idma_mask(channel->num)) {
471 if (time_after(jiffies, timeout))
472 return -ETIMEDOUT;
473 cpu_relax();
474 }
475
476 return 0;
477}
478EXPORT_SYMBOL_GPL(ipu_idmac_wait_busy);
479
Philipp Zabel17075502014-04-14 23:53:17 +0200480int ipu_wait_interrupt(struct ipu_soc *ipu, int irq, int ms)
481{
482 unsigned long timeout;
483
484 timeout = jiffies + msecs_to_jiffies(ms);
485 ipu_cm_write(ipu, BIT(irq % 32), IPU_INT_STAT(irq / 32));
486 while (!(ipu_cm_read(ipu, IPU_INT_STAT(irq / 32) & BIT(irq % 32)))) {
487 if (time_after(jiffies, timeout))
488 return -ETIMEDOUT;
489 cpu_relax();
490 }
491
492 return 0;
493}
494EXPORT_SYMBOL_GPL(ipu_wait_interrupt);
495
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200496int ipu_idmac_disable_channel(struct ipuv3_channel *channel)
497{
498 struct ipu_soc *ipu = channel->ipu;
499 u32 val;
500 unsigned long flags;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200501
502 spin_lock_irqsave(&ipu->lock, flags);
503
504 /* Disable DMA channel(s) */
505 val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
506 val &= ~idma_mask(channel->num);
507 ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
508
Steve Longerbeame7268c62014-06-25 18:05:42 -0700509 __ipu_idmac_reset_current_buffer(channel);
510
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200511 /* Set channel buffers NOT to be ready */
512 ipu_cm_write(ipu, 0xf0000000, IPU_GPR); /* write one to clear */
513
514 if (ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num)) &
515 idma_mask(channel->num)) {
516 ipu_cm_write(ipu, idma_mask(channel->num),
517 IPU_CHA_BUF0_RDY(channel->num));
518 }
519
520 if (ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num)) &
521 idma_mask(channel->num)) {
522 ipu_cm_write(ipu, idma_mask(channel->num),
523 IPU_CHA_BUF1_RDY(channel->num));
524 }
525
526 ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
527
528 /* Reset the double buffer */
529 val = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
530 val &= ~idma_mask(channel->num);
531 ipu_cm_write(ipu, val, IPU_CHA_DB_MODE_SEL(channel->num));
532
533 spin_unlock_irqrestore(&ipu->lock, flags);
534
535 return 0;
536}
537EXPORT_SYMBOL_GPL(ipu_idmac_disable_channel);
538
Philipp Zabel6c641552013-03-28 17:35:21 +0100539static int ipu_memory_reset(struct ipu_soc *ipu)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200540{
541 unsigned long timeout;
542
543 ipu_cm_write(ipu, 0x807FFFFF, IPU_MEM_RST);
544
545 timeout = jiffies + msecs_to_jiffies(1000);
546 while (ipu_cm_read(ipu, IPU_MEM_RST) & 0x80000000) {
547 if (time_after(jiffies, timeout))
548 return -ETIME;
549 cpu_relax();
550 }
551
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200552 return 0;
553}
554
Steve Longerbeamba079752014-06-25 18:05:30 -0700555/*
556 * Set the source mux for the given CSI. Selects either parallel or
557 * MIPI CSI2 sources.
558 */
559void ipu_set_csi_src_mux(struct ipu_soc *ipu, int csi_id, bool mipi_csi2)
560{
561 unsigned long flags;
562 u32 val, mask;
563
564 mask = (csi_id == 1) ? IPU_CONF_CSI1_DATA_SOURCE :
565 IPU_CONF_CSI0_DATA_SOURCE;
566
567 spin_lock_irqsave(&ipu->lock, flags);
568
569 val = ipu_cm_read(ipu, IPU_CONF);
570 if (mipi_csi2)
571 val |= mask;
572 else
573 val &= ~mask;
574 ipu_cm_write(ipu, val, IPU_CONF);
575
576 spin_unlock_irqrestore(&ipu->lock, flags);
577}
578EXPORT_SYMBOL_GPL(ipu_set_csi_src_mux);
579
580/*
581 * Set the source mux for the IC. Selects either CSI[01] or the VDI.
582 */
583void ipu_set_ic_src_mux(struct ipu_soc *ipu, int csi_id, bool vdi)
584{
585 unsigned long flags;
586 u32 val;
587
588 spin_lock_irqsave(&ipu->lock, flags);
589
590 val = ipu_cm_read(ipu, IPU_CONF);
591 if (vdi) {
592 val |= IPU_CONF_IC_INPUT;
593 } else {
594 val &= ~IPU_CONF_IC_INPUT;
595 if (csi_id == 1)
596 val |= IPU_CONF_CSI_SEL;
597 else
598 val &= ~IPU_CONF_CSI_SEL;
599 }
600 ipu_cm_write(ipu, val, IPU_CONF);
601
602 spin_unlock_irqrestore(&ipu->lock, flags);
603}
604EXPORT_SYMBOL_GPL(ipu_set_ic_src_mux);
605
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200606struct ipu_devtype {
607 const char *name;
608 unsigned long cm_ofs;
609 unsigned long cpmem_ofs;
610 unsigned long srm_ofs;
611 unsigned long tpm_ofs;
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700612 unsigned long csi0_ofs;
613 unsigned long csi1_ofs;
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200614 unsigned long ic_ofs;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200615 unsigned long disp0_ofs;
616 unsigned long disp1_ofs;
617 unsigned long dc_tmpl_ofs;
618 unsigned long vdi_ofs;
619 enum ipuv3_type type;
620};
621
622static struct ipu_devtype ipu_type_imx51 = {
623 .name = "IPUv3EX",
624 .cm_ofs = 0x1e000000,
625 .cpmem_ofs = 0x1f000000,
626 .srm_ofs = 0x1f040000,
627 .tpm_ofs = 0x1f060000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700628 .csi0_ofs = 0x1f030000,
629 .csi1_ofs = 0x1f038000,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200630 .ic_ofs = 0x1f020000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200631 .disp0_ofs = 0x1e040000,
632 .disp1_ofs = 0x1e048000,
633 .dc_tmpl_ofs = 0x1f080000,
634 .vdi_ofs = 0x1e068000,
635 .type = IPUV3EX,
636};
637
638static struct ipu_devtype ipu_type_imx53 = {
639 .name = "IPUv3M",
640 .cm_ofs = 0x06000000,
641 .cpmem_ofs = 0x07000000,
642 .srm_ofs = 0x07040000,
643 .tpm_ofs = 0x07060000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700644 .csi0_ofs = 0x07030000,
645 .csi1_ofs = 0x07038000,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200646 .ic_ofs = 0x07020000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200647 .disp0_ofs = 0x06040000,
648 .disp1_ofs = 0x06048000,
649 .dc_tmpl_ofs = 0x07080000,
650 .vdi_ofs = 0x06068000,
651 .type = IPUV3M,
652};
653
654static struct ipu_devtype ipu_type_imx6q = {
655 .name = "IPUv3H",
656 .cm_ofs = 0x00200000,
657 .cpmem_ofs = 0x00300000,
658 .srm_ofs = 0x00340000,
659 .tpm_ofs = 0x00360000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700660 .csi0_ofs = 0x00230000,
661 .csi1_ofs = 0x00238000,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200662 .ic_ofs = 0x00220000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200663 .disp0_ofs = 0x00240000,
664 .disp1_ofs = 0x00248000,
665 .dc_tmpl_ofs = 0x00380000,
666 .vdi_ofs = 0x00268000,
667 .type = IPUV3H,
668};
669
670static const struct of_device_id imx_ipu_dt_ids[] = {
671 { .compatible = "fsl,imx51-ipu", .data = &ipu_type_imx51, },
672 { .compatible = "fsl,imx53-ipu", .data = &ipu_type_imx53, },
673 { .compatible = "fsl,imx6q-ipu", .data = &ipu_type_imx6q, },
674 { /* sentinel */ }
675};
676MODULE_DEVICE_TABLE(of, imx_ipu_dt_ids);
677
678static int ipu_submodules_init(struct ipu_soc *ipu,
679 struct platform_device *pdev, unsigned long ipu_base,
680 struct clk *ipu_clk)
681{
682 char *unit;
683 int ret;
684 struct device *dev = &pdev->dev;
685 const struct ipu_devtype *devtype = ipu->devtype;
686
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700687 ret = ipu_cpmem_init(ipu, dev, ipu_base + devtype->cpmem_ofs);
688 if (ret) {
689 unit = "cpmem";
690 goto err_cpmem;
691 }
692
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700693 ret = ipu_csi_init(ipu, dev, 0, ipu_base + devtype->csi0_ofs,
694 IPU_CONF_CSI0_EN, ipu_clk);
695 if (ret) {
696 unit = "csi0";
697 goto err_csi_0;
698 }
699
700 ret = ipu_csi_init(ipu, dev, 1, ipu_base + devtype->csi1_ofs,
701 IPU_CONF_CSI1_EN, ipu_clk);
702 if (ret) {
703 unit = "csi1";
704 goto err_csi_1;
705 }
706
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200707 ret = ipu_ic_init(ipu, dev,
708 ipu_base + devtype->ic_ofs,
709 ipu_base + devtype->tpm_ofs);
710 if (ret) {
711 unit = "ic";
712 goto err_ic;
713 }
714
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200715 ret = ipu_di_init(ipu, dev, 0, ipu_base + devtype->disp0_ofs,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200716 IPU_CONF_DI0_EN, ipu_clk);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200717 if (ret) {
718 unit = "di0";
719 goto err_di_0;
720 }
721
722 ret = ipu_di_init(ipu, dev, 1, ipu_base + devtype->disp1_ofs,
723 IPU_CONF_DI1_EN, ipu_clk);
724 if (ret) {
725 unit = "di1";
726 goto err_di_1;
727 }
728
729 ret = ipu_dc_init(ipu, dev, ipu_base + devtype->cm_ofs +
730 IPU_CM_DC_REG_OFS, ipu_base + devtype->dc_tmpl_ofs);
731 if (ret) {
732 unit = "dc_template";
733 goto err_dc;
734 }
735
736 ret = ipu_dmfc_init(ipu, dev, ipu_base +
737 devtype->cm_ofs + IPU_CM_DMFC_REG_OFS, ipu_clk);
738 if (ret) {
739 unit = "dmfc";
740 goto err_dmfc;
741 }
742
743 ret = ipu_dp_init(ipu, dev, ipu_base + devtype->srm_ofs);
744 if (ret) {
745 unit = "dp";
746 goto err_dp;
747 }
748
Philipp Zabel35de9252012-05-09 16:59:01 +0200749 ret = ipu_smfc_init(ipu, dev, ipu_base +
750 devtype->cm_ofs + IPU_CM_SMFC_REG_OFS);
751 if (ret) {
752 unit = "smfc";
753 goto err_smfc;
754 }
755
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200756 return 0;
757
Philipp Zabel35de9252012-05-09 16:59:01 +0200758err_smfc:
759 ipu_dp_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200760err_dp:
761 ipu_dmfc_exit(ipu);
762err_dmfc:
763 ipu_dc_exit(ipu);
764err_dc:
765 ipu_di_exit(ipu, 1);
766err_di_1:
767 ipu_di_exit(ipu, 0);
768err_di_0:
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200769 ipu_ic_exit(ipu);
770err_ic:
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700771 ipu_csi_exit(ipu, 1);
772err_csi_1:
773 ipu_csi_exit(ipu, 0);
774err_csi_0:
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700775 ipu_cpmem_exit(ipu);
776err_cpmem:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200777 dev_err(&pdev->dev, "init %s failed with %d\n", unit, ret);
778 return ret;
779}
780
781static void ipu_irq_handle(struct ipu_soc *ipu, const int *regs, int num_regs)
782{
783 unsigned long status;
Philipp Zabelb7287662013-06-21 10:27:39 +0200784 int i, bit, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200785
786 for (i = 0; i < num_regs; i++) {
787
788 status = ipu_cm_read(ipu, IPU_INT_STAT(regs[i]));
789 status &= ipu_cm_read(ipu, IPU_INT_CTRL(regs[i]));
790
Philipp Zabelb7287662013-06-21 10:27:39 +0200791 for_each_set_bit(bit, &status, 32) {
Antoine Schweitzer-Chaput838201a2014-04-18 23:20:06 +0200792 irq = irq_linear_revmap(ipu->domain,
793 regs[i] * 32 + bit);
Philipp Zabelb7287662013-06-21 10:27:39 +0200794 if (irq)
795 generic_handle_irq(irq);
796 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200797 }
798}
799
800static void ipu_irq_handler(unsigned int irq, struct irq_desc *desc)
801{
802 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
803 const int int_reg[] = { 0, 1, 2, 3, 10, 11, 12, 13, 14};
804 struct irq_chip *chip = irq_get_chip(irq);
805
806 chained_irq_enter(chip, desc);
807
808 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
809
810 chained_irq_exit(chip, desc);
811}
812
813static void ipu_err_irq_handler(unsigned int irq, struct irq_desc *desc)
814{
815 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
816 const int int_reg[] = { 4, 5, 8, 9};
817 struct irq_chip *chip = irq_get_chip(irq);
818
819 chained_irq_enter(chip, desc);
820
821 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
822
823 chained_irq_exit(chip, desc);
824}
825
Philipp Zabel861a50c2014-04-14 23:53:16 +0200826int ipu_map_irq(struct ipu_soc *ipu, int irq)
827{
828 int virq;
829
830 virq = irq_linear_revmap(ipu->domain, irq);
831 if (!virq)
832 virq = irq_create_mapping(ipu->domain, irq);
833
834 return virq;
835}
836EXPORT_SYMBOL_GPL(ipu_map_irq);
837
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200838int ipu_idmac_channel_irq(struct ipu_soc *ipu, struct ipuv3_channel *channel,
839 enum ipu_channel_irq irq_type)
840{
Philipp Zabel861a50c2014-04-14 23:53:16 +0200841 return ipu_map_irq(ipu, irq_type + channel->num);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200842}
843EXPORT_SYMBOL_GPL(ipu_idmac_channel_irq);
844
845static void ipu_submodules_exit(struct ipu_soc *ipu)
846{
Philipp Zabel35de9252012-05-09 16:59:01 +0200847 ipu_smfc_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200848 ipu_dp_exit(ipu);
849 ipu_dmfc_exit(ipu);
850 ipu_dc_exit(ipu);
851 ipu_di_exit(ipu, 1);
852 ipu_di_exit(ipu, 0);
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200853 ipu_ic_exit(ipu);
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700854 ipu_csi_exit(ipu, 1);
855 ipu_csi_exit(ipu, 0);
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700856 ipu_cpmem_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200857}
858
859static int platform_remove_devices_fn(struct device *dev, void *unused)
860{
861 struct platform_device *pdev = to_platform_device(dev);
862
863 platform_device_unregister(pdev);
864
865 return 0;
866}
867
868static void platform_device_unregister_children(struct platform_device *pdev)
869{
870 device_for_each_child(&pdev->dev, NULL, platform_remove_devices_fn);
871}
872
873struct ipu_platform_reg {
874 struct ipu_client_platformdata pdata;
875 const char *name;
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200876 int reg_offset;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200877};
878
879static const struct ipu_platform_reg client_reg[] = {
880 {
881 .pdata = {
882 .di = 0,
883 .dc = 5,
884 .dp = IPU_DP_FLOW_SYNC_BG,
885 .dma[0] = IPUV3_CHANNEL_MEM_BG_SYNC,
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200886 .dma[1] = IPUV3_CHANNEL_MEM_FG_SYNC,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200887 },
888 .name = "imx-ipuv3-crtc",
889 }, {
890 .pdata = {
891 .di = 1,
892 .dc = 1,
893 .dp = -EINVAL,
894 .dma[0] = IPUV3_CHANNEL_MEM_DC_SYNC,
895 .dma[1] = -EINVAL,
896 },
897 .name = "imx-ipuv3-crtc",
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200898 }, {
899 .pdata = {
900 .csi = 0,
901 .dma[0] = IPUV3_CHANNEL_CSI0,
902 .dma[1] = -EINVAL,
903 },
904 .reg_offset = IPU_CM_CSI0_REG_OFS,
905 .name = "imx-ipuv3-camera",
906 }, {
907 .pdata = {
908 .csi = 1,
909 .dma[0] = IPUV3_CHANNEL_CSI1,
910 .dma[1] = -EINVAL,
911 },
912 .reg_offset = IPU_CM_CSI1_REG_OFS,
913 .name = "imx-ipuv3-camera",
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200914 },
915};
916
Russell King4ae078d2013-12-16 11:34:25 +0000917static DEFINE_MUTEX(ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200918static int ipu_client_id;
919
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200920static int ipu_add_client_devices(struct ipu_soc *ipu, unsigned long ipu_base)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200921{
Russell King4ae078d2013-12-16 11:34:25 +0000922 struct device *dev = ipu->dev;
923 unsigned i;
924 int id, ret;
925
926 mutex_lock(&ipu_client_id_mutex);
927 id = ipu_client_id;
928 ipu_client_id += ARRAY_SIZE(client_reg);
929 mutex_unlock(&ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200930
931 for (i = 0; i < ARRAY_SIZE(client_reg); i++) {
932 const struct ipu_platform_reg *reg = &client_reg[i];
Russell King4ae078d2013-12-16 11:34:25 +0000933 struct platform_device *pdev;
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200934 struct resource res;
Russell King4ae078d2013-12-16 11:34:25 +0000935
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200936 if (reg->reg_offset) {
937 memset(&res, 0, sizeof(res));
938 res.flags = IORESOURCE_MEM;
939 res.start = ipu_base + ipu->devtype->cm_ofs + reg->reg_offset;
940 res.end = res.start + PAGE_SIZE - 1;
941 pdev = platform_device_register_resndata(dev, reg->name,
942 id++, &res, 1, &reg->pdata, sizeof(reg->pdata));
943 } else {
944 pdev = platform_device_register_data(dev, reg->name,
945 id++, &reg->pdata, sizeof(reg->pdata));
946 }
Russell King4ae078d2013-12-16 11:34:25 +0000947
948 if (IS_ERR(pdev))
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200949 goto err_register;
950 }
951
952 return 0;
953
954err_register:
Russell King4ae078d2013-12-16 11:34:25 +0000955 platform_device_unregister_children(to_platform_device(dev));
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200956
957 return ret;
958}
959
Philipp Zabelb7287662013-06-21 10:27:39 +0200960
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200961static int ipu_irq_init(struct ipu_soc *ipu)
962{
Philipp Zabel379cdec2013-06-21 14:52:17 +0200963 struct irq_chip_generic *gc;
964 struct irq_chip_type *ct;
Philipp Zabel37f85b262013-06-21 14:52:18 +0200965 unsigned long unused[IPU_NUM_IRQS / 32] = {
966 0x400100d0, 0xffe000fd,
967 0x400100d0, 0xffe000fd,
968 0x400100d0, 0xffe000fd,
969 0x4077ffff, 0xffe7e1fd,
970 0x23fffffe, 0x8880fff0,
971 0xf98fe7d0, 0xfff81fff,
972 0x400100d0, 0xffe000fd,
973 0x00000000,
974 };
Philipp Zabel379cdec2013-06-21 14:52:17 +0200975 int ret, i;
976
Philipp Zabelb7287662013-06-21 10:27:39 +0200977 ipu->domain = irq_domain_add_linear(ipu->dev->of_node, IPU_NUM_IRQS,
Philipp Zabel379cdec2013-06-21 14:52:17 +0200978 &irq_generic_chip_ops, ipu);
Philipp Zabelb7287662013-06-21 10:27:39 +0200979 if (!ipu->domain) {
980 dev_err(ipu->dev, "failed to add irq domain\n");
981 return -ENODEV;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200982 }
983
Philipp Zabel379cdec2013-06-21 14:52:17 +0200984 ret = irq_alloc_domain_generic_chips(ipu->domain, 32, 1, "IPU",
Antoine Schweitzer-Chaput838201a2014-04-18 23:20:06 +0200985 handle_level_irq, 0,
986 IRQF_VALID, 0);
Philipp Zabel379cdec2013-06-21 14:52:17 +0200987 if (ret < 0) {
988 dev_err(ipu->dev, "failed to alloc generic irq chips\n");
989 irq_domain_remove(ipu->domain);
990 return ret;
991 }
992
993 for (i = 0; i < IPU_NUM_IRQS; i += 32) {
994 gc = irq_get_domain_generic_chip(ipu->domain, i);
995 gc->reg_base = ipu->cm_reg;
Philipp Zabel37f85b262013-06-21 14:52:18 +0200996 gc->unused = unused[i / 32];
Philipp Zabel379cdec2013-06-21 14:52:17 +0200997 ct = gc->chip_types;
998 ct->chip.irq_ack = irq_gc_ack_set_bit;
999 ct->chip.irq_mask = irq_gc_mask_clr_bit;
1000 ct->chip.irq_unmask = irq_gc_mask_set_bit;
1001 ct->regs.ack = IPU_INT_STAT(i / 32);
1002 ct->regs.mask = IPU_INT_CTRL(i / 32);
1003 }
1004
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001005 irq_set_chained_handler(ipu->irq_sync, ipu_irq_handler);
1006 irq_set_handler_data(ipu->irq_sync, ipu);
1007 irq_set_chained_handler(ipu->irq_err, ipu_err_irq_handler);
1008 irq_set_handler_data(ipu->irq_err, ipu);
1009
1010 return 0;
1011}
1012
1013static void ipu_irq_exit(struct ipu_soc *ipu)
1014{
Philipp Zabelb7287662013-06-21 10:27:39 +02001015 int i, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001016
1017 irq_set_chained_handler(ipu->irq_err, NULL);
1018 irq_set_handler_data(ipu->irq_err, NULL);
1019 irq_set_chained_handler(ipu->irq_sync, NULL);
1020 irq_set_handler_data(ipu->irq_sync, NULL);
1021
Philipp Zabel379cdec2013-06-21 14:52:17 +02001022 /* TODO: remove irq_domain_generic_chips */
1023
Philipp Zabelb7287662013-06-21 10:27:39 +02001024 for (i = 0; i < IPU_NUM_IRQS; i++) {
1025 irq = irq_linear_revmap(ipu->domain, i);
1026 if (irq)
1027 irq_dispose_mapping(irq);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001028 }
1029
Philipp Zabelb7287662013-06-21 10:27:39 +02001030 irq_domain_remove(ipu->domain);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001031}
1032
Bill Pembertonc4aabf82012-11-19 13:22:11 -05001033static int ipu_probe(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001034{
1035 const struct of_device_id *of_id =
1036 of_match_device(imx_ipu_dt_ids, &pdev->dev);
1037 struct ipu_soc *ipu;
1038 struct resource *res;
1039 unsigned long ipu_base;
1040 int i, ret, irq_sync, irq_err;
1041 const struct ipu_devtype *devtype;
1042
1043 devtype = of_id->data;
1044
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001045 irq_sync = platform_get_irq(pdev, 0);
1046 irq_err = platform_get_irq(pdev, 1);
1047 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1048
Fabio Estevamfd563db2012-10-24 21:36:46 -02001049 dev_dbg(&pdev->dev, "irq_sync: %d irq_err: %d\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001050 irq_sync, irq_err);
1051
1052 if (!res || irq_sync < 0 || irq_err < 0)
1053 return -ENODEV;
1054
1055 ipu_base = res->start;
1056
1057 ipu = devm_kzalloc(&pdev->dev, sizeof(*ipu), GFP_KERNEL);
1058 if (!ipu)
1059 return -ENODEV;
1060
1061 for (i = 0; i < 64; i++)
1062 ipu->channel[i].ipu = ipu;
1063 ipu->devtype = devtype;
1064 ipu->ipu_type = devtype->type;
1065
1066 spin_lock_init(&ipu->lock);
1067 mutex_init(&ipu->channel_lock);
1068
Fabio Estevamfd563db2012-10-24 21:36:46 -02001069 dev_dbg(&pdev->dev, "cm_reg: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001070 ipu_base + devtype->cm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001071 dev_dbg(&pdev->dev, "idmac: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001072 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001073 dev_dbg(&pdev->dev, "cpmem: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001074 ipu_base + devtype->cpmem_ofs);
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -07001075 dev_dbg(&pdev->dev, "csi0: 0x%08lx\n",
1076 ipu_base + devtype->csi0_ofs);
1077 dev_dbg(&pdev->dev, "csi1: 0x%08lx\n",
1078 ipu_base + devtype->csi1_ofs);
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +02001079 dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
1080 ipu_base + devtype->ic_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001081 dev_dbg(&pdev->dev, "disp0: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001082 ipu_base + devtype->disp0_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001083 dev_dbg(&pdev->dev, "disp1: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001084 ipu_base + devtype->disp1_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001085 dev_dbg(&pdev->dev, "srm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001086 ipu_base + devtype->srm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001087 dev_dbg(&pdev->dev, "tpm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001088 ipu_base + devtype->tpm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001089 dev_dbg(&pdev->dev, "dc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001090 ipu_base + devtype->cm_ofs + IPU_CM_DC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001091 dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001092 ipu_base + devtype->cm_ofs + IPU_CM_IC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001093 dev_dbg(&pdev->dev, "dmfc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001094 ipu_base + devtype->cm_ofs + IPU_CM_DMFC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001095 dev_dbg(&pdev->dev, "vdi: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001096 ipu_base + devtype->vdi_ofs);
1097
1098 ipu->cm_reg = devm_ioremap(&pdev->dev,
1099 ipu_base + devtype->cm_ofs, PAGE_SIZE);
1100 ipu->idmac_reg = devm_ioremap(&pdev->dev,
1101 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS,
1102 PAGE_SIZE);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001103
Steve Longerbeam7d2691d2014-06-25 18:05:47 -07001104 if (!ipu->cm_reg || !ipu->idmac_reg)
Fabio Estevambe798b22013-07-20 18:22:09 -03001105 return -ENOMEM;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001106
1107 ipu->clk = devm_clk_get(&pdev->dev, "bus");
1108 if (IS_ERR(ipu->clk)) {
1109 ret = PTR_ERR(ipu->clk);
1110 dev_err(&pdev->dev, "clk_get failed with %d", ret);
Fabio Estevambe798b22013-07-20 18:22:09 -03001111 return ret;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001112 }
1113
1114 platform_set_drvdata(pdev, ipu);
1115
Fabio Estevam62645a22013-07-20 18:22:10 -03001116 ret = clk_prepare_enable(ipu->clk);
1117 if (ret) {
1118 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1119 return ret;
1120 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001121
1122 ipu->dev = &pdev->dev;
1123 ipu->irq_sync = irq_sync;
1124 ipu->irq_err = irq_err;
1125
1126 ret = ipu_irq_init(ipu);
1127 if (ret)
1128 goto out_failed_irq;
1129
Philipp Zabel6c641552013-03-28 17:35:21 +01001130 ret = device_reset(&pdev->dev);
1131 if (ret) {
1132 dev_err(&pdev->dev, "failed to reset: %d\n", ret);
1133 goto out_failed_reset;
1134 }
1135 ret = ipu_memory_reset(ipu);
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001136 if (ret)
1137 goto out_failed_reset;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001138
1139 /* Set MCU_T to divide MCU access window into 2 */
1140 ipu_cm_write(ipu, 0x00400000L | (IPU_MCU_T_DEFAULT << 18),
1141 IPU_DISP_GEN);
1142
1143 ret = ipu_submodules_init(ipu, pdev, ipu_base, ipu->clk);
1144 if (ret)
1145 goto failed_submodules_init;
1146
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +02001147 ret = ipu_add_client_devices(ipu, ipu_base);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001148 if (ret) {
1149 dev_err(&pdev->dev, "adding client devices failed with %d\n",
1150 ret);
1151 goto failed_add_clients;
1152 }
1153
Fabio Estevam9c2c4382012-10-24 21:36:47 -02001154 dev_info(&pdev->dev, "%s probed\n", devtype->name);
1155
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001156 return 0;
1157
1158failed_add_clients:
1159 ipu_submodules_exit(ipu);
1160failed_submodules_init:
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001161out_failed_reset:
Philipp Zabel6c641552013-03-28 17:35:21 +01001162 ipu_irq_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001163out_failed_irq:
1164 clk_disable_unprepare(ipu->clk);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001165 return ret;
1166}
1167
Bill Pemberton8aa1be42012-11-19 13:26:38 -05001168static int ipu_remove(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001169{
1170 struct ipu_soc *ipu = platform_get_drvdata(pdev);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001171
1172 platform_device_unregister_children(pdev);
1173 ipu_submodules_exit(ipu);
1174 ipu_irq_exit(ipu);
1175
1176 clk_disable_unprepare(ipu->clk);
1177
1178 return 0;
1179}
1180
1181static struct platform_driver imx_ipu_driver = {
1182 .driver = {
1183 .name = "imx-ipuv3",
1184 .of_match_table = imx_ipu_dt_ids,
1185 },
1186 .probe = ipu_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -05001187 .remove = ipu_remove,
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001188};
1189
1190module_platform_driver(imx_ipu_driver);
1191
Fabio Estevam10f22682013-07-20 18:22:11 -03001192MODULE_ALIAS("platform:imx-ipuv3");
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001193MODULE_DESCRIPTION("i.MX IPU v3 driver");
1194MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1195MODULE_LICENSE("GPL");