1 /* 2 * Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver 3 * 4 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17 #include "si2157_priv.h" 18 19 static const struct dvb_tuner_ops si2157_ops; 20 21 /* execute firmware command */ 22 static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd) 23 { 24 struct si2157_dev *dev = i2c_get_clientdata(client); 25 int ret; 26 unsigned long timeout; 27 28 mutex_lock(&dev->i2c_mutex); 29 30 if (cmd->wlen) { 31 /* write cmd and args for firmware */ 32 ret = i2c_master_send(client, cmd->args, cmd->wlen); 33 if (ret < 0) { 34 goto err_mutex_unlock; 35 } else if (ret != cmd->wlen) { 36 ret = -EREMOTEIO; 37 goto err_mutex_unlock; 38 } 39 } 40 41 if (cmd->rlen) { 42 /* wait cmd execution terminate */ 43 #define TIMEOUT 80 44 timeout = jiffies + msecs_to_jiffies(TIMEOUT); 45 while (!time_after(jiffies, timeout)) { 46 ret = i2c_master_recv(client, cmd->args, cmd->rlen); 47 if (ret < 0) { 48 goto err_mutex_unlock; 49 } else if (ret != cmd->rlen) { 50 ret = -EREMOTEIO; 51 goto err_mutex_unlock; 52 } 53 54 /* firmware ready? */ 55 if ((cmd->args[0] >> 7) & 0x01) 56 break; 57 } 58 59 dev_dbg(&client->dev, "cmd execution took %d ms\n", 60 jiffies_to_msecs(jiffies) - 61 (jiffies_to_msecs(timeout) - TIMEOUT)); 62 63 if (!((cmd->args[0] >> 7) & 0x01)) { 64 ret = -ETIMEDOUT; 65 goto err_mutex_unlock; 66 } 67 } 68 69 mutex_unlock(&dev->i2c_mutex); 70 return 0; 71 72 err_mutex_unlock: 73 mutex_unlock(&dev->i2c_mutex); 74 dev_dbg(&client->dev, "failed=%d\n", ret); 75 return ret; 76 } 77 78 static int si2157_init(struct dvb_frontend *fe) 79 { 80 struct i2c_client *client = fe->tuner_priv; 81 struct si2157_dev *dev = i2c_get_clientdata(client); 82 int ret, len, remaining; 83 struct si2157_cmd cmd; 84 const struct firmware *fw; 85 const char *fw_name; 86 unsigned int chip_id; 87 88 dev_dbg(&client->dev, "\n"); 89 90 if (dev->fw_loaded) 91 goto warm; 92 93 /* power up */ 94 if (dev->chiptype == SI2157_CHIPTYPE_SI2146) { 95 memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9); 96 cmd.wlen = 9; 97 } else { 98 memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15); 99 cmd.wlen = 15; 100 } 101 cmd.rlen = 1; 102 ret = si2157_cmd_execute(client, &cmd); 103 if (ret) 104 goto err; 105 106 /* query chip revision */ 107 memcpy(cmd.args, "\x02", 1); 108 cmd.wlen = 1; 109 cmd.rlen = 13; 110 ret = si2157_cmd_execute(client, &cmd); 111 if (ret) 112 goto err; 113 114 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 | 115 cmd.args[4] << 0; 116 117 #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0) 118 #define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0) 119 #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0) 120 #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0) 121 #define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0) 122 123 switch (chip_id) { 124 case SI2158_A20: 125 case SI2148_A20: 126 fw_name = SI2158_A20_FIRMWARE; 127 break; 128 case SI2157_A30: 129 case SI2147_A30: 130 case SI2146_A10: 131 fw_name = NULL; 132 break; 133 default: 134 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n", 135 cmd.args[2], cmd.args[1], 136 cmd.args[3], cmd.args[4]); 137 ret = -EINVAL; 138 goto err; 139 } 140 141 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n", 142 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]); 143 144 if (fw_name == NULL) 145 goto skip_fw_download; 146 147 /* request the firmware, this will block and timeout */ 148 ret = request_firmware(&fw, fw_name, &client->dev); 149 if (ret) { 150 dev_err(&client->dev, "firmware file '%s' not found\n", 151 fw_name); 152 goto err; 153 } 154 155 /* firmware should be n chunks of 17 bytes */ 156 if (fw->size % 17 != 0) { 157 dev_err(&client->dev, "firmware file '%s' is invalid\n", 158 fw_name); 159 ret = -EINVAL; 160 goto err_release_firmware; 161 } 162 163 dev_info(&client->dev, "downloading firmware from file '%s'\n", 164 fw_name); 165 166 for (remaining = fw->size; remaining > 0; remaining -= 17) { 167 len = fw->data[fw->size - remaining]; 168 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len); 169 cmd.wlen = len; 170 cmd.rlen = 1; 171 ret = si2157_cmd_execute(client, &cmd); 172 if (ret) { 173 dev_err(&client->dev, "firmware download failed %d\n", 174 ret); 175 goto err_release_firmware; 176 } 177 } 178 179 release_firmware(fw); 180 181 skip_fw_download: 182 /* reboot the tuner with new firmware? */ 183 memcpy(cmd.args, "\x01\x01", 2); 184 cmd.wlen = 2; 185 cmd.rlen = 1; 186 ret = si2157_cmd_execute(client, &cmd); 187 if (ret) 188 goto err; 189 190 /* query firmware version */ 191 memcpy(cmd.args, "\x11", 1); 192 cmd.wlen = 1; 193 cmd.rlen = 10; 194 ret = si2157_cmd_execute(client, &cmd); 195 if (ret) 196 goto err; 197 198 dev_info(&client->dev, "firmware version: %c.%c.%d\n", 199 cmd.args[6], cmd.args[7], cmd.args[8]); 200 201 dev->fw_loaded = true; 202 203 warm: 204 dev->active = true; 205 return 0; 206 207 err_release_firmware: 208 release_firmware(fw); 209 err: 210 dev_dbg(&client->dev, "failed=%d\n", ret); 211 return ret; 212 } 213 214 static int si2157_sleep(struct dvb_frontend *fe) 215 { 216 struct i2c_client *client = fe->tuner_priv; 217 struct si2157_dev *dev = i2c_get_clientdata(client); 218 int ret; 219 struct si2157_cmd cmd; 220 221 dev_dbg(&client->dev, "\n"); 222 223 dev->active = false; 224 225 /* standby */ 226 memcpy(cmd.args, "\x16\x00", 2); 227 cmd.wlen = 2; 228 cmd.rlen = 1; 229 ret = si2157_cmd_execute(client, &cmd); 230 if (ret) 231 goto err; 232 233 return 0; 234 err: 235 dev_dbg(&client->dev, "failed=%d\n", ret); 236 return ret; 237 } 238 239 static int si2157_set_params(struct dvb_frontend *fe) 240 { 241 struct i2c_client *client = fe->tuner_priv; 242 struct si2157_dev *dev = i2c_get_clientdata(client); 243 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 244 int ret; 245 struct si2157_cmd cmd; 246 u8 bandwidth, delivery_system; 247 u32 if_frequency = 5000000; 248 249 dev_dbg(&client->dev, 250 "delivery_system=%d frequency=%u bandwidth_hz=%u\n", 251 c->delivery_system, c->frequency, c->bandwidth_hz); 252 253 if (!dev->active) { 254 ret = -EAGAIN; 255 goto err; 256 } 257 258 if (c->bandwidth_hz <= 6000000) 259 bandwidth = 0x06; 260 else if (c->bandwidth_hz <= 7000000) 261 bandwidth = 0x07; 262 else if (c->bandwidth_hz <= 8000000) 263 bandwidth = 0x08; 264 else 265 bandwidth = 0x0f; 266 267 switch (c->delivery_system) { 268 case SYS_ATSC: 269 delivery_system = 0x00; 270 if_frequency = 3250000; 271 break; 272 case SYS_DVBC_ANNEX_B: 273 delivery_system = 0x10; 274 if_frequency = 4000000; 275 break; 276 case SYS_DVBT: 277 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */ 278 delivery_system = 0x20; 279 break; 280 case SYS_DVBC_ANNEX_A: 281 delivery_system = 0x30; 282 break; 283 default: 284 ret = -EINVAL; 285 goto err; 286 } 287 288 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6); 289 cmd.args[4] = delivery_system | bandwidth; 290 if (dev->inversion) 291 cmd.args[5] = 0x01; 292 cmd.wlen = 6; 293 cmd.rlen = 4; 294 ret = si2157_cmd_execute(client, &cmd); 295 if (ret) 296 goto err; 297 298 if (dev->chiptype == SI2157_CHIPTYPE_SI2146) 299 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6); 300 else 301 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x00", 6); 302 cmd.args[4] = dev->if_port; 303 cmd.wlen = 6; 304 cmd.rlen = 4; 305 ret = si2157_cmd_execute(client, &cmd); 306 if (ret) 307 goto err; 308 309 /* set if frequency if needed */ 310 if (if_frequency != dev->if_frequency) { 311 memcpy(cmd.args, "\x14\x00\x06\x07", 4); 312 cmd.args[4] = (if_frequency / 1000) & 0xff; 313 cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff; 314 cmd.wlen = 6; 315 cmd.rlen = 4; 316 ret = si2157_cmd_execute(client, &cmd); 317 if (ret) 318 goto err; 319 320 dev->if_frequency = if_frequency; 321 } 322 323 /* set frequency */ 324 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8); 325 cmd.args[4] = (c->frequency >> 0) & 0xff; 326 cmd.args[5] = (c->frequency >> 8) & 0xff; 327 cmd.args[6] = (c->frequency >> 16) & 0xff; 328 cmd.args[7] = (c->frequency >> 24) & 0xff; 329 cmd.wlen = 8; 330 cmd.rlen = 1; 331 ret = si2157_cmd_execute(client, &cmd); 332 if (ret) 333 goto err; 334 335 return 0; 336 err: 337 dev_dbg(&client->dev, "failed=%d\n", ret); 338 return ret; 339 } 340 341 static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) 342 { 343 struct i2c_client *client = fe->tuner_priv; 344 struct si2157_dev *dev = i2c_get_clientdata(client); 345 346 *frequency = dev->if_frequency; 347 return 0; 348 } 349 350 static const struct dvb_tuner_ops si2157_ops = { 351 .info = { 352 .name = "Silicon Labs Si2146/2147/2148/2157/2158", 353 .frequency_min = 55000000, 354 .frequency_max = 862000000, 355 }, 356 357 .init = si2157_init, 358 .sleep = si2157_sleep, 359 .set_params = si2157_set_params, 360 .get_if_frequency = si2157_get_if_frequency, 361 }; 362 363 static int si2157_probe(struct i2c_client *client, 364 const struct i2c_device_id *id) 365 { 366 struct si2157_config *cfg = client->dev.platform_data; 367 struct dvb_frontend *fe = cfg->fe; 368 struct si2157_dev *dev; 369 struct si2157_cmd cmd; 370 int ret; 371 372 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 373 if (!dev) { 374 ret = -ENOMEM; 375 dev_err(&client->dev, "kzalloc() failed\n"); 376 goto err; 377 } 378 379 i2c_set_clientdata(client, dev); 380 dev->fe = cfg->fe; 381 dev->inversion = cfg->inversion; 382 dev->if_port = cfg->if_port; 383 dev->fw_loaded = false; 384 dev->chiptype = (u8)id->driver_data; 385 dev->if_frequency = 5000000; /* default value of property 0x0706 */ 386 mutex_init(&dev->i2c_mutex); 387 388 /* check if the tuner is there */ 389 cmd.wlen = 0; 390 cmd.rlen = 1; 391 ret = si2157_cmd_execute(client, &cmd); 392 if (ret) 393 goto err_kfree; 394 395 memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops)); 396 fe->tuner_priv = client; 397 398 dev_info(&client->dev, "Silicon Labs %s successfully attached\n", 399 dev->chiptype == SI2157_CHIPTYPE_SI2146 ? 400 "Si2146" : "Si2147/2148/2157/2158"); 401 402 return 0; 403 404 err_kfree: 405 kfree(dev); 406 err: 407 dev_dbg(&client->dev, "failed=%d\n", ret); 408 return ret; 409 } 410 411 static int si2157_remove(struct i2c_client *client) 412 { 413 struct si2157_dev *dev = i2c_get_clientdata(client); 414 struct dvb_frontend *fe = dev->fe; 415 416 dev_dbg(&client->dev, "\n"); 417 418 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); 419 fe->tuner_priv = NULL; 420 kfree(dev); 421 422 return 0; 423 } 424 425 static const struct i2c_device_id si2157_id_table[] = { 426 {"si2157", SI2157_CHIPTYPE_SI2157}, 427 {"si2146", SI2157_CHIPTYPE_SI2146}, 428 {} 429 }; 430 MODULE_DEVICE_TABLE(i2c, si2157_id_table); 431 432 static struct i2c_driver si2157_driver = { 433 .driver = { 434 .owner = THIS_MODULE, 435 .name = "si2157", 436 }, 437 .probe = si2157_probe, 438 .remove = si2157_remove, 439 .id_table = si2157_id_table, 440 }; 441 442 module_i2c_driver(si2157_driver); 443 444 MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver"); 445 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 446 MODULE_LICENSE("GPL"); 447 MODULE_FIRMWARE(SI2158_A20_FIRMWARE); 448