2 * For Philips TEA5761 FM Chip
3 * I2C address is allways 0x20 (0x10 at 7-bit mode).
5 * Copyright (c) 2005-2007 Mauro Carvalho Chehab (mchehab@infradead.org)
6 * This code is placed under the terms of the GNUv2 General Public License
10 #include <linux/i2c.h>
11 #include <linux/delay.h>
12 #include <linux/videodev.h>
13 #include <media/tuner.h>
14 #include "tuner-i2c.h"
18 module_param(debug, int, 0644);
19 MODULE_PARM_DESC(debug, "enable verbose debug messages");
22 struct tuner_i2c_props i2c_props;
27 /*****************************************************************************/
29 /***************************
30 * TEA5761HN I2C registers *
31 ***************************/
33 /* INTREG - Read: bytes 0 and 1 / Write: byte 0 */
35 /* first byte for reading */
36 #define TEA5761_INTREG_IFFLAG 0x10
37 #define TEA5761_INTREG_LEVFLAG 0x8
38 #define TEA5761_INTREG_FRRFLAG 0x2
39 #define TEA5761_INTREG_BLFLAG 0x1
41 /* second byte for reading / byte for writing */
42 #define TEA5761_INTREG_IFMSK 0x10
43 #define TEA5761_INTREG_LEVMSK 0x8
44 #define TEA5761_INTREG_FRMSK 0x2
45 #define TEA5761_INTREG_BLMSK 0x1
47 /* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */
50 #define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */
51 #define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */
53 /* Bits 0-5 for divider MSB */
56 /* Bits 0-7 for divider LSB */
58 /* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */
62 #define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */
63 #define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */
64 #define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */
65 #define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */
66 #define TEA5761_TNCTRL_AFM 0x04
67 #define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */
68 #define TEA5761_TNCTRL_SNC 0x01
72 #define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */
73 #define TEA5761_TNCTRL_SSL_1 0x40
74 #define TEA5761_TNCTRL_SSL_0 0x20
75 #define TEA5761_TNCTRL_HLSI 0x10
76 #define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */
77 #define TEA5761_TNCTRL_SWP 0x04
78 #define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */
79 #define TEA5761_TNCTRL_AHLSI 0x01
81 /* FRQCHECK - Read: bytes 6 and 7 */
84 /* Bits 0-5 for divider MSB */
87 /* Bits 0-7 for divider LSB */
89 /* TUNCHECK - Read: bytes 8 and 9 */
92 #define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */
93 #define TEA5761_TUNCHECK_TUNTO 0x01
96 #define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */
97 #define TEA5761_TUNCHECK_LD 0x08
98 #define TEA5761_TUNCHECK_STEREO 0x04
100 /* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */
102 /* All zero = no test mode */
104 #define TEA5761_TESTREG_TRIGFR 0x08
106 /* MANID - Read: bytes 12 and 13 */
108 /* First byte - should be 0x10 */
109 #define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */
110 #define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */
112 /* Second byte - Should be 0x2b */
114 #define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */
115 #define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */
117 /* Chip ID - Read: bytes 14 and 15 */
119 /* First byte - should be 0x57 */
121 /* Second byte - should be 0x61 */
123 /*****************************************************************************/
125 #define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */
126 static void tea5761_status_dump(unsigned char *buffer)
128 unsigned int div, frq;
130 div = ((buffer[2] & 0x3f) << 8) | buffer[3];
132 frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */
134 printk(KERN_INFO "tea5761: Frequency %d.%03d KHz (divider = 0x%04x)\n",
135 frq / 1000, frq % 1000, div);
138 /* Freq should be specifyed at 62.5 Hz */
139 static int set_radio_freq(struct dvb_frontend *fe,
140 struct analog_parameters *params)
142 struct tea5761_priv *priv = fe->tuner_priv;
143 unsigned int frq = params->frequency;
144 unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 };
148 tuner_dbg("radio freq counter %d\n", frq);
150 if (params->mode == T_STANDBY) {
151 tuner_dbg("TEA5761 set to standby mode\n");
152 buffer[4] |= TEA5761_TNCTRL_MU;
154 buffer[3] |= TEA5761_TNCTRL_PUPD_0;
157 buffer[5] = TEA5761_TESTREG_TRIGFR;
159 if (params->audmode == V4L2_TUNER_MODE_MONO) {
160 tuner_dbg("TEA5761 set to mono\n");
161 buffer[4] |= TEA5761_TNCTRL_MST;
163 tuner_dbg("TEA5761 set to stereo\n");
166 div = (frq * 125 / 2 - 225000) >> 13;
167 buffer[1] = (div >> 8) & 0x3f;
168 buffer[2] = div & 0xff;
171 tea5761_status_dump(buffer);
173 if (7 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 7)))
174 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
176 priv->frequency = frq * 125 / 2;
181 static int tea5761_init(struct dvb_frontend *fe)
183 struct tea5761_priv *priv = fe->tuner_priv;
184 unsigned char buffer[] = {0, 0, 0, 0, 0, 0, 0 };
187 tuner_dbg("Power up radio\n");
189 buffer[3] = TEA5761_TNCTRL_PUPD_0;
192 tea5761_status_dump(buffer);
194 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, ARRAY_SIZE(buffer));
195 if (rc != ARRAY_SIZE(buffer))
196 tuner_warn("i2c i/o error: rc == %d (should be %d)\n", rc,
202 static int tea5761_sleep(struct dvb_frontend *fe)
204 struct tea5761_priv *priv = fe->tuner_priv;
205 unsigned char buffer[] = {0, 0, 0, 0, 0, 0, 0 };
208 tuner_dbg("Power down radio\n");
210 buffer[3] &= ~TEA5761_TNCTRL_PUPD_0;
213 tea5761_status_dump(buffer);
215 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, ARRAY_SIZE(buffer));
216 if (rc != ARRAY_SIZE(buffer))
217 tuner_warn("i2c i/o error: rc == %d (should be %d)\n", rc,
223 static int tea5761_read_status(struct dvb_frontend *fe, char *buffer)
225 struct tea5761_priv *priv = fe->tuner_priv;
228 memset(buffer, 0, 16);
229 if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16))) {
230 tuner_warn("i2c i/o error: rc == %d (should be 16)\n", rc);
237 static inline int tea5761_signal(struct dvb_frontend *fe, const char *buffer)
239 struct tea5761_priv *priv = fe->tuner_priv;
241 int signal = ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4));
243 tuner_dbg("Signal strength: %d\n", signal);
248 static inline int tea5761_stereo(struct dvb_frontend *fe, const char *buffer)
250 struct tea5761_priv *priv = fe->tuner_priv;
252 int stereo = buffer[9] & TEA5761_TUNCHECK_STEREO;
254 tuner_dbg("Radio ST GET = %02x\n", stereo);
256 return (stereo ? V4L2_TUNER_SUB_STEREO : 0);
259 static int tea5761_get_status(struct dvb_frontend *fe, u32 *status)
261 unsigned char buffer[16];
265 if (0 == tea5761_read_status(fe, buffer)) {
266 if (tea5761_signal(fe, buffer))
267 *status = TUNER_STATUS_LOCKED;
268 if (tea5761_stereo(fe, buffer))
269 *status |= TUNER_STATUS_STEREO;
275 static int tea5761_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
277 unsigned char buffer[16];
281 if (0 == tea5761_read_status(fe, buffer))
282 *strength = tea5761_signal(fe, buffer);
287 int tea5761_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)
289 unsigned char buffer[16];
291 struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr };
293 if (16 != (rc = tuner_i2c_xfer_recv(&i2c, buffer, 16))) {
294 printk(KERN_WARNING "it is not a TEA5761. Received %i chars.\n", rc);
298 if ((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061)) {
299 printk(KERN_WARNING "Manufacturer ID= 0x%02x, Chip ID = %02x%02x."
300 " It is not a TEA5761\n",
301 buffer[13], buffer[14], buffer[15]);
304 printk(KERN_WARNING "tea5761: TEA%02x%02x detected. "
305 "Manufacturer ID= 0x%02x\n",
306 buffer[14], buffer[15], buffer[13]);
311 static int tea5761_release(struct dvb_frontend *fe)
313 kfree(fe->tuner_priv);
314 fe->tuner_priv = NULL;
319 static int tea5761_get_frequency(struct dvb_frontend *fe, u32 *frequency)
321 struct tea5761_priv *priv = fe->tuner_priv;
322 *frequency = priv->frequency;
326 static struct dvb_tuner_ops tea5761_tuner_ops = {
328 .name = "tea5761", // Philips TEA5761HN FM Radio
330 .set_analog_params = set_radio_freq,
331 .release = tea5761_release,
332 .get_frequency = tea5761_get_frequency,
333 .get_status = tea5761_get_status,
334 .get_rf_strength = tea5761_get_rf_strength,
335 .init = tea5761_init,
336 .sleep = tea5761_sleep,
339 struct dvb_frontend *tea5761_attach(struct dvb_frontend *fe,
340 struct i2c_adapter* i2c_adap,
343 struct tea5761_priv *priv = NULL;
345 if (tea5761_autodetection(i2c_adap, i2c_addr) != 0)
348 priv = kzalloc(sizeof(struct tea5761_priv), GFP_KERNEL);
351 fe->tuner_priv = priv;
353 priv->i2c_props.addr = i2c_addr;
354 priv->i2c_props.adap = i2c_adap;
355 priv->i2c_props.name = "tea5761";
357 memcpy(&fe->ops.tuner_ops, &tea5761_tuner_ops,
358 sizeof(struct dvb_tuner_ops));
360 tuner_info("type set to %s\n", "Philips TEA5761HN FM Radio");
366 EXPORT_SYMBOL_GPL(tea5761_attach);
367 EXPORT_SYMBOL_GPL(tea5761_autodetection);
369 MODULE_DESCRIPTION("Philips TEA5761 FM tuner driver");
370 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
371 MODULE_LICENSE("GPL");