root/drivers/media/tuners/it913x.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. it913x_init
  2. it913x_sleep
  3. it913x_set_params
  4. it913x_probe
  5. it913x_remove

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * ITE IT913X silicon tuner driver
   4  *
   5  *  Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
   6  *  IT9137 Copyright (C) ITE Tech Inc.
   7  */
   8 
   9 #include "it913x.h"
  10 #include <linux/platform_device.h>
  11 #include <linux/regmap.h>
  12 
  13 struct it913x_dev {
  14         struct platform_device *pdev;
  15         struct regmap *regmap;
  16         struct dvb_frontend *fe;
  17         u8 chip_ver:2;
  18         u8 role:2;
  19         u16 xtal;
  20         u8 fdiv;
  21         u8 clk_mode;
  22         u32 fn_min;
  23         bool active;
  24 };
  25 
  26 static int it913x_init(struct dvb_frontend *fe)
  27 {
  28         struct it913x_dev *dev = fe->tuner_priv;
  29         struct platform_device *pdev = dev->pdev;
  30         int ret;
  31         unsigned int utmp;
  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;
  35 
  36         dev_dbg(&pdev->dev, "role %u\n", dev->role);
  37 
  38         ret = regmap_write(dev->regmap, 0x80ec4c, 0x68);
  39         if (ret)
  40                 goto err;
  41 
  42         usleep_range(10000, 100000);
  43 
  44         ret = regmap_read(dev->regmap, 0x80ec86, &utmp);
  45         if (ret)
  46                 goto err;
  47 
  48         switch (utmp) {
  49         case 0:
  50                 /* 12.000 MHz */
  51                 dev->clk_mode = utmp;
  52                 dev->xtal = 2000;
  53                 dev->fdiv = 3;
  54                 iqik_m_cal = 16;
  55                 break;
  56         case 1:
  57                 /* 20.480 MHz */
  58                 dev->clk_mode = utmp;
  59                 dev->xtal = 640;
  60                 dev->fdiv = 1;
  61                 iqik_m_cal = 6;
  62                 break;
  63         default:
  64                 dev_err(&pdev->dev, "unknown clock identifier %d\n", utmp);
  65                 goto err;
  66         }
  67 
  68         ret = regmap_read(dev->regmap, 0x80ed03,  &utmp);
  69         if (ret)
  70                 goto err;
  71 
  72         else if (utmp < ARRAY_SIZE(nv))
  73                 nv_val = nv[utmp];
  74         else
  75                 nv_val = 2;
  76 
  77         #define TIMEOUT 50
  78         timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  79         while (!time_after(jiffies, timeout)) {
  80                 ret = regmap_bulk_read(dev->regmap, 0x80ed23, buf, 2);
  81                 if (ret)
  82                         goto err;
  83 
  84                 utmp = (buf[1] << 8) | (buf[0] << 0);
  85                 if (utmp)
  86                         break;
  87         }
  88 
  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);
  92 
  93         dev->fn_min = dev->xtal * utmp;
  94         dev->fn_min /= (dev->fdiv * nv_val);
  95         dev->fn_min *= 1000;
  96         dev_dbg(&pdev->dev, "fn_min %u\n", dev->fn_min);
  97 
  98         /*
  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.
 103          */
 104         if (dev->chip_ver == 1) {
 105                 #define TIMEOUT 50
 106                 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
 107                 while (!time_after(jiffies, timeout)) {
 108                         ret = regmap_read(dev->regmap, 0x80ec82, &utmp);
 109                         if (ret)
 110                                 goto err;
 111 
 112                         if (utmp)
 113                                 break;
 114                 }
 115 
 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);
 119         } else {
 120                 msleep(50);
 121         }
 122 
 123         ret = regmap_write(dev->regmap, 0x80ed81, iqik_m_cal);
 124         if (ret)
 125                 goto err;
 126 
 127         ret = regmap_write(dev->regmap, 0x80ec57, 0x00);
 128         if (ret)
 129                 goto err;
 130 
 131         ret = regmap_write(dev->regmap, 0x80ec58, 0x00);
 132         if (ret)
 133                 goto err;
 134 
 135         ret = regmap_write(dev->regmap, 0x80ec40, 0x01);
 136         if (ret)
 137                 goto err;
 138 
 139         dev->active = true;
 140 
 141         return 0;
 142 err:
 143         dev_dbg(&pdev->dev, "failed %d\n", ret);
 144         return ret;
 145 }
 146 
 147 static int it913x_sleep(struct dvb_frontend *fe)
 148 {
 149         struct it913x_dev *dev = fe->tuner_priv;
 150         struct platform_device *pdev = dev->pdev;
 151         int ret, len;
 152 
 153         dev_dbg(&pdev->dev, "role %u\n", dev->role);
 154 
 155         dev->active = false;
 156 
 157         ret  = regmap_bulk_write(dev->regmap, 0x80ec40, "\x00", 1);
 158         if (ret)
 159                 goto err;
 160 
 161         /*
 162          * Writing '0x00' to master tuner register '0x80ec08' causes slave tuner
 163          * communication lost. Due to that, we cannot put master full sleep.
 164          */
 165         if (dev->role == IT913X_ROLE_DUAL_MASTER)
 166                 len = 4;
 167         else
 168                 len = 15;
 169 
 170         dev_dbg(&pdev->dev, "role %u, len %d\n", dev->role, len);
 171 
 172         ret = regmap_bulk_write(dev->regmap, 0x80ec02,
 173                         "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
 174                         len);
 175         if (ret)
 176                 goto err;
 177 
 178         ret = regmap_bulk_write(dev->regmap, 0x80ec12, "\x00\x00\x00\x00", 4);
 179         if (ret)
 180                 goto err;
 181 
 182         ret = regmap_bulk_write(dev->regmap, 0x80ec17,
 183                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9);
 184         if (ret)
 185                 goto err;
 186 
 187         ret = regmap_bulk_write(dev->regmap, 0x80ec22,
 188                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 10);
 189         if (ret)
 190                 goto err;
 191 
 192         ret = regmap_bulk_write(dev->regmap, 0x80ec20, "\x00", 1);
 193         if (ret)
 194                 goto err;
 195 
 196         ret = regmap_bulk_write(dev->regmap, 0x80ec3f, "\x01", 1);
 197         if (ret)
 198                 goto err;
 199 
 200         return 0;
 201 err:
 202         dev_dbg(&pdev->dev, "failed %d\n", ret);
 203         return ret;
 204 }
 205 
 206 static int it913x_set_params(struct dvb_frontend *fe)
 207 {
 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;
 211         int ret;
 212         unsigned int utmp;
 213         u32 pre_lo_freq, t_cal_freq;
 214         u16 iqik_m_cal, n_div;
 215         u8 u8tmp, n, l_band, lna_band;
 216 
 217         dev_dbg(&pdev->dev, "role=%u, frequency %u, bandwidth_hz %u\n",
 218                         dev->role, c->frequency, c->bandwidth_hz);
 219 
 220         if (!dev->active) {
 221                 ret = -EINVAL;
 222                 goto err;
 223         }
 224 
 225         if (c->frequency <=         74000000) {
 226                 n_div = 48;
 227                 n = 0;
 228         } else if (c->frequency <= 111000000) {
 229                 n_div = 32;
 230                 n = 1;
 231         } else if (c->frequency <= 148000000) {
 232                 n_div = 24;
 233                 n = 2;
 234         } else if (c->frequency <= 222000000) {
 235                 n_div = 16;
 236                 n = 3;
 237         } else if (c->frequency <= 296000000) {
 238                 n_div = 12;
 239                 n = 4;
 240         } else if (c->frequency <= 445000000) {
 241                 n_div = 8;
 242                 n = 5;
 243         } else if (c->frequency <= dev->fn_min) {
 244                 n_div = 6;
 245                 n = 6;
 246         } else if (c->frequency <= 950000000) {
 247                 n_div = 4;
 248                 n = 7;
 249         } else {
 250                 n_div = 2;
 251                 n = 0;
 252         }
 253 
 254         ret = regmap_read(dev->regmap, 0x80ed81, &utmp);
 255         if (ret)
 256                 goto err;
 257 
 258         iqik_m_cal = utmp * n_div;
 259 
 260         if (utmp < 0x20) {
 261                 if (dev->clk_mode == 0)
 262                         iqik_m_cal = (iqik_m_cal * 9) >> 5;
 263                 else
 264                         iqik_m_cal >>= 1;
 265         } else {
 266                 iqik_m_cal = 0x40 - iqik_m_cal;
 267                 if (dev->clk_mode == 0)
 268                         iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
 269                 else
 270                         iqik_m_cal = ~(iqik_m_cal >> 1);
 271         }
 272 
 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;
 276 
 277         if ((t_cal_freq - utmp) >= (dev->xtal >> 1))
 278                 pre_lo_freq++;
 279 
 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);
 285 
 286         if (c->frequency <=         440000000) {
 287                 l_band = 0;
 288                 lna_band = 0;
 289         } else if (c->frequency <=  484000000) {
 290                 l_band = 1;
 291                 lna_band = 1;
 292         } else if (c->frequency <=  533000000) {
 293                 l_band = 1;
 294                 lna_band = 2;
 295         } else if (c->frequency <=  587000000) {
 296                 l_band = 1;
 297                 lna_band = 3;
 298         } else if (c->frequency <=  645000000) {
 299                 l_band = 1;
 300                 lna_band = 4;
 301         } else if (c->frequency <=  710000000) {
 302                 l_band = 1;
 303                 lna_band = 5;
 304         } else if (c->frequency <=  782000000) {
 305                 l_band = 1;
 306                 lna_band = 6;
 307         } else if (c->frequency <=  860000000) {
 308                 l_band = 1;
 309                 lna_band = 7;
 310         } else if (c->frequency <= 1492000000) {
 311                 l_band = 1;
 312                 lna_band = 0;
 313         } else if (c->frequency <= 1685000000) {
 314                 l_band = 1;
 315                 lna_band = 1;
 316         } else {
 317                 ret = -EINVAL;
 318                 goto err;
 319         }
 320 
 321         /* XXX: latest windows driver does not set that at all */
 322         ret = regmap_write(dev->regmap, 0x80ee06, lna_band);
 323         if (ret)
 324                 goto err;
 325 
 326         if (c->bandwidth_hz <=      5000000)
 327                 u8tmp = 0;
 328         else if (c->bandwidth_hz <= 6000000)
 329                 u8tmp = 2;
 330         else if (c->bandwidth_hz <= 7000000)
 331                 u8tmp = 4;
 332         else
 333                 u8tmp = 6;       /* 8000000 */
 334 
 335         ret = regmap_write(dev->regmap, 0x80ec56, u8tmp);
 336         if (ret)
 337                 goto err;
 338 
 339         /* XXX: latest windows driver sets different value (a8 != 68) */
 340         ret = regmap_write(dev->regmap, 0x80ec4c, 0xa0 | (l_band << 3));
 341         if (ret)
 342                 goto err;
 343 
 344         ret = regmap_write(dev->regmap, 0x80ec4d, (t_cal_freq >> 0) & 0xff);
 345         if (ret)
 346                 goto err;
 347 
 348         ret = regmap_write(dev->regmap, 0x80ec4e, (t_cal_freq >> 8) & 0xff);
 349         if (ret)
 350                 goto err;
 351 
 352         ret = regmap_write(dev->regmap, 0x80011e, (pre_lo_freq >> 0) & 0xff);
 353         if (ret)
 354                 goto err;
 355 
 356         ret = regmap_write(dev->regmap, 0x80011f, (pre_lo_freq >> 8) & 0xff);
 357         if (ret)
 358                 goto err;
 359 
 360         return 0;
 361 err:
 362         dev_dbg(&pdev->dev, "failed %d\n", ret);
 363         return ret;
 364 }
 365 
 366 static const struct dvb_tuner_ops it913x_tuner_ops = {
 367         .info = {
 368                 .name             = "ITE IT913X",
 369                 .frequency_min_hz = 174 * MHz,
 370                 .frequency_max_hz = 862 * MHz,
 371         },
 372 
 373         .init = it913x_init,
 374         .sleep = it913x_sleep,
 375         .set_params = it913x_set_params,
 376 };
 377 
 378 static int it913x_probe(struct platform_device *pdev)
 379 {
 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);
 384         int ret;
 385         char *chip_ver_str;
 386 
 387         dev = kzalloc(sizeof(struct it913x_dev), GFP_KERNEL);
 388         if (dev == NULL) {
 389                 ret = -ENOMEM;
 390                 dev_err(&pdev->dev, "kzalloc() failed\n");
 391                 goto err;
 392         }
 393 
 394         dev->pdev = pdev;
 395         dev->regmap = pdata->regmap;
 396         dev->fe = pdata->fe;
 397         dev->chip_ver = id->driver_data;
 398         dev->role = pdata->role;
 399 
 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);
 404 
 405         if (dev->chip_ver == 1)
 406                 chip_ver_str = "AX";
 407         else if (dev->chip_ver == 2)
 408                 chip_ver_str = "BX";
 409         else
 410                 chip_ver_str = "??";
 411 
 412         dev_info(&pdev->dev, "ITE IT913X %s successfully attached\n",
 413                  chip_ver_str);
 414         dev_dbg(&pdev->dev, "chip_ver %u, role %u\n", dev->chip_ver, dev->role);
 415         return 0;
 416 err:
 417         dev_dbg(&pdev->dev, "failed %d\n", ret);
 418         return ret;
 419 }
 420 
 421 static int it913x_remove(struct platform_device *pdev)
 422 {
 423         struct it913x_dev *dev = platform_get_drvdata(pdev);
 424         struct dvb_frontend *fe = dev->fe;
 425 
 426         dev_dbg(&pdev->dev, "\n");
 427 
 428         memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
 429         fe->tuner_priv = NULL;
 430         kfree(dev);
 431 
 432         return 0;
 433 }
 434 
 435 static const struct platform_device_id it913x_id_table[] = {
 436         {"it9133ax-tuner", 1},
 437         {"it9133bx-tuner", 2},
 438         {},
 439 };
 440 MODULE_DEVICE_TABLE(platform, it913x_id_table);
 441 
 442 static struct platform_driver it913x_driver = {
 443         .driver = {
 444                 .name   = "it913x",
 445                 .suppress_bind_attrs    = true,
 446         },
 447         .probe          = it913x_probe,
 448         .remove         = it913x_remove,
 449         .id_table       = it913x_id_table,
 450 };
 451 
 452 module_platform_driver(it913x_driver);
 453 
 454 MODULE_DESCRIPTION("ITE IT913X silicon tuner driver");
 455 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
 456 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */