1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ITE IT913X silicon tuner driver
5 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
6 * IT9137 Copyright (C) ITE Tech Inc.
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
14 struct platform_device
*pdev
;
15 struct regmap
*regmap
;
16 struct dvb_frontend
*fe
;
26 static int it913x_init(struct dvb_frontend
*fe
)
28 struct it913x_dev
*dev
= fe
->tuner_priv
;
29 struct platform_device
*pdev
= dev
->pdev
;
32 u8 iqik_m_cal
, nv_val
, buf
[2];
33 static const u8 nv
[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
34 unsigned long timeout
;
36 dev_dbg(&pdev
->dev
, "role %u\n", dev
->role
);
38 ret
= regmap_write(dev
->regmap
, 0x80ec4c, 0x68);
42 usleep_range(10000, 100000);
44 ret
= regmap_read(dev
->regmap
, 0x80ec86, &utmp
);
64 dev_err(&pdev
->dev
, "unknown clock identifier %d\n", utmp
);
69 ret
= regmap_read(dev
->regmap
, 0x80ed03, &utmp
);
73 else if (utmp
< ARRAY_SIZE(nv
))
79 timeout
= jiffies
+ msecs_to_jiffies(TIMEOUT
);
80 while (!time_after(jiffies
, timeout
)) {
81 ret
= regmap_bulk_read(dev
->regmap
, 0x80ed23, buf
, 2);
85 utmp
= (buf
[1] << 8) | (buf
[0] << 0);
90 dev_dbg(&pdev
->dev
, "r_fbc_m_bdry took %u ms, val %u\n",
91 jiffies_to_msecs(jiffies
) -
92 (jiffies_to_msecs(timeout
) - TIMEOUT
), utmp
);
94 dev
->fn_min
= dev
->xtal
* utmp
;
95 dev
->fn_min
/= (dev
->fdiv
* nv_val
);
97 dev_dbg(&pdev
->dev
, "fn_min %u\n", dev
->fn_min
);
100 * Chip version BX never sets that flag so we just wait 50ms in that
101 * case. It is possible poll BX similarly than AX and then timeout in
102 * order to get 50ms delay, but that causes about 120 extra I2C
103 * messages. As for now, we just wait and reduce IO.
105 if (dev
->chip_ver
== 1) {
107 timeout
= jiffies
+ msecs_to_jiffies(TIMEOUT
);
108 while (!time_after(jiffies
, timeout
)) {
109 ret
= regmap_read(dev
->regmap
, 0x80ec82, &utmp
);
117 dev_dbg(&pdev
->dev
, "p_tsm_init_mode took %u ms, val %u\n",
118 jiffies_to_msecs(jiffies
) -
119 (jiffies_to_msecs(timeout
) - TIMEOUT
), utmp
);
124 ret
= regmap_write(dev
->regmap
, 0x80ed81, iqik_m_cal
);
128 ret
= regmap_write(dev
->regmap
, 0x80ec57, 0x00);
132 ret
= regmap_write(dev
->regmap
, 0x80ec58, 0x00);
136 ret
= regmap_write(dev
->regmap
, 0x80ec40, 0x01);
144 dev_dbg(&pdev
->dev
, "failed %d\n", ret
);
148 static int it913x_sleep(struct dvb_frontend
*fe
)
150 struct it913x_dev
*dev
= fe
->tuner_priv
;
151 struct platform_device
*pdev
= dev
->pdev
;
154 dev_dbg(&pdev
->dev
, "role %u\n", dev
->role
);
158 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec40, "\x00", 1);
163 * Writing '0x00' to master tuner register '0x80ec08' causes slave tuner
164 * communication lost. Due to that, we cannot put master full sleep.
166 if (dev
->role
== IT913X_ROLE_DUAL_MASTER
)
171 dev_dbg(&pdev
->dev
, "role %u, len %d\n", dev
->role
, len
);
173 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec02,
174 "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
179 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec12, "\x00\x00\x00\x00", 4);
183 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec17,
184 "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9);
188 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec22,
189 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 10);
193 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec20, "\x00", 1);
197 ret
= regmap_bulk_write(dev
->regmap
, 0x80ec3f, "\x01", 1);
203 dev_dbg(&pdev
->dev
, "failed %d\n", ret
);
207 static int it913x_set_params(struct dvb_frontend
*fe
)
209 struct it913x_dev
*dev
= fe
->tuner_priv
;
210 struct platform_device
*pdev
= dev
->pdev
;
211 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
214 u32 pre_lo_freq
, t_cal_freq
;
215 u16 iqik_m_cal
, n_div
;
216 u8 u8tmp
, n
, l_band
, lna_band
;
218 dev_dbg(&pdev
->dev
, "role=%u, frequency %u, bandwidth_hz %u\n",
219 dev
->role
, c
->frequency
, c
->bandwidth_hz
);
226 if (c
->frequency
<= 74000000) {
229 } else if (c
->frequency
<= 111000000) {
232 } else if (c
->frequency
<= 148000000) {
235 } else if (c
->frequency
<= 222000000) {
238 } else if (c
->frequency
<= 296000000) {
241 } else if (c
->frequency
<= 445000000) {
244 } else if (c
->frequency
<= dev
->fn_min
) {
247 } else if (c
->frequency
<= 950000000) {
255 ret
= regmap_read(dev
->regmap
, 0x80ed81, &utmp
);
259 iqik_m_cal
= utmp
* n_div
;
262 if (dev
->clk_mode
== 0)
263 iqik_m_cal
= (iqik_m_cal
* 9) >> 5;
267 iqik_m_cal
= 0x40 - iqik_m_cal
;
268 if (dev
->clk_mode
== 0)
269 iqik_m_cal
= ~((iqik_m_cal
* 9) >> 5);
271 iqik_m_cal
= ~(iqik_m_cal
>> 1);
274 t_cal_freq
= (c
->frequency
/ 1000) * n_div
* dev
->fdiv
;
275 pre_lo_freq
= t_cal_freq
/ dev
->xtal
;
276 utmp
= pre_lo_freq
* dev
->xtal
;
278 if ((t_cal_freq
- utmp
) >= (dev
->xtal
>> 1))
281 pre_lo_freq
+= (u32
) n
<< 13;
282 /* Frequency OMEGA_IQIK_M_CAL_MID*/
283 t_cal_freq
= pre_lo_freq
+ (u32
)iqik_m_cal
;
284 dev_dbg(&pdev
->dev
, "t_cal_freq %u, pre_lo_freq %u\n",
285 t_cal_freq
, pre_lo_freq
);
287 if (c
->frequency
<= 440000000) {
290 } else if (c
->frequency
<= 484000000) {
293 } else if (c
->frequency
<= 533000000) {
296 } else if (c
->frequency
<= 587000000) {
299 } else if (c
->frequency
<= 645000000) {
302 } else if (c
->frequency
<= 710000000) {
305 } else if (c
->frequency
<= 782000000) {
308 } else if (c
->frequency
<= 860000000) {
311 } else if (c
->frequency
<= 1492000000) {
314 } else if (c
->frequency
<= 1685000000) {
322 /* XXX: latest windows driver does not set that at all */
323 ret
= regmap_write(dev
->regmap
, 0x80ee06, lna_band
);
327 if (c
->bandwidth_hz
<= 5000000)
329 else if (c
->bandwidth_hz
<= 6000000)
331 else if (c
->bandwidth_hz
<= 7000000)
334 u8tmp
= 6; /* 8000000 */
336 ret
= regmap_write(dev
->regmap
, 0x80ec56, u8tmp
);
340 /* XXX: latest windows driver sets different value (a8 != 68) */
341 ret
= regmap_write(dev
->regmap
, 0x80ec4c, 0xa0 | (l_band
<< 3));
345 ret
= regmap_write(dev
->regmap
, 0x80ec4d, (t_cal_freq
>> 0) & 0xff);
349 ret
= regmap_write(dev
->regmap
, 0x80ec4e, (t_cal_freq
>> 8) & 0xff);
353 ret
= regmap_write(dev
->regmap
, 0x80011e, (pre_lo_freq
>> 0) & 0xff);
357 ret
= regmap_write(dev
->regmap
, 0x80011f, (pre_lo_freq
>> 8) & 0xff);
363 dev_dbg(&pdev
->dev
, "failed %d\n", ret
);
367 static const struct dvb_tuner_ops it913x_tuner_ops
= {
369 .name
= "ITE IT913X",
370 .frequency_min_hz
= 174 * MHz
,
371 .frequency_max_hz
= 862 * MHz
,
375 .sleep
= it913x_sleep
,
376 .set_params
= it913x_set_params
,
379 static int it913x_probe(struct platform_device
*pdev
)
381 struct it913x_platform_data
*pdata
= pdev
->dev
.platform_data
;
382 struct dvb_frontend
*fe
= pdata
->fe
;
383 struct it913x_dev
*dev
;
384 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
388 dev
= kzalloc(sizeof(struct it913x_dev
), GFP_KERNEL
);
391 dev_err(&pdev
->dev
, "kzalloc() failed\n");
396 dev
->regmap
= pdata
->regmap
;
398 dev
->chip_ver
= id
->driver_data
;
399 dev
->role
= pdata
->role
;
401 fe
->tuner_priv
= dev
;
402 memcpy(&fe
->ops
.tuner_ops
, &it913x_tuner_ops
,
403 sizeof(struct dvb_tuner_ops
));
404 platform_set_drvdata(pdev
, dev
);
406 if (dev
->chip_ver
== 1)
408 else if (dev
->chip_ver
== 2)
413 dev_info(&pdev
->dev
, "ITE IT913X %s successfully attached\n",
415 dev_dbg(&pdev
->dev
, "chip_ver %u, role %u\n", dev
->chip_ver
, dev
->role
);
418 dev_dbg(&pdev
->dev
, "failed %d\n", ret
);
422 static void it913x_remove(struct platform_device
*pdev
)
424 struct it913x_dev
*dev
= platform_get_drvdata(pdev
);
425 struct dvb_frontend
*fe
= dev
->fe
;
427 dev_dbg(&pdev
->dev
, "\n");
429 memset(&fe
->ops
.tuner_ops
, 0, sizeof(struct dvb_tuner_ops
));
430 fe
->tuner_priv
= NULL
;
434 static const struct platform_device_id it913x_id_table
[] = {
435 {"it9133ax-tuner", 1},
436 {"it9133bx-tuner", 2},
439 MODULE_DEVICE_TABLE(platform
, it913x_id_table
);
441 static struct platform_driver it913x_driver
= {
444 .suppress_bind_attrs
= true,
446 .probe
= it913x_probe
,
447 .remove_new
= it913x_remove
,
448 .id_table
= it913x_id_table
,
451 module_platform_driver(it913x_driver
);
453 MODULE_DESCRIPTION("ITE IT913X silicon tuner driver");
454 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
455 MODULE_LICENSE("GPL");