soc_camera_platform.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_try_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. return 0;
  60. }
  61. static struct v4l2_subdev_core_ops platform_subdev_core_ops;
  62. static int soc_camera_platform_enum_fmt(struct v4l2_subdev *sd, int index,
  63. enum v4l2_mbus_pixelcode *code)
  64. {
  65. struct soc_camera_platform_info *p = v4l2_get_subdevdata(sd);
  66. if (index)
  67. return -EINVAL;
  68. *code = p->format.code;
  69. return 0;
  70. }
  71. static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
  72. .s_stream = soc_camera_platform_s_stream,
  73. .try_mbus_fmt = soc_camera_platform_try_fmt,
  74. .enum_mbus_fmt = soc_camera_platform_enum_fmt,
  75. };
  76. static struct v4l2_subdev_ops platform_subdev_ops = {
  77. .core = &platform_subdev_core_ops,
  78. .video = &platform_subdev_video_ops,
  79. };
  80. static struct soc_camera_ops soc_camera_platform_ops = {
  81. .set_bus_param = soc_camera_platform_set_bus_param,
  82. .query_bus_param = soc_camera_platform_query_bus_param,
  83. };
  84. static int soc_camera_platform_probe(struct platform_device *pdev)
  85. {
  86. struct soc_camera_host *ici;
  87. struct soc_camera_platform_priv *priv;
  88. struct soc_camera_platform_info *p = pdev->dev.platform_data;
  89. struct soc_camera_device *icd;
  90. int ret;
  91. if (!p)
  92. return -EINVAL;
  93. if (!p->dev) {
  94. dev_err(&pdev->dev,
  95. "Platform has not set soc_camera_device pointer!\n");
  96. return -EINVAL;
  97. }
  98. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  99. if (!priv)
  100. return -ENOMEM;
  101. icd = to_soc_camera_dev(p->dev);
  102. /* soc-camera convention: control's drvdata points to the subdev */
  103. platform_set_drvdata(pdev, &priv->subdev);
  104. /* Set the control device reference */
  105. dev_set_drvdata(&icd->dev, &pdev->dev);
  106. icd->ops = &soc_camera_platform_ops;
  107. ici = to_soc_camera_host(icd->dev.parent);
  108. v4l2_subdev_init(&priv->subdev, &platform_subdev_ops);
  109. v4l2_set_subdevdata(&priv->subdev, p);
  110. strncpy(priv->subdev.name, dev_name(&pdev->dev), V4L2_SUBDEV_NAME_SIZE);
  111. ret = v4l2_device_register_subdev(&ici->v4l2_dev, &priv->subdev);
  112. if (ret)
  113. goto evdrs;
  114. return ret;
  115. evdrs:
  116. icd->ops = NULL;
  117. platform_set_drvdata(pdev, NULL);
  118. kfree(priv);
  119. return ret;
  120. }
  121. static int soc_camera_platform_remove(struct platform_device *pdev)
  122. {
  123. struct soc_camera_platform_priv *priv = get_priv(pdev);
  124. struct soc_camera_platform_info *p = pdev->dev.platform_data;
  125. struct soc_camera_device *icd = to_soc_camera_dev(p->dev);
  126. v4l2_device_unregister_subdev(&priv->subdev);
  127. icd->ops = NULL;
  128. platform_set_drvdata(pdev, NULL);
  129. kfree(priv);
  130. return 0;
  131. }
  132. static struct platform_driver soc_camera_platform_driver = {
  133. .driver = {
  134. .name = "soc_camera_platform",
  135. .owner = THIS_MODULE,
  136. },
  137. .probe = soc_camera_platform_probe,
  138. .remove = soc_camera_platform_remove,
  139. };
  140. static int __init soc_camera_platform_module_init(void)
  141. {
  142. return platform_driver_register(&soc_camera_platform_driver);
  143. }
  144. static void __exit soc_camera_platform_module_exit(void)
  145. {
  146. platform_driver_unregister(&soc_camera_platform_driver);
  147. }
  148. module_init(soc_camera_platform_module_init);
  149. module_exit(soc_camera_platform_module_exit);
  150. MODULE_DESCRIPTION("SoC Camera Platform driver");
  151. MODULE_AUTHOR("Magnus Damm");
  152. MODULE_LICENSE("GPL v2");
  153. MODULE_ALIAS("platform:soc_camera_platform");