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> |
eb4b0ec7 | 21 | #include <linux/platform_data/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 | 39 | static int soc_camera_platform_fill_fmt(struct v4l2_subdev *sd, |
da298c6d HV |
40 | struct v4l2_subdev_pad_config *cfg, |
41 | struct v4l2_subdev_format *format) | |
326c9862 | 42 | { |
979ea1dd | 43 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); |
da298c6d | 44 | struct v4l2_mbus_framefmt *mf = &format->format; |
326c9862 | 45 | |
760697be GL |
46 | mf->width = p->format.width; |
47 | mf->height = p->format.height; | |
48 | mf->code = p->format.code; | |
49 | mf->colorspace = p->format.colorspace; | |
e7d403f5 | 50 | mf->field = p->format.field; |
760697be | 51 | |
326c9862 MD |
52 | return 0; |
53 | } | |
54 | ||
4ec10bac LP |
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 | ||
9aea470b | 59 | return soc_camera_set_power(p->icd->control, &p->icd->sdesc->subdev_desc, NULL, on); |
4ec10bac LP |
60 | } |
61 | ||
62 | static struct v4l2_subdev_core_ops platform_subdev_core_ops = { | |
63 | .s_power = soc_camera_platform_s_power, | |
64 | }; | |
760697be | 65 | |
ebcff5fc HV |
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) | |
326c9862 | 69 | { |
760697be | 70 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); |
326c9862 | 71 | |
ebcff5fc | 72 | if (code->pad || code->index) |
760697be | 73 | return -EINVAL; |
326c9862 | 74 | |
ebcff5fc | 75 | code->code = p->format.code; |
760697be | 76 | return 0; |
326c9862 MD |
77 | } |
78 | ||
e7d403f5 KM |
79 | static int soc_camera_platform_g_crop(struct v4l2_subdev *sd, |
80 | struct v4l2_crop *a) | |
81 | { | |
82 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
83 | ||
84 | a->c.left = 0; | |
85 | a->c.top = 0; | |
86 | a->c.width = p->format.width; | |
87 | a->c.height = p->format.height; | |
88 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | static int soc_camera_platform_cropcap(struct v4l2_subdev *sd, | |
94 | struct v4l2_cropcap *a) | |
95 | { | |
96 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
97 | ||
98 | a->bounds.left = 0; | |
99 | a->bounds.top = 0; | |
100 | a->bounds.width = p->format.width; | |
101 | a->bounds.height = p->format.height; | |
102 | a->defrect = a->bounds; | |
103 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
104 | a->pixelaspect.numerator = 1; | |
105 | a->pixelaspect.denominator = 1; | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
84c760a5 GL |
110 | static int soc_camera_platform_g_mbus_config(struct v4l2_subdev *sd, |
111 | struct v4l2_mbus_config *cfg) | |
112 | { | |
113 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); | |
114 | ||
115 | cfg->flags = p->mbus_param; | |
116 | cfg->type = p->mbus_type; | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
979ea1dd GL |
121 | static struct v4l2_subdev_video_ops platform_subdev_video_ops = { |
122 | .s_stream = soc_camera_platform_s_stream, | |
e7d403f5 KM |
123 | .cropcap = soc_camera_platform_cropcap, |
124 | .g_crop = soc_camera_platform_g_crop, | |
84c760a5 | 125 | .g_mbus_config = soc_camera_platform_g_mbus_config, |
979ea1dd GL |
126 | }; |
127 | ||
ebcff5fc HV |
128 | static const struct v4l2_subdev_pad_ops platform_subdev_pad_ops = { |
129 | .enum_mbus_code = soc_camera_platform_enum_mbus_code, | |
da298c6d HV |
130 | .get_fmt = soc_camera_platform_fill_fmt, |
131 | .set_fmt = soc_camera_platform_fill_fmt, | |
ebcff5fc HV |
132 | }; |
133 | ||
979ea1dd GL |
134 | static struct v4l2_subdev_ops platform_subdev_ops = { |
135 | .core = &platform_subdev_core_ops, | |
136 | .video = &platform_subdev_video_ops, | |
ebcff5fc | 137 | .pad = &platform_subdev_pad_ops, |
979ea1dd GL |
138 | }; |
139 | ||
326c9862 MD |
140 | static int soc_camera_platform_probe(struct platform_device *pdev) |
141 | { | |
979ea1dd | 142 | struct soc_camera_host *ici; |
326c9862 | 143 | struct soc_camera_platform_priv *priv; |
40e2e092 | 144 | struct soc_camera_platform_info *p = pdev->dev.platform_data; |
326c9862 | 145 | struct soc_camera_device *icd; |
326c9862 | 146 | |
326c9862 MD |
147 | if (!p) |
148 | return -EINVAL; | |
149 | ||
7dfff953 | 150 | if (!p->icd) { |
979ea1dd GL |
151 | dev_err(&pdev->dev, |
152 | "Platform has not set soc_camera_device pointer!\n"); | |
153 | return -EINVAL; | |
154 | } | |
155 | ||
70e176a5 | 156 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
326c9862 MD |
157 | if (!priv) |
158 | return -ENOMEM; | |
159 | ||
7dfff953 | 160 | icd = p->icd; |
40e2e092 | 161 | |
08590b96 GL |
162 | /* soc-camera convention: control's drvdata points to the subdev */ |
163 | platform_set_drvdata(pdev, &priv->subdev); | |
164 | /* Set the control device reference */ | |
7dfff953 | 165 | icd->control = &pdev->dev; |
979ea1dd | 166 | |
7dfff953 | 167 | ici = to_soc_camera_host(icd->parent); |
979ea1dd | 168 | |
979ea1dd GL |
169 | v4l2_subdev_init(&priv->subdev, &platform_subdev_ops); |
170 | v4l2_set_subdevdata(&priv->subdev, p); | |
979ea1dd GL |
171 | strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE); |
172 | ||
f253f184 | 173 | return v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev); |
326c9862 MD |
174 | } |
175 | ||
176 | static int soc_camera_platform_remove(struct platform_device *pdev) | |
177 | { | |
08590b96 | 178 | struct soc_camera_platform_priv *priv = get_priv(pdev); |
fff96b66 | 179 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(&priv->subdev); |
326c9862 | 180 | |
fff96b66 | 181 | p->icd->control = NULL; |
979ea1dd | 182 | v4l2_device_unregister_subdev(&priv->subdev); |
326c9862 MD |
183 | return 0; |
184 | } | |
185 | ||
186 | static struct platform_driver soc_camera_platform_driver = { | |
992bc797 | 187 | .driver = { |
326c9862 MD |
188 | .name = "soc_camera_platform", |
189 | }, | |
190 | .probe = soc_camera_platform_probe, | |
191 | .remove = soc_camera_platform_remove, | |
192 | }; | |
193 | ||
1d6629b1 | 194 | module_platform_driver(soc_camera_platform_driver); |
326c9862 MD |
195 | |
196 | MODULE_DESCRIPTION("SoC Camera Platform driver"); | |
197 | MODULE_AUTHOR("Magnus Damm"); | |
198 | MODULE_LICENSE("GPL v2"); | |
40e2e092 | 199 | MODULE_ALIAS("platform:soc_camera_platform"); |