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