blob: c9062ceddc719d25cb05fbe253c1f8d3dc46b8ae [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 Krufky8c125f22007-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;
98 tuner_i2c_xfer_send(&priv->i2c_props, msg, 1);
99 tuner_i2c_xfer_recv(&priv->i2c_props, &msg[1], 1);
100
101 buf[2] = msg[1];
102 buf[2] &= ~0x04;
103 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
104 msleep(5);
105
106 msg[1] |= 0x04;
107 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
108 }
Michael Krufkya72dd302007-10-24 09:30:17 -0300109
110 return 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300111}
112
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800113/*---------------------------------------------------------------------*/
114
Michael Krufkyc7919d52007-12-08 17:06:30 -0300115static void set_audio(struct dvb_frontend *fe,
116 struct analog_parameters *params)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800117{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300118 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300119 char* mode;
120
Michael Krufkyc7919d52007-12-08 17:06:30 -0300121 if (params->std & V4L2_STD_MN) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300122 priv->tda8290_easy_mode = 0x01;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300123 mode = "MN";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300124 } else if (params->std & V4L2_STD_B) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300125 priv->tda8290_easy_mode = 0x02;
126 mode = "B";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300127 } else if (params->std & V4L2_STD_GH) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300128 priv->tda8290_easy_mode = 0x04;
129 mode = "GH";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300130 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300131 priv->tda8290_easy_mode = 0x08;
132 mode = "I";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300133 } else if (params->std & V4L2_STD_DK) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300134 priv->tda8290_easy_mode = 0x10;
135 mode = "DK";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300136 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300137 priv->tda8290_easy_mode = 0x20;
138 mode = "L";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300139 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300140 priv->tda8290_easy_mode = 0x40;
141 mode = "LC";
142 } else {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300143 priv->tda8290_easy_mode = 0x10;
144 mode = "xx";
145 }
146
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300147 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufky3f76cf82010-01-10 18:13:33 -0300148 /* Set TDA8295 to FM radio; Start TDA8290 with MN values */
149 priv->tda8290_easy_mode = (priv->ver & TDA8295) ? 0x80 : 0x01;
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300150 tuner_dbg("setting to radio FM\n");
151 } else {
152 tuner_dbg("setting tda829x to system %s\n", mode);
153 }
Michael Krufky910bb3e2007-08-27 21:22:20 -0300154}
155
Mauro Carvalho Chehab4c27f1a2009-01-05 01:23:50 -0300156static struct {
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300157 unsigned char seq[2];
158} fm_mode[] = {
159 { { 0x01, 0x81} }, /* Put device into expert mode */
160 { { 0x03, 0x48} }, /* Disable NOTCH and VIDEO filters */
161 { { 0x04, 0x04} }, /* Disable color carrier filter (SSIF) */
162 { { 0x05, 0x04} }, /* ADC headroom */
163 { { 0x06, 0x10} }, /* group delay flat */
164
165 { { 0x07, 0x00} }, /* use the same radio DTO values as a tda8295 */
166 { { 0x08, 0x00} },
167 { { 0x09, 0x80} },
168 { { 0x0a, 0xda} },
169 { { 0x0b, 0x4b} },
170 { { 0x0c, 0x68} },
171
172 { { 0x0d, 0x00} }, /* PLL off, no video carrier detect */
173 { { 0x14, 0x00} }, /* disable auto mute if no video */
174};
175
Michael Krufkyc7919d52007-12-08 17:06:30 -0300176static void tda8290_set_params(struct dvb_frontend *fe,
177 struct analog_parameters *params)
Michael Krufky910bb3e2007-08-27 21:22:20 -0300178{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300179 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky4e9154b2007-10-21 19:39:50 -0300180
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800181 unsigned char soft_reset[] = { 0x00, 0x00 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300182 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800183 unsigned char expert_mode[] = { 0x01, 0x80 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200184 unsigned char agc_out_on[] = { 0x02, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800185 unsigned char gainset_off[] = { 0x28, 0x14 };
186 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
187 unsigned char adc_head_6[] = { 0x05, 0x04 };
188 unsigned char adc_head_9[] = { 0x05, 0x02 };
189 unsigned char adc_head_12[] = { 0x05, 0x01 };
190 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
191 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
192 unsigned char gainset_2[] = { 0x28, 0x64 };
193 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
194 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
195 unsigned char if_agc_set[] = { 0x0f, 0x81 };
196 unsigned char addr_adc_sat = 0x1a;
197 unsigned char addr_agc_stat = 0x1d;
198 unsigned char addr_pll_stat = 0x1b;
199 unsigned char adc_sat, agc_stat,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800200 pll_stat;
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300201 int i;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800202
Michael Krufkyc7919d52007-12-08 17:06:30 -0300203 set_audio(fe, params);
Michael Krufky746d97322007-08-25 19:08:45 -0300204
Michael Krufkyab166052007-12-09 02:26:48 -0300205 if (priv->cfg.config)
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300206 tuner_dbg("tda827xa config is 0x%02x\n", priv->cfg.config);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300207 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
208 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
209 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800210 msleep(1);
211
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300212 if (params->mode == V4L2_TUNER_RADIO) {
Mauro Carvalho Chehab9af0ef22008-12-08 10:36:57 -0300213 unsigned char deemphasis[] = { 0x13, 1 };
214
215 /* FIXME: allow using a different deemphasis */
216
217 if (deemphasis_50)
218 deemphasis[1] = 2;
219
220 for (i = 0; i < ARRAY_SIZE(fm_mode); i++)
221 tuner_i2c_xfer_send(&priv->i2c_props, fm_mode[i].seq, 2);
222
223 tuner_i2c_xfer_send(&priv->i2c_props, deemphasis, 2);
224 } else {
225 expert_mode[1] = priv->tda8290_easy_mode + 0x80;
226 tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
227 tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
228 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
229 if (priv->tda8290_easy_mode & 0x60)
230 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
231 else
232 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
233 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
234 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800235
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++) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300242 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
243 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300244 if (pll_stat & 0x80) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300245 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
246 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
247 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
248 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300249 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
250 break;
251 } else {
252 tuner_dbg("tda8290 not locked, no signal?\n");
253 msleep(100);
254 }
255 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800256 /* adjust headroom resp. gain */
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800257 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
258 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
259 agc_stat, adc_sat, pll_stat & 0x80);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300260 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800261 msleep(100);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300262 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
263 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
264 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
265 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800266 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800267 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
268 agc_stat, pll_stat & 0x80);
Michael Krufky746d97322007-08-25 19:08:45 -0300269 if (priv->cfg.agcf)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300270 priv->cfg.agcf(fe);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800271 msleep(100);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300272 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
273 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
274 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
275 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800276 if((agc_stat > 115) || !(pll_stat & 0x80)) {
277 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300278 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
279 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800280 msleep(100);
281 }
282 }
283 }
284
285 /* l/ l' deadlock? */
Michael Krufkyb2083192007-05-29 22:54:06 -0300286 if(priv->tda8290_easy_mode & 0x60) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300287 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
288 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
289 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
290 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800291 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800292 tuner_dbg("trying to resolve SECAM L deadlock\n");
Michael Krufkydb8a6952007-08-21 01:24:42 -0300293 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800294 msleep(40);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300295 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800296 }
297 }
298
Michael Krufky4e9154b2007-10-21 19:39:50 -0300299 tda8290_i2c_bridge(fe, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300300 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800301}
302
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800303/*---------------------------------------------------------------------*/
304
Michael Krufky4e9154b2007-10-21 19:39:50 -0300305static void tda8295_power(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300306{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300307 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300308 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
309
310 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
311 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
312
313 if (enable)
314 buf[1] = 0x01;
315 else
316 buf[1] = 0x03;
317
318 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
319}
320
Michael Krufky4e9154b2007-10-21 19:39:50 -0300321static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300322{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300323 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300324 unsigned char buf[] = { 0x01, 0x00 };
325
326 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
327 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
328
329 if (enable)
330 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
331 else
332 buf[1] = 0x00; /* reset active bit */
333
334 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
335}
336
Michael Krufky4e9154b2007-10-21 19:39:50 -0300337static void tda8295_set_video_std(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300338{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300339 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300340 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
341
342 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
343
Michael Krufky4e9154b2007-10-21 19:39:50 -0300344 tda8295_set_easy_mode(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300345 msleep(20);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300346 tda8295_set_easy_mode(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300347}
348
349/*---------------------------------------------------------------------*/
350
Michael Krufky4e9154b2007-10-21 19:39:50 -0300351static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300352{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300353 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300354 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
355
356 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
357 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
358
359 if (enable)
360 buf[1] &= ~0x40;
361 else
362 buf[1] |= 0x40;
363
364 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
365}
366
Michael Krufky4e9154b2007-10-21 19:39:50 -0300367static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300368{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300369 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300370 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
371 unsigned char set_gpio_val[] = { 0x46, 0x00 };
372
373 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_cf[0], 1);
374 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_cf[1], 1);
375 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_val[0], 1);
376 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_val[1], 1);
377
378 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
379
380 if (enable) {
381 set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
382 set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
383 }
384 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
385 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
386}
387
Michael Krufky4e9154b2007-10-21 19:39:50 -0300388static int tda8295_has_signal(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300389{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300390 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300391
392 unsigned char hvpll_stat = 0x26;
393 unsigned char ret;
394
395 tuner_i2c_xfer_send(&priv->i2c_props, &hvpll_stat, 1);
396 tuner_i2c_xfer_recv(&priv->i2c_props, &ret, 1);
397 return (ret & 0x01) ? 65535 : 0;
398}
399
400/*---------------------------------------------------------------------*/
401
Michael Krufkyc7919d52007-12-08 17:06:30 -0300402static void tda8295_set_params(struct dvb_frontend *fe,
403 struct analog_parameters *params)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300404{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300405 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300406
407 unsigned char blanking_mode[] = { 0x1d, 0x00 };
408
Michael Krufkyc7919d52007-12-08 17:06:30 -0300409 set_audio(fe, params);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300410
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300411 tuner_dbg("%s: freq = %d\n", __func__, params->frequency);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300412
Michael Krufky4e9154b2007-10-21 19:39:50 -0300413 tda8295_power(fe, 1);
414 tda8295_agc1_out(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300415
416 tuner_i2c_xfer_send(&priv->i2c_props, &blanking_mode[0], 1);
417 tuner_i2c_xfer_recv(&priv->i2c_props, &blanking_mode[1], 1);
418
Michael Krufky4e9154b2007-10-21 19:39:50 -0300419 tda8295_set_video_std(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300420
421 blanking_mode[1] = 0x03;
422 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
423 msleep(20);
424
Michael Krufky4e9154b2007-10-21 19:39:50 -0300425 tda8295_i2c_bridge(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300426
Michael Krufky4e9154b2007-10-21 19:39:50 -0300427 if (fe->ops.tuner_ops.set_analog_params)
Michael Krufkyc7919d52007-12-08 17:06:30 -0300428 fe->ops.tuner_ops.set_analog_params(fe, params);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300429
430 if (priv->cfg.agcf)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300431 priv->cfg.agcf(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300432
Michael Krufky4e9154b2007-10-21 19:39:50 -0300433 if (tda8295_has_signal(fe))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300434 tuner_dbg("tda8295 is locked\n");
435 else
436 tuner_dbg("tda8295 not locked, no signal?\n");
437
Michael Krufky4e9154b2007-10-21 19:39:50 -0300438 tda8295_i2c_bridge(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300439}
440
441/*---------------------------------------------------------------------*/
442
Michael Krufky4e9154b2007-10-21 19:39:50 -0300443static int tda8290_has_signal(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300445 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 unsigned char i2c_get_afc[1] = { 0x1B };
448 unsigned char afc = 0;
449
Michael Krufkydb8a6952007-08-21 01:24:42 -0300450 tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
451 tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300452 return (afc & 0x80)? 65535:0;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300453}
454
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800455/*---------------------------------------------------------------------*/
456
Michael Krufky4e9154b2007-10-21 19:39:50 -0300457static void tda8290_standby(struct dvb_frontend *fe)
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700458{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300459 struct tda8290_priv *priv = fe->analog_demod_priv;
460
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800461 unsigned char cb1[] = { 0x30, 0xD0 };
462 unsigned char tda8290_standby[] = { 0x00, 0x02 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200463 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300464 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800465
Michael Krufky4e9154b2007-10-21 19:39:50 -0300466 tda8290_i2c_bridge(fe, 1);
Michael Krufky8c125f22007-10-27 02:00:57 -0300467 if (priv->ver & TDA8275A)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800468 cb1[1] = 0x90;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300469 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300470 tda8290_i2c_bridge(fe, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300471 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
472 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700473}
474
Michael Krufky4e9154b2007-10-21 19:39:50 -0300475static void tda8295_standby(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300476{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300477 tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300478
Michael Krufky4e9154b2007-10-21 19:39:50 -0300479 tda8295_power(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300480}
481
Michael Krufky4e9154b2007-10-21 19:39:50 -0300482static void tda8290_init_if(struct dvb_frontend *fe)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800483{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300484 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300485
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800486 unsigned char set_VS[] = { 0x30, 0x6F };
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300487 unsigned char set_GP00_CF[] = { 0x20, 0x01 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800488 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
489
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300490 if ((priv->cfg.config == 1) || (priv->cfg.config == 2))
Michael Krufkydb8a6952007-08-21 01:24:42 -0300491 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300492 else
Michael Krufkydb8a6952007-08-21 01:24:42 -0300493 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
494 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800495}
496
Michael Krufky4e9154b2007-10-21 19:39:50 -0300497static void tda8295_init_if(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300498{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300499 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300500
501 static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
502 static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
503 static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
504 static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
505 static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
506 static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
507 static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
508
Michael Krufky4e9154b2007-10-21 19:39:50 -0300509 tda8295_power(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300510
Michael Krufky4e9154b2007-10-21 19:39:50 -0300511 tda8295_set_easy_mode(fe, 0);
512 tda8295_set_video_std(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300513
514 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
515 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
516 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
517 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
518 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
519 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
520 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
521
Michael Krufky4e9154b2007-10-21 19:39:50 -0300522 tda8295_agc1_out(fe, 0);
523 tda8295_agc2_out(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300524}
525
Michael Krufky4e9154b2007-10-21 19:39:50 -0300526static void tda8290_init_tuner(struct dvb_frontend *fe)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800527{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300528 struct tda8290_priv *priv = fe->analog_demod_priv;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800529 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800530 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800531 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800532 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
Michael Krufkyb2083192007-05-29 22:54:06 -0300533 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
Ricardo Cerqueirac2f6f9d2005-11-08 21:37:51 -0800534 .buf=tda8275_init, .len = 14};
Michael Krufky8c125f22007-10-27 02:00:57 -0300535 if (priv->ver & TDA8275A)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800536 msg.buf = tda8275a_init;
537
Michael Krufky4e9154b2007-10-21 19:39:50 -0300538 tda8290_i2c_bridge(fe, 1);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300539 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300540 tda8290_i2c_bridge(fe, 0);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800541}
542
543/*---------------------------------------------------------------------*/
544
Michael Krufky4e9154b2007-10-21 19:39:50 -0300545static void tda829x_release(struct dvb_frontend *fe)
Michael Krufky024cf532007-06-04 15:20:11 -0300546{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300547 struct tda8290_priv *priv = fe->analog_demod_priv;
548
Michael Krufky006ed1e2008-01-09 10:44:27 -0300549 /* only try to release the tuner if we've
550 * attached it from within this module */
551 if (priv->ver & (TDA18271 | TDA8275 | TDA8275A))
Michael Krufkya4f263b2008-01-06 15:52:56 -0300552 if (fe->ops.tuner_ops.release)
553 fe->ops.tuner_ops.release(fe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300554
Michael Krufky4e9154b2007-10-21 19:39:50 -0300555 kfree(fe->analog_demod_priv);
556 fe->analog_demod_priv = NULL;
Michael Krufky024cf532007-06-04 15:20:11 -0300557}
558
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300559static struct tda18271_config tda829x_tda18271_config = {
560 .gate = TDA18271_GATE_ANALOG,
561};
562
Michael Krufky8c125f22007-10-27 02:00:57 -0300563static int tda829x_find_tuner(struct dvb_frontend *fe)
564{
565 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300566 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
Michael Krufky8c125f22007-10-27 02:00:57 -0300567 int i, ret, tuners_found;
568 u32 tuner_addrs;
569 u8 data;
570 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
571
Mauro Carvalho Chehab31063812009-02-08 08:42:29 -0300572 if (!analog_ops->i2c_gate_ctrl) {
573 printk(KERN_ERR "tda8290: no gate control were provided!\n");
574
Michael Krufky8c125f22007-10-27 02:00:57 -0300575 return -EINVAL;
Mauro Carvalho Chehab31063812009-02-08 08:42:29 -0300576 }
Michael Krufky8c125f22007-10-27 02:00:57 -0300577
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300578 analog_ops->i2c_gate_ctrl(fe, 1);
Michael Krufky8c125f22007-10-27 02:00:57 -0300579
580 /* probe for tuner chip */
581 tuners_found = 0;
582 tuner_addrs = 0;
583 for (i = 0x60; i <= 0x63; i++) {
584 msg.addr = i;
585 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
586 if (ret == 1) {
587 tuners_found++;
588 tuner_addrs = (tuner_addrs << 8) + i;
589 }
590 }
591 /* if there is more than one tuner, we expect the right one is
592 behind the bridge and we choose the highest address that doesn't
593 give a response now
594 */
595
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300596 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f22007-10-27 02:00:57 -0300597
598 if (tuners_found > 1)
599 for (i = 0; i < tuners_found; i++) {
600 msg.addr = tuner_addrs & 0xff;
601 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
602 if (ret == 1)
603 tuner_addrs = tuner_addrs >> 8;
604 else
605 break;
606 }
607
608 if (tuner_addrs == 0) {
609 tuner_addrs = 0x60;
610 tuner_info("could not clearly identify tuner address, "
611 "defaulting to %x\n", tuner_addrs);
612 } else {
613 tuner_addrs = tuner_addrs & 0xff;
614 tuner_info("setting tuner address to %x\n", tuner_addrs);
615 }
616 priv->tda827x_addr = tuner_addrs;
617 msg.addr = tuner_addrs;
618
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300619 analog_ops->i2c_gate_ctrl(fe, 1);
Michael Krufky8c125f22007-10-27 02:00:57 -0300620 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
621
622 if (ret != 1) {
623 tuner_warn("tuner access failed!\n");
Mauro Carvalho Chehab31063812009-02-08 08:42:29 -0300624 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f22007-10-27 02:00:57 -0300625 return -EREMOTEIO;
626 }
627
Michael Krufky255b5112008-01-01 22:52:09 -0300628 if ((data == 0x83) || (data == 0x84)) {
Michael Krufky8c125f22007-10-27 02:00:57 -0300629 priv->ver |= TDA18271;
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300630 tda829x_tda18271_config.config = priv->cfg.config;
Michael Krufkya07c8772008-04-29 03:54:19 -0300631 dvb_attach(tda18271_attach, fe, priv->tda827x_addr,
632 priv->i2c_props.adap, &tda829x_tda18271_config);
Michael Krufky8c125f22007-10-27 02:00:57 -0300633 } else {
634 if ((data & 0x3c) == 0)
635 priv->ver |= TDA8275;
636 else
637 priv->ver |= TDA8275A;
638
Michael Krufkya07c8772008-04-29 03:54:19 -0300639 dvb_attach(tda827x_attach, fe, priv->tda827x_addr,
640 priv->i2c_props.adap, &priv->cfg);
Hartmut Hackmann7bff4b42008-04-22 14:46:08 -0300641 priv->cfg.switch_addr = priv->i2c_props.addr;
Michael Krufky8c125f22007-10-27 02:00:57 -0300642 }
Michael Krufky68816472007-12-08 16:25:41 -0300643 if (fe->ops.tuner_ops.init)
644 fe->ops.tuner_ops.init(fe);
Michael Krufky63c25482007-11-23 15:08:11 -0300645
Michael Krufky68816472007-12-08 16:25:41 -0300646 if (fe->ops.tuner_ops.sleep)
647 fe->ops.tuner_ops.sleep(fe);
Michael Krufky63c25482007-11-23 15:08:11 -0300648
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300649 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f22007-10-27 02:00:57 -0300650
Michael Krufky8c125f22007-10-27 02:00:57 -0300651 return 0;
652}
653
Michael Krufkyf1f32842007-11-03 22:14:54 -0300654static int tda8290_probe(struct tuner_i2c_props *i2c_props)
655{
656#define TDA8290_ID 0x89
657 unsigned char tda8290_id[] = { 0x1f, 0x00 };
658
659 /* detect tda8290 */
660 tuner_i2c_xfer_send(i2c_props, &tda8290_id[0], 1);
661 tuner_i2c_xfer_recv(i2c_props, &tda8290_id[1], 1);
662
663 if (tda8290_id[1] == TDA8290_ID) {
Michael Krufkyab166052007-12-09 02:26:48 -0300664 if (debug)
Michael Krufkyf1f32842007-11-03 22:14:54 -0300665 printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300666 __func__, i2c_adapter_id(i2c_props->adap),
Michael Krufkyf1f32842007-11-03 22:14:54 -0300667 i2c_props->addr);
668 return 0;
669 }
670
Michael Krufky1f3a4e32007-11-04 10:51:28 -0300671 return -ENODEV;
Michael Krufkyf1f32842007-11-03 22:14:54 -0300672}
673
674static int tda8295_probe(struct tuner_i2c_props *i2c_props)
675{
676#define TDA8295_ID 0x8a
Michael Krufky19f8a6c2009-12-31 04:32:29 -0300677#define TDA8295C2_ID 0x8b
Michael Krufkyf1f32842007-11-03 22:14:54 -0300678 unsigned char tda8295_id[] = { 0x2f, 0x00 };
679
680 /* detect tda8295 */
681 tuner_i2c_xfer_send(i2c_props, &tda8295_id[0], 1);
682 tuner_i2c_xfer_recv(i2c_props, &tda8295_id[1], 1);
683
Michael Krufky19f8a6c2009-12-31 04:32:29 -0300684 if ((tda8295_id[1] & 0xfe) == TDA8295_ID) {
Michael Krufkyab166052007-12-09 02:26:48 -0300685 if (debug)
Michael Krufky19f8a6c2009-12-31 04:32:29 -0300686 printk(KERN_DEBUG "%s: %s detected @ %d-%04x\n",
687 __func__, (tda8295_id[1] == TDA8295_ID) ?
688 "tda8295c1" : "tda8295c2",
689 i2c_adapter_id(i2c_props->adap),
Michael Krufkyf1f32842007-11-03 22:14:54 -0300690 i2c_props->addr);
691 return 0;
692 }
693
Michael Krufky1f3a4e32007-11-04 10:51:28 -0300694 return -ENODEV;
Michael Krufkyf1f32842007-11-03 22:14:54 -0300695}
696
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300697static struct analog_demod_ops tda8290_ops = {
Michael Krufkyc7919d52007-12-08 17:06:30 -0300698 .set_params = tda8290_set_params,
Michael Krufky746d97322007-08-25 19:08:45 -0300699 .has_signal = tda8290_has_signal,
700 .standby = tda8290_standby,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300701 .release = tda829x_release,
Michael Krufkya72dd302007-10-24 09:30:17 -0300702 .i2c_gate_ctrl = tda8290_i2c_bridge,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300703};
704
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300705static struct analog_demod_ops tda8295_ops = {
Michael Krufkyc7919d52007-12-08 17:06:30 -0300706 .set_params = tda8295_set_params,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300707 .has_signal = tda8295_has_signal,
708 .standby = tda8295_standby,
709 .release = tda829x_release,
Michael Krufkya72dd302007-10-24 09:30:17 -0300710 .i2c_gate_ctrl = tda8295_i2c_bridge,
Michael Krufky7fd8b262007-06-06 16:15:15 -0300711};
712
Michael Krufkyab166052007-12-09 02:26:48 -0300713struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
714 struct i2c_adapter *i2c_adap, u8 i2c_addr,
715 struct tda829x_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Michael Krufkyb2083192007-05-29 22:54:06 -0300717 struct tda8290_priv *priv = NULL;
Michael Krufkyab166052007-12-09 02:26:48 -0300718 char *name;
Michael Krufky8c125f22007-10-27 02:00:57 -0300719
Michael Krufkyb2083192007-05-29 22:54:06 -0300720 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
721 if (priv == NULL)
Michael Krufkyab166052007-12-09 02:26:48 -0300722 return NULL;
Michael Krufky8c125f22007-10-27 02:00:57 -0300723 fe->analog_demod_priv = priv;
Michael Krufkyb2083192007-05-29 22:54:06 -0300724
Michael Krufkyab166052007-12-09 02:26:48 -0300725 priv->i2c_props.addr = i2c_addr;
726 priv->i2c_props.adap = i2c_adap;
Michael Krufky27566652008-04-22 14:41:53 -0300727 priv->i2c_props.name = "tda829x";
Michael Krufkyd7cba042008-09-12 13:31:45 -0300728 if (cfg)
Michael Krufkyab166052007-12-09 02:26:48 -0300729 priv->cfg.config = cfg->lna_cfg;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300730
Michael Krufkyf1f32842007-11-03 22:14:54 -0300731 if (tda8290_probe(&priv->i2c_props) == 0) {
Michael Krufky8c125f22007-10-27 02:00:57 -0300732 priv->ver = TDA8290;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300733 memcpy(&fe->ops.analog_ops, &tda8290_ops,
734 sizeof(struct analog_demod_ops));
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800735 }
Michael Krufky8c125f22007-10-27 02:00:57 -0300736
Michael Krufkyf1f32842007-11-03 22:14:54 -0300737 if (tda8295_probe(&priv->i2c_props) == 0) {
Michael Krufky8c125f22007-10-27 02:00:57 -0300738 priv->ver = TDA8295;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300739 memcpy(&fe->ops.analog_ops, &tda8295_ops,
740 sizeof(struct analog_demod_ops));
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800741 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800742
Michael Krufkyc9076272007-12-24 04:36:14 -0300743 if ((!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) &&
744 (tda829x_find_tuner(fe) < 0))
Michael Krufkyfa746ae2007-12-09 05:16:10 -0300745 goto fail;
Michael Krufky746d97322007-08-25 19:08:45 -0300746
Michael Krufkyab166052007-12-09 02:26:48 -0300747 switch (priv->ver) {
Michael Krufkyc9076272007-12-24 04:36:14 -0300748 case TDA8290:
749 name = "tda8290";
750 break;
751 case TDA8295:
752 name = "tda8295";
753 break;
Michael Krufkyab166052007-12-09 02:26:48 -0300754 case TDA8290 | TDA8275:
755 name = "tda8290+75";
756 break;
757 case TDA8295 | TDA8275:
758 name = "tda8295+75";
759 break;
760 case TDA8290 | TDA8275A:
761 name = "tda8290+75a";
762 break;
763 case TDA8295 | TDA8275A:
764 name = "tda8295+75a";
765 break;
766 case TDA8290 | TDA18271:
767 name = "tda8290+18271";
768 break;
769 case TDA8295 | TDA18271:
770 name = "tda8295+18271";
771 break;
772 default:
773 goto fail;
774 }
775 tuner_info("type set to %s\n", name);
776
Michael Krufky0f2ce982008-01-21 10:55:37 -0300777 fe->ops.analog_ops.info.name = name;
778
Michael Krufky8c125f22007-10-27 02:00:57 -0300779 if (priv->ver & TDA8290) {
Michael Krufky439b72b2009-01-05 18:25:04 -0300780 if (priv->ver & (TDA8275 | TDA8275A))
781 tda8290_init_tuner(fe);
Michael Krufky8c125f22007-10-27 02:00:57 -0300782 tda8290_init_if(fe);
783 } else if (priv->ver & TDA8295)
784 tda8295_init_if(fe);
Michael Krufky746d97322007-08-25 19:08:45 -0300785
Michael Krufkyab166052007-12-09 02:26:48 -0300786 return fe;
Michael Krufkyfa746ae2007-12-09 05:16:10 -0300787
788fail:
789 tda829x_release(fe);
Michael Krufkyab166052007-12-09 02:26:48 -0300790 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
Michael Krufky8c125f22007-10-27 02:00:57 -0300792EXPORT_SYMBOL_GPL(tda829x_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Michael Krufkyab166052007-12-09 02:26:48 -0300794int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
Hartmut Hackmann95736032005-11-08 21:38:00 -0800795{
Michael Krufky910bb3e2007-08-27 21:22:20 -0300796 struct tuner_i2c_props i2c_props = {
Michael Krufkyab166052007-12-09 02:26:48 -0300797 .adap = i2c_adap,
798 .addr = i2c_addr,
Michael Krufky910bb3e2007-08-27 21:22:20 -0300799 };
Michael Krufkydb8a6952007-08-21 01:24:42 -0300800
Hartmut Hackmann44fd06f2006-02-27 00:09:11 -0300801 unsigned char soft_reset[] = { 0x00, 0x00 };
802 unsigned char easy_mode_b[] = { 0x01, 0x02 };
803 unsigned char easy_mode_g[] = { 0x01, 0x04 };
804 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
Hartmut Hackmann95736032005-11-08 21:38:00 -0800805 unsigned char addr_dto_lsb = 0x07;
806 unsigned char data;
Michael Krufkya818e1c2007-11-04 11:03:22 -0300807#define PROBE_BUFFER_SIZE 8
808 unsigned char buf[PROBE_BUFFER_SIZE];
809 int i;
810
811 /* rule out tda9887, which would return the same byte repeatedly */
812 tuner_i2c_xfer_send(&i2c_props, soft_reset, 1);
813 tuner_i2c_xfer_recv(&i2c_props, buf, PROBE_BUFFER_SIZE);
814 for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
Michael Krufkybbe1e0b2007-11-05 09:54:42 -0300815 if (buf[i] != buf[0])
816 break;
Michael Krufkya818e1c2007-11-04 11:03:22 -0300817 }
818
819 /* all bytes are equal, not a tda829x - probably a tda9887 */
820 if (i == PROBE_BUFFER_SIZE)
821 return -ENODEV;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800822
Michael Krufkyf1f32842007-11-03 22:14:54 -0300823 if ((tda8290_probe(&i2c_props) == 0) ||
824 (tda8295_probe(&i2c_props) == 0))
825 return 0;
826
827 /* fall back to old probing method */
Michael Krufky910bb3e2007-08-27 21:22:20 -0300828 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
829 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
830 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
831 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800832 if (data == 0) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300833 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
834 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
835 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
836 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800837 if (data == 0x7b) {
838 return 0;
839 }
840 }
Michael Krufky910bb3e2007-08-27 21:22:20 -0300841 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
Michael Krufky1f3a4e32007-11-04 10:51:28 -0300842 return -ENODEV;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800843}
Michael Krufkyf1f32842007-11-03 22:14:54 -0300844EXPORT_SYMBOL_GPL(tda829x_probe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300845
Michael Krufky8c125f22007-10-27 02:00:57 -0300846MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300847MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
Michael Krufky910bb3e2007-08-27 21:22:20 -0300848MODULE_LICENSE("GPL");
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*
851 * Overrides for Emacs so that we follow Linus's tabbing style.
852 * ---------------------------------------------------------------------------
853 * Local variables:
854 * c-basic-offset: 8
855 * End:
856 */