summaryrefslogtreecommitdiff
path: root/src/libcamera/pipeline/ipu3/ipu3.cpp
blob: 9629057a1b2ff60427ea0df8bd2fdaf355522896 (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
/* 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"
#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::vector<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(const Camera *camera) override;
	void stop(const Camera *camera) override;

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

	bool match(DeviceEnumerator *enumerator);

private:
	class IPU3CameraData : public CameraData
	{
	public:
		IPU3CameraData()
			: 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::vector<Stream *> &streams)
{
	IPU3CameraData *data = cameraData(camera);

	std::map<Stream *, StreamConfiguration> configs;

	StreamConfiguration config{};

	LOG(IPU3, Info) << "TODO: Return a good default format";

	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_];

	LOG(IPU3, Info) << "TODO: Configure the camera for resolution "
			<< cfg->width << "x" << cfg->height;

	return 0;
}

int PipelineHandlerIPU3::allocateBuffers(Camera *camera, Stream *stream)
{
	return -ENOTRECOVERABLE;
}

int PipelineHandlerIPU3::freeBuffers(Camera *camera, Stream *stream)
{
	return 0;
}

int PipelineHandlerIPU3::start(const Camera *camera)
{
	LOG(IPU3, Error) << "TODO: start camera";
	return 0;
}

void PipelineHandlerIPU3::stop(const Camera *camera)
{
	LOG(IPU3, Error) << "TODO: stop camera";
}

int PipelineHandlerIPU3::queueRequest(const Camera *camera, Request *request)
{
	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");

	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;
}

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

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

		/*
		 * 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;

		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 */