2 * ITE IT913X silicon tuner driver
4 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
5 * IT9137 Copyright (C) ITE Tech Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
24 #include <linux/regmap.h>
27 struct i2c_client
*client
;
28 struct regmap
*regmap
;
29 struct dvb_frontend
*fe
;
39 static int it913x_init(struct dvb_frontend
*fe
)
41 struct it913x_dev
*dev
= fe
->tuner_priv
;
44 u8 iqik_m_cal
, nv_val
, buf
[2];
45 static const u8 nv
[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
46 unsigned long timeout
;
48 dev_dbg(&dev
->client
->dev
, "role %u\n", dev
->role
);
50 ret
= regmap_write(dev
->regmap
, 0x80ec4c, 0x68);
54 usleep_range(10000, 100000);
56 ret
= regmap_read(dev
->regmap
, 0x80ec86, &utmp
);
76 dev_err(&dev
->client
->dev
, "unknown clock identifier %d\n", utmp
);
80 ret
= regmap_read(dev
->regmap
, 0x80ed03, &utmp
);
84 else if (utmp
< ARRAY_SIZE(nv
))
90 timeout
= jiffies
+ msecs_to_jiffies(TIMEOUT
);
91 while (!time_after(jiffies
, timeout
)) {
92 ret
= regmap_bulk_read(dev
->regmap
, 0x80ed23, buf
, 2);
96 utmp
= (buf
[1] << 8) | (buf
[0] << 0);
101 dev_dbg(&dev
->client
->dev
, "r_fbc_m_bdry took %u ms, val %u\n",
102 jiffies_to_msecs(jiffies
) -
103 (jiffies_to_msecs(timeout
) - TIMEOUT
), utmp
);
105 dev
->fn_min
= dev
->xtal
* utmp
;
106 dev
->fn_min
/= (dev
->fdiv
* nv_val
);
108 dev_dbg(&dev
->client
->dev
, "fn_min %u\n", dev
->fn_min
);
111 * Chip version BX never sets that flag so we just wait 50ms in that
112 * case. It is possible poll BX similarly than AX and then timeout in
113 * order to get 50ms delay, but that causes about 120 extra I2C
114 * messages. As for now, we just wait and reduce IO.
116 if (dev
->chip_ver
== 1) {
118 timeout
= jiffies
+ msecs_to_jiffies(TIMEOUT
);
119 while (!time_after(jiffies
, timeout
)) {
120 ret
= regmap_read(dev
->regmap
, 0x80ec82, &utmp
);
128 dev_dbg(&dev
->client
->dev
, "p_tsm_init_mode took %u ms, val %u\n",
129 jiffies_to_msecs(jiffies
) -
130 (jiffies_to_msecs(timeout
) - TIMEOUT
), utmp
);
135 ret
= regmap_write(dev
->regmap
, 0x80ed81, iqik_m_cal
);
139 ret
= regmap_write(dev
->regmap
, 0x80ec57, 0x00);
143 ret
= regmap_write(dev
->regmap
, 0x80ec58, 0x00);
147 ret
= regmap_write(dev
->regmap
, 0x80ec40, 0x01);
155 dev_dbg(&dev
->client
->dev
, "failed %d\n", ret
);
159 static int it913x_sleep(struct dvb_frontend
*fe
)
161 struct it913x_dev
*dev
= fe
->tuner_priv
;
164 dev_dbg(&dev
->client
->dev
, "role %u\n", dev
->role
);
168 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec40, "\x00", 1);
173 * Writing '0x00' to master tuner register '0x80ec08' causes slave tuner
174 * communication lost. Due to that, we cannot put master full sleep.
176 if (dev
->role
== IT913X_ROLE_DUAL_MASTER
)
181 dev_dbg(&dev
->client
->dev
, "role %u, len %d\n", dev
->role
, len
);
183 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec02,
184 "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
189 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec12, "\x00\x00\x00\x00", 4);
193 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec17,
194 "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9);
198 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec22,
199 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 10);
203 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec20, "\x00", 1);
207 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec3f, "\x01", 1);
213 dev_dbg(&dev
->client
->dev
, "failed %d\n", ret
);
217 static int it913x_set_params(struct dvb_frontend
*fe
)
219 struct it913x_dev
*dev
= fe
->tuner_priv
;
220 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
223 u32 pre_lo_freq
, t_cal_freq
;
224 u16 iqik_m_cal
, n_div
;
225 u8 u8tmp
, n
, l_band
, lna_band
;
227 dev_dbg(&dev
->client
->dev
, "role=%u, frequency %u, bandwidth_hz %u\n",
228 dev
->role
, c
->frequency
, c
->bandwidth_hz
);
235 if (c
->frequency
<= 74000000) {
238 } else if (c
->frequency
<= 111000000) {
241 } else if (c
->frequency
<= 148000000) {
244 } else if (c
->frequency
<= 222000000) {
247 } else if (c
->frequency
<= 296000000) {
250 } else if (c
->frequency
<= 445000000) {
253 } else if (c
->frequency
<= dev
->fn_min
) {
256 } else if (c
->frequency
<= 950000000) {
264 ret
= regmap_read(dev
->regmap
, 0x80ed81, &utmp
);
268 iqik_m_cal
= utmp
* n_div
;
271 if (dev
->clk_mode
== 0)
272 iqik_m_cal
= (iqik_m_cal
* 9) >> 5;
276 iqik_m_cal
= 0x40 - iqik_m_cal
;
277 if (dev
->clk_mode
== 0)
278 iqik_m_cal
= ~((iqik_m_cal
* 9) >> 5);
280 iqik_m_cal
= ~(iqik_m_cal
>> 1);
283 t_cal_freq
= (c
->frequency
/ 1000) * n_div
* dev
->fdiv
;
284 pre_lo_freq
= t_cal_freq
/ dev
->xtal
;
285 utmp
= pre_lo_freq
* dev
->xtal
;
287 if ((t_cal_freq
- utmp
) >= (dev
->xtal
>> 1))
290 pre_lo_freq
+= (u32
) n
<< 13;
291 /* Frequency OMEGA_IQIK_M_CAL_MID*/
292 t_cal_freq
= pre_lo_freq
+ (u32
)iqik_m_cal
;
293 dev_dbg(&dev
->client
->dev
, "t_cal_freq %u, pre_lo_freq %u\n",
294 t_cal_freq
, pre_lo_freq
);
296 if (c
->frequency
<= 440000000) {
299 } else if (c
->frequency
<= 484000000) {
302 } else if (c
->frequency
<= 533000000) {
305 } else if (c
->frequency
<= 587000000) {
308 } else if (c
->frequency
<= 645000000) {
311 } else if (c
->frequency
<= 710000000) {
314 } else if (c
->frequency
<= 782000000) {
317 } else if (c
->frequency
<= 860000000) {
320 } else if (c
->frequency
<= 1492000000) {
323 } else if (c
->frequency
<= 1685000000) {
331 /* XXX: latest windows driver does not set that at all */
332 ret
= regmap_write(dev
->regmap
, 0x80ee06, lna_band
);
336 if (c
->bandwidth_hz
<= 5000000)
338 else if (c
->bandwidth_hz
<= 6000000)
340 else if (c
->bandwidth_hz
<= 7000000)
343 u8tmp
= 6; /* 8000000 */
345 ret
= regmap_write(dev
->regmap
, 0x80ec56, u8tmp
);
349 /* XXX: latest windows driver sets different value (a8 != 68) */
350 ret
= regmap_write(dev
->regmap
, 0x80ec4c, 0xa0 | (l_band
<< 3));
354 ret
= regmap_write(dev
->regmap
, 0x80ec4d, (t_cal_freq
>> 0) & 0xff);
358 ret
= regmap_write(dev
->regmap
, 0x80ec4e, (t_cal_freq
>> 8) & 0xff);
362 ret
= regmap_write(dev
->regmap
, 0x80011e, (pre_lo_freq
>> 0) & 0xff);
366 ret
= regmap_write(dev
->regmap
, 0x80011f, (pre_lo_freq
>> 8) & 0xff);
372 dev_dbg(&dev
->client
->dev
, "failed %d\n", ret
);
376 static const struct dvb_tuner_ops it913x_tuner_ops
= {
378 .name
= "ITE IT913X",
379 .frequency_min
= 174000000,
380 .frequency_max
= 862000000,
384 .sleep
= it913x_sleep
,
385 .set_params
= it913x_set_params
,
388 static int it913x_probe(struct i2c_client
*client
,
389 const struct i2c_device_id
*id
)
391 struct it913x_config
*cfg
= client
->dev
.platform_data
;
392 struct dvb_frontend
*fe
= cfg
->fe
;
393 struct it913x_dev
*dev
;
396 static const struct regmap_config regmap_config
= {
401 dev
= kzalloc(sizeof(struct it913x_dev
), GFP_KERNEL
);
404 dev_err(&client
->dev
, "kzalloc() failed\n");
408 dev
->client
= client
;
410 dev
->chip_ver
= cfg
->chip_ver
;
411 dev
->role
= cfg
->role
;
412 dev
->regmap
= regmap_init_i2c(client
, ®map_config
);
413 if (IS_ERR(dev
->regmap
)) {
414 ret
= PTR_ERR(dev
->regmap
);
418 fe
->tuner_priv
= dev
;
419 memcpy(&fe
->ops
.tuner_ops
, &it913x_tuner_ops
,
420 sizeof(struct dvb_tuner_ops
));
421 i2c_set_clientdata(client
, dev
);
423 if (dev
->chip_ver
== 1)
425 else if (dev
->chip_ver
== 2)
430 dev_info(&dev
->client
->dev
, "ITE IT913X %s successfully attached\n",
432 dev_dbg(&dev
->client
->dev
, "chip_ver %u, role %u\n",
433 dev
->chip_ver
, dev
->role
);
439 dev_dbg(&client
->dev
, "failed %d\n", ret
);
443 static int it913x_remove(struct i2c_client
*client
)
445 struct it913x_dev
*dev
= i2c_get_clientdata(client
);
446 struct dvb_frontend
*fe
= dev
->fe
;
448 dev_dbg(&client
->dev
, "\n");
450 memset(&fe
->ops
.tuner_ops
, 0, sizeof(struct dvb_tuner_ops
));
451 fe
->tuner_priv
= NULL
;
452 regmap_exit(dev
->regmap
);
458 static const struct i2c_device_id it913x_id_table
[] = {
462 MODULE_DEVICE_TABLE(i2c
, it913x_id_table
);
464 static struct i2c_driver it913x_driver
= {
467 .suppress_bind_attrs
= true,
469 .probe
= it913x_probe
,
470 .remove
= it913x_remove
,
471 .id_table
= it913x_id_table
,
474 module_i2c_driver(it913x_driver
);
476 MODULE_DESCRIPTION("ITE IT913X silicon tuner driver");
477 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
478 MODULE_LICENSE("GPL");