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