Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 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> |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 19 | #include <media/v4l2-subdev.h> |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 20 | #include <media/soc_camera.h> |
Guennadi Liakhovetski | 50c616f | 2008-10-16 19:49:27 -0300 | [diff] [blame] | 21 | #include <media/soc_camera_platform.h> |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 22 | |
| 23 | struct soc_camera_platform_priv { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 24 | struct v4l2_subdev subdev; |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 25 | }; |
| 26 | |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 27 | static struct soc_camera_platform_priv *get_priv(struct platform_device *pdev) |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 28 | { |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 29 | struct v4l2_subdev *subdev = platform_get_drvdata(pdev); |
| 30 | return container_of(subdev, struct soc_camera_platform_priv, subdev); |
| 31 | } |
| 32 | |
| 33 | static struct soc_camera_platform_info *get_info(struct soc_camera_device *icd) |
| 34 | { |
Guennadi Liakhovetski | 96c7539 | 2009-08-25 11:53:23 -0300 | [diff] [blame] | 35 | struct platform_device *pdev = |
| 36 | to_platform_device(to_soc_camera_control(icd)); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 37 | return pdev->dev.platform_data; |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 38 | } |
| 39 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 40 | static int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable) |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 41 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 42 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); |
| 43 | return p->set_capture(p, enable); |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static int soc_camera_platform_set_bus_param(struct soc_camera_device *icd, |
| 47 | unsigned long flags) |
| 48 | { |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static unsigned long |
| 53 | soc_camera_platform_query_bus_param(struct soc_camera_device *icd) |
| 54 | { |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 55 | struct soc_camera_platform_info *p = get_info(icd); |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 56 | return p->bus_param; |
| 57 | } |
| 58 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 59 | static int soc_camera_platform_try_fmt(struct v4l2_subdev *sd, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 60 | struct v4l2_mbus_framefmt *mf) |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 61 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 62 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 63 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 64 | mf->width = p->format.width; |
| 65 | mf->height = p->format.height; |
| 66 | mf->code = p->format.code; |
| 67 | mf->colorspace = p->format.colorspace; |
| 68 | |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 72 | static struct v4l2_subdev_core_ops platform_subdev_core_ops; |
| 73 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 74 | static int soc_camera_platform_enum_fmt(struct v4l2_subdev *sd, int index, |
| 75 | enum v4l2_mbus_pixelcode *code) |
| 76 | { |
| 77 | struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd); |
| 78 | |
| 79 | if (index) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | *code = p->format.code; |
| 83 | return 0; |
| 84 | } |
| 85 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 86 | static struct v4l2_subdev_video_ops platform_subdev_video_ops = { |
| 87 | .s_stream = soc_camera_platform_s_stream, |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 88 | .try_mbus_fmt = soc_camera_platform_try_fmt, |
| 89 | .enum_mbus_fmt = soc_camera_platform_enum_fmt, |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | static struct v4l2_subdev_ops platform_subdev_ops = { |
| 93 | .core = &platform_subdev_core_ops, |
| 94 | .video = &platform_subdev_video_ops, |
| 95 | }; |
| 96 | |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 97 | static struct soc_camera_ops soc_camera_platform_ops = { |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 98 | .set_bus_param = soc_camera_platform_set_bus_param, |
| 99 | .query_bus_param = soc_camera_platform_query_bus_param, |
| 100 | }; |
| 101 | |
| 102 | static int soc_camera_platform_probe(struct platform_device *pdev) |
| 103 | { |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 104 | struct soc_camera_host *ici; |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 105 | struct soc_camera_platform_priv *priv; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 106 | struct soc_camera_platform_info *p = pdev->dev.platform_data; |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 107 | struct soc_camera_device *icd; |
| 108 | int ret; |
| 109 | |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 110 | if (!p) |
| 111 | return -EINVAL; |
| 112 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 113 | if (!p->dev) { |
| 114 | dev_err(&pdev->dev, |
| 115 | "Platform has not set soc_camera_device pointer!\n"); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 119 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 120 | if (!priv) |
| 121 | return -ENOMEM; |
| 122 | |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 123 | icd = to_soc_camera_dev(p->dev); |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 124 | |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 125 | /* soc-camera convention: control's drvdata points to the subdev */ |
| 126 | platform_set_drvdata(pdev, &priv->subdev); |
| 127 | /* Set the control device reference */ |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 128 | dev_set_drvdata(&icd->dev, &pdev->dev); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 129 | |
Guennadi Liakhovetski | 760697b | 2009-12-11 11:46:49 -0300 | [diff] [blame] | 130 | icd->ops = &soc_camera_platform_ops; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 131 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 132 | ici = to_soc_camera_host(icd->dev.parent); |
| 133 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 134 | v4l2_subdev_init(&priv->subdev, &platform_subdev_ops); |
| 135 | v4l2_set_subdevdata(&priv->subdev, p); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 136 | strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE); |
| 137 | |
| 138 | ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev); |
| 139 | if (ret) |
| 140 | goto evdrs; |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 141 | |
| 142 | return ret; |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 143 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 144 | evdrs: |
| 145 | icd->ops = NULL; |
| 146 | platform_set_drvdata(pdev, NULL); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 147 | kfree(priv); |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 148 | return ret; |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int soc_camera_platform_remove(struct platform_device *pdev) |
| 152 | { |
Guennadi Liakhovetski | 08590b9 | 2009-08-25 11:46:54 -0300 | [diff] [blame] | 153 | struct soc_camera_platform_priv *priv = get_priv(pdev); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 154 | struct soc_camera_platform_info *p = pdev->dev.platform_data; |
| 155 | struct soc_camera_device *icd = to_soc_camera_dev(p->dev); |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 156 | |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 157 | v4l2_device_unregister_subdev(&priv->subdev); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 158 | icd->ops = NULL; |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 159 | platform_set_drvdata(pdev, NULL); |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 160 | kfree(priv); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static struct platform_driver soc_camera_platform_driver = { |
| 165 | .driver = { |
| 166 | .name = "soc_camera_platform", |
Guennadi Liakhovetski | 979ea1d | 2009-08-25 11:43:33 -0300 | [diff] [blame] | 167 | .owner = THIS_MODULE, |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 168 | }, |
| 169 | .probe = soc_camera_platform_probe, |
| 170 | .remove = soc_camera_platform_remove, |
| 171 | }; |
| 172 | |
| 173 | static int __init soc_camera_platform_module_init(void) |
| 174 | { |
| 175 | return platform_driver_register(&soc_camera_platform_driver); |
| 176 | } |
| 177 | |
| 178 | static void __exit soc_camera_platform_module_exit(void) |
| 179 | { |
Paul Mundt | 01c1e4c | 2008-08-01 19:48:51 -0300 | [diff] [blame] | 180 | platform_driver_unregister(&soc_camera_platform_driver); |
Magnus Damm | 326c986 | 2008-07-16 23:02:08 -0300 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | module_init(soc_camera_platform_module_init); |
| 184 | module_exit(soc_camera_platform_module_exit); |
| 185 | |
| 186 | MODULE_DESCRIPTION("SoC Camera Platform driver"); |
| 187 | MODULE_AUTHOR("Magnus Damm"); |
| 188 | MODULE_LICENSE("GPL v2"); |
Guennadi Liakhovetski | 40e2e09 | 2009-08-25 11:28:22 -0300 | [diff] [blame] | 189 | MODULE_ALIAS("platform:soc_camera_platform"); |