rcar-core.c 8.3 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-fwnode.h>
25 26 27 28 29 30 31 32 33

#include "rcar-vin.h"

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

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

34 35 36 37 38 39 40 41 42 43 44 45 46 47
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;
}

48
static bool rvin_mbus_supported(struct rvin_graph_entity *entity)
49
{
50
	struct v4l2_subdev *sd = entity->subdev;
51 52 53 54 55
	struct v4l2_subdev_mbus_code_enum code = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};

	code.index = 0;
56
	code.pad = entity->source_pad;
57 58 59 60
	while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
		code.index++;
		switch (code.code) {
		case MEDIA_BUS_FMT_YUYV8_1X16:
61 62
		case MEDIA_BUS_FMT_UYVY8_2X8:
		case MEDIA_BUS_FMT_UYVY10_2X10:
63
		case MEDIA_BUS_FMT_RGB888_1X24:
64
			entity->code = code.code;
65 66 67 68 69 70 71 72 73
			return true;
		default:
			break;
		}
	}

	return false;
}

74
static int rvin_digital_notify_complete(struct v4l2_async_notifier *notifier)
75 76 77 78
{
	struct rvin_dev *vin = notifier_to_vin(notifier);
	int ret;

79
	/* Verify subdevices mbus format */
80
	if (!rvin_mbus_supported(&vin->digital)) {
81 82 83 84 85 86
		vin_err(vin, "Unsupported media bus format for %s\n",
			vin->digital.subdev->name);
		return -EINVAL;
	}

	vin_dbg(vin, "Found media bus format for %s: %d\n",
87
		vin->digital.subdev->name, vin->digital.code);
88

89 90 91 92 93 94 95 96 97
	ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
	if (ret < 0) {
		vin_err(vin, "Failed to register subdev nodes\n");
		return ret;
	}

	return rvin_v4l2_probe(vin);
}

98 99 100
static void rvin_digital_notify_unbind(struct v4l2_async_notifier *notifier,
				       struct v4l2_subdev *subdev,
				       struct v4l2_async_subdev *asd)
101 102 103
{
	struct rvin_dev *vin = notifier_to_vin(notifier);

104 105 106 107 108 109 110 111
	if (vin->digital.subdev == subdev) {
		vin_dbg(vin, "unbind digital subdev %s\n", subdev->name);
		rvin_v4l2_remove(vin);
		vin->digital.subdev = NULL;
		return;
	}

	vin_err(vin, "no entity for subdev %s to unbind\n", subdev->name);
112 113
}

114 115 116
static int rvin_digital_notify_bound(struct v4l2_async_notifier *notifier,
				     struct v4l2_subdev *subdev,
				     struct v4l2_async_subdev *asd)
117 118
{
	struct rvin_dev *vin = notifier_to_vin(notifier);
119
	int ret;
120

121
	v4l2_set_subdev_hostdata(subdev, vin);
122

123 124
	if (vin->digital.asd.match.fwnode.fwnode ==
	    of_fwnode_handle(subdev->dev->of_node)) {
125 126 127 128 129 130 131 132 133 134
		/* 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;

		ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
		vin->digital.sink_pad = ret < 0 ? 0 : ret;

135
		vin->digital.subdev = subdev;
136 137 138 139

		vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n",
			subdev->name, vin->digital.source_pad,
			vin->digital.sink_pad);
140 141
		return 0;
	}
142

143 144
	vin_err(vin, "no entity for subdev %s to bind\n", subdev->name);
	return -EINVAL;
145 146
}

147 148 149
static int rvin_digitial_parse_v4l2(struct rvin_dev *vin,
				    struct device_node *ep,
				    struct v4l2_mbus_config *mbus_cfg)
150
{
151
	struct v4l2_fwnode_endpoint v4l2_ep;
152
	int ret;
153

154
	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &v4l2_ep);
155 156 157 158
	if (ret) {
		vin_err(vin, "Could not parse v4l2 endpoint\n");
		return -EINVAL;
	}
159

160
	mbus_cfg->type = v4l2_ep.bus_type;
161

162 163 164 165 166 167 168 169 170 171 172 173 174
	switch (mbus_cfg->type) {
	case V4L2_MBUS_PARALLEL:
		vin_dbg(vin, "Found PARALLEL media bus\n");
		mbus_cfg->flags = v4l2_ep.bus.parallel.flags;
		break;
	case V4L2_MBUS_BT656:
		vin_dbg(vin, "Found BT656 media bus\n");
		mbus_cfg->flags = 0;
		break;
	default:
		vin_err(vin, "Unknown media bus type\n");
		return -EINVAL;
	}
175

176 177 178 179 180 181 182 183
	return 0;
}

static int rvin_digital_graph_parse(struct rvin_dev *vin)
{
	struct device_node *ep, *np;
	int ret;

184
	vin->digital.asd.match.fwnode.fwnode = NULL;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	vin->digital.subdev = NULL;

	/*
	 * Port 0 id 0 is local digital input, try to get it.
	 * Not all instances can or will have this, that is OK
	 */
	ep = of_graph_get_endpoint_by_regs(vin->dev->of_node, 0, 0);
	if (!ep)
		return 0;

	np = of_graph_get_remote_port_parent(ep);
	if (!np) {
		vin_err(vin, "No remote parent for digital input\n");
		of_node_put(ep);
		return -EINVAL;
200
	}
201
	of_node_put(np);
202

203
	ret = rvin_digitial_parse_v4l2(vin, ep, &vin->digital.mbus_cfg);
204
	of_node_put(ep);
205 206
	if (ret)
		return ret;
207

208 209
	vin->digital.asd.match.fwnode.fwnode = of_fwnode_handle(np);
	vin->digital.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
210 211

	return 0;
212 213
}

214
static int rvin_digital_graph_init(struct rvin_dev *vin)
215 216 217 218
{
	struct v4l2_async_subdev **subdevs = NULL;
	int ret;

219 220 221
	ret = rvin_digital_graph_parse(vin);
	if (ret)
		return ret;
222

223
	if (!vin->digital.asd.match.fwnode.fwnode) {
224 225
		vin_dbg(vin, "No digital subdevice found\n");
		return -ENODEV;
226 227 228 229
	}

	/* Register the subdevices notifier. */
	subdevs = devm_kzalloc(vin->dev, sizeof(*subdevs), GFP_KERNEL);
230 231
	if (subdevs == NULL)
		return -ENOMEM;
232

233
	subdevs[0] = &vin->digital.asd;
234

235
	vin_dbg(vin, "Found digital subdevice %s\n",
236
		of_node_full_name(to_of_node(subdevs[0]->match.fwnode.fwnode)));
237

238
	vin->notifier.num_subdevs = 1;
239
	vin->notifier.subdevs = subdevs;
240 241 242
	vin->notifier.bound = rvin_digital_notify_bound;
	vin->notifier.unbind = rvin_digital_notify_unbind;
	vin->notifier.complete = rvin_digital_notify_complete;
243 244 245 246

	ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier);
	if (ret < 0) {
		vin_err(vin, "Notifier registration failed\n");
247
		return ret;
248 249
	}

250
	return 0;
251 252 253 254 255 256 257 258 259 260 261 262 263
}

/* -----------------------------------------------------------------------------
 * Platform Device Driver
 */

static const struct of_device_id rvin_of_id_table[] = {
	{ .compatible = "renesas,vin-r8a7794", .data = (void *)RCAR_GEN2 },
	{ .compatible = "renesas,vin-r8a7793", .data = (void *)RCAR_GEN2 },
	{ .compatible = "renesas,vin-r8a7791", .data = (void *)RCAR_GEN2 },
	{ .compatible = "renesas,vin-r8a7790", .data = (void *)RCAR_GEN2 },
	{ .compatible = "renesas,vin-r8a7779", .data = (void *)RCAR_H1 },
	{ .compatible = "renesas,vin-r8a7778", .data = (void *)RCAR_M1 },
264
	{ .compatible = "renesas,rcar-gen2-vin", .data = (void *)RCAR_GEN2 },
265 266 267 268 269 270
	{ },
};
MODULE_DEVICE_TABLE(of, rvin_of_id_table);

static int rcar_vin_probe(struct platform_device *pdev)
{
271
	const struct of_device_id *match;
272 273 274 275 276 277 278 279
	struct rvin_dev *vin;
	struct resource *mem;
	int irq, ret;

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

280 281 282
	match = of_match_device(of_match_ptr(rvin_of_id_table), &pdev->dev);
	if (!match)
		return -ENODEV;
283

284 285
	vin->dev = &pdev->dev;
	vin->chip = (enum chip_id)match->data;
286 287 288 289 290 291 292 293 294 295

	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);
296 297
	if (irq < 0)
		return irq;
298 299 300 301 302

	ret = rvin_dma_probe(vin, irq);
	if (ret)
		return ret;

303
	ret = rvin_digital_graph_init(vin);
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
	if (ret < 0)
		goto error;

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

	platform_set_drvdata(pdev, vin);

	return 0;
error:
	rvin_dma_remove(vin);

	return ret;
}

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

	pm_runtime_disable(&pdev->dev);

	v4l2_async_notifier_unregister(&vin->notifier);

	rvin_dma_remove(vin);

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