Mauro Carvalho Chehab | 8573a9e | 2007-04-08 01:09:11 -0300 | [diff] [blame] | 1 | /* |
| 2 | * For Philips TEA5761 FM Chip |
| 3 | * I2C address is allways 0x20 (0x10 at 7-bit mode). |
| 4 | * |
| 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 |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <linux/i2c.h> |
Mauro Carvalho Chehab | 8573a9e | 2007-04-08 01:09:11 -0300 | [diff] [blame] | 11 | #include <linux/delay.h> |
Michael Krufky | ffbb807 | 2007-08-21 01:14:12 -0300 | [diff] [blame] | 12 | #include <linux/videodev.h> |
Mauro Carvalho Chehab | 8573a9e | 2007-04-08 01:09:11 -0300 | [diff] [blame] | 13 | #include <media/tuner.h> |
Michael Krufky | 8218b0b | 2007-06-26 13:12:08 -0300 | [diff] [blame] | 14 | #include "tuner-driver.h" |
Mauro Carvalho Chehab | 8573a9e | 2007-04-08 01:09:11 -0300 | [diff] [blame] | 15 | |
| 16 | #define PREFIX "TEA5761 " |
| 17 | |
| 18 | /* from tuner-core.c */ |
| 19 | extern int tuner_debug; |
| 20 | |
| 21 | /*****************************************************************************/ |
| 22 | |
| 23 | /*************************** |
| 24 | * TEA5761HN I2C registers * |
| 25 | ***************************/ |
| 26 | |
| 27 | /* INTREG - Read: bytes 0 and 1 / Write: byte 0 */ |
| 28 | |
| 29 | /* first byte for reading */ |
| 30 | #define TEA5761_INTREG_IFFLAG 0x10 |
| 31 | #define TEA5761_INTREG_LEVFLAG 0x8 |
| 32 | #define TEA5761_INTREG_FRRFLAG 0x2 |
| 33 | #define TEA5761_INTREG_BLFLAG 0x1 |
| 34 | |
| 35 | /* second byte for reading / byte for writing */ |
| 36 | #define TEA5761_INTREG_IFMSK 0x10 |
| 37 | #define TEA5761_INTREG_LEVMSK 0x8 |
| 38 | #define TEA5761_INTREG_FRMSK 0x2 |
| 39 | #define TEA5761_INTREG_BLMSK 0x1 |
| 40 | |
| 41 | /* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */ |
| 42 | |
| 43 | /* First byte */ |
| 44 | #define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */ |
| 45 | #define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */ |
| 46 | |
| 47 | /* Bits 0-5 for divider MSB */ |
| 48 | |
| 49 | /* Second byte */ |
| 50 | /* Bits 0-7 for divider LSB */ |
| 51 | |
| 52 | /* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */ |
| 53 | |
| 54 | /* first byte */ |
| 55 | |
| 56 | #define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */ |
| 57 | #define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */ |
| 58 | #define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */ |
| 59 | #define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */ |
| 60 | #define TEA5761_TNCTRL_AFM 0x04 |
| 61 | #define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */ |
| 62 | #define TEA5761_TNCTRL_SNC 0x01 |
| 63 | |
| 64 | /* second byte */ |
| 65 | |
| 66 | #define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */ |
| 67 | #define TEA5761_TNCTRL_SSL_1 0x40 |
| 68 | #define TEA5761_TNCTRL_SSL_0 0x20 |
| 69 | #define TEA5761_TNCTRL_HLSI 0x10 |
| 70 | #define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */ |
| 71 | #define TEA5761_TNCTRL_SWP 0x04 |
| 72 | #define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */ |
| 73 | #define TEA5761_TNCTRL_AHLSI 0x01 |
| 74 | |
| 75 | /* FRQCHECK - Read: bytes 6 and 7 */ |
| 76 | /* First byte */ |
| 77 | |
| 78 | /* Bits 0-5 for divider MSB */ |
| 79 | |
| 80 | /* Second byte */ |
| 81 | /* Bits 0-7 for divider LSB */ |
| 82 | |
| 83 | /* TUNCHECK - Read: bytes 8 and 9 */ |
| 84 | |
| 85 | /* First byte */ |
| 86 | #define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */ |
| 87 | #define TEA5761_TUNCHECK_TUNTO 0x01 |
| 88 | |
| 89 | /* Second byte */ |
| 90 | #define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */ |
| 91 | #define TEA5761_TUNCHECK_LD 0x08 |
| 92 | #define TEA5761_TUNCHECK_STEREO 0x04 |
| 93 | |
| 94 | /* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */ |
| 95 | |
| 96 | /* All zero = no test mode */ |
| 97 | |
| 98 | /* MANID - Read: bytes 12 and 13 */ |
| 99 | |
| 100 | /* First byte - should be 0x10 */ |
| 101 | #define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */ |
| 102 | #define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */ |
| 103 | |
| 104 | /* Second byte - Should be 0x2b */ |
| 105 | |
| 106 | #define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */ |
| 107 | #define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */ |
| 108 | |
| 109 | /* Chip ID - Read: bytes 14 and 15 */ |
| 110 | |
| 111 | /* First byte - should be 0x57 */ |
| 112 | |
| 113 | /* Second byte - should be 0x61 */ |
| 114 | |
| 115 | /*****************************************************************************/ |
| 116 | |
| 117 | static void set_tv_freq(struct i2c_client *c, unsigned int freq) |
| 118 | { |
| 119 | struct tuner *t = i2c_get_clientdata(c); |
| 120 | |
| 121 | tuner_warn("This tuner doesn't support TV freq.\n"); |
| 122 | } |
| 123 | |
| 124 | #define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */ |
| 125 | static void tea5761_status_dump(unsigned char *buffer) |
| 126 | { |
| 127 | unsigned int div, frq; |
| 128 | |
| 129 | div = ((buffer[2] & 0x3f) << 8) | buffer[3]; |
| 130 | |
| 131 | frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */ |
| 132 | |
| 133 | printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n", |
| 134 | frq / 1000, frq % 1000, div); |
| 135 | } |
| 136 | |
| 137 | /* Freq should be specifyed at 62.5 Hz */ |
| 138 | static void set_radio_freq(struct i2c_client *c, unsigned int frq) |
| 139 | { |
| 140 | struct tuner *t = i2c_get_clientdata(c); |
| 141 | unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 }; |
| 142 | unsigned div; |
| 143 | int rc; |
| 144 | |
| 145 | tuner_dbg (PREFIX "radio freq counter %d\n", frq); |
| 146 | |
| 147 | if (t->mode == T_STANDBY) { |
| 148 | tuner_dbg("TEA5761 set to standby mode\n"); |
| 149 | buffer[5] |= TEA5761_TNCTRL_MU; |
| 150 | } else { |
| 151 | buffer[4] |= TEA5761_TNCTRL_PUPD_0; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | if (t->audmode == V4L2_TUNER_MODE_MONO) { |
| 156 | tuner_dbg("TEA5761 set to mono\n"); |
| 157 | buffer[5] |= TEA5761_TNCTRL_MST; |
| 158 | ; |
| 159 | } else { |
| 160 | tuner_dbg("TEA5761 set to stereo\n"); |
| 161 | } |
| 162 | |
| 163 | div = (1000 * (frq * 4 / 16 + 700 + 225) ) >> 15; |
| 164 | buffer[1] = (div >> 8) & 0x3f; |
| 165 | buffer[2] = div & 0xff; |
| 166 | |
| 167 | if (tuner_debug) |
| 168 | tea5761_status_dump(buffer); |
| 169 | |
| 170 | if (7 != (rc = i2c_master_send(c, buffer, 7))) |
| 171 | tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc); |
| 172 | } |
| 173 | |
| 174 | static int tea5761_signal(struct i2c_client *c) |
| 175 | { |
| 176 | unsigned char buffer[16]; |
| 177 | int rc; |
| 178 | struct tuner *t = i2c_get_clientdata(c); |
| 179 | |
| 180 | memset(buffer, 0, sizeof(buffer)); |
| 181 | if (16 != (rc = i2c_master_recv(c, buffer, 16))) |
| 182 | tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc); |
| 183 | |
| 184 | return ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4)); |
| 185 | } |
| 186 | |
| 187 | static int tea5761_stereo(struct i2c_client *c) |
| 188 | { |
| 189 | unsigned char buffer[16]; |
| 190 | int rc; |
| 191 | struct tuner *t = i2c_get_clientdata(c); |
| 192 | |
| 193 | memset(buffer, 0, sizeof(buffer)); |
| 194 | if (16 != (rc = i2c_master_recv(c, buffer, 16))) |
| 195 | tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc); |
| 196 | |
| 197 | rc = buffer[9] & TEA5761_TUNCHECK_STEREO; |
| 198 | |
| 199 | tuner_dbg("TEA5761 radio ST GET = %02x\n", rc); |
| 200 | |
| 201 | return (rc ? V4L2_TUNER_SUB_STEREO : 0); |
| 202 | } |
| 203 | |
| 204 | int tea5761_autodetection(struct i2c_client *c) |
| 205 | { |
| 206 | unsigned char buffer[16]; |
| 207 | int rc; |
| 208 | struct tuner *t = i2c_get_clientdata(c); |
| 209 | |
| 210 | if (16 != (rc = i2c_master_recv(c, buffer, 16))) { |
| 211 | tuner_warn("it is not a TEA5761. Received %i chars.\n", rc); |
| 212 | return EINVAL; |
| 213 | } |
| 214 | |
| 215 | if (!((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061))) { |
| 216 | tuner_warn("Manufacturer ID= 0x%02x, Chip ID = %02x%02x. It is not a TEA5761\n",buffer[13],buffer[14],buffer[15]); |
| 217 | return EINVAL; |
| 218 | } |
| 219 | tuner_warn("TEA5761 detected.\n"); |
| 220 | return 0; |
| 221 | } |
| 222 | |
Michael Krufky | e407cd5 | 2007-06-06 16:16:29 -0300 | [diff] [blame] | 223 | static struct tuner_operations tea5761_tuner_ops = { |
| 224 | .set_tv_freq = set_tv_freq, |
| 225 | .set_radio_freq = set_radio_freq, |
| 226 | .has_signal = tea5761_signal, |
| 227 | .is_stereo = tea5761_stereo, |
| 228 | }; |
| 229 | |
Mauro Carvalho Chehab | 8573a9e | 2007-04-08 01:09:11 -0300 | [diff] [blame] | 230 | int tea5761_tuner_init(struct i2c_client *c) |
| 231 | { |
| 232 | struct tuner *t = i2c_get_clientdata(c); |
| 233 | |
| 234 | if (tea5761_autodetection(c) == EINVAL) |
| 235 | return EINVAL; |
| 236 | |
| 237 | tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5761HN FM Radio"); |
| 238 | strlcpy(c->name, "tea5761", sizeof(c->name)); |
| 239 | |
Michael Krufky | e407cd5 | 2007-06-06 16:16:29 -0300 | [diff] [blame] | 240 | memcpy(&t->ops, &tea5761_tuner_ops, sizeof(struct tuner_operations)); |
Mauro Carvalho Chehab | 8573a9e | 2007-04-08 01:09:11 -0300 | [diff] [blame] | 241 | |
| 242 | return (0); |
| 243 | } |