Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * i2c tv tuner chip device driver |
| 4 | * controls the philips tda8290+75 tuner chip combo. |
| 5 | */ |
| 6 | #include <linux/i2c.h> |
| 7 | #include <linux/videodev.h> |
| 8 | #include <linux/delay.h> |
| 9 | #include <media/tuner.h> |
| 10 | |
Mauro Carvalho Chehab | 793cf9e | 2005-09-09 13:03:37 -0700 | [diff] [blame] | 11 | #define I2C_ADDR_TDA8290 0x4b |
| 12 | #define I2C_ADDR_TDA8275 0x61 |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | /* ---------------------------------------------------------------------- */ |
| 15 | |
| 16 | struct freq_entry { |
| 17 | u16 freq; |
| 18 | u8 value; |
| 19 | }; |
| 20 | |
| 21 | static struct freq_entry band_table[] = { |
| 22 | { 0x2DF4, 0x1C }, |
| 23 | { 0x2574, 0x14 }, |
| 24 | { 0x22B4, 0x0C }, |
| 25 | { 0x20D4, 0x0B }, |
| 26 | { 0x1E74, 0x3B }, |
| 27 | { 0x1C34, 0x33 }, |
| 28 | { 0x16F4, 0x5B }, |
| 29 | { 0x1454, 0x53 }, |
| 30 | { 0x12D4, 0x52 }, |
| 31 | { 0x1034, 0x4A }, |
| 32 | { 0x0EE4, 0x7A }, |
| 33 | { 0x0D34, 0x72 }, |
| 34 | { 0x0B54, 0x9A }, |
| 35 | { 0x0914, 0x91 }, |
| 36 | { 0x07F4, 0x89 }, |
| 37 | { 0x0774, 0xB9 }, |
| 38 | { 0x067B, 0xB1 }, |
| 39 | { 0x0634, 0xD9 }, |
| 40 | { 0x05A4, 0xD8 }, // FM radio |
| 41 | { 0x0494, 0xD0 }, |
| 42 | { 0x03BC, 0xC8 }, |
| 43 | { 0x0394, 0xF8 }, // 57250000 Hz |
| 44 | { 0x0000, 0xF0 }, // 0 |
| 45 | }; |
| 46 | |
| 47 | static struct freq_entry div_table[] = { |
| 48 | { 0x1C34, 3 }, |
| 49 | { 0x0D34, 2 }, |
| 50 | { 0x067B, 1 }, |
| 51 | { 0x0000, 0 }, |
| 52 | }; |
| 53 | |
| 54 | static struct freq_entry agc_table[] = { |
| 55 | { 0x22B4, 0x8F }, |
| 56 | { 0x0B54, 0x9F }, |
| 57 | { 0x09A4, 0x8F }, |
| 58 | { 0x0554, 0x9F }, |
| 59 | { 0x0000, 0xBF }, |
| 60 | }; |
| 61 | |
| 62 | static __u8 get_freq_entry( struct freq_entry* table, __u16 freq) |
| 63 | { |
| 64 | while(table->freq && table->freq > freq) |
| 65 | table++; |
| 66 | return table->value; |
| 67 | } |
| 68 | |
| 69 | /* ---------------------------------------------------------------------- */ |
| 70 | |
| 71 | static unsigned char i2c_enable_bridge[2] = { 0x21, 0xC0 }; |
| 72 | static unsigned char i2c_disable_bridge[2] = { 0x21, 0x80 }; |
| 73 | static unsigned char i2c_init_tda8275[14] = { 0x00, 0x00, 0x00, 0x00, |
Mauro Carvalho Chehab | 586b0ca | 2005-06-28 20:45:21 -0700 | [diff] [blame] | 74 | 0xfC, 0x04, 0xA3, 0x3F, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 0x2A, 0x04, 0xFF, 0x00, |
| 76 | 0x00, 0x40 }; |
| 77 | static unsigned char i2c_set_VS[2] = { 0x30, 0x6F }; |
| 78 | static unsigned char i2c_set_GP01_CF[2] = { 0x20, 0x0B }; |
| 79 | static unsigned char i2c_tda8290_reset[2] = { 0x00, 0x00 }; |
Mauro Carvalho Chehab | 793cf9e | 2005-09-09 13:03:37 -0700 | [diff] [blame] | 80 | static unsigned char i2c_tda8290_standby[2] = { 0x00, 0x02 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | static unsigned char i2c_gainset_off[2] = { 0x28, 0x14 }; |
| 82 | static unsigned char i2c_gainset_on[2] = { 0x28, 0x54 }; |
| 83 | static unsigned char i2c_agc3_00[2] = { 0x80, 0x00 }; |
| 84 | static unsigned char i2c_agc2_BF[2] = { 0x60, 0xBF }; |
Mauro Carvalho Chehab | 793cf9e | 2005-09-09 13:03:37 -0700 | [diff] [blame] | 85 | static unsigned char i2c_cb1_D0[2] = { 0x30, 0xD0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static unsigned char i2c_cb1_D2[2] = { 0x30, 0xD2 }; |
| 87 | static unsigned char i2c_cb1_56[2] = { 0x30, 0x56 }; |
| 88 | static unsigned char i2c_cb1_52[2] = { 0x30, 0x52 }; |
| 89 | static unsigned char i2c_cb1_50[2] = { 0x30, 0x50 }; |
| 90 | static unsigned char i2c_agc2_7F[2] = { 0x60, 0x7F }; |
| 91 | static unsigned char i2c_agc3_08[2] = { 0x80, 0x08 }; |
| 92 | |
Mauro Carvalho Chehab | c35d4b8 | 2005-11-08 21:36:27 -0800 | [diff] [blame^] | 93 | /* FIXME: European PAL/SECAM should select 9MHz Lowpass Filter, while |
| 94 | NTSC/M and PAL/M should be using 7MHz filter, by selecting CB3 */ |
| 95 | static unsigned char i2c_cb3_9MHz[2] = { 0xc0, 0x39 }; |
| 96 | static unsigned char i2c_cb3_7MHz[2] = { 0xc0, 0x3B }; |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | static struct i2c_msg i2c_msg_init[] = { |
| 99 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_init_tda8275), i2c_init_tda8275 }, |
| 100 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge }, |
| 101 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_set_VS), i2c_set_VS }, |
| 102 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_set_GP01_CF), i2c_set_GP01_CF }, |
| 103 | }; |
| 104 | |
| 105 | static struct i2c_msg i2c_msg_prolog[] = { |
| 106 | // { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_easy_mode), i2c_easy_mode }, |
| 107 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_gainset_off), i2c_gainset_off }, |
| 108 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_tda8290_reset), i2c_tda8290_reset }, |
| 109 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_enable_bridge), i2c_enable_bridge }, |
| 110 | }; |
| 111 | |
| 112 | static struct i2c_msg i2c_msg_config[] = { |
| 113 | // { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_set_freq), i2c_set_freq }, |
| 114 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc3_00), i2c_agc3_00 }, |
| 115 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc2_BF), i2c_agc2_BF }, |
| 116 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_D2), i2c_cb1_D2 }, |
| 117 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_56), i2c_cb1_56 }, |
| 118 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_52), i2c_cb1_52 }, |
| 119 | }; |
| 120 | |
| 121 | static struct i2c_msg i2c_msg_epilog[] = { |
| 122 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_50), i2c_cb1_50 }, |
| 123 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc2_7F), i2c_agc2_7F }, |
| 124 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc3_08), i2c_agc3_08 }, |
| 125 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge }, |
| 126 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_gainset_on), i2c_gainset_on }, |
| 127 | }; |
| 128 | |
Mauro Carvalho Chehab | 793cf9e | 2005-09-09 13:03:37 -0700 | [diff] [blame] | 129 | static struct i2c_msg i2c_msg_standby[] = { |
| 130 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_enable_bridge), i2c_enable_bridge }, |
| 131 | { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_D0), i2c_cb1_D0 }, |
| 132 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge }, |
| 133 | { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_tda8290_standby), i2c_tda8290_standby }, |
| 134 | }; |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | static int tda8290_tune(struct i2c_client *c) |
| 137 | { |
| 138 | struct tuner *t = i2c_get_clientdata(c); |
| 139 | struct i2c_msg easy_mode = |
| 140 | { I2C_ADDR_TDA8290, 0, 2, t->i2c_easy_mode }; |
| 141 | struct i2c_msg set_freq = |
| 142 | { I2C_ADDR_TDA8275, 0, 8, t->i2c_set_freq }; |
| 143 | |
| 144 | i2c_transfer(c->adapter, &easy_mode, 1); |
| 145 | i2c_transfer(c->adapter, i2c_msg_prolog, ARRAY_SIZE(i2c_msg_prolog)); |
| 146 | |
| 147 | i2c_transfer(c->adapter, &set_freq, 1); |
| 148 | i2c_transfer(c->adapter, i2c_msg_config, ARRAY_SIZE(i2c_msg_config)); |
| 149 | |
| 150 | msleep(550); |
| 151 | i2c_transfer(c->adapter, i2c_msg_epilog, ARRAY_SIZE(i2c_msg_epilog)); |
| 152 | return 0; |
| 153 | } |
| 154 | |
Mauro Carvalho Chehab | f7ce3cc | 2005-07-12 13:58:55 -0700 | [diff] [blame] | 155 | static void set_frequency(struct tuner *t, u16 ifc, unsigned int freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Mauro Carvalho Chehab | 586b0ca | 2005-06-28 20:45:21 -0700 | [diff] [blame] | 157 | u32 N; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Mauro Carvalho Chehab | 586b0ca | 2005-06-28 20:45:21 -0700 | [diff] [blame] | 159 | if (t->mode == V4L2_TUNER_RADIO) |
Mauro Carvalho Chehab | f7ce3cc | 2005-07-12 13:58:55 -0700 | [diff] [blame] | 160 | freq = freq / 1000; |
Mauro Carvalho Chehab | 586b0ca | 2005-06-28 20:45:21 -0700 | [diff] [blame] | 161 | |
| 162 | N = (((freq<<3)+ifc)&0x3fffc); |
| 163 | |
| 164 | N = N >> get_freq_entry(div_table, freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | t->i2c_set_freq[0] = 0; |
| 166 | t->i2c_set_freq[1] = (unsigned char)(N>>8); |
| 167 | t->i2c_set_freq[2] = (unsigned char) N; |
| 168 | t->i2c_set_freq[3] = 0x40; |
| 169 | t->i2c_set_freq[4] = 0x52; |
Mauro Carvalho Chehab | 586b0ca | 2005-06-28 20:45:21 -0700 | [diff] [blame] | 170 | t->i2c_set_freq[5] = get_freq_entry(band_table, freq); |
| 171 | t->i2c_set_freq[6] = get_freq_entry(agc_table, freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | t->i2c_set_freq[7] = 0x8f; |
| 173 | } |
| 174 | |
| 175 | #define V4L2_STD_MN (V4L2_STD_PAL_M|V4L2_STD_PAL_N|V4L2_STD_PAL_Nc|V4L2_STD_NTSC) |
| 176 | #define V4L2_STD_B (V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_SECAM_B) |
| 177 | #define V4L2_STD_GH (V4L2_STD_PAL_G|V4L2_STD_PAL_H|V4L2_STD_SECAM_G|V4L2_STD_SECAM_H) |
| 178 | #define V4L2_STD_DK (V4L2_STD_PAL_DK|V4L2_STD_SECAM_DK) |
| 179 | |
| 180 | static void set_audio(struct tuner *t) |
| 181 | { |
| 182 | t->i2c_easy_mode[0] = 0x01; |
| 183 | |
| 184 | if (t->std & V4L2_STD_MN) |
| 185 | t->i2c_easy_mode[1] = 0x01; |
| 186 | else if (t->std & V4L2_STD_B) |
| 187 | t->i2c_easy_mode[1] = 0x02; |
| 188 | else if (t->std & V4L2_STD_GH) |
| 189 | t->i2c_easy_mode[1] = 0x04; |
| 190 | else if (t->std & V4L2_STD_PAL_I) |
| 191 | t->i2c_easy_mode[1] = 0x08; |
| 192 | else if (t->std & V4L2_STD_DK) |
| 193 | t->i2c_easy_mode[1] = 0x10; |
| 194 | else if (t->std & V4L2_STD_SECAM_L) |
| 195 | t->i2c_easy_mode[1] = 0x20; |
| 196 | } |
| 197 | |
| 198 | static void set_tv_freq(struct i2c_client *c, unsigned int freq) |
| 199 | { |
| 200 | struct tuner *t = i2c_get_clientdata(c); |
| 201 | |
| 202 | set_audio(t); |
Mauro Carvalho Chehab | f7ce3cc | 2005-07-12 13:58:55 -0700 | [diff] [blame] | 203 | set_frequency(t, 864, freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | tda8290_tune(c); |
| 205 | } |
| 206 | |
| 207 | static void set_radio_freq(struct i2c_client *c, unsigned int freq) |
| 208 | { |
| 209 | struct tuner *t = i2c_get_clientdata(c); |
Mauro Carvalho Chehab | f7ce3cc | 2005-07-12 13:58:55 -0700 | [diff] [blame] | 210 | set_frequency(t, 704, freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | tda8290_tune(c); |
| 212 | } |
| 213 | |
| 214 | static int has_signal(struct i2c_client *c) |
| 215 | { |
| 216 | unsigned char i2c_get_afc[1] = { 0x1B }; |
| 217 | unsigned char afc = 0; |
| 218 | |
| 219 | i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc)); |
| 220 | i2c_master_recv(c, &afc, 1); |
| 221 | return (afc & 0x80)? 65535:0; |
| 222 | } |
| 223 | |
Mauro Carvalho Chehab | 793cf9e | 2005-09-09 13:03:37 -0700 | [diff] [blame] | 224 | static void standby(struct i2c_client *c) |
| 225 | { |
| 226 | i2c_transfer(c->adapter, i2c_msg_standby, ARRAY_SIZE(i2c_msg_standby)); |
| 227 | } |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | int tda8290_init(struct i2c_client *c) |
| 230 | { |
| 231 | struct tuner *t = i2c_get_clientdata(c); |
| 232 | |
| 233 | strlcpy(c->name, "tda8290+75", sizeof(c->name)); |
| 234 | tuner_info("tuner: type set to %s\n", c->name); |
| 235 | t->tv_freq = set_tv_freq; |
| 236 | t->radio_freq = set_radio_freq; |
| 237 | t->has_signal = has_signal; |
Mauro Carvalho Chehab | 793cf9e | 2005-09-09 13:03:37 -0700 | [diff] [blame] | 238 | t->standby = standby; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | i2c_master_send(c, i2c_enable_bridge, ARRAY_SIZE(i2c_enable_bridge)); |
| 241 | i2c_transfer(c->adapter, i2c_msg_init, ARRAY_SIZE(i2c_msg_init)); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 247 | * --------------------------------------------------------------------------- |
| 248 | * Local variables: |
| 249 | * c-basic-offset: 8 |
| 250 | * End: |
| 251 | */ |