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