Commit 5b73e98c authored by Hans Verkuil's avatar Hans Verkuil Committed by Mauro Carvalho Chehab

V4L/DVB (10246): saa6752hs: convert to v4l2_subdev.

Signed-off-by: default avatarHans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent b634a93f
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-common.h> #include <media/v4l2-common.h>
#include <media/v4l2-chip-ident.h> #include <media/v4l2-chip-ident.h>
#include <media/v4l2-i2c-drv-legacy.h> #include <media/v4l2-i2c-drv-legacy.h>
...@@ -95,6 +96,7 @@ static const struct v4l2_format v4l2_format_table[] = ...@@ -95,6 +96,7 @@ static const struct v4l2_format v4l2_format_table[] =
}; };
struct saa6752hs_state { struct saa6752hs_state {
struct v4l2_subdev sd;
int chip; int chip;
u32 revision; u32 revision;
int has_ac3; int has_ac3;
...@@ -115,6 +117,11 @@ enum saa6752hs_command { ...@@ -115,6 +117,11 @@ enum saa6752hs_command {
SAA6752HS_COMMAND_MAX SAA6752HS_COMMAND_MAX
}; };
static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
{
return container_of(sd, struct saa6752hs_state, sd);
}
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
static u8 PAT[] = { static u8 PAT[] = {
...@@ -360,57 +367,65 @@ static int saa6752hs_set_bitrate(struct i2c_client *client, ...@@ -360,57 +367,65 @@ static int saa6752hs_set_bitrate(struct i2c_client *client,
return 0; return 0;
} }
static void saa6752hs_set_subsampling(struct i2c_client *client,
struct v4l2_format *f)
{
struct saa6752hs_state *h = i2c_get_clientdata(client);
int dist_352, dist_480, dist_720;
/*
FIXME: translate and round width/height into EMPRESS
subsample type:
type | PAL | NTSC static int get_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
--------------------------- struct v4l2_ext_control *ctrl)
SIF | 352x288 | 352x240 {
1/2 D1 | 352x576 | 352x480 switch (ctrl->id) {
2/3 D1 | 480x576 | 480x480 case V4L2_CID_MPEG_STREAM_TYPE:
D1 | 720x576 | 720x480 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
*/ break;
case V4L2_CID_MPEG_STREAM_PID_PMT:
dist_352 = abs(f->fmt.pix.width - 352); ctrl->value = params->ts_pid_pmt;
dist_480 = abs(f->fmt.pix.width - 480); break;
dist_720 = abs(f->fmt.pix.width - 720); case V4L2_CID_MPEG_STREAM_PID_AUDIO:
if (dist_720 < dist_480) { ctrl->value = params->ts_pid_audio;
f->fmt.pix.width = 720; break;
f->fmt.pix.height = 576; case V4L2_CID_MPEG_STREAM_PID_VIDEO:
h->video_format = SAA6752HS_VF_D1; ctrl->value = params->ts_pid_video;
} break;
else if (dist_480 < dist_352) { case V4L2_CID_MPEG_STREAM_PID_PCR:
f->fmt.pix.width = 480; ctrl->value = params->ts_pid_pcr;
f->fmt.pix.height = 576; break;
h->video_format = SAA6752HS_VF_2_3_D1; case V4L2_CID_MPEG_AUDIO_ENCODING:
} ctrl->value = params->au_encoding;
else { break;
f->fmt.pix.width = 352; case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
if (abs(f->fmt.pix.height - 576) < ctrl->value = params->au_l2_bitrate;
abs(f->fmt.pix.height - 288)) { break;
f->fmt.pix.height = 576; case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
h->video_format = SAA6752HS_VF_1_2_D1; if (!has_ac3)
} return -EINVAL;
else { ctrl->value = params->au_ac3_bitrate;
f->fmt.pix.height = 288; break;
h->video_format = SAA6752HS_VF_SIF; case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
} ctrl->value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
break;
case V4L2_CID_MPEG_VIDEO_ENCODING:
ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
break;
case V4L2_CID_MPEG_VIDEO_ASPECT:
ctrl->value = params->vi_aspect;
break;
case V4L2_CID_MPEG_VIDEO_BITRATE:
ctrl->value = params->vi_bitrate * 1000;
break;
case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
ctrl->value = params->vi_bitrate_peak * 1000;
break;
case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
ctrl->value = params->vi_bitrate_mode;
break;
default:
return -EINVAL;
} }
return 0;
} }
static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params, static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
struct v4l2_ext_control *ctrl, unsigned int cmd) struct v4l2_ext_control *ctrl, int set)
{ {
int old = 0, new; int old = 0, new;
int set = (cmd == VIDIOC_S_EXT_CTRLS);
new = ctrl->value; new = ctrl->value;
switch (ctrl->id) { switch (ctrl->id) {
...@@ -529,16 +544,14 @@ static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params, ...@@ -529,16 +544,14 @@ static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
default: default:
return -EINVAL; return -EINVAL;
} }
if (cmd == VIDIOC_G_EXT_CTRLS)
ctrl->value = old;
else
ctrl->value = new; ctrl->value = new;
return 0; return 0;
} }
static int saa6752hs_qctrl(struct saa6752hs_state *h,
struct v4l2_queryctrl *qctrl) static int saa6752hs_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
{ {
struct saa6752hs_state *h = to_state(sd);
struct saa6752hs_mpeg_params *params = &h->params; struct saa6752hs_mpeg_params *params = &h->params;
int err; int err;
...@@ -610,8 +623,7 @@ static int saa6752hs_qctrl(struct saa6752hs_state *h, ...@@ -610,8 +623,7 @@ static int saa6752hs_qctrl(struct saa6752hs_state *h,
return -EINVAL; return -EINVAL;
} }
static int saa6752hs_qmenu(struct saa6752hs_state *h, static int saa6752hs_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qmenu)
struct v4l2_querymenu *qmenu)
{ {
static const u32 mpeg_audio_encoding[] = { static const u32 mpeg_audio_encoding[] = {
V4L2_MPEG_AUDIO_ENCODING_LAYER_2, V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
...@@ -632,11 +644,12 @@ static int saa6752hs_qmenu(struct saa6752hs_state *h, ...@@ -632,11 +644,12 @@ static int saa6752hs_qmenu(struct saa6752hs_state *h,
V4L2_MPEG_AUDIO_AC3_BITRATE_384K, V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
V4L2_CTRL_MENU_IDS_END V4L2_CTRL_MENU_IDS_END
}; };
struct saa6752hs_state *h = to_state(sd);
struct v4l2_queryctrl qctrl; struct v4l2_queryctrl qctrl;
int err; int err;
qctrl.id = qmenu->id; qctrl.id = qmenu->id;
err = saa6752hs_qctrl(h, &qctrl); err = saa6752hs_queryctrl(sd, &qctrl);
if (err) if (err)
return err; return err;
switch (qmenu->id) { switch (qmenu->id) {
...@@ -656,17 +669,16 @@ static int saa6752hs_qmenu(struct saa6752hs_state *h, ...@@ -656,17 +669,16 @@ static int saa6752hs_qmenu(struct saa6752hs_state *h,
return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL); return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
} }
static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes) static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
{ {
unsigned char buf[9], buf2[4]; unsigned char buf[9], buf2[4];
struct saa6752hs_state *h; struct saa6752hs_state *h = to_state(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd);
unsigned size; unsigned size;
u32 crc; u32 crc;
unsigned char localPAT[256]; unsigned char localPAT[256];
unsigned char localPMT[256]; unsigned char localPMT[256];
h = i2c_get_clientdata(client);
/* Set video format - must be done first as it resets other settings */ /* Set video format - must be done first as it resets other settings */
set_reg8(client, 0x41, h->video_format); set_reg8(client, 0x41, h->video_format);
...@@ -762,7 +774,7 @@ static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes) ...@@ -762,7 +774,7 @@ static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes)
buf[3] = 0x82; buf[3] = 0x82;
buf[4] = 0xB0; buf[4] = 0xB0;
buf[5] = buf2[0]; buf[5] = buf2[0];
switch(h->params.vi_aspect) { switch (h->params.vi_aspect) {
case V4L2_MPEG_VIDEO_ASPECT_16x9: case V4L2_MPEG_VIDEO_ASPECT_16x9:
buf[6] = buf2[1] | 0x40; buf[6] = buf2[1] | 0x40;
break; break;
...@@ -770,7 +782,6 @@ static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes) ...@@ -770,7 +782,6 @@ static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes)
default: default:
buf[6] = buf2[1] & 0xBF; buf[6] = buf2[1] & 0xBF;
break; break;
break;
} }
buf[7] = buf2[2]; buf[7] = buf2[2];
buf[8] = buf2[3]; buf[8] = buf2[3];
...@@ -779,45 +790,61 @@ static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes) ...@@ -779,45 +790,61 @@ static int saa6752hs_init(struct i2c_client *client, u32 leading_null_bytes)
return 0; return 0;
} }
static int static int saa6752hs_do_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls, int set)
saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
{ {
struct saa6752hs_state *h = i2c_get_clientdata(client); struct saa6752hs_state *h = to_state(sd);
struct v4l2_ext_controls *ctrls = arg;
struct saa6752hs_mpeg_params params; struct saa6752hs_mpeg_params params;
int err = 0;
int i; int i;
switch (cmd) {
case VIDIOC_INT_INIT:
/* apply settings and start encoder */
saa6752hs_init(client, *(u32 *)arg);
break;
case VIDIOC_S_EXT_CTRLS:
if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
return -EINVAL;
/* fall through */
case VIDIOC_TRY_EXT_CTRLS:
case VIDIOC_G_EXT_CTRLS:
if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
return -EINVAL; return -EINVAL;
params = h->params; params = h->params;
for (i = 0; i < ctrls->count; i++) { for (i = 0; i < ctrls->count; i++) {
err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, cmd); int err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, set);
if (err) { if (err) {
ctrls->error_idx = i; ctrls->error_idx = i;
return err; return err;
} }
} }
if (set)
h->params = params; h->params = params;
break; return 0;
case VIDIOC_QUERYCTRL: }
return saa6752hs_qctrl(h, arg);
case VIDIOC_QUERYMENU: static int saa6752hs_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
return saa6752hs_qmenu(h, arg); {
case VIDIOC_G_FMT: return saa6752hs_do_ext_ctrls(sd, ctrls, 1);
{ }
struct v4l2_format *f = arg;
static int saa6752hs_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
{
return saa6752hs_do_ext_ctrls(sd, ctrls, 0);
}
static int saa6752hs_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
{
struct saa6752hs_state *h = to_state(sd);
int i;
if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
return -EINVAL;
for (i = 0; i < ctrls->count; i++) {
int err = get_ctrl(h->has_ac3, &h->params, ctrls->controls + i);
if (err) {
ctrls->error_idx = i;
return err;
}
}
return 0;
}
static int saa6752hs_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
{
struct saa6752hs_state *h = to_state(sd);
if (h->video_format == SAA6752HS_VF_UNKNOWN) if (h->video_format == SAA6752HS_VF_UNKNOWN)
h->video_format = SAA6752HS_VF_D1; h->video_format = SAA6752HS_VF_D1;
...@@ -825,35 +852,105 @@ saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg) ...@@ -825,35 +852,105 @@ saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
v4l2_format_table[h->video_format].fmt.pix.width; v4l2_format_table[h->video_format].fmt.pix.width;
f->fmt.pix.height = f->fmt.pix.height =
v4l2_format_table[h->video_format].fmt.pix.height; v4l2_format_table[h->video_format].fmt.pix.height;
break ; return 0;
} }
case VIDIOC_S_FMT:
{
struct v4l2_format *f = arg;
saa6752hs_set_subsampling(client, f); static int saa6752hs_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
break; {
} struct saa6752hs_state *h = to_state(sd);
case VIDIOC_S_STD: int dist_352, dist_480, dist_720;
h->standard = *((v4l2_std_id *) arg);
break;
case VIDIOC_DBG_G_CHIP_IDENT: /*
return v4l2_chip_ident_i2c_client(client, FIXME: translate and round width/height into EMPRESS
arg, h->chip, h->revision); subsample type:
default: type | PAL | NTSC
/* nothing */ ---------------------------
break; SIF | 352x288 | 352x240
1/2 D1 | 352x576 | 352x480
2/3 D1 | 480x576 | 480x480
D1 | 720x576 | 720x480
*/
dist_352 = abs(f->fmt.pix.width - 352);
dist_480 = abs(f->fmt.pix.width - 480);
dist_720 = abs(f->fmt.pix.width - 720);
if (dist_720 < dist_480) {
f->fmt.pix.width = 720;
f->fmt.pix.height = 576;
h->video_format = SAA6752HS_VF_D1;
} else if (dist_480 < dist_352) {
f->fmt.pix.width = 480;
f->fmt.pix.height = 576;
h->video_format = SAA6752HS_VF_2_3_D1;
} else {
f->fmt.pix.width = 352;
if (abs(f->fmt.pix.height - 576) <
abs(f->fmt.pix.height - 288)) {
f->fmt.pix.height = 576;
h->video_format = SAA6752HS_VF_1_2_D1;
} else {
f->fmt.pix.height = 288;
h->video_format = SAA6752HS_VF_SIF;
}
} }
return 0;
}
return err; static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
{
struct saa6752hs_state *h = to_state(sd);
h->standard = std;
return 0;
}
static int saa6752hs_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct saa6752hs_state *h = to_state(sd);
return v4l2_chip_ident_i2c_client(client,
chip, h->chip, h->revision);
}
static int saa6752hs_command(struct i2c_client *client, unsigned cmd, void *arg)
{
return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
} }
/* ----------------------------------------------------------------------- */
static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
.g_chip_ident = saa6752hs_g_chip_ident,
.init = saa6752hs_init,
.queryctrl = saa6752hs_queryctrl,
.querymenu = saa6752hs_querymenu,
.g_ext_ctrls = saa6752hs_g_ext_ctrls,
.s_ext_ctrls = saa6752hs_s_ext_ctrls,
.try_ext_ctrls = saa6752hs_try_ext_ctrls,
};
static const struct v4l2_subdev_tuner_ops saa6752hs_tuner_ops = {
.s_std = saa6752hs_s_std,
};
static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
.s_fmt = saa6752hs_s_fmt,
.g_fmt = saa6752hs_g_fmt,
};
static const struct v4l2_subdev_ops saa6752hs_ops = {
.core = &saa6752hs_core_ops,
.tuner = &saa6752hs_tuner_ops,
.video = &saa6752hs_video_ops,
};
static int saa6752hs_probe(struct i2c_client *client, static int saa6752hs_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL); struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
struct v4l2_subdev *sd;
u8 addr = 0x13; u8 addr = 0x13;
u8 data[12]; u8 data[12];
...@@ -861,6 +958,8 @@ static int saa6752hs_probe(struct i2c_client *client, ...@@ -861,6 +958,8 @@ static int saa6752hs_probe(struct i2c_client *client,
client->addr << 1, client->adapter->name); client->addr << 1, client->adapter->name);
if (h == NULL) if (h == NULL)
return -ENOMEM; return -ENOMEM;
sd = &h->sd;
v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
i2c_master_send(client, &addr, 1); i2c_master_send(client, &addr, 1);
i2c_master_recv(client, data, sizeof(data)); i2c_master_recv(client, data, sizeof(data));
...@@ -874,14 +973,15 @@ static int saa6752hs_probe(struct i2c_client *client, ...@@ -874,14 +973,15 @@ static int saa6752hs_probe(struct i2c_client *client,
} }
h->params = param_defaults; h->params = param_defaults;
h->standard = 0; /* Assume 625 input lines */ h->standard = 0; /* Assume 625 input lines */
i2c_set_clientdata(client, h);
return 0; return 0;
} }
static int saa6752hs_remove(struct i2c_client *client) static int saa6752hs_remove(struct i2c_client *client)
{ {
kfree(i2c_get_clientdata(client)); struct v4l2_subdev *sd = i2c_get_clientdata(client);
v4l2_device_unregister_subdev(sd);
kfree(to_state(sd));
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment