soc_camera_platform.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. struct soc_camera_data_format format;
  24. };
  25. static struct soc_camera_platform_priv *get_priv(struct platform_device *pdev)
  26. {
  27. struct v4l2_subdev *subdev = platform_get_drvdata(pdev);
  28. return container_of(subdev, struct soc_camera_platform_priv, subdev);
  29. }
  30. static struct soc_camera_platform_info *get_info(struct soc_camera_device *icd)
  31. {
  32. struct platform_device *pdev =
  33. to_platform_device(to_soc_camera_control(icd));
  34. return pdev->dev.platform_data;
  35. }
  36. static int soc_camera_platform_s_stream(struct v4l2_subdev *sd, int enable)
  37. {
  38. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  39. return p->set_capture(p, enable);
  40. }
  41. static int soc_camera_platform_set_bus_param(struct soc_camera_device *icd,
  42. unsigned long flags)
  43. {
  44. return 0;
  45. }
  46. static unsigned long
  47. soc_camera_platform_query_bus_param(struct soc_camera_device *icd)
  48. {
  49. struct soc_camera_platform_info *p = get_info(icd);
  50. return p->bus_param;
  51. }
  52. static int soc_camera_platform_try_fmt(struct v4l2_subdev *sd,
  53. struct v4l2_format *f)
  54. {
  55. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  56. struct v4l2_pix_format *pix = &f->fmt.pix;
  57. pix->width = p->format.width;
  58. pix->height = p->format.height;
  59. return 0;
  60. }
  61. static void soc_camera_platform_video_probe(struct soc_camera_device *icd,
  62. struct platform_device *pdev)
  63. {
  64. struct soc_camera_platform_priv *priv = get_priv(pdev);
  65. struct soc_camera_platform_info *p = pdev->dev.platform_data;
  66. priv->format.name = p->format_name;
  67. priv->format.depth = p->format_depth;
  68. priv->format.fourcc = p->format.pixelformat;
  69. priv->format.colorspace = p->format.colorspace;
  70. icd->formats = &priv->format;
  71. icd->num_formats = 1;
  72. }
  73. static struct v4l2_subdev_core_ops platform_subdev_core_ops;
  74. static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
  75. .s_stream = soc_camera_platform_s_stream,
  76. .try_fmt = soc_camera_platform_try_fmt,
  77. };
  78. static struct v4l2_subdev_ops platform_subdev_ops = {
  79. .core = &platform_subdev_core_ops,
  80. .video = &platform_subdev_video_ops,
  81. };
  82. static struct soc_camera_ops soc_camera_platform_ops = {
  83. .set_bus_param = soc_camera_platform_set_bus_param,
  84. .query_bus_param = soc_camera_platform_query_bus_param,
  85. };
  86. static int soc_camera_platform_probe(struct platform_device *pdev)
  87. {
  88. struct soc_camera_host *ici;
  89. struct soc_camera_platform_priv *priv;
  90. struct soc_camera_platform_info *p = pdev->dev.platform_data;
  91. struct soc_camera_device *icd;
  92. int ret;
  93. if (!p)
  94. return -EINVAL;
  95. if (!p->dev) {
  96. dev_err(&pdev->dev,
  97. "Platform has not set soc_camera_device pointer!\n");
  98. return -EINVAL;
  99. }
  100. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  101. if (!priv)
  102. return -ENOMEM;
  103. icd = to_soc_camera_dev(p->dev);
  104. /* soc-camera convention: control's drvdata points to the subdev */
  105. platform_set_drvdata(pdev, &priv->subdev);
  106. /* Set the control device reference */
  107. dev_set_drvdata(&icd->dev, &pdev->dev);
  108. icd->y_skip_top = 0;
  109. icd->ops = &soc_camera_platform_ops;
  110. ici = to_soc_camera_host(icd->dev.parent);
  111. soc_camera_platform_video_probe(icd, pdev);
  112. v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
  113. v4l2_set_subdevdata(&priv->subdev, p);
  114. strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE);
  115. ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
  116. if (ret)
  117. goto evdrs;
  118. return ret;
  119. evdrs:
  120. icd->ops = NULL;
  121. platform_set_drvdata(pdev, NULL);
  122. kfree(priv);
  123. return ret;
  124. }
  125. static int soc_camera_platform_remove(struct platform_device *pdev)
  126. {
  127. struct soc_camera_platform_priv *priv = get_priv(pdev);
  128. struct soc_camera_platform_info *p = pdev->dev.platform_data;
  129. struct soc_camera_device *icd = to_soc_camera_dev(p->dev);
  130. v4l2_device_unregister_subdev(&priv->subdev);
  131. icd->ops = NULL;
  132. platform_set_drvdata(pdev, NULL);
  133. kfree(priv);
  134. return 0;
  135. }
  136. static struct platform_driver soc_camera_platform_driver = {
  137. .driver = {
  138. .name = "soc_camera_platform",
  139. .owner = THIS_MODULE,
  140. },
  141. .probe = soc_camera_platform_probe,
  142. .remove = soc_camera_platform_remove,
  143. };
  144. static int __init soc_camera_platform_module_init(void)
  145. {
  146. return platform_driver_register(&soc_camera_platform_driver);
  147. }
  148. static void __exit soc_camera_platform_module_exit(void)
  149. {
  150. platform_driver_unregister(&soc_camera_platform_driver);
  151. }
  152. module_init(soc_camera_platform_module_init);
  153. module_exit(soc_camera_platform_module_exit);
  154. MODULE_DESCRIPTION("SoC Camera Platform driver");
  155. MODULE_AUTHOR("Magnus Damm");
  156. MODULE_LICENSE("GPL v2");
  157. MODULE_ALIAS("platform:soc_camera_platform");