summaryrefslogtreecommitdiff
path: root/src/libcamera/pipeline/ipu3/ipu3.cpp
blob: 79dbfa13f7aecada6cce25c14f3e8b6c5dccde97 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* 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/request.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"
#include "v4l2_subdevice.h"

namespace libcamera {

LOG_DEFINE_CATEGORY(IPU3)

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

	std::map<Stream *, StreamConfiguration>
	streamConfiguration(Camera *camera,
			    std::set<Stream *> &streams) override;
	int configureStreams(Camera *camera,
			     std::map<Stream *, StreamConfiguration> &config) override;

	int allocateBuffers(Camera *camera, Stream *stream) override;
	int freeBuffers(Camera *camera, Stream *stream) override;

	int start(Camera *camera) override;
	void stop(Camera *camera) override;

	int queueRequest(Camera *camera, Request *request) override;

	bool match(DeviceEnumerator *enumerator);

private:
	class IPU3CameraData : public CameraData
	{
	public:
		IPU3CameraData(PipelineHandler *pipe)
			: CameraData(pipe), cio2_(nullptr), csi2_(nullptr),
			  sensor_(nullptr)
		{
		}

		~IPU3CameraData()
		{
			delete cio2_;
			delete csi2_;
			delete sensor_;
		}

		V4L2Device *cio2_;
		V4L2Subdevice *csi2_;
		V4L2Subdevice *sensor_;

		Stream stream_;
	};

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

	void registerCameras();

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

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

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

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

std::map<Stream *, StreamConfiguration>
PipelineHandlerIPU3::streamConfiguration(Camera *camera,
					 std::set<Stream *> &streams)
{
	IPU3CameraData *data = cameraData(camera);
	std::map<Stream *, StreamConfiguration> configs;
	V4L2SubdeviceFormat format = {};

	/*
	 * FIXME: As of now, return the image format reported by the sensor.
	 * In future good defaults should be provided for each stream.
	 */
	if (data->sensor_->getFormat(0, &format)) {
		LOG(IPU3, Error) << "Failed to create stream configurations";
		return configs;
	}

	StreamConfiguration config = {};
	config.width = format.width;
	config.height = format.height;
	config.pixelFormat = V4L2_PIX_FMT_IPU3_SGRBG10;
	config.bufferCount = 4;

	configs[&data->stream_] = config;

	return configs;
}

int PipelineHandlerIPU3::configureStreams(Camera *camera,
					  std::map<Stream *, StreamConfiguration> &config)
{
	IPU3CameraData *data = cameraData(camera);
	StreamConfiguration *cfg = &config[&data->stream_];
	V4L2Subdevice *sensor = data->sensor_;
	V4L2Subdevice *csi2 = data->csi2_;
	V4L2Device *cio2 = data->cio2_;
	V4L2SubdeviceFormat subdevFormat = {};
	V4L2DeviceFormat devFormat = {};
	int ret;

	/*
	 * FIXME: as of now, the format gets applied to the sensor and is
	 * propagated along the pipeline. It should instead be applied on the
	 * capture device and the sensor format calculated accordingly.
	 */

	ret = sensor->getFormat(0, &subdevFormat);
	if (ret)
		return ret;

	subdevFormat.width = cfg->width;
	subdevFormat.height = cfg->height;
	ret = sensor->setFormat(0, &subdevFormat);
	if (ret)
		return ret;

	/* Return error if the requested format cannot be applied to sensor. */
	if (subdevFormat.width != cfg->width ||
	    subdevFormat.height != cfg->height) {
		LOG(IPU3, Error)
			<< "Failed to apply image format "
			<< subdevFormat.width << "x" << subdevFormat.height
			<< " - got: " << cfg->width << "x" << cfg->height;
		return -EINVAL;
	}

	ret = csi2->setFormat(0, &subdevFormat);
	if (ret)
		return ret;

	ret = cio2->getFormat(&devFormat);
	if (ret)
		return ret;

	devFormat.width = subdevFormat.width;
	devFormat.height = subdevFormat.height;
	devFormat.fourcc = cfg->pixelFormat;

	ret = cio2->setFormat(&devFormat);
	if (ret)
		return ret;

	LOG(IPU3, Info) << cio2->driverName() << ": "
			<< devFormat.width << "x" << devFormat.height
			<< "- 0x" << std::hex << devFormat.fourcc << " planes: "
			<< devFormat.planes;

	return 0;
}

int PipelineHandlerIPU3::allocateBuffers(Camera *camera, Stream *stream)
{
	IPU3CameraData *data = cameraData(camera);
	const StreamConfiguration &cfg = stream->configuration();

	if (!cfg.bufferCount)
		return -EINVAL;

	int ret = data->cio2_->exportBuffers(&stream->bufferPool());
	if (ret) {
		LOG(IPU3, Error) << "Failed to request memory";
		return ret;
	}

	return 0;
}

int PipelineHandlerIPU3::freeBuffers(Camera *camera, Stream *stream)
{
	IPU3CameraData *data = cameraData(camera);

	int ret = data->cio2_->releaseBuffers();
	if (ret) {
		LOG(IPU3, Error) << "Failed to release memory";
		return ret;
	}

	return 0;
}

int PipelineHandlerIPU3::start(Camera *camera)
{
	IPU3CameraData *data = cameraData(camera);
	int ret;

	ret = data->cio2_->streamOn();
	if (ret) {
		LOG(IPU3, Info) << "Failed to start camera " << camera->name();
		return ret;
	}

	return 0;
}

void PipelineHandlerIPU3::stop(Camera *camera)
{
	IPU3CameraData *data = cameraData(camera);

	if (data->cio2_->streamOff())
		LOG(IPU3, Info) << "Failed to stop camera " << camera->name();
}

int PipelineHandlerIPU3::queueRequest(Camera *camera, Request *request)
{
	IPU3CameraData *data = cameraData(camera);
	Stream *stream = &data->stream_;

	Buffer *buffer = request->findBuffer(stream);
	if (!buffer) {
		LOG(IPU3, Error)
			<< "Attempt to queue request with invalid stream";
		return -ENOENT;
	}

	data->cio2_->queueBuffer(buffer);

	return 0;
}

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");

	/*
	 * It is safe to acquire both media devices at this point as
	 * DeviceEnumerator::search() skips the busy ones for us.
	 */
	cio2_ = enumerator->search(cio2_dm);
	if (!cio2_)
		return false;

	cio2_->acquire();

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

	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())
		return false;

	if (cio2_->disableLinks()) {
		cio2_->close();
		return false;
	}

	registerCameras();

	cio2_->close();

	return true;
}

/*
 * 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);
		int ret;

		/*
		 * 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>(this);

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

		/*
		 * Create and open video devices and subdevices associated with
		 * the camera.
		 *
		 * If any of these operations fails, the Camera instance won't
		 * be registered. The 'camera' shared pointer and the 'data'
		 * unique pointers go out of scope and delete the objects they
		 * manage.
		 */
		std::string cio2Name = "ipu3-cio2 " + std::to_string(id);
		MediaEntity *cio2 = cio2_->getEntityByName(cio2Name);
		if (!cio2) {
			LOG(IPU3, Error)
				<< "Failed to get entity '" << cio2Name << "'";
			continue;
		}

		data->cio2_ = new V4L2Device(cio2);
		ret = data->cio2_->open();
		if (ret)
			continue;

		data->sensor_ = new V4L2Subdevice(sensor);
		ret = data->sensor_->open();
		if (ret)
			continue;

		data->csi2_ = new V4L2Subdevice(csi2);
		ret = data->csi2_->open();
		if (ret)
			continue;

		registerCamera(std::move(camera), std::move(data));

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

		numCameras++;
	}
}

REGISTER_PIPELINE_HANDLER(PipelineHandlerIPU3);

} /* namespace libcamera */