csc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. u32 read_reg(struct csc_data *csc, int offset)
  90. {
  91. return ioread32(csc->base + offset);
  92. }
  93. #define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, read_reg(csc, CSC_##r))
  94. DUMPREG(CSC00);
  95. DUMPREG(CSC01);
  96. DUMPREG(CSC02);
  97. DUMPREG(CSC03);
  98. DUMPREG(CSC04);
  99. DUMPREG(CSC05);
  100. #undef DUMPREG
  101. }
  102. void csc_set_coeff_bypass(struct csc_data *csc, u32 *csc_reg5)
  103. {
  104. *csc_reg5 |= CSC_BYPASS;
  105. }
  106. /*
  107. * set the color space converter coefficient shadow register values
  108. */
  109. void csc_set_coeff(struct csc_data *csc, u32 *csc_reg0,
  110. enum v4l2_colorspace src_colorspace,
  111. enum v4l2_colorspace dst_colorspace)
  112. {
  113. u32 *csc_reg5 = csc_reg0 + 5;
  114. u32 *shadow_csc = csc_reg0;
  115. struct colorspace_coeffs *sd_hd_coeffs;
  116. u16 *coeff, *end_coeff;
  117. enum v4l2_colorspace yuv_colorspace;
  118. int sel = 0;
  119. /*
  120. * support only graphics data range(full range) for now, a control ioctl
  121. * would be nice here
  122. */
  123. /* Y2R */
  124. if (dst_colorspace == V4L2_COLORSPACE_SRGB &&
  125. (src_colorspace == V4L2_COLORSPACE_SMPTE170M ||
  126. src_colorspace == V4L2_COLORSPACE_REC709)) {
  127. /* Y2R */
  128. sel = 1;
  129. yuv_colorspace = src_colorspace;
  130. } else if ((dst_colorspace == V4L2_COLORSPACE_SMPTE170M ||
  131. dst_colorspace == V4L2_COLORSPACE_REC709) &&
  132. src_colorspace == V4L2_COLORSPACE_SRGB) {
  133. /* R2Y */
  134. sel = 3;
  135. yuv_colorspace = dst_colorspace;
  136. } else {
  137. *csc_reg5 |= CSC_BYPASS;
  138. return;
  139. }
  140. sd_hd_coeffs = &colorspace_coeffs[sel];
  141. /* select between SD or HD coefficients */
  142. if (yuv_colorspace == V4L2_COLORSPACE_SMPTE170M)
  143. coeff = sd_hd_coeffs->sd;
  144. else
  145. coeff = sd_hd_coeffs->hd;
  146. end_coeff = coeff + 12;
  147. for (; coeff < end_coeff; coeff += 2)
  148. *shadow_csc++ = (*(coeff + 1) << 16) | *coeff;
  149. }
  150. struct csc_data *csc_create(struct platform_device *pdev)
  151. {
  152. struct csc_data *csc;
  153. dev_dbg(&pdev->dev, "csc_create\n");
  154. csc = devm_kzalloc(&pdev->dev, sizeof(*csc), GFP_KERNEL);
  155. if (!csc) {
  156. dev_err(&pdev->dev, "couldn't alloc csc_data\n");
  157. return ERR_PTR(-ENOMEM);
  158. }
  159. csc->pdev = pdev;
  160. csc->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  161. "csc");
  162. if (csc->res == NULL) {
  163. dev_err(&pdev->dev, "missing platform resources data\n");
  164. return ERR_PTR(-ENODEV);
  165. }
  166. csc->base = devm_ioremap_resource(&pdev->dev, csc->res);
  167. if (IS_ERR(csc->base)) {
  168. dev_err(&pdev->dev, "failed to ioremap\n");
  169. return csc->base;
  170. }
  171. return csc;
  172. }