blob: bc6a67768af1ed7a7ecb35b6af176895bf69e533 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -08002
3 i2c tv tuner chip device driver
4 controls the philips tda8290+75 tuner chip combo.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Michael Krufky910bb3e2007-08-27 21:22:20 -030019
20 This "tda8290" module was split apart from the original "tuner" module.
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080021*/
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/delay.h>
Hans Verkuil7f6adea2009-02-19 17:31:17 -030026#include <linux/videodev2.h>
Michael Krufkyab166052007-12-09 02:26:48 -030027#include "tuner-i2c.h"
Michael Krufky910bb3e2007-08-27 21:22:20 -030028#include "tda8290.h"
Michael Krufky746d97322007-08-25 19:08:45 -030029#include "tda827x.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030030#include "tda18271.h"
Michael Krufky910bb3e2007-08-27 21:22:20 -030031
Michael Krufkyab166052007-12-09 02:26:48 -030032static int debug;
33module_param(debug, int, 0644);
Michael Krufky910bb3e2007-08-27 21:22:20 -030034MODULE_PARM_DESC(debug, "enable verbose debug messages");
35
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -030036static int deemphasis_50;
Henk Vergonetb450b922009-09-27 18:19:58 -030037module_param(deemphasis_50, int, 0644);
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -030038MODULE_PARM_DESC(deemphasis_50, "0 - 75us deemphasis; 1 - 50us deemphasis");
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* ---------------------------------------------------------------------- */
41
Michael Krufkyb2083192007-05-29 22:54:06 -030042struct tda8290_priv {
Michael Krufkydb8a6952007-08-21 01:24:42 -030043 struct tuner_i2c_props i2c_props;
44
Michael Krufkyb2083192007-05-29 22:54:06 -030045 unsigned char tda8290_easy_mode;
Michael Krufky746d97322007-08-25 19:08:45 -030046
Michael Krufkyb2083192007-05-29 22:54:06 -030047 unsigned char tda827x_addr;
Michael Krufky8c125f2c2007-10-27 02:00:57 -030048
49 unsigned char ver;
50#define TDA8290 1
51#define TDA8295 2
52#define TDA8275 4
53#define TDA8275A 8
54#define TDA18271 16
Michael Krufky910bb3e2007-08-27 21:22:20 -030055
Michael Krufky746d97322007-08-25 19:08:45 -030056 struct tda827x_config cfg;
Michael Krufkyb2083192007-05-29 22:54:06 -030057};
58
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080059/*---------------------------------------------------------------------*/
60
Michael Krufkya72dd302007-10-24 09:30:17 -030061static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080062{
Michael Krufky4e9154b2007-10-21 19:39:50 -030063 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -030064
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080065 unsigned char enable[2] = { 0x21, 0xC0 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -020066 unsigned char disable[2] = { 0x21, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080067 unsigned char *msg;
Michael Krufkya72dd302007-10-24 09:30:17 -030068
69 if (close) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080070 msg = enable;
Michael Krufkydb8a6952007-08-21 01:24:42 -030071 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080072 /* let the bridge stabilize */
73 msleep(20);
74 } else {
75 msg = disable;
Michael Krufkydb8a6952007-08-21 01:24:42 -030076 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080077 }
Michael Krufkya72dd302007-10-24 09:30:17 -030078
79 return 0;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080080}
81
Michael Krufkya72dd302007-10-24 09:30:17 -030082static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
Michael Krufky5bea1cd2007-10-22 09:56:38 -030083{
Michael Krufky4e9154b2007-10-21 19:39:50 -030084 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -030085
86 unsigned char enable[2] = { 0x45, 0xc1 };
87 unsigned char disable[2] = { 0x46, 0x00 };
88 unsigned char buf[3] = { 0x45, 0x01, 0x00 };
89 unsigned char *msg;
Michael Krufkya72dd302007-10-24 09:30:17 -030090
Michael Krufky5bea1cd2007-10-22 09:56:38 -030091 if (close) {
92 msg = enable;
93 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
94 /* let the bridge stabilize */
95 msleep(20);
96 } else {
97 msg = disable;
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -030098 tuner_i2c_xfer_send_recv(&priv->i2c_props, msg, 1, &msg[1], 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -030099
100 buf[2] = msg[1];
101 buf[2] &= ~0x04;
102 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
103 msleep(5);
104
105 msg[1] |= 0x04;
106 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
107 }
Michael Krufkya72dd302007-10-24 09:30:17 -0300108
109 return 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300110}
111
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800112/*---------------------------------------------------------------------*/
113
Michael Krufkyc7919d52007-12-08 17:06:30 -0300114static void set_audio(struct dvb_frontend *fe,
115 struct analog_parameters *params)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800116{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300117 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300118 char* mode;
119
Michael Krufkyc7919d52007-12-08 17:06:30 -0300120 if (params->std & V4L2_STD_MN) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300121 priv->tda8290_easy_mode = 0x01;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300122 mode = "MN";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300123 } else if (params->std & V4L2_STD_B) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300124 priv->tda8290_easy_mode = 0x02;
125 mode = "B";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300126 } else if (params->std & V4L2_STD_GH) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300127 priv->tda8290_easy_mode = 0x04;
128 mode = "GH";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300129 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300130 priv->tda8290_easy_mode = 0x08;
131 mode = "I";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300132 } else if (params->std & V4L2_STD_DK) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300133 priv->tda8290_easy_mode = 0x10;
134 mode = "DK";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300135 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300136 priv->tda8290_easy_mode = 0x20;
137 mode = "L";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300138 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300139 priv->tda8290_easy_mode = 0x40;
140 mode = "LC";
141 } else {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300142 priv->tda8290_easy_mode = 0x10;
143 mode = "xx";
144 }
145
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300146 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufky3f76cf82010-01-10 18:13:33 -0300147 /* Set TDA8295 to FM radio; Start TDA8290 with MN values */
148 priv->tda8290_easy_mode = (priv->ver & TDA8295) ? 0x80 : 0x01;
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300149 tuner_dbg("setting to radio FM\n");
150 } else {
151 tuner_dbg("setting tda829x to system %s\n", mode);
152 }
Michael Krufky910bb3e2007-08-27 21:22:20 -0300153}
154
Mauro Carvalho Chehab4c27f1a2009-01-05 01:23:50 -0300155static struct {
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300156 unsigned char seq[2];
157} fm_mode[] = {
158 { { 0x01, 0x81} }, /* Put device into expert mode */
159 { { 0x03, 0x48} }, /* Disable NOTCH and VIDEO filters */
160 { { 0x04, 0x04} }, /* Disable color carrier filter (SSIF) */
161 { { 0x05, 0x04} }, /* ADC headroom */
162 { { 0x06, 0x10} }, /* group delay flat */
163
164 { { 0x07, 0x00} }, /* use the same radio DTO values as a tda8295 */
165 { { 0x08, 0x00} },
166 { { 0x09, 0x80} },
167 { { 0x0a, 0xda} },
168 { { 0x0b, 0x4b} },
169 { { 0x0c, 0x68} },
170
171 { { 0x0d, 0x00} }, /* PLL off, no video carrier detect */
172 { { 0x14, 0x00} }, /* disable auto mute if no video */
173};
174
Michael Krufkyc7919d52007-12-08 17:06:30 -0300175static void tda8290_set_params(struct dvb_frontend *fe,
176 struct analog_parameters *params)
Michael Krufky910bb3e2007-08-27 21:22:20 -0300177{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300178 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky4e9154b2007-10-21 19:39:50 -0300179
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800180 unsigned char soft_reset[] = { 0x00, 0x00 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300181 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800182 unsigned char expert_mode[] = { 0x01, 0x80 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200183 unsigned char agc_out_on[] = { 0x02, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800184 unsigned char gainset_off[] = { 0x28, 0x14 };
185 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
186 unsigned char adc_head_6[] = { 0x05, 0x04 };
187 unsigned char adc_head_9[] = { 0x05, 0x02 };
188 unsigned char adc_head_12[] = { 0x05, 0x01 };
189 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
190 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
191 unsigned char gainset_2[] = { 0x28, 0x64 };
192 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
193 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
194 unsigned char if_agc_set[] = { 0x0f, 0x81 };
195 unsigned char addr_adc_sat = 0x1a;
196 unsigned char addr_agc_stat = 0x1d;
197 unsigned char addr_pll_stat = 0x1b;
198 unsigned char adc_sat, agc_stat,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800199 pll_stat;
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300200 int i;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800201
Michael Krufkyc7919d52007-12-08 17:06:30 -0300202 set_audio(fe, params);
Michael Krufky746d97322007-08-25 19:08:45 -0300203
Michael Krufkyab166052007-12-09 02:26:48 -0300204 if (priv->cfg.config)
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300205 tuner_dbg("tda827xa config is 0x%02x\n", priv->cfg.config);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300206 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
207 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
208 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800209 msleep(1);
210
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300211 if (params->mode == V4L2_TUNER_RADIO) {
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300212 unsigned char deemphasis[] = { 0x13, 1 };
213
214 /* FIXME: allow using a different deemphasis */
215
216 if (deemphasis_50)
217 deemphasis[1] = 2;
218
219 for (i = 0; i < ARRAY_SIZE(fm_mode); i++)
220 tuner_i2c_xfer_send(&priv->i2c_props, fm_mode[i].seq, 2);
221
222 tuner_i2c_xfer_send(&priv->i2c_props, deemphasis, 2);
223 } else {
224 expert_mode[1] = priv->tda8290_easy_mode + 0x80;
225 tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
226 tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
227 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
228 if (priv->tda8290_easy_mode & 0x60)
229 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
230 else
231 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
232 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
233 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800234
Mauro Carvalho Chehabecb71d22011-01-14 12:03:03 -0300235
Michael Krufky4e9154b2007-10-21 19:39:50 -0300236 tda8290_i2c_bridge(fe, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300237
Michael Krufky4e9154b2007-10-21 19:39:50 -0300238 if (fe->ops.tuner_ops.set_analog_params)
Michael Krufkyc7919d52007-12-08 17:06:30 -0300239 fe->ops.tuner_ops.set_analog_params(fe, params);
Michael Krufky746d97322007-08-25 19:08:45 -0300240
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300241 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300242 tuner_i2c_xfer_send_recv(&priv->i2c_props,
243 &addr_pll_stat, 1, &pll_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300244 if (pll_stat & 0x80) {
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300245 tuner_i2c_xfer_send_recv(&priv->i2c_props,
246 &addr_adc_sat, 1,
247 &adc_sat, 1);
248 tuner_i2c_xfer_send_recv(&priv->i2c_props,
249 &addr_agc_stat, 1,
250 &agc_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300251 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
252 break;
253 } else {
254 tuner_dbg("tda8290 not locked, no signal?\n");
255 msleep(100);
256 }
257 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800258 /* adjust headroom resp. gain */
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800259 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
260 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
261 agc_stat, adc_sat, pll_stat & 0x80);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300262 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800263 msleep(100);
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300264 tuner_i2c_xfer_send_recv(&priv->i2c_props,
265 &addr_agc_stat, 1, &agc_stat, 1);
266 tuner_i2c_xfer_send_recv(&priv->i2c_props,
267 &addr_pll_stat, 1, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800268 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800269 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
270 agc_stat, pll_stat & 0x80);
Michael Krufky746d97322007-08-25 19:08:45 -0300271 if (priv->cfg.agcf)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300272 priv->cfg.agcf(fe);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800273 msleep(100);
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300274 tuner_i2c_xfer_send_recv(&priv->i2c_props,
275 &addr_agc_stat, 1,
276 &agc_stat, 1);
277 tuner_i2c_xfer_send_recv(&priv->i2c_props,
278 &addr_pll_stat, 1,
279 &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800280 if((agc_stat > 115) || !(pll_stat & 0x80)) {
281 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300282 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
283 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800284 msleep(100);
285 }
286 }
287 }
288
289 /* l/ l' deadlock? */
Michael Krufkyb2083192007-05-29 22:54:06 -0300290 if(priv->tda8290_easy_mode & 0x60) {
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300291 tuner_i2c_xfer_send_recv(&priv->i2c_props,
292 &addr_adc_sat, 1,
293 &adc_sat, 1);
294 tuner_i2c_xfer_send_recv(&priv->i2c_props,
295 &addr_pll_stat, 1,
296 &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800297 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800298 tuner_dbg("trying to resolve SECAM L deadlock\n");
Michael Krufkydb8a6952007-08-21 01:24:42 -0300299 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800300 msleep(40);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300301 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800302 }
303 }
304
Michael Krufky4e9154b2007-10-21 19:39:50 -0300305 tda8290_i2c_bridge(fe, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300306 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800307}
308
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800309/*---------------------------------------------------------------------*/
310
Michael Krufky4e9154b2007-10-21 19:39:50 -0300311static void tda8295_power(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300312{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300313 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300314 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
315
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300316 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300317
318 if (enable)
319 buf[1] = 0x01;
320 else
321 buf[1] = 0x03;
322
323 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
324}
325
Michael Krufky4e9154b2007-10-21 19:39:50 -0300326static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300327{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300328 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300329 unsigned char buf[] = { 0x01, 0x00 };
330
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300331 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300332
333 if (enable)
334 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
335 else
336 buf[1] = 0x00; /* reset active bit */
337
338 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
339}
340
Michael Krufky4e9154b2007-10-21 19:39:50 -0300341static void tda8295_set_video_std(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300342{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300343 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300344 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
345
346 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
347
Michael Krufky4e9154b2007-10-21 19:39:50 -0300348 tda8295_set_easy_mode(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300349 msleep(20);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300350 tda8295_set_easy_mode(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300351}
352
353/*---------------------------------------------------------------------*/
354
Michael Krufky4e9154b2007-10-21 19:39:50 -0300355static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300356{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300357 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300358 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
359
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300360 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300361
362 if (enable)
363 buf[1] &= ~0x40;
364 else
365 buf[1] |= 0x40;
366
367 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
368}
369
Michael Krufky4e9154b2007-10-21 19:39:50 -0300370static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300371{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300372 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300373 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
374 unsigned char set_gpio_val[] = { 0x46, 0x00 };
375
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300376 tuner_i2c_xfer_send_recv(&priv->i2c_props,
377 &set_gpio_cf[0], 1, &set_gpio_cf[1], 1);
378 tuner_i2c_xfer_send_recv(&priv->i2c_props,
379 &set_gpio_val[0], 1, &set_gpio_val[1], 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300380
381 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
382
383 if (enable) {
384 set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
385 set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
386 }
387 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
388 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
389}
390
Michael Krufky4e9154b2007-10-21 19:39:50 -0300391static int tda8295_has_signal(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300392{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300393 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300394
395 unsigned char hvpll_stat = 0x26;
396 unsigned char ret;
397
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300398 tuner_i2c_xfer_send_recv(&priv->i2c_props, &hvpll_stat, 1, &ret, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300399 return (ret & 0x01) ? 65535 : 0;
400}
401
402/*---------------------------------------------------------------------*/
403
Michael Krufkyc7919d52007-12-08 17:06:30 -0300404static void tda8295_set_params(struct dvb_frontend *fe,
405 struct analog_parameters *params)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300406{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300407 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300408
409 unsigned char blanking_mode[] = { 0x1d, 0x00 };
410
Michael Krufkyc7919d52007-12-08 17:06:30 -0300411 set_audio(fe, params);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300412
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300413 tuner_dbg("%s: freq = %d\n", __func__, params->frequency);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300414
Michael Krufky4e9154b2007-10-21 19:39:50 -0300415 tda8295_power(fe, 1);
416 tda8295_agc1_out(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300417
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300418 tuner_i2c_xfer_send_recv(&priv->i2c_props,
419 &blanking_mode[0], 1, &blanking_mode[1], 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300420
Michael Krufky4e9154b2007-10-21 19:39:50 -0300421 tda8295_set_video_std(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300422
423 blanking_mode[1] = 0x03;
424 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
425 msleep(20);
426
Michael Krufky4e9154b2007-10-21 19:39:50 -0300427 tda8295_i2c_bridge(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300428
Michael Krufky4e9154b2007-10-21 19:39:50 -0300429 if (fe->ops.tuner_ops.set_analog_params)
Michael Krufkyc7919d52007-12-08 17:06:30 -0300430 fe->ops.tuner_ops.set_analog_params(fe, params);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300431
432 if (priv->cfg.agcf)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300433 priv->cfg.agcf(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300434
Michael Krufky4e9154b2007-10-21 19:39:50 -0300435 if (tda8295_has_signal(fe))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300436 tuner_dbg("tda8295 is locked\n");
437 else
438 tuner_dbg("tda8295 not locked, no signal?\n");
439
Michael Krufky4e9154b2007-10-21 19:39:50 -0300440 tda8295_i2c_bridge(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300441}
442
443/*---------------------------------------------------------------------*/
444
Michael Krufky4e9154b2007-10-21 19:39:50 -0300445static int tda8290_has_signal(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300447 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 unsigned char i2c_get_afc[1] = { 0x1B };
450 unsigned char afc = 0;
451
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300452 tuner_i2c_xfer_send_recv(&priv->i2c_props,
453 i2c_get_afc, ARRAY_SIZE(i2c_get_afc), &afc, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300454 return (afc & 0x80)? 65535:0;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300455}
456
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800457/*---------------------------------------------------------------------*/
458
Michael Krufky4e9154b2007-10-21 19:39:50 -0300459static void tda8290_standby(struct dvb_frontend *fe)
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700460{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300461 struct tda8290_priv *priv = fe->analog_demod_priv;
462
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800463 unsigned char cb1[] = { 0x30, 0xD0 };
464 unsigned char tda8290_standby[] = { 0x00, 0x02 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200465 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300466 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800467
Michael Krufky4e9154b2007-10-21 19:39:50 -0300468 tda8290_i2c_bridge(fe, 1);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300469 if (priv->ver & TDA8275A)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800470 cb1[1] = 0x90;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300471 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300472 tda8290_i2c_bridge(fe, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300473 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
474 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700475}
476
Michael Krufky4e9154b2007-10-21 19:39:50 -0300477static void tda8295_standby(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300478{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300479 tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300480
Michael Krufky4e9154b2007-10-21 19:39:50 -0300481 tda8295_power(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300482}
483
Michael Krufky4e9154b2007-10-21 19:39:50 -0300484static void tda8290_init_if(struct dvb_frontend *fe)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800485{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300486 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300487
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800488 unsigned char set_VS[] = { 0x30, 0x6F };
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300489 unsigned char set_GP00_CF[] = { 0x20, 0x01 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800490 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
491
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300492 if ((priv->cfg.config == 1) || (priv->cfg.config == 2))
Michael Krufkydb8a6952007-08-21 01:24:42 -0300493 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300494 else
Michael Krufkydb8a6952007-08-21 01:24:42 -0300495 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
496 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800497}
498
Michael Krufky4e9154b2007-10-21 19:39:50 -0300499static void tda8295_init_if(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300500{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300501 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300502
503 static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
504 static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
505 static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
506 static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
507 static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
508 static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
509 static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
510
Michael Krufky4e9154b2007-10-21 19:39:50 -0300511 tda8295_power(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300512
Michael Krufky4e9154b2007-10-21 19:39:50 -0300513 tda8295_set_easy_mode(fe, 0);
514 tda8295_set_video_std(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300515
516 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
517 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
518 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
519 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
520 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
521 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
522 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
523
Michael Krufky4e9154b2007-10-21 19:39:50 -0300524 tda8295_agc1_out(fe, 0);
525 tda8295_agc2_out(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300526}
527
Michael Krufky4e9154b2007-10-21 19:39:50 -0300528static void tda8290_init_tuner(struct dvb_frontend *fe)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800529{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300530 struct tda8290_priv *priv = fe->analog_demod_priv;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800531 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800532 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800533 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800534 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
Michael Krufkyb2083192007-05-29 22:54:06 -0300535 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
Ricardo Cerqueirac2f6f9d2005-11-08 21:37:51 -0800536 .buf=tda8275_init, .len = 14};
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300537 if (priv->ver & TDA8275A)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800538 msg.buf = tda8275a_init;
539
Michael Krufky4e9154b2007-10-21 19:39:50 -0300540 tda8290_i2c_bridge(fe, 1);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300541 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300542 tda8290_i2c_bridge(fe, 0);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800543}
544
545/*---------------------------------------------------------------------*/
546
Michael Krufky4e9154b2007-10-21 19:39:50 -0300547static void tda829x_release(struct dvb_frontend *fe)
Michael Krufky024cf532007-06-04 15:20:11 -0300548{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300549 struct tda8290_priv *priv = fe->analog_demod_priv;
550
Michael Krufky006ed1e2008-01-09 10:44:27 -0300551 /* only try to release the tuner if we've
552 * attached it from within this module */
553 if (priv->ver & (TDA18271 | TDA8275 | TDA8275A))
Michael Krufkya4f263b2008-01-06 15:52:56 -0300554 if (fe->ops.tuner_ops.release)
555 fe->ops.tuner_ops.release(fe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300556
Michael Krufky4e9154b2007-10-21 19:39:50 -0300557 kfree(fe->analog_demod_priv);
558 fe->analog_demod_priv = NULL;
Michael Krufky024cf532007-06-04 15:20:11 -0300559}
560
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300561static struct tda18271_config tda829x_tda18271_config = {
562 .gate = TDA18271_GATE_ANALOG,
563};
564
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300565static int tda829x_find_tuner(struct dvb_frontend *fe)
566{
567 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300568 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300569 int i, ret, tuners_found;
570 u32 tuner_addrs;
571 u8 data;
572 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
573
Mauro Carvalho Chehab31063812009-02-08 08:42:29 -0300574 if (!analog_ops->i2c_gate_ctrl) {
575 printk(KERN_ERR "tda8290: no gate control were provided!\n");
576
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300577 return -EINVAL;
Mauro Carvalho Chehab31063812009-02-08 08:42:29 -0300578 }
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300579
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300580 analog_ops->i2c_gate_ctrl(fe, 1);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300581
582 /* probe for tuner chip */
583 tuners_found = 0;
584 tuner_addrs = 0;
585 for (i = 0x60; i <= 0x63; i++) {
586 msg.addr = i;
587 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
588 if (ret == 1) {
589 tuners_found++;
590 tuner_addrs = (tuner_addrs << 8) + i;
591 }
592 }
593 /* if there is more than one tuner, we expect the right one is
594 behind the bridge and we choose the highest address that doesn't
595 give a response now
596 */
597
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300598 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300599
600 if (tuners_found > 1)
601 for (i = 0; i < tuners_found; i++) {
602 msg.addr = tuner_addrs & 0xff;
603 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
604 if (ret == 1)
605 tuner_addrs = tuner_addrs >> 8;
606 else
607 break;
608 }
609
610 if (tuner_addrs == 0) {
611 tuner_addrs = 0x60;
612 tuner_info("could not clearly identify tuner address, "
613 "defaulting to %x\n", tuner_addrs);
614 } else {
615 tuner_addrs = tuner_addrs & 0xff;
616 tuner_info("setting tuner address to %x\n", tuner_addrs);
617 }
618 priv->tda827x_addr = tuner_addrs;
619 msg.addr = tuner_addrs;
620
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300621 analog_ops->i2c_gate_ctrl(fe, 1);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300622 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
623
624 if (ret != 1) {
625 tuner_warn("tuner access failed!\n");
Mauro Carvalho Chehab31063812009-02-08 08:42:29 -0300626 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300627 return -EREMOTEIO;
628 }
629
Michael Krufky255b5112008-01-01 22:52:09 -0300630 if ((data == 0x83) || (data == 0x84)) {
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300631 priv->ver |= TDA18271;
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300632 tda829x_tda18271_config.config = priv->cfg.config;
Michael Krufkya07c8772008-04-29 03:54:19 -0300633 dvb_attach(tda18271_attach, fe, priv->tda827x_addr,
634 priv->i2c_props.adap, &tda829x_tda18271_config);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300635 } else {
636 if ((data & 0x3c) == 0)
637 priv->ver |= TDA8275;
638 else
639 priv->ver |= TDA8275A;
640
Michael Krufkya07c8772008-04-29 03:54:19 -0300641 dvb_attach(tda827x_attach, fe, priv->tda827x_addr,
642 priv->i2c_props.adap, &priv->cfg);
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300643 priv->cfg.switch_addr = priv->i2c_props.addr;
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300644 }
Michael Krufky68816472007-12-08 16:25:41 -0300645 if (fe->ops.tuner_ops.init)
646 fe->ops.tuner_ops.init(fe);
Michael Krufky63c25482007-11-23 15:08:11 -0300647
Michael Krufky68816472007-12-08 16:25:41 -0300648 if (fe->ops.tuner_ops.sleep)
649 fe->ops.tuner_ops.sleep(fe);
Michael Krufky63c25482007-11-23 15:08:11 -0300650
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300651 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300652
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300653 return 0;
654}
655
Michael Krufkyf1f32842007-11-03 22:14:54 -0300656static int tda8290_probe(struct tuner_i2c_props *i2c_props)
657{
658#define TDA8290_ID 0x89
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300659 u8 reg = 0x1f, id;
660 struct i2c_msg msg_read[] = {
661 { .addr = 0x4b, .flags = 0, .len = 1, .buf = &reg },
662 { .addr = 0x4b, .flags = I2C_M_RD, .len = 1, .buf = &id },
663 };
Michael Krufkyf1f32842007-11-03 22:14:54 -0300664
665 /* detect tda8290 */
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300666 if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
667 printk(KERN_WARNING "%s: tda8290 couldn't read register 0x%02x\n",
668 __func__, reg);
669 return -ENODEV;
670 }
Michael Krufkyf1f32842007-11-03 22:14:54 -0300671
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300672 if (id == TDA8290_ID) {
Michael Krufkyab166052007-12-09 02:26:48 -0300673 if (debug)
Michael Krufkyf1f32842007-11-03 22:14:54 -0300674 printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300675 __func__, i2c_adapter_id(i2c_props->adap),
Michael Krufkyf1f32842007-11-03 22:14:54 -0300676 i2c_props->addr);
677 return 0;
678 }
Michael Krufky1f3a4e322007-11-04 10:51:28 -0300679 return -ENODEV;
Michael Krufkyf1f32842007-11-03 22:14:54 -0300680}
681
682static int tda8295_probe(struct tuner_i2c_props *i2c_props)
683{
684#define TDA8295_ID 0x8a
Michael Krufky19f8a6c2009-12-31 04:32:29 -0300685#define TDA8295C2_ID 0x8b
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300686 u8 reg = 0x2f, id;
687 struct i2c_msg msg_read[] = {
688 { .addr = 0x4b, .flags = 0, .len = 1, .buf = &reg },
689 { .addr = 0x4b, .flags = I2C_M_RD, .len = 1, .buf = &id },
690 };
Michael Krufkyf1f32842007-11-03 22:14:54 -0300691
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300692 /* detect tda8290 */
693 if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
694 printk(KERN_WARNING "%s: tda8290 couldn't read register 0x%02x\n",
695 __func__, reg);
696 return -ENODEV;
697 }
Michael Krufkyf1f32842007-11-03 22:14:54 -0300698
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300699 if ((id & 0xfe) == TDA8295_ID) {
Michael Krufkyab166052007-12-09 02:26:48 -0300700 if (debug)
Michael Krufky19f8a6c2009-12-31 04:32:29 -0300701 printk(KERN_DEBUG "%s: %s detected @ %d-%04x\n",
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300702 __func__, (id == TDA8295_ID) ?
Michael Krufky19f8a6c2009-12-31 04:32:29 -0300703 "tda8295c1" : "tda8295c2",
704 i2c_adapter_id(i2c_props->adap),
Michael Krufkyf1f32842007-11-03 22:14:54 -0300705 i2c_props->addr);
706 return 0;
707 }
708
Michael Krufky1f3a4e322007-11-04 10:51:28 -0300709 return -ENODEV;
Michael Krufkyf1f32842007-11-03 22:14:54 -0300710}
711
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300712static struct analog_demod_ops tda8290_ops = {
Michael Krufkyc7919d52007-12-08 17:06:30 -0300713 .set_params = tda8290_set_params,
Michael Krufky746d97322007-08-25 19:08:45 -0300714 .has_signal = tda8290_has_signal,
715 .standby = tda8290_standby,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300716 .release = tda829x_release,
Michael Krufkya72dd302007-10-24 09:30:17 -0300717 .i2c_gate_ctrl = tda8290_i2c_bridge,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300718};
719
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300720static struct analog_demod_ops tda8295_ops = {
Michael Krufkyc7919d52007-12-08 17:06:30 -0300721 .set_params = tda8295_set_params,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300722 .has_signal = tda8295_has_signal,
723 .standby = tda8295_standby,
724 .release = tda829x_release,
Michael Krufkya72dd302007-10-24 09:30:17 -0300725 .i2c_gate_ctrl = tda8295_i2c_bridge,
Michael Krufky7fd8b262007-06-06 16:15:15 -0300726};
727
Michael Krufkyab166052007-12-09 02:26:48 -0300728struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
729 struct i2c_adapter *i2c_adap, u8 i2c_addr,
730 struct tda829x_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Michael Krufkyb2083192007-05-29 22:54:06 -0300732 struct tda8290_priv *priv = NULL;
Michael Krufkyab166052007-12-09 02:26:48 -0300733 char *name;
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300734
Michael Krufkyb2083192007-05-29 22:54:06 -0300735 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
736 if (priv == NULL)
Michael Krufkyab166052007-12-09 02:26:48 -0300737 return NULL;
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300738 fe->analog_demod_priv = priv;
Michael Krufkyb2083192007-05-29 22:54:06 -0300739
Michael Krufkyab166052007-12-09 02:26:48 -0300740 priv->i2c_props.addr = i2c_addr;
741 priv->i2c_props.adap = i2c_adap;
Michael Krufky27566652008-04-22 14:41:53 -0300742 priv->i2c_props.name = "tda829x";
Michael Krufkyd7cba042008-09-12 13:31:45 -0300743 if (cfg)
Michael Krufkyab166052007-12-09 02:26:48 -0300744 priv->cfg.config = cfg->lna_cfg;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300745
Michael Krufkyf1f32842007-11-03 22:14:54 -0300746 if (tda8290_probe(&priv->i2c_props) == 0) {
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300747 priv->ver = TDA8290;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300748 memcpy(&fe->ops.analog_ops, &tda8290_ops,
749 sizeof(struct analog_demod_ops));
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800750 }
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300751
Michael Krufkyf1f32842007-11-03 22:14:54 -0300752 if (tda8295_probe(&priv->i2c_props) == 0) {
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300753 priv->ver = TDA8295;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300754 memcpy(&fe->ops.analog_ops, &tda8295_ops,
755 sizeof(struct analog_demod_ops));
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800756 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800757
Mauro Carvalho Chehab9d700a02011-01-13 14:01:39 -0300758 if (!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) {
759 tda8295_power(fe, 1);
760 if (tda829x_find_tuner(fe) < 0)
761 goto fail;
Mauro Carvalho Chehab47ab2852011-01-13 12:02:00 -0300762 }
Michael Krufky746d97322007-08-25 19:08:45 -0300763
Michael Krufkyab166052007-12-09 02:26:48 -0300764 switch (priv->ver) {
Michael Krufkyc9076272007-12-24 04:36:14 -0300765 case TDA8290:
766 name = "tda8290";
767 break;
768 case TDA8295:
769 name = "tda8295";
770 break;
Michael Krufkyab166052007-12-09 02:26:48 -0300771 case TDA8290 | TDA8275:
772 name = "tda8290+75";
773 break;
774 case TDA8295 | TDA8275:
775 name = "tda8295+75";
776 break;
777 case TDA8290 | TDA8275A:
778 name = "tda8290+75a";
779 break;
780 case TDA8295 | TDA8275A:
781 name = "tda8295+75a";
782 break;
783 case TDA8290 | TDA18271:
784 name = "tda8290+18271";
785 break;
786 case TDA8295 | TDA18271:
787 name = "tda8295+18271";
788 break;
789 default:
790 goto fail;
791 }
792 tuner_info("type set to %s\n", name);
793
Michael Krufky0f2ce982008-01-21 10:55:37 -0300794 fe->ops.analog_ops.info.name = name;
795
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300796 if (priv->ver & TDA8290) {
Michael Krufky439b72b2009-01-05 18:25:04 -0300797 if (priv->ver & (TDA8275 | TDA8275A))
798 tda8290_init_tuner(fe);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300799 tda8290_init_if(fe);
800 } else if (priv->ver & TDA8295)
801 tda8295_init_if(fe);
Michael Krufky746d97322007-08-25 19:08:45 -0300802
Michael Krufkyab166052007-12-09 02:26:48 -0300803 return fe;
Michael Krufkyfa746aee2007-12-09 05:16:10 -0300804
805fail:
Mauro Carvalho Chehab9d700a02011-01-13 14:01:39 -0300806 memset(&fe->ops.analog_ops, 0, sizeof(struct analog_demod_ops));
807
Michael Krufkyfa746aee2007-12-09 05:16:10 -0300808 tda829x_release(fe);
Michael Krufkyab166052007-12-09 02:26:48 -0300809 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300811EXPORT_SYMBOL_GPL(tda829x_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Michael Krufkyab166052007-12-09 02:26:48 -0300813int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
Hartmut Hackmann95736032005-11-08 21:38:00 -0800814{
Michael Krufky910bb3e2007-08-27 21:22:20 -0300815 struct tuner_i2c_props i2c_props = {
Michael Krufkyab166052007-12-09 02:26:48 -0300816 .adap = i2c_adap,
817 .addr = i2c_addr,
Michael Krufky910bb3e2007-08-27 21:22:20 -0300818 };
Michael Krufkydb8a6952007-08-21 01:24:42 -0300819
Hartmut Hackmann44fd06f2006-02-27 00:09:11 -0300820 unsigned char soft_reset[] = { 0x00, 0x00 };
821 unsigned char easy_mode_b[] = { 0x01, 0x02 };
822 unsigned char easy_mode_g[] = { 0x01, 0x04 };
823 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
Hartmut Hackmann95736032005-11-08 21:38:00 -0800824 unsigned char addr_dto_lsb = 0x07;
825 unsigned char data;
Michael Krufkya818e1c2007-11-04 11:03:22 -0300826#define PROBE_BUFFER_SIZE 8
827 unsigned char buf[PROBE_BUFFER_SIZE];
828 int i;
829
830 /* rule out tda9887, which would return the same byte repeatedly */
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300831 tuner_i2c_xfer_send_recv(&i2c_props,
832 soft_reset, 1, buf, PROBE_BUFFER_SIZE);
Michael Krufkya818e1c2007-11-04 11:03:22 -0300833 for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
Michael Krufkybbe1e0b2007-11-05 09:54:42 -0300834 if (buf[i] != buf[0])
835 break;
Michael Krufkya818e1c2007-11-04 11:03:22 -0300836 }
837
838 /* all bytes are equal, not a tda829x - probably a tda9887 */
839 if (i == PROBE_BUFFER_SIZE)
840 return -ENODEV;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800841
Michael Krufkyf1f32842007-11-03 22:14:54 -0300842 if ((tda8290_probe(&i2c_props) == 0) ||
843 (tda8295_probe(&i2c_props) == 0))
844 return 0;
845
846 /* fall back to old probing method */
Michael Krufky910bb3e2007-08-27 21:22:20 -0300847 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
848 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300849 tuner_i2c_xfer_send_recv(&i2c_props, &addr_dto_lsb, 1, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800850 if (data == 0) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300851 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
852 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
Mauro Carvalho Chehab567aba02011-01-13 11:58:36 -0300853 tuner_i2c_xfer_send_recv(&i2c_props,
854 &addr_dto_lsb, 1, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800855 if (data == 0x7b) {
856 return 0;
857 }
858 }
Michael Krufky910bb3e2007-08-27 21:22:20 -0300859 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
Michael Krufky1f3a4e322007-11-04 10:51:28 -0300860 return -ENODEV;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800861}
Michael Krufkyf1f32842007-11-03 22:14:54 -0300862EXPORT_SYMBOL_GPL(tda829x_probe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300863
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300864MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300865MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
Michael Krufky910bb3e2007-08-27 21:22:20 -0300866MODULE_LICENSE("GPL");
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/*
869 * Overrides for Emacs so that we follow Linus's tabbing style.
870 * ---------------------------------------------------------------------------
871 * Local variables:
872 * c-basic-offset: 8
873 * End:
874 */