Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 1 | /* |
| 2 | * ITE Tech IT9137 silicon tuner driver |
| 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. |
| 17 | * |
| 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.= |
| 21 | */ |
| 22 | |
Antti Palosaari | c2ba972 | 2014-08-26 00:08:16 -0300 | [diff] [blame] | 23 | #include "it913x_priv.h" |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 24 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 25 | struct it913x_dev { |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 26 | struct i2c_client *client; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 27 | struct regmap *regmap; |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 28 | struct dvb_frontend *fe; |
Antti Palosaari | 676c350 | 2014-08-27 03:59:27 -0300 | [diff] [blame^] | 29 | u8 chip_ver:2; |
| 30 | u8 role:2; |
Antti Palosaari | 1085979 | 2013-02-28 19:02:19 -0300 | [diff] [blame] | 31 | u8 firmware_ver; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 32 | u16 tun_xtal; |
| 33 | u8 tun_fdiv; |
| 34 | u8 tun_clk_mode; |
| 35 | u32 tun_fn_min; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 36 | }; |
| 37 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 38 | static int it913x_script_loader(struct it913x_dev *dev, |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 39 | struct it913xset *loadscript) |
| 40 | { |
| 41 | int ret, i; |
Antti Palosaari | d111324 | 2014-08-09 15:02:14 -0300 | [diff] [blame] | 42 | |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 43 | if (loadscript == NULL) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | for (i = 0; i < 1000; ++i) { |
Antti Palosaari | 8da55c9 | 2014-08-26 18:56:46 -0300 | [diff] [blame] | 47 | if (loadscript[i].address == 0x000000) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 48 | break; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 49 | ret = regmap_bulk_write(dev->regmap, loadscript[i].address, |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 50 | loadscript[i].reg, loadscript[i].count); |
| 51 | if (ret < 0) |
| 52 | return -ENODEV; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
Antti Palosaari | 42432b3 | 2013-02-26 20:57:01 -0300 | [diff] [blame] | 57 | static int it913x_init(struct dvb_frontend *fe) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 58 | { |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 59 | struct it913x_dev *dev = fe->tuner_priv; |
Antti Palosaari | 8da55c9 | 2014-08-26 18:56:46 -0300 | [diff] [blame] | 60 | int ret, i; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 61 | unsigned int reg; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 62 | u8 val, nv_val; |
| 63 | u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2}; |
| 64 | u8 b[2]; |
| 65 | |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 66 | ret = regmap_read(dev->regmap, 0x80ec86, ®); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 67 | switch (reg) { |
| 68 | case 0: |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 69 | dev->tun_clk_mode = reg; |
| 70 | dev->tun_xtal = 2000; |
| 71 | dev->tun_fdiv = 3; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 72 | val = 16; |
| 73 | break; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 74 | case 1: |
Antti Palosaari | 8da55c9 | 2014-08-26 18:56:46 -0300 | [diff] [blame] | 75 | default: /* I/O error too */ |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 76 | dev->tun_clk_mode = reg; |
| 77 | dev->tun_xtal = 640; |
| 78 | dev->tun_fdiv = 1; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 79 | val = 6; |
| 80 | break; |
| 81 | } |
| 82 | |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 83 | ret = regmap_read(dev->regmap, 0x80ed03, ®); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 84 | |
| 85 | if (reg < 0) |
| 86 | return -ENODEV; |
| 87 | else if (reg < ARRAY_SIZE(nv)) |
| 88 | nv_val = nv[reg]; |
| 89 | else |
| 90 | nv_val = 2; |
| 91 | |
| 92 | for (i = 0; i < 50; i++) { |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 93 | ret = regmap_bulk_read(dev->regmap, 0x80ed23, &b[0], sizeof(b)); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 94 | reg = (b[1] << 8) + b[0]; |
| 95 | if (reg > 0) |
| 96 | break; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 97 | if (ret) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 98 | return -ENODEV; |
| 99 | udelay(2000); |
| 100 | } |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 101 | dev->tun_fn_min = dev->tun_xtal * reg; |
| 102 | dev->tun_fn_min /= (dev->tun_fdiv * nv_val); |
| 103 | dev_dbg(&dev->client->dev, "Tuner fn_min %d\n", dev->tun_fn_min); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 104 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 105 | if (dev->chip_ver > 1) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 106 | msleep(50); |
| 107 | else { |
| 108 | for (i = 0; i < 50; i++) { |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 109 | ret = regmap_read(dev->regmap, 0x80ec82, ®); |
Antti Palosaari | 8da55c9 | 2014-08-26 18:56:46 -0300 | [diff] [blame] | 110 | if (ret < 0) |
| 111 | return -ENODEV; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 112 | if (reg > 0) |
| 113 | break; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 114 | udelay(2000); |
| 115 | } |
| 116 | } |
| 117 | |
Antti Palosaari | d19812e | 2013-02-27 23:29:02 -0300 | [diff] [blame] | 118 | /* Power Up Tuner - common all versions */ |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 119 | ret = regmap_write(dev->regmap, 0x80ec40, 0x1); |
| 120 | ret |= regmap_write(dev->regmap, 0x80ec57, 0x0); |
| 121 | ret |= regmap_write(dev->regmap, 0x80ec58, 0x0); |
Antti Palosaari | d19812e | 2013-02-27 23:29:02 -0300 | [diff] [blame] | 122 | |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 123 | return regmap_write(dev->regmap, 0x80ed81, val); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 124 | } |
| 125 | |
Antti Palosaari | 676c350 | 2014-08-27 03:59:27 -0300 | [diff] [blame^] | 126 | static int it913x_sleep(struct dvb_frontend *fe) |
| 127 | { |
| 128 | struct it913x_dev *dev = fe->tuner_priv; |
| 129 | int ret, len; |
| 130 | |
| 131 | dev_dbg(&dev->client->dev, "role=%u\n", dev->role); |
| 132 | |
| 133 | ret = regmap_bulk_write(dev->regmap, 0x80ec40, "\x00", 1); |
| 134 | if (ret) |
| 135 | goto err; |
| 136 | |
| 137 | /* |
| 138 | * Writing '0x00' to master tuner register '0x80ec08' causes slave tuner |
| 139 | * communication lost. Due to that, we cannot put master full sleep. |
| 140 | */ |
| 141 | if (dev->role == IT913X_ROLE_DUAL_MASTER) |
| 142 | len = 4; |
| 143 | else |
| 144 | len = 15; |
| 145 | |
| 146 | dev_dbg(&dev->client->dev, "role=%u len=%d\n", dev->role, len); |
| 147 | |
| 148 | ret = regmap_bulk_write(dev->regmap, 0x80ec02, |
| 149 | "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", |
| 150 | len); |
| 151 | if (ret) |
| 152 | goto err; |
| 153 | |
| 154 | ret = regmap_bulk_write(dev->regmap, 0x80ec12, "\x00\x00\x00\x00", 4); |
| 155 | if (ret) |
| 156 | goto err; |
| 157 | |
| 158 | ret = regmap_bulk_write(dev->regmap, 0x80ec17, |
| 159 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9); |
| 160 | if (ret) |
| 161 | goto err; |
| 162 | |
| 163 | ret = regmap_bulk_write(dev->regmap, 0x80ec22, |
| 164 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 10); |
| 165 | if (ret) |
| 166 | goto err; |
| 167 | |
| 168 | ret = regmap_bulk_write(dev->regmap, 0x80ec20, "\x00", 1); |
| 169 | if (ret) |
| 170 | goto err; |
| 171 | |
| 172 | ret = regmap_bulk_write(dev->regmap, 0x80ec3f, "\x01", 1); |
| 173 | if (ret) |
| 174 | goto err; |
| 175 | |
| 176 | return 0; |
| 177 | err: |
| 178 | dev_dbg(&dev->client->dev, "failed %d\n", ret); |
| 179 | return ret; |
| 180 | } |
| 181 | |
Antti Palosaari | 42432b3 | 2013-02-26 20:57:01 -0300 | [diff] [blame] | 182 | static int it9137_set_params(struct dvb_frontend *fe) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 183 | { |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 184 | struct it913x_dev *dev = fe->tuner_priv; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 185 | struct it913xset *set_tuner = set_it9137_template; |
| 186 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
| 187 | u32 bandwidth = p->bandwidth_hz; |
| 188 | u32 frequency_m = p->frequency; |
Antti Palosaari | 8da55c9 | 2014-08-26 18:56:46 -0300 | [diff] [blame] | 189 | int ret; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 190 | unsigned int reg; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 191 | u32 frequency = frequency_m / 1000; |
| 192 | u32 freq, temp_f, tmp; |
| 193 | u16 iqik_m_cal; |
| 194 | u16 n_div; |
| 195 | u8 n; |
| 196 | u8 l_band; |
| 197 | u8 lna_band; |
| 198 | u8 bw; |
| 199 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 200 | if (dev->firmware_ver == 1) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 201 | set_tuner = set_it9135_template; |
| 202 | else |
| 203 | set_tuner = set_it9137_template; |
| 204 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 205 | dev_dbg(&dev->client->dev, "Tuner Frequency %d Bandwidth %d\n", |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 206 | frequency, bandwidth); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 207 | |
| 208 | if (frequency >= 51000 && frequency <= 440000) { |
| 209 | l_band = 0; |
| 210 | lna_band = 0; |
| 211 | } else if (frequency > 440000 && frequency <= 484000) { |
| 212 | l_band = 1; |
| 213 | lna_band = 1; |
| 214 | } else if (frequency > 484000 && frequency <= 533000) { |
| 215 | l_band = 1; |
| 216 | lna_band = 2; |
| 217 | } else if (frequency > 533000 && frequency <= 587000) { |
| 218 | l_band = 1; |
| 219 | lna_band = 3; |
| 220 | } else if (frequency > 587000 && frequency <= 645000) { |
| 221 | l_band = 1; |
| 222 | lna_band = 4; |
| 223 | } else if (frequency > 645000 && frequency <= 710000) { |
| 224 | l_band = 1; |
| 225 | lna_band = 5; |
| 226 | } else if (frequency > 710000 && frequency <= 782000) { |
| 227 | l_band = 1; |
| 228 | lna_band = 6; |
| 229 | } else if (frequency > 782000 && frequency <= 860000) { |
| 230 | l_band = 1; |
| 231 | lna_band = 7; |
| 232 | } else if (frequency > 1450000 && frequency <= 1492000) { |
| 233 | l_band = 1; |
| 234 | lna_band = 0; |
| 235 | } else if (frequency > 1660000 && frequency <= 1685000) { |
| 236 | l_band = 1; |
| 237 | lna_band = 1; |
| 238 | } else |
| 239 | return -EINVAL; |
| 240 | set_tuner[0].reg[0] = lna_band; |
| 241 | |
| 242 | switch (bandwidth) { |
| 243 | case 5000000: |
| 244 | bw = 0; |
| 245 | break; |
| 246 | case 6000000: |
| 247 | bw = 2; |
| 248 | break; |
| 249 | case 7000000: |
| 250 | bw = 4; |
| 251 | break; |
| 252 | default: |
| 253 | case 8000000: |
| 254 | bw = 6; |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | set_tuner[1].reg[0] = bw; |
| 259 | set_tuner[2].reg[0] = 0xa0 | (l_band << 3); |
| 260 | |
| 261 | if (frequency > 53000 && frequency <= 74000) { |
| 262 | n_div = 48; |
| 263 | n = 0; |
| 264 | } else if (frequency > 74000 && frequency <= 111000) { |
| 265 | n_div = 32; |
| 266 | n = 1; |
| 267 | } else if (frequency > 111000 && frequency <= 148000) { |
| 268 | n_div = 24; |
| 269 | n = 2; |
| 270 | } else if (frequency > 148000 && frequency <= 222000) { |
| 271 | n_div = 16; |
| 272 | n = 3; |
| 273 | } else if (frequency > 222000 && frequency <= 296000) { |
| 274 | n_div = 12; |
| 275 | n = 4; |
| 276 | } else if (frequency > 296000 && frequency <= 445000) { |
| 277 | n_div = 8; |
| 278 | n = 5; |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 279 | } else if (frequency > 445000 && frequency <= dev->tun_fn_min) { |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 280 | n_div = 6; |
| 281 | n = 6; |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 282 | } else if (frequency > dev->tun_fn_min && frequency <= 950000) { |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 283 | n_div = 4; |
| 284 | n = 7; |
| 285 | } else if (frequency > 1450000 && frequency <= 1680000) { |
| 286 | n_div = 2; |
| 287 | n = 0; |
| 288 | } else |
| 289 | return -EINVAL; |
| 290 | |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 291 | ret = regmap_read(dev->regmap, 0x80ed81, ®); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 292 | iqik_m_cal = (u16)reg * n_div; |
| 293 | |
| 294 | if (reg < 0x20) { |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 295 | if (dev->tun_clk_mode == 0) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 296 | iqik_m_cal = (iqik_m_cal * 9) >> 5; |
| 297 | else |
| 298 | iqik_m_cal >>= 1; |
| 299 | } else { |
| 300 | iqik_m_cal = 0x40 - iqik_m_cal; |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 301 | if (dev->tun_clk_mode == 0) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 302 | iqik_m_cal = ~((iqik_m_cal * 9) >> 5); |
| 303 | else |
| 304 | iqik_m_cal = ~(iqik_m_cal >> 1); |
| 305 | } |
| 306 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 307 | temp_f = frequency * (u32)n_div * (u32)dev->tun_fdiv; |
| 308 | freq = temp_f / dev->tun_xtal; |
| 309 | tmp = freq * dev->tun_xtal; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 310 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 311 | if ((temp_f - tmp) >= (dev->tun_xtal >> 1)) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 312 | freq++; |
| 313 | |
| 314 | freq += (u32) n << 13; |
| 315 | /* Frequency OMEGA_IQIK_M_CAL_MID*/ |
| 316 | temp_f = freq + (u32)iqik_m_cal; |
| 317 | |
| 318 | set_tuner[3].reg[0] = temp_f & 0xff; |
| 319 | set_tuner[4].reg[0] = (temp_f >> 8) & 0xff; |
| 320 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 321 | dev_dbg(&dev->client->dev, "High Frequency = %04x\n", temp_f); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 322 | |
| 323 | /* Lower frequency */ |
| 324 | set_tuner[5].reg[0] = freq & 0xff; |
| 325 | set_tuner[6].reg[0] = (freq >> 8) & 0xff; |
| 326 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 327 | dev_dbg(&dev->client->dev, "low Frequency = %04x\n", freq); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 328 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 329 | ret = it913x_script_loader(dev, set_tuner); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 330 | |
| 331 | return (ret < 0) ? -ENODEV : 0; |
| 332 | } |
| 333 | |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 334 | static const struct dvb_tuner_ops it913x_tuner_ops = { |
| 335 | .info = { |
| 336 | .name = "ITE Tech IT913X", |
| 337 | .frequency_min = 174000000, |
| 338 | .frequency_max = 862000000, |
| 339 | }, |
| 340 | |
Antti Palosaari | 42432b3 | 2013-02-26 20:57:01 -0300 | [diff] [blame] | 341 | .init = it913x_init, |
| 342 | .sleep = it913x_sleep, |
| 343 | .set_params = it9137_set_params, |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 344 | }; |
| 345 | |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 346 | static int it913x_probe(struct i2c_client *client, |
| 347 | const struct i2c_device_id *id) |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 348 | { |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 349 | struct it913x_config *cfg = client->dev.platform_data; |
| 350 | struct dvb_frontend *fe = cfg->fe; |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 351 | struct it913x_dev *dev; |
Bimow Chen | 01b461b | 2014-08-05 00:14:47 -0300 | [diff] [blame] | 352 | int ret; |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 353 | char *chip_ver_str; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 354 | static const struct regmap_config regmap_config = { |
| 355 | .reg_bits = 24, |
| 356 | .val_bits = 8, |
| 357 | }; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 358 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 359 | dev = kzalloc(sizeof(struct it913x_dev), GFP_KERNEL); |
| 360 | if (dev == NULL) { |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 361 | ret = -ENOMEM; |
| 362 | dev_err(&client->dev, "kzalloc() failed\n"); |
| 363 | goto err; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 364 | } |
| 365 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 366 | dev->client = client; |
| 367 | dev->fe = cfg->fe; |
| 368 | dev->chip_ver = cfg->chip_ver; |
Antti Palosaari | 676c350 | 2014-08-27 03:59:27 -0300 | [diff] [blame^] | 369 | dev->role = cfg->role; |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 370 | dev->firmware_ver = 1; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 371 | dev->regmap = regmap_init_i2c(client, ®map_config); |
| 372 | if (IS_ERR(dev->regmap)) { |
| 373 | ret = PTR_ERR(dev->regmap); |
| 374 | goto err_kfree; |
| 375 | } |
Antti Palosaari | 44af747 | 2013-02-28 00:14:06 -0300 | [diff] [blame] | 376 | |
Bimow Chen | 01b461b | 2014-08-05 00:14:47 -0300 | [diff] [blame] | 377 | /* tuner RF initial */ |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 378 | ret = regmap_write(dev->regmap, 0x80ec4c, 0x68); |
| 379 | if (ret) |
| 380 | goto err_regmap_exit; |
Bimow Chen | 01b461b | 2014-08-05 00:14:47 -0300 | [diff] [blame] | 381 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 382 | fe->tuner_priv = dev; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 383 | memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops, |
| 384 | sizeof(struct dvb_tuner_ops)); |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 385 | i2c_set_clientdata(client, dev); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 386 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 387 | if (dev->chip_ver == 1) |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 388 | chip_ver_str = "AX"; |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 389 | else if (dev->chip_ver == 2) |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 390 | chip_ver_str = "BX"; |
| 391 | else |
| 392 | chip_ver_str = "??"; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 393 | |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 394 | dev_info(&dev->client->dev, "ITE IT913X %s successfully attached\n", |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 395 | chip_ver_str); |
Antti Palosaari | 676c350 | 2014-08-27 03:59:27 -0300 | [diff] [blame^] | 396 | dev_dbg(&dev->client->dev, "chip_ver=%u role=%u\n", dev->chip_ver, dev->role); |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 397 | return 0; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 398 | |
| 399 | err_regmap_exit: |
| 400 | regmap_exit(dev->regmap); |
| 401 | err_kfree: |
| 402 | kfree(dev); |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 403 | err: |
| 404 | dev_dbg(&client->dev, "failed %d\n", ret); |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 405 | return ret; |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 406 | } |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 407 | |
| 408 | static int it913x_remove(struct i2c_client *client) |
| 409 | { |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 410 | struct it913x_dev *dev = i2c_get_clientdata(client); |
| 411 | struct dvb_frontend *fe = dev->fe; |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 412 | |
| 413 | dev_dbg(&client->dev, "\n"); |
| 414 | |
| 415 | memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); |
| 416 | fe->tuner_priv = NULL; |
Antti Palosaari | d2dbc00 | 2014-08-26 21:45:33 -0300 | [diff] [blame] | 417 | regmap_exit(dev->regmap); |
Antti Palosaari | a71b65e | 2014-08-26 20:11:08 -0300 | [diff] [blame] | 418 | kfree(dev); |
Antti Palosaari | 3b2a5e8 | 2014-08-26 17:14:16 -0300 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static const struct i2c_device_id it913x_id_table[] = { |
| 424 | {"it913x", 0}, |
| 425 | {} |
| 426 | }; |
| 427 | MODULE_DEVICE_TABLE(i2c, it913x_id_table); |
| 428 | |
| 429 | static struct i2c_driver it913x_driver = { |
| 430 | .driver = { |
| 431 | .owner = THIS_MODULE, |
| 432 | .name = "it913x", |
| 433 | }, |
| 434 | .probe = it913x_probe, |
| 435 | .remove = it913x_remove, |
| 436 | .id_table = it913x_id_table, |
| 437 | }; |
| 438 | |
| 439 | module_i2c_driver(it913x_driver); |
Antti Palosaari | 88b38be | 2013-01-07 09:37:30 -0300 | [diff] [blame] | 440 | |
| 441 | MODULE_DESCRIPTION("ITE Tech IT913X silicon tuner driver"); |
| 442 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 443 | MODULE_LICENSE("GPL"); |