vimc-lens.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * vimc-lens.c Virtual Media Controller Driver
  4. * Copyright (C) 2022 Google, Inc
  5. * Author: [email protected] (Yunke Cao)
  6. */
  7. #include <media/v4l2-ctrls.h>
  8. #include <media/v4l2-event.h>
  9. #include <media/v4l2-subdev.h>
  10. #include "vimc-common.h"
  11. #define VIMC_LENS_MAX_FOCUS_POS 1023
  12. #define VIMC_LENS_MAX_FOCUS_STEP 1
  13. struct vimc_lens_device {
  14. struct vimc_ent_device ved;
  15. struct v4l2_subdev sd;
  16. struct v4l2_ctrl_handler hdl;
  17. u32 focus_absolute;
  18. };
  19. static const struct v4l2_subdev_core_ops vimc_lens_core_ops = {
  20. .log_status = v4l2_ctrl_subdev_log_status,
  21. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  22. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  23. };
  24. static const struct v4l2_subdev_ops vimc_lens_ops = {
  25. .core = &vimc_lens_core_ops
  26. };
  27. static int vimc_lens_s_ctrl(struct v4l2_ctrl *ctrl)
  28. {
  29. struct vimc_lens_device *vlens =
  30. container_of(ctrl->handler, struct vimc_lens_device, hdl);
  31. if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
  32. vlens->focus_absolute = ctrl->val;
  33. return 0;
  34. }
  35. return -EINVAL;
  36. }
  37. static const struct v4l2_ctrl_ops vimc_lens_ctrl_ops = {
  38. .s_ctrl = vimc_lens_s_ctrl,
  39. };
  40. static struct vimc_ent_device *vimc_lens_add(struct vimc_device *vimc,
  41. const char *vcfg_name)
  42. {
  43. struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
  44. struct vimc_lens_device *vlens;
  45. int ret;
  46. /* Allocate the vlens struct */
  47. vlens = kzalloc(sizeof(*vlens), GFP_KERNEL);
  48. if (!vlens)
  49. return ERR_PTR(-ENOMEM);
  50. v4l2_ctrl_handler_init(&vlens->hdl, 1);
  51. v4l2_ctrl_new_std(&vlens->hdl, &vimc_lens_ctrl_ops,
  52. V4L2_CID_FOCUS_ABSOLUTE, 0,
  53. VIMC_LENS_MAX_FOCUS_POS, VIMC_LENS_MAX_FOCUS_STEP, 0);
  54. vlens->sd.ctrl_handler = &vlens->hdl;
  55. if (vlens->hdl.error) {
  56. ret = vlens->hdl.error;
  57. goto err_free_vlens;
  58. }
  59. vlens->ved.dev = vimc->mdev.dev;
  60. ret = vimc_ent_sd_register(&vlens->ved, &vlens->sd, v4l2_dev,
  61. vcfg_name, MEDIA_ENT_F_LENS, 0,
  62. NULL, &vimc_lens_ops);
  63. if (ret)
  64. goto err_free_hdl;
  65. return &vlens->ved;
  66. err_free_hdl:
  67. v4l2_ctrl_handler_free(&vlens->hdl);
  68. err_free_vlens:
  69. kfree(vlens);
  70. return ERR_PTR(ret);
  71. }
  72. static void vimc_lens_release(struct vimc_ent_device *ved)
  73. {
  74. struct vimc_lens_device *vlens =
  75. container_of(ved, struct vimc_lens_device, ved);
  76. v4l2_ctrl_handler_free(&vlens->hdl);
  77. media_entity_cleanup(vlens->ved.ent);
  78. kfree(vlens);
  79. }
  80. struct vimc_ent_type vimc_lens_type = {
  81. .add = vimc_lens_add,
  82. .release = vimc_lens_release
  83. };