cx25840-vbi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* cx25840 VBI functions
  3. */
  4. #include <linux/videodev2.h>
  5. #include <linux/i2c.h>
  6. #include <media/v4l2-common.h>
  7. #include <media/drv-intf/cx25840.h>
  8. #include "cx25840-core.h"
  9. static int odd_parity(u8 c)
  10. {
  11. c ^= (c >> 4);
  12. c ^= (c >> 2);
  13. c ^= (c >> 1);
  14. return c & 1;
  15. }
  16. static int decode_vps(u8 * dst, u8 * p)
  17. {
  18. static const u8 biphase_tbl[] = {
  19. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  20. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  21. 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
  22. 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
  23. 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
  24. 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
  25. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  26. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  27. 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
  28. 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
  29. 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
  30. 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
  31. 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
  32. 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
  33. 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
  34. 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
  35. 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
  36. 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
  37. 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
  38. 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
  39. 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
  40. 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
  41. 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
  42. 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
  43. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  44. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  45. 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
  46. 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
  47. 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
  48. 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
  49. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  50. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  51. };
  52. u8 c, err = 0;
  53. int i;
  54. for (i = 0; i < 2 * 13; i += 2) {
  55. err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
  56. c = (biphase_tbl[p[i + 1]] & 0xf) |
  57. ((biphase_tbl[p[i]] & 0xf) << 4);
  58. dst[i / 2] = c;
  59. }
  60. return err & 0xf0;
  61. }
  62. int cx25840_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  63. {
  64. struct i2c_client *client = v4l2_get_subdevdata(sd);
  65. struct cx25840_state *state = to_state(sd);
  66. static const u16 lcr2vbi[] = {
  67. 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */
  68. 0, V4L2_SLICED_WSS_625, 0, /* 4 */
  69. V4L2_SLICED_CAPTION_525, /* 6 */
  70. 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */
  71. 0, 0, 0, 0
  72. };
  73. int is_pal = !(state->std & V4L2_STD_525_60);
  74. int i;
  75. memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
  76. svbi->service_set = 0;
  77. /* we're done if raw VBI is active */
  78. /* TODO: this will have to be changed for generic_mode VBI */
  79. if ((cx25840_read(client, 0x404) & 0x10) == 0)
  80. return 0;
  81. if (is_pal) {
  82. for (i = 7; i <= 23; i++) {
  83. u8 v = cx25840_read(client,
  84. state->vbi_regs_offset + 0x424 + i - 7);
  85. svbi->service_lines[0][i] = lcr2vbi[v >> 4];
  86. svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
  87. svbi->service_set |= svbi->service_lines[0][i] |
  88. svbi->service_lines[1][i];
  89. }
  90. } else {
  91. for (i = 10; i <= 21; i++) {
  92. u8 v = cx25840_read(client,
  93. state->vbi_regs_offset + 0x424 + i - 10);
  94. svbi->service_lines[0][i] = lcr2vbi[v >> 4];
  95. svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
  96. svbi->service_set |= svbi->service_lines[0][i] |
  97. svbi->service_lines[1][i];
  98. }
  99. }
  100. return 0;
  101. }
  102. int cx25840_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
  103. {
  104. struct i2c_client *client = v4l2_get_subdevdata(sd);
  105. struct cx25840_state *state = to_state(sd);
  106. int is_pal = !(state->std & V4L2_STD_525_60);
  107. int vbi_offset = is_pal ? 1 : 0;
  108. /* Setup standard */
  109. cx25840_std_setup(client);
  110. /* VBI Offset */
  111. if (is_cx23888(state))
  112. cx25840_write(client, 0x54f, vbi_offset);
  113. else
  114. cx25840_write(client, 0x47f, vbi_offset);
  115. /* TODO: this will have to be changed for generic_mode VBI */
  116. cx25840_write(client, 0x404, 0x2e);
  117. return 0;
  118. }
  119. int cx25840_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  120. {
  121. struct i2c_client *client = v4l2_get_subdevdata(sd);
  122. struct cx25840_state *state = to_state(sd);
  123. int is_pal = !(state->std & V4L2_STD_525_60);
  124. int vbi_offset = is_pal ? 1 : 0;
  125. int i, x;
  126. u8 lcr[24];
  127. for (x = 0; x <= 23; x++)
  128. lcr[x] = 0x00;
  129. /* Setup standard */
  130. cx25840_std_setup(client);
  131. /* Sliced VBI */
  132. /* TODO: this will have to be changed for generic_mode VBI */
  133. cx25840_write(client, 0x404, 0x32); /* Ancillary data */
  134. cx25840_write(client, 0x406, 0x13);
  135. if (is_cx23888(state))
  136. cx25840_write(client, 0x54f, vbi_offset);
  137. else
  138. cx25840_write(client, 0x47f, vbi_offset);
  139. if (is_pal) {
  140. for (i = 0; i <= 6; i++)
  141. svbi->service_lines[0][i] =
  142. svbi->service_lines[1][i] = 0;
  143. } else {
  144. for (i = 0; i <= 9; i++)
  145. svbi->service_lines[0][i] =
  146. svbi->service_lines[1][i] = 0;
  147. for (i = 22; i <= 23; i++)
  148. svbi->service_lines[0][i] =
  149. svbi->service_lines[1][i] = 0;
  150. }
  151. for (i = 7; i <= 23; i++) {
  152. for (x = 0; x <= 1; x++) {
  153. switch (svbi->service_lines[1-x][i]) {
  154. case V4L2_SLICED_TELETEXT_B:
  155. lcr[i] |= 1 << (4 * x);
  156. break;
  157. case V4L2_SLICED_WSS_625:
  158. lcr[i] |= 4 << (4 * x);
  159. break;
  160. case V4L2_SLICED_CAPTION_525:
  161. lcr[i] |= 6 << (4 * x);
  162. break;
  163. case V4L2_SLICED_VPS:
  164. lcr[i] |= 9 << (4 * x);
  165. break;
  166. }
  167. }
  168. }
  169. if (is_pal) {
  170. for (x = 1, i = state->vbi_regs_offset + 0x424;
  171. i <= state->vbi_regs_offset + 0x434; i++, x++)
  172. cx25840_write(client, i, lcr[6 + x]);
  173. } else {
  174. for (x = 1, i = state->vbi_regs_offset + 0x424;
  175. i <= state->vbi_regs_offset + 0x430; i++, x++)
  176. cx25840_write(client, i, lcr[9 + x]);
  177. for (i = state->vbi_regs_offset + 0x431;
  178. i <= state->vbi_regs_offset + 0x434; i++)
  179. cx25840_write(client, i, 0);
  180. }
  181. cx25840_write(client, state->vbi_regs_offset + 0x43c, 0x16);
  182. /* TODO: this will have to be changed for generic_mode VBI */
  183. if (is_cx23888(state))
  184. cx25840_write(client, 0x428, is_pal ? 0x2a : 0x22);
  185. else
  186. cx25840_write(client, 0x474, is_pal ? 0x2a : 0x22);
  187. return 0;
  188. }
  189. int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi)
  190. {
  191. struct cx25840_state *state = to_state(sd);
  192. u8 *p = vbi->p;
  193. int id1, id2, l, err = 0;
  194. if (p[0] || p[1] != 0xff || p[2] != 0xff ||
  195. (p[3] != 0x55 && p[3] != 0x91)) {
  196. vbi->line = vbi->type = 0;
  197. return 0;
  198. }
  199. p += 4;
  200. id1 = p[-1];
  201. id2 = p[0] & 0xf;
  202. l = p[2] & 0x3f;
  203. l += state->vbi_line_offset;
  204. p += 4;
  205. switch (id2) {
  206. case 1:
  207. id2 = V4L2_SLICED_TELETEXT_B;
  208. break;
  209. case 4:
  210. id2 = V4L2_SLICED_WSS_625;
  211. break;
  212. case 6:
  213. id2 = V4L2_SLICED_CAPTION_525;
  214. err = !odd_parity(p[0]) || !odd_parity(p[1]);
  215. break;
  216. case 9:
  217. id2 = V4L2_SLICED_VPS;
  218. if (decode_vps(p, p) != 0)
  219. err = 1;
  220. break;
  221. default:
  222. id2 = 0;
  223. err = 1;
  224. break;
  225. }
  226. vbi->type = err ? 0 : id2;
  227. vbi->line = err ? 0 : l;
  228. vbi->is_second_field = err ? 0 : (id1 == 0x55);
  229. vbi->p = p;
  230. return 0;
  231. }