Path: blob/master/drivers/media/video/cx25840/cx25840-vbi.c
17787 views
/* cx25840 VBI functions1*2* This program is free software; you can redistribute it and/or3* modify it under the terms of the GNU General Public License4* as published by the Free Software Foundation; either version 25* of the License, or (at your option) any later version.6*7* This program is distributed in the hope that it will be useful,8* but WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the10* GNU General Public License for more details.11*12* You should have received a copy of the GNU General Public License13* along with this program; if not, write to the Free Software14* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.15*/161718#include <linux/videodev2.h>19#include <linux/i2c.h>20#include <media/v4l2-common.h>21#include <media/cx25840.h>2223#include "cx25840-core.h"2425static int odd_parity(u8 c)26{27c ^= (c >> 4);28c ^= (c >> 2);29c ^= (c >> 1);3031return c & 1;32}3334static int decode_vps(u8 * dst, u8 * p)35{36static const u8 biphase_tbl[] = {370xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,380xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,390xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,400x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,410xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,420x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,430xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,440xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,450xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,460xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,470xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,480x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,490xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,500x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,510xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,520xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,530xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,540xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,550xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,560x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,570xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,580x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,590xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,600xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,610xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,620xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,630xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,640x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,650xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,660x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,670xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,680xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,69};7071u8 c, err = 0;72int i;7374for (i = 0; i < 2 * 13; i += 2) {75err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];76c = (biphase_tbl[p[i + 1]] & 0xf) |77((biphase_tbl[p[i]] & 0xf) << 4);78dst[i / 2] = c;79}8081return err & 0xf0;82}8384int cx25840_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)85{86struct i2c_client *client = v4l2_get_subdevdata(sd);87struct cx25840_state *state = to_state(sd);88static const u16 lcr2vbi[] = {890, V4L2_SLICED_TELETEXT_B, 0, /* 1 */900, V4L2_SLICED_WSS_625, 0, /* 4 */91V4L2_SLICED_CAPTION_525, /* 6 */920, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */930, 0, 0, 094};95int is_pal = !(state->std & V4L2_STD_525_60);96int i;9798memset(svbi, 0, sizeof(*svbi));99/* we're done if raw VBI is active */100if ((cx25840_read(client, 0x404) & 0x10) == 0)101return 0;102103if (is_pal) {104for (i = 7; i <= 23; i++) {105u8 v = cx25840_read(client, 0x424 + i - 7);106107svbi->service_lines[0][i] = lcr2vbi[v >> 4];108svbi->service_lines[1][i] = lcr2vbi[v & 0xf];109svbi->service_set |= svbi->service_lines[0][i] |110svbi->service_lines[1][i];111}112} else {113for (i = 10; i <= 21; i++) {114u8 v = cx25840_read(client, 0x424 + i - 10);115116svbi->service_lines[0][i] = lcr2vbi[v >> 4];117svbi->service_lines[1][i] = lcr2vbi[v & 0xf];118svbi->service_set |= svbi->service_lines[0][i] |119svbi->service_lines[1][i];120}121}122return 0;123}124125int cx25840_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)126{127struct i2c_client *client = v4l2_get_subdevdata(sd);128struct cx25840_state *state = to_state(sd);129int is_pal = !(state->std & V4L2_STD_525_60);130int vbi_offset = is_pal ? 1 : 0;131132/* Setup standard */133cx25840_std_setup(client);134135/* VBI Offset */136cx25840_write(client, 0x47f, vbi_offset);137cx25840_write(client, 0x404, 0x2e);138return 0;139}140141int cx25840_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)142{143struct i2c_client *client = v4l2_get_subdevdata(sd);144struct cx25840_state *state = to_state(sd);145int is_pal = !(state->std & V4L2_STD_525_60);146int vbi_offset = is_pal ? 1 : 0;147int i, x;148u8 lcr[24];149150for (x = 0; x <= 23; x++)151lcr[x] = 0x00;152153/* Setup standard */154cx25840_std_setup(client);155156/* Sliced VBI */157cx25840_write(client, 0x404, 0x32); /* Ancillary data */158cx25840_write(client, 0x406, 0x13);159cx25840_write(client, 0x47f, vbi_offset);160161if (is_pal) {162for (i = 0; i <= 6; i++)163svbi->service_lines[0][i] =164svbi->service_lines[1][i] = 0;165} else {166for (i = 0; i <= 9; i++)167svbi->service_lines[0][i] =168svbi->service_lines[1][i] = 0;169170for (i = 22; i <= 23; i++)171svbi->service_lines[0][i] =172svbi->service_lines[1][i] = 0;173}174175for (i = 7; i <= 23; i++) {176for (x = 0; x <= 1; x++) {177switch (svbi->service_lines[1-x][i]) {178case V4L2_SLICED_TELETEXT_B:179lcr[i] |= 1 << (4 * x);180break;181case V4L2_SLICED_WSS_625:182lcr[i] |= 4 << (4 * x);183break;184case V4L2_SLICED_CAPTION_525:185lcr[i] |= 6 << (4 * x);186break;187case V4L2_SLICED_VPS:188lcr[i] |= 9 << (4 * x);189break;190}191}192}193194if (is_pal) {195for (x = 1, i = 0x424; i <= 0x434; i++, x++)196cx25840_write(client, i, lcr[6 + x]);197} else {198for (x = 1, i = 0x424; i <= 0x430; i++, x++)199cx25840_write(client, i, lcr[9 + x]);200for (i = 0x431; i <= 0x434; i++)201cx25840_write(client, i, 0);202}203204cx25840_write(client, 0x43c, 0x16);205cx25840_write(client, 0x474, is_pal ? 0x2a : 0x22);206return 0;207}208209int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi)210{211struct cx25840_state *state = to_state(sd);212u8 *p = vbi->p;213int id1, id2, l, err = 0;214215if (p[0] || p[1] != 0xff || p[2] != 0xff ||216(p[3] != 0x55 && p[3] != 0x91)) {217vbi->line = vbi->type = 0;218return 0;219}220221p += 4;222id1 = p[-1];223id2 = p[0] & 0xf;224l = p[2] & 0x3f;225l += state->vbi_line_offset;226p += 4;227228switch (id2) {229case 1:230id2 = V4L2_SLICED_TELETEXT_B;231break;232case 4:233id2 = V4L2_SLICED_WSS_625;234break;235case 6:236id2 = V4L2_SLICED_CAPTION_525;237err = !odd_parity(p[0]) || !odd_parity(p[1]);238break;239case 9:240id2 = V4L2_SLICED_VPS;241if (decode_vps(p, p) != 0)242err = 1;243break;244default:245id2 = 0;246err = 1;247break;248}249250vbi->type = err ? 0 : id2;251vbi->line = err ? 0 : l;252vbi->is_second_field = err ? 0 : (id1 == 0x55);253vbi->p = p;254return 0;255}256257258