Path: blob/master/drivers/media/common/tuners/tea5761.c
15112 views
/*1* For Philips TEA5761 FM Chip2* I2C address is allways 0x20 (0x10 at 7-bit mode).3*4* Copyright (c) 2005-2007 Mauro Carvalho Chehab ([email protected])5* This code is placed under the terms of the GNUv2 General Public License6*7*/89#include <linux/i2c.h>10#include <linux/slab.h>11#include <linux/delay.h>12#include <linux/videodev2.h>13#include <media/tuner.h>14#include "tuner-i2c.h"15#include "tea5761.h"1617static int debug;18module_param(debug, int, 0644);19MODULE_PARM_DESC(debug, "enable verbose debug messages");2021struct tea5761_priv {22struct tuner_i2c_props i2c_props;2324u32 frequency;25bool standby;26};2728/*****************************************************************************/2930/***************************31* TEA5761HN I2C registers *32***************************/3334/* INTREG - Read: bytes 0 and 1 / Write: byte 0 */3536/* first byte for reading */37#define TEA5761_INTREG_IFFLAG 0x1038#define TEA5761_INTREG_LEVFLAG 0x839#define TEA5761_INTREG_FRRFLAG 0x240#define TEA5761_INTREG_BLFLAG 0x14142/* second byte for reading / byte for writing */43#define TEA5761_INTREG_IFMSK 0x1044#define TEA5761_INTREG_LEVMSK 0x845#define TEA5761_INTREG_FRMSK 0x246#define TEA5761_INTREG_BLMSK 0x14748/* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */4950/* First byte */51#define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */52#define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */5354/* Bits 0-5 for divider MSB */5556/* Second byte */57/* Bits 0-7 for divider LSB */5859/* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */6061/* first byte */6263#define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */64#define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */65#define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */66#define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */67#define TEA5761_TNCTRL_AFM 0x0468#define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */69#define TEA5761_TNCTRL_SNC 0x017071/* second byte */7273#define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */74#define TEA5761_TNCTRL_SSL_1 0x4075#define TEA5761_TNCTRL_SSL_0 0x2076#define TEA5761_TNCTRL_HLSI 0x1077#define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */78#define TEA5761_TNCTRL_SWP 0x0479#define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */80#define TEA5761_TNCTRL_AHLSI 0x018182/* FRQCHECK - Read: bytes 6 and 7 */83/* First byte */8485/* Bits 0-5 for divider MSB */8687/* Second byte */88/* Bits 0-7 for divider LSB */8990/* TUNCHECK - Read: bytes 8 and 9 */9192/* First byte */93#define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */94#define TEA5761_TUNCHECK_TUNTO 0x019596/* Second byte */97#define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */98#define TEA5761_TUNCHECK_LD 0x0899#define TEA5761_TUNCHECK_STEREO 0x04100101/* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */102103/* All zero = no test mode */104105/* MANID - Read: bytes 12 and 13 */106107/* First byte - should be 0x10 */108#define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */109#define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */110111/* Second byte - Should be 0x2b */112113#define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */114#define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */115116/* Chip ID - Read: bytes 14 and 15 */117118/* First byte - should be 0x57 */119120/* Second byte - should be 0x61 */121122/*****************************************************************************/123124#define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */125static void tea5761_status_dump(unsigned char *buffer)126{127unsigned int div, frq;128129div = ((buffer[2] & 0x3f) << 8) | buffer[3];130131frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */132133printk(KERN_INFO "tea5761: Frequency %d.%03d KHz (divider = 0x%04x)\n",134frq / 1000, frq % 1000, div);135}136137/* Freq should be specifyed at 62.5 Hz */138static int __set_radio_freq(struct dvb_frontend *fe,139unsigned int freq,140bool mono)141{142struct tea5761_priv *priv = fe->tuner_priv;143unsigned int frq = freq;144unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 };145unsigned div;146int rc;147148tuner_dbg("radio freq counter %d\n", frq);149150if (priv->standby) {151tuner_dbg("TEA5761 set to standby mode\n");152buffer[5] |= TEA5761_TNCTRL_MU;153} else {154buffer[4] |= TEA5761_TNCTRL_PUPD_0;155}156157158if (mono) {159tuner_dbg("TEA5761 set to mono\n");160buffer[5] |= TEA5761_TNCTRL_MST;161} else {162tuner_dbg("TEA5761 set to stereo\n");163}164165div = (1000 * (frq * 4 / 16 + 700 + 225) ) >> 15;166buffer[1] = (div >> 8) & 0x3f;167buffer[2] = div & 0xff;168169if (debug)170tea5761_status_dump(buffer);171172if (7 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 7)))173tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);174175priv->frequency = frq * 125 / 2;176177return 0;178}179180static int set_radio_freq(struct dvb_frontend *fe,181struct analog_parameters *params)182{183struct tea5761_priv *priv = fe->analog_demod_priv;184185priv->standby = false;186187return __set_radio_freq(fe, params->frequency,188params->audmode == V4L2_TUNER_MODE_MONO);189}190191static int set_radio_sleep(struct dvb_frontend *fe)192{193struct tea5761_priv *priv = fe->analog_demod_priv;194195priv->standby = true;196197return __set_radio_freq(fe, priv->frequency, false);198}199200static int tea5761_read_status(struct dvb_frontend *fe, char *buffer)201{202struct tea5761_priv *priv = fe->tuner_priv;203int rc;204205memset(buffer, 0, 16);206if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16))) {207tuner_warn("i2c i/o error: rc == %d (should be 16)\n", rc);208return -EREMOTEIO;209}210211return 0;212}213214static inline int tea5761_signal(struct dvb_frontend *fe, const char *buffer)215{216struct tea5761_priv *priv = fe->tuner_priv;217218int signal = ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4));219220tuner_dbg("Signal strength: %d\n", signal);221222return signal;223}224225static inline int tea5761_stereo(struct dvb_frontend *fe, const char *buffer)226{227struct tea5761_priv *priv = fe->tuner_priv;228229int stereo = buffer[9] & TEA5761_TUNCHECK_STEREO;230231tuner_dbg("Radio ST GET = %02x\n", stereo);232233return (stereo ? V4L2_TUNER_SUB_STEREO : 0);234}235236static int tea5761_get_status(struct dvb_frontend *fe, u32 *status)237{238unsigned char buffer[16];239240*status = 0;241242if (0 == tea5761_read_status(fe, buffer)) {243if (tea5761_signal(fe, buffer))244*status = TUNER_STATUS_LOCKED;245if (tea5761_stereo(fe, buffer))246*status |= TUNER_STATUS_STEREO;247}248249return 0;250}251252static int tea5761_get_rf_strength(struct dvb_frontend *fe, u16 *strength)253{254unsigned char buffer[16];255256*strength = 0;257258if (0 == tea5761_read_status(fe, buffer))259*strength = tea5761_signal(fe, buffer);260261return 0;262}263264int tea5761_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)265{266unsigned char buffer[16];267int rc;268struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr };269270if (16 != (rc = tuner_i2c_xfer_recv(&i2c, buffer, 16))) {271printk(KERN_WARNING "it is not a TEA5761. Received %i chars.\n", rc);272return -EINVAL;273}274275if ((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061)) {276printk(KERN_WARNING "Manufacturer ID= 0x%02x, Chip ID = %02x%02x."277" It is not a TEA5761\n",278buffer[13], buffer[14], buffer[15]);279return -EINVAL;280}281printk(KERN_WARNING "tea5761: TEA%02x%02x detected. "282"Manufacturer ID= 0x%02x\n",283buffer[14], buffer[15], buffer[13]);284285return 0;286}287288static int tea5761_release(struct dvb_frontend *fe)289{290kfree(fe->tuner_priv);291fe->tuner_priv = NULL;292293return 0;294}295296static int tea5761_get_frequency(struct dvb_frontend *fe, u32 *frequency)297{298struct tea5761_priv *priv = fe->tuner_priv;299*frequency = priv->frequency;300return 0;301}302303static struct dvb_tuner_ops tea5761_tuner_ops = {304.info = {305.name = "tea5761", // Philips TEA5761HN FM Radio306},307.set_analog_params = set_radio_freq,308.sleep = set_radio_sleep,309.release = tea5761_release,310.get_frequency = tea5761_get_frequency,311.get_status = tea5761_get_status,312.get_rf_strength = tea5761_get_rf_strength,313};314315struct dvb_frontend *tea5761_attach(struct dvb_frontend *fe,316struct i2c_adapter* i2c_adap,317u8 i2c_addr)318{319struct tea5761_priv *priv = NULL;320321if (tea5761_autodetection(i2c_adap, i2c_addr) != 0)322return NULL;323324priv = kzalloc(sizeof(struct tea5761_priv), GFP_KERNEL);325if (priv == NULL)326return NULL;327fe->tuner_priv = priv;328329priv->i2c_props.addr = i2c_addr;330priv->i2c_props.adap = i2c_adap;331priv->i2c_props.name = "tea5761";332333memcpy(&fe->ops.tuner_ops, &tea5761_tuner_ops,334sizeof(struct dvb_tuner_ops));335336tuner_info("type set to %s\n", "Philips TEA5761HN FM Radio");337338return fe;339}340341342EXPORT_SYMBOL_GPL(tea5761_attach);343EXPORT_SYMBOL_GPL(tea5761_autodetection);344345MODULE_DESCRIPTION("Philips TEA5761 FM tuner driver");346MODULE_AUTHOR("Mauro Carvalho Chehab <[email protected]>");347MODULE_LICENSE("GPL");348349350