blob: bf406e89c992b6515ed7c27f8f87ef6577202d6e [file] [log] [blame]
Magnus Damm326c9862008-07-16 23:02:08 -03001/*
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>
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030019#include <media/v4l2-subdev.h>
Magnus Damm326c9862008-07-16 23:02:08 -030020#include <media/soc_camera.h>
Guennadi Liakhovetski50c616f2008-10-16 19:49:27 -030021#include <media/soc_camera_platform.h>
Magnus Damm326c9862008-07-16 23:02:08 -030022
23struct soc_camera_platform_priv {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030024 struct v4l2_subdev subdev;
Magnus Damm326c9862008-07-16 23:02:08 -030025};
26
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -030027static struct soc_camera_platform_priv *get_priv(struct platform_device *pdev)
Magnus Damm326c9862008-07-16 23:02:08 -030028{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -030029 struct v4l2_subdev *subdev = platform_get_drvdata(pdev);
30 return container_of(subdev, struct soc_camera_platform_priv, subdev);
31}
32
33static struct soc_camera_platform_info *get_info(struct soc_camera_device *icd)
34{
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030035 struct platform_device *pdev =
36 to_platform_device(to_soc_camera_control(icd));
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -030037 return pdev->dev.platform_data;
Magnus Damm326c9862008-07-16 23:02:08 -030038}
39
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030040static int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable)
Magnus Damm326c9862008-07-16 23:02:08 -030041{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030042 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
43 return p->set_capture(p, enable);
Magnus Damm326c9862008-07-16 23:02:08 -030044}
45
46static int soc_camera_platform_set_bus_param(struct soc_camera_device *icd,
47 unsigned long flags)
48{
49 return 0;
50}
51
52static unsigned long
53soc_camera_platform_query_bus_param(struct soc_camera_device *icd)
54{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -030055 struct soc_camera_platform_info *p = get_info(icd);
Magnus Damm326c9862008-07-16 23:02:08 -030056 return p->bus_param;
57}
58
Kuninori Morimotoe7d403f2010-05-11 06:14:18 -030059static int soc_camera_platform_fill_fmt(struct v4l2_subdev *sd,
60 struct v4l2_mbus_framefmt *mf)
Magnus Damm326c9862008-07-16 23:02:08 -030061{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030062 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
Magnus Damm326c9862008-07-16 23:02:08 -030063
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030064 mf->width = p->format.width;
65 mf->height = p->format.height;
66 mf->code = p->format.code;
67 mf->colorspace = p->format.colorspace;
Kuninori Morimotoe7d403f2010-05-11 06:14:18 -030068 mf->field = p->format.field;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030069
Magnus Damm326c9862008-07-16 23:02:08 -030070 return 0;
71}
72
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030073static struct v4l2_subdev_core_ops platform_subdev_core_ops;
74
Hans Verkuil3805f202010-05-08 17:55:00 -030075static int soc_camera_platform_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030076 enum v4l2_mbus_pixelcode *code)
77{
78 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
79
80 if (index)
81 return -EINVAL;
82
83 *code = p->format.code;
84 return 0;
85}
86
Kuninori Morimotoe7d403f2010-05-11 06:14:18 -030087static int soc_camera_platform_g_crop(struct v4l2_subdev *sd,
88 struct v4l2_crop *a)
89{
90 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
91
92 a->c.left = 0;
93 a->c.top = 0;
94 a->c.width = p->format.width;
95 a->c.height = p->format.height;
96 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
97
98 return 0;
99}
100
101static int soc_camera_platform_cropcap(struct v4l2_subdev *sd,
102 struct v4l2_cropcap *a)
103{
104 struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
105
106 a->bounds.left = 0;
107 a->bounds.top = 0;
108 a->bounds.width = p->format.width;
109 a->bounds.height = p->format.height;
110 a->defrect = a->bounds;
111 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
112 a->pixelaspect.numerator = 1;
113 a->pixelaspect.denominator = 1;
114
115 return 0;
116}
117
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300118static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
119 .s_stream = soc_camera_platform_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300120 .enum_mbus_fmt = soc_camera_platform_enum_fmt,
Kuninori Morimotoe7d403f2010-05-11 06:14:18 -0300121 .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,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300126};
127
128static struct v4l2_subdev_ops platform_subdev_ops = {
129 .core = &platform_subdev_core_ops,
130 .video = &platform_subdev_video_ops,
131};
132
Magnus Damm326c9862008-07-16 23:02:08 -0300133static struct soc_camera_ops soc_camera_platform_ops = {
Magnus Damm326c9862008-07-16 23:02:08 -0300134 .set_bus_param = soc_camera_platform_set_bus_param,
135 .query_bus_param = soc_camera_platform_query_bus_param,
136};
137
138static int soc_camera_platform_probe(struct platform_device *pdev)
139{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300140 struct soc_camera_host *ici;
Magnus Damm326c9862008-07-16 23:02:08 -0300141 struct soc_camera_platform_priv *priv;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300142 struct soc_camera_platform_info *p = pdev->dev.platform_data;
Magnus Damm326c9862008-07-16 23:02:08 -0300143 struct soc_camera_device *icd;
144 int ret;
145
Magnus Damm326c9862008-07-16 23:02:08 -0300146 if (!p)
147 return -EINVAL;
148
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300149 if (!p->dev) {
150 dev_err(&pdev->dev,
151 "Platform has not set soc_camera_device pointer!\n");
152 return -EINVAL;
153 }
154
Magnus Damm326c9862008-07-16 23:02:08 -0300155 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
156 if (!priv)
157 return -ENOMEM;
158
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300159 icd = to_soc_camera_dev(p->dev);
Magnus Damm326c9862008-07-16 23:02:08 -0300160
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300161 /* soc-camera convention: control's drvdata points to the subdev */
162 platform_set_drvdata(pdev, &priv->subdev);
163 /* Set the control device reference */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300164 dev_set_drvdata(&icd->dev, &pdev->dev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300165
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300166 icd->ops = &soc_camera_platform_ops;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300167
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300168 ici = to_soc_camera_host(icd->dev.parent);
169
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300170 v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
171 v4l2_set_subdevdata(&priv->subdev, p);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300172 strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE);
173
174 ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
175 if (ret)
176 goto evdrs;
Magnus Damm326c9862008-07-16 23:02:08 -0300177
178 return ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300179
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300180evdrs:
181 icd->ops = NULL;
182 platform_set_drvdata(pdev, NULL);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300183 kfree(priv);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300184 return ret;
Magnus Damm326c9862008-07-16 23:02:08 -0300185}
186
187static int soc_camera_platform_remove(struct platform_device *pdev)
188{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300189 struct soc_camera_platform_priv *priv = get_priv(pdev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300190 struct soc_camera_platform_info *p = pdev->dev.platform_data;
191 struct soc_camera_device *icd = to_soc_camera_dev(p->dev);
Magnus Damm326c9862008-07-16 23:02:08 -0300192
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300193 v4l2_device_unregister_subdev(&priv->subdev);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300194 icd->ops = NULL;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300195 platform_set_drvdata(pdev, NULL);
Magnus Damm326c9862008-07-16 23:02:08 -0300196 kfree(priv);
197 return 0;
198}
199
200static struct platform_driver soc_camera_platform_driver = {
201 .driver = {
202 .name = "soc_camera_platform",
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300203 .owner = THIS_MODULE,
Magnus Damm326c9862008-07-16 23:02:08 -0300204 },
205 .probe = soc_camera_platform_probe,
206 .remove = soc_camera_platform_remove,
207};
208
209static int __init soc_camera_platform_module_init(void)
210{
211 return platform_driver_register(&soc_camera_platform_driver);
212}
213
214static void __exit soc_camera_platform_module_exit(void)
215{
Paul Mundt01c1e4c2008-08-01 19:48:51 -0300216 platform_driver_unregister(&soc_camera_platform_driver);
Magnus Damm326c9862008-07-16 23:02:08 -0300217}
218
219module_init(soc_camera_platform_module_init);
220module_exit(soc_camera_platform_module_exit);
221
222MODULE_DESCRIPTION("SoC Camera Platform driver");
223MODULE_AUTHOR("Magnus Damm");
224MODULE_LICENSE("GPL v2");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300225MODULE_ALIAS("platform:soc_camera_platform");