Path: blob/master/drivers/media/common/tuners/tea5767.c
15112 views
/*1* For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview2* I2C address is allways 0xC0.3*4*5* Copyright (c) 2005 Mauro Carvalho Chehab ([email protected])6* This code is placed under the terms of the GNU General Public License7*8* tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa9* from their contributions on DScaler.10*/1112#include <linux/i2c.h>13#include <linux/slab.h>14#include <linux/delay.h>15#include <linux/videodev2.h>16#include "tuner-i2c.h"17#include "tea5767.h"1819static int debug;20module_param(debug, int, 0644);21MODULE_PARM_DESC(debug, "enable verbose debug messages");2223/*****************************************************************************/2425struct tea5767_priv {26struct tuner_i2c_props i2c_props;27u32 frequency;28struct tea5767_ctrl ctrl;29};3031/*****************************************************************************/3233/******************************34* Write mode register values *35******************************/3637/* First register */38#define TEA5767_MUTE 0x80 /* Mutes output */39#define TEA5767_SEARCH 0x40 /* Activates station search */40/* Bits 0-5 for divider MSB */4142/* Second register */43/* Bits 0-7 for divider LSB */4445/* Third register */4647/* Station search from botton to up */48#define TEA5767_SEARCH_UP 0x804950/* Searches with ADC output = 10 */51#define TEA5767_SRCH_HIGH_LVL 0x605253/* Searches with ADC output = 10 */54#define TEA5767_SRCH_MID_LVL 0x405556/* Searches with ADC output = 5 */57#define TEA5767_SRCH_LOW_LVL 0x205859/* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */60#define TEA5767_HIGH_LO_INJECT 0x106162/* Disable stereo */63#define TEA5767_MONO 0x086465/* Disable right channel and turns to mono */66#define TEA5767_MUTE_RIGHT 0x046768/* Disable left channel and turns to mono */69#define TEA5767_MUTE_LEFT 0x027071#define TEA5767_PORT1_HIGH 0x017273/* Fourth register */74#define TEA5767_PORT2_HIGH 0x8075/* Chips stops working. Only I2C bus remains on */76#define TEA5767_STDBY 0x407778/* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */79#define TEA5767_JAPAN_BAND 0x208081/* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */82#define TEA5767_XTAL_32768 0x108384/* Cuts weak signals */85#define TEA5767_SOFT_MUTE 0x088687/* Activates high cut control */88#define TEA5767_HIGH_CUT_CTRL 0x048990/* Activates stereo noise control */91#define TEA5767_ST_NOISE_CTL 0x029293/* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */94#define TEA5767_SRCH_IND 0x019596/* Fifth register */9798/* By activating, it will use Xtal at 13 MHz as reference for divider */99#define TEA5767_PLLREF_ENABLE 0x80100101/* By activating, deemphasis=50, or else, deemphasis of 50us */102#define TEA5767_DEEMPH_75 0X40103104/*****************************105* Read mode register values *106*****************************/107108/* First register */109#define TEA5767_READY_FLAG_MASK 0x80110#define TEA5767_BAND_LIMIT_MASK 0X40111/* Bits 0-5 for divider MSB after search or preset */112113/* Second register */114/* Bits 0-7 for divider LSB after search or preset */115116/* Third register */117#define TEA5767_STEREO_MASK 0x80118#define TEA5767_IF_CNTR_MASK 0x7f119120/* Fourth register */121#define TEA5767_ADC_LEVEL_MASK 0xf0122123/* should be 0 */124#define TEA5767_CHIP_ID_MASK 0x0f125126/* Fifth register */127/* Reserved for future extensions */128#define TEA5767_RESERVED_MASK 0xff129130/*****************************************************************************/131132static void tea5767_status_dump(struct tea5767_priv *priv,133unsigned char *buffer)134{135unsigned int div, frq;136137if (TEA5767_READY_FLAG_MASK & buffer[0])138tuner_info("Ready Flag ON\n");139else140tuner_info("Ready Flag OFF\n");141142if (TEA5767_BAND_LIMIT_MASK & buffer[0])143tuner_info("Tuner at band limit\n");144else145tuner_info("Tuner not at band limit\n");146147div = ((buffer[0] & 0x3f) << 8) | buffer[1];148149switch (priv->ctrl.xtal_freq) {150case TEA5767_HIGH_LO_13MHz:151frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */152break;153case TEA5767_LOW_LO_13MHz:154frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */155break;156case TEA5767_LOW_LO_32768:157frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */158break;159case TEA5767_HIGH_LO_32768:160default:161frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */162break;163}164buffer[0] = (div >> 8) & 0x3f;165buffer[1] = div & 0xff;166167tuner_info("Frequency %d.%03d KHz (divider = 0x%04x)\n",168frq / 1000, frq % 1000, div);169170if (TEA5767_STEREO_MASK & buffer[2])171tuner_info("Stereo\n");172else173tuner_info("Mono\n");174175tuner_info("IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);176177tuner_info("ADC Level = %d\n",178(buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);179180tuner_info("Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));181182tuner_info("Reserved = 0x%02x\n",183(buffer[4] & TEA5767_RESERVED_MASK));184}185186/* Freq should be specifyed at 62.5 Hz */187static int set_radio_freq(struct dvb_frontend *fe,188struct analog_parameters *params)189{190struct tea5767_priv *priv = fe->tuner_priv;191unsigned int frq = params->frequency;192unsigned char buffer[5];193unsigned div;194int rc;195196tuner_dbg("radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);197198buffer[2] = 0;199200if (priv->ctrl.port1)201buffer[2] |= TEA5767_PORT1_HIGH;202203if (params->audmode == V4L2_TUNER_MODE_MONO) {204tuner_dbg("TEA5767 set to mono\n");205buffer[2] |= TEA5767_MONO;206} else {207tuner_dbg("TEA5767 set to stereo\n");208}209210211buffer[3] = 0;212213if (priv->ctrl.port2)214buffer[3] |= TEA5767_PORT2_HIGH;215216if (priv->ctrl.high_cut)217buffer[3] |= TEA5767_HIGH_CUT_CTRL;218219if (priv->ctrl.st_noise)220buffer[3] |= TEA5767_ST_NOISE_CTL;221222if (priv->ctrl.soft_mute)223buffer[3] |= TEA5767_SOFT_MUTE;224225if (priv->ctrl.japan_band)226buffer[3] |= TEA5767_JAPAN_BAND;227228buffer[4] = 0;229230if (priv->ctrl.deemph_75)231buffer[4] |= TEA5767_DEEMPH_75;232233if (priv->ctrl.pllref)234buffer[4] |= TEA5767_PLLREF_ENABLE;235236237/* Rounds freq to next decimal value - for 62.5 KHz step */238/* frq = 20*(frq/16)+radio_frq[frq%16]; */239240switch (priv->ctrl.xtal_freq) {241case TEA5767_HIGH_LO_13MHz:242tuner_dbg("radio HIGH LO inject xtal @ 13 MHz\n");243buffer[2] |= TEA5767_HIGH_LO_INJECT;244div = (frq * (4000 / 16) + 700000 + 225000 + 25000) / 50000;245break;246case TEA5767_LOW_LO_13MHz:247tuner_dbg("radio LOW LO inject xtal @ 13 MHz\n");248249div = (frq * (4000 / 16) - 700000 - 225000 + 25000) / 50000;250break;251case TEA5767_LOW_LO_32768:252tuner_dbg("radio LOW LO inject xtal @ 32,768 MHz\n");253buffer[3] |= TEA5767_XTAL_32768;254/* const 700=4000*175 Khz - to adjust freq to right value */255div = ((frq * (4000 / 16) - 700000 - 225000) + 16384) >> 15;256break;257case TEA5767_HIGH_LO_32768:258default:259tuner_dbg("radio HIGH LO inject xtal @ 32,768 MHz\n");260261buffer[2] |= TEA5767_HIGH_LO_INJECT;262buffer[3] |= TEA5767_XTAL_32768;263div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;264break;265}266buffer[0] = (div >> 8) & 0x3f;267buffer[1] = div & 0xff;268269if (5 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 5)))270tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);271272if (debug) {273if (5 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 5)))274tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);275else276tea5767_status_dump(priv, buffer);277}278279priv->frequency = frq * 125 / 2;280281return 0;282}283284static int tea5767_read_status(struct dvb_frontend *fe, char *buffer)285{286struct tea5767_priv *priv = fe->tuner_priv;287int rc;288289memset(buffer, 0, 5);290if (5 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 5))) {291tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);292return -EREMOTEIO;293}294295return 0;296}297298static inline int tea5767_signal(struct dvb_frontend *fe, const char *buffer)299{300struct tea5767_priv *priv = fe->tuner_priv;301302int signal = ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << 8);303304tuner_dbg("Signal strength: %d\n", signal);305306return signal;307}308309static inline int tea5767_stereo(struct dvb_frontend *fe, const char *buffer)310{311struct tea5767_priv *priv = fe->tuner_priv;312313int stereo = buffer[2] & TEA5767_STEREO_MASK;314315tuner_dbg("Radio ST GET = %02x\n", stereo);316317return (stereo ? V4L2_TUNER_SUB_STEREO : 0);318}319320static int tea5767_get_status(struct dvb_frontend *fe, u32 *status)321{322unsigned char buffer[5];323324*status = 0;325326if (0 == tea5767_read_status(fe, buffer)) {327if (tea5767_signal(fe, buffer))328*status = TUNER_STATUS_LOCKED;329if (tea5767_stereo(fe, buffer))330*status |= TUNER_STATUS_STEREO;331}332333return 0;334}335336static int tea5767_get_rf_strength(struct dvb_frontend *fe, u16 *strength)337{338unsigned char buffer[5];339340*strength = 0;341342if (0 == tea5767_read_status(fe, buffer))343*strength = tea5767_signal(fe, buffer);344345return 0;346}347348static int tea5767_standby(struct dvb_frontend *fe)349{350unsigned char buffer[5];351struct tea5767_priv *priv = fe->tuner_priv;352unsigned div, rc;353354div = (87500 * 4 + 700 + 225 + 25) / 50; /* Set frequency to 87.5 MHz */355buffer[0] = (div >> 8) & 0x3f;356buffer[1] = div & 0xff;357buffer[2] = TEA5767_PORT1_HIGH;358buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |359TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND | TEA5767_STDBY;360buffer[4] = 0;361362if (5 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 5)))363tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);364365return 0;366}367368int tea5767_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)369{370struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr };371unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };372int rc;373374if ((rc = tuner_i2c_xfer_recv(&i2c, buffer, 7))< 5) {375printk(KERN_WARNING "It is not a TEA5767. Received %i bytes.\n", rc);376return -EINVAL;377}378379/* If all bytes are the same then it's a TV tuner and not a tea5767 */380if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&381buffer[0] == buffer[3] && buffer[0] == buffer[4]) {382printk(KERN_WARNING "All bytes are equal. It is not a TEA5767\n");383return -EINVAL;384}385386/* Status bytes:387* Byte 4: bit 3:1 : CI (Chip Identification) == 0388* bit 0 : internally set to 0389* Byte 5: bit 7:0 : == 0390*/391if (((buffer[3] & 0x0f) != 0x00) || (buffer[4] != 0x00)) {392printk(KERN_WARNING "Chip ID is not zero. It is not a TEA5767\n");393return -EINVAL;394}395396397return 0;398}399400static int tea5767_release(struct dvb_frontend *fe)401{402kfree(fe->tuner_priv);403fe->tuner_priv = NULL;404405return 0;406}407408static int tea5767_get_frequency(struct dvb_frontend *fe, u32 *frequency)409{410struct tea5767_priv *priv = fe->tuner_priv;411*frequency = priv->frequency;412413return 0;414}415416static int tea5767_set_config (struct dvb_frontend *fe, void *priv_cfg)417{418struct tea5767_priv *priv = fe->tuner_priv;419420memcpy(&priv->ctrl, priv_cfg, sizeof(priv->ctrl));421422return 0;423}424425static struct dvb_tuner_ops tea5767_tuner_ops = {426.info = {427.name = "tea5767", // Philips TEA5767HN FM Radio428},429430.set_analog_params = set_radio_freq,431.set_config = tea5767_set_config,432.sleep = tea5767_standby,433.release = tea5767_release,434.get_frequency = tea5767_get_frequency,435.get_status = tea5767_get_status,436.get_rf_strength = tea5767_get_rf_strength,437};438439struct dvb_frontend *tea5767_attach(struct dvb_frontend *fe,440struct i2c_adapter* i2c_adap,441u8 i2c_addr)442{443struct tea5767_priv *priv = NULL;444445priv = kzalloc(sizeof(struct tea5767_priv), GFP_KERNEL);446if (priv == NULL)447return NULL;448fe->tuner_priv = priv;449450priv->i2c_props.addr = i2c_addr;451priv->i2c_props.adap = i2c_adap;452priv->i2c_props.name = "tea5767";453454priv->ctrl.xtal_freq = TEA5767_HIGH_LO_32768;455priv->ctrl.port1 = 1;456priv->ctrl.port2 = 1;457priv->ctrl.high_cut = 1;458priv->ctrl.st_noise = 1;459priv->ctrl.japan_band = 1;460461memcpy(&fe->ops.tuner_ops, &tea5767_tuner_ops,462sizeof(struct dvb_tuner_ops));463464tuner_info("type set to %s\n", "Philips TEA5767HN FM Radio");465466return fe;467}468469EXPORT_SYMBOL_GPL(tea5767_attach);470EXPORT_SYMBOL_GPL(tea5767_autodetection);471472MODULE_DESCRIPTION("Philips TEA5767 FM tuner driver");473MODULE_AUTHOR("Mauro Carvalho Chehab <[email protected]>");474MODULE_LICENSE("GPL");475476477