[media] V4L: soc_camera_platform: support the new mbus-config subdev ops
[pandora-kernel.git] / drivers / media / video / soc_camera_platform.c
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>
19 #include <media/v4l2-subdev.h>
20 #include <media/soc_camera.h>
21 #include <media/soc_camera_platform.h>
22
23 struct soc_camera_platform_priv {
24         struct v4l2_subdev subdev;
25 };
26
27 static struct soc_camera_platform_priv *get_priv(struct platform_device *pdev)
28 {
29         struct v4l2_subdev *subdev = platform_get_drvdata(pdev);
30         return container_of(subdev, struct soc_camera_platform_priv, subdev);
31 }
32
33 static struct soc_camera_platform_info *get_info(struct soc_camera_device *icd)
34 {
35         struct platform_device *pdev =
36                 to_platform_device(to_soc_camera_control(icd));
37         return pdev->dev.platform_data;
38 }
39
40 static int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable)
41 {
42         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
43         return p->set_capture(p, enable);
44 }
45
46 static int soc_camera_platform_set_bus_param(struct soc_camera_device *icd,
47                                              unsigned long flags)
48 {
49         return 0;
50 }
51
52 static unsigned long
53 soc_camera_platform_query_bus_param(struct soc_camera_device *icd)
54 {
55         struct soc_camera_platform_info *p = get_info(icd);
56         return p->bus_param;
57 }
58
59 static int soc_camera_platform_fill_fmt(struct v4l2_subdev *sd,
60                                         struct v4l2_mbus_framefmt *mf)
61 {
62         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
63
64         mf->width       = p->format.width;
65         mf->height      = p->format.height;
66         mf->code        = p->format.code;
67         mf->colorspace  = p->format.colorspace;
68         mf->field       = p->format.field;
69
70         return 0;
71 }
72
73 static struct v4l2_subdev_core_ops platform_subdev_core_ops;
74
75 static int soc_camera_platform_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
76                                         enum v4l2_mbus_pixelcode *code)
77 {
78         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
79
80         if (index)
81                 return -EINVAL;
82
83         *code = p->format.code;
84         return 0;
85 }
86
87 static int soc_camera_platform_g_crop(struct v4l2_subdev *sd,
88                                       struct v4l2_crop *a)
89 {
90         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
91
92         a->c.left       = 0;
93         a->c.top        = 0;
94         a->c.width      = p->format.width;
95         a->c.height     = p->format.height;
96         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
97
98         return 0;
99 }
100
101 static int soc_camera_platform_cropcap(struct v4l2_subdev *sd,
102                                        struct v4l2_cropcap *a)
103 {
104         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
105
106         a->bounds.left                  = 0;
107         a->bounds.top                   = 0;
108         a->bounds.width                 = p->format.width;
109         a->bounds.height                = p->format.height;
110         a->defrect                      = a->bounds;
111         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
112         a->pixelaspect.numerator        = 1;
113         a->pixelaspect.denominator      = 1;
114
115         return 0;
116 }
117
118 static int soc_camera_platform_g_mbus_config(struct v4l2_subdev *sd,
119                                              struct v4l2_mbus_config *cfg)
120 {
121         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
122
123         cfg->flags = p->mbus_param;
124         cfg->type = p->mbus_type;
125
126         return 0;
127 }
128
129 static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
130         .s_stream       = soc_camera_platform_s_stream,
131         .enum_mbus_fmt  = soc_camera_platform_enum_fmt,
132         .cropcap        = soc_camera_platform_cropcap,
133         .g_crop         = soc_camera_platform_g_crop,
134         .try_mbus_fmt   = soc_camera_platform_fill_fmt,
135         .g_mbus_fmt     = soc_camera_platform_fill_fmt,
136         .s_mbus_fmt     = soc_camera_platform_fill_fmt,
137         .g_mbus_config  = soc_camera_platform_g_mbus_config,
138 };
139
140 static struct v4l2_subdev_ops platform_subdev_ops = {
141         .core   = &platform_subdev_core_ops,
142         .video  = &platform_subdev_video_ops,
143 };
144
145 static struct soc_camera_ops soc_camera_platform_ops = {
146         .set_bus_param          = soc_camera_platform_set_bus_param,
147         .query_bus_param        = soc_camera_platform_query_bus_param,
148 };
149
150 static int soc_camera_platform_probe(struct platform_device *pdev)
151 {
152         struct soc_camera_host *ici;
153         struct soc_camera_platform_priv *priv;
154         struct soc_camera_platform_info *p = pdev->dev.platform_data;
155         struct soc_camera_device *icd;
156         int ret;
157
158         if (!p)
159                 return -EINVAL;
160
161         if (!p->icd) {
162                 dev_err(&pdev->dev,
163                         "Platform has not set soc_camera_device pointer!\n");
164                 return -EINVAL;
165         }
166
167         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
168         if (!priv)
169                 return -ENOMEM;
170
171         icd = p->icd;
172
173         /* soc-camera convention: control's drvdata points to the subdev */
174         platform_set_drvdata(pdev, &priv->subdev);
175         /* Set the control device reference */
176         icd->control = &pdev->dev;
177
178         icd->ops = &soc_camera_platform_ops;
179
180         ici = to_soc_camera_host(icd->parent);
181
182         v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
183         v4l2_set_subdevdata(&priv->subdev, p);
184         strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE);
185
186         ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
187         if (ret)
188                 goto evdrs;
189
190         return ret;
191
192 evdrs:
193         icd->ops = NULL;
194         platform_set_drvdata(pdev, NULL);
195         kfree(priv);
196         return ret;
197 }
198
199 static int soc_camera_platform_remove(struct platform_device *pdev)
200 {
201         struct soc_camera_platform_priv *priv = get_priv(pdev);
202         struct soc_camera_platform_info *p = pdev->dev.platform_data;
203         struct soc_camera_device *icd = p->icd;
204
205         v4l2_device_unregister_subdev(&priv->subdev);
206         icd->ops = NULL;
207         platform_set_drvdata(pdev, NULL);
208         kfree(priv);
209         return 0;
210 }
211
212 static struct platform_driver soc_camera_platform_driver = {
213         .driver         = {
214                 .name   = "soc_camera_platform",
215                 .owner  = THIS_MODULE,
216         },
217         .probe          = soc_camera_platform_probe,
218         .remove         = soc_camera_platform_remove,
219 };
220
221 static int __init soc_camera_platform_module_init(void)
222 {
223         return platform_driver_register(&soc_camera_platform_driver);
224 }
225
226 static void __exit soc_camera_platform_module_exit(void)
227 {
228         platform_driver_unregister(&soc_camera_platform_driver);
229 }
230
231 module_init(soc_camera_platform_module_init);
232 module_exit(soc_camera_platform_module_exit);
233
234 MODULE_DESCRIPTION("SoC Camera Platform driver");
235 MODULE_AUTHOR("Magnus Damm");
236 MODULE_LICENSE("GPL v2");
237 MODULE_ALIAS("platform:soc_camera_platform");