[media] soc-camera: use devm_kzalloc in subdevice drivers
[deliverable/linux.git] / drivers / media / platform / soc_camera / soc_camera_platform.c
CommitLineData
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
23struct soc_camera_platform_priv {
979ea1dd 24 struct v4l2_subdev subdev;
326c9862
MD
25};
26
08590b96 27static 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 33static 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
39static 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
4ec10bac
LP
53static int soc_camera_platform_s_power(struct v4l2_subdev *sd, int on)
54{
55 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
56
25a34811 57 return soc_camera_set_power(p->icd->control, &p->icd->sdesc->subdev_desc, on);
4ec10bac
LP
58}
59
60static struct v4l2_subdev_core_ops platform_subdev_core_ops = {
61 .s_power = soc_camera_platform_s_power,
62};
760697be 63
3805f201 64static int soc_camera_platform_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
760697be 65 enum v4l2_mbus_pixelcode *code)
326c9862 66{
760697be 67 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
326c9862 68
760697be
GL
69 if (index)
70 return -EINVAL;
326c9862 71
760697be
GL
72 *code = p->format.code;
73 return 0;
326c9862
MD
74}
75
e7d403f5
KM
76static int soc_camera_platform_g_crop(struct v4l2_subdev *sd,
77 struct v4l2_crop *a)
78{
79 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
80
81 a->c.left = 0;
82 a->c.top = 0;
83 a->c.width = p->format.width;
84 a->c.height = p->format.height;
85 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
86
87 return 0;
88}
89
90static int soc_camera_platform_cropcap(struct v4l2_subdev *sd,
91 struct v4l2_cropcap *a)
92{
93 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
94
95 a->bounds.left = 0;
96 a->bounds.top = 0;
97 a->bounds.width = p->format.width;
98 a->bounds.height = p->format.height;
99 a->defrect = a->bounds;
100 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
101 a->pixelaspect.numerator = 1;
102 a->pixelaspect.denominator = 1;
103
104 return 0;
105}
106
84c760a5
GL
107static int soc_camera_platform_g_mbus_config(struct v4l2_subdev *sd,
108 struct v4l2_mbus_config *cfg)
109{
110 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
111
112 cfg->flags = p->mbus_param;
113 cfg->type = p->mbus_type;
114
115 return 0;
116}
117
979ea1dd
GL
118static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
119 .s_stream = soc_camera_platform_s_stream,
760697be 120 .enum_mbus_fmt = soc_camera_platform_enum_fmt,
e7d403f5
KM
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,
84c760a5 126 .g_mbus_config = soc_camera_platform_g_mbus_config,
979ea1dd
GL
127};
128
129static struct v4l2_subdev_ops platform_subdev_ops = {
130 .core = &platform_subdev_core_ops,
131 .video = &platform_subdev_video_ops,
132};
133
326c9862
MD
134static int soc_camera_platform_probe(struct platform_device *pdev)
135{
979ea1dd 136 struct soc_camera_host *ici;
326c9862 137 struct soc_camera_platform_priv *priv;
40e2e092 138 struct soc_camera_platform_info *p = pdev->dev.platform_data;
326c9862
MD
139 struct soc_camera_device *icd;
140 int ret;
141
326c9862
MD
142 if (!p)
143 return -EINVAL;
144
7dfff953 145 if (!p->icd) {
979ea1dd
GL
146 dev_err(&pdev->dev,
147 "Platform has not set soc_camera_device pointer!\n");
148 return -EINVAL;
149 }
150
70e176a5 151 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
326c9862
MD
152 if (!priv)
153 return -ENOMEM;
154
7dfff953 155 icd = p->icd;
40e2e092 156
08590b96
GL
157 /* soc-camera convention: control's drvdata points to the subdev */
158 platform_set_drvdata(pdev, &priv->subdev);
159 /* Set the control device reference */
7dfff953 160 icd->control = &pdev->dev;
979ea1dd 161
7dfff953 162 ici = to_soc_camera_host(icd->parent);
979ea1dd 163
979ea1dd
GL
164 v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
165 v4l2_set_subdevdata(&priv->subdev, p);
979ea1dd
GL
166 strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE);
167
168 ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
169 if (ret)
170 goto evdrs;
326c9862
MD
171
172 return ret;
40e2e092 173
979ea1dd 174evdrs:
979ea1dd 175 platform_set_drvdata(pdev, NULL);
979ea1dd 176 return ret;
326c9862
MD
177}
178
179static int soc_camera_platform_remove(struct platform_device *pdev)
180{
08590b96 181 struct soc_camera_platform_priv *priv = get_priv(pdev);
fff96b66 182 struct soc_camera_platform_info *p = v4l2_get_subdevdata(&priv->subdev);
326c9862 183
fff96b66 184 p->icd->control = NULL;
979ea1dd 185 v4l2_device_unregister_subdev(&priv->subdev);
979ea1dd 186 platform_set_drvdata(pdev, NULL);
326c9862
MD
187 return 0;
188}
189
190static struct platform_driver soc_camera_platform_driver = {
191 .driver = {
192 .name = "soc_camera_platform",
979ea1dd 193 .owner = THIS_MODULE,
326c9862
MD
194 },
195 .probe = soc_camera_platform_probe,
196 .remove = soc_camera_platform_remove,
197};
198
1d6629b1 199module_platform_driver(soc_camera_platform_driver);
326c9862
MD
200
201MODULE_DESCRIPTION("SoC Camera Platform driver");
202MODULE_AUTHOR("Magnus Damm");
203MODULE_LICENSE("GPL v2");
40e2e092 204MODULE_ALIAS("platform:soc_camera_platform");
This page took 0.401788 seconds and 5 git commands to generate.