1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PiSP Back End driver.
4 * Copyright (c) 2021-2024 Raspberry Pi Limited.
5 *
6 */
7 #include <linux/clk.h>
8 #include <linux/interrupt.h>
9 #include <linux/io.h>
10 #include <linux/kernel.h>
11 #include <linux/lockdep.h>
12 #include <linux/minmax.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-ioctl.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <media/videobuf2-vmalloc.h>
21
22 #include <uapi/linux/media/raspberrypi/pisp_be_config.h>
23
24 #include "pisp_be_formats.h"
25
26 /* Maximum number of config buffers possible */
27 #define PISP_BE_NUM_CONFIG_BUFFERS VB2_MAX_FRAME
28
29 #define PISPBE_NAME "pispbe"
30
31 /* Some ISP-BE registers */
32 #define PISP_BE_VERSION_REG 0x0
33 #define PISP_BE_CONTROL_REG 0x4
34 #define PISP_BE_CONTROL_COPY_CONFIG BIT(1)
35 #define PISP_BE_CONTROL_QUEUE_JOB BIT(0)
36 #define PISP_BE_CONTROL_NUM_TILES(n) ((n) << 16)
37 #define PISP_BE_TILE_ADDR_LO_REG 0x8
38 #define PISP_BE_TILE_ADDR_HI_REG 0xc
39 #define PISP_BE_STATUS_REG 0x10
40 #define PISP_BE_STATUS_QUEUED BIT(0)
41 #define PISP_BE_BATCH_STATUS_REG 0x14
42 #define PISP_BE_INTERRUPT_EN_REG 0x18
43 #define PISP_BE_INTERRUPT_STATUS_REG 0x1c
44 #define PISP_BE_AXI_REG 0x20
45 #define PISP_BE_CONFIG_BASE_REG 0x40
46 #define PISP_BE_IO_ADDR_LOW(n) (PISP_BE_CONFIG_BASE_REG + 8 * (n))
47 #define PISP_BE_IO_ADDR_HIGH(n) (PISP_BE_IO_ADDR_LOW((n)) + 4)
48 #define PISP_BE_GLOBAL_BAYER_ENABLE 0xb0
49 #define PISP_BE_GLOBAL_RGB_ENABLE 0xb4
50 #define N_HW_ADDRESSES 13
51 #define N_HW_ENABLES 2
52
53 #define PISP_BE_VERSION_2712 0x02252700
54 #define PISP_BE_VERSION_MINOR_BITS 0xf
55
56 /*
57 * This maps our nodes onto the inputs/outputs of the actual PiSP Back End.
58 * Be wary of the word "OUTPUT" which is used ambiguously here. In a V4L2
59 * context it means an input to the hardware (source image or metadata).
60 * Elsewhere it means an output from the hardware.
61 */
62 enum pispbe_node_ids {
63 MAIN_INPUT_NODE,
64 TDN_INPUT_NODE,
65 STITCH_INPUT_NODE,
66 OUTPUT0_NODE,
67 OUTPUT1_NODE,
68 TDN_OUTPUT_NODE,
69 STITCH_OUTPUT_NODE,
70 CONFIG_NODE,
71 PISPBE_NUM_NODES
72 };
73
74 struct pispbe_node_description {
75 const char *ent_name;
76 enum v4l2_buf_type buf_type;
77 unsigned int caps;
78 };
79
80 static const struct pispbe_node_description node_desc[PISPBE_NUM_NODES] = {
81 /* MAIN_INPUT_NODE */
82 {
83 .ent_name = PISPBE_NAME "-input",
84 .buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
85 .caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE,
86 },
87 /* TDN_INPUT_NODE */
88 {
89 .ent_name = PISPBE_NAME "-tdn_input",
90 .buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
91 .caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE,
92 },
93 /* STITCH_INPUT_NODE */
94 {
95 .ent_name = PISPBE_NAME "-stitch_input",
96 .buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
97 .caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE,
98 },
99 /* OUTPUT0_NODE */
100 {
101 .ent_name = PISPBE_NAME "-output0",
102 .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
103 .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
104 },
105 /* OUTPUT1_NODE */
106 {
107 .ent_name = PISPBE_NAME "-output1",
108 .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
109 .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
110 },
111 /* TDN_OUTPUT_NODE */
112 {
113 .ent_name = PISPBE_NAME "-tdn_output",
114 .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
115 .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
116 },
117 /* STITCH_OUTPUT_NODE */
118 {
119 .ent_name = PISPBE_NAME "-stitch_output",
120 .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
121 .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
122 },
123 /* CONFIG_NODE */
124 {
125 .ent_name = PISPBE_NAME "-config",
126 .buf_type = V4L2_BUF_TYPE_META_OUTPUT,
127 .caps = V4L2_CAP_META_OUTPUT,
128 }
129 };
130
131 #define NODE_DESC_IS_OUTPUT(desc) ( \
132 ((desc)->buf_type == V4L2_BUF_TYPE_META_OUTPUT) || \
133 ((desc)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT) || \
134 ((desc)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
135
136 #define NODE_IS_META(node) ( \
137 ((node)->buf_type == V4L2_BUF_TYPE_META_OUTPUT))
138 #define NODE_IS_OUTPUT(node) ( \
139 ((node)->buf_type == V4L2_BUF_TYPE_META_OUTPUT) || \
140 ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT) || \
141 ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
142 #define NODE_IS_CAPTURE(node) ( \
143 ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) || \
144 ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
145 #define NODE_IS_MPLANE(node) ( \
146 ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) || \
147 ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
148
149 /*
150 * Structure to describe a single node /dev/video<N> which represents a single
151 * input or output queue to the PiSP Back End device.
152 */
153 struct pispbe_node {
154 unsigned int id;
155 int vfl_dir;
156 enum v4l2_buf_type buf_type;
157 struct video_device vfd;
158 struct media_pad pad;
159 struct media_intf_devnode *intf_devnode;
160 struct media_link *intf_link;
161 struct pispbe_dev *pispbe;
162 /* Video device lock */
163 struct mutex node_lock;
164 /* vb2_queue lock */
165 struct mutex queue_lock;
166 struct list_head ready_queue;
167 struct vb2_queue queue;
168 struct v4l2_format format;
169 const struct pisp_be_format *pisp_format;
170 };
171
172 /* For logging only, use the entity name with "pispbe" and separator removed */
173 #define NODE_NAME(node) \
174 (node_desc[(node)->id].ent_name + sizeof(PISPBE_NAME))
175
176 /* Records details of the jobs currently running or queued on the h/w. */
177 struct pispbe_job {
178 bool valid;
179 /*
180 * An array of buffer pointers - remember it's source buffers first,
181 * then captures, then metadata last.
182 */
183 struct pispbe_buffer *buf[PISPBE_NUM_NODES];
184 };
185
186 struct pispbe_hw_enables {
187 u32 bayer_enables;
188 u32 rgb_enables;
189 };
190
191 /* Records a job configuration and memory addresses. */
192 struct pispbe_job_descriptor {
193 struct list_head queue;
194 struct pispbe_buffer *buffers[PISPBE_NUM_NODES];
195 dma_addr_t hw_dma_addrs[N_HW_ADDRESSES];
196 struct pisp_be_tiles_config *config;
197 struct pispbe_hw_enables hw_enables;
198 dma_addr_t tiles;
199 };
200
201 /*
202 * Structure representing the entire PiSP Back End device, comprising several
203 * nodes which share platform resources and a mutex for the actual HW.
204 */
205 struct pispbe_dev {
206 struct device *dev;
207 struct pispbe_dev *pispbe;
208 struct pisp_be_tiles_config *config;
209 void __iomem *be_reg_base;
210 struct clk *clk;
211 struct v4l2_device v4l2_dev;
212 struct v4l2_subdev sd;
213 struct media_device mdev;
214 struct media_pad pad[PISPBE_NUM_NODES]; /* output pads first */
215 struct pispbe_node node[PISPBE_NUM_NODES];
216 dma_addr_t config_dma_addr;
217 unsigned int sequence;
218 u32 streaming_map;
219 struct pispbe_job queued_job, running_job;
220 /* protects "hw_busy" flag, streaming_map and job_queue */
221 spinlock_t hw_lock;
222 bool hw_busy; /* non-zero if a job is queued or is being started */
223 struct list_head job_queue;
224 int irq;
225 u32 hw_version;
226 u8 done, started;
227 };
228
pispbe_rd(struct pispbe_dev * pispbe,unsigned int offset)229 static u32 pispbe_rd(struct pispbe_dev *pispbe, unsigned int offset)
230 {
231 return readl(pispbe->be_reg_base + offset);
232 }
233
pispbe_wr(struct pispbe_dev * pispbe,unsigned int offset,u32 val)234 static void pispbe_wr(struct pispbe_dev *pispbe, unsigned int offset, u32 val)
235 {
236 writel(val, pispbe->be_reg_base + offset);
237 }
238
239 /*
240 * Queue a job to the h/w. If the h/w is idle it will begin immediately.
241 * Caller must ensure it is "safe to queue", i.e. we don't already have a
242 * queued, unstarted job.
243 */
pispbe_queue_job(struct pispbe_dev * pispbe,struct pispbe_job_descriptor * job)244 static void pispbe_queue_job(struct pispbe_dev *pispbe,
245 struct pispbe_job_descriptor *job)
246 {
247 unsigned int begin, end;
248
249 if (pispbe_rd(pispbe, PISP_BE_STATUS_REG) & PISP_BE_STATUS_QUEUED)
250 dev_err(pispbe->dev, "ERROR: not safe to queue new job!\n");
251
252 /*
253 * Write configuration to hardware. DMA addresses and enable flags
254 * are passed separately, because the driver needs to sanitize them,
255 * and we don't want to modify (or be vulnerable to modifications of)
256 * the mmap'd buffer.
257 */
258 for (unsigned int u = 0; u < N_HW_ADDRESSES; ++u) {
259 pispbe_wr(pispbe, PISP_BE_IO_ADDR_LOW(u),
260 lower_32_bits(job->hw_dma_addrs[u]));
261 pispbe_wr(pispbe, PISP_BE_IO_ADDR_HIGH(u),
262 upper_32_bits(job->hw_dma_addrs[u]));
263 }
264 pispbe_wr(pispbe, PISP_BE_GLOBAL_BAYER_ENABLE,
265 job->hw_enables.bayer_enables);
266 pispbe_wr(pispbe, PISP_BE_GLOBAL_RGB_ENABLE,
267 job->hw_enables.rgb_enables);
268
269 /* Everything else is as supplied by the user. */
270 begin = offsetof(struct pisp_be_config, global.bayer_order) /
271 sizeof(u32);
272 end = sizeof(struct pisp_be_config) / sizeof(u32);
273 for (unsigned int u = begin; u < end; u++)
274 pispbe_wr(pispbe, PISP_BE_CONFIG_BASE_REG + sizeof(u32) * u,
275 ((u32 *)job->config)[u]);
276
277 /* Read back the addresses -- an error here could be fatal */
278 for (unsigned int u = 0; u < N_HW_ADDRESSES; ++u) {
279 unsigned int offset = PISP_BE_IO_ADDR_LOW(u);
280 u64 along = pispbe_rd(pispbe, offset);
281
282 along += ((u64)pispbe_rd(pispbe, offset + 4)) << 32;
283 if (along != (u64)(job->hw_dma_addrs[u])) {
284 dev_dbg(pispbe->dev,
285 "ISP BE config error: check if ISP RAMs enabled?\n");
286 return;
287 }
288 }
289
290 /*
291 * Write tile pointer to hardware. The IOMMU should prevent
292 * out-of-bounds offsets reaching non-ISP buffers.
293 */
294 pispbe_wr(pispbe, PISP_BE_TILE_ADDR_LO_REG, lower_32_bits(job->tiles));
295 pispbe_wr(pispbe, PISP_BE_TILE_ADDR_HI_REG, upper_32_bits(job->tiles));
296
297 /* Enqueue the job */
298 pispbe_wr(pispbe, PISP_BE_CONTROL_REG,
299 PISP_BE_CONTROL_COPY_CONFIG | PISP_BE_CONTROL_QUEUE_JOB |
300 PISP_BE_CONTROL_NUM_TILES(job->config->num_tiles));
301 }
302
303 struct pispbe_buffer {
304 struct vb2_v4l2_buffer vb;
305 struct list_head ready_list;
306 unsigned int config_index;
307 };
308
pispbe_get_planes_addr(dma_addr_t addr[3],struct pispbe_buffer * buf,struct pispbe_node * node)309 static int pispbe_get_planes_addr(dma_addr_t addr[3], struct pispbe_buffer *buf,
310 struct pispbe_node *node)
311 {
312 unsigned int num_planes = node->format.fmt.pix_mp.num_planes;
313 unsigned int plane_factor = 0;
314 unsigned int size;
315 unsigned int p;
316
317 if (!buf || !node->pisp_format)
318 return 0;
319
320 /*
321 * Determine the base plane size. This will not be the same
322 * as node->format.fmt.pix_mp.plane_fmt[0].sizeimage for a single
323 * plane buffer in an mplane format.
324 */
325 size = node->format.fmt.pix_mp.plane_fmt[0].bytesperline *
326 node->format.fmt.pix_mp.height;
327
328 for (p = 0; p < num_planes && p < PISPBE_MAX_PLANES; p++) {
329 addr[p] = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, p);
330 plane_factor += node->pisp_format->plane_factor[p];
331 }
332
333 for (; p < PISPBE_MAX_PLANES && node->pisp_format->plane_factor[p]; p++) {
334 /*
335 * Calculate the address offset of this plane as needed
336 * by the hardware. This is specifically for non-mplane
337 * buffer formats, where there are 3 image planes, e.g.
338 * for the V4L2_PIX_FMT_YUV420 format.
339 */
340 addr[p] = addr[0] + ((size * plane_factor) >> 3);
341 plane_factor += node->pisp_format->plane_factor[p];
342 }
343
344 return num_planes;
345 }
346
pispbe_get_addr(struct pispbe_buffer * buf)347 static dma_addr_t pispbe_get_addr(struct pispbe_buffer *buf)
348 {
349 if (buf)
350 return vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
351
352 return 0;
353 }
354
pispbe_xlate_addrs(struct pispbe_dev * pispbe,struct pispbe_job_descriptor * job,struct pispbe_buffer * buf[PISPBE_NUM_NODES])355 static void pispbe_xlate_addrs(struct pispbe_dev *pispbe,
356 struct pispbe_job_descriptor *job,
357 struct pispbe_buffer *buf[PISPBE_NUM_NODES])
358 {
359 struct pispbe_hw_enables *hw_en = &job->hw_enables;
360 struct pisp_be_tiles_config *config = job->config;
361 dma_addr_t *addrs = job->hw_dma_addrs;
362 int ret;
363
364 /* Take a copy of the "enable" bitmaps so we can modify them. */
365 hw_en->bayer_enables = config->config.global.bayer_enables;
366 hw_en->rgb_enables = config->config.global.rgb_enables;
367
368 /*
369 * Main input first. There are 3 address pointers, corresponding to up
370 * to 3 planes.
371 */
372 ret = pispbe_get_planes_addr(addrs, buf[MAIN_INPUT_NODE],
373 &pispbe->node[MAIN_INPUT_NODE]);
374 if (ret <= 0) {
375 /* Shouldn't happen, we have validated an input is available. */
376 dev_warn(pispbe->dev, "ISP-BE missing input\n");
377 hw_en->bayer_enables = 0;
378 hw_en->rgb_enables = 0;
379 return;
380 }
381
382 /*
383 * Now TDN/Stitch inputs and outputs. These are single-plane and only
384 * used with Bayer input. Input enables must match the requirements
385 * of the processing stages, otherwise the hardware can lock up!
386 */
387 if (hw_en->bayer_enables & PISP_BE_BAYER_ENABLE_INPUT) {
388 addrs[3] = pispbe_get_addr(buf[TDN_INPUT_NODE]);
389 if (addrs[3] == 0 ||
390 !(hw_en->bayer_enables & PISP_BE_BAYER_ENABLE_TDN_INPUT) ||
391 !(hw_en->bayer_enables & PISP_BE_BAYER_ENABLE_TDN) ||
392 (config->config.tdn.reset & 1)) {
393 hw_en->bayer_enables &=
394 ~(PISP_BE_BAYER_ENABLE_TDN_INPUT |
395 PISP_BE_BAYER_ENABLE_TDN_DECOMPRESS);
396 if (!(config->config.tdn.reset & 1))
397 hw_en->bayer_enables &=
398 ~PISP_BE_BAYER_ENABLE_TDN;
399 }
400
401 addrs[4] = pispbe_get_addr(buf[STITCH_INPUT_NODE]);
402 if (addrs[4] == 0 ||
403 !(hw_en->bayer_enables & PISP_BE_BAYER_ENABLE_STITCH_INPUT) ||
404 !(hw_en->bayer_enables & PISP_BE_BAYER_ENABLE_STITCH)) {
405 hw_en->bayer_enables &=
406 ~(PISP_BE_BAYER_ENABLE_STITCH_INPUT |
407 PISP_BE_BAYER_ENABLE_STITCH_DECOMPRESS |
408 PISP_BE_BAYER_ENABLE_STITCH);
409 }
410
411 addrs[5] = pispbe_get_addr(buf[TDN_OUTPUT_NODE]);
412 if (addrs[5] == 0)
413 hw_en->bayer_enables &=
414 ~(PISP_BE_BAYER_ENABLE_TDN_COMPRESS |
415 PISP_BE_BAYER_ENABLE_TDN_OUTPUT);
416
417 addrs[6] = pispbe_get_addr(buf[STITCH_OUTPUT_NODE]);
418 if (addrs[6] == 0)
419 hw_en->bayer_enables &=
420 ~(PISP_BE_BAYER_ENABLE_STITCH_COMPRESS |
421 PISP_BE_BAYER_ENABLE_STITCH_OUTPUT);
422 } else {
423 /* No Bayer input? Disable entire Bayer pipe (else lockup) */
424 hw_en->bayer_enables = 0;
425 }
426
427 /* Main image output channels. */
428 for (unsigned int i = 0; i < PISP_BACK_END_NUM_OUTPUTS; i++) {
429 ret = pispbe_get_planes_addr(addrs + 7 + 3 * i,
430 buf[OUTPUT0_NODE + i],
431 &pispbe->node[OUTPUT0_NODE + i]);
432 if (ret <= 0)
433 hw_en->rgb_enables &= ~(PISP_BE_RGB_ENABLE_OUTPUT0 << i);
434 }
435 }
436
437 /*
438 * Prepare a job description to be submitted to the HW.
439 *
440 * To schedule a job, we need all streaming nodes (apart from Output0,
441 * Output1, Tdn and Stitch) to have a buffer ready, which must
442 * include at least a config buffer and a main input image.
443 *
444 * For Output0, Output1, Tdn and Stitch, a buffer only needs to be
445 * available if the blocks are enabled in the config.
446 *
447 * If all the buffers required to form a job are available, append the
448 * job descriptor to the job queue to be later queued to the HW.
449 *
450 * Returns 0 if a job has been successfully prepared, < 0 otherwise.
451 */
pispbe_prepare_job(struct pispbe_dev * pispbe)452 static int pispbe_prepare_job(struct pispbe_dev *pispbe)
453 {
454 struct pispbe_job_descriptor __free(kfree) *job = NULL;
455 struct pispbe_buffer *buf[PISPBE_NUM_NODES] = {};
456 unsigned int streaming_map;
457 unsigned int config_index;
458 struct pispbe_node *node;
459
460 lockdep_assert_irqs_enabled();
461
462 scoped_guard(spinlock_irq, &pispbe->hw_lock) {
463 static const u32 mask = BIT(CONFIG_NODE) | BIT(MAIN_INPUT_NODE);
464
465 if ((pispbe->streaming_map & mask) != mask)
466 return -ENODEV;
467
468 /*
469 * Take a copy of streaming_map: nodes activated after this
470 * point are ignored when preparing this job.
471 */
472 streaming_map = pispbe->streaming_map;
473 }
474
475 job = kzalloc(sizeof(*job), GFP_KERNEL);
476 if (!job)
477 return -ENOMEM;
478
479 node = &pispbe->node[CONFIG_NODE];
480 buf[CONFIG_NODE] = list_first_entry_or_null(&node->ready_queue,
481 struct pispbe_buffer,
482 ready_list);
483 if (!buf[CONFIG_NODE])
484 return -ENODEV;
485
486 list_del(&buf[CONFIG_NODE]->ready_list);
487 job->buffers[CONFIG_NODE] = buf[CONFIG_NODE];
488
489 config_index = buf[CONFIG_NODE]->vb.vb2_buf.index;
490 job->config = &pispbe->config[config_index];
491 job->tiles = pispbe->config_dma_addr +
492 config_index * sizeof(struct pisp_be_tiles_config) +
493 offsetof(struct pisp_be_tiles_config, tiles);
494
495 /* remember: srcimages, captures then metadata */
496 for (unsigned int i = 0; i < PISPBE_NUM_NODES; i++) {
497 unsigned int bayer_en =
498 job->config->config.global.bayer_enables;
499 unsigned int rgb_en =
500 job->config->config.global.rgb_enables;
501 bool ignore_buffers = false;
502
503 /* Config node is handled outside the loop above. */
504 if (i == CONFIG_NODE)
505 continue;
506
507 buf[i] = NULL;
508 if (!(streaming_map & BIT(i)))
509 continue;
510
511 if ((!(rgb_en & PISP_BE_RGB_ENABLE_OUTPUT0) &&
512 i == OUTPUT0_NODE) ||
513 (!(rgb_en & PISP_BE_RGB_ENABLE_OUTPUT1) &&
514 i == OUTPUT1_NODE) ||
515 (!(bayer_en & PISP_BE_BAYER_ENABLE_TDN_INPUT) &&
516 i == TDN_INPUT_NODE) ||
517 (!(bayer_en & PISP_BE_BAYER_ENABLE_TDN_OUTPUT) &&
518 i == TDN_OUTPUT_NODE) ||
519 (!(bayer_en & PISP_BE_BAYER_ENABLE_STITCH_INPUT) &&
520 i == STITCH_INPUT_NODE) ||
521 (!(bayer_en & PISP_BE_BAYER_ENABLE_STITCH_OUTPUT) &&
522 i == STITCH_OUTPUT_NODE)) {
523 /*
524 * Ignore Output0/Output1/Tdn/Stitch buffer check if the
525 * global enables aren't set for these blocks. If a
526 * buffer has been provided, we dequeue it back to the
527 * user with the other in-use buffers.
528 */
529 ignore_buffers = true;
530 }
531
532 node = &pispbe->node[i];
533
534 /* Pull a buffer from each V4L2 queue to form the queued job */
535 buf[i] = list_first_entry_or_null(&node->ready_queue,
536 struct pispbe_buffer,
537 ready_list);
538 if (buf[i]) {
539 list_del(&buf[i]->ready_list);
540 job->buffers[i] = buf[i];
541 }
542
543 if (!buf[i] && !ignore_buffers)
544 goto err_return_buffers;
545 }
546
547 /* Convert buffers to DMA addresses for the hardware */
548 pispbe_xlate_addrs(pispbe, job, buf);
549
550 scoped_guard(spinlock_irq, &pispbe->hw_lock) {
551 list_add_tail(&job->queue, &pispbe->job_queue);
552 }
553
554 /* Set job to NULL to avoid automatic release due to __free(). */
555 job = NULL;
556
557 return 0;
558
559 err_return_buffers:
560 for (unsigned int i = 0; i < PISPBE_NUM_NODES; i++) {
561 struct pispbe_node *n = &pispbe->node[i];
562
563 if (!buf[i])
564 continue;
565
566 /* Return the buffer to the ready_list queue */
567 list_add(&buf[i]->ready_list, &n->ready_queue);
568 }
569
570 return -ENODEV;
571 }
572
pispbe_schedule(struct pispbe_dev * pispbe,bool clear_hw_busy)573 static void pispbe_schedule(struct pispbe_dev *pispbe, bool clear_hw_busy)
574 {
575 struct pispbe_job_descriptor *job;
576
577 scoped_guard(spinlock_irqsave, &pispbe->hw_lock) {
578 if (clear_hw_busy)
579 pispbe->hw_busy = false;
580
581 if (pispbe->hw_busy)
582 return;
583
584 job = list_first_entry_or_null(&pispbe->job_queue,
585 struct pispbe_job_descriptor,
586 queue);
587 if (!job)
588 return;
589
590 list_del(&job->queue);
591
592 for (unsigned int i = 0; i < PISPBE_NUM_NODES; i++)
593 pispbe->queued_job.buf[i] = job->buffers[i];
594 pispbe->queued_job.valid = true;
595
596 pispbe->hw_busy = true;
597 }
598
599 /*
600 * We can kick the job off without the hw_lock, as this can
601 * never run again until hw_busy is cleared, which will happen
602 * only when the following job has been queued and an interrupt
603 * is rised.
604 */
605 pispbe_queue_job(pispbe, job);
606 kfree(job);
607 }
608
pispbe_isr_jobdone(struct pispbe_dev * pispbe,struct pispbe_job * job)609 static void pispbe_isr_jobdone(struct pispbe_dev *pispbe,
610 struct pispbe_job *job)
611 {
612 struct pispbe_buffer **buf = job->buf;
613 u64 ts = ktime_get_ns();
614
615 for (unsigned int i = 0; i < PISPBE_NUM_NODES; i++) {
616 if (buf[i]) {
617 buf[i]->vb.vb2_buf.timestamp = ts;
618 buf[i]->vb.sequence = pispbe->sequence;
619 vb2_buffer_done(&buf[i]->vb.vb2_buf,
620 VB2_BUF_STATE_DONE);
621 }
622 }
623
624 pispbe->sequence++;
625 }
626
pispbe_isr(int irq,void * dev)627 static irqreturn_t pispbe_isr(int irq, void *dev)
628 {
629 struct pispbe_dev *pispbe = (struct pispbe_dev *)dev;
630 bool can_queue_another = false;
631 u8 started, done;
632 u32 u;
633
634 u = pispbe_rd(pispbe, PISP_BE_INTERRUPT_STATUS_REG);
635 if (u == 0)
636 return IRQ_NONE;
637
638 pispbe_wr(pispbe, PISP_BE_INTERRUPT_STATUS_REG, u);
639 u = pispbe_rd(pispbe, PISP_BE_BATCH_STATUS_REG);
640 done = (uint8_t)u;
641 started = (uint8_t)(u >> 8);
642
643 /*
644 * Be aware that done can go up by 2 and started by 1 when: a job that
645 * we previously saw "start" now finishes, and we then queued a new job
646 * which we see both start and finish "simultaneously".
647 */
648 if (pispbe->running_job.valid && pispbe->done != done) {
649 pispbe_isr_jobdone(pispbe, &pispbe->running_job);
650 memset(&pispbe->running_job, 0, sizeof(pispbe->running_job));
651 pispbe->done++;
652 }
653
654 if (pispbe->started != started) {
655 pispbe->started++;
656 can_queue_another = 1;
657
658 if (pispbe->done != done && pispbe->queued_job.valid) {
659 pispbe_isr_jobdone(pispbe, &pispbe->queued_job);
660 pispbe->done++;
661 } else {
662 pispbe->running_job = pispbe->queued_job;
663 }
664
665 memset(&pispbe->queued_job, 0, sizeof(pispbe->queued_job));
666 }
667
668 if (pispbe->done != done || pispbe->started != started) {
669 dev_dbg(pispbe->dev,
670 "Job counters not matching: done = %u, expected %u - started = %u, expected %u\n",
671 pispbe->done, done, pispbe->started, started);
672 pispbe->started = started;
673 pispbe->done = done;
674 }
675
676 /* check if there's more to do before going to sleep */
677 pispbe_schedule(pispbe, can_queue_another);
678
679 return IRQ_HANDLED;
680 }
681
pisp_be_validate_config(struct pispbe_dev * pispbe,struct pisp_be_tiles_config * config)682 static int pisp_be_validate_config(struct pispbe_dev *pispbe,
683 struct pisp_be_tiles_config *config)
684 {
685 u32 bayer_enables = config->config.global.bayer_enables;
686 u32 rgb_enables = config->config.global.rgb_enables;
687 struct device *dev = pispbe->dev;
688 struct v4l2_format *fmt;
689 unsigned int bpl, size;
690
691 if (!(bayer_enables & PISP_BE_BAYER_ENABLE_INPUT) ==
692 !(rgb_enables & PISP_BE_RGB_ENABLE_INPUT)) {
693 dev_dbg(dev, "%s: Not one input enabled\n", __func__);
694 return -EIO;
695 }
696
697 if (config->num_tiles == 0 ||
698 config->num_tiles > PISP_BACK_END_NUM_TILES) {
699 dev_dbg(dev, "%s: Invalid number of tiles: %d\n", __func__,
700 config->num_tiles);
701 return -EINVAL;
702 }
703
704 /* Ensure output config strides and buffer sizes match the V4L2 formats. */
705 fmt = &pispbe->node[TDN_OUTPUT_NODE].format;
706 if (bayer_enables & PISP_BE_BAYER_ENABLE_TDN_OUTPUT) {
707 bpl = config->config.tdn_output_format.stride;
708 size = bpl * config->config.tdn_output_format.height;
709
710 if (fmt->fmt.pix_mp.plane_fmt[0].bytesperline < bpl) {
711 dev_dbg(dev, "%s: bpl mismatch on tdn_output\n",
712 __func__);
713 return -EINVAL;
714 }
715
716 if (fmt->fmt.pix_mp.plane_fmt[0].sizeimage < size) {
717 dev_dbg(dev, "%s: size mismatch on tdn_output\n",
718 __func__);
719 return -EINVAL;
720 }
721 }
722
723 fmt = &pispbe->node[STITCH_OUTPUT_NODE].format;
724 if (bayer_enables & PISP_BE_BAYER_ENABLE_STITCH_OUTPUT) {
725 bpl = config->config.stitch_output_format.stride;
726 size = bpl * config->config.stitch_output_format.height;
727
728 if (fmt->fmt.pix_mp.plane_fmt[0].bytesperline < bpl) {
729 dev_dbg(dev, "%s: bpl mismatch on stitch_output\n",
730 __func__);
731 return -EINVAL;
732 }
733
734 if (fmt->fmt.pix_mp.plane_fmt[0].sizeimage < size) {
735 dev_dbg(dev, "%s: size mismatch on stitch_output\n",
736 __func__);
737 return -EINVAL;
738 }
739 }
740
741 for (unsigned int j = 0; j < PISP_BACK_END_NUM_OUTPUTS; j++) {
742 if (!(rgb_enables & PISP_BE_RGB_ENABLE_OUTPUT(j)))
743 continue;
744
745 if (config->config.output_format[j].image.format &
746 PISP_IMAGE_FORMAT_WALLPAPER_ROLL)
747 continue; /* TODO: Size checks for wallpaper formats */
748
749 fmt = &pispbe->node[OUTPUT0_NODE + j].format;
750 for (unsigned int i = 0; i < fmt->fmt.pix_mp.num_planes; i++) {
751 bpl = !i ? config->config.output_format[j].image.stride
752 : config->config.output_format[j].image.stride2;
753 size = bpl * config->config.output_format[j].image.height;
754
755 if (config->config.output_format[j].image.format &
756 PISP_IMAGE_FORMAT_SAMPLING_420)
757 size >>= 1;
758
759 if (fmt->fmt.pix_mp.plane_fmt[i].bytesperline < bpl) {
760 dev_dbg(dev, "%s: bpl mismatch on output %d\n",
761 __func__, j);
762 return -EINVAL;
763 }
764
765 if (fmt->fmt.pix_mp.plane_fmt[i].sizeimage < size) {
766 dev_dbg(dev, "%s: size mismatch on output\n",
767 __func__);
768 return -EINVAL;
769 }
770 }
771 }
772
773 return 0;
774 }
775
pispbe_node_queue_setup(struct vb2_queue * q,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])776 static int pispbe_node_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
777 unsigned int *nplanes, unsigned int sizes[],
778 struct device *alloc_devs[])
779 {
780 struct pispbe_node *node = vb2_get_drv_priv(q);
781 struct pispbe_dev *pispbe = node->pispbe;
782 unsigned int num_planes = NODE_IS_MPLANE(node) ?
783 node->format.fmt.pix_mp.num_planes : 1;
784
785 if (*nplanes) {
786 if (*nplanes != num_planes)
787 return -EINVAL;
788
789 for (unsigned int i = 0; i < *nplanes; i++) {
790 unsigned int size = NODE_IS_MPLANE(node) ?
791 node->format.fmt.pix_mp.plane_fmt[i].sizeimage :
792 node->format.fmt.meta.buffersize;
793
794 if (sizes[i] < size)
795 return -EINVAL;
796 }
797
798 return 0;
799 }
800
801 *nplanes = num_planes;
802 for (unsigned int i = 0; i < *nplanes; i++) {
803 unsigned int size = NODE_IS_MPLANE(node) ?
804 node->format.fmt.pix_mp.plane_fmt[i].sizeimage :
805 node->format.fmt.meta.buffersize;
806 sizes[i] = size;
807 }
808
809 dev_dbg(pispbe->dev,
810 "Image (or metadata) size %u, nbuffers %u for node %s\n",
811 sizes[0], *nbuffers, NODE_NAME(node));
812
813 return 0;
814 }
815
pispbe_node_buffer_prepare(struct vb2_buffer * vb)816 static int pispbe_node_buffer_prepare(struct vb2_buffer *vb)
817 {
818 struct pispbe_node *node = vb2_get_drv_priv(vb->vb2_queue);
819 struct pispbe_dev *pispbe = node->pispbe;
820 unsigned int num_planes = NODE_IS_MPLANE(node) ?
821 node->format.fmt.pix_mp.num_planes : 1;
822
823 for (unsigned int i = 0; i < num_planes; i++) {
824 unsigned long size = NODE_IS_MPLANE(node) ?
825 node->format.fmt.pix_mp.plane_fmt[i].sizeimage :
826 node->format.fmt.meta.buffersize;
827
828 if (vb2_plane_size(vb, i) < size) {
829 dev_dbg(pispbe->dev,
830 "data will not fit into plane %d (%lu < %lu)\n",
831 i, vb2_plane_size(vb, i), size);
832 return -EINVAL;
833 }
834
835 vb2_set_plane_payload(vb, i, size);
836 }
837
838 if (node->id == CONFIG_NODE) {
839 void *dst = &node->pispbe->config[vb->index];
840 void *src = vb2_plane_vaddr(vb, 0);
841
842 memcpy(dst, src, sizeof(struct pisp_be_tiles_config));
843
844 return pisp_be_validate_config(pispbe, dst);
845 }
846
847 return 0;
848 }
849
pispbe_node_buffer_queue(struct vb2_buffer * buf)850 static void pispbe_node_buffer_queue(struct vb2_buffer *buf)
851 {
852 struct vb2_v4l2_buffer *vbuf =
853 container_of(buf, struct vb2_v4l2_buffer, vb2_buf);
854 struct pispbe_buffer *buffer =
855 container_of(vbuf, struct pispbe_buffer, vb);
856 struct pispbe_node *node = vb2_get_drv_priv(buf->vb2_queue);
857 struct pispbe_dev *pispbe = node->pispbe;
858
859 dev_dbg(pispbe->dev, "%s: for node %s\n", __func__, NODE_NAME(node));
860 list_add_tail(&buffer->ready_list, &node->ready_queue);
861
862 /*
863 * Every time we add a buffer, check if there's now some work for the hw
864 * to do.
865 */
866 if (!pispbe_prepare_job(pispbe))
867 pispbe_schedule(pispbe, false);
868 }
869
pispbe_node_start_streaming(struct vb2_queue * q,unsigned int count)870 static int pispbe_node_start_streaming(struct vb2_queue *q, unsigned int count)
871 {
872 struct pispbe_node *node = vb2_get_drv_priv(q);
873 struct pispbe_dev *pispbe = node->pispbe;
874 struct pispbe_buffer *buf, *tmp;
875 int ret;
876
877 ret = pm_runtime_resume_and_get(pispbe->dev);
878 if (ret < 0)
879 goto err_return_buffers;
880
881 scoped_guard(spinlock_irq, &pispbe->hw_lock) {
882 node->pispbe->streaming_map |= BIT(node->id);
883 node->pispbe->sequence = 0;
884 }
885
886 dev_dbg(pispbe->dev, "%s: for node %s (count %u)\n",
887 __func__, NODE_NAME(node), count);
888 dev_dbg(pispbe->dev, "Nodes streaming now 0x%x\n",
889 node->pispbe->streaming_map);
890
891 /* Maybe we're ready to run. */
892 if (!pispbe_prepare_job(pispbe))
893 pispbe_schedule(pispbe, false);
894
895 return 0;
896
897 err_return_buffers:
898 list_for_each_entry_safe(buf, tmp, &node->ready_queue, ready_list) {
899 list_del(&buf->ready_list);
900 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
901 }
902
903 return ret;
904 }
905
pispbe_node_stop_streaming(struct vb2_queue * q)906 static void pispbe_node_stop_streaming(struct vb2_queue *q)
907 {
908 struct pispbe_node *node = vb2_get_drv_priv(q);
909 struct pispbe_dev *pispbe = node->pispbe;
910 struct pispbe_job_descriptor *job, *temp;
911 struct pispbe_buffer *buf;
912 LIST_HEAD(tmp_list);
913
914 /*
915 * Now this is a bit awkward. In a simple M2M device we could just wait
916 * for all queued jobs to complete, but here there's a risk that a
917 * partial set of buffers was queued and cannot be run. For now, just
918 * cancel all buffers stuck in the "ready queue", then wait for any
919 * running job.
920 *
921 * This may return buffers out of order.
922 */
923 dev_dbg(pispbe->dev, "%s: for node %s\n", __func__, NODE_NAME(node));
924 do {
925 buf = list_first_entry_or_null(&node->ready_queue,
926 struct pispbe_buffer,
927 ready_list);
928 if (buf) {
929 list_del(&buf->ready_list);
930 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
931 }
932 } while (buf);
933
934 vb2_wait_for_all_buffers(&node->queue);
935
936 spin_lock_irq(&pispbe->hw_lock);
937 pispbe->streaming_map &= ~BIT(node->id);
938
939 if (pispbe->streaming_map == 0) {
940 /*
941 * If all nodes have stopped streaming release all jobs
942 * without holding the lock.
943 */
944 list_splice_init(&pispbe->job_queue, &tmp_list);
945 }
946 spin_unlock_irq(&pispbe->hw_lock);
947
948 list_for_each_entry_safe(job, temp, &tmp_list, queue) {
949 list_del(&job->queue);
950 kfree(job);
951 }
952
953 pm_runtime_mark_last_busy(pispbe->dev);
954 pm_runtime_put_autosuspend(pispbe->dev);
955
956 dev_dbg(pispbe->dev, "Nodes streaming now 0x%x\n",
957 pispbe->streaming_map);
958 }
959
960 static const struct vb2_ops pispbe_node_queue_ops = {
961 .queue_setup = pispbe_node_queue_setup,
962 .buf_prepare = pispbe_node_buffer_prepare,
963 .buf_queue = pispbe_node_buffer_queue,
964 .start_streaming = pispbe_node_start_streaming,
965 .stop_streaming = pispbe_node_stop_streaming,
966 };
967
968 static const struct v4l2_file_operations pispbe_fops = {
969 .owner = THIS_MODULE,
970 .open = v4l2_fh_open,
971 .release = vb2_fop_release,
972 .poll = vb2_fop_poll,
973 .unlocked_ioctl = video_ioctl2,
974 .mmap = vb2_fop_mmap
975 };
976
pispbe_node_querycap(struct file * file,void * priv,struct v4l2_capability * cap)977 static int pispbe_node_querycap(struct file *file, void *priv,
978 struct v4l2_capability *cap)
979 {
980 struct pispbe_node *node = video_drvdata(file);
981 struct pispbe_dev *pispbe = node->pispbe;
982
983 strscpy(cap->driver, PISPBE_NAME, sizeof(cap->driver));
984 strscpy(cap->card, PISPBE_NAME, sizeof(cap->card));
985
986 dev_dbg(pispbe->dev, "Caps for node %s: %x and %x (dev %x)\n",
987 NODE_NAME(node), cap->capabilities, cap->device_caps,
988 node->vfd.device_caps);
989
990 return 0;
991 }
992
pispbe_node_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)993 static int pispbe_node_g_fmt_vid_cap(struct file *file, void *priv,
994 struct v4l2_format *f)
995 {
996 struct pispbe_node *node = video_drvdata(file);
997 struct pispbe_dev *pispbe = node->pispbe;
998
999 if (!NODE_IS_CAPTURE(node) || NODE_IS_META(node)) {
1000 dev_dbg(pispbe->dev,
1001 "Cannot get capture fmt for output node %s\n",
1002 NODE_NAME(node));
1003 return -EINVAL;
1004 }
1005
1006 *f = node->format;
1007 dev_dbg(pispbe->dev, "Get capture format for node %s\n",
1008 NODE_NAME(node));
1009
1010 return 0;
1011 }
1012
pispbe_node_g_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)1013 static int pispbe_node_g_fmt_vid_out(struct file *file, void *priv,
1014 struct v4l2_format *f)
1015 {
1016 struct pispbe_node *node = video_drvdata(file);
1017 struct pispbe_dev *pispbe = node->pispbe;
1018
1019 if (NODE_IS_CAPTURE(node) || NODE_IS_META(node)) {
1020 dev_dbg(pispbe->dev,
1021 "Cannot get capture fmt for output node %s\n",
1022 NODE_NAME(node));
1023 return -EINVAL;
1024 }
1025
1026 *f = node->format;
1027 dev_dbg(pispbe->dev, "Get output format for node %s\n",
1028 NODE_NAME(node));
1029
1030 return 0;
1031 }
1032
pispbe_node_g_fmt_meta_out(struct file * file,void * priv,struct v4l2_format * f)1033 static int pispbe_node_g_fmt_meta_out(struct file *file, void *priv,
1034 struct v4l2_format *f)
1035 {
1036 struct pispbe_node *node = video_drvdata(file);
1037 struct pispbe_dev *pispbe = node->pispbe;
1038
1039 if (!NODE_IS_META(node) || NODE_IS_CAPTURE(node)) {
1040 dev_dbg(pispbe->dev,
1041 "Cannot get capture fmt for meta output node %s\n",
1042 NODE_NAME(node));
1043 return -EINVAL;
1044 }
1045
1046 *f = node->format;
1047 dev_dbg(pispbe->dev, "Get output format for meta node %s\n",
1048 NODE_NAME(node));
1049
1050 return 0;
1051 }
1052
pispbe_find_fmt(unsigned int fourcc)1053 static const struct pisp_be_format *pispbe_find_fmt(unsigned int fourcc)
1054 {
1055 for (unsigned int i = 0; i < ARRAY_SIZE(supported_formats); i++) {
1056 if (supported_formats[i].fourcc == fourcc)
1057 return &supported_formats[i];
1058 }
1059
1060 return NULL;
1061 }
1062
pispbe_set_plane_params(struct v4l2_format * f,const struct pisp_be_format * fmt)1063 static void pispbe_set_plane_params(struct v4l2_format *f,
1064 const struct pisp_be_format *fmt)
1065 {
1066 unsigned int nplanes = f->fmt.pix_mp.num_planes;
1067 unsigned int total_plane_factor = 0;
1068
1069 for (unsigned int i = 0; i < PISPBE_MAX_PLANES; i++)
1070 total_plane_factor += fmt->plane_factor[i];
1071
1072 for (unsigned int i = 0; i < nplanes; i++) {
1073 struct v4l2_plane_pix_format *p = &f->fmt.pix_mp.plane_fmt[i];
1074 unsigned int bpl, plane_size;
1075
1076 bpl = (f->fmt.pix_mp.width * fmt->bit_depth) >> 3;
1077 bpl = ALIGN(max(p->bytesperline, bpl), fmt->align);
1078
1079 plane_size = bpl * f->fmt.pix_mp.height *
1080 (nplanes > 1 ? fmt->plane_factor[i] : total_plane_factor);
1081 /*
1082 * The shift is to divide out the plane_factor fixed point
1083 * scaling of 8.
1084 */
1085 plane_size = max(p->sizeimage, plane_size >> 3);
1086
1087 p->bytesperline = bpl;
1088 p->sizeimage = plane_size;
1089 }
1090 }
1091
pispbe_try_format(struct v4l2_format * f,struct pispbe_node * node)1092 static void pispbe_try_format(struct v4l2_format *f, struct pispbe_node *node)
1093 {
1094 struct pispbe_dev *pispbe = node->pispbe;
1095 u32 pixfmt = f->fmt.pix_mp.pixelformat;
1096 const struct pisp_be_format *fmt;
1097 bool is_rgb;
1098
1099 dev_dbg(pispbe->dev,
1100 "%s: [%s] req %ux%u %p4cc, planes %d\n",
1101 __func__, NODE_NAME(node), f->fmt.pix_mp.width,
1102 f->fmt.pix_mp.height, &pixfmt,
1103 f->fmt.pix_mp.num_planes);
1104
1105 fmt = pispbe_find_fmt(pixfmt);
1106 if (!fmt) {
1107 dev_dbg(pispbe->dev,
1108 "%s: [%s] Format not found, defaulting to YUV420\n",
1109 __func__, NODE_NAME(node));
1110 fmt = pispbe_find_fmt(V4L2_PIX_FMT_YUV420);
1111 }
1112
1113 f->fmt.pix_mp.pixelformat = fmt->fourcc;
1114 f->fmt.pix_mp.num_planes = fmt->num_planes;
1115 f->fmt.pix_mp.field = V4L2_FIELD_NONE;
1116 f->fmt.pix_mp.width = clamp(f->fmt.pix_mp.width,
1117 PISP_BACK_END_MIN_TILE_WIDTH,
1118 PISP_BACK_END_MAX_TILE_WIDTH);
1119 f->fmt.pix_mp.height = clamp(f->fmt.pix_mp.height,
1120 PISP_BACK_END_MIN_TILE_HEIGHT,
1121 PISP_BACK_END_MAX_TILE_HEIGHT);
1122
1123 /*
1124 * Fill in the actual colour space when the requested one was
1125 * not supported. This also catches the case when the "default"
1126 * colour space was requested (as that's never in the mask).
1127 */
1128 if (!(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) &
1129 fmt->colorspace_mask))
1130 f->fmt.pix_mp.colorspace = fmt->colorspace_default;
1131
1132 /* In all cases, we only support the defaults for these: */
1133 f->fmt.pix_mp.ycbcr_enc =
1134 V4L2_MAP_YCBCR_ENC_DEFAULT(f->fmt.pix_mp.colorspace);
1135 f->fmt.pix_mp.xfer_func =
1136 V4L2_MAP_XFER_FUNC_DEFAULT(f->fmt.pix_mp.colorspace);
1137
1138 is_rgb = f->fmt.pix_mp.colorspace == V4L2_COLORSPACE_SRGB;
1139 f->fmt.pix_mp.quantization =
1140 V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb, f->fmt.pix_mp.colorspace,
1141 f->fmt.pix_mp.ycbcr_enc);
1142
1143 /* Set plane size and bytes/line for each plane. */
1144 pispbe_set_plane_params(f, fmt);
1145
1146 for (unsigned int i = 0; i < f->fmt.pix_mp.num_planes; i++) {
1147 dev_dbg(pispbe->dev,
1148 "%s: [%s] calc plane %d, %ux%u, depth %u, bpl %u size %u\n",
1149 __func__, NODE_NAME(node), i, f->fmt.pix_mp.width,
1150 f->fmt.pix_mp.height, fmt->bit_depth,
1151 f->fmt.pix_mp.plane_fmt[i].bytesperline,
1152 f->fmt.pix_mp.plane_fmt[i].sizeimage);
1153 }
1154 }
1155
pispbe_node_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1156 static int pispbe_node_try_fmt_vid_cap(struct file *file, void *priv,
1157 struct v4l2_format *f)
1158 {
1159 struct pispbe_node *node = video_drvdata(file);
1160 struct pispbe_dev *pispbe = node->pispbe;
1161
1162 if (!NODE_IS_CAPTURE(node) || NODE_IS_META(node)) {
1163 dev_dbg(pispbe->dev,
1164 "Cannot set capture fmt for output node %s\n",
1165 NODE_NAME(node));
1166 return -EINVAL;
1167 }
1168
1169 pispbe_try_format(f, node);
1170
1171 return 0;
1172 }
1173
pispbe_node_try_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)1174 static int pispbe_node_try_fmt_vid_out(struct file *file, void *priv,
1175 struct v4l2_format *f)
1176 {
1177 struct pispbe_node *node = video_drvdata(file);
1178 struct pispbe_dev *pispbe = node->pispbe;
1179
1180 if (!NODE_IS_OUTPUT(node) || NODE_IS_META(node)) {
1181 dev_dbg(pispbe->dev,
1182 "Cannot set capture fmt for output node %s\n",
1183 NODE_NAME(node));
1184 return -EINVAL;
1185 }
1186
1187 pispbe_try_format(f, node);
1188
1189 return 0;
1190 }
1191
pispbe_node_try_fmt_meta_out(struct file * file,void * priv,struct v4l2_format * f)1192 static int pispbe_node_try_fmt_meta_out(struct file *file, void *priv,
1193 struct v4l2_format *f)
1194 {
1195 struct pispbe_node *node = video_drvdata(file);
1196 struct pispbe_dev *pispbe = node->pispbe;
1197
1198 if (!NODE_IS_META(node) || NODE_IS_CAPTURE(node)) {
1199 dev_dbg(pispbe->dev,
1200 "Cannot set capture fmt for meta output node %s\n",
1201 NODE_NAME(node));
1202 return -EINVAL;
1203 }
1204
1205 f->fmt.meta.dataformat = V4L2_META_FMT_RPI_BE_CFG;
1206 f->fmt.meta.buffersize = sizeof(struct pisp_be_tiles_config);
1207
1208 return 0;
1209 }
1210
pispbe_node_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1211 static int pispbe_node_s_fmt_vid_cap(struct file *file, void *priv,
1212 struct v4l2_format *f)
1213 {
1214 struct pispbe_node *node = video_drvdata(file);
1215 struct pispbe_dev *pispbe = node->pispbe;
1216 int ret;
1217
1218 ret = pispbe_node_try_fmt_vid_cap(file, priv, f);
1219 if (ret < 0)
1220 return ret;
1221
1222 if (vb2_is_busy(&node->queue))
1223 return -EBUSY;
1224
1225 node->format = *f;
1226 node->pisp_format = pispbe_find_fmt(f->fmt.pix_mp.pixelformat);
1227
1228 dev_dbg(pispbe->dev, "Set capture format for node %s to %p4cc\n",
1229 NODE_NAME(node), &f->fmt.pix_mp.pixelformat);
1230
1231 return 0;
1232 }
1233
pispbe_node_s_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)1234 static int pispbe_node_s_fmt_vid_out(struct file *file, void *priv,
1235 struct v4l2_format *f)
1236 {
1237 struct pispbe_node *node = video_drvdata(file);
1238 struct pispbe_dev *pispbe = node->pispbe;
1239 int ret;
1240
1241 ret = pispbe_node_try_fmt_vid_out(file, priv, f);
1242 if (ret < 0)
1243 return ret;
1244
1245 if (vb2_is_busy(&node->queue))
1246 return -EBUSY;
1247
1248 node->format = *f;
1249 node->pisp_format = pispbe_find_fmt(f->fmt.pix_mp.pixelformat);
1250
1251 dev_dbg(pispbe->dev, "Set output format for node %s to %p4cc\n",
1252 NODE_NAME(node), &f->fmt.pix_mp.pixelformat);
1253
1254 return 0;
1255 }
1256
pispbe_node_s_fmt_meta_out(struct file * file,void * priv,struct v4l2_format * f)1257 static int pispbe_node_s_fmt_meta_out(struct file *file, void *priv,
1258 struct v4l2_format *f)
1259 {
1260 struct pispbe_node *node = video_drvdata(file);
1261 struct pispbe_dev *pispbe = node->pispbe;
1262 int ret;
1263
1264 ret = pispbe_node_try_fmt_meta_out(file, priv, f);
1265 if (ret < 0)
1266 return ret;
1267
1268 if (vb2_is_busy(&node->queue))
1269 return -EBUSY;
1270
1271 node->format = *f;
1272 node->pisp_format = &meta_out_supported_formats[0];
1273
1274 dev_dbg(pispbe->dev, "Set output format for meta node %s to %p4cc\n",
1275 NODE_NAME(node), &f->fmt.meta.dataformat);
1276
1277 return 0;
1278 }
1279
pispbe_node_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)1280 static int pispbe_node_enum_fmt(struct file *file, void *priv,
1281 struct v4l2_fmtdesc *f)
1282 {
1283 struct pispbe_node *node = video_drvdata(file);
1284
1285 if (f->type != node->queue.type)
1286 return -EINVAL;
1287
1288 if (NODE_IS_META(node)) {
1289 if (f->index)
1290 return -EINVAL;
1291
1292 f->pixelformat = V4L2_META_FMT_RPI_BE_CFG;
1293 f->flags = 0;
1294 return 0;
1295 }
1296
1297 if (f->index >= ARRAY_SIZE(supported_formats))
1298 return -EINVAL;
1299
1300 f->pixelformat = supported_formats[f->index].fourcc;
1301 f->flags = 0;
1302
1303 return 0;
1304 }
1305
pispbe_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)1306 static int pispbe_enum_framesizes(struct file *file, void *priv,
1307 struct v4l2_frmsizeenum *fsize)
1308 {
1309 struct pispbe_node *node = video_drvdata(file);
1310 struct pispbe_dev *pispbe = node->pispbe;
1311
1312 if (NODE_IS_META(node) || fsize->index)
1313 return -EINVAL;
1314
1315 if (!pispbe_find_fmt(fsize->pixel_format)) {
1316 dev_dbg(pispbe->dev, "Invalid pixel code: %x\n",
1317 fsize->pixel_format);
1318 return -EINVAL;
1319 }
1320
1321 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1322 fsize->stepwise.min_width = 32;
1323 fsize->stepwise.max_width = 65535;
1324 fsize->stepwise.step_width = 2;
1325
1326 fsize->stepwise.min_height = 32;
1327 fsize->stepwise.max_height = 65535;
1328 fsize->stepwise.step_height = 2;
1329
1330 return 0;
1331 }
1332
1333 static const struct v4l2_ioctl_ops pispbe_node_ioctl_ops = {
1334 .vidioc_querycap = pispbe_node_querycap,
1335 .vidioc_g_fmt_vid_cap_mplane = pispbe_node_g_fmt_vid_cap,
1336 .vidioc_g_fmt_vid_out_mplane = pispbe_node_g_fmt_vid_out,
1337 .vidioc_g_fmt_meta_out = pispbe_node_g_fmt_meta_out,
1338 .vidioc_try_fmt_vid_cap_mplane = pispbe_node_try_fmt_vid_cap,
1339 .vidioc_try_fmt_vid_out_mplane = pispbe_node_try_fmt_vid_out,
1340 .vidioc_try_fmt_meta_out = pispbe_node_try_fmt_meta_out,
1341 .vidioc_s_fmt_vid_cap_mplane = pispbe_node_s_fmt_vid_cap,
1342 .vidioc_s_fmt_vid_out_mplane = pispbe_node_s_fmt_vid_out,
1343 .vidioc_s_fmt_meta_out = pispbe_node_s_fmt_meta_out,
1344 .vidioc_enum_fmt_vid_cap = pispbe_node_enum_fmt,
1345 .vidioc_enum_fmt_vid_out = pispbe_node_enum_fmt,
1346 .vidioc_enum_fmt_meta_out = pispbe_node_enum_fmt,
1347 .vidioc_enum_framesizes = pispbe_enum_framesizes,
1348 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1349 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1350 .vidioc_querybuf = vb2_ioctl_querybuf,
1351 .vidioc_qbuf = vb2_ioctl_qbuf,
1352 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1353 .vidioc_expbuf = vb2_ioctl_expbuf,
1354 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1355 .vidioc_streamon = vb2_ioctl_streamon,
1356 .vidioc_streamoff = vb2_ioctl_streamoff,
1357 };
1358
1359 static const struct video_device pispbe_videodev = {
1360 .name = PISPBE_NAME,
1361 .vfl_dir = VFL_DIR_M2M, /* gets overwritten */
1362 .fops = &pispbe_fops,
1363 .ioctl_ops = &pispbe_node_ioctl_ops,
1364 .minor = -1,
1365 .release = video_device_release_empty,
1366 };
1367
pispbe_node_def_fmt(struct pispbe_node * node)1368 static void pispbe_node_def_fmt(struct pispbe_node *node)
1369 {
1370 if (NODE_IS_META(node) && NODE_IS_OUTPUT(node)) {
1371 /* Config node */
1372 struct v4l2_format *f = &node->format;
1373
1374 f->fmt.meta.dataformat = V4L2_META_FMT_RPI_BE_CFG;
1375 f->fmt.meta.buffersize = sizeof(struct pisp_be_tiles_config);
1376 f->type = node->buf_type;
1377 } else {
1378 struct v4l2_format f = {
1379 .fmt.pix_mp.pixelformat = V4L2_PIX_FMT_YUV420,
1380 .fmt.pix_mp.width = 1920,
1381 .fmt.pix_mp.height = 1080,
1382 .type = node->buf_type,
1383 };
1384 pispbe_try_format(&f, node);
1385 node->format = f;
1386 }
1387
1388 node->pisp_format = pispbe_find_fmt(node->format.fmt.pix_mp.pixelformat);
1389 }
1390
1391 /*
1392 * Initialise a struct pispbe_node and register it as /dev/video<N>
1393 * to represent one of the PiSP Back End's input or output streams.
1394 */
pispbe_init_node(struct pispbe_dev * pispbe,unsigned int id)1395 static int pispbe_init_node(struct pispbe_dev *pispbe, unsigned int id)
1396 {
1397 bool output = NODE_DESC_IS_OUTPUT(&node_desc[id]);
1398 struct pispbe_node *node = &pispbe->node[id];
1399 struct media_entity *entity = &node->vfd.entity;
1400 struct video_device *vdev = &node->vfd;
1401 struct vb2_queue *q = &node->queue;
1402 int ret;
1403
1404 node->id = id;
1405 node->pispbe = pispbe;
1406 node->buf_type = node_desc[id].buf_type;
1407
1408 mutex_init(&node->node_lock);
1409 mutex_init(&node->queue_lock);
1410 INIT_LIST_HEAD(&node->ready_queue);
1411
1412 node->format.type = node->buf_type;
1413 pispbe_node_def_fmt(node);
1414
1415 q->type = node->buf_type;
1416 q->io_modes = VB2_MMAP | VB2_DMABUF;
1417 q->mem_ops = &vb2_dma_contig_memops;
1418 q->drv_priv = node;
1419 q->ops = &pispbe_node_queue_ops;
1420 q->buf_struct_size = sizeof(struct pispbe_buffer);
1421 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1422 q->dev = pispbe->dev;
1423 /* get V4L2 to handle node->queue locking */
1424 q->lock = &node->queue_lock;
1425
1426 ret = vb2_queue_init(q);
1427 if (ret < 0) {
1428 dev_err(pispbe->dev, "vb2_queue_init failed\n");
1429 goto err_mutex_destroy;
1430 }
1431
1432 *vdev = pispbe_videodev; /* default initialization */
1433 strscpy(vdev->name, node_desc[id].ent_name, sizeof(vdev->name));
1434 vdev->v4l2_dev = &pispbe->v4l2_dev;
1435 vdev->vfl_dir = output ? VFL_DIR_TX : VFL_DIR_RX;
1436 /* get V4L2 to serialise our ioctls */
1437 vdev->lock = &node->node_lock;
1438 vdev->queue = &node->queue;
1439 vdev->device_caps = V4L2_CAP_STREAMING | node_desc[id].caps;
1440
1441 node->pad.flags = output ? MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK;
1442 ret = media_entity_pads_init(entity, 1, &node->pad);
1443 if (ret) {
1444 dev_err(pispbe->dev,
1445 "Failed to register media pads for %s device node\n",
1446 NODE_NAME(node));
1447 goto err_unregister_queue;
1448 }
1449
1450 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1451 if (ret) {
1452 dev_err(pispbe->dev,
1453 "Failed to register video %s device node\n",
1454 NODE_NAME(node));
1455 goto err_unregister_queue;
1456 }
1457 video_set_drvdata(vdev, node);
1458
1459 if (output)
1460 ret = media_create_pad_link(entity, 0, &pispbe->sd.entity,
1461 id, MEDIA_LNK_FL_IMMUTABLE |
1462 MEDIA_LNK_FL_ENABLED);
1463 else
1464 ret = media_create_pad_link(&pispbe->sd.entity, id, entity,
1465 0, MEDIA_LNK_FL_IMMUTABLE |
1466 MEDIA_LNK_FL_ENABLED);
1467 if (ret)
1468 goto err_unregister_video_dev;
1469
1470 dev_dbg(pispbe->dev, "%s device node registered as /dev/video%d\n",
1471 NODE_NAME(node), node->vfd.num);
1472
1473 return 0;
1474
1475 err_unregister_video_dev:
1476 video_unregister_device(&node->vfd);
1477 err_unregister_queue:
1478 vb2_queue_release(&node->queue);
1479 err_mutex_destroy:
1480 mutex_destroy(&node->node_lock);
1481 mutex_destroy(&node->queue_lock);
1482 return ret;
1483 }
1484
1485 static const struct v4l2_subdev_pad_ops pispbe_pad_ops = {
1486 .link_validate = v4l2_subdev_link_validate_default,
1487 };
1488
1489 static const struct v4l2_subdev_ops pispbe_sd_ops = {
1490 .pad = &pispbe_pad_ops,
1491 };
1492
pispbe_init_subdev(struct pispbe_dev * pispbe)1493 static int pispbe_init_subdev(struct pispbe_dev *pispbe)
1494 {
1495 struct v4l2_subdev *sd = &pispbe->sd;
1496 int ret;
1497
1498 v4l2_subdev_init(sd, &pispbe_sd_ops);
1499 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1500 sd->owner = THIS_MODULE;
1501 sd->dev = pispbe->dev;
1502 strscpy(sd->name, PISPBE_NAME, sizeof(sd->name));
1503
1504 for (unsigned int i = 0; i < PISPBE_NUM_NODES; i++)
1505 pispbe->pad[i].flags =
1506 NODE_DESC_IS_OUTPUT(&node_desc[i]) ?
1507 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1508
1509 ret = media_entity_pads_init(&sd->entity, PISPBE_NUM_NODES,
1510 pispbe->pad);
1511 if (ret)
1512 goto error;
1513
1514 ret = v4l2_device_register_subdev(&pispbe->v4l2_dev, sd);
1515 if (ret)
1516 goto error;
1517
1518 return 0;
1519
1520 error:
1521 media_entity_cleanup(&sd->entity);
1522 return ret;
1523 }
1524
pispbe_init_devices(struct pispbe_dev * pispbe)1525 static int pispbe_init_devices(struct pispbe_dev *pispbe)
1526 {
1527 struct v4l2_device *v4l2_dev;
1528 struct media_device *mdev;
1529 unsigned int num_regist;
1530 int ret;
1531
1532 /* Register v4l2_device and media_device */
1533 mdev = &pispbe->mdev;
1534 mdev->hw_revision = pispbe->hw_version;
1535 mdev->dev = pispbe->dev;
1536 strscpy(mdev->model, PISPBE_NAME, sizeof(mdev->model));
1537 media_device_init(mdev);
1538
1539 v4l2_dev = &pispbe->v4l2_dev;
1540 v4l2_dev->mdev = &pispbe->mdev;
1541 strscpy(v4l2_dev->name, PISPBE_NAME, sizeof(v4l2_dev->name));
1542
1543 ret = v4l2_device_register(pispbe->dev, v4l2_dev);
1544 if (ret)
1545 goto err_media_dev_cleanup;
1546
1547 /* Register the PISPBE subdevice. */
1548 ret = pispbe_init_subdev(pispbe);
1549 if (ret)
1550 goto err_unregister_v4l2;
1551
1552 /* Create device video nodes */
1553 for (num_regist = 0; num_regist < PISPBE_NUM_NODES; num_regist++) {
1554 ret = pispbe_init_node(pispbe, num_regist);
1555 if (ret)
1556 goto err_unregister_nodes;
1557 }
1558
1559 ret = media_device_register(mdev);
1560 if (ret)
1561 goto err_unregister_nodes;
1562
1563 pispbe->config =
1564 dma_alloc_coherent(pispbe->dev,
1565 sizeof(struct pisp_be_tiles_config) *
1566 PISP_BE_NUM_CONFIG_BUFFERS,
1567 &pispbe->config_dma_addr, GFP_KERNEL);
1568 if (!pispbe->config) {
1569 dev_err(pispbe->dev, "Unable to allocate cached config buffers.\n");
1570 ret = -ENOMEM;
1571 goto err_unregister_mdev;
1572 }
1573
1574 return 0;
1575
1576 err_unregister_mdev:
1577 media_device_unregister(mdev);
1578 err_unregister_nodes:
1579 while (num_regist-- > 0) {
1580 video_unregister_device(&pispbe->node[num_regist].vfd);
1581 vb2_queue_release(&pispbe->node[num_regist].queue);
1582 }
1583 v4l2_device_unregister_subdev(&pispbe->sd);
1584 media_entity_cleanup(&pispbe->sd.entity);
1585 err_unregister_v4l2:
1586 v4l2_device_unregister(v4l2_dev);
1587 err_media_dev_cleanup:
1588 media_device_cleanup(mdev);
1589 return ret;
1590 }
1591
pispbe_destroy_devices(struct pispbe_dev * pispbe)1592 static void pispbe_destroy_devices(struct pispbe_dev *pispbe)
1593 {
1594 if (pispbe->config) {
1595 dma_free_coherent(pispbe->dev,
1596 sizeof(struct pisp_be_tiles_config) *
1597 PISP_BE_NUM_CONFIG_BUFFERS,
1598 pispbe->config,
1599 pispbe->config_dma_addr);
1600 }
1601
1602 dev_dbg(pispbe->dev, "Unregister from media controller\n");
1603
1604 v4l2_device_unregister_subdev(&pispbe->sd);
1605 media_entity_cleanup(&pispbe->sd.entity);
1606 media_device_unregister(&pispbe->mdev);
1607
1608 for (int i = PISPBE_NUM_NODES - 1; i >= 0; i--) {
1609 video_unregister_device(&pispbe->node[i].vfd);
1610 vb2_queue_release(&pispbe->node[i].queue);
1611 mutex_destroy(&pispbe->node[i].node_lock);
1612 mutex_destroy(&pispbe->node[i].queue_lock);
1613 }
1614
1615 media_device_cleanup(&pispbe->mdev);
1616 v4l2_device_unregister(&pispbe->v4l2_dev);
1617 }
1618
pispbe_runtime_suspend(struct device * dev)1619 static int pispbe_runtime_suspend(struct device *dev)
1620 {
1621 struct pispbe_dev *pispbe = dev_get_drvdata(dev);
1622
1623 clk_disable_unprepare(pispbe->clk);
1624
1625 return 0;
1626 }
1627
pispbe_runtime_resume(struct device * dev)1628 static int pispbe_runtime_resume(struct device *dev)
1629 {
1630 struct pispbe_dev *pispbe = dev_get_drvdata(dev);
1631 int ret;
1632
1633 ret = clk_prepare_enable(pispbe->clk);
1634 if (ret) {
1635 dev_err(dev, "Unable to enable clock\n");
1636 return ret;
1637 }
1638
1639 dev_dbg(dev, "%s: Enabled clock, rate=%lu\n",
1640 __func__, clk_get_rate(pispbe->clk));
1641
1642 return 0;
1643 }
1644
pispbe_hw_init(struct pispbe_dev * pispbe)1645 static int pispbe_hw_init(struct pispbe_dev *pispbe)
1646 {
1647 u32 u;
1648
1649 /* Check the HW is present and has a known version */
1650 u = pispbe_rd(pispbe, PISP_BE_VERSION_REG);
1651 dev_dbg(pispbe->dev, "pispbe_probe: HW version: 0x%08x", u);
1652 pispbe->hw_version = u;
1653 if ((u & ~PISP_BE_VERSION_MINOR_BITS) != PISP_BE_VERSION_2712)
1654 return -ENODEV;
1655
1656 /* Clear leftover interrupts */
1657 pispbe_wr(pispbe, PISP_BE_INTERRUPT_STATUS_REG, 0xFFFFFFFFu);
1658 u = pispbe_rd(pispbe, PISP_BE_BATCH_STATUS_REG);
1659 dev_dbg(pispbe->dev, "pispbe_probe: BatchStatus: 0x%08x", u);
1660
1661 pispbe->done = (uint8_t)u;
1662 pispbe->started = (uint8_t)(u >> 8);
1663 u = pispbe_rd(pispbe, PISP_BE_STATUS_REG);
1664 dev_dbg(pispbe->dev, "pispbe_probe: Status: 0x%08x", u);
1665
1666 if (u != 0 || pispbe->done != pispbe->started) {
1667 dev_err(pispbe->dev, "pispbe_probe: HW is stuck or busy\n");
1668 return -EBUSY;
1669 }
1670
1671 /*
1672 * AXI QOS=0, CACHE=4'b0010, PROT=3'b011
1673 * Also set "chicken bits" 22:20 which enable sub-64-byte bursts
1674 * and AXI AWID/BID variability (on versions which support this).
1675 */
1676 pispbe_wr(pispbe, PISP_BE_AXI_REG, 0x32703200u);
1677
1678 /* Enable both interrupt flags */
1679 pispbe_wr(pispbe, PISP_BE_INTERRUPT_EN_REG, 0x00000003u);
1680
1681 return 0;
1682 }
1683
1684 /* Probe the ISP-BE hardware block, as a single platform device. */
pispbe_probe(struct platform_device * pdev)1685 static int pispbe_probe(struct platform_device *pdev)
1686 {
1687 struct pispbe_dev *pispbe;
1688 int ret;
1689
1690 pispbe = devm_kzalloc(&pdev->dev, sizeof(*pispbe), GFP_KERNEL);
1691 if (!pispbe)
1692 return -ENOMEM;
1693
1694 INIT_LIST_HEAD(&pispbe->job_queue);
1695
1696 dev_set_drvdata(&pdev->dev, pispbe);
1697 pispbe->dev = &pdev->dev;
1698 platform_set_drvdata(pdev, pispbe);
1699
1700 pispbe->be_reg_base = devm_platform_ioremap_resource(pdev, 0);
1701 if (IS_ERR(pispbe->be_reg_base)) {
1702 dev_err(&pdev->dev, "Failed to get ISP-BE registers address\n");
1703 return PTR_ERR(pispbe->be_reg_base);
1704 }
1705
1706 pispbe->irq = platform_get_irq(pdev, 0);
1707 if (pispbe->irq <= 0)
1708 return -EINVAL;
1709
1710 ret = devm_request_irq(&pdev->dev, pispbe->irq, pispbe_isr, 0,
1711 PISPBE_NAME, pispbe);
1712 if (ret) {
1713 dev_err(&pdev->dev, "Unable to request interrupt\n");
1714 return ret;
1715 }
1716
1717 ret = dma_set_mask_and_coherent(pispbe->dev, DMA_BIT_MASK(36));
1718 if (ret)
1719 return ret;
1720
1721 pispbe->clk = devm_clk_get(&pdev->dev, NULL);
1722 if (IS_ERR(pispbe->clk))
1723 return dev_err_probe(&pdev->dev, PTR_ERR(pispbe->clk),
1724 "Failed to get clock");
1725
1726 /* Hardware initialisation */
1727 pm_runtime_set_autosuspend_delay(pispbe->dev, 200);
1728 pm_runtime_use_autosuspend(pispbe->dev);
1729 pm_runtime_enable(pispbe->dev);
1730
1731 ret = pm_runtime_resume_and_get(pispbe->dev);
1732 if (ret)
1733 goto pm_runtime_disable_err;
1734
1735 pispbe->hw_busy = false;
1736 spin_lock_init(&pispbe->hw_lock);
1737 ret = pispbe_hw_init(pispbe);
1738 if (ret)
1739 goto pm_runtime_suspend_err;
1740
1741 ret = pispbe_init_devices(pispbe);
1742 if (ret)
1743 goto disable_devs_err;
1744
1745 pm_runtime_mark_last_busy(pispbe->dev);
1746 pm_runtime_put_autosuspend(pispbe->dev);
1747
1748 return 0;
1749
1750 disable_devs_err:
1751 pispbe_destroy_devices(pispbe);
1752 pm_runtime_suspend_err:
1753 pm_runtime_put(pispbe->dev);
1754 pm_runtime_disable_err:
1755 pm_runtime_dont_use_autosuspend(pispbe->dev);
1756 pm_runtime_disable(pispbe->dev);
1757
1758 return ret;
1759 }
1760
pispbe_remove(struct platform_device * pdev)1761 static void pispbe_remove(struct platform_device *pdev)
1762 {
1763 struct pispbe_dev *pispbe = platform_get_drvdata(pdev);
1764
1765 pispbe_destroy_devices(pispbe);
1766
1767 pm_runtime_dont_use_autosuspend(pispbe->dev);
1768 pm_runtime_disable(pispbe->dev);
1769 }
1770
1771 static const struct dev_pm_ops pispbe_pm_ops = {
1772 SET_RUNTIME_PM_OPS(pispbe_runtime_suspend, pispbe_runtime_resume, NULL)
1773 };
1774
1775 static const struct of_device_id pispbe_of_match[] = {
1776 {
1777 .compatible = "raspberrypi,pispbe",
1778 },
1779 { /* sentinel */ },
1780 };
1781 MODULE_DEVICE_TABLE(of, pispbe_of_match);
1782
1783 static struct platform_driver pispbe_pdrv = {
1784 .probe = pispbe_probe,
1785 .remove = pispbe_remove,
1786 .driver = {
1787 .name = PISPBE_NAME,
1788 .of_match_table = pispbe_of_match,
1789 .pm = &pispbe_pm_ops,
1790 },
1791 };
1792
1793 module_platform_driver(pispbe_pdrv);
1794
1795 MODULE_DESCRIPTION("PiSP Back End driver");
1796 MODULE_AUTHOR("David Plowman <david.plowman@raspberrypi.com>");
1797 MODULE_AUTHOR("Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>");
1798 MODULE_LICENSE("GPL");
1799