]>
Commit | Line | Data |
---|---|---|
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 <linux/platform_data/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 int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable) | |
34 | { | |
35 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
36 | return p->set_capture(p, enable); | |
37 | } | |
38 | ||
39 | static int soc_camera_platform_fill_fmt(struct v4l2_subdev *sd, | |
40 | struct v4l2_subdev_pad_config *cfg, | |
41 | struct v4l2_subdev_format *format) | |
42 | { | |
43 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
44 | struct v4l2_mbus_framefmt *mf = &format->format; | |
45 | ||
46 | mf->width = p->format.width; | |
47 | mf->height = p->format.height; | |
48 | mf->code = p->format.code; | |
49 | mf->colorspace = p->format.colorspace; | |
50 | mf->field = p->format.field; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static int soc_camera_platform_s_power(struct v4l2_subdev *sd, int on) | |
56 | { | |
57 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
58 | ||
59 | return soc_camera_set_power(p->icd->control, &p->icd->sdesc->subdev_desc, NULL, on); | |
60 | } | |
61 | ||
62 | static const struct v4l2_subdev_core_ops platform_subdev_core_ops = { | |
63 | .s_power = soc_camera_platform_s_power, | |
64 | }; | |
65 | ||
66 | static int soc_camera_platform_enum_mbus_code(struct v4l2_subdev *sd, | |
67 | struct v4l2_subdev_pad_config *cfg, | |
68 | struct v4l2_subdev_mbus_code_enum *code) | |
69 | { | |
70 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
71 | ||
72 | if (code->pad || code->index) | |
73 | return -EINVAL; | |
74 | ||
75 | code->code = p->format.code; | |
76 | return 0; | |
77 | } | |
78 | ||
79 | static int soc_camera_platform_get_selection(struct v4l2_subdev *sd, | |
80 | struct v4l2_subdev_pad_config *cfg, | |
81 | struct v4l2_subdev_selection *sel) | |
82 | { | |
83 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
84 | ||
85 | if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) | |
86 | return -EINVAL; | |
87 | ||
88 | switch (sel->target) { | |
89 | case V4L2_SEL_TGT_CROP_BOUNDS: | |
90 | case V4L2_SEL_TGT_CROP_DEFAULT: | |
91 | case V4L2_SEL_TGT_CROP: | |
92 | sel->r.left = 0; | |
93 | sel->r.top = 0; | |
94 | sel->r.width = p->format.width; | |
95 | sel->r.height = p->format.height; | |
96 | return 0; | |
97 | default: | |
98 | return -EINVAL; | |
99 | } | |
100 | } | |
101 | ||
102 | static int soc_camera_platform_g_mbus_config(struct v4l2_subdev *sd, | |
103 | struct v4l2_mbus_config *cfg) | |
104 | { | |
105 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
106 | ||
107 | cfg->flags = p->mbus_param; | |
108 | cfg->type = p->mbus_type; | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
113 | static const struct v4l2_subdev_video_ops platform_subdev_video_ops = { | |
114 | .s_stream = soc_camera_platform_s_stream, | |
115 | .g_mbus_config = soc_camera_platform_g_mbus_config, | |
116 | }; | |
117 | ||
118 | static const struct v4l2_subdev_pad_ops platform_subdev_pad_ops = { | |
119 | .enum_mbus_code = soc_camera_platform_enum_mbus_code, | |
120 | .get_selection = soc_camera_platform_get_selection, | |
121 | .get_fmt = soc_camera_platform_fill_fmt, | |
122 | .set_fmt = soc_camera_platform_fill_fmt, | |
123 | }; | |
124 | ||
125 | static const struct v4l2_subdev_ops platform_subdev_ops = { | |
126 | .core = &platform_subdev_core_ops, | |
127 | .video = &platform_subdev_video_ops, | |
128 | .pad = &platform_subdev_pad_ops, | |
129 | }; | |
130 | ||
131 | static int soc_camera_platform_probe(struct platform_device *pdev) | |
132 | { | |
133 | struct soc_camera_host *ici; | |
134 | struct soc_camera_platform_priv *priv; | |
135 | struct soc_camera_platform_info *p = pdev->dev.platform_data; | |
136 | struct soc_camera_device *icd; | |
137 | ||
138 | if (!p) | |
139 | return -EINVAL; | |
140 | ||
141 | if (!p->icd) { | |
142 | dev_err(&pdev->dev, | |
143 | "Platform has not set soc_camera_device pointer!\n"); | |
144 | return -EINVAL; | |
145 | } | |
146 | ||
147 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); | |
148 | if (!priv) | |
149 | return -ENOMEM; | |
150 | ||
151 | icd = p->icd; | |
152 | ||
153 | /* soc-camera convention: control's drvdata points to the subdev */ | |
154 | platform_set_drvdata(pdev, &priv->subdev); | |
155 | /* Set the control device reference */ | |
156 | icd->control = &pdev->dev; | |
157 | ||
158 | ici = to_soc_camera_host(icd->parent); | |
159 | ||
160 | v4l2_subdev_init(&priv->subdev, &platform_subdev_ops); | |
161 | v4l2_set_subdevdata(&priv->subdev, p); | |
162 | strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE); | |
163 | ||
164 | return v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev); | |
165 | } | |
166 | ||
167 | static int soc_camera_platform_remove(struct platform_device *pdev) | |
168 | { | |
169 | struct soc_camera_platform_priv *priv = get_priv(pdev); | |
170 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(&priv->subdev); | |
171 | ||
172 | p->icd->control = NULL; | |
173 | v4l2_device_unregister_subdev(&priv->subdev); | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static struct platform_driver soc_camera_platform_driver = { | |
178 | .driver = { | |
179 | .name = "soc_camera_platform", | |
180 | }, | |
181 | .probe = soc_camera_platform_probe, | |
182 | .remove = soc_camera_platform_remove, | |
183 | }; | |
184 | ||
185 | module_platform_driver(soc_camera_platform_driver); | |
186 | ||
187 | MODULE_DESCRIPTION("SoC Camera Platform driver"); | |
188 | MODULE_AUTHOR("Magnus Damm"); | |
189 | MODULE_LICENSE("GPL v2"); | |
190 | MODULE_ALIAS("platform:soc_camera_platform"); |