soc_camera_platform.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable)
  30. {
  31. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  32. return p->set_capture(p, enable);
  33. }
  34. static int soc_camera_platform_fill_fmt(struct v4l2_subdev *sd,
  35. struct v4l2_mbus_framefmt *mf)
  36. {
  37. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  38. mf->width = p->format.width;
  39. mf->height = p->format.height;
  40. mf->code = p->format.code;
  41. mf->colorspace = p->format.colorspace;
  42. mf->field = p->format.field;
  43. return 0;
  44. }
  45. static struct v4l2_subdev_core_ops platform_subdev_core_ops;
  46. static int soc_camera_platform_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  47. enum v4l2_mbus_pixelcode *code)
  48. {
  49. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  50. if (index)
  51. return -EINVAL;
  52. *code = p->format.code;
  53. return 0;
  54. }
  55. static int soc_camera_platform_g_crop(struct v4l2_subdev *sd,
  56. struct v4l2_crop *a)
  57. {
  58. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  59. a->c.left = 0;
  60. a->c.top = 0;
  61. a->c.width = p->format.width;
  62. a->c.height = p->format.height;
  63. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  64. return 0;
  65. }
  66. static int soc_camera_platform_cropcap(struct v4l2_subdev *sd,
  67. struct v4l2_cropcap *a)
  68. {
  69. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  70. a->bounds.left = 0;
  71. a->bounds.top = 0;
  72. a->bounds.width = p->format.width;
  73. a->bounds.height = p->format.height;
  74. a->defrect = a->bounds;
  75. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  76. a->pixelaspect.numerator = 1;
  77. a->pixelaspect.denominator = 1;
  78. return 0;
  79. }
  80. static int soc_camera_platform_g_mbus_config(struct v4l2_subdev *sd,
  81. struct v4l2_mbus_config *cfg)
  82. {
  83. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  84. cfg->flags = p->mbus_param;
  85. cfg->type = p->mbus_type;
  86. return 0;
  87. }
  88. static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
  89. .s_stream = soc_camera_platform_s_stream,
  90. .enum_mbus_fmt = soc_camera_platform_enum_fmt,
  91. .cropcap = soc_camera_platform_cropcap,
  92. .g_crop = soc_camera_platform_g_crop,
  93. .try_mbus_fmt = soc_camera_platform_fill_fmt,
  94. .g_mbus_fmt = soc_camera_platform_fill_fmt,
  95. .s_mbus_fmt = soc_camera_platform_fill_fmt,
  96. .g_mbus_config = soc_camera_platform_g_mbus_config,
  97. };
  98. static struct v4l2_subdev_ops platform_subdev_ops = {
  99. .core = &platform_subdev_core_ops,
  100. .video = &platform_subdev_video_ops,
  101. };
  102. static int soc_camera_platform_probe(struct platform_device *pdev)
  103. {
  104. struct soc_camera_host *ici;
  105. struct soc_camera_platform_priv *priv;
  106. struct soc_camera_platform_info *p = pdev->dev.platform_data;
  107. struct soc_camera_device *icd;
  108. int ret;
  109. if (!p)
  110. return -EINVAL;
  111. if (!p->icd) {
  112. dev_err(&pdev->dev,
  113. "Platform has not set soc_camera_device pointer!\n");
  114. return -EINVAL;
  115. }
  116. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  117. if (!priv)
  118. return -ENOMEM;
  119. icd = p->icd;
  120. /* soc-camera convention: control's drvdata points to the subdev */
  121. platform_set_drvdata(pdev, &priv->subdev);
  122. /* Set the control device reference */
  123. icd->control = &pdev->dev;
  124. ici = to_soc_camera_host(icd->parent);
  125. v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
  126. v4l2_set_subdevdata(&priv->subdev, p);
  127. strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE);
  128. ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
  129. if (ret)
  130. goto evdrs;
  131. return ret;
  132. evdrs:
  133. platform_set_drvdata(pdev, NULL);
  134. kfree(priv);
  135. return ret;
  136. }
  137. static int soc_camera_platform_remove(struct platform_device *pdev)
  138. {
  139. struct soc_camera_platform_priv *priv = get_priv(pdev);
  140. struct soc_camera_platform_info *p = v4l2_get_subdevdata(&priv->subdev);
  141. p->icd->control = NULL;
  142. v4l2_device_unregister_subdev(&priv->subdev);
  143. platform_set_drvdata(pdev, NULL);
  144. kfree(priv);
  145. return 0;
  146. }
  147. static struct platform_driver soc_camera_platform_driver = {
  148. .driver = {
  149. .name = "soc_camera_platform",
  150. .owner = THIS_MODULE,
  151. },
  152. .probe = soc_camera_platform_probe,
  153. .remove = soc_camera_platform_remove,
  154. };
  155. module_platform_driver(soc_camera_platform_driver);
  156. MODULE_DESCRIPTION("SoC Camera Platform driver");
  157. MODULE_AUTHOR("Magnus Damm");
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_ALIAS("platform:soc_camera_platform");