GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / media / platform / soc_camera / soc_camera_platform.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic Platform Camera Driver
4  *
5  * Copyright (C) 2008 Magnus Damm
6  * Based on mt9m001 driver,
7  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
8  */
9
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/platform_device.h>
15 #include <linux/videodev2.h>
16 #include <media/v4l2-subdev.h>
17 #include <media/soc_camera.h>
18 #include <linux/platform_data/media/soc_camera_platform.h>
19
20 struct soc_camera_platform_priv {
21         struct v4l2_subdev subdev;
22 };
23
24 static struct soc_camera_platform_priv *get_priv(struct platform_device *pdev)
25 {
26         struct v4l2_subdev *subdev = platform_get_drvdata(pdev);
27         return container_of(subdev, struct soc_camera_platform_priv, subdev);
28 }
29
30 static int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable)
31 {
32         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
33         return p->set_capture(p, enable);
34 }
35
36 static int soc_camera_platform_fill_fmt(struct v4l2_subdev *sd,
37                 struct v4l2_subdev_pad_config *cfg,
38                 struct v4l2_subdev_format *format)
39 {
40         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
41         struct v4l2_mbus_framefmt *mf = &format->format;
42
43         mf->width       = p->format.width;
44         mf->height      = p->format.height;
45         mf->code        = p->format.code;
46         mf->colorspace  = p->format.colorspace;
47         mf->field       = p->format.field;
48
49         return 0;
50 }
51
52 static int soc_camera_platform_s_power(struct v4l2_subdev *sd, int on)
53 {
54         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
55
56         return soc_camera_set_power(p->icd->control, &p->icd->sdesc->subdev_desc, NULL, on);
57 }
58
59 static const struct v4l2_subdev_core_ops platform_subdev_core_ops = {
60         .s_power = soc_camera_platform_s_power,
61 };
62
63 static int soc_camera_platform_enum_mbus_code(struct v4l2_subdev *sd,
64                 struct v4l2_subdev_pad_config *cfg,
65                 struct v4l2_subdev_mbus_code_enum *code)
66 {
67         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
68
69         if (code->pad || code->index)
70                 return -EINVAL;
71
72         code->code = p->format.code;
73         return 0;
74 }
75
76 static int soc_camera_platform_get_selection(struct v4l2_subdev *sd,
77                 struct v4l2_subdev_pad_config *cfg,
78                 struct v4l2_subdev_selection *sel)
79 {
80         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
81
82         if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
83                 return -EINVAL;
84
85         switch (sel->target) {
86         case V4L2_SEL_TGT_CROP_BOUNDS:
87         case V4L2_SEL_TGT_CROP_DEFAULT:
88         case V4L2_SEL_TGT_CROP:
89                 sel->r.left = 0;
90                 sel->r.top = 0;
91                 sel->r.width = p->format.width;
92                 sel->r.height = p->format.height;
93                 return 0;
94         default:
95                 return -EINVAL;
96         }
97 }
98
99 static int soc_camera_platform_g_mbus_config(struct v4l2_subdev *sd,
100                                              struct v4l2_mbus_config *cfg)
101 {
102         struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
103
104         cfg->flags = p->mbus_param;
105         cfg->type = p->mbus_type;
106
107         return 0;
108 }
109
110 static const struct v4l2_subdev_video_ops platform_subdev_video_ops = {
111         .s_stream       = soc_camera_platform_s_stream,
112         .g_mbus_config  = soc_camera_platform_g_mbus_config,
113 };
114
115 static const struct v4l2_subdev_pad_ops platform_subdev_pad_ops = {
116         .enum_mbus_code = soc_camera_platform_enum_mbus_code,
117         .get_selection  = soc_camera_platform_get_selection,
118         .get_fmt        = soc_camera_platform_fill_fmt,
119         .set_fmt        = soc_camera_platform_fill_fmt,
120 };
121
122 static const struct v4l2_subdev_ops platform_subdev_ops = {
123         .core   = &platform_subdev_core_ops,
124         .video  = &platform_subdev_video_ops,
125         .pad    = &platform_subdev_pad_ops,
126 };
127
128 static int soc_camera_platform_probe(struct platform_device *pdev)
129 {
130         struct soc_camera_host *ici;
131         struct soc_camera_platform_priv *priv;
132         struct soc_camera_platform_info *p = pdev->dev.platform_data;
133         struct soc_camera_device *icd;
134
135         if (!p)
136                 return -EINVAL;
137
138         if (!p->icd) {
139                 dev_err(&pdev->dev,
140                         "Platform has not set soc_camera_device pointer!\n");
141                 return -EINVAL;
142         }
143
144         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
145         if (!priv)
146                 return -ENOMEM;
147
148         icd = p->icd;
149
150         /* soc-camera convention: control's drvdata points to the subdev */
151         platform_set_drvdata(pdev, &priv->subdev);
152         /* Set the control device reference */
153         icd->control = &pdev->dev;
154
155         ici = to_soc_camera_host(icd->parent);
156
157         v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
158         v4l2_set_subdevdata(&priv->subdev, p);
159         strlcpy(priv->subdev.name, dev_name(&pdev->dev),
160                 sizeof(priv->subdev.name));
161
162         return v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
163 }
164
165 static int soc_camera_platform_remove(struct platform_device *pdev)
166 {
167         struct soc_camera_platform_priv *priv = get_priv(pdev);
168         struct soc_camera_platform_info *p = v4l2_get_subdevdata(&priv->subdev);
169
170         p->icd->control = NULL;
171         v4l2_device_unregister_subdev(&priv->subdev);
172         return 0;
173 }
174
175 static struct platform_driver soc_camera_platform_driver = {
176         .driver         = {
177                 .name   = "soc_camera_platform",
178         },
179         .probe          = soc_camera_platform_probe,
180         .remove         = soc_camera_platform_remove,
181 };
182
183 module_platform_driver(soc_camera_platform_driver);
184
185 MODULE_DESCRIPTION("SoC Camera Platform driver");
186 MODULE_AUTHOR("Magnus Damm");
187 MODULE_LICENSE("GPL v2");
188 MODULE_ALIAS("platform:soc_camera_platform");