Antti Palosaari | ba92ae0 | 2014-04-14 21:51:32 -0300 | [diff] [blame] | 1 | /* |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 2 | * Silicon Labs Si2157/2158 silicon tuner driver |
Antti Palosaari | ba92ae0 | 2014-04-14 21:51:32 -0300 | [diff] [blame] | 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 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 17 | #include "si2157_priv.h" |
| 18 | |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 19 | static const struct dvb_tuner_ops si2157_ops; |
| 20 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 21 | /* execute firmware command */ |
| 22 | static int si2157_cmd_execute(struct si2157 *s, struct si2157_cmd *cmd) |
| 23 | { |
| 24 | int ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 25 | unsigned long timeout; |
| 26 | |
| 27 | mutex_lock(&s->i2c_mutex); |
| 28 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 29 | if (cmd->wlen) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 30 | /* write cmd and args for firmware */ |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 31 | ret = i2c_master_send(s->client, cmd->args, cmd->wlen); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 32 | if (ret < 0) { |
| 33 | goto err_mutex_unlock; |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 34 | } else if (ret != cmd->wlen) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 35 | ret = -EREMOTEIO; |
| 36 | goto err_mutex_unlock; |
| 37 | } |
| 38 | } |
| 39 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 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; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 56 | } |
| 57 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 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 | } |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 67 | } |
| 68 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 69 | ret = 0; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 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; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 85 | int ret, len, remaining; |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 86 | struct si2157_cmd cmd; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 87 | const struct firmware *fw = NULL; |
| 88 | u8 *fw_file; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 89 | unsigned int chip_id; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 90 | |
| 91 | dev_dbg(&s->client->dev, "%s:\n", __func__); |
| 92 | |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 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 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 109 | chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 | |
| 110 | cmd.args[4] << 0; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 111 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 112 | #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0) |
| 113 | #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0) |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 114 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 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; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 129 | } |
| 130 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 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: |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 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 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 180 | s->active = true; |
| 181 | |
| 182 | return 0; |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 183 | err: |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 184 | if (fw) |
| 185 | release_firmware(fw); |
| 186 | |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 187 | dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret); |
| 188 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int si2157_sleep(struct dvb_frontend *fe) |
| 192 | { |
| 193 | struct si2157 *s = fe->tuner_priv; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 194 | int ret; |
| 195 | struct si2157_cmd cmd; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 196 | |
| 197 | dev_dbg(&s->client->dev, "%s:\n", __func__); |
| 198 | |
| 199 | s->active = false; |
| 200 | |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 201 | memcpy(cmd.args, "\x13", 1); |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 202 | cmd.wlen = 1; |
| 203 | cmd.rlen = 0; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 204 | ret = si2157_cmd_execute(s, &cmd); |
| 205 | if (ret) |
| 206 | goto err; |
| 207 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 208 | return 0; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 209 | err: |
| 210 | dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret); |
| 211 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 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; |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 220 | u8 bandwidth, delivery_system; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 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 | |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 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 | cmd.wlen = 6; |
| 257 | cmd.rlen = 1; |
| 258 | ret = si2157_cmd_execute(s, &cmd); |
| 259 | if (ret) |
| 260 | goto err; |
| 261 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 262 | /* set frequency */ |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 263 | memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 264 | cmd.args[4] = (c->frequency >> 0) & 0xff; |
| 265 | cmd.args[5] = (c->frequency >> 8) & 0xff; |
| 266 | cmd.args[6] = (c->frequency >> 16) & 0xff; |
| 267 | cmd.args[7] = (c->frequency >> 24) & 0xff; |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 268 | cmd.wlen = 8; |
| 269 | cmd.rlen = 1; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 270 | ret = si2157_cmd_execute(s, &cmd); |
| 271 | if (ret) |
| 272 | goto err; |
| 273 | |
| 274 | return 0; |
| 275 | err: |
| 276 | dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static const struct dvb_tuner_ops si2157_tuner_ops = { |
| 281 | .info = { |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 282 | .name = "Silicon Labs Si2157/Si2158", |
Antti Palosaari | ae4c891 | 2014-04-12 01:53:51 -0300 | [diff] [blame] | 283 | .frequency_min = 110000000, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 284 | .frequency_max = 862000000, |
| 285 | }, |
| 286 | |
| 287 | .init = si2157_init, |
| 288 | .sleep = si2157_sleep, |
| 289 | .set_params = si2157_set_params, |
| 290 | }; |
| 291 | |
| 292 | static int si2157_probe(struct i2c_client *client, |
| 293 | const struct i2c_device_id *id) |
| 294 | { |
| 295 | struct si2157_config *cfg = client->dev.platform_data; |
| 296 | struct dvb_frontend *fe = cfg->fe; |
| 297 | struct si2157 *s; |
| 298 | struct si2157_cmd cmd; |
| 299 | int ret; |
| 300 | |
| 301 | s = kzalloc(sizeof(struct si2157), GFP_KERNEL); |
| 302 | if (!s) { |
| 303 | ret = -ENOMEM; |
| 304 | dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME); |
| 305 | goto err; |
| 306 | } |
| 307 | |
| 308 | s->client = client; |
| 309 | s->fe = cfg->fe; |
| 310 | mutex_init(&s->i2c_mutex); |
| 311 | |
| 312 | /* check if the tuner is there */ |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 313 | cmd.wlen = 0; |
| 314 | cmd.rlen = 1; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 315 | ret = si2157_cmd_execute(s, &cmd); |
| 316 | if (ret) |
| 317 | goto err; |
| 318 | |
| 319 | fe->tuner_priv = s; |
| 320 | memcpy(&fe->ops.tuner_ops, &si2157_tuner_ops, |
| 321 | sizeof(struct dvb_tuner_ops)); |
| 322 | |
| 323 | i2c_set_clientdata(client, s); |
| 324 | |
| 325 | dev_info(&s->client->dev, |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 326 | "%s: Silicon Labs Si2157/Si2158 successfully attached\n", |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 327 | KBUILD_MODNAME); |
| 328 | return 0; |
| 329 | err: |
| 330 | dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret); |
| 331 | kfree(s); |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static int si2157_remove(struct i2c_client *client) |
| 337 | { |
| 338 | struct si2157 *s = i2c_get_clientdata(client); |
| 339 | struct dvb_frontend *fe = s->fe; |
| 340 | |
| 341 | dev_dbg(&client->dev, "%s:\n", __func__); |
| 342 | |
| 343 | memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); |
| 344 | fe->tuner_priv = NULL; |
| 345 | kfree(s); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static const struct i2c_device_id si2157_id[] = { |
| 351 | {"si2157", 0}, |
| 352 | {} |
| 353 | }; |
| 354 | MODULE_DEVICE_TABLE(i2c, si2157_id); |
| 355 | |
| 356 | static struct i2c_driver si2157_driver = { |
| 357 | .driver = { |
| 358 | .owner = THIS_MODULE, |
| 359 | .name = "si2157", |
| 360 | }, |
| 361 | .probe = si2157_probe, |
| 362 | .remove = si2157_remove, |
| 363 | .id_table = si2157_id, |
| 364 | }; |
| 365 | |
| 366 | module_i2c_driver(si2157_driver); |
| 367 | |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 368 | MODULE_DESCRIPTION("Silicon Labs Si2157/Si2158 silicon tuner driver"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 369 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 370 | MODULE_LICENSE("GPL"); |
Antti Palosaari | bac53a2 | 2014-07-13 16:13:49 -0300 | [diff] [blame] | 371 | MODULE_FIRMWARE(SI2158_A20_FIRMWARE); |