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 248 dev_dbg(&client->dev, 249 "delivery_system=%d frequency=%u bandwidth_hz=%u\n", 250 c->delivery_system, c->frequency, c->bandwidth_hz); 251 252 if (!dev->active) { 253 ret = -EAGAIN; 254 goto err; 255 } 256 257 if (c->bandwidth_hz <= 6000000) 258 bandwidth = 0x06; 259 else if (c->bandwidth_hz <= 7000000) 260 bandwidth = 0x07; 261 else if (c->bandwidth_hz <= 8000000) 262 bandwidth = 0x08; 263 else 264 bandwidth = 0x0f; 265 266 switch (c->delivery_system) { 267 case SYS_ATSC: 268 delivery_system = 0x00; 269 break; 270 case SYS_DVBC_ANNEX_B: 271 delivery_system = 0x10; 272 break; 273 case SYS_DVBT: 274 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */ 275 delivery_system = 0x20; 276 break; 277 case SYS_DVBC_ANNEX_A: 278 delivery_system = 0x30; 279 break; 280 default: 281 ret = -EINVAL; 282 goto err; 283 } 284 285 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6); 286 cmd.args[4] = delivery_system | bandwidth; 287 if (dev->inversion) 288 cmd.args[5] = 0x01; 289 cmd.wlen = 6; 290 cmd.rlen = 4; 291 ret = si2157_cmd_execute(client, &cmd); 292 if (ret) 293 goto err; 294 295 if (dev->chiptype == SI2157_CHIPTYPE_SI2146) 296 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6); 297 else 298 memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6); 299 cmd.wlen = 6; 300 cmd.rlen = 4; 301 ret = si2157_cmd_execute(client, &cmd); 302 if (ret) 303 goto err; 304 305 /* set frequency */ 306 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8); 307 cmd.args[4] = (c->frequency >> 0) & 0xff; 308 cmd.args[5] = (c->frequency >> 8) & 0xff; 309 cmd.args[6] = (c->frequency >> 16) & 0xff; 310 cmd.args[7] = (c->frequency >> 24) & 0xff; 311 cmd.wlen = 8; 312 cmd.rlen = 1; 313 ret = si2157_cmd_execute(client, &cmd); 314 if (ret) 315 goto err; 316 317 return 0; 318 err: 319 dev_dbg(&client->dev, "failed=%d\n", ret); 320 return ret; 321 } 322 323 static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) 324 { 325 *frequency = 5000000; /* default value of property 0x0706 */ 326 return 0; 327 } 328 329 static const struct dvb_tuner_ops si2157_ops = { 330 .info = { 331 .name = "Silicon Labs Si2146/2147/2148/2157/2158", 332 .frequency_min = 110000000, 333 .frequency_max = 862000000, 334 }, 335 336 .init = si2157_init, 337 .sleep = si2157_sleep, 338 .set_params = si2157_set_params, 339 .get_if_frequency = si2157_get_if_frequency, 340 }; 341 342 static int si2157_probe(struct i2c_client *client, 343 const struct i2c_device_id *id) 344 { 345 struct si2157_config *cfg = client->dev.platform_data; 346 struct dvb_frontend *fe = cfg->fe; 347 struct si2157_dev *dev; 348 struct si2157_cmd cmd; 349 int ret; 350 351 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 352 if (!dev) { 353 ret = -ENOMEM; 354 dev_err(&client->dev, "kzalloc() failed\n"); 355 goto err; 356 } 357 358 i2c_set_clientdata(client, dev); 359 dev->fe = cfg->fe; 360 dev->inversion = cfg->inversion; 361 dev->fw_loaded = false; 362 dev->chiptype = (u8)id->driver_data; 363 mutex_init(&dev->i2c_mutex); 364 365 /* check if the tuner is there */ 366 cmd.wlen = 0; 367 cmd.rlen = 1; 368 ret = si2157_cmd_execute(client, &cmd); 369 if (ret) 370 goto err_kfree; 371 372 memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops)); 373 fe->tuner_priv = client; 374 375 dev_info(&client->dev, "Silicon Labs %s successfully attached\n", 376 dev->chiptype == SI2157_CHIPTYPE_SI2146 ? 377 "Si2146" : "Si2147/2148/2157/2158"); 378 379 return 0; 380 381 err_kfree: 382 kfree(dev); 383 err: 384 dev_dbg(&client->dev, "failed=%d\n", ret); 385 return ret; 386 } 387 388 static int si2157_remove(struct i2c_client *client) 389 { 390 struct si2157_dev *dev = i2c_get_clientdata(client); 391 struct dvb_frontend *fe = dev->fe; 392 393 dev_dbg(&client->dev, "\n"); 394 395 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); 396 fe->tuner_priv = NULL; 397 kfree(dev); 398 399 return 0; 400 } 401 402 static const struct i2c_device_id si2157_id_table[] = { 403 {"si2157", SI2157_CHIPTYPE_SI2157}, 404 {"si2146", SI2157_CHIPTYPE_SI2146}, 405 {} 406 }; 407 MODULE_DEVICE_TABLE(i2c, si2157_id_table); 408 409 static struct i2c_driver si2157_driver = { 410 .driver = { 411 .owner = THIS_MODULE, 412 .name = "si2157", 413 }, 414 .probe = si2157_probe, 415 .remove = si2157_remove, 416 .id_table = si2157_id_table, 417 }; 418 419 module_i2c_driver(si2157_driver); 420 421 MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver"); 422 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 423 MODULE_LICENSE("GPL"); 424 MODULE_FIRMWARE(SI2158_A20_FIRMWARE); 425