soc_camera_platform.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Generic Platform Camera Driver
 *
 * Copyright (C) 2008 Magnus Damm
 * Based on mt9m001 driver,
 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/videodev2.h>
#include <media/v4l2-common.h>
#include <media/soc_camera.h>
21
#include <media/soc_camera_platform.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

struct soc_camera_platform_priv {
	struct soc_camera_platform_info *info;
	struct soc_camera_device icd;
	struct soc_camera_data_format format;
};

static struct soc_camera_platform_info *
soc_camera_platform_get_info(struct soc_camera_device *icd)
{
	struct soc_camera_platform_priv *priv;
	priv = container_of(icd, struct soc_camera_platform_priv, icd);
	return priv->info;
}

static int soc_camera_platform_init(struct soc_camera_device *icd)
{
39 40 41 42 43
	struct soc_camera_platform_info *p = soc_camera_platform_get_info(icd);

	if (p->power)
		p->power(1);

44 45 46 47 48
	return 0;
}

static int soc_camera_platform_release(struct soc_camera_device *icd)
{
49 50 51 52 53
	struct soc_camera_platform_info *p = soc_camera_platform_get_info(icd);

	if (p->power)
		p->power(0);

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

static int soc_camera_platform_start_capture(struct soc_camera_device *icd)
{
	struct soc_camera_platform_info *p = soc_camera_platform_get_info(icd);
	return p->set_capture(p, 1);
}

static int soc_camera_platform_stop_capture(struct soc_camera_device *icd)
{
	struct soc_camera_platform_info *p = soc_camera_platform_get_info(icd);
	return p->set_capture(p, 0);
}

static int soc_camera_platform_set_bus_param(struct soc_camera_device *icd,
					     unsigned long flags)
{
	return 0;
}

static unsigned long
soc_camera_platform_query_bus_param(struct soc_camera_device *icd)
{
	struct soc_camera_platform_info *p = soc_camera_platform_get_info(icd);
	return p->bus_param;
}

82 83
static int soc_camera_platform_set_fmt(struct soc_camera_device *icd,
				       __u32 pixfmt, struct v4l2_rect *rect)
84 85 86 87
{
	return 0;
}

88 89
static int soc_camera_platform_try_fmt(struct soc_camera_device *icd,
				       struct v4l2_format *f)
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
{
	struct soc_camera_platform_info *p = soc_camera_platform_get_info(icd);

	f->fmt.pix.width = p->format.width;
	f->fmt.pix.height = p->format.height;
	return 0;
}

static int soc_camera_platform_video_probe(struct soc_camera_device *icd)
{
	struct soc_camera_platform_priv *priv;
	priv = container_of(icd, struct soc_camera_platform_priv, icd);

	priv->format.name = priv->info->format_name;
	priv->format.depth = priv->info->format_depth;
	priv->format.fourcc = priv->info->format.pixelformat;
	priv->format.colorspace = priv->info->format.colorspace;

	icd->formats = &priv->format;
	icd->num_formats = 1;

	return soc_camera_video_start(icd);
}

static void soc_camera_platform_video_remove(struct soc_camera_device *icd)
{
	soc_camera_video_stop(icd);
}

static struct soc_camera_ops soc_camera_platform_ops = {
	.owner			= THIS_MODULE,
	.probe			= soc_camera_platform_video_probe,
	.remove			= soc_camera_platform_video_remove,
	.init			= soc_camera_platform_init,
	.release		= soc_camera_platform_release,
	.start_capture		= soc_camera_platform_start_capture,
	.stop_capture		= soc_camera_platform_stop_capture,
127 128
	.set_fmt		= soc_camera_platform_set_fmt,
	.try_fmt		= soc_camera_platform_try_fmt,
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
	.set_bus_param		= soc_camera_platform_set_bus_param,
	.query_bus_param	= soc_camera_platform_query_bus_param,
};

static int soc_camera_platform_probe(struct platform_device *pdev)
{
	struct soc_camera_platform_priv *priv;
	struct soc_camera_platform_info *p;
	struct soc_camera_device *icd;
	int ret;

	p = pdev->dev.platform_data;
	if (!p)
		return -EINVAL;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->info = p;
	platform_set_drvdata(pdev, priv);

	icd = &priv->icd;
	icd->ops	= &soc_camera_platform_ops;
	icd->control	= &pdev->dev;
	icd->width_min	= 0;
	icd->width_max	= priv->info->format.width;
	icd->height_min	= 0;
	icd->height_max	= priv->info->format.height;
	icd->y_skip_top	= 0;
	icd->iface	= priv->info->iface;

	ret = soc_camera_device_register(icd);
	if (ret)
		kfree(priv);

	return ret;
}

static int soc_camera_platform_remove(struct platform_device *pdev)
{
	struct soc_camera_platform_priv *priv = platform_get_drvdata(pdev);

	soc_camera_device_unregister(&priv->icd);
	kfree(priv);
	return 0;
}

static struct platform_driver soc_camera_platform_driver = {
	.driver 	= {
		.name	= "soc_camera_platform",
	},
	.probe		= soc_camera_platform_probe,
	.remove		= soc_camera_platform_remove,
};

static int __init soc_camera_platform_module_init(void)
{
	return platform_driver_register(&soc_camera_platform_driver);
}

static void __exit soc_camera_platform_module_exit(void)
{
192
	platform_driver_unregister(&soc_camera_platform_driver);
193 194 195 196 197 198 199 200
}

module_init(soc_camera_platform_module_init);
module_exit(soc_camera_platform_module_exit);

MODULE_DESCRIPTION("SoC Camera Platform driver");
MODULE_AUTHOR("Magnus Damm");
MODULE_LICENSE("GPL v2");