camss-format.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * camss-format.c
  4. *
  5. * Qualcomm MSM Camera Subsystem - Format helpers
  6. *
  7. * Copyright (c) 2023, The Linux Foundation. All rights reserved.
  8. * Copyright (c) 2023 Qualcomm Technologies, Inc.
  9. */
  10. #include <linux/bug.h>
  11. #include <linux/errno.h>
  12. #include "camss-format.h"
  13. /*
  14. * camss_format_get_bpp - Map media bus format to bits per pixel
  15. * @formats: supported media bus formats array
  16. * @nformats: size of @formats array
  17. * @code: media bus format code
  18. *
  19. * Return number of bits per pixel
  20. */
  21. u8 camss_format_get_bpp(const struct camss_format_info *formats, unsigned int nformats, u32 code)
  22. {
  23. unsigned int i;
  24. for (i = 0; i < nformats; i++)
  25. if (code == formats[i].code)
  26. return formats[i].mbus_bpp;
  27. WARN(1, "Unknown format\n");
  28. return formats[0].mbus_bpp;
  29. }
  30. /*
  31. * camss_format_find_code - Find a format code in an array
  32. * @code: a pointer to media bus format codes array
  33. * @n_code: size of @code array
  34. * @index: index of code in the array
  35. * @req_code: required code
  36. *
  37. * Return media bus format code
  38. */
  39. u32 camss_format_find_code(u32 *code, unsigned int n_code, unsigned int index, u32 req_code)
  40. {
  41. unsigned int i;
  42. if (!req_code && index >= n_code)
  43. return 0;
  44. for (i = 0; i < n_code; i++) {
  45. if (req_code) {
  46. if (req_code == code[i])
  47. return req_code;
  48. } else {
  49. if (i == index)
  50. return code[i];
  51. }
  52. }
  53. return code[0];
  54. }
  55. /*
  56. * camss_format_find_format - Find a format in an array
  57. * @code: media bus format code
  58. * @pixelformat: V4L2 pixel format FCC identifier
  59. * @formats: a pointer to formats array
  60. * @nformats: size of @formats array
  61. *
  62. * Return index of a format or a negative error code otherwise
  63. */
  64. int camss_format_find_format(u32 code, u32 pixelformat, const struct camss_format_info *formats,
  65. unsigned int nformats)
  66. {
  67. unsigned int i;
  68. for (i = 0; i < nformats; i++) {
  69. if (formats[i].code == code &&
  70. formats[i].pixelformat == pixelformat)
  71. return i;
  72. }
  73. for (i = 0; i < nformats; i++) {
  74. if (formats[i].code == code)
  75. return i;
  76. }
  77. return -EINVAL;
  78. }