Mauro Carvalho Chehab | 586b0ca | 2005-06-28 20:45:21 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview |
| 3 | * I2C address is allways 0xC0. |
| 4 | * |
| 5 | * $Id: tea5767.c,v 1.11 2005/06/21 15:40:33 mchehab Exp $ |
| 6 | * |
| 7 | * Copyright (c) 2005 Mauro Carvalho Chehab (mchehab@brturbo.com.br) |
| 8 | * This code is placed under the terms of the GNU General Public License |
| 9 | * |
| 10 | * tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa |
| 11 | * from their contributions on DScaler. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/timer.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/videodev.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/i2c-algo-bit.h> |
| 26 | |
| 27 | #include <media/tuner.h> |
| 28 | |
| 29 | /* Declared at tuner-core.c */ |
| 30 | extern unsigned int tuner_debug; |
| 31 | |
| 32 | #define PREFIX "TEA5767 " |
| 33 | |
| 34 | /*****************************************************************************/ |
| 35 | |
| 36 | /****************************** |
| 37 | * Write mode register values * |
| 38 | ******************************/ |
| 39 | |
| 40 | /* First register */ |
| 41 | #define TEA5767_MUTE 0x80 /* Mutes output */ |
| 42 | #define TEA5767_SEARCH 0x40 /* Activates station search */ |
| 43 | /* Bits 0-5 for divider MSB */ |
| 44 | |
| 45 | /* Second register */ |
| 46 | /* Bits 0-7 for divider LSB */ |
| 47 | |
| 48 | /* Third register */ |
| 49 | |
| 50 | /* Station search from botton to up */ |
| 51 | #define TEA5767_SEARCH_UP 0x80 |
| 52 | |
| 53 | /* Searches with ADC output = 10 */ |
| 54 | #define TEA5767_SRCH_HIGH_LVL 0x60 |
| 55 | |
| 56 | /* Searches with ADC output = 10 */ |
| 57 | #define TEA5767_SRCH_MID_LVL 0x40 |
| 58 | |
| 59 | /* Searches with ADC output = 5 */ |
| 60 | #define TEA5767_SRCH_LOW_LVL 0x20 |
| 61 | |
| 62 | /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */ |
| 63 | #define TEA5767_HIGH_LO_INJECT 0x10 |
| 64 | |
| 65 | /* Disable stereo */ |
| 66 | #define TEA5767_MONO 0x08 |
| 67 | |
| 68 | /* Disable right channel and turns to mono */ |
| 69 | #define TEA5767_MUTE_RIGHT 0x04 |
| 70 | |
| 71 | /* Disable left channel and turns to mono */ |
| 72 | #define TEA5767_MUTE_LEFT 0x02 |
| 73 | |
| 74 | #define TEA5767_PORT1_HIGH 0x01 |
| 75 | |
| 76 | /* Forth register */ |
| 77 | #define TEA5767_PORT2_HIGH 0x80 |
| 78 | /* Chips stops working. Only I2C bus remains on */ |
| 79 | #define TEA5767_STDBY 0x40 |
| 80 | |
| 81 | /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */ |
| 82 | #define TEA5767_JAPAN_BAND 0x20 |
| 83 | |
| 84 | /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */ |
| 85 | #define TEA5767_XTAL_32768 0x10 |
| 86 | |
| 87 | /* Cuts weak signals */ |
| 88 | #define TEA5767_SOFT_MUTE 0x08 |
| 89 | |
| 90 | /* Activates high cut control */ |
| 91 | #define TEA5767_HIGH_CUT_CTRL 0x04 |
| 92 | |
| 93 | /* Activates stereo noise control */ |
| 94 | #define TEA5767_ST_NOISE_CTL 0x02 |
| 95 | |
| 96 | /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */ |
| 97 | #define TEA5767_SRCH_IND 0x01 |
| 98 | |
| 99 | /* Fiveth register */ |
| 100 | |
| 101 | /* By activating, it will use Xtal at 13 MHz as reference for divider */ |
| 102 | #define TEA5767_PLLREF_ENABLE 0x80 |
| 103 | |
| 104 | /* By activating, deemphasis=50, or else, deemphasis of 50us */ |
| 105 | #define TEA5767_DEEMPH_75 0X40 |
| 106 | |
| 107 | /***************************** |
| 108 | * Read mode register values * |
| 109 | *****************************/ |
| 110 | |
| 111 | /* First register */ |
| 112 | #define TEA5767_READY_FLAG_MASK 0x80 |
| 113 | #define TEA5767_BAND_LIMIT_MASK 0X40 |
| 114 | /* Bits 0-5 for divider MSB after search or preset */ |
| 115 | |
| 116 | /* Second register */ |
| 117 | /* Bits 0-7 for divider LSB after search or preset */ |
| 118 | |
| 119 | /* Third register */ |
| 120 | #define TEA5767_STEREO_MASK 0x80 |
| 121 | #define TEA5767_IF_CNTR_MASK 0x7f |
| 122 | |
| 123 | /* Four register */ |
| 124 | #define TEA5767_ADC_LEVEL_MASK 0xf0 |
| 125 | |
| 126 | /* should be 0 */ |
| 127 | #define TEA5767_CHIP_ID_MASK 0x0f |
| 128 | |
| 129 | /* Fiveth register */ |
| 130 | /* Reserved for future extensions */ |
| 131 | #define TEA5767_RESERVED_MASK 0xff |
| 132 | |
| 133 | /*****************************************************************************/ |
| 134 | |
| 135 | static void set_tv_freq(struct i2c_client *c, unsigned int freq) |
| 136 | { |
| 137 | struct tuner *t = i2c_get_clientdata(c); |
| 138 | |
| 139 | tuner_warn("This tuner doesn't support TV freq.\n"); |
| 140 | } |
| 141 | |
| 142 | static void tea5767_status_dump(unsigned char *buffer) |
| 143 | { |
| 144 | unsigned int div, frq; |
| 145 | |
| 146 | if (TEA5767_READY_FLAG_MASK & buffer[0]) |
| 147 | printk(PREFIX "Ready Flag ON\n"); |
| 148 | else |
| 149 | printk(PREFIX "Ready Flag OFF\n"); |
| 150 | |
| 151 | if (TEA5767_BAND_LIMIT_MASK & buffer[0]) |
| 152 | printk(PREFIX "Tuner at band limit\n"); |
| 153 | else |
| 154 | printk(PREFIX "Tuner not at band limit\n"); |
| 155 | |
| 156 | div=((buffer[0]&0x3f)<<8) | buffer[1]; |
| 157 | |
| 158 | switch (TEA5767_HIGH_LO_32768) { |
| 159 | case TEA5767_HIGH_LO_13MHz: |
| 160 | frq = 1000*(div*50-700-225)/4; /* Freq in KHz */ |
| 161 | break; |
| 162 | case TEA5767_LOW_LO_13MHz: |
| 163 | frq = 1000*(div*50+700+225)/4; /* Freq in KHz */ |
| 164 | break; |
| 165 | case TEA5767_LOW_LO_32768: |
| 166 | frq = 1000*(div*32768/1000+700+225)/4; /* Freq in KHz */ |
| 167 | break; |
| 168 | case TEA5767_HIGH_LO_32768: |
| 169 | default: |
| 170 | frq = 1000*(div*32768/1000-700-225)/4; /* Freq in KHz */ |
| 171 | break; |
| 172 | } |
| 173 | buffer[0] = (div>>8) & 0x3f; |
| 174 | buffer[1] = div & 0xff; |
| 175 | |
| 176 | printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n", |
| 177 | frq/1000,frq%1000,div); |
| 178 | |
| 179 | if (TEA5767_STEREO_MASK & buffer[2]) |
| 180 | printk(PREFIX "Stereo\n"); |
| 181 | else |
| 182 | printk(PREFIX "Mono\n"); |
| 183 | |
| 184 | printk(PREFIX "IF Counter = %d\n",buffer[2] & TEA5767_IF_CNTR_MASK); |
| 185 | |
| 186 | printk(PREFIX "ADC Level = %d\n",(buffer[3] & TEA5767_ADC_LEVEL_MASK)>>4); |
| 187 | |
| 188 | printk(PREFIX "Chip ID = %d\n",(buffer[3] & TEA5767_CHIP_ID_MASK)); |
| 189 | |
| 190 | printk(PREFIX "Reserved = 0x%02x\n",(buffer[4] & TEA5767_RESERVED_MASK)); |
| 191 | } |
| 192 | |
| 193 | /* Freq should be specifyed at 62.5 Hz */ |
| 194 | static void set_radio_freq(struct i2c_client *c, unsigned int frq) |
| 195 | { |
| 196 | struct tuner *t = i2c_get_clientdata(c); |
| 197 | unsigned char buffer[5]; |
| 198 | unsigned div; |
| 199 | int rc; |
| 200 | |
| 201 | if ( tuner_debug ) |
| 202 | printk(PREFIX "radio freq counter %d\n",frq); |
| 203 | |
| 204 | /* Rounds freq to next decimal value - for 62.5 KHz step */ |
| 205 | /* frq = 20*(frq/16)+radio_frq[frq%16]; */ |
| 206 | |
| 207 | buffer[2] = TEA5767_PORT1_HIGH; |
| 208 | buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL | TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND; |
| 209 | buffer[4]=0; |
| 210 | |
| 211 | if (t->audmode == V4L2_TUNER_MODE_MONO) { |
| 212 | tuner_dbg("TEA5767 set to mono\n"); |
| 213 | buffer[2] |= TEA5767_MONO; |
| 214 | } else |
| 215 | tuner_dbg("TEA5767 set to stereo\n"); |
| 216 | |
| 217 | switch (t->type) { |
| 218 | case TEA5767_HIGH_LO_13MHz: |
| 219 | tuner_dbg("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n"); |
| 220 | buffer[2] |= TEA5767_HIGH_LO_INJECT; |
| 221 | buffer[4] |= TEA5767_PLLREF_ENABLE; |
| 222 | div = (frq*4/16+700+225+25)/50; |
| 223 | break; |
| 224 | case TEA5767_LOW_LO_13MHz: |
| 225 | tuner_dbg("TEA5767 radio LOW LO inject xtal @ 13 MHz\n"); |
| 226 | |
| 227 | buffer[4] |= TEA5767_PLLREF_ENABLE; |
| 228 | div = (frq*4/16-700-225+25)/50; |
| 229 | break; |
| 230 | case TEA5767_LOW_LO_32768: |
| 231 | tuner_dbg("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n"); |
| 232 | buffer[3] |= TEA5767_XTAL_32768; |
| 233 | /* const 700=4000*175 Khz - to adjust freq to right value */ |
| 234 | div = (1000*(frq*4/16-700-225)+16384)>>15; |
| 235 | break; |
| 236 | case TEA5767_HIGH_LO_32768: |
| 237 | default: |
| 238 | tuner_dbg("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n"); |
| 239 | |
| 240 | buffer[2] |= TEA5767_HIGH_LO_INJECT; |
| 241 | buffer[3] |= TEA5767_XTAL_32768; |
| 242 | div = (1000*(frq*4/16+700+225)+16384)>>15; |
| 243 | break; |
| 244 | } |
| 245 | buffer[0] = (div>>8) & 0x3f; |
| 246 | buffer[1] = div & 0xff; |
| 247 | |
| 248 | if ( tuner_debug ) |
| 249 | tea5767_status_dump(buffer); |
| 250 | |
| 251 | if (5 != (rc = i2c_master_send(c,buffer,5))) |
| 252 | tuner_warn("i2c i/o error: rc == %d (should be 5)\n",rc); |
| 253 | } |
| 254 | |
| 255 | static int tea5767_signal(struct i2c_client *c) |
| 256 | { |
| 257 | unsigned char buffer[5]; |
| 258 | int rc; |
| 259 | struct tuner *t = i2c_get_clientdata(c); |
| 260 | |
| 261 | memset(buffer,0,sizeof(buffer)); |
| 262 | if (5 != (rc = i2c_master_recv(c,buffer,5))) |
| 263 | tuner_warn ( "i2c i/o error: rc == %d (should be 5)\n",rc); |
| 264 | |
| 265 | return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) <<(13-4)); |
| 266 | } |
| 267 | |
| 268 | static int tea5767_stereo(struct i2c_client *c) |
| 269 | { |
| 270 | unsigned char buffer[5]; |
| 271 | int rc; |
| 272 | struct tuner *t = i2c_get_clientdata(c); |
| 273 | |
| 274 | memset(buffer,0,sizeof(buffer)); |
| 275 | if (5 != (rc = i2c_master_recv(c,buffer,5))) |
| 276 | tuner_warn ( "i2c i/o error: rc == %d (should be 5)\n",rc); |
| 277 | |
| 278 | rc = buffer[2] & TEA5767_STEREO_MASK; |
| 279 | |
| 280 | if ( tuner_debug ) |
| 281 | tuner_dbg("TEA5767 radio ST GET = %02x\n", rc); |
| 282 | |
| 283 | return ( (buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO: 0); |
| 284 | } |
| 285 | |
| 286 | int tea_detection(struct i2c_client *c) |
| 287 | { |
| 288 | unsigned char buffer[5]= { 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 289 | int rc; |
| 290 | struct tuner *t = i2c_get_clientdata(c); |
| 291 | |
| 292 | if (5 != (rc = i2c_master_recv(c,buffer,5))) { |
| 293 | tuner_warn ( "it is not a TEA5767. Received %i chars.\n",rc ); |
| 294 | return EINVAL; |
| 295 | } |
| 296 | |
| 297 | /* If all bytes are the same then it's a TV tuner and not a tea5767 chip. */ |
| 298 | if (buffer[0] == buffer[1] && buffer[0] == buffer[2] && |
| 299 | buffer[0] == buffer[3] && buffer[0] == buffer[4]) { |
| 300 | tuner_warn ( "All bytes are equal. It is not a TEA5767\n" ); |
| 301 | return EINVAL; |
| 302 | } |
| 303 | |
| 304 | /* Status bytes: |
| 305 | * Byte 4: bit 3:1 : CI (Chip Identification) == 0 |
| 306 | * bit 0 : internally set to 0 |
| 307 | * Byte 5: bit 7:0 : == 0 |
| 308 | */ |
| 309 | |
| 310 | if (!((buffer[3] & 0x0f) == 0x00) && (buffer[4] == 0x00)) { |
| 311 | tuner_warn ( "Chip ID is not zero. It is not a TEA5767\n" ); |
| 312 | return EINVAL; |
| 313 | } |
| 314 | tuner_warn ( "TEA5767 detected.\n" ); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | int tea5767_tuner_init(struct i2c_client *c) |
| 319 | { |
| 320 | struct tuner *t = i2c_get_clientdata(c); |
| 321 | |
| 322 | if (tea_detection(c)==EINVAL) return EINVAL; |
| 323 | |
| 324 | tuner_info("type set to %d (%s)\n", |
| 325 | t->type, TEA5767_TUNER_NAME); |
| 326 | strlcpy(c->name, TEA5767_TUNER_NAME, sizeof(c->name)); |
| 327 | |
| 328 | t->tv_freq = set_tv_freq; |
| 329 | t->radio_freq = set_radio_freq; |
| 330 | t->has_signal = tea5767_signal; |
| 331 | t->is_stereo = tea5767_stereo; |
| 332 | |
| 333 | return (0); |
| 334 | } |