soc_camera_platform.c 5.2 KB

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