V4L/DVB (12536): soc-camera: remove .gain and .exposure struct soc_camera_device...
[linux-2.6-block.git] / drivers / media / video / soc_camera_platform.c
CommitLineData
326c9862
MD
1/*
2 * Generic Platform Camera Driver
3 *
4 * Copyright (C) 2008 Magnus Damm
5 * Based on mt9m001 driver,
6 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/delay.h>
17#include <linux/platform_device.h>
18#include <linux/videodev2.h>
979ea1dd 19#include <media/v4l2-subdev.h>
326c9862 20#include <media/soc_camera.h>
50c616fd 21#include <media/soc_camera_platform.h>
326c9862
MD
22
23struct soc_camera_platform_priv {
979ea1dd 24 struct v4l2_subdev subdev;
326c9862
MD
25 struct soc_camera_data_format format;
26};
27
08590b96 28static struct soc_camera_platform_priv *get_priv(struct platform_device *pdev)
326c9862 29{
08590b96
GL
30 struct v4l2_subdev *subdev = platform_get_drvdata(pdev);
31 return container_of(subdev, struct soc_camera_platform_priv, subdev);
32}
33
34static struct soc_camera_platform_info *get_info(struct soc_camera_device *icd)
35{
96c75399
GL
36 struct platform_device *pdev =
37 to_platform_device(to_soc_camera_control(icd));
40e2e092 38 return pdev->dev.platform_data;
326c9862
MD
39}
40
979ea1dd 41static int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable)
326c9862 42{
979ea1dd
GL
43 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
44 return p->set_capture(p, enable);
326c9862
MD
45}
46
47static int soc_camera_platform_set_bus_param(struct soc_camera_device *icd,
48 unsigned long flags)
49{
50 return 0;
51}
52
53static unsigned long
54soc_camera_platform_query_bus_param(struct soc_camera_device *icd)
55{
08590b96 56 struct soc_camera_platform_info *p = get_info(icd);
326c9862
MD
57 return p->bus_param;
58}
59
979ea1dd 60static int soc_camera_platform_try_fmt(struct v4l2_subdev *sd,
09e231b3 61 struct v4l2_format *f)
326c9862 62{
979ea1dd 63 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
64f5905e 64 struct v4l2_pix_format *pix = &f->fmt.pix;
326c9862 65
64f5905e
GL
66 pix->width = p->format.width;
67 pix->height = p->format.height;
326c9862
MD
68 return 0;
69}
70
979ea1dd
GL
71static void soc_camera_platform_video_probe(struct soc_camera_device *icd,
72 struct platform_device *pdev)
326c9862 73{
08590b96 74 struct soc_camera_platform_priv *priv = get_priv(pdev);
40e2e092 75 struct soc_camera_platform_info *p = pdev->dev.platform_data;
326c9862 76
40e2e092
GL
77 priv->format.name = p->format_name;
78 priv->format.depth = p->format_depth;
79 priv->format.fourcc = p->format.pixelformat;
80 priv->format.colorspace = p->format.colorspace;
326c9862
MD
81
82 icd->formats = &priv->format;
83 icd->num_formats = 1;
326c9862
MD
84}
85
979ea1dd
GL
86static struct v4l2_subdev_core_ops platform_subdev_core_ops;
87
88static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
89 .s_stream = soc_camera_platform_s_stream,
90 .try_fmt = soc_camera_platform_try_fmt,
91};
92
93static struct v4l2_subdev_ops platform_subdev_ops = {
94 .core = &platform_subdev_core_ops,
95 .video = &platform_subdev_video_ops,
96};
97
326c9862 98static struct soc_camera_ops soc_camera_platform_ops = {
326c9862
MD
99 .set_bus_param = soc_camera_platform_set_bus_param,
100 .query_bus_param = soc_camera_platform_query_bus_param,
101};
102
103static int soc_camera_platform_probe(struct platform_device *pdev)
104{
979ea1dd 105 struct soc_camera_host *ici;
326c9862 106 struct soc_camera_platform_priv *priv;
40e2e092 107 struct soc_camera_platform_info *p = pdev->dev.platform_data;
326c9862
MD
108 struct soc_camera_device *icd;
109 int ret;
110
326c9862
MD
111 if (!p)
112 return -EINVAL;
113
979ea1dd
GL
114 if (!p->dev) {
115 dev_err(&pdev->dev,
116 "Platform has not set soc_camera_device pointer!\n");
117 return -EINVAL;
118 }
119
326c9862
MD
120 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
121 if (!priv)
122 return -ENOMEM;
123
40e2e092 124 icd = to_soc_camera_dev(p->dev);
40e2e092 125
08590b96
GL
126 /* soc-camera convention: control's drvdata points to the subdev */
127 platform_set_drvdata(pdev, &priv->subdev);
128 /* Set the control device reference */
40e2e092 129 dev_set_drvdata(&icd->dev, &pdev->dev);
979ea1dd 130
a0705b07
GL
131 icd->y_skip_top = 0;
132 icd->ops = &soc_camera_platform_ops;
326c9862 133
979ea1dd
GL
134 ici = to_soc_camera_host(icd->dev.parent);
135
136 soc_camera_platform_video_probe(icd, pdev);
137
138 v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
139 v4l2_set_subdevdata(&priv->subdev, p);
140 priv->subdev.grp_id = (__u32)icd;
141 strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE);
142
143 ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
144 if (ret)
145 goto evdrs;
326c9862
MD
146
147 return ret;
40e2e092 148
979ea1dd
GL
149evdrs:
150 icd->ops = NULL;
151 platform_set_drvdata(pdev, NULL);
40e2e092 152 kfree(priv);
979ea1dd 153 return ret;
326c9862
MD
154}
155
156static int soc_camera_platform_remove(struct platform_device *pdev)
157{
08590b96 158 struct soc_camera_platform_priv *priv = get_priv(pdev);
40e2e092
GL
159 struct soc_camera_platform_info *p = pdev->dev.platform_data;
160 struct soc_camera_device *icd = to_soc_camera_dev(p->dev);
326c9862 161
979ea1dd 162 v4l2_device_unregister_subdev(&priv->subdev);
40e2e092 163 icd->ops = NULL;
979ea1dd 164 platform_set_drvdata(pdev, NULL);
326c9862
MD
165 kfree(priv);
166 return 0;
167}
168
169static struct platform_driver soc_camera_platform_driver = {
170 .driver = {
171 .name = "soc_camera_platform",
979ea1dd 172 .owner = THIS_MODULE,
326c9862
MD
173 },
174 .probe = soc_camera_platform_probe,
175 .remove = soc_camera_platform_remove,
176};
177
178static int __init soc_camera_platform_module_init(void)
179{
180 return platform_driver_register(&soc_camera_platform_driver);
181}
182
183static void __exit soc_camera_platform_module_exit(void)
184{
01c1e4ca 185 platform_driver_unregister(&soc_camera_platform_driver);
326c9862
MD
186}
187
188module_init(soc_camera_platform_module_init);
189module_exit(soc_camera_platform_module_exit);
190
191MODULE_DESCRIPTION("SoC Camera Platform driver");
192MODULE_AUTHOR("Magnus Damm");
193MODULE_LICENSE("GPL v2");
40e2e092 194MODULE_ALIAS("platform:soc_camera_platform");