2016-04-26 21:22:19 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
#include <media/v4l2-async.h>
|
2016-08-27 07:17:25 +08:00
|
|
|
#include <media/v4l2-fwnode.h>
|
2016-04-26 21:22:19 +08:00
|
|
|
|
|
|
|
#include "rcar-vin.h"
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* Async notifier
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define notifier_to_vin(n) container_of(n, struct rvin_dev, notifier)
|
|
|
|
|
2017-05-24 08:15:30 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:35 +08:00
|
|
|
static bool rvin_mbus_supported(struct rvin_graph_entity *entity)
|
2016-04-26 21:22:19 +08:00
|
|
|
{
|
2016-08-15 23:06:35 +08:00
|
|
|
struct v4l2_subdev *sd = entity->subdev;
|
2016-04-26 21:22:19 +08:00
|
|
|
struct v4l2_subdev_mbus_code_enum code = {
|
|
|
|
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
|
|
|
|
};
|
|
|
|
|
|
|
|
code.index = 0;
|
2017-05-24 08:15:31 +08:00
|
|
|
code.pad = entity->source_pad;
|
2016-04-26 21:22:19 +08:00
|
|
|
while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
|
|
|
|
code.index++;
|
|
|
|
switch (code.code) {
|
|
|
|
case MEDIA_BUS_FMT_YUYV8_1X16:
|
2016-09-02 23:37:06 +08:00
|
|
|
case MEDIA_BUS_FMT_UYVY8_2X8:
|
|
|
|
case MEDIA_BUS_FMT_UYVY10_2X10:
|
2016-04-26 21:22:19 +08:00
|
|
|
case MEDIA_BUS_FMT_RGB888_1X24:
|
2016-08-15 23:06:35 +08:00
|
|
|
entity->code = code.code;
|
2016-04-26 21:22:19 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:29 +08:00
|
|
|
static int rvin_digital_notify_complete(struct v4l2_async_notifier *notifier)
|
2016-04-26 21:22:19 +08:00
|
|
|
{
|
|
|
|
struct rvin_dev *vin = notifier_to_vin(notifier);
|
|
|
|
int ret;
|
|
|
|
|
2016-08-15 23:06:34 +08:00
|
|
|
/* Verify subdevices mbus format */
|
2017-09-02 06:41:19 +08:00
|
|
|
if (!rvin_mbus_supported(vin->digital)) {
|
2016-08-15 23:06:34 +08:00
|
|
|
vin_err(vin, "Unsupported media bus format for %s\n",
|
2017-09-02 06:41:19 +08:00
|
|
|
vin->digital->subdev->name);
|
2016-08-15 23:06:34 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vin_dbg(vin, "Found media bus format for %s: %d\n",
|
2017-09-02 06:41:19 +08:00
|
|
|
vin->digital->subdev->name, vin->digital->code);
|
2016-08-15 23:06:34 +08:00
|
|
|
|
2016-04-26 21:22:19 +08:00
|
|
|
ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
|
|
|
|
if (ret < 0) {
|
|
|
|
vin_err(vin, "Failed to register subdev nodes\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:56:57 +08:00
|
|
|
return rvin_v4l2_register(vin);
|
2016-04-26 21:22:19 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:29 +08:00
|
|
|
static void rvin_digital_notify_unbind(struct v4l2_async_notifier *notifier,
|
|
|
|
struct v4l2_subdev *subdev,
|
|
|
|
struct v4l2_async_subdev *asd)
|
2016-04-26 21:22:19 +08:00
|
|
|
{
|
|
|
|
struct rvin_dev *vin = notifier_to_vin(notifier);
|
|
|
|
|
2017-05-24 08:15:37 +08:00
|
|
|
vin_dbg(vin, "unbind digital subdev %s\n", subdev->name);
|
2018-04-14 19:56:57 +08:00
|
|
|
rvin_v4l2_unregister(vin);
|
2017-09-02 06:41:19 +08:00
|
|
|
vin->digital->subdev = NULL;
|
2016-04-26 21:22:19 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:29 +08:00
|
|
|
static int rvin_digital_notify_bound(struct v4l2_async_notifier *notifier,
|
|
|
|
struct v4l2_subdev *subdev,
|
|
|
|
struct v4l2_async_subdev *asd)
|
2016-04-26 21:22:19 +08:00
|
|
|
{
|
|
|
|
struct rvin_dev *vin = notifier_to_vin(notifier);
|
2017-05-24 08:15:30 +08:00
|
|
|
int ret;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2016-08-15 23:06:34 +08:00
|
|
|
v4l2_set_subdev_hostdata(subdev, vin);
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-05-24 08:15:37 +08:00
|
|
|
/* Find source and sink pad of remote subdevice */
|
2017-05-24 08:15:30 +08:00
|
|
|
|
2017-05-24 08:15:37 +08:00
|
|
|
ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-09-02 06:41:19 +08:00
|
|
|
vin->digital->source_pad = ret;
|
2017-05-24 08:15:30 +08:00
|
|
|
|
2017-05-24 08:15:37 +08:00
|
|
|
ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
|
2017-09-02 06:41:19 +08:00
|
|
|
vin->digital->sink_pad = ret < 0 ? 0 : ret;
|
2017-05-24 08:15:30 +08:00
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
vin->digital->subdev = subdev;
|
2017-05-24 08:15:30 +08:00
|
|
|
|
2017-05-24 08:15:37 +08:00
|
|
|
vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n",
|
2017-09-02 06:41:19 +08:00
|
|
|
subdev->name, vin->digital->source_pad,
|
|
|
|
vin->digital->sink_pad);
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-05-24 08:15:37 +08:00
|
|
|
return 0;
|
2016-04-26 21:22:19 +08:00
|
|
|
}
|
2017-08-31 01:18:04 +08:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
static int rvin_digital_parse_v4l2(struct device *dev,
|
|
|
|
struct v4l2_fwnode_endpoint *vep,
|
|
|
|
struct v4l2_async_subdev *asd)
|
2016-04-26 21:22:19 +08:00
|
|
|
{
|
2017-09-02 06:41:19 +08:00
|
|
|
struct rvin_dev *vin = dev_get_drvdata(dev);
|
|
|
|
struct rvin_graph_entity *rvge =
|
|
|
|
container_of(asd, struct rvin_graph_entity, asd);
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
if (vep->base.port || vep->base.id)
|
|
|
|
return -ENOTCONN;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
rvge->mbus_cfg.type = vep->bus_type;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
switch (rvge->mbus_cfg.type) {
|
2016-08-15 23:06:34 +08:00
|
|
|
case V4L2_MBUS_PARALLEL:
|
|
|
|
vin_dbg(vin, "Found PARALLEL media bus\n");
|
2017-09-02 06:41:19 +08:00
|
|
|
rvge->mbus_cfg.flags = vep->bus.parallel.flags;
|
2016-08-15 23:06:34 +08:00
|
|
|
break;
|
|
|
|
case V4L2_MBUS_BT656:
|
|
|
|
vin_dbg(vin, "Found BT656 media bus\n");
|
2017-09-02 06:41:19 +08:00
|
|
|
rvge->mbus_cfg.flags = 0;
|
2016-08-15 23:06:34 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
vin_err(vin, "Unknown media bus type\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
vin->digital = rvge;
|
2016-08-15 23:06:34 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-04-26 21:22:19 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:34 +08:00
|
|
|
static int rvin_digital_graph_init(struct rvin_dev *vin)
|
2016-04-26 21:22:19 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
ret = v4l2_async_notifier_parse_fwnode_endpoints(
|
|
|
|
vin->dev, &vin->notifier,
|
|
|
|
sizeof(struct rvin_graph_entity), rvin_digital_parse_v4l2);
|
2016-08-15 23:06:34 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
if (!vin->digital)
|
2016-08-15 23:06:34 +08:00
|
|
|
return -ENODEV;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2017-07-22 03:28:33 +08:00
|
|
|
vin_dbg(vin, "Found digital subdevice %pOF\n",
|
2017-09-27 22:12:00 +08:00
|
|
|
to_of_node(vin->digital->asd.match.fwnode));
|
2016-08-15 23:06:34 +08:00
|
|
|
|
2017-08-31 01:18:04 +08:00
|
|
|
vin->notifier.ops = &rvin_digital_notify_ops;
|
2016-04-26 21:22:19 +08:00
|
|
|
ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier);
|
|
|
|
if (ret < 0) {
|
|
|
|
vin_err(vin, "Notifier registration failed\n");
|
2016-08-15 23:06:34 +08:00
|
|
|
return ret;
|
2016-04-26 21:22:19 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:34 +08:00
|
|
|
return 0;
|
2016-04-26 21:22:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* 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 },
|
2016-07-26 03:19:33 +08:00
|
|
|
{ .compatible = "renesas,rcar-gen2-vin", .data = (void *)RCAR_GEN2 },
|
2016-04-26 21:22:19 +08:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, rvin_of_id_table);
|
|
|
|
|
|
|
|
static int rcar_vin_probe(struct platform_device *pdev)
|
|
|
|
{
|
2016-08-15 23:06:34 +08:00
|
|
|
const struct of_device_id *match;
|
2016-04-26 21:22:19 +08:00
|
|
|
struct rvin_dev *vin;
|
|
|
|
struct resource *mem;
|
|
|
|
int irq, ret;
|
|
|
|
|
|
|
|
vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
|
|
|
|
if (!vin)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-08-15 23:06:34 +08:00
|
|
|
match = of_match_device(of_match_ptr(rvin_of_id_table), &pdev->dev);
|
|
|
|
if (!match)
|
|
|
|
return -ENODEV;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2016-08-15 23:06:34 +08:00
|
|
|
vin->dev = &pdev->dev;
|
|
|
|
vin->chip = (enum chip_id)match->data;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
|
|
|
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);
|
2016-08-15 23:06:30 +08:00
|
|
|
if (irq < 0)
|
|
|
|
return irq;
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2018-04-14 19:56:57 +08:00
|
|
|
ret = rvin_dma_register(vin, irq);
|
2016-04-26 21:22:19 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-09-02 06:41:19 +08:00
|
|
|
platform_set_drvdata(pdev, vin);
|
|
|
|
|
2016-08-15 23:06:34 +08:00
|
|
|
ret = rvin_digital_graph_init(vin);
|
2016-04-26 21:22:19 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
pm_suspend_ignore_children(&pdev->dev, true);
|
|
|
|
pm_runtime_enable(&pdev->dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
error:
|
2018-04-14 19:56:57 +08:00
|
|
|
rvin_dma_unregister(vin);
|
2017-09-02 06:41:19 +08:00
|
|
|
v4l2_async_notifier_cleanup(&vin->notifier);
|
2016-04-26 21:22:19 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_vin_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct rvin_dev *vin = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
|
|
|
|
2018-04-14 19:56:58 +08:00
|
|
|
rvin_v4l2_unregister(vin);
|
|
|
|
|
2016-04-26 21:22:19 +08:00
|
|
|
v4l2_async_notifier_unregister(&vin->notifier);
|
2017-09-02 06:41:19 +08:00
|
|
|
v4l2_async_notifier_cleanup(&vin->notifier);
|
2016-04-26 21:22:19 +08:00
|
|
|
|
2018-04-14 19:56:57 +08:00
|
|
|
rvin_dma_unregister(vin);
|
2016-04-26 21:22:19 +08:00
|
|
|
|
|
|
|
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");
|