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);
68 ret = regmap_read(dev->regmap, 0x80ed03, &utmp);
72 else if (utmp < ARRAY_SIZE(nv))
78 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
79 while (!time_after(jiffies, timeout)) {
80 ret = regmap_bulk_read(dev->regmap, 0x80ed23, buf, 2);
84 utmp = (buf[1] << 8) | (buf[0] << 0);
89 dev_dbg(&pdev->dev, "r_fbc_m_bdry took %u ms, val %u\n",
90 jiffies_to_msecs(jiffies) -
91 (jiffies_to_msecs(timeout) - TIMEOUT), utmp);
93 dev->fn_min = dev->xtal * utmp;
94 dev->fn_min /= (dev->fdiv * nv_val);
96 dev_dbg(&pdev->dev, "fn_min %u\n", dev->fn_min);
99 * Chip version BX never sets that flag so we just wait 50ms in that
100 * case. It is possible poll BX similarly than AX and then timeout in
101 * order to get 50ms delay, but that causes about 120 extra I2C
102 * messages. As for now, we just wait and reduce IO.
104 if (dev->chip_ver == 1) {
106 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
107 while (!time_after(jiffies, timeout)) {
108 ret = regmap_read(dev->regmap, 0x80ec82, &utmp);
116 dev_dbg(&pdev->dev, "p_tsm_init_mode took %u ms, val %u\n",
117 jiffies_to_msecs(jiffies) -
118 (jiffies_to_msecs(timeout) - TIMEOUT), utmp);
123 ret = regmap_write(dev->regmap, 0x80ed81, iqik_m_cal);
127 ret = regmap_write(dev->regmap, 0x80ec57, 0x00);
131 ret = regmap_write(dev->regmap, 0x80ec58, 0x00);
135 ret = regmap_write(dev->regmap, 0x80ec40, 0x01);
143 dev_dbg(&pdev->dev, "failed %d\n", ret);
147 static int it913x_sleep(struct dvb_frontend *fe)
149 struct it913x_dev *dev = fe->tuner_priv;
150 struct platform_device *pdev = dev->pdev;
153 dev_dbg(&pdev->dev, "role %u\n", dev->role);
157 ret = regmap_bulk_write(dev->regmap, 0x80ec40, "\x00", 1);
162 * Writing '0x00' to master tuner register '0x80ec08' causes slave tuner
163 * communication lost. Due to that, we cannot put master full sleep.
165 if (dev->role == IT913X_ROLE_DUAL_MASTER)
170 dev_dbg(&pdev->dev, "role %u, len %d\n", dev->role, len);
172 ret = regmap_bulk_write(dev->regmap, 0x80ec02,
173 "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
178 ret = regmap_bulk_write(dev->regmap, 0x80ec12, "\x00\x00\x00\x00", 4);
182 ret = regmap_bulk_write(dev->regmap, 0x80ec17,
183 "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9);
187 ret = regmap_bulk_write(dev->regmap, 0x80ec22,
188 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 10);
192 ret = regmap_bulk_write(dev->regmap, 0x80ec20, "\x00", 1);
196 ret = regmap_bulk_write(dev->regmap, 0x80ec3f, "\x01", 1);
202 dev_dbg(&pdev->dev, "failed %d\n", ret);
206 static int it913x_set_params(struct dvb_frontend *fe)
208 struct it913x_dev *dev = fe->tuner_priv;
209 struct platform_device *pdev = dev->pdev;
210 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
213 u32 pre_lo_freq, t_cal_freq;
214 u16 iqik_m_cal, n_div;
215 u8 u8tmp, n, l_band, lna_band;
217 dev_dbg(&pdev->dev, "role=%u, frequency %u, bandwidth_hz %u\n",
218 dev->role, c->frequency, c->bandwidth_hz);
225 if (c->frequency <= 74000000) {
228 } else if (c->frequency <= 111000000) {
231 } else if (c->frequency <= 148000000) {
234 } else if (c->frequency <= 222000000) {
237 } else if (c->frequency <= 296000000) {
240 } else if (c->frequency <= 445000000) {
243 } else if (c->frequency <= dev->fn_min) {
246 } else if (c->frequency <= 950000000) {
254 ret = regmap_read(dev->regmap, 0x80ed81, &utmp);
258 iqik_m_cal = utmp * n_div;
261 if (dev->clk_mode == 0)
262 iqik_m_cal = (iqik_m_cal * 9) >> 5;
266 iqik_m_cal = 0x40 - iqik_m_cal;
267 if (dev->clk_mode == 0)
268 iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
270 iqik_m_cal = ~(iqik_m_cal >> 1);
273 t_cal_freq = (c->frequency / 1000) * n_div * dev->fdiv;
274 pre_lo_freq = t_cal_freq / dev->xtal;
275 utmp = pre_lo_freq * dev->xtal;
277 if ((t_cal_freq - utmp) >= (dev->xtal >> 1))
280 pre_lo_freq += (u32) n << 13;
281 /* Frequency OMEGA_IQIK_M_CAL_MID*/
282 t_cal_freq = pre_lo_freq + (u32)iqik_m_cal;
283 dev_dbg(&pdev->dev, "t_cal_freq %u, pre_lo_freq %u\n",
284 t_cal_freq, pre_lo_freq);
286 if (c->frequency <= 440000000) {
289 } else if (c->frequency <= 484000000) {
292 } else if (c->frequency <= 533000000) {
295 } else if (c->frequency <= 587000000) {
298 } else if (c->frequency <= 645000000) {
301 } else if (c->frequency <= 710000000) {
304 } else if (c->frequency <= 782000000) {
307 } else if (c->frequency <= 860000000) {
310 } else if (c->frequency <= 1492000000) {
313 } else if (c->frequency <= 1685000000) {
321 /* XXX: latest windows driver does not set that at all */
322 ret = regmap_write(dev->regmap, 0x80ee06, lna_band);
326 if (c->bandwidth_hz <= 5000000)
328 else if (c->bandwidth_hz <= 6000000)
330 else if (c->bandwidth_hz <= 7000000)
333 u8tmp = 6; /* 8000000 */
335 ret = regmap_write(dev->regmap, 0x80ec56, u8tmp);
339 /* XXX: latest windows driver sets different value (a8 != 68) */
340 ret = regmap_write(dev->regmap, 0x80ec4c, 0xa0 | (l_band << 3));
344 ret = regmap_write(dev->regmap, 0x80ec4d, (t_cal_freq >> 0) & 0xff);
348 ret = regmap_write(dev->regmap, 0x80ec4e, (t_cal_freq >> 8) & 0xff);
352 ret = regmap_write(dev->regmap, 0x80011e, (pre_lo_freq >> 0) & 0xff);
356 ret = regmap_write(dev->regmap, 0x80011f, (pre_lo_freq >> 8) & 0xff);
362 dev_dbg(&pdev->dev, "failed %d\n", ret);
366 static const struct dvb_tuner_ops it913x_tuner_ops = {
368 .name = "ITE IT913X",
369 .frequency_min_hz = 174 * MHz,
370 .frequency_max_hz = 862 * MHz,
374 .sleep = it913x_sleep,
375 .set_params = it913x_set_params,
378 static int it913x_probe(struct platform_device *pdev)
380 struct it913x_platform_data *pdata = pdev->dev.platform_data;
381 struct dvb_frontend *fe = pdata->fe;
382 struct it913x_dev *dev;
383 const struct platform_device_id *id = platform_get_device_id(pdev);
387 dev = kzalloc(sizeof(struct it913x_dev), GFP_KERNEL);
390 dev_err(&pdev->dev, "kzalloc() failed\n");
395 dev->regmap = pdata->regmap;
397 dev->chip_ver = id->driver_data;
398 dev->role = pdata->role;
400 fe->tuner_priv = dev;
401 memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops,
402 sizeof(struct dvb_tuner_ops));
403 platform_set_drvdata(pdev, dev);
405 if (dev->chip_ver == 1)
407 else if (dev->chip_ver == 2)
412 dev_info(&pdev->dev, "ITE IT913X %s successfully attached\n",
414 dev_dbg(&pdev->dev, "chip_ver %u, role %u\n", dev->chip_ver, dev->role);
417 dev_dbg(&pdev->dev, "failed %d\n", ret);
421 static int it913x_remove(struct platform_device *pdev)
423 struct it913x_dev *dev = platform_get_drvdata(pdev);
424 struct dvb_frontend *fe = dev->fe;
426 dev_dbg(&pdev->dev, "\n");
428 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
429 fe->tuner_priv = NULL;
435 static const struct platform_device_id it913x_id_table[] = {
436 {"it9133ax-tuner", 1},
437 {"it9133bx-tuner", 2},
440 MODULE_DEVICE_TABLE(platform, it913x_id_table);
442 static struct platform_driver it913x_driver = {
445 .suppress_bind_attrs = true,
447 .probe = it913x_probe,
448 .remove = it913x_remove,
449 .id_table = it913x_id_table,
452 module_platform_driver(it913x_driver);
454 MODULE_DESCRIPTION("ITE IT913X silicon tuner driver");
455 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
456 MODULE_LICENSE("GPL");