rcar-core.c 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Driver for Renesas R-Car VIN
 *
 * Copyright (C) 2016 Renesas Electronics Corp.
 * Copyright (C) 2011-2013 Renesas Solutions Corp.
 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
 * Copyright (C) 2008 Magnus Damm
 *
 * Based on the soc-camera rcar_vin driver
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_graph.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>

24
#include <media/v4l2-async.h>
25
#include <media/v4l2-fwnode.h>
26 27 28 29 30 31 32 33 34

#include "rcar-vin.h"

/* -----------------------------------------------------------------------------
 * Async notifier
 */

#define notifier_to_vin(n) container_of(n, struct rvin_dev, notifier)

35 36 37 38 39 40 41 42 43 44 45 46 47 48
static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
{
	unsigned int pad;

	if (sd->entity.num_pads <= 1)
		return 0;

	for (pad = 0; pad < sd->entity.num_pads; pad++)
		if (sd->entity.pads[pad].flags & direction)
			return pad;

	return -EINVAL;
}

49 50 51 52
/* -----------------------------------------------------------------------------
 * Digital async notifier
 */

53 54 55
/* The vin lock should be held when calling the subdevice attach and detach */
static int rvin_digital_subdevice_attach(struct rvin_dev *vin,
					 struct v4l2_subdev *subdev)
56 57 58 59
{
	struct v4l2_subdev_mbus_code_enum code = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
60 61 62 63 64 65 66
	int ret;

	/* Find source and sink pad of remote subdevice */
	ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
	if (ret < 0)
		return ret;
	vin->digital->source_pad = ret;
67

68 69 70 71
	ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
	vin->digital->sink_pad = ret < 0 ? 0 : ret;

	/* Find compatible subdevices mbus format */
72
	vin->mbus_code = 0;
73
	code.index = 0;
74
	code.pad = vin->digital->source_pad;
75
	while (!vin->mbus_code &&
76
	       !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
77 78 79
		code.index++;
		switch (code.code) {
		case MEDIA_BUS_FMT_YUYV8_1X16:
80 81
		case MEDIA_BUS_FMT_UYVY8_2X8:
		case MEDIA_BUS_FMT_UYVY10_2X10:
82
		case MEDIA_BUS_FMT_RGB888_1X24:
83
			vin->mbus_code = code.code;
84
			vin_dbg(vin, "Found media bus format for %s: %d\n",
85
				subdev->name, vin->mbus_code);
86
			break;
87 88 89 90 91
		default:
			break;
		}
	}

92
	if (!vin->mbus_code) {
93 94 95 96 97 98 99 100 101 102
		vin_err(vin, "Unsupported media bus format for %s\n",
			subdev->name);
		return -EINVAL;
	}

	/* Read tvnorms */
	ret = v4l2_subdev_call(subdev, video, g_tvnorms, &vin->vdev.tvnorms);
	if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
		return ret;

103 104 105 106 107 108
	/* Read standard */
	vin->std = V4L2_STD_UNKNOWN;
	ret = v4l2_subdev_call(subdev, video, g_std, &vin->std);
	if (ret < 0 && ret != -ENOIOCTLCMD)
		return ret;

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
	/* Add the controls */
	ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16);
	if (ret < 0)
		return ret;

	ret = v4l2_ctrl_add_handler(&vin->ctrl_handler, subdev->ctrl_handler,
				    NULL);
	if (ret < 0) {
		v4l2_ctrl_handler_free(&vin->ctrl_handler);
		return ret;
	}

	vin->vdev.ctrl_handler = &vin->ctrl_handler;

	vin->digital->subdev = subdev;

	return 0;
}

static void rvin_digital_subdevice_detach(struct rvin_dev *vin)
{
	rvin_v4l2_unregister(vin);
	v4l2_ctrl_handler_free(&vin->ctrl_handler);

	vin->vdev.ctrl_handler = NULL;
	vin->digital->subdev = NULL;
135 136
}

137
static int rvin_digital_notify_complete(struct v4l2_async_notifier *notifier)
138 139 140 141 142 143 144 145 146 147
{
	struct rvin_dev *vin = notifier_to_vin(notifier);
	int ret;

	ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
	if (ret < 0) {
		vin_err(vin, "Failed to register subdev nodes\n");
		return ret;
	}

148
	return rvin_v4l2_register(vin);
149 150
}

151 152 153
static void rvin_digital_notify_unbind(struct v4l2_async_notifier *notifier,
				       struct v4l2_subdev *subdev,
				       struct v4l2_async_subdev *asd)
154 155 156
{
	struct rvin_dev *vin = notifier_to_vin(notifier);

157
	vin_dbg(vin, "unbind digital subdev %s\n", subdev->name);
158 159 160 161

	mutex_lock(&vin->lock);
	rvin_digital_subdevice_detach(vin);
	mutex_unlock(&vin->lock);
162 163
}

164 165 166
static int rvin_digital_notify_bound(struct v4l2_async_notifier *notifier,
				     struct v4l2_subdev *subdev,
				     struct v4l2_async_subdev *asd)
167 168
{
	struct rvin_dev *vin = notifier_to_vin(notifier);
169
	int ret;
170

171 172 173 174
	mutex_lock(&vin->lock);
	ret = rvin_digital_subdevice_attach(vin, subdev);
	mutex_unlock(&vin->lock);
	if (ret)
175
		return ret;
176

177
	v4l2_set_subdev_hostdata(subdev, vin);
178

179
	vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n",
180 181
		subdev->name, vin->digital->source_pad,
		vin->digital->sink_pad);
182

183
	return 0;
184
}
185

186 187 188 189 190 191
static const struct v4l2_async_notifier_operations rvin_digital_notify_ops = {
	.bound = rvin_digital_notify_bound,
	.unbind = rvin_digital_notify_unbind,
	.complete = rvin_digital_notify_complete,
};

192 193 194
static int rvin_digital_parse_v4l2(struct device *dev,
				   struct v4l2_fwnode_endpoint *vep,
				   struct v4l2_async_subdev *asd)
195
{
196 197 198
	struct rvin_dev *vin = dev_get_drvdata(dev);
	struct rvin_graph_entity *rvge =
		container_of(asd, struct rvin_graph_entity, asd);
199

200 201
	if (vep->base.port || vep->base.id)
		return -ENOTCONN;
202

203
	vin->mbus_cfg.type = vep->bus_type;
204

205
	switch (vin->mbus_cfg.type) {
206 207
	case V4L2_MBUS_PARALLEL:
		vin_dbg(vin, "Found PARALLEL media bus\n");
208
		vin->mbus_cfg.flags = vep->bus.parallel.flags;
209 210 211
		break;
	case V4L2_MBUS_BT656:
		vin_dbg(vin, "Found BT656 media bus\n");
212
		vin->mbus_cfg.flags = 0;
213 214 215 216 217
		break;
	default:
		vin_err(vin, "Unknown media bus type\n");
		return -EINVAL;
	}
218

219
	vin->digital = rvge;
220 221

	return 0;
222 223
}

224
static int rvin_digital_graph_init(struct rvin_dev *vin)
225 226 227
{
	int ret;

228 229 230
	ret = v4l2_async_notifier_parse_fwnode_endpoints(
		vin->dev, &vin->notifier,
		sizeof(struct rvin_graph_entity), rvin_digital_parse_v4l2);
231 232
	if (ret)
		return ret;
233

234
	if (!vin->digital)
235
		return -ENODEV;
236

237
	vin_dbg(vin, "Found digital subdevice %pOF\n",
238
		to_of_node(vin->digital->asd.match.fwnode));
239

240
	vin->notifier.ops = &rvin_digital_notify_ops;
241 242 243
	ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier);
	if (ret < 0) {
		vin_err(vin, "Notifier registration failed\n");
244
		return ret;
245 246
	}

247
	return 0;
248 249
}

250 251 252 253 254 255 256 257 258 259
static int rvin_mc_init(struct rvin_dev *vin)
{
	/* All our sources are CSI-2 */
	vin->mbus_cfg.type = V4L2_MBUS_CSI2;
	vin->mbus_cfg.flags = 0;

	vin->pad.flags = MEDIA_PAD_FL_SINK;
	return media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
}

260 261 262 263
/* -----------------------------------------------------------------------------
 * Platform Device Driver
 */

264 265
static const struct rvin_info rcar_info_h1 = {
	.model = RCAR_H1,
266
	.use_mc = false,
267 268
	.max_width = 2048,
	.max_height = 2048,
269 270 271 272
};

static const struct rvin_info rcar_info_m1 = {
	.model = RCAR_M1,
273
	.use_mc = false,
274 275
	.max_width = 2048,
	.max_height = 2048,
276 277 278 279
};

static const struct rvin_info rcar_info_gen2 = {
	.model = RCAR_GEN2,
280
	.use_mc = false,
281 282
	.max_width = 2048,
	.max_height = 2048,
283 284
};

285
static const struct of_device_id rvin_of_id_table[] = {
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
	{
		.compatible = "renesas,vin-r8a7778",
		.data = &rcar_info_m1,
	},
	{
		.compatible = "renesas,vin-r8a7779",
		.data = &rcar_info_h1,
	},
	{
		.compatible = "renesas,vin-r8a7790",
		.data = &rcar_info_gen2,
	},
	{
		.compatible = "renesas,vin-r8a7791",
		.data = &rcar_info_gen2,
	},
	{
		.compatible = "renesas,vin-r8a7793",
		.data = &rcar_info_gen2,
	},
	{
		.compatible = "renesas,vin-r8a7794",
		.data = &rcar_info_gen2,
	},
	{
		.compatible = "renesas,rcar-gen2-vin",
		.data = &rcar_info_gen2,
	},
	{ /* Sentinel */ },
315 316 317 318 319 320 321 322 323 324 325 326 327
};
MODULE_DEVICE_TABLE(of, rvin_of_id_table);

static int rcar_vin_probe(struct platform_device *pdev)
{
	struct rvin_dev *vin;
	struct resource *mem;
	int irq, ret;

	vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
	if (!vin)
		return -ENOMEM;

328
	vin->dev = &pdev->dev;
329
	vin->info = of_device_get_match_data(&pdev->dev);
330 331 332 333 334 335 336 337 338 339

	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (mem == NULL)
		return -EINVAL;

	vin->base = devm_ioremap_resource(vin->dev, mem);
	if (IS_ERR(vin->base))
		return PTR_ERR(vin->base);

	irq = platform_get_irq(pdev, 0);
340 341
	if (irq < 0)
		return irq;
342

343
	ret = rvin_dma_register(vin, irq);
344 345 346
	if (ret)
		return ret;

347
	platform_set_drvdata(pdev, vin);
348 349 350 351
	if (vin->info->use_mc)
		ret = rvin_mc_init(vin);
	else
		ret = rvin_digital_graph_init(vin);
352 353 354 355 356 357 358 359
	if (ret < 0)
		goto error;

	pm_suspend_ignore_children(&pdev->dev, true);
	pm_runtime_enable(&pdev->dev);

	return 0;
error:
360
	rvin_dma_unregister(vin);
361
	v4l2_async_notifier_cleanup(&vin->notifier);
362 363 364 365 366 367 368 369 370 371

	return ret;
}

static int rcar_vin_remove(struct platform_device *pdev)
{
	struct rvin_dev *vin = platform_get_drvdata(pdev);

	pm_runtime_disable(&pdev->dev);

372 373
	rvin_v4l2_unregister(vin);

374
	v4l2_async_notifier_unregister(&vin->notifier);
375
	v4l2_async_notifier_cleanup(&vin->notifier);
376

377 378
	if (!vin->info->use_mc)
		v4l2_ctrl_handler_free(&vin->ctrl_handler);
379

380
	rvin_dma_unregister(vin);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

	return 0;
}

static struct platform_driver rcar_vin_driver = {
	.driver = {
		.name = "rcar-vin",
		.of_match_table = rvin_of_id_table,
	},
	.probe = rcar_vin_probe,
	.remove = rcar_vin_remove,
};

module_platform_driver(rcar_vin_driver);

MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
MODULE_LICENSE("GPL v2");