2 * Generic Platform Camera Driver
4 * Copyright (C) 2008 Magnus Damm
5 * Based on mt9m001 driver,
6 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
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>
23 struct soc_camera_platform_priv
{
24 struct v4l2_subdev subdev
;
27 static struct soc_camera_platform_priv
*get_priv(struct platform_device
*pdev
)
29 struct v4l2_subdev
*subdev
= platform_get_drvdata(pdev
);
30 return container_of(subdev
, struct soc_camera_platform_priv
, subdev
);
33 static struct soc_camera_platform_info
*get_info(struct soc_camera_device
*icd
)
35 struct platform_device
*pdev
=
36 to_platform_device(to_soc_camera_control(icd
));
37 return pdev
->dev
.platform_data
;
40 static int soc_camera_platform_s_stream(struct v4l2_subdev
*sd
, int enable
)
42 struct soc_camera_platform_info
*p
= v4l2_get_subdevdata(sd
);
43 return p
->set_capture(p
, enable
);
46 static int soc_camera_platform_set_bus_param(struct soc_camera_device
*icd
,
53 soc_camera_platform_query_bus_param(struct soc_camera_device
*icd
)
55 struct soc_camera_platform_info
*p
= get_info(icd
);
59 static int soc_camera_platform_fill_fmt(struct v4l2_subdev
*sd
,
60 struct v4l2_mbus_framefmt
*mf
)
62 struct soc_camera_platform_info
*p
= v4l2_get_subdevdata(sd
);
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
;
73 static struct v4l2_subdev_core_ops platform_subdev_core_ops
;
75 static int soc_camera_platform_enum_fmt(struct v4l2_subdev
*sd
, unsigned int index
,
76 enum v4l2_mbus_pixelcode
*code
)
78 struct soc_camera_platform_info
*p
= v4l2_get_subdevdata(sd
);
83 *code
= p
->format
.code
;
87 static int soc_camera_platform_g_crop(struct v4l2_subdev
*sd
,
90 struct soc_camera_platform_info
*p
= v4l2_get_subdevdata(sd
);
94 a
->c
.width
= p
->format
.width
;
95 a
->c
.height
= p
->format
.height
;
96 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
101 static int soc_camera_platform_cropcap(struct v4l2_subdev
*sd
,
102 struct v4l2_cropcap
*a
)
104 struct soc_camera_platform_info
*p
= v4l2_get_subdevdata(sd
);
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;
118 static struct v4l2_subdev_video_ops platform_subdev_video_ops
= {
119 .s_stream
= soc_camera_platform_s_stream
,
120 .enum_mbus_fmt
= soc_camera_platform_enum_fmt
,
121 .cropcap
= soc_camera_platform_cropcap
,
122 .g_crop
= soc_camera_platform_g_crop
,
123 .try_mbus_fmt
= soc_camera_platform_fill_fmt
,
124 .g_mbus_fmt
= soc_camera_platform_fill_fmt
,
125 .s_mbus_fmt
= soc_camera_platform_fill_fmt
,
128 static struct v4l2_subdev_ops platform_subdev_ops
= {
129 .core
= &platform_subdev_core_ops
,
130 .video
= &platform_subdev_video_ops
,
133 static struct soc_camera_ops soc_camera_platform_ops
= {
134 .set_bus_param
= soc_camera_platform_set_bus_param
,
135 .query_bus_param
= soc_camera_platform_query_bus_param
,
138 static int soc_camera_platform_probe(struct platform_device
*pdev
)
140 struct soc_camera_host
*ici
;
141 struct soc_camera_platform_priv
*priv
;
142 struct soc_camera_platform_info
*p
= pdev
->dev
.platform_data
;
143 struct soc_camera_device
*icd
;
151 "Platform has not set soc_camera_device pointer!\n");
155 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
159 icd
= to_soc_camera_dev(p
->dev
);
161 /* soc-camera convention: control's drvdata points to the subdev */
162 platform_set_drvdata(pdev
, &priv
->subdev
);
163 /* Set the control device reference */
164 dev_set_drvdata(&icd
->dev
, &pdev
->dev
);
166 icd
->ops
= &soc_camera_platform_ops
;
168 ici
= to_soc_camera_host(icd
->dev
.parent
);
170 v4l2_subdev_init(&priv
->subdev
, &platform_subdev_ops
);
171 v4l2_set_subdevdata(&priv
->subdev
, p
);
172 strncpy(priv
->subdev
.name
, dev_name(&pdev
->dev
), V4L2_SUBDEV_NAME_SIZE
);
174 ret
= v4l2_device_register_subdev(&ici
->v4l2_dev
, &priv
->subdev
);
182 platform_set_drvdata(pdev
, NULL
);
187 static int soc_camera_platform_remove(struct platform_device
*pdev
)
189 struct soc_camera_platform_priv
*priv
= get_priv(pdev
);
190 struct soc_camera_platform_info
*p
= pdev
->dev
.platform_data
;
191 struct soc_camera_device
*icd
= to_soc_camera_dev(p
->dev
);
193 v4l2_device_unregister_subdev(&priv
->subdev
);
195 platform_set_drvdata(pdev
, NULL
);
200 static struct platform_driver soc_camera_platform_driver
= {
202 .name
= "soc_camera_platform",
203 .owner
= THIS_MODULE
,
205 .probe
= soc_camera_platform_probe
,
206 .remove
= soc_camera_platform_remove
,
209 static int __init
soc_camera_platform_module_init(void)
211 return platform_driver_register(&soc_camera_platform_driver
);
214 static void __exit
soc_camera_platform_module_exit(void)
216 platform_driver_unregister(&soc_camera_platform_driver
);
219 module_init(soc_camera_platform_module_init
);
220 module_exit(soc_camera_platform_module_exit
);
222 MODULE_DESCRIPTION("SoC Camera Platform driver");
223 MODULE_AUTHOR("Magnus Damm");
224 MODULE_LICENSE("GPL v2");
225 MODULE_ALIAS("platform:soc_camera_platform");