csc.c 4.5 KB

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