soc_camera_platform.c 5.8 KB

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