soc_camera_platform.c 5.3 KB

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