blob: eca2ff249a36f7dc704662bf95cd41332495577b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * i2c tv tuner chip device driver
4 * controls all those simple 4-control-bytes style tuners.
5 */
6#include <linux/delay.h>
7#include <linux/i2c.h>
8#include <linux/videodev.h>
9#include <media/tuner.h>
Hans Verkuilba8fc392006-06-25 15:34:39 -030010#include <media/v4l2-common.h>
Michael Krufky8218b0b2007-06-26 13:12:08 -030011#include <media/tuner-types.h>
12#include "tuner-driver.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Mauro Carvalho Chehab5c07db02006-01-09 15:25:11 -020014static int offset = 0;
Mauro Carvalho Chehab4d988162006-08-12 21:59:19 -030015module_param(offset, int, 0664);
Mauro Carvalho Chehab5c07db02006-01-09 15:25:11 -020016MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/* ---------------------------------------------------------------------- */
19
20/* tv standard selection for Temic 4046 FM5
21 this value takes the low bits of control byte 2
22 from datasheet Rev.01, Feb.00
23 standard BG I L L2 D
24 picture IF 38.9 38.9 38.9 33.95 38.9
25 sound 1 33.4 32.9 32.4 40.45 32.4
26 sound 2 33.16
27 NICAM 33.05 32.348 33.05 33.05
28 */
29#define TEMIC_SET_PAL_I 0x05
30#define TEMIC_SET_PAL_DK 0x09
31#define TEMIC_SET_PAL_L 0x0a // SECAM ?
32#define TEMIC_SET_PAL_L2 0x0b // change IF !
33#define TEMIC_SET_PAL_BG 0x0c
34
35/* tv tuner system standard selection for Philips FQ1216ME
36 this value takes the low bits of control byte 2
37 from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
38 standard BG DK I L L`
39 picture carrier 38.90 38.90 38.90 38.90 33.95
40 colour 34.47 34.47 34.47 34.47 38.38
41 sound 1 33.40 32.40 32.90 32.40 40.45
42 sound 2 33.16 - - - -
43 NICAM 33.05 33.05 32.35 33.05 39.80
44 */
45#define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
46#define PHILIPS_SET_PAL_BGDK 0x09
47#define PHILIPS_SET_PAL_L2 0x0a
48#define PHILIPS_SET_PAL_L 0x0b
49
50/* system switching for Philips FI1216MF MK2
51 from datasheet "1996 Jul 09",
52 standard BG L L'
53 picture carrier 38.90 38.90 33.95
54 colour 34.47 34.37 38.38
55 sound 1 33.40 32.40 40.45
56 sound 2 33.16 - -
57 NICAM 33.05 33.05 39.80
58 */
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -030059#define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
60#define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
61#define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070063/* Control byte */
64
65#define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
66#define TUNER_RATIO_SELECT_50 0x00
67#define TUNER_RATIO_SELECT_32 0x02
68#define TUNER_RATIO_SELECT_166 0x04
69#define TUNER_RATIO_SELECT_62 0x06
70
71#define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
72
73/* Status byte */
74
75#define TUNER_POR 0x80
76#define TUNER_FL 0x40
77#define TUNER_MODE 0x38
78#define TUNER_AFC 0x07
79#define TUNER_SIGNAL 0x07
80#define TUNER_STEREO 0x10
81
82#define TUNER_PLL_LOCKED 0x40
83#define TUNER_STEREO_MK3 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Michael Krufky6b1dde92007-08-11 15:42:12 -030085struct tuner_simple_priv {
86 u16 last_div;
Michael Krufkydb8a6952007-08-21 01:24:42 -030087 struct tuner_i2c_props i2c_props;
Michael Krufky6b1dde92007-08-11 15:42:12 -030088};
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* ---------------------------------------------------------------------- */
91
Michael Krufkydb8a6952007-08-21 01:24:42 -030092static int tuner_getstatus(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Michael Krufkydb8a6952007-08-21 01:24:42 -030094 struct tuner_simple_priv *priv = t->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unsigned char byte;
96
Michael Krufkydb8a6952007-08-21 01:24:42 -030097 if (1 != tuner_i2c_xfer_recv(&priv->i2c_props,&byte,1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return byte;
101}
102
Michael Krufkydb8a6952007-08-21 01:24:42 -0300103static int tuner_signal(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300105 return (tuner_getstatus(t) & TUNER_SIGNAL) << 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Michael Krufkydb8a6952007-08-21 01:24:42 -0300108static int tuner_stereo(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700110 int stereo, status;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700111
Michael Krufkydb8a6952007-08-21 01:24:42 -0300112 status = tuner_getstatus(t);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700113
114 switch (t->type) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800115 case TUNER_PHILIPS_FM1216ME_MK3:
Trent Piepho657de3c2006-06-20 00:30:57 -0300116 case TUNER_PHILIPS_FM1236_MK3:
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700117 case TUNER_PHILIPS_FM1256_IH3:
Hans Verkuil122b5db2006-12-03 06:45:07 -0300118 case TUNER_LG_NTSC_TAPE:
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700119 stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
120 break;
121 default:
122 stereo = status & TUNER_STEREO;
123 }
124
125 return stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* ---------------------------------------------------------------------- */
130
Michael Krufkydb8a6952007-08-21 01:24:42 -0300131static void default_set_tv_freq(struct tuner *t, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Michael Krufky6b1dde92007-08-11 15:42:12 -0300133 struct tuner_simple_priv *priv = t->priv;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200134 u8 config, cb, tuneraddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 u16 div;
136 struct tunertype *tun;
Hans Verkuil27487d42006-01-15 15:04:52 -0200137 u8 buffer[4];
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200138 int rc, IFPCoff, i, j;
Michael Krufky476d63d2006-02-07 06:25:34 -0200139 enum param_type desired_type;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300140 struct tuner_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 tun = &tuners[t->type];
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200143
Michael Krufky5f594182006-02-07 06:25:33 -0200144 /* IFPCoff = Video Intermediate Frequency - Vif:
145 940 =16*58.75 NTSC/J (Japan)
146 732 =16*45.75 M/N STD
147 704 =16*44 ATSC (at DVB code)
148 632 =16*39.50 I U.K.
149 622.4=16*38.90 B/G D/K I, L STD
150 592 =16*37.00 D China
151 590 =16.36.875 B Australia
152 543.2=16*33.95 L' STD
153 171.2=16*10.70 FM Radio (at set_radio_freq)
154 */
155
156 if (t->std == V4L2_STD_NTSC_M_JP) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200157 IFPCoff = 940;
158 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky5f594182006-02-07 06:25:33 -0200159 } else if ((t->std & V4L2_STD_MN) &&
160 !(t->std & ~V4L2_STD_MN)) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200161 IFPCoff = 732;
162 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky5f594182006-02-07 06:25:33 -0200163 } else if (t->std == V4L2_STD_SECAM_LC) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200164 IFPCoff = 543;
165 desired_type = TUNER_PARAM_TYPE_SECAM;
Michael Krufky5f594182006-02-07 06:25:33 -0200166 } else {
Michael Krufky476d63d2006-02-07 06:25:34 -0200167 IFPCoff = 623;
168 desired_type = TUNER_PARAM_TYPE_PAL;
Michael Krufky5f594182006-02-07 06:25:33 -0200169 }
170
Michael Krufky476d63d2006-02-07 06:25:34 -0200171 for (j = 0; j < tun->count-1; j++) {
172 if (desired_type != tun->params[j].type)
173 continue;
174 break;
175 }
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200176 /* use default tuner_params if desired_type not available */
Michael Krufky15207b22006-02-07 06:25:40 -0200177 if (desired_type != tun->params[j].type) {
178 tuner_dbg("IFPCoff = %d: tuner_params undefined for tuner %d\n",
179 IFPCoff,t->type);
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200180 j = 0;
Michael Krufky15207b22006-02-07 06:25:40 -0200181 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300182 params = &tun->params[j];
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200183
Hans Verkuilba8fc392006-06-25 15:34:39 -0300184 for (i = 0; i < params->count; i++) {
185 if (freq > params->ranges[i].limit)
Michael Krufky90200d22006-01-09 15:25:38 -0200186 continue;
187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300189 if (i == params->count) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200190 tuner_dbg("TV frequency out of range (%d > %d)",
Hans Verkuilba8fc392006-06-25 15:34:39 -0300191 freq, params->ranges[i - 1].limit);
192 freq = params->ranges[--i].limit;
Hans Verkuil27487d42006-01-15 15:04:52 -0200193 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300194 config = params->ranges[i].config;
195 cb = params->ranges[i].cb;
Michael Krufky5f594182006-02-07 06:25:33 -0200196 /* i == 0 -> VHF_LO
197 * i == 1 -> VHF_HI
198 * i == 2 -> UHF */
Michael Krufkybd0d0f52006-02-07 06:25:35 -0200199 tuner_dbg("tv: param %d, range %d\n",j,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Michael Krufky5f594182006-02-07 06:25:33 -0200201 div=freq + IFPCoff + offset;
202
203 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
204 freq / 16, freq % 16 * 100 / 16,
205 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
206 offset / 16, offset % 16 * 100 / 16,
207 div);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* tv norm specific stuff for multi-norm tuners */
210 switch (t->type) {
211 case TUNER_PHILIPS_SECAM: // FI1216MF
212 /* 0x01 -> ??? no change ??? */
213 /* 0x02 -> PAL BDGHI / SECAM L */
214 /* 0x04 -> ??? PAL others / SECAM others ??? */
matthieu castet82c01d32007-05-21 11:15:09 -0300215 cb &= ~0x03;
216 if (t->std & V4L2_STD_SECAM_L) //also valid for V4L2_STD_SECAM
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -0300217 cb |= PHILIPS_MF_SET_STD_L;
matthieu castet82c01d32007-05-21 11:15:09 -0300218 else if (t->std & V4L2_STD_SECAM_LC)
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -0300219 cb |= PHILIPS_MF_SET_STD_LC;
matthieu castet82c01d32007-05-21 11:15:09 -0300220 else /* V4L2_STD_B|V4L2_STD_GH */
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -0300221 cb |= PHILIPS_MF_SET_STD_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
223
224 case TUNER_TEMIC_4046FM5:
Michael Krufkyab66b222006-01-23 17:11:11 -0200225 cb &= ~0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (t->std & V4L2_STD_PAL_BG) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200228 cb |= TEMIC_SET_PAL_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 } else if (t->std & V4L2_STD_PAL_I) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200231 cb |= TEMIC_SET_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 } else if (t->std & V4L2_STD_PAL_DK) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200234 cb |= TEMIC_SET_PAL_DK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 } else if (t->std & V4L2_STD_SECAM_L) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200237 cb |= TEMIC_SET_PAL_L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 }
240 break;
241
242 case TUNER_PHILIPS_FQ1216ME:
Michael Krufkyab66b222006-01-23 17:11:11 -0200243 cb &= ~0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200246 cb |= PHILIPS_SET_PAL_BGDK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 } else if (t->std & V4L2_STD_PAL_I) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200249 cb |= PHILIPS_SET_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 } else if (t->std & V4L2_STD_SECAM_L) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200252 cb |= PHILIPS_SET_PAL_L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 }
255 break;
256
257 case TUNER_PHILIPS_ATSC:
258 /* 0x00 -> ATSC antenna input 1 */
259 /* 0x01 -> ATSC antenna input 2 */
260 /* 0x02 -> NTSC antenna input 1 */
261 /* 0x03 -> NTSC antenna input 2 */
Michael Krufkyab66b222006-01-23 17:11:11 -0200262 cb &= ~0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!(t->std & V4L2_STD_ATSC))
Michael Krufkyab66b222006-01-23 17:11:11 -0200264 cb |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* FIXME: input */
266 break;
267
268 case TUNER_MICROTUNE_4042FI5:
269 /* Set the charge pump for fast tuning */
Michael Krufky3fc46d32006-01-23 17:11:11 -0200270 config |= TUNER_CHARGE_PUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 break;
Kirk Lapraye976f9372005-11-08 21:37:04 -0800272
273 case TUNER_PHILIPS_TUV1236D:
274 /* 0x40 -> ATSC antenna input 1 */
275 /* 0x48 -> ATSC antenna input 2 */
276 /* 0x00 -> NTSC antenna input 1 */
277 /* 0x08 -> NTSC antenna input 2 */
Kirk Laprayfde6d312005-11-08 21:38:18 -0800278 buffer[0] = 0x14;
279 buffer[1] = 0x00;
280 buffer[2] = 0x17;
281 buffer[3] = 0x00;
Michael Krufkyab66b222006-01-23 17:11:11 -0200282 cb &= ~0x40;
Kirk Laprayfde6d312005-11-08 21:38:18 -0800283 if (t->std & V4L2_STD_ATSC) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200284 cb |= 0x40;
Kirk Laprayfde6d312005-11-08 21:38:18 -0800285 buffer[1] = 0x04;
286 }
287 /* set to the correct mode (analog or digital) */
Michael Krufkydb8a6952007-08-21 01:24:42 -0300288 tuneraddr = priv->i2c_props.addr;
289 priv->i2c_props.addr = 0x0a;
290 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,&buffer[0],2)))
Kirk Laprayfde6d312005-11-08 21:38:18 -0800291 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300292 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,&buffer[2],2)))
Kirk Laprayfde6d312005-11-08 21:38:18 -0800293 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300294 priv->i2c_props.addr = tuneraddr;
Kirk Lapraye976f9372005-11-08 21:37:04 -0800295 /* FIXME: input */
296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Michael Krufky6b1dde92007-08-11 15:42:12 -0300299 if (params->cb_first_if_lower_freq && div < priv->last_div) {
Michael Krufky3fc46d32006-01-23 17:11:11 -0200300 buffer[0] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200301 buffer[1] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 buffer[2] = (div>>8) & 0x7f;
303 buffer[3] = div & 0xff;
304 } else {
305 buffer[0] = (div>>8) & 0x7f;
306 buffer[1] = div & 0xff;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200307 buffer[2] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200308 buffer[3] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Michael Krufky6b1dde92007-08-11 15:42:12 -0300310 priv->last_div = div;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300311 if (params->has_tda9887) {
312 int config = 0;
313 int is_secam_l = (t->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
314 !(t->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
315
316 if (t->std == V4L2_STD_SECAM_LC) {
317 if (params->port1_active ^ params->port1_invert_for_secam_lc)
318 config |= TDA9887_PORT1_ACTIVE;
319 if (params->port2_active ^ params->port2_invert_for_secam_lc)
320 config |= TDA9887_PORT2_ACTIVE;
321 }
322 else {
323 if (params->port1_active)
324 config |= TDA9887_PORT1_ACTIVE;
325 if (params->port2_active)
326 config |= TDA9887_PORT2_ACTIVE;
327 }
328 if (params->intercarrier_mode)
329 config |= TDA9887_INTERCARRIER;
330 if (is_secam_l) {
331 if (i == 0 && params->default_top_secam_low)
332 config |= TDA9887_TOP(params->default_top_secam_low);
333 else if (i == 1 && params->default_top_secam_mid)
334 config |= TDA9887_TOP(params->default_top_secam_mid);
335 else if (params->default_top_secam_high)
336 config |= TDA9887_TOP(params->default_top_secam_high);
337 }
338 else {
339 if (i == 0 && params->default_top_low)
340 config |= TDA9887_TOP(params->default_top_low);
341 else if (i == 1 && params->default_top_mid)
342 config |= TDA9887_TOP(params->default_top_mid);
343 else if (params->default_top_high)
344 config |= TDA9887_TOP(params->default_top_high);
345 }
Trent Piephod7304de2006-08-24 22:43:45 -0300346 if (params->default_pll_gating_18)
347 config |= TDA9887_GATING_18;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300348 i2c_clients_command(priv->i2c_props.adap, TDA9887_SET_CONFIG, &config);
Hans Verkuilba8fc392006-06-25 15:34:39 -0300349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
351 buffer[0],buffer[1],buffer[2],buffer[3]);
352
Michael Krufkydb8a6952007-08-21 01:24:42 -0300353 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
355
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300356 switch (t->type) {
357 case TUNER_LG_TDVS_H06XF:
358 /* Set the Auxiliary Byte. */
359 buffer[0] = buffer[2];
360 buffer[0] &= ~0x20;
361 buffer[0] |= 0x18;
362 buffer[1] = 0x20;
363 tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
364
Michael Krufkydb8a6952007-08-21 01:24:42 -0300365 if (2 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,2)))
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300366 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
367 break;
368 case TUNER_MICROTUNE_4042FI5:
369 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 // FIXME - this may also work for other tuners
371 unsigned long timeout = jiffies + msecs_to_jiffies(1);
372 u8 status_byte = 0;
373
374 /* Wait until the PLL locks */
375 for (;;) {
376 if (time_after(jiffies,timeout))
377 return;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300378 if (1 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props,&status_byte,1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
380 break;
381 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700382 if (status_byte & TUNER_PLL_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
384 udelay(10);
385 }
386
387 /* Set the charge pump for optimized phase noise figure */
Michael Krufky3fc46d32006-01-23 17:11:11 -0200388 config &= ~TUNER_CHARGE_PUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 buffer[0] = (div>>8) & 0x7f;
390 buffer[1] = div & 0xff;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200391 buffer[2] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200392 buffer[3] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300394 buffer[0],buffer[1],buffer[2],buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Michael Krufkydb8a6952007-08-21 01:24:42 -0300396 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300398 break;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401}
402
Michael Krufkydb8a6952007-08-21 01:24:42 -0300403static void default_set_radio_freq(struct tuner *t, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct tunertype *tun;
Michael Krufky6b1dde92007-08-11 15:42:12 -0300406 struct tuner_simple_priv *priv = t->priv;
Hans Verkuil27487d42006-01-15 15:04:52 -0200407 u8 buffer[4];
408 u16 div;
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200409 int rc, j;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300410 struct tuner_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700412 tun = &tuners[t->type];
Michael Krufky476d63d2006-02-07 06:25:34 -0200413
Trent Piepho5e082f12007-08-03 18:32:38 -0300414 for (j = tun->count-1; j > 0; j--)
415 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
416 break;
417 /* default params (j=0) will be used if desired type wasn't found */
Hans Verkuilba8fc392006-06-25 15:34:39 -0300418 params = &tun->params[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Trent Piepho5e082f12007-08-03 18:32:38 -0300420 /* Select Radio 1st IF used */
421 switch (params->radio_if) {
422 case 0: /* 10.7 MHz */
423 freq += (unsigned int)(10.7*16000);
424 break;
425 case 1: /* 33.3 MHz */
426 freq += (unsigned int)(33.3*16000);
427 break;
428 case 2: /* 41.3 MHz */
429 freq += (unsigned int)(41.3*16000);
430 break;
431 default:
432 tuner_warn("Unsupported radio_if value %d\n", params->radio_if);
433 return;
434 }
435
436 /* Bandswitch byte */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 switch (t->type) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700438 case TUNER_TENA_9533_DI:
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700439 case TUNER_YMEC_TVF_5533MF:
Trent Piepho5e082f12007-08-03 18:32:38 -0300440 tuner_dbg ("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 case TUNER_PHILIPS_FM1216ME_MK3:
443 case TUNER_PHILIPS_FM1236_MK3:
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700444 case TUNER_PHILIPS_FMD1216ME_MK3:
Hans Verkuil122b5db2006-12-03 06:45:07 -0300445 case TUNER_LG_NTSC_TAPE:
Trent Piepho5e082f12007-08-03 18:32:38 -0300446 case TUNER_PHILIPS_FM1256_IH3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 buffer[3] = 0x19;
448 break;
Mauro Carvalho Chehabefcf55c2006-03-09 11:17:43 -0300449 case TUNER_TNF_5335MF:
450 buffer[3] = 0x11;
451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case TUNER_LG_PAL_FM:
453 buffer[3] = 0xa5;
454 break;
Trent Piepho5e082f12007-08-03 18:32:38 -0300455 case TUNER_THOMSON_DTT761X:
456 buffer[3] = 0x39;
Mauro Carvalho Chehab33ac6b52005-09-09 13:03:56 -0700457 break;
Trent Piepho5e082f12007-08-03 18:32:38 -0300458 case TUNER_MICROTUNE_4049FM5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 default:
460 buffer[3] = 0xa4;
461 break;
462 }
Trent Piepho5e082f12007-08-03 18:32:38 -0300463
464 buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) |
465 TUNER_RATIO_SELECT_50; /* 50 kHz step */
466
467 /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
468 freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
469 freq * (1/800) */
470 div = (freq + 400) / 800;
471
Michael Krufky6b1dde92007-08-11 15:42:12 -0300472 if (params->cb_first_if_lower_freq && div < priv->last_div) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200473 buffer[0] = buffer[2];
474 buffer[1] = buffer[3];
475 buffer[2] = (div>>8) & 0x7f;
476 buffer[3] = div & 0xff;
477 } else {
478 buffer[0] = (div>>8) & 0x7f;
479 buffer[1] = div & 0xff;
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
483 buffer[0],buffer[1],buffer[2],buffer[3]);
Michael Krufky6b1dde92007-08-11 15:42:12 -0300484 priv->last_div = div;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Hans Verkuilba8fc392006-06-25 15:34:39 -0300486 if (params->has_tda9887) {
487 int config = 0;
488 if (params->port1_active && !params->port1_fm_high_sensitivity)
489 config |= TDA9887_PORT1_ACTIVE;
490 if (params->port2_active && !params->port2_fm_high_sensitivity)
491 config |= TDA9887_PORT2_ACTIVE;
492 if (params->intercarrier_mode)
493 config |= TDA9887_INTERCARRIER;
494/* if (params->port1_set_for_fm_mono)
495 config &= ~TDA9887_PORT1_ACTIVE;*/
Mauro Carvalho Chehab483deb02006-12-04 08:31:38 -0300496 if (params->fm_gain_normal)
497 config |= TDA9887_GAIN_NORMAL;
Trent Piepho5e082f12007-08-03 18:32:38 -0300498 if (params->radio_if == 2)
499 config |= TDA9887_RIF_41_3;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300500 i2c_clients_command(priv->i2c_props.adap, TDA9887_SET_CONFIG, &config);
Hans Verkuilba8fc392006-06-25 15:34:39 -0300501 }
Michael Krufkydb8a6952007-08-21 01:24:42 -0300502 if (4 != (rc = tuner_i2c_xfer_send(&priv->i2c_props,buffer,4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
504}
505
Michael Krufkydb8a6952007-08-21 01:24:42 -0300506static void tuner_release(struct tuner *t)
Michael Krufky6b1dde92007-08-11 15:42:12 -0300507{
Michael Krufky6b1dde92007-08-11 15:42:12 -0300508 kfree(t->priv);
509 t->priv = NULL;
510}
511
Michael Krufky5d807c92007-06-06 16:17:57 -0300512static struct tuner_operations simple_tuner_ops = {
513 .set_tv_freq = default_set_tv_freq,
514 .set_radio_freq = default_set_radio_freq,
515 .has_signal = tuner_signal,
516 .is_stereo = tuner_stereo,
Michael Krufky6b1dde92007-08-11 15:42:12 -0300517 .release = tuner_release,
Michael Krufky5d807c92007-06-06 16:17:57 -0300518};
519
Michael Krufkydb8a6952007-08-21 01:24:42 -0300520int default_tuner_init(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Michael Krufky6b1dde92007-08-11 15:42:12 -0300522 struct tuner_simple_priv *priv = NULL;
523
524 priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
525 if (priv == NULL)
526 return -ENOMEM;
527 t->priv = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Michael Krufkydb8a6952007-08-21 01:24:42 -0300529 priv->i2c_props.addr = t->i2c.addr;
530 priv->i2c_props.adap = t->i2c.adapter;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 tuner_info("type set to %d (%s)\n",
533 t->type, tuners[t->type].name);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300534 strlcpy(t->i2c.name, tuners[t->type].name, sizeof(t->i2c.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Michael Krufky5d807c92007-06-06 16:17:57 -0300536 memcpy(&t->ops, &simple_tuner_ops, sizeof(struct tuner_operations));
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539}
540
541/*
542 * Overrides for Emacs so that we follow Linus's tabbing style.
543 * ---------------------------------------------------------------------------
544 * Local variables:
545 * c-basic-offset: 8
546 * End:
547 */