csc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Color space converter library
  3. *
  4. * Copyright (c) 2013 Texas Instruments Inc.
  5. *
  6. * David Griego, <dagriego@biglakesoftware.com>
  7. * Dale Farnsworth, <dale@farnsworth.org>
  8. * Archit Taneja, <archit@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/videodev2.h>
  20. #include "csc.h"
  21. /*
  22. * 16 coefficients in the order:
  23. * a0, b0, c0, a1, b1, c1, a2, b2, c2, d0, d1, d2
  24. * (we may need to pass non-default values from user space later on, we might
  25. * need to make the coefficient struct more easy to populate)
  26. */
  27. struct colorspace_coeffs {
  28. u16 sd[12];
  29. u16 hd[12];
  30. };
  31. /* VIDEO_RANGE: limited range, GRAPHICS_RANGE: full range */
  32. #define CSC_COEFFS_VIDEO_RANGE_Y2R 0
  33. #define CSC_COEFFS_GRAPHICS_RANGE_Y2R 1
  34. #define CSC_COEFFS_VIDEO_RANGE_R2Y 2
  35. #define CSC_COEFFS_GRAPHICS_RANGE_R2Y 3
  36. /* default colorspace coefficients */
  37. static struct colorspace_coeffs colorspace_coeffs[4] = {
  38. [CSC_COEFFS_VIDEO_RANGE_Y2R] = {
  39. {
  40. /* SDTV */
  41. 0x0400, 0x0000, 0x057D, 0x0400, 0x1EA7, 0x1D35,
  42. 0x0400, 0x06EF, 0x1FFE, 0x0D40, 0x0210, 0x0C88,
  43. },
  44. {
  45. /* HDTV */
  46. 0x0400, 0x0000, 0x0629, 0x0400, 0x1F45, 0x1E2B,
  47. 0x0400, 0x0742, 0x0000, 0x0CEC, 0x0148, 0x0C60,
  48. },
  49. },
  50. [CSC_COEFFS_GRAPHICS_RANGE_Y2R] = {
  51. {
  52. /* SDTV */
  53. 0x04A8, 0x1FFE, 0x0662, 0x04A8, 0x1E6F, 0x1CBF,
  54. 0x04A8, 0x0812, 0x1FFF, 0x0C84, 0x0220, 0x0BAC,
  55. },
  56. {
  57. /* HDTV */
  58. 0x04A8, 0x0000, 0x072C, 0x04A8, 0x1F26, 0x1DDE,
  59. 0x04A8, 0x0873, 0x0000, 0x0C20, 0x0134, 0x0B7C,
  60. },
  61. },
  62. [CSC_COEFFS_VIDEO_RANGE_R2Y] = {
  63. {
  64. /* SDTV */
  65. 0x0132, 0x0259, 0x0075, 0x1F50, 0x1EA5, 0x020B,
  66. 0x020B, 0x1E4A, 0x1FAB, 0x0000, 0x0200, 0x0200,
  67. },
  68. {
  69. /* HDTV */
  70. 0x00DA, 0x02DC, 0x004A, 0x1F88, 0x1E6C, 0x020C,
  71. 0x020C, 0x1E24, 0x1FD0, 0x0000, 0x0200, 0x0200,
  72. },
  73. },
  74. [CSC_COEFFS_GRAPHICS_RANGE_R2Y] = {
  75. {
  76. /* SDTV */
  77. 0x0107, 0x0204, 0x0064, 0x1F68, 0x1ED6, 0x01C2,
  78. 0x01C2, 0x1E87, 0x1FB7, 0x0040, 0x0200, 0x0200,
  79. },
  80. {
  81. /* HDTV */
  82. 0x04A8, 0x0000, 0x072C, 0x04A8, 0x1F26, 0x1DDE,
  83. 0x04A8, 0x0873, 0x0000, 0x0C20, 0x0134, 0x0B7C,
  84. },
  85. },
  86. };
  87. void csc_dump_regs(struct csc_data *csc)
  88. {
  89. struct device *dev = &csc->pdev->dev;
  90. #define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, \
  91. ioread32(csc->base + CSC_##r))
  92. dev_dbg(dev, "CSC Registers @ %pa:\n", &csc->res->start);
  93. DUMPREG(CSC00);
  94. DUMPREG(CSC01);
  95. DUMPREG(CSC02);
  96. DUMPREG(CSC03);
  97. DUMPREG(CSC04);
  98. DUMPREG(CSC05);
  99. #undef DUMPREG
  100. }
  101. EXPORT_SYMBOL(csc_dump_regs);
  102. void csc_set_coeff_bypass(struct csc_data *csc, u32 *csc_reg5)
  103. {
  104. *csc_reg5 |= CSC_BYPASS;
  105. }
  106. EXPORT_SYMBOL(csc_set_coeff_bypass);
  107. /*
  108. * set the color space converter coefficient shadow register values
  109. */
  110. void csc_set_coeff(struct csc_data *csc, u32 *csc_reg0,
  111. enum v4l2_colorspace src_colorspace,
  112. enum v4l2_colorspace dst_colorspace)
  113. {
  114. u32 *csc_reg5 = csc_reg0 + 5;
  115. u32 *shadow_csc = csc_reg0;
  116. struct colorspace_coeffs *sd_hd_coeffs;
  117. u16 *coeff, *end_coeff;
  118. enum v4l2_colorspace yuv_colorspace;
  119. int sel = 0;
  120. /*
  121. * support only graphics data range(full range) for now, a control ioctl
  122. * would be nice here
  123. */
  124. /* Y2R */
  125. if (dst_colorspace == V4L2_COLORSPACE_SRGB &&
  126. (src_colorspace == V4L2_COLORSPACE_SMPTE170M ||
  127. src_colorspace == V4L2_COLORSPACE_REC709)) {
  128. /* Y2R */
  129. sel = 1;
  130. yuv_colorspace = src_colorspace;
  131. } else if ((dst_colorspace == V4L2_COLORSPACE_SMPTE170M ||
  132. dst_colorspace == V4L2_COLORSPACE_REC709) &&
  133. src_colorspace == V4L2_COLORSPACE_SRGB) {
  134. /* R2Y */
  135. sel = 3;
  136. yuv_colorspace = dst_colorspace;
  137. } else {
  138. *csc_reg5 |= CSC_BYPASS;
  139. return;
  140. }
  141. sd_hd_coeffs = &colorspace_coeffs[sel];
  142. /* select between SD or HD coefficients */
  143. if (yuv_colorspace == V4L2_COLORSPACE_SMPTE170M)
  144. coeff = sd_hd_coeffs->sd;
  145. else
  146. coeff = sd_hd_coeffs->hd;
  147. end_coeff = coeff + 12;
  148. for (; coeff < end_coeff; coeff += 2)
  149. *shadow_csc++ = (*(coeff + 1) << 16) | *coeff;
  150. }
  151. EXPORT_SYMBOL(csc_set_coeff);
  152. struct csc_data *csc_create(struct platform_device *pdev, const char *res_name)
  153. {
  154. struct csc_data *csc;
  155. dev_dbg(&pdev->dev, "csc_create\n");
  156. csc = devm_kzalloc(&pdev->dev, sizeof(*csc), GFP_KERNEL);
  157. if (!csc) {
  158. dev_err(&pdev->dev, "couldn't alloc csc_data\n");
  159. return ERR_PTR(-ENOMEM);
  160. }
  161. csc->pdev = pdev;
  162. csc->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  163. res_name);
  164. if (csc->res == NULL) {
  165. dev_err(&pdev->dev, "missing '%s' platform resources data\n",
  166. res_name);
  167. return ERR_PTR(-ENODEV);
  168. }
  169. csc->base = devm_ioremap_resource(&pdev->dev, csc->res);
  170. if (IS_ERR(csc->base)) {
  171. dev_err(&pdev->dev, "failed to ioremap\n");
  172. return ERR_CAST(csc->base);
  173. }
  174. return csc;
  175. }
  176. EXPORT_SYMBOL(csc_create);
  177. MODULE_DESCRIPTION("TI VIP/VPE Color Space Converter");
  178. MODULE_AUTHOR("Texas Instruments Inc.");
  179. MODULE_LICENSE("GPL v2");