* inputs.
                         */
                        WARN_ON(list_empty(&rpf->entity.list_pipe));
+                       rpf->entity.pipe = NULL;
                        list_del_init(&rpf->entity.list_pipe);
                        pipe->inputs[i] = NULL;
 
                        continue;
                }
 
-               if (list_empty(&rpf->entity.list_pipe))
+               if (list_empty(&rpf->entity.list_pipe)) {
+                       rpf->entity.pipe = pipe;
                        list_add_tail(&rpf->entity.list_pipe, &pipe->entities);
+               }
 
                bru->inputs[i].rpf = rpf;
                rpf->bru_input = i;
                        vsp1_dl_list_write(dl, entity->route->reg,
                                           VI6_DPR_NODE_UNUSED);
 
+                       entity->pipe = NULL;
                        list_del_init(&entity->list_pipe);
 
                        continue;
 
                vsp1_pipeline_init(pipe);
 
+               pipe->frame_end = vsp1_du_pipeline_frame_end;
+
                /*
                 * The DRM pipeline is static, add entities manually. The first
                 * pipeline uses the BRU and the second pipeline the BRS.
                 */
                pipe->bru = i == 0 ? &vsp1->bru->entity : &vsp1->brs->entity;
-               pipe->lif = &vsp1->lif[i]->entity;
                pipe->output = vsp1->wpf[i];
-               pipe->output->pipe = pipe;
-               pipe->frame_end = vsp1_du_pipeline_frame_end;
+               pipe->lif = &vsp1->lif[i]->entity;
 
+               pipe->bru->pipe = pipe;
                pipe->bru->sink = &pipe->output->entity;
                pipe->bru->sink_pad = 0;
+               list_add_tail(&pipe->bru->list_pipe, &pipe->entities);
+
+               pipe->output->entity.pipe = pipe;
                pipe->output->entity.sink = pipe->lif;
                pipe->output->entity.sink_pad = 0;
+               list_add_tail(&pipe->output->entity.list_pipe, &pipe->entities);
 
-               list_add_tail(&pipe->bru->list_pipe, &pipe->entities);
+               pipe->lif->pipe = pipe;
                list_add_tail(&pipe->lif->list_pipe, &pipe->entities);
-               list_add_tail(&pipe->output->entity.list_pipe, &pipe->entities);
        }
 
        /* Disable all RPFs initially. */
 
                vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
 
                if (status & VI6_WFP_IRQ_STA_DFE) {
-                       vsp1_pipeline_frame_end(wpf->pipe);
+                       vsp1_pipeline_frame_end(wpf->entity.pipe);
                        ret = IRQ_HANDLED;
                }
        }
 
        unsigned int index;
        const struct vsp1_route *route;
 
+       struct vsp1_pipeline *pipe;
+
        struct list_head list_dev;
        struct list_head list_pipe;
 
 
                                    struct vsp1_histogram_buffer *buf,
                                    size_t size)
 {
-       struct vsp1_pipeline *pipe = histo->pipe;
+       struct vsp1_pipeline *pipe = histo->entity.pipe;
        unsigned long flags;
 
        /*
 
 #include "vsp1_entity.h"
 
 struct vsp1_device;
-struct vsp1_pipeline;
 
 #define HISTO_PAD_SINK                         0
 #define HISTO_PAD_SOURCE                       1
 };
 
 struct vsp1_histogram {
-       struct vsp1_pipeline *pipe;
-
        struct vsp1_entity entity;
        struct video_device video;
        struct media_pad pad;
 
 
 void vsp1_pipeline_reset(struct vsp1_pipeline *pipe)
 {
+       struct vsp1_entity *entity;
        unsigned int i;
 
        if (pipe->bru) {
                        bru->inputs[i].rpf = NULL;
        }
 
-       for (i = 0; i < ARRAY_SIZE(pipe->inputs); ++i) {
-               if (pipe->inputs[i]) {
-                       pipe->inputs[i]->pipe = NULL;
-                       pipe->inputs[i] = NULL;
-               }
-       }
-
-       if (pipe->output) {
-               pipe->output->pipe = NULL;
-               pipe->output = NULL;
-       }
+       for (i = 0; i < ARRAY_SIZE(pipe->inputs); ++i)
+               pipe->inputs[i] = NULL;
 
-       if (pipe->hgo) {
-               struct vsp1_hgo *hgo = to_hgo(&pipe->hgo->subdev);
+       pipe->output = NULL;
 
-               hgo->histo.pipe = NULL;
-       }
-
-       if (pipe->hgt) {
-               struct vsp1_hgt *hgt = to_hgt(&pipe->hgt->subdev);
-
-               hgt->histo.pipe = NULL;
-       }
+       list_for_each_entry(entity, &pipe->entities, list_pipe)
+               entity->pipe = NULL;
 
        INIT_LIST_HEAD(&pipe->entities);
        pipe->state = VSP1_PIPELINE_STOPPED;
                if (wpf == NULL)
                        continue;
 
-               pipe = wpf->pipe;
+               pipe = wpf->entity.pipe;
                if (pipe == NULL)
                        continue;
 
                if (wpf == NULL)
                        continue;
 
-               pipe = wpf->pipe;
+               pipe = wpf->entity.pipe;
                if (pipe == NULL)
                        continue;
 
                if (wpf == NULL)
                        continue;
 
-               pipe = wpf->pipe;
+               pipe = wpf->entity.pipe;
                if (pipe == NULL)
                        continue;
 
 
 
 struct v4l2_ctrl;
 struct vsp1_dl_manager;
-struct vsp1_pipeline;
 struct vsp1_rwpf;
 struct vsp1_video;
 
        struct vsp1_entity entity;
        struct v4l2_ctrl_handler ctrls;
 
-       struct vsp1_pipeline *pipe;
        struct vsp1_video *video;
 
        unsigned int max_width;
 
 static struct vsp1_vb2_buffer *
 vsp1_video_complete_buffer(struct vsp1_video *video)
 {
-       struct vsp1_pipeline *pipe = video->rwpf->pipe;
+       struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
        struct vsp1_vb2_buffer *next = NULL;
        struct vsp1_vb2_buffer *done;
        unsigned long flags;
                subdev = media_entity_to_v4l2_subdev(entity);
                e = to_vsp1_entity(subdev);
                list_add_tail(&e->list_pipe, &pipe->entities);
+               e->pipe = pipe;
 
                switch (e->type) {
                case VSP1_ENTITY_RPF:
                        rwpf = to_rwpf(subdev);
                        pipe->inputs[rwpf->entity.index] = rwpf;
                        rwpf->video->pipe_index = ++pipe->num_inputs;
-                       rwpf->pipe = pipe;
                        break;
 
                case VSP1_ENTITY_WPF:
                        rwpf = to_rwpf(subdev);
                        pipe->output = rwpf;
                        rwpf->video->pipe_index = 0;
-                       rwpf->pipe = pipe;
                        break;
 
                case VSP1_ENTITY_LIF:
 
                case VSP1_ENTITY_HGO:
                        pipe->hgo = e;
-                       to_hgo(subdev)->histo.pipe = pipe;
                        break;
 
                case VSP1_ENTITY_HGT:
                        pipe->hgt = e;
-                       to_hgt(subdev)->histo.pipe = pipe;
                        break;
 
                default:
         * Otherwise allocate a new pipeline and initialize it, it will be freed
         * when the last reference is released.
         */
-       if (!video->rwpf->pipe) {
+       if (!video->rwpf->entity.pipe) {
                pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
                if (!pipe)
                        return ERR_PTR(-ENOMEM);
                        return ERR_PTR(ret);
                }
        } else {
-               pipe = video->rwpf->pipe;
+               pipe = video->rwpf->entity.pipe;
                kref_get(&pipe->kref);
        }
 
 {
        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
        struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
-       struct vsp1_pipeline *pipe = video->rwpf->pipe;
+       struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
        struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
        unsigned long flags;
        bool empty;
 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
 {
        struct vsp1_video *video = vb2_get_drv_priv(vq);
-       struct vsp1_pipeline *pipe = video->rwpf->pipe;
+       struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
        bool start_pipeline = false;
        unsigned long flags;
        int ret;
 static void vsp1_video_stop_streaming(struct vb2_queue *vq)
 {
        struct vsp1_video *video = vb2_get_drv_priv(vq);
-       struct vsp1_pipeline *pipe = video->rwpf->pipe;
+       struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
        unsigned long flags;
        int ret;