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; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame^] | 85 | int ret, 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 | u8 chip, len = 0; |
| 88 | const struct firmware *fw = NULL; |
| 89 | u8 *fw_file; |
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 | |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame^] | 109 | chip = cmd.args[2]; /* 57 for Si2157, 58 for Si2158 */ |
| 110 | |
| 111 | /* Si2158 requires firmware download */ |
| 112 | if (chip == 58) { |
| 113 | if (((cmd.args[1] & 0x0f) == 1) && (cmd.args[3] == '2') && |
| 114 | (cmd.args[4] == '0')) |
| 115 | fw_file = SI2158_A20_FIRMWARE; |
| 116 | else { |
| 117 | dev_err(&s->client->dev, |
| 118 | "%s: no firmware file for Si%d-%c%c defined\n", |
| 119 | KBUILD_MODNAME, chip, cmd.args[3], cmd.args[4]); |
| 120 | ret = -EINVAL; |
| 121 | goto err; |
| 122 | } |
| 123 | |
| 124 | /* cold state - try to download firmware */ |
| 125 | dev_info(&s->client->dev, "%s: found a '%s' in cold state\n", |
| 126 | KBUILD_MODNAME, si2157_ops.info.name); |
| 127 | |
| 128 | /* request the firmware, this will block and timeout */ |
| 129 | ret = request_firmware(&fw, fw_file, &s->client->dev); |
| 130 | if (ret) { |
| 131 | dev_err(&s->client->dev, "%s: firmware file '%s' not found\n", |
| 132 | KBUILD_MODNAME, fw_file); |
| 133 | goto err; |
| 134 | } |
| 135 | |
| 136 | dev_info(&s->client->dev, "%s: downloading firmware from file '%s'\n", |
| 137 | KBUILD_MODNAME, fw_file); |
| 138 | |
| 139 | /* firmware should be n chunks of 17 bytes */ |
| 140 | if (fw->size % 17 != 0) { |
| 141 | dev_err(&s->client->dev, "%s: firmware file '%s' is invalid\n", |
| 142 | KBUILD_MODNAME, fw_file); |
| 143 | ret = -EINVAL; |
| 144 | goto err; |
| 145 | } |
| 146 | |
| 147 | for (remaining = fw->size; remaining > 0; remaining -= 17) { |
| 148 | memcpy(&len, &fw->data[fw->size - remaining], 1); |
| 149 | memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], |
| 150 | len); |
| 151 | cmd.wlen = len; |
| 152 | cmd.rlen = 1; |
| 153 | ret = si2157_cmd_execute(s, &cmd); |
| 154 | if (ret) { |
| 155 | dev_err(&s->client->dev, |
| 156 | "%s: firmware download failed=%d\n", |
| 157 | KBUILD_MODNAME, ret); |
| 158 | goto err; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | release_firmware(fw); |
| 163 | fw = NULL; |
| 164 | |
| 165 | } |
| 166 | |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 167 | /* reboot the tuner with new firmware? */ |
| 168 | memcpy(cmd.args, "\x01\x01", 2); |
| 169 | cmd.wlen = 2; |
| 170 | cmd.rlen = 1; |
| 171 | ret = si2157_cmd_execute(s, &cmd); |
| 172 | if (ret) |
| 173 | goto err; |
| 174 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 175 | s->active = true; |
| 176 | |
| 177 | return 0; |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 178 | err: |
| 179 | dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret); |
| 180 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static int si2157_sleep(struct dvb_frontend *fe) |
| 184 | { |
| 185 | struct si2157 *s = fe->tuner_priv; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 186 | int ret; |
| 187 | struct si2157_cmd cmd; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 188 | |
| 189 | dev_dbg(&s->client->dev, "%s:\n", __func__); |
| 190 | |
| 191 | s->active = false; |
| 192 | |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 193 | memcpy(cmd.args, "\x13", 1); |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 194 | cmd.wlen = 1; |
| 195 | cmd.rlen = 0; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 196 | ret = si2157_cmd_execute(s, &cmd); |
| 197 | if (ret) |
| 198 | goto err; |
| 199 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 200 | return 0; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 201 | err: |
| 202 | dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret); |
| 203 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static int si2157_set_params(struct dvb_frontend *fe) |
| 207 | { |
| 208 | struct si2157 *s = fe->tuner_priv; |
| 209 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 210 | int ret; |
| 211 | struct si2157_cmd cmd; |
| 212 | |
| 213 | dev_dbg(&s->client->dev, |
| 214 | "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n", |
| 215 | __func__, c->delivery_system, c->frequency, |
| 216 | c->bandwidth_hz); |
| 217 | |
| 218 | if (!s->active) { |
| 219 | ret = -EAGAIN; |
| 220 | goto err; |
| 221 | } |
| 222 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 223 | /* set frequency */ |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 224 | memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 225 | cmd.args[4] = (c->frequency >> 0) & 0xff; |
| 226 | cmd.args[5] = (c->frequency >> 8) & 0xff; |
| 227 | cmd.args[6] = (c->frequency >> 16) & 0xff; |
| 228 | cmd.args[7] = (c->frequency >> 24) & 0xff; |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 229 | cmd.wlen = 8; |
| 230 | cmd.rlen = 1; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 231 | ret = si2157_cmd_execute(s, &cmd); |
| 232 | if (ret) |
| 233 | goto err; |
| 234 | |
| 235 | return 0; |
| 236 | err: |
| 237 | dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret); |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | static const struct dvb_tuner_ops si2157_tuner_ops = { |
| 242 | .info = { |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame^] | 243 | .name = "Silicon Labs Si2157/Si2158", |
Antti Palosaari | ae4c891 | 2014-04-12 01:53:51 -0300 | [diff] [blame] | 244 | .frequency_min = 110000000, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 245 | .frequency_max = 862000000, |
| 246 | }, |
| 247 | |
| 248 | .init = si2157_init, |
| 249 | .sleep = si2157_sleep, |
| 250 | .set_params = si2157_set_params, |
| 251 | }; |
| 252 | |
| 253 | static int si2157_probe(struct i2c_client *client, |
| 254 | const struct i2c_device_id *id) |
| 255 | { |
| 256 | struct si2157_config *cfg = client->dev.platform_data; |
| 257 | struct dvb_frontend *fe = cfg->fe; |
| 258 | struct si2157 *s; |
| 259 | struct si2157_cmd cmd; |
| 260 | int ret; |
| 261 | |
| 262 | s = kzalloc(sizeof(struct si2157), GFP_KERNEL); |
| 263 | if (!s) { |
| 264 | ret = -ENOMEM; |
| 265 | dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME); |
| 266 | goto err; |
| 267 | } |
| 268 | |
| 269 | s->client = client; |
| 270 | s->fe = cfg->fe; |
| 271 | mutex_init(&s->i2c_mutex); |
| 272 | |
| 273 | /* check if the tuner is there */ |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 274 | cmd.wlen = 0; |
| 275 | cmd.rlen = 1; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 276 | ret = si2157_cmd_execute(s, &cmd); |
| 277 | if (ret) |
| 278 | goto err; |
| 279 | |
| 280 | fe->tuner_priv = s; |
| 281 | memcpy(&fe->ops.tuner_ops, &si2157_tuner_ops, |
| 282 | sizeof(struct dvb_tuner_ops)); |
| 283 | |
| 284 | i2c_set_clientdata(client, s); |
| 285 | |
| 286 | dev_info(&s->client->dev, |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame^] | 287 | "%s: Silicon Labs Si2157/Si2158 successfully attached\n", |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 288 | KBUILD_MODNAME); |
| 289 | return 0; |
| 290 | err: |
| 291 | dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret); |
| 292 | kfree(s); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int si2157_remove(struct i2c_client *client) |
| 298 | { |
| 299 | struct si2157 *s = i2c_get_clientdata(client); |
| 300 | struct dvb_frontend *fe = s->fe; |
| 301 | |
| 302 | dev_dbg(&client->dev, "%s:\n", __func__); |
| 303 | |
| 304 | memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); |
| 305 | fe->tuner_priv = NULL; |
| 306 | kfree(s); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static const struct i2c_device_id si2157_id[] = { |
| 312 | {"si2157", 0}, |
| 313 | {} |
| 314 | }; |
| 315 | MODULE_DEVICE_TABLE(i2c, si2157_id); |
| 316 | |
| 317 | static struct i2c_driver si2157_driver = { |
| 318 | .driver = { |
| 319 | .owner = THIS_MODULE, |
| 320 | .name = "si2157", |
| 321 | }, |
| 322 | .probe = si2157_probe, |
| 323 | .remove = si2157_remove, |
| 324 | .id_table = si2157_id, |
| 325 | }; |
| 326 | |
| 327 | module_i2c_driver(si2157_driver); |
| 328 | |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame^] | 329 | MODULE_DESCRIPTION("Silicon Labs Si2157/Si2158 silicon tuner driver"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 330 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 331 | MODULE_LICENSE("GPL"); |