// SPDX-License-Identifier: GPL-2.0-only /* * i2c tv tuner chip device driver * core core, i.e. kernel interfaces, registering and so on * * Copyright(c) by Ralph Metzler, Gerd Knorr, Gunther Mayer * * Copyright(c) 2005-2011 by Mauro Carvalho Chehab * - Added support for a separate Radio tuner * - Major rework and cleanups at the code * * This driver supports many devices and the idea is to let the driver * detect which device is present. So rather than listing all supported * devices here, we pretend to support a single, fake device type that will * handle both radio and analog TV tuning.
*/
#define dprintk(fmt, arg...) do { \ if (tuner_debug) \
printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg); \
} while (0)
/* * Internal enums/struct used inside the driver
*/
/** * enum tuner_pad_index - tuner pad index for MEDIA_ENT_F_TUNER * * @TUNER_PAD_RF_INPUT: * Radiofrequency (RF) sink pad, usually linked to a RF connector entity. * @TUNER_PAD_OUTPUT: * tuner video output source pad. Contains the video chrominance * and luminance or the hole bandwidth of the signal converted to * an Intermediate Frequency (IF) or to baseband (on zero-IF tuners). * @TUNER_PAD_AUD_OUT: * Tuner audio output source pad. Tuners used to decode analog TV * signals have an extra pad for audio output. Old tuners use an * analog stage with a saw filter for the audio IF frequency. The * output of the pad is, in this case, the audio IF, with should be * decoded either by the bridge chipset (that's the case of cx2388x * chipsets) or may require an external IF sound processor, like * msp34xx. On modern silicon tuners, the audio IF decoder is usually * incorporated at the tuner. On such case, the output of this pad * is an audio sampled data. * @TUNER_NUM_PADS: * Number of pads of the tuner.
*/ enum tuner_pad_index {
TUNER_PAD_RF_INPUT,
TUNER_PAD_OUTPUT,
TUNER_PAD_AUD_OUT,
TUNER_NUM_PADS
};
/** * enum if_vid_dec_pad_index - video IF-PLL pad index * for MEDIA_ENT_F_IF_VID_DECODER * * @IF_VID_DEC_PAD_IF_INPUT: * video Intermediate Frequency (IF) sink pad * @IF_VID_DEC_PAD_OUT: * IF-PLL video output source pad. Contains the video chrominance * and luminance IF signals. * @IF_VID_DEC_PAD_NUM_PADS: * Number of pads of the video IF-PLL.
*/ enum if_vid_dec_pad_index {
IF_VID_DEC_PAD_IF_INPUT,
IF_VID_DEC_PAD_OUT,
IF_VID_DEC_PAD_NUM_PADS
};
if (NULL == fe_tuner_ops->set_analog_params) {
pr_warn("Tuner frontend module has no way to set freq\n"); return;
}
fe_tuner_ops->set_analog_params(fe, params);
}
/* * Functions to select between radio and TV and tuner probe/remove functions
*/
/** * set_type - Sets the tuner type for a given device * * @c: i2c_client descriptor * @type: type of the tuner (e. g. tuner number) * @new_mode_mask: Indicates if tuner supports TV and/or Radio * @new_config: an optional parameter used by a few tuners to adjust * internal parameters, like LNA mode * @tuner_callback: an optional function to be called when switching * to analog mode * * This function applies the tuner config to tuner specified * by tun_setup structure. It contains several per-tuner initialization "magic"
*/ staticvoid set_type(struct i2c_client *c, unsignedint type, unsignedint new_mode_mask, void *new_config, int (*tuner_callback) (void *dev, int component, int cmd, int arg))
{ struct tuner *t = to_tuner(i2c_get_clientdata(c)); struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; unsignedchar buffer[4]; int tune_now = 1;
if (type == UNSET || type == TUNER_ABSENT) {
dprintk("tuner 0x%02x: Tuner type absent\n", c->addr); return;
}
if (fe_tuner_ops->get_rf_strength)
analog_ops->has_signal = fe_tuner_ops->get_rf_strength; if (fe_tuner_ops->get_afc)
analog_ops->get_afc = fe_tuner_ops->get_afc;
/* Some tuners require more initialization setup before use, such as firmware download or device calibration. trying to set a frequency here will just fail FIXME: better to move set_freq to the tuner code. This is needed on analog tuners for PLL to properly work
*/ if (tune_now) { if (V4L2_TUNER_RADIO == t->mode)
set_radio_freq(c, t->radio_freq); else
set_tv_freq(c, t->tv_freq);
}
dprintk("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
c->adapter->name, c->dev.driver->name, c->addr << 1, type,
t->mode_mask); return;
attach_failed:
dprintk("Tuner attach for type = %d failed.\n", t->type);
t->type = TUNER_ABSENT;
return;
}
/** * tuner_s_type_addr - Sets the tuner type for a device * * @sd: subdev descriptor * @tun_setup: type to be associated to a given tuner i2c address * * This function applies the tuner config to tuner specified * by tun_setup structure. * If tuner I2C address is UNSET, then it will only set the device * if the tuner supports the mode specified in the call. * If the address is specified, the change will be applied only if * tuner I2C address matches. * The call can change the tuner number and the tuner mode.
*/ staticint tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *tun_setup)
{ struct tuner *t = to_tuner(sd); struct i2c_client *c = v4l2_get_subdevdata(sd);
if (analog_ops->set_config) {
analog_ops->set_config(&t->fe, cfg->priv); return 0;
}
dprintk("Tuner frontend module has no way to set config\n"); return 0;
}
/** * tuner_lookup - Seek for tuner adapters * * @adap: i2c_adapter struct * @radio: pointer to be filled if the adapter is radio * @tv: pointer to be filled if the adapter is TV * * Search for existing radio and/or TV tuners on the given I2C adapter, * discarding demod-only adapters (tda9887). * * Note that when this function is called from tuner_probe you can be * certain no other devices will be added/deleted at the same time, I2C * core protects against that.
*/ staticvoid tuner_lookup(struct i2c_adapter *adap, struct tuner **radio, struct tuner **tv)
{ struct tuner *pos;
*radio = NULL;
*tv = NULL;
list_for_each_entry(pos, &tuner_list, list) { int mode_mask;
if (pos->i2c->adapter != adap ||
strcmp(pos->i2c->dev.driver->name, "tuner")) continue;
mode_mask = pos->mode_mask; if (*radio == NULL && mode_mask == T_RADIO)
*radio = pos; /* Note: currently TDA9887 is the only demod-only device. If other devices appear then we need to
make this test more general. */ elseif (*tv == NULL && pos->type != TUNER_TDA9887 &&
(pos->mode_mask & T_ANALOG_TV))
*tv = pos;
}
}
/** *tuner_probe - Probes the existing tuners on an I2C bus * * @client: i2c_client descriptor * * This routine probes for tuners at the expected I2C addresses. On most * cases, if a device answers to a given I2C address, it assumes that the * device is a tuner. On a few cases, however, an additional logic is needed * to double check if the device is really a tuner, or to identify the tuner * type, like on tea5767/5761 devices. * * During client attach, set_type is called by adapter's attach_inform callback. * set_type must then be completed by tuner_probe.
*/ staticint tuner_probe(struct i2c_client *client)
{ struct tuner *t; struct tuner *radio; struct tuner *tv; #ifdef CONFIG_MEDIA_CONTROLLER int ret; #endif
t = kzalloc(sizeof(struct tuner), GFP_KERNEL); if (NULL == t) return -ENOMEM;
v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
t->i2c = client;
t->name = "(tuner unset)";
t->type = UNSET;
t->audmode = V4L2_TUNER_MODE_STEREO;
t->standby = true;
t->radio_freq = 87.5 * 16000; /* Initial freq range */
t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
/* autodetection code based on the i2c addr */ if (!no_autodetect) { switch (client->addr) { case 0x10: if (tuner_symbol_probe(tea5761_autodetection,
t->i2c->adapter,
t->i2c->addr) >= 0) {
t->type = TUNER_TEA5761;
t->mode_mask = T_RADIO;
tuner_lookup(t->i2c->adapter, &radio, &tv); if (tv)
tv->mode_mask &= ~T_RADIO;
goto register_client;
}
kfree(t); return -ENODEV; case 0x42: case 0x43: case 0x4a: case 0x4b: /* If chip is not tda8290, don't register.
since it can be tda9887*/ if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
t->i2c->addr) >= 0) {
dprintk("tda829x detected\n");
} else { /* Default is being tda9887 */
t->type = TUNER_TDA9887;
t->mode_mask = T_RADIO | T_ANALOG_TV; goto register_client;
} break; case 0x60: if (tuner_symbol_probe(tea5767_autodetection,
t->i2c->adapter, t->i2c->addr)
>= 0) {
t->type = TUNER_TEA5767;
t->mode_mask = T_RADIO; /* Sets freq to FM range */
tuner_lookup(t->i2c->adapter, &radio, &tv); if (tv)
tv->mode_mask &= ~T_RADIO;
goto register_client;
} break;
}
}
/* Initializes only the first TV tuner on this adapter. Why only the first? Because there are some devices (notably the ones with TI tuners) that have more than one i2c address for the *same* device. Experience shows that, except for just one case, the first address is the right one. The exception is a Russian tuner (ACORP_Y878F). So, the desired behavior is just to enable the
first found TV tuner. */
tuner_lookup(t->i2c->adapter, &radio, &tv); if (tv == NULL) {
t->mode_mask = T_ANALOG_TV; if (radio == NULL)
t->mode_mask |= T_RADIO;
dprintk("Setting mode_mask to 0x%02x\n", t->mode_mask);
}
/* Should be just before return */
register_client: #ifdefined(CONFIG_MEDIA_CONTROLLER)
t->sd.entity.name = t->name; /* * Handle the special case where the tuner has actually * two stages: the PLL to tune into a frequency and the * IF-PLL demodulator (tda988x).
*/ if (t->type == TUNER_TDA9887) {
t->pad[IF_VID_DEC_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
t->pad[IF_VID_DEC_PAD_IF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
t->pad[IF_VID_DEC_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
t->pad[IF_VID_DEC_PAD_OUT].sig_type = PAD_SIGNAL_ANALOG;
ret = media_entity_pads_init(&t->sd.entity,
IF_VID_DEC_PAD_NUM_PADS,
&t->pad[0]);
t->sd.entity.function = MEDIA_ENT_F_IF_VID_DECODER;
} else {
t->pad[TUNER_PAD_RF_INPUT].flags = MEDIA_PAD_FL_SINK;
t->pad[TUNER_PAD_RF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
t->pad[TUNER_PAD_OUTPUT].flags = MEDIA_PAD_FL_SOURCE;
t->pad[TUNER_PAD_OUTPUT].sig_type = PAD_SIGNAL_ANALOG;
t->pad[TUNER_PAD_AUD_OUT].flags = MEDIA_PAD_FL_SOURCE;
t->pad[TUNER_PAD_AUD_OUT].sig_type = PAD_SIGNAL_AUDIO;
ret = media_entity_pads_init(&t->sd.entity, TUNER_NUM_PADS,
&t->pad[0]);
t->sd.entity.function = MEDIA_ENT_F_TUNER;
}
if (ret < 0) {
pr_err("failed to initialize media entity!\n");
kfree(t); return ret;
} #endif /* Sets a default mode */ if (t->mode_mask & T_ANALOG_TV)
t->mode = V4L2_TUNER_ANALOG_TV; else
t->mode = V4L2_TUNER_RADIO;
set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
list_add_tail(&t->list, &tuner_list);
/* * Functions to switch between Radio and TV * * A few cards have a separate I2C tuner for radio. Those routines * take care of switching between TV/Radio mode, filtering only the * commands that apply to the Radio or TV tuner.
*/
/** * check_mode - Verify if tuner supports the requested mode * @t: a pointer to the module's internal struct_tuner * @mode: mode of the tuner, as defined by &enum v4l2_tuner_type. * * This function checks if the tuner is capable of tuning analog TV, * digital TV or radio, depending on what the caller wants. If the * tuner can't support that mode, it returns -EINVAL. Otherwise, it * returns 0. * This function is needed for boards that have a separate tuner for * radio (like devices with tea5767). * * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to * select a TV frequency. So, t_mode = T_ANALOG_TV could actually * be used to represent a Digital TV too.
*/ staticinlineint check_mode(struct tuner *t, enum v4l2_tuner_type mode)
{ int t_mode; if (mode == V4L2_TUNER_RADIO)
t_mode = T_RADIO; else
t_mode = T_ANALOG_TV;
if ((t_mode & t->mode_mask) == 0) return -EINVAL;
return 0;
}
/** * set_mode - Switch tuner to other mode. * @t: a pointer to the module's internal struct_tuner * @mode: enum v4l2_type (radio or TV) * * If tuner doesn't support the needed mode (radio or TV), prints a * debug message and returns -EINVAL, changing its state to standby. * Otherwise, changes the mode and returns 0.
*/ staticint set_mode(struct tuner *t, enum v4l2_tuner_type mode)
{ struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
if (mode != t->mode) { if (check_mode(t, mode) == -EINVAL) {
dprintk("Tuner doesn't support mode %d. Putting tuner to sleep\n",
mode);
t->standby = true; if (analog_ops->standby)
analog_ops->standby(&t->fe); return -EINVAL;
}
t->mode = mode;
dprintk("Changing to mode %d\n", mode);
} return 0;
}
/** * set_freq - Set the tuner to the desired frequency. * @t: a pointer to the module's internal struct_tuner * @freq: frequency to set (0 means to use the current frequency)
*/ staticvoid set_freq(struct tuner *t, unsignedint freq)
{ struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
if (t->mode == V4L2_TUNER_RADIO) { if (!freq)
freq = t->radio_freq;
set_radio_freq(client, freq);
} else { if (!freq)
freq = t->tv_freq;
set_tv_freq(client, freq);
}
}
/* * Functions that are specific for TV mode
*/
/** * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz * * @c: i2c_client descriptor * @freq: frequency
*/ staticvoid set_tv_freq(struct i2c_client *c, unsignedint freq)
{ struct tuner *t = to_tuner(i2c_get_clientdata(c)); struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
if (t->type == UNSET) {
pr_warn("tuner type not set\n"); return;
} if (NULL == analog_ops->set_params) {
pr_warn("Tuner has no way to set tv freq\n"); return;
} if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
dprintk("TV freq (%d.%02d) out of range (%d-%d)\n",
freq / 16, freq % 16 * 100 / 16, tv_range[0],
tv_range[1]); /* V4L2 spec: if the freq is not possible then the closest
possible value should be selected */ if (freq < tv_range[0] * 16)
freq = tv_range[0] * 16; else
freq = tv_range[1] * 16;
}
params.frequency = freq;
dprintk("tv freq set to %d.%02d\n",
freq / 16, freq % 16 * 100 / 16);
t->tv_freq = freq;
t->standby = false;
analog_ops->set_params(&t->fe, ¶ms);
}
/** * tuner_fixup_std - force a given video standard variant * * @t: tuner internal struct * @std: TV standard * * A few devices or drivers have problem to detect some standard variations. * On other operational systems, the drivers generally have a per-country * code, and some logic to apply per-country hacks. V4L2 API doesn't provide * such hacks. Instead, it relies on a proper video standard selection from * the userspace application. However, as some apps are buggy, not allowing * to distinguish all video standard variations, a modprobe parameter can * be used to force a video standard match.
*/ static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
{ if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) { switch (pal[0]) { case'6': return V4L2_STD_PAL_60; case'b': case'B': case'g': case'G': return V4L2_STD_PAL_BG; case'i': case'I': return V4L2_STD_PAL_I; case'd': case'D': case'k': case'K': return V4L2_STD_PAL_DK; case'M': case'm': return V4L2_STD_PAL_M; case'N': case'n': if (pal[1] == 'c' || pal[1] == 'C') return V4L2_STD_PAL_Nc; return V4L2_STD_PAL_N; default:
pr_warn("pal= argument not recognised\n"); break;
}
} if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) { switch (secam[0]) { case'b': case'B': case'g': case'G': case'h': case'H': return V4L2_STD_SECAM_B |
V4L2_STD_SECAM_G |
V4L2_STD_SECAM_H; case'd': case'D': case'k': case'K': return V4L2_STD_SECAM_DK; case'l': case'L': if ((secam[1] == 'C') || (secam[1] == 'c')) return V4L2_STD_SECAM_LC; return V4L2_STD_SECAM_L; default:
pr_warn("secam= argument not recognised\n"); break;
}
}
if (t->type == UNSET) {
pr_warn("tuner type not set\n"); return;
} if (NULL == analog_ops->set_params) {
pr_warn("tuner has no way to set radio frequency\n"); return;
} if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
dprintk("radio freq (%d.%02d) out of range (%d-%d)\n",
freq / 16000, freq % 16000 * 100 / 16000,
radio_range[0], radio_range[1]); /* V4L2 spec: if the freq is not possible then the closest
possible value should be selected */ if (freq < radio_range[0] * 16000)
freq = radio_range[0] * 16000; else
freq = radio_range[1] * 16000;
}
params.frequency = freq;
dprintk("radio freq set to %d.%02d\n",
freq / 16000, freq % 16000 * 100 / 16000);
t->radio_freq = freq;
t->standby = false;
analog_ops->set_params(&t->fe, ¶ms); /* * The tuner driver might decide to change the audmode if it only * supports stereo, so update t->audmode.
*/
t->audmode = params.audmode;
}
/* * Debug function for reporting tuner status to userspace
*/
/** * tuner_status - Dumps the current tuner status at dmesg * @fe: pointer to struct dvb_frontend * * This callback is used only for driver debug purposes, answering to * VIDIOC_LOG_STATUS. No changes should happen on this call.
*/ staticvoid tuner_status(struct dvb_frontend *fe)
{ struct tuner *t = fe->analog_demod_priv; unsignedlong freq, freq_fraction; struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; struct analog_demod_ops *analog_ops = &fe->ops.analog_ops; constchar *p;
switch (t->mode) { case V4L2_TUNER_RADIO:
p = "radio"; break; case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
p = "digital TV"; break; case V4L2_TUNER_ANALOG_TV: default:
p = "analog TV"; break;
} if (t->mode == V4L2_TUNER_RADIO) {
freq = t->radio_freq / 16000;
freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
} else {
freq = t->tv_freq / 16;
freq_fraction = (t->tv_freq % 16) * 100 / 16;
}
pr_info("Tuner mode: %s%s\n", p,
t->standby ? " on standby mode" : "");
pr_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
pr_info("Standard: 0x%08lx\n", (unsignedlong)t->std); if (t->mode != V4L2_TUNER_RADIO) return; if (fe_tuner_ops->get_status) {
u32 tuner_status = 0;
fe_tuner_ops->get_status(&t->fe, &tuner_status); if (tuner_status & TUNER_STATUS_LOCKED)
pr_info("Tuner is locked.\n"); if (tuner_status & TUNER_STATUS_STEREO)
pr_info("Stereo: yes\n");
} if (analog_ops->has_signal) {
u16 signal;
if (!analog_ops->has_signal(fe, &signal))
pr_info("Signal strength: %hu\n", signal);
}
}
/* * Function to splicitly change mode to radio. Probably not needed anymore
*/
if (set_mode(t, f->type) == 0)
set_freq(t, f->frequency); return 0;
}
/** * tuner_g_frequency - Get the tuned frequency for the tuner * @sd: pointer to struct v4l2_subdev * @f: pointer to struct v4l2_frequency * * At return, the structure f will be filled with tuner frequency * if the tuner matches the f->type. * Note: f->type should be initialized before calling it. * This is done by either video_ioctl2 or by the bridge driver.
*/ staticint tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
{ struct tuner *t = to_tuner(sd); struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
if (check_mode(t, f->type) == -EINVAL) return 0; if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
u32 abs_freq;
/** * tuner_g_tuner - Fill in tuner information * @sd: pointer to struct v4l2_subdev * @vt: pointer to struct v4l2_tuner * * At return, the structure vt will be filled with tuner information * if the tuner matches vt->type. * Note: vt->type should be initialized before calling it. * This is done by either video_ioctl2 or by the bridge driver.
*/ staticint tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
{ struct tuner *t = to_tuner(sd); struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
if (check_mode(t, vt->type) == -EINVAL) return 0; if (vt->type == t->mode && analog_ops->get_afc)
analog_ops->get_afc(&t->fe, &vt->afc); if (vt->type == t->mode && analog_ops->has_signal) {
u16 signal = (u16)vt->signal;
/** * tuner_s_tuner - Set the tuner's audio mode * @sd: pointer to struct v4l2_subdev * @vt: pointer to struct v4l2_tuner * * Sets the audio mode if the tuner matches vt->type. * Note: vt->type should be initialized before calling it. * This is done by either video_ioctl2 or by the bridge driver.
*/ staticint tuner_s_tuner(struct v4l2_subdev *sd, conststruct v4l2_tuner *vt)
{ struct tuner *t = to_tuner(sd);
if (set_mode(t, vt->type)) return 0;
if (t->mode == V4L2_TUNER_RADIO) {
t->audmode = vt->audmode; /* * For radio audmode can only be mono or stereo. Map any * other values to stereo. The actual tuner driver that is * called in set_radio_freq can decide to limit the audmode to * mono if only mono is supported.
*/ if (t->audmode != V4L2_TUNER_MODE_MONO &&
t->audmode != V4L2_TUNER_MODE_STEREO)
t->audmode = V4L2_TUNER_MODE_STEREO;
}
set_freq(t, 0);
/* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have to handle it here.
There must be a better way of doing this... */ switch (cmd) { case TUNER_SET_CONFIG: return tuner_s_config(sd, arg);
} return -ENOIOCTLCMD;
}
MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
MODULE_LICENSE("GPL");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.