blob: 0a02465ba102112a0e171dcf9fb513f5459e3dd1 [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
Steve Longerbeam2bcf5772014-06-25 18:05:44 -0700539/*
540 * The imx6 rev. D TRM says that enabling the WM feature will increase
541 * a channel's priority. Refer to Table 36-8 Calculated priority value.
542 * The sub-module that is the sink or source for the channel must enable
543 * watermark signal for this to take effect (SMFC_WM for instance).
544 */
545void ipu_idmac_enable_watermark(struct ipuv3_channel *channel, bool enable)
546{
547 struct ipu_soc *ipu = channel->ipu;
548 unsigned long flags;
549 u32 val;
550
551 spin_lock_irqsave(&ipu->lock, flags);
552
553 val = ipu_idmac_read(ipu, IDMAC_WM_EN(channel->num));
554 if (enable)
555 val |= 1 << (channel->num % 32);
556 else
557 val &= ~(1 << (channel->num % 32));
558 ipu_idmac_write(ipu, val, IDMAC_WM_EN(channel->num));
559
560 spin_unlock_irqrestore(&ipu->lock, flags);
561}
562EXPORT_SYMBOL_GPL(ipu_idmac_enable_watermark);
563
Philipp Zabel6c641552013-03-28 17:35:21 +0100564static int ipu_memory_reset(struct ipu_soc *ipu)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200565{
566 unsigned long timeout;
567
568 ipu_cm_write(ipu, 0x807FFFFF, IPU_MEM_RST);
569
570 timeout = jiffies + msecs_to_jiffies(1000);
571 while (ipu_cm_read(ipu, IPU_MEM_RST) & 0x80000000) {
572 if (time_after(jiffies, timeout))
573 return -ETIME;
574 cpu_relax();
575 }
576
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200577 return 0;
578}
579
Steve Longerbeamba079752014-06-25 18:05:30 -0700580/*
581 * Set the source mux for the given CSI. Selects either parallel or
582 * MIPI CSI2 sources.
583 */
584void ipu_set_csi_src_mux(struct ipu_soc *ipu, int csi_id, bool mipi_csi2)
585{
586 unsigned long flags;
587 u32 val, mask;
588
589 mask = (csi_id == 1) ? IPU_CONF_CSI1_DATA_SOURCE :
590 IPU_CONF_CSI0_DATA_SOURCE;
591
592 spin_lock_irqsave(&ipu->lock, flags);
593
594 val = ipu_cm_read(ipu, IPU_CONF);
595 if (mipi_csi2)
596 val |= mask;
597 else
598 val &= ~mask;
599 ipu_cm_write(ipu, val, IPU_CONF);
600
601 spin_unlock_irqrestore(&ipu->lock, flags);
602}
603EXPORT_SYMBOL_GPL(ipu_set_csi_src_mux);
604
605/*
606 * Set the source mux for the IC. Selects either CSI[01] or the VDI.
607 */
608void ipu_set_ic_src_mux(struct ipu_soc *ipu, int csi_id, bool vdi)
609{
610 unsigned long flags;
611 u32 val;
612
613 spin_lock_irqsave(&ipu->lock, flags);
614
615 val = ipu_cm_read(ipu, IPU_CONF);
616 if (vdi) {
617 val |= IPU_CONF_IC_INPUT;
618 } else {
619 val &= ~IPU_CONF_IC_INPUT;
620 if (csi_id == 1)
621 val |= IPU_CONF_CSI_SEL;
622 else
623 val &= ~IPU_CONF_CSI_SEL;
624 }
625 ipu_cm_write(ipu, val, IPU_CONF);
626
627 spin_unlock_irqrestore(&ipu->lock, flags);
628}
629EXPORT_SYMBOL_GPL(ipu_set_ic_src_mux);
630
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200631struct ipu_devtype {
632 const char *name;
633 unsigned long cm_ofs;
634 unsigned long cpmem_ofs;
635 unsigned long srm_ofs;
636 unsigned long tpm_ofs;
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700637 unsigned long csi0_ofs;
638 unsigned long csi1_ofs;
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200639 unsigned long ic_ofs;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200640 unsigned long disp0_ofs;
641 unsigned long disp1_ofs;
642 unsigned long dc_tmpl_ofs;
643 unsigned long vdi_ofs;
644 enum ipuv3_type type;
645};
646
647static struct ipu_devtype ipu_type_imx51 = {
648 .name = "IPUv3EX",
649 .cm_ofs = 0x1e000000,
650 .cpmem_ofs = 0x1f000000,
651 .srm_ofs = 0x1f040000,
652 .tpm_ofs = 0x1f060000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700653 .csi0_ofs = 0x1f030000,
654 .csi1_ofs = 0x1f038000,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200655 .ic_ofs = 0x1f020000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200656 .disp0_ofs = 0x1e040000,
657 .disp1_ofs = 0x1e048000,
658 .dc_tmpl_ofs = 0x1f080000,
659 .vdi_ofs = 0x1e068000,
660 .type = IPUV3EX,
661};
662
663static struct ipu_devtype ipu_type_imx53 = {
664 .name = "IPUv3M",
665 .cm_ofs = 0x06000000,
666 .cpmem_ofs = 0x07000000,
667 .srm_ofs = 0x07040000,
668 .tpm_ofs = 0x07060000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700669 .csi0_ofs = 0x07030000,
670 .csi1_ofs = 0x07038000,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200671 .ic_ofs = 0x07020000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200672 .disp0_ofs = 0x06040000,
673 .disp1_ofs = 0x06048000,
674 .dc_tmpl_ofs = 0x07080000,
675 .vdi_ofs = 0x06068000,
676 .type = IPUV3M,
677};
678
679static struct ipu_devtype ipu_type_imx6q = {
680 .name = "IPUv3H",
681 .cm_ofs = 0x00200000,
682 .cpmem_ofs = 0x00300000,
683 .srm_ofs = 0x00340000,
684 .tpm_ofs = 0x00360000,
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700685 .csi0_ofs = 0x00230000,
686 .csi1_ofs = 0x00238000,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200687 .ic_ofs = 0x00220000,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200688 .disp0_ofs = 0x00240000,
689 .disp1_ofs = 0x00248000,
690 .dc_tmpl_ofs = 0x00380000,
691 .vdi_ofs = 0x00268000,
692 .type = IPUV3H,
693};
694
695static const struct of_device_id imx_ipu_dt_ids[] = {
696 { .compatible = "fsl,imx51-ipu", .data = &ipu_type_imx51, },
697 { .compatible = "fsl,imx53-ipu", .data = &ipu_type_imx53, },
698 { .compatible = "fsl,imx6q-ipu", .data = &ipu_type_imx6q, },
699 { /* sentinel */ }
700};
701MODULE_DEVICE_TABLE(of, imx_ipu_dt_ids);
702
703static int ipu_submodules_init(struct ipu_soc *ipu,
704 struct platform_device *pdev, unsigned long ipu_base,
705 struct clk *ipu_clk)
706{
707 char *unit;
708 int ret;
709 struct device *dev = &pdev->dev;
710 const struct ipu_devtype *devtype = ipu->devtype;
711
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700712 ret = ipu_cpmem_init(ipu, dev, ipu_base + devtype->cpmem_ofs);
713 if (ret) {
714 unit = "cpmem";
715 goto err_cpmem;
716 }
717
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700718 ret = ipu_csi_init(ipu, dev, 0, ipu_base + devtype->csi0_ofs,
719 IPU_CONF_CSI0_EN, ipu_clk);
720 if (ret) {
721 unit = "csi0";
722 goto err_csi_0;
723 }
724
725 ret = ipu_csi_init(ipu, dev, 1, ipu_base + devtype->csi1_ofs,
726 IPU_CONF_CSI1_EN, ipu_clk);
727 if (ret) {
728 unit = "csi1";
729 goto err_csi_1;
730 }
731
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200732 ret = ipu_ic_init(ipu, dev,
733 ipu_base + devtype->ic_ofs,
734 ipu_base + devtype->tpm_ofs);
735 if (ret) {
736 unit = "ic";
737 goto err_ic;
738 }
739
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200740 ret = ipu_di_init(ipu, dev, 0, ipu_base + devtype->disp0_ofs,
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200741 IPU_CONF_DI0_EN, ipu_clk);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200742 if (ret) {
743 unit = "di0";
744 goto err_di_0;
745 }
746
747 ret = ipu_di_init(ipu, dev, 1, ipu_base + devtype->disp1_ofs,
748 IPU_CONF_DI1_EN, ipu_clk);
749 if (ret) {
750 unit = "di1";
751 goto err_di_1;
752 }
753
754 ret = ipu_dc_init(ipu, dev, ipu_base + devtype->cm_ofs +
755 IPU_CM_DC_REG_OFS, ipu_base + devtype->dc_tmpl_ofs);
756 if (ret) {
757 unit = "dc_template";
758 goto err_dc;
759 }
760
761 ret = ipu_dmfc_init(ipu, dev, ipu_base +
762 devtype->cm_ofs + IPU_CM_DMFC_REG_OFS, ipu_clk);
763 if (ret) {
764 unit = "dmfc";
765 goto err_dmfc;
766 }
767
768 ret = ipu_dp_init(ipu, dev, ipu_base + devtype->srm_ofs);
769 if (ret) {
770 unit = "dp";
771 goto err_dp;
772 }
773
Philipp Zabel35de9252012-05-09 16:59:01 +0200774 ret = ipu_smfc_init(ipu, dev, ipu_base +
775 devtype->cm_ofs + IPU_CM_SMFC_REG_OFS);
776 if (ret) {
777 unit = "smfc";
778 goto err_smfc;
779 }
780
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200781 return 0;
782
Philipp Zabel35de9252012-05-09 16:59:01 +0200783err_smfc:
784 ipu_dp_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200785err_dp:
786 ipu_dmfc_exit(ipu);
787err_dmfc:
788 ipu_dc_exit(ipu);
789err_dc:
790 ipu_di_exit(ipu, 1);
791err_di_1:
792 ipu_di_exit(ipu, 0);
793err_di_0:
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200794 ipu_ic_exit(ipu);
795err_ic:
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700796 ipu_csi_exit(ipu, 1);
797err_csi_1:
798 ipu_csi_exit(ipu, 0);
799err_csi_0:
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700800 ipu_cpmem_exit(ipu);
801err_cpmem:
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200802 dev_err(&pdev->dev, "init %s failed with %d\n", unit, ret);
803 return ret;
804}
805
806static void ipu_irq_handle(struct ipu_soc *ipu, const int *regs, int num_regs)
807{
808 unsigned long status;
Philipp Zabelb7287662013-06-21 10:27:39 +0200809 int i, bit, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200810
811 for (i = 0; i < num_regs; i++) {
812
813 status = ipu_cm_read(ipu, IPU_INT_STAT(regs[i]));
814 status &= ipu_cm_read(ipu, IPU_INT_CTRL(regs[i]));
815
Philipp Zabelb7287662013-06-21 10:27:39 +0200816 for_each_set_bit(bit, &status, 32) {
Antoine Schweitzer-Chaput838201a2014-04-18 23:20:06 +0200817 irq = irq_linear_revmap(ipu->domain,
818 regs[i] * 32 + bit);
Philipp Zabelb7287662013-06-21 10:27:39 +0200819 if (irq)
820 generic_handle_irq(irq);
821 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200822 }
823}
824
825static void ipu_irq_handler(unsigned int irq, struct irq_desc *desc)
826{
827 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
828 const int int_reg[] = { 0, 1, 2, 3, 10, 11, 12, 13, 14};
829 struct irq_chip *chip = irq_get_chip(irq);
830
831 chained_irq_enter(chip, desc);
832
833 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
834
835 chained_irq_exit(chip, desc);
836}
837
838static void ipu_err_irq_handler(unsigned int irq, struct irq_desc *desc)
839{
840 struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
841 const int int_reg[] = { 4, 5, 8, 9};
842 struct irq_chip *chip = irq_get_chip(irq);
843
844 chained_irq_enter(chip, desc);
845
846 ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
847
848 chained_irq_exit(chip, desc);
849}
850
Philipp Zabel861a50c2014-04-14 23:53:16 +0200851int ipu_map_irq(struct ipu_soc *ipu, int irq)
852{
853 int virq;
854
855 virq = irq_linear_revmap(ipu->domain, irq);
856 if (!virq)
857 virq = irq_create_mapping(ipu->domain, irq);
858
859 return virq;
860}
861EXPORT_SYMBOL_GPL(ipu_map_irq);
862
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200863int ipu_idmac_channel_irq(struct ipu_soc *ipu, struct ipuv3_channel *channel,
864 enum ipu_channel_irq irq_type)
865{
Philipp Zabel861a50c2014-04-14 23:53:16 +0200866 return ipu_map_irq(ipu, irq_type + channel->num);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200867}
868EXPORT_SYMBOL_GPL(ipu_idmac_channel_irq);
869
870static void ipu_submodules_exit(struct ipu_soc *ipu)
871{
Philipp Zabel35de9252012-05-09 16:59:01 +0200872 ipu_smfc_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200873 ipu_dp_exit(ipu);
874 ipu_dmfc_exit(ipu);
875 ipu_dc_exit(ipu);
876 ipu_di_exit(ipu, 1);
877 ipu_di_exit(ipu, 0);
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +0200878 ipu_ic_exit(ipu);
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -0700879 ipu_csi_exit(ipu, 1);
880 ipu_csi_exit(ipu, 0);
Steve Longerbeam7d2691d2014-06-25 18:05:47 -0700881 ipu_cpmem_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200882}
883
884static int platform_remove_devices_fn(struct device *dev, void *unused)
885{
886 struct platform_device *pdev = to_platform_device(dev);
887
888 platform_device_unregister(pdev);
889
890 return 0;
891}
892
893static void platform_device_unregister_children(struct platform_device *pdev)
894{
895 device_for_each_child(&pdev->dev, NULL, platform_remove_devices_fn);
896}
897
898struct ipu_platform_reg {
899 struct ipu_client_platformdata pdata;
900 const char *name;
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200901 int reg_offset;
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200902};
903
904static const struct ipu_platform_reg client_reg[] = {
905 {
906 .pdata = {
907 .di = 0,
908 .dc = 5,
909 .dp = IPU_DP_FLOW_SYNC_BG,
910 .dma[0] = IPUV3_CHANNEL_MEM_BG_SYNC,
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200911 .dma[1] = IPUV3_CHANNEL_MEM_FG_SYNC,
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200912 },
913 .name = "imx-ipuv3-crtc",
914 }, {
915 .pdata = {
916 .di = 1,
917 .dc = 1,
918 .dp = -EINVAL,
919 .dma[0] = IPUV3_CHANNEL_MEM_DC_SYNC,
920 .dma[1] = -EINVAL,
921 },
922 .name = "imx-ipuv3-crtc",
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200923 }, {
924 .pdata = {
925 .csi = 0,
926 .dma[0] = IPUV3_CHANNEL_CSI0,
927 .dma[1] = -EINVAL,
928 },
929 .reg_offset = IPU_CM_CSI0_REG_OFS,
930 .name = "imx-ipuv3-camera",
931 }, {
932 .pdata = {
933 .csi = 1,
934 .dma[0] = IPUV3_CHANNEL_CSI1,
935 .dma[1] = -EINVAL,
936 },
937 .reg_offset = IPU_CM_CSI1_REG_OFS,
938 .name = "imx-ipuv3-camera",
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200939 },
940};
941
Russell King4ae078d2013-12-16 11:34:25 +0000942static DEFINE_MUTEX(ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200943static int ipu_client_id;
944
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200945static int ipu_add_client_devices(struct ipu_soc *ipu, unsigned long ipu_base)
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200946{
Russell King4ae078d2013-12-16 11:34:25 +0000947 struct device *dev = ipu->dev;
948 unsigned i;
949 int id, ret;
950
951 mutex_lock(&ipu_client_id_mutex);
952 id = ipu_client_id;
953 ipu_client_id += ARRAY_SIZE(client_reg);
954 mutex_unlock(&ipu_client_id_mutex);
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200955
956 for (i = 0; i < ARRAY_SIZE(client_reg); i++) {
957 const struct ipu_platform_reg *reg = &client_reg[i];
Russell King4ae078d2013-12-16 11:34:25 +0000958 struct platform_device *pdev;
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200959 struct resource res;
Russell King4ae078d2013-12-16 11:34:25 +0000960
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +0200961 if (reg->reg_offset) {
962 memset(&res, 0, sizeof(res));
963 res.flags = IORESOURCE_MEM;
964 res.start = ipu_base + ipu->devtype->cm_ofs + reg->reg_offset;
965 res.end = res.start + PAGE_SIZE - 1;
966 pdev = platform_device_register_resndata(dev, reg->name,
967 id++, &res, 1, &reg->pdata, sizeof(reg->pdata));
968 } else {
969 pdev = platform_device_register_data(dev, reg->name,
970 id++, &reg->pdata, sizeof(reg->pdata));
971 }
Russell King4ae078d2013-12-16 11:34:25 +0000972
973 if (IS_ERR(pdev))
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200974 goto err_register;
975 }
976
977 return 0;
978
979err_register:
Russell King4ae078d2013-12-16 11:34:25 +0000980 platform_device_unregister_children(to_platform_device(dev));
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200981
982 return ret;
983}
984
Philipp Zabelb7287662013-06-21 10:27:39 +0200985
Sascha Haueraecfbdb2012-09-21 10:07:49 +0200986static int ipu_irq_init(struct ipu_soc *ipu)
987{
Philipp Zabel379cdec2013-06-21 14:52:17 +0200988 struct irq_chip_generic *gc;
989 struct irq_chip_type *ct;
Philipp Zabel37f85b262013-06-21 14:52:18 +0200990 unsigned long unused[IPU_NUM_IRQS / 32] = {
991 0x400100d0, 0xffe000fd,
992 0x400100d0, 0xffe000fd,
993 0x400100d0, 0xffe000fd,
994 0x4077ffff, 0xffe7e1fd,
995 0x23fffffe, 0x8880fff0,
996 0xf98fe7d0, 0xfff81fff,
997 0x400100d0, 0xffe000fd,
998 0x00000000,
999 };
Philipp Zabel379cdec2013-06-21 14:52:17 +02001000 int ret, i;
1001
Philipp Zabelb7287662013-06-21 10:27:39 +02001002 ipu->domain = irq_domain_add_linear(ipu->dev->of_node, IPU_NUM_IRQS,
Philipp Zabel379cdec2013-06-21 14:52:17 +02001003 &irq_generic_chip_ops, ipu);
Philipp Zabelb7287662013-06-21 10:27:39 +02001004 if (!ipu->domain) {
1005 dev_err(ipu->dev, "failed to add irq domain\n");
1006 return -ENODEV;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001007 }
1008
Philipp Zabel379cdec2013-06-21 14:52:17 +02001009 ret = irq_alloc_domain_generic_chips(ipu->domain, 32, 1, "IPU",
Antoine Schweitzer-Chaput838201a2014-04-18 23:20:06 +02001010 handle_level_irq, 0,
1011 IRQF_VALID, 0);
Philipp Zabel379cdec2013-06-21 14:52:17 +02001012 if (ret < 0) {
1013 dev_err(ipu->dev, "failed to alloc generic irq chips\n");
1014 irq_domain_remove(ipu->domain);
1015 return ret;
1016 }
1017
1018 for (i = 0; i < IPU_NUM_IRQS; i += 32) {
1019 gc = irq_get_domain_generic_chip(ipu->domain, i);
1020 gc->reg_base = ipu->cm_reg;
Philipp Zabel37f85b262013-06-21 14:52:18 +02001021 gc->unused = unused[i / 32];
Philipp Zabel379cdec2013-06-21 14:52:17 +02001022 ct = gc->chip_types;
1023 ct->chip.irq_ack = irq_gc_ack_set_bit;
1024 ct->chip.irq_mask = irq_gc_mask_clr_bit;
1025 ct->chip.irq_unmask = irq_gc_mask_set_bit;
1026 ct->regs.ack = IPU_INT_STAT(i / 32);
1027 ct->regs.mask = IPU_INT_CTRL(i / 32);
1028 }
1029
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001030 irq_set_chained_handler(ipu->irq_sync, ipu_irq_handler);
1031 irq_set_handler_data(ipu->irq_sync, ipu);
1032 irq_set_chained_handler(ipu->irq_err, ipu_err_irq_handler);
1033 irq_set_handler_data(ipu->irq_err, ipu);
1034
1035 return 0;
1036}
1037
1038static void ipu_irq_exit(struct ipu_soc *ipu)
1039{
Philipp Zabelb7287662013-06-21 10:27:39 +02001040 int i, irq;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001041
1042 irq_set_chained_handler(ipu->irq_err, NULL);
1043 irq_set_handler_data(ipu->irq_err, NULL);
1044 irq_set_chained_handler(ipu->irq_sync, NULL);
1045 irq_set_handler_data(ipu->irq_sync, NULL);
1046
Philipp Zabel379cdec2013-06-21 14:52:17 +02001047 /* TODO: remove irq_domain_generic_chips */
1048
Philipp Zabelb7287662013-06-21 10:27:39 +02001049 for (i = 0; i < IPU_NUM_IRQS; i++) {
1050 irq = irq_linear_revmap(ipu->domain, i);
1051 if (irq)
1052 irq_dispose_mapping(irq);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001053 }
1054
Philipp Zabelb7287662013-06-21 10:27:39 +02001055 irq_domain_remove(ipu->domain);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001056}
1057
Bill Pembertonc4aabf82012-11-19 13:22:11 -05001058static int ipu_probe(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001059{
1060 const struct of_device_id *of_id =
1061 of_match_device(imx_ipu_dt_ids, &pdev->dev);
1062 struct ipu_soc *ipu;
1063 struct resource *res;
1064 unsigned long ipu_base;
1065 int i, ret, irq_sync, irq_err;
1066 const struct ipu_devtype *devtype;
1067
1068 devtype = of_id->data;
1069
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001070 irq_sync = platform_get_irq(pdev, 0);
1071 irq_err = platform_get_irq(pdev, 1);
1072 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1073
Fabio Estevamfd563db2012-10-24 21:36:46 -02001074 dev_dbg(&pdev->dev, "irq_sync: %d irq_err: %d\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001075 irq_sync, irq_err);
1076
1077 if (!res || irq_sync < 0 || irq_err < 0)
1078 return -ENODEV;
1079
1080 ipu_base = res->start;
1081
1082 ipu = devm_kzalloc(&pdev->dev, sizeof(*ipu), GFP_KERNEL);
1083 if (!ipu)
1084 return -ENODEV;
1085
1086 for (i = 0; i < 64; i++)
1087 ipu->channel[i].ipu = ipu;
1088 ipu->devtype = devtype;
1089 ipu->ipu_type = devtype->type;
1090
1091 spin_lock_init(&ipu->lock);
1092 mutex_init(&ipu->channel_lock);
1093
Fabio Estevamfd563db2012-10-24 21:36:46 -02001094 dev_dbg(&pdev->dev, "cm_reg: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001095 ipu_base + devtype->cm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001096 dev_dbg(&pdev->dev, "idmac: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001097 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001098 dev_dbg(&pdev->dev, "cpmem: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001099 ipu_base + devtype->cpmem_ofs);
Steve Longerbeam2ffd48f2014-08-19 10:52:40 -07001100 dev_dbg(&pdev->dev, "csi0: 0x%08lx\n",
1101 ipu_base + devtype->csi0_ofs);
1102 dev_dbg(&pdev->dev, "csi1: 0x%08lx\n",
1103 ipu_base + devtype->csi1_ofs);
Steve Longerbeam1aa8ea02014-08-11 13:04:50 +02001104 dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
1105 ipu_base + devtype->ic_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001106 dev_dbg(&pdev->dev, "disp0: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001107 ipu_base + devtype->disp0_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001108 dev_dbg(&pdev->dev, "disp1: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001109 ipu_base + devtype->disp1_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001110 dev_dbg(&pdev->dev, "srm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001111 ipu_base + devtype->srm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001112 dev_dbg(&pdev->dev, "tpm: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001113 ipu_base + devtype->tpm_ofs);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001114 dev_dbg(&pdev->dev, "dc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001115 ipu_base + devtype->cm_ofs + IPU_CM_DC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001116 dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001117 ipu_base + devtype->cm_ofs + IPU_CM_IC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001118 dev_dbg(&pdev->dev, "dmfc: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001119 ipu_base + devtype->cm_ofs + IPU_CM_DMFC_REG_OFS);
Fabio Estevamfd563db2012-10-24 21:36:46 -02001120 dev_dbg(&pdev->dev, "vdi: 0x%08lx\n",
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001121 ipu_base + devtype->vdi_ofs);
1122
1123 ipu->cm_reg = devm_ioremap(&pdev->dev,
1124 ipu_base + devtype->cm_ofs, PAGE_SIZE);
1125 ipu->idmac_reg = devm_ioremap(&pdev->dev,
1126 ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS,
1127 PAGE_SIZE);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001128
Steve Longerbeam7d2691d2014-06-25 18:05:47 -07001129 if (!ipu->cm_reg || !ipu->idmac_reg)
Fabio Estevambe798b22013-07-20 18:22:09 -03001130 return -ENOMEM;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001131
1132 ipu->clk = devm_clk_get(&pdev->dev, "bus");
1133 if (IS_ERR(ipu->clk)) {
1134 ret = PTR_ERR(ipu->clk);
1135 dev_err(&pdev->dev, "clk_get failed with %d", ret);
Fabio Estevambe798b22013-07-20 18:22:09 -03001136 return ret;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001137 }
1138
1139 platform_set_drvdata(pdev, ipu);
1140
Fabio Estevam62645a22013-07-20 18:22:10 -03001141 ret = clk_prepare_enable(ipu->clk);
1142 if (ret) {
1143 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1144 return ret;
1145 }
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001146
1147 ipu->dev = &pdev->dev;
1148 ipu->irq_sync = irq_sync;
1149 ipu->irq_err = irq_err;
1150
1151 ret = ipu_irq_init(ipu);
1152 if (ret)
1153 goto out_failed_irq;
1154
Philipp Zabel6c641552013-03-28 17:35:21 +01001155 ret = device_reset(&pdev->dev);
1156 if (ret) {
1157 dev_err(&pdev->dev, "failed to reset: %d\n", ret);
1158 goto out_failed_reset;
1159 }
1160 ret = ipu_memory_reset(ipu);
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001161 if (ret)
1162 goto out_failed_reset;
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001163
1164 /* Set MCU_T to divide MCU access window into 2 */
1165 ipu_cm_write(ipu, 0x00400000L | (IPU_MCU_T_DEFAULT << 18),
1166 IPU_DISP_GEN);
1167
1168 ret = ipu_submodules_init(ipu, pdev, ipu_base, ipu->clk);
1169 if (ret)
1170 goto failed_submodules_init;
1171
Philipp Zabeld6ca8ca2012-05-23 17:08:19 +02001172 ret = ipu_add_client_devices(ipu, ipu_base);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001173 if (ret) {
1174 dev_err(&pdev->dev, "adding client devices failed with %d\n",
1175 ret);
1176 goto failed_add_clients;
1177 }
1178
Fabio Estevam9c2c438c2012-10-24 21:36:47 -02001179 dev_info(&pdev->dev, "%s probed\n", devtype->name);
1180
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001181 return 0;
1182
1183failed_add_clients:
1184 ipu_submodules_exit(ipu);
1185failed_submodules_init:
Lothar Waßmann4d27b2c2012-12-25 15:58:37 +01001186out_failed_reset:
Philipp Zabel6c641552013-03-28 17:35:21 +01001187 ipu_irq_exit(ipu);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001188out_failed_irq:
1189 clk_disable_unprepare(ipu->clk);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001190 return ret;
1191}
1192
Bill Pemberton8aa1be42012-11-19 13:26:38 -05001193static int ipu_remove(struct platform_device *pdev)
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001194{
1195 struct ipu_soc *ipu = platform_get_drvdata(pdev);
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001196
1197 platform_device_unregister_children(pdev);
1198 ipu_submodules_exit(ipu);
1199 ipu_irq_exit(ipu);
1200
1201 clk_disable_unprepare(ipu->clk);
1202
1203 return 0;
1204}
1205
1206static struct platform_driver imx_ipu_driver = {
1207 .driver = {
1208 .name = "imx-ipuv3",
1209 .of_match_table = imx_ipu_dt_ids,
1210 },
1211 .probe = ipu_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -05001212 .remove = ipu_remove,
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001213};
1214
1215module_platform_driver(imx_ipu_driver);
1216
Fabio Estevam10f22682013-07-20 18:22:11 -03001217MODULE_ALIAS("platform:imx-ipuv3");
Sascha Haueraecfbdb2012-09-21 10:07:49 +02001218MODULE_DESCRIPTION("i.MX IPU v3 driver");
1219MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1220MODULE_LICENSE("GPL");