summaryrefslogtreecommitdiff
path: root/src/libcamera/pipeline/ipu3/ipu3.cpp
blob: 52844da78419943dec3766d64274346cf9e800d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * ipu3.cpp - Pipeline handler for Intel IPU3
 */

#include <memory>
#include <vector>

#include <libcamera/camera.h>
#include <libcamera/stream.h>

#include "device_enumerator.h"
#include "log.h"
#include "media_device.h"
#include "pipeline_handler.h"
#include "utils.h"
#include "v4l2_device.h"

namespace libcamera {

LOG_DEFINE_CATEGORY(IPU3)

class PipelineHandlerIPU3 : public PipelineHandler
{
public:
	PipelineHandlerIPU3(CameraManager *manager);
	~PipelineHandlerIPU3();

	bool match(DeviceEnumerator *enumerator);

private:
	class IPU3CameraData : public CameraData
	{
	public:
		IPU3CameraData()
			: dev_(nullptr) {}
		~IPU3CameraData() { delete dev_; }
		V4L2Device *dev_;
		Stream stream_;
	};

	std::shared_ptr<MediaDevice> cio2_;
	std::shared_ptr<MediaDevice> imgu_;

	IPU3CameraData *cameraData(const Camera *camera)
	{
		return static_cast<IPU3CameraData *>(
			PipelineHandler::cameraData(camera));
	}

	V4L2Device *createVideoDevice(unsigned int id);
	void registerCameras();
};

PipelineHandlerIPU3::PipelineHandlerIPU3(CameraManager *manager)
	: PipelineHandler(manager), cio2_(nullptr), imgu_(nullptr)
{
}

PipelineHandlerIPU3::~PipelineHandlerIPU3()
{
	if (cio2_)
		cio2_->release();

	if (imgu_)
		imgu_->release();
}

bool PipelineHandlerIPU3::match(DeviceEnumerator *enumerator)
{
	DeviceMatch cio2_dm("ipu3-cio2");
	cio2_dm.add("ipu3-csi2 0");
	cio2_dm.add("ipu3-cio2 0");
	cio2_dm.add("ipu3-csi2 1");
	cio2_dm.add("ipu3-cio2 1");
	cio2_dm.add("ipu3-csi2 2");
	cio2_dm.add("ipu3-cio2 2");
	cio2_dm.add("ipu3-csi2 3");
	cio2_dm.add("ipu3-cio2 3");

	DeviceMatch imgu_dm("ipu3-imgu");
	imgu_dm.add("ipu3-imgu 0");
	imgu_dm.add("ipu3-imgu 0 input");
	imgu_dm.add("ipu3-imgu 0 parameters");
	imgu_dm.add("ipu3-imgu 0 output");
	imgu_dm.add("ipu3-imgu 0 viewfinder");
	imgu_dm.add("ipu3-imgu 0 3a stat");
	imgu_dm.add("ipu3-imgu 1");
	imgu_dm.add("ipu3-imgu 1 input");
	imgu_dm.add("ipu3-imgu 1 parameters");
	imgu_dm.add("ipu3-imgu 1 output");
	imgu_dm.add("ipu3-imgu 1 viewfinder");
	imgu_dm.add("ipu3-imgu 1 3a stat");

	cio2_ = std::move(enumerator->search(cio2_dm));
	if (!cio2_)
		return false;

	imgu_ = std::move(enumerator->search(imgu_dm));
	if (!imgu_)
		return false;

	/*
	 * It is safe to acquire both media devices at this point as
	 * DeviceEnumerator::search() skips the busy ones for us.
	 */
	cio2_->acquire();
	imgu_->acquire();

	/*
	 * Disable all links that are enabled by default on CIO2, as camera
	 * creation enables all valid links it finds.
	 *
	 * Close the CIO2 media device after, as links are enabled and should
	 * not need to be changed after.
	 */
	if (cio2_->open())
		goto error_release_mdev;

	if (cio2_->disableLinks())
		goto error_close_cio2;

	registerCameras();

	cio2_->close();

	return true;

error_close_cio2:
	cio2_->close();

error_release_mdev:
	cio2_->release();
	imgu_->release();

	return false;
}

/* Create video devices for the CIO2 unit associated with a camera. */
V4L2Device *PipelineHandlerIPU3::createVideoDevice(unsigned int id)
{
	std::string cio2Name = "ipu3-cio2 " + std::to_string(id);
	MediaEntity *cio2 = cio2_->getEntityByName(cio2Name);
	if (!cio2)
		return nullptr;

	V4L2Device *dev = new V4L2Device(*cio2);
	if (dev->open()) {
		delete dev;
		return nullptr;
	}
	dev->close();

	return dev;
}

/*
 * Cameras are created associating an image sensor (represented by a
 * media entity with function MEDIA_ENT_F_CAM_SENSOR) to one of the four
 * CIO2 CSI-2 receivers.
 */
void PipelineHandlerIPU3::registerCameras()
{
	/*
	 * For each CSI-2 receiver on the IPU3, create a Camera if an
	 * image sensor is connected to it.
	 */
	unsigned int numCameras = 0;
	for (unsigned int id = 0; id < 4; ++id) {
		std::string csi2Name = "ipu3-csi2 " + std::to_string(id);
		MediaEntity *csi2 = cio2_->getEntityByName(csi2Name);

		/*
		 * This shall not happen, as the device enumerator matched
		 * all entities described in the cio2_dm DeviceMatch.
		 *
		 * As this check is basically free, better stay safe than sorry.
		 */
		if (!csi2)
			continue;

		const std::vector<MediaPad *> &pads = csi2->pads();
		if (pads.empty())
			continue;

		/* IPU3 CSI-2 receivers have a single sink pad at index 0. */
		MediaPad *sink = pads[0];
		const std::vector<MediaLink *> &links = sink->links();
		if (links.empty())
			continue;

		/*
		 * Verify that the receiver is connected to a sensor, enable
		 * the media link between the two, and create a Camera with
		 * a unique name.
		 */
		MediaLink *link = links[0];
		MediaEntity *sensor = link->source()->entity();
		if (sensor->function() != MEDIA_ENT_F_CAM_SENSOR)
			continue;

		if (link->setEnabled(true))
			continue;

		std::unique_ptr<IPU3CameraData> data = utils::make_unique<IPU3CameraData>();

		std::string cameraName = sensor->name() + " " + std::to_string(id);
		std::vector<Stream *> streams{ &data->stream_ };
		std::shared_ptr<Camera> camera = Camera::create(this, cameraName, streams);

		/*
		 * If V4L2 device creation fails, the Camera instance won't be
		 * registered. The 'camera' shared pointer goes out of scope
		 * and deletes the Camera it manages.
		 */
		data->dev_ = createVideoDevice(id);
		if (!data->dev_) {
			LOG(IPU3, Error)
				<< "Failed to register camera["
				<< numCameras << "] \"" << cameraName << "\"";
			continue;
		}

		setCameraData(camera.get(), std::move(data));
		registerCamera(std::move(camera));

		LOG(IPU3, Info)
			<< "Registered Camera[" << numCameras << "] \""
			<< cameraName << "\""
			<< " connected to CSI-2 receiver " << id;

		numCameras++;
	}
}

REGISTER_PIPELINE_HANDLER(PipelineHandlerIPU3);

} /* namespace libcamera */