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