Antti Palosaari | 711615d | 2014-04-14 21:55:12 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Silicon Labs Si2168 DVB-T/T2/C demodulator 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 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 17 | #include "si2168_priv.h" |
| 18 | |
| 19 | static const struct dvb_frontend_ops si2168_ops; |
| 20 | |
| 21 | /* execute firmware command */ |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 22 | static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd) |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 23 | { |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 24 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 25 | int ret; |
| 26 | unsigned long timeout; |
| 27 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 28 | mutex_lock(&dev->i2c_mutex); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 29 | |
| 30 | if (cmd->wlen) { |
| 31 | /* write cmd and args for firmware */ |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 32 | ret = i2c_master_send(client, cmd->args, cmd->wlen); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 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 */ |
Jurgen Kramer | 551c33e | 2014-12-08 05:30:44 -0300 | [diff] [blame] | 43 | #define TIMEOUT 70 |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 44 | timeout = jiffies + msecs_to_jiffies(TIMEOUT); |
| 45 | while (!time_after(jiffies, timeout)) { |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 46 | ret = i2c_master_recv(client, cmd->args, cmd->rlen); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 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 | |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 59 | dev_dbg(&client->dev, "cmd execution took %d ms\n", |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 60 | jiffies_to_msecs(jiffies) - |
| 61 | (jiffies_to_msecs(timeout) - TIMEOUT)); |
| 62 | |
Antti Palosaari | eefae30 | 2014-06-13 11:08:25 -0300 | [diff] [blame] | 63 | if (!((cmd->args[0] >> 7) & 0x01)) { |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 64 | ret = -ETIMEDOUT; |
| 65 | goto err_mutex_unlock; |
| 66 | } |
| 67 | } |
| 68 | |
Antti Palosaari | 4affbe1 | 2014-12-05 14:30:44 -0300 | [diff] [blame] | 69 | mutex_unlock(&dev->i2c_mutex); |
| 70 | return 0; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 71 | |
| 72 | err_mutex_unlock: |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 73 | mutex_unlock(&dev->i2c_mutex); |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 74 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static int si2168_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 79 | { |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 80 | struct i2c_client *client = fe->demodulator_priv; |
| 81 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 82 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 83 | int ret; |
| 84 | struct si2168_cmd cmd; |
| 85 | |
| 86 | *status = 0; |
| 87 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 88 | if (!dev->active) { |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 89 | ret = -EAGAIN; |
| 90 | goto err; |
| 91 | } |
| 92 | |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 93 | switch (c->delivery_system) { |
| 94 | case SYS_DVBT: |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 95 | memcpy(cmd.args, "\xa0\x01", 2); |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 96 | cmd.wlen = 2; |
| 97 | cmd.rlen = 13; |
| 98 | break; |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 99 | case SYS_DVBC_ANNEX_A: |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 100 | memcpy(cmd.args, "\x90\x01", 2); |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 101 | cmd.wlen = 2; |
| 102 | cmd.rlen = 9; |
| 103 | break; |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 104 | case SYS_DVBT2: |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 105 | memcpy(cmd.args, "\x50\x01", 2); |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 106 | cmd.wlen = 2; |
| 107 | cmd.rlen = 14; |
| 108 | break; |
| 109 | default: |
| 110 | ret = -EINVAL; |
| 111 | goto err; |
| 112 | } |
| 113 | |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 114 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 115 | if (ret) |
| 116 | goto err; |
| 117 | |
Antti Palosaari | 722a042 | 2014-04-22 21:36:32 -0300 | [diff] [blame] | 118 | switch ((cmd.args[2] >> 1) & 0x03) { |
| 119 | case 0x01: |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 120 | *status = FE_HAS_SIGNAL | FE_HAS_CARRIER; |
| 121 | break; |
Antti Palosaari | 722a042 | 2014-04-22 21:36:32 -0300 | [diff] [blame] | 122 | case 0x03: |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 123 | *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | |
| 124 | FE_HAS_SYNC | FE_HAS_LOCK; |
| 125 | break; |
| 126 | } |
| 127 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 128 | dev->fe_status = *status; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 129 | |
Antti Palosaari | 88ac8f8 | 2014-07-09 20:22:27 -0300 | [diff] [blame] | 130 | if (*status & FE_HAS_LOCK) { |
| 131 | c->cnr.len = 1; |
| 132 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 133 | c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4; |
| 134 | } else { |
| 135 | c->cnr.len = 1; |
| 136 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 137 | } |
| 138 | |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 139 | dev_dbg(&client->dev, "status=%02x args=%*ph\n", |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 140 | *status, cmd.rlen, cmd.args); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | err: |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 144 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int si2168_set_frontend(struct dvb_frontend *fe) |
| 149 | { |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 150 | struct i2c_client *client = fe->demodulator_priv; |
| 151 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 152 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 153 | int ret; |
| 154 | struct si2168_cmd cmd; |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 155 | u8 bandwidth, delivery_system; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 156 | |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 157 | dev_dbg(&client->dev, |
Antti Palosaari | e5dd110 | 2014-12-05 15:57:03 -0300 | [diff] [blame] | 158 | "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n", |
| 159 | c->delivery_system, c->modulation, c->frequency, |
| 160 | c->bandwidth_hz, c->symbol_rate, c->inversion, |
| 161 | c->stream_id); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 162 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 163 | if (!dev->active) { |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 164 | ret = -EAGAIN; |
| 165 | goto err; |
| 166 | } |
| 167 | |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 168 | switch (c->delivery_system) { |
| 169 | case SYS_DVBT: |
| 170 | delivery_system = 0x20; |
| 171 | break; |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 172 | case SYS_DVBC_ANNEX_A: |
| 173 | delivery_system = 0x30; |
| 174 | break; |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 175 | case SYS_DVBT2: |
| 176 | delivery_system = 0x70; |
| 177 | break; |
| 178 | default: |
| 179 | ret = -EINVAL; |
| 180 | goto err; |
| 181 | } |
| 182 | |
Olli Salonen | 683e98b | 2015-01-16 09:35:19 -0300 | [diff] [blame] | 183 | if (c->bandwidth_hz == 0) { |
| 184 | ret = -EINVAL; |
| 185 | goto err; |
Olli Salonen | 17d4d6a | 2015-01-16 09:35:20 -0300 | [diff] [blame] | 186 | } else if (c->bandwidth_hz <= 2000000) |
| 187 | bandwidth = 0x02; |
| 188 | else if (c->bandwidth_hz <= 5000000) |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 189 | bandwidth = 0x05; |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 190 | else if (c->bandwidth_hz <= 6000000) |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 191 | bandwidth = 0x06; |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 192 | else if (c->bandwidth_hz <= 7000000) |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 193 | bandwidth = 0x07; |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 194 | else if (c->bandwidth_hz <= 8000000) |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 195 | bandwidth = 0x08; |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 196 | else if (c->bandwidth_hz <= 9000000) |
| 197 | bandwidth = 0x09; |
| 198 | else if (c->bandwidth_hz <= 10000000) |
| 199 | bandwidth = 0x0a; |
| 200 | else |
| 201 | bandwidth = 0x0f; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 202 | |
| 203 | /* program tuner */ |
| 204 | if (fe->ops.tuner_ops.set_params) { |
| 205 | ret = fe->ops.tuner_ops.set_params(fe); |
| 206 | if (ret) |
| 207 | goto err; |
| 208 | } |
| 209 | |
| 210 | memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5); |
| 211 | cmd.wlen = 5; |
| 212 | cmd.rlen = 5; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 213 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 214 | if (ret) |
| 215 | goto err; |
| 216 | |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 217 | /* that has no big effect */ |
| 218 | if (c->delivery_system == SYS_DVBT) |
| 219 | memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6); |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 220 | else if (c->delivery_system == SYS_DVBC_ANNEX_A) |
| 221 | memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6); |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 222 | else if (c->delivery_system == SYS_DVBT2) |
| 223 | memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 224 | cmd.wlen = 6; |
| 225 | cmd.rlen = 3; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 226 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 227 | if (ret) |
| 228 | goto err; |
| 229 | |
CrazyCat | e395e57 | 2014-08-16 18:33:14 -0300 | [diff] [blame] | 230 | if (c->delivery_system == SYS_DVBT2) { |
| 231 | /* select PLP */ |
| 232 | cmd.args[0] = 0x52; |
| 233 | cmd.args[1] = c->stream_id & 0xff; |
| 234 | cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1; |
| 235 | cmd.wlen = 3; |
| 236 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 237 | ret = si2168_cmd_execute(client, &cmd); |
CrazyCat | e395e57 | 2014-08-16 18:33:14 -0300 | [diff] [blame] | 238 | if (ret) |
| 239 | goto err; |
| 240 | } |
| 241 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 242 | memcpy(cmd.args, "\x51\x03", 2); |
| 243 | cmd.wlen = 2; |
| 244 | cmd.rlen = 12; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 245 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 246 | if (ret) |
| 247 | goto err; |
| 248 | |
| 249 | memcpy(cmd.args, "\x12\x08\x04", 3); |
| 250 | cmd.wlen = 3; |
| 251 | cmd.rlen = 3; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 252 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 253 | if (ret) |
| 254 | goto err; |
| 255 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 256 | memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6); |
| 257 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 258 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 259 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 260 | if (ret) |
| 261 | goto err; |
| 262 | |
| 263 | memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6); |
| 264 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 265 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 266 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 267 | if (ret) |
| 268 | goto err; |
| 269 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 270 | memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6); |
| 271 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 272 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 273 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 274 | if (ret) |
| 275 | goto err; |
| 276 | |
| 277 | memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6); |
Antti Palosaari | bffab93 | 2014-04-11 20:35:27 -0300 | [diff] [blame] | 278 | cmd.args[4] = delivery_system | bandwidth; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 279 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 280 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 281 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 282 | if (ret) |
| 283 | goto err; |
| 284 | |
Luis Alves | 32bf881 | 2014-07-17 14:31:28 -0300 | [diff] [blame] | 285 | /* set DVB-C symbol rate */ |
| 286 | if (c->delivery_system == SYS_DVBC_ANNEX_A) { |
| 287 | memcpy(cmd.args, "\x14\x00\x02\x11", 4); |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 288 | cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff; |
Luis Alves | 32bf881 | 2014-07-17 14:31:28 -0300 | [diff] [blame] | 289 | cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff; |
| 290 | cmd.wlen = 6; |
| 291 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 292 | ret = si2168_cmd_execute(client, &cmd); |
Luis Alves | 32bf881 | 2014-07-17 14:31:28 -0300 | [diff] [blame] | 293 | if (ret) |
| 294 | goto err; |
| 295 | } |
| 296 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 297 | memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6); |
| 298 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 299 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 300 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 301 | if (ret) |
| 302 | goto err; |
| 303 | |
CrazyCat | 5279197 | 2014-11-14 18:22:10 -0300 | [diff] [blame] | 304 | memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6); |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 305 | cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 306 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 307 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 308 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 309 | if (ret) |
| 310 | goto err; |
| 311 | |
CrazyCat | 5279197 | 2014-11-14 18:22:10 -0300 | [diff] [blame] | 312 | memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6); |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 313 | cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 314 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 315 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 316 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 317 | if (ret) |
| 318 | goto err; |
| 319 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 320 | memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6); |
| 321 | cmd.wlen = 6; |
Antti Palosaari | 1d518c2 | 2014-07-11 10:40:29 -0300 | [diff] [blame] | 322 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 323 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 324 | if (ret) |
| 325 | goto err; |
| 326 | |
Olli Salonen | 4391177 | 2014-07-17 15:43:27 -0300 | [diff] [blame] | 327 | memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6); |
| 328 | cmd.wlen = 6; |
| 329 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 330 | ret = si2168_cmd_execute(client, &cmd); |
Olli Salonen | 4391177 | 2014-07-17 15:43:27 -0300 | [diff] [blame] | 331 | if (ret) |
| 332 | goto err; |
| 333 | |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 334 | memcpy(cmd.args, "\x85", 1); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 335 | cmd.wlen = 1; |
| 336 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 337 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 338 | if (ret) |
| 339 | goto err; |
| 340 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 341 | dev->delivery_system = c->delivery_system; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | err: |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 345 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | static int si2168_init(struct dvb_frontend *fe) |
| 350 | { |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 351 | struct i2c_client *client = fe->demodulator_priv; |
| 352 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 353 | int ret, len, remaining; |
Antti Palosaari | 58f6693 | 2014-12-05 17:22:42 -0300 | [diff] [blame] | 354 | const struct firmware *fw; |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 355 | const char *fw_name; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 356 | struct si2168_cmd cmd; |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 357 | unsigned int chip_id; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 358 | |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 359 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 360 | |
Olli Salonen | 8e41722 | 2014-08-25 15:07:04 -0300 | [diff] [blame] | 361 | /* initialize */ |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 362 | memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 363 | cmd.wlen = 13; |
| 364 | cmd.rlen = 0; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 365 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 366 | if (ret) |
| 367 | goto err; |
| 368 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 369 | if (dev->fw_loaded) { |
Olli Salonen | 8e41722 | 2014-08-25 15:07:04 -0300 | [diff] [blame] | 370 | /* resume */ |
| 371 | memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8); |
| 372 | cmd.wlen = 8; |
| 373 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 374 | ret = si2168_cmd_execute(client, &cmd); |
Olli Salonen | 8e41722 | 2014-08-25 15:07:04 -0300 | [diff] [blame] | 375 | if (ret) |
| 376 | goto err; |
| 377 | |
| 378 | memcpy(cmd.args, "\x85", 1); |
| 379 | cmd.wlen = 1; |
| 380 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 381 | ret = si2168_cmd_execute(client, &cmd); |
Olli Salonen | 8e41722 | 2014-08-25 15:07:04 -0300 | [diff] [blame] | 382 | if (ret) |
| 383 | goto err; |
| 384 | |
| 385 | goto warm; |
| 386 | } |
| 387 | |
| 388 | /* power up */ |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 389 | memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 390 | cmd.wlen = 8; |
| 391 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 392 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 393 | if (ret) |
| 394 | goto err; |
| 395 | |
Olli Salonen | c9cb082 | 2014-07-13 10:52:18 -0300 | [diff] [blame] | 396 | /* query chip revision */ |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 397 | memcpy(cmd.args, "\x02", 1); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 398 | cmd.wlen = 1; |
| 399 | cmd.rlen = 13; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 400 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 401 | if (ret) |
| 402 | goto err; |
| 403 | |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 404 | chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 | |
| 405 | cmd.args[4] << 0; |
| 406 | |
Luis Alves | 635a90c | 2014-07-17 19:43:37 -0300 | [diff] [blame] | 407 | #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0) |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 408 | #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0) |
| 409 | #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0) |
| 410 | |
| 411 | switch (chip_id) { |
Luis Alves | 635a90c | 2014-07-17 19:43:37 -0300 | [diff] [blame] | 412 | case SI2168_A20: |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 413 | fw_name = SI2168_A20_FIRMWARE; |
Luis Alves | 635a90c | 2014-07-17 19:43:37 -0300 | [diff] [blame] | 414 | break; |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 415 | case SI2168_A30: |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 416 | fw_name = SI2168_A30_FIRMWARE; |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 417 | break; |
| 418 | case SI2168_B40: |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 419 | fw_name = SI2168_B40_FIRMWARE; |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 420 | break; |
| 421 | default: |
Antti Palosaari | 9b7839c | 2014-12-06 17:43:27 -0300 | [diff] [blame] | 422 | dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n", |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 423 | cmd.args[2], cmd.args[1], |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 424 | cmd.args[3], cmd.args[4]); |
Olli Salonen | c9cb082 | 2014-07-13 10:52:18 -0300 | [diff] [blame] | 425 | ret = -EINVAL; |
| 426 | goto err; |
| 427 | } |
| 428 | |
Antti Palosaari | 9b7839c | 2014-12-06 17:43:27 -0300 | [diff] [blame] | 429 | dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n", |
| 430 | cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 431 | |
| 432 | /* request the firmware, this will block and timeout */ |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 433 | ret = request_firmware(&fw, fw_name, &client->dev); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 434 | if (ret) { |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 435 | /* fallback mechanism to handle old name for Si2168 B40 fw */ |
| 436 | if (chip_id == SI2168_B40) { |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 437 | fw_name = SI2168_B40_FIRMWARE_FALLBACK; |
| 438 | ret = request_firmware(&fw, fw_name, &client->dev); |
Olli Salonen | c9cb082 | 2014-07-13 10:52:18 -0300 | [diff] [blame] | 439 | } |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 440 | |
| 441 | if (ret == 0) { |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 442 | dev_notice(&client->dev, |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 443 | "please install firmware file '%s'\n", |
| 444 | SI2168_B40_FIRMWARE); |
Antti Palosaari | b6b6fd6 | 2014-07-13 19:22:19 -0300 | [diff] [blame] | 445 | } else { |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 446 | dev_err(&client->dev, |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 447 | "firmware file '%s' not found\n", |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 448 | fw_name); |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 449 | goto err_release_firmware; |
Olli Salonen | c9cb082 | 2014-07-13 10:52:18 -0300 | [diff] [blame] | 450 | } |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 451 | } |
| 452 | |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 453 | dev_info(&client->dev, "downloading firmware from file '%s'\n", |
Antti Palosaari | c32b281 | 2014-12-06 17:53:16 -0300 | [diff] [blame] | 454 | fw_name); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 455 | |
Olli Salonen | 1b97dc9 | 2014-11-27 16:42:23 -0300 | [diff] [blame] | 456 | if ((fw->size % 17 == 0) && (fw->data[0] > 5)) { |
| 457 | /* firmware is in the new format */ |
| 458 | for (remaining = fw->size; remaining > 0; remaining -= 17) { |
| 459 | len = fw->data[fw->size - remaining]; |
| 460 | memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len); |
| 461 | cmd.wlen = len; |
| 462 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 463 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 68c16a7 | 2014-12-05 17:08:22 -0300 | [diff] [blame] | 464 | if (ret) |
| 465 | break; |
Olli Salonen | 1b97dc9 | 2014-11-27 16:42:23 -0300 | [diff] [blame] | 466 | } |
Antti Palosaari | 68c16a7 | 2014-12-05 17:08:22 -0300 | [diff] [blame] | 467 | } else if (fw->size % 8 == 0) { |
Olli Salonen | 1b97dc9 | 2014-11-27 16:42:23 -0300 | [diff] [blame] | 468 | /* firmware is in the old format */ |
Antti Palosaari | 68c16a7 | 2014-12-05 17:08:22 -0300 | [diff] [blame] | 469 | for (remaining = fw->size; remaining > 0; remaining -= 8) { |
| 470 | len = 8; |
Olli Salonen | 1b97dc9 | 2014-11-27 16:42:23 -0300 | [diff] [blame] | 471 | memcpy(cmd.args, &fw->data[fw->size - remaining], len); |
| 472 | cmd.wlen = len; |
| 473 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 474 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 68c16a7 | 2014-12-05 17:08:22 -0300 | [diff] [blame] | 475 | if (ret) |
| 476 | break; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 477 | } |
Antti Palosaari | 68c16a7 | 2014-12-05 17:08:22 -0300 | [diff] [blame] | 478 | } else { |
| 479 | /* bad or unknown firmware format */ |
| 480 | ret = -EINVAL; |
| 481 | } |
| 482 | |
| 483 | if (ret) { |
| 484 | dev_err(&client->dev, "firmware download failed %d\n", ret); |
| 485 | goto err_release_firmware; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | release_firmware(fw); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 489 | |
Antti Palosaari | 888680f | 2014-07-09 19:36:58 -0300 | [diff] [blame] | 490 | memcpy(cmd.args, "\x01\x01", 2); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 491 | cmd.wlen = 2; |
| 492 | cmd.rlen = 1; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 493 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 494 | if (ret) |
| 495 | goto err; |
| 496 | |
Olli Salonen | a594cf2 | 2014-11-27 16:42:22 -0300 | [diff] [blame] | 497 | /* query firmware version */ |
| 498 | memcpy(cmd.args, "\x11", 1); |
| 499 | cmd.wlen = 1; |
| 500 | cmd.rlen = 10; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 501 | ret = si2168_cmd_execute(client, &cmd); |
Olli Salonen | a594cf2 | 2014-11-27 16:42:22 -0300 | [diff] [blame] | 502 | if (ret) |
| 503 | goto err; |
| 504 | |
Antti Palosaari | 955e0ab | 2014-12-05 15:04:12 -0300 | [diff] [blame] | 505 | dev_info(&client->dev, "firmware version: %c.%c.%d\n", |
Olli Salonen | a594cf2 | 2014-11-27 16:42:22 -0300 | [diff] [blame] | 506 | cmd.args[6], cmd.args[7], cmd.args[8]); |
| 507 | |
Olli Salonen | 389ce39 | 2014-08-11 16:58:10 -0300 | [diff] [blame] | 508 | /* set ts mode */ |
| 509 | memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6); |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 510 | cmd.args[4] |= dev->ts_mode; |
Olli Salonen | 8117a31 | 2015-05-05 13:54:14 -0300 | [diff] [blame^] | 511 | if (dev->ts_clock_gapped) |
| 512 | cmd.args[4] |= 0x40; |
Olli Salonen | 389ce39 | 2014-08-11 16:58:10 -0300 | [diff] [blame] | 513 | cmd.wlen = 6; |
| 514 | cmd.rlen = 4; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 515 | ret = si2168_cmd_execute(client, &cmd); |
Olli Salonen | 389ce39 | 2014-08-11 16:58:10 -0300 | [diff] [blame] | 516 | if (ret) |
| 517 | goto err; |
| 518 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 519 | dev->fw_loaded = true; |
Antti Palosaari | 45c3cbb | 2014-11-03 18:28:39 -0300 | [diff] [blame] | 520 | warm: |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 521 | dev->active = true; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 522 | |
| 523 | return 0; |
Antti Palosaari | 9b7839c | 2014-12-06 17:43:27 -0300 | [diff] [blame] | 524 | |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 525 | err_release_firmware: |
Markus Elfring | 034e1ec | 2014-11-19 19:23:15 -0300 | [diff] [blame] | 526 | release_firmware(fw); |
| 527 | err: |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 528 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | static int si2168_sleep(struct dvb_frontend *fe) |
| 533 | { |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 534 | struct i2c_client *client = fe->demodulator_priv; |
| 535 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 4de0ed7 | 2014-07-09 18:45:31 -0300 | [diff] [blame] | 536 | int ret; |
| 537 | struct si2168_cmd cmd; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 538 | |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 539 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 540 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 541 | dev->active = false; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 542 | |
Antti Palosaari | 4de0ed7 | 2014-07-09 18:45:31 -0300 | [diff] [blame] | 543 | memcpy(cmd.args, "\x13", 1); |
| 544 | cmd.wlen = 1; |
| 545 | cmd.rlen = 0; |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 546 | ret = si2168_cmd_execute(client, &cmd); |
Antti Palosaari | 4de0ed7 | 2014-07-09 18:45:31 -0300 | [diff] [blame] | 547 | if (ret) |
| 548 | goto err; |
| 549 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 550 | return 0; |
Antti Palosaari | 4de0ed7 | 2014-07-09 18:45:31 -0300 | [diff] [blame] | 551 | err: |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 552 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 4de0ed7 | 2014-07-09 18:45:31 -0300 | [diff] [blame] | 553 | return ret; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static int si2168_get_tune_settings(struct dvb_frontend *fe, |
| 557 | struct dvb_frontend_tune_settings *s) |
| 558 | { |
| 559 | s->min_delay_ms = 900; |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * I2C gate logic |
| 566 | * We must use unlocked i2c_transfer() here because I2C lock is already taken |
| 567 | * by tuner driver. |
| 568 | */ |
| 569 | static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan) |
| 570 | { |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 571 | struct i2c_client *client = mux_priv; |
| 572 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 573 | int ret; |
| 574 | struct i2c_msg gate_open_msg = { |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 575 | .addr = client->addr, |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 576 | .flags = 0, |
| 577 | .len = 3, |
| 578 | .buf = "\xc0\x0d\x01", |
| 579 | }; |
| 580 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 581 | mutex_lock(&dev->i2c_mutex); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 582 | |
| 583 | /* open tuner I2C gate */ |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 584 | ret = __i2c_transfer(client->adapter, &gate_open_msg, 1); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 585 | if (ret != 1) { |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 586 | dev_warn(&client->dev, "i2c write failed=%d\n", ret); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 587 | if (ret >= 0) |
| 588 | ret = -EREMOTEIO; |
| 589 | } else { |
| 590 | ret = 0; |
| 591 | } |
| 592 | |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan) |
| 597 | { |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 598 | struct i2c_client *client = mux_priv; |
| 599 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 600 | int ret; |
| 601 | struct i2c_msg gate_close_msg = { |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 602 | .addr = client->addr, |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 603 | .flags = 0, |
| 604 | .len = 3, |
| 605 | .buf = "\xc0\x0d\x00", |
| 606 | }; |
| 607 | |
| 608 | /* close tuner I2C gate */ |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 609 | ret = __i2c_transfer(client->adapter, &gate_close_msg, 1); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 610 | if (ret != 1) { |
Antti Palosaari | 3de3583 | 2014-11-25 17:53:21 -0300 | [diff] [blame] | 611 | dev_warn(&client->dev, "i2c write failed=%d\n", ret); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 612 | if (ret >= 0) |
| 613 | ret = -EREMOTEIO; |
| 614 | } else { |
| 615 | ret = 0; |
| 616 | } |
| 617 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 618 | mutex_unlock(&dev->i2c_mutex); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 619 | |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | static const struct dvb_frontend_ops si2168_ops = { |
Antti Palosaari | c790885 | 2014-04-12 01:58:32 -0300 | [diff] [blame] | 624 | .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A}, |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 625 | .info = { |
| 626 | .name = "Silicon Labs Si2168", |
Antti Palosaari | f1ecc5d | 2014-11-25 16:26:49 -0300 | [diff] [blame] | 627 | .symbol_rate_min = 1000000, |
| 628 | .symbol_rate_max = 7200000, |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 629 | .caps = FE_CAN_FEC_1_2 | |
| 630 | FE_CAN_FEC_2_3 | |
| 631 | FE_CAN_FEC_3_4 | |
| 632 | FE_CAN_FEC_5_6 | |
| 633 | FE_CAN_FEC_7_8 | |
| 634 | FE_CAN_FEC_AUTO | |
| 635 | FE_CAN_QPSK | |
| 636 | FE_CAN_QAM_16 | |
| 637 | FE_CAN_QAM_32 | |
| 638 | FE_CAN_QAM_64 | |
| 639 | FE_CAN_QAM_128 | |
| 640 | FE_CAN_QAM_256 | |
| 641 | FE_CAN_QAM_AUTO | |
| 642 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 643 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 644 | FE_CAN_HIERARCHY_AUTO | |
| 645 | FE_CAN_MUTE_TS | |
Olli Salonen | 327eeb3 | 2014-09-23 13:53:09 -0300 | [diff] [blame] | 646 | FE_CAN_2G_MODULATION | |
| 647 | FE_CAN_MULTISTREAM |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 648 | }, |
| 649 | |
| 650 | .get_tune_settings = si2168_get_tune_settings, |
| 651 | |
| 652 | .init = si2168_init, |
| 653 | .sleep = si2168_sleep, |
| 654 | |
| 655 | .set_frontend = si2168_set_frontend, |
| 656 | |
| 657 | .read_status = si2168_read_status, |
| 658 | }; |
| 659 | |
| 660 | static int si2168_probe(struct i2c_client *client, |
| 661 | const struct i2c_device_id *id) |
| 662 | { |
| 663 | struct si2168_config *config = client->dev.platform_data; |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 664 | struct si2168_dev *dev; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 665 | int ret; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 666 | |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 667 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 668 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 669 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 670 | if (!dev) { |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 671 | ret = -ENOMEM; |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 672 | dev_err(&client->dev, "kzalloc() failed\n"); |
Antti Palosaari | 1ee5e7d | 2014-12-05 16:02:42 -0300 | [diff] [blame] | 673 | goto err; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 674 | } |
| 675 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 676 | mutex_init(&dev->i2c_mutex); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 677 | |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 678 | /* create mux i2c adapter for tuner */ |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 679 | dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev, |
| 680 | client, 0, 0, 0, si2168_select, si2168_deselect); |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 681 | if (dev->adapter == NULL) { |
Luis Alves | 4d6efc7 | 2014-07-17 16:38:08 -0300 | [diff] [blame] | 682 | ret = -ENODEV; |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 683 | goto err_kfree; |
Luis Alves | 4d6efc7 | 2014-07-17 16:38:08 -0300 | [diff] [blame] | 684 | } |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 685 | |
| 686 | /* create dvb_frontend */ |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 687 | memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops)); |
Antti Palosaari | 6307b56 | 2014-11-25 17:31:43 -0300 | [diff] [blame] | 688 | dev->fe.demodulator_priv = client; |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 689 | *config->i2c_adapter = dev->adapter; |
| 690 | *config->fe = &dev->fe; |
| 691 | dev->ts_mode = config->ts_mode; |
| 692 | dev->ts_clock_inv = config->ts_clock_inv; |
Olli Salonen | 8117a31 | 2015-05-05 13:54:14 -0300 | [diff] [blame^] | 693 | dev->ts_clock_gapped = config->ts_clock_gapped; |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 694 | dev->fw_loaded = false; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 695 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 696 | i2c_set_clientdata(client, dev); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 697 | |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 698 | dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n"); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 699 | return 0; |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 700 | err_kfree: |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 701 | kfree(dev); |
Antti Palosaari | 1ee5e7d | 2014-12-05 16:02:42 -0300 | [diff] [blame] | 702 | err: |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 703 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | static int si2168_remove(struct i2c_client *client) |
| 708 | { |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 709 | struct si2168_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 710 | |
Olli Salonen | 37b4e43 | 2014-08-05 08:54:08 -0300 | [diff] [blame] | 711 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 712 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 713 | i2c_del_mux_adapter(dev->adapter); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 714 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 715 | dev->fe.ops.release = NULL; |
| 716 | dev->fe.demodulator_priv = NULL; |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 717 | |
Antti Palosaari | bd01c76 | 2014-11-25 16:46:16 -0300 | [diff] [blame] | 718 | kfree(dev); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 723 | static const struct i2c_device_id si2168_id_table[] = { |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 724 | {"si2168", 0}, |
| 725 | {} |
| 726 | }; |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 727 | MODULE_DEVICE_TABLE(i2c, si2168_id_table); |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 728 | |
| 729 | static struct i2c_driver si2168_driver = { |
| 730 | .driver = { |
| 731 | .owner = THIS_MODULE, |
| 732 | .name = "si2168", |
| 733 | }, |
| 734 | .probe = si2168_probe, |
| 735 | .remove = si2168_remove, |
Antti Palosaari | 346d490 | 2014-12-05 14:54:14 -0300 | [diff] [blame] | 736 | .id_table = si2168_id_table, |
Antti Palosaari | 845f350 | 2014-04-10 22:00:50 -0300 | [diff] [blame] | 737 | }; |
| 738 | |
| 739 | module_i2c_driver(si2168_driver); |
| 740 | |
| 741 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 742 | MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver"); |
| 743 | MODULE_LICENSE("GPL"); |
Luis Alves | 635a90c | 2014-07-17 19:43:37 -0300 | [diff] [blame] | 744 | MODULE_FIRMWARE(SI2168_A20_FIRMWARE); |
Antti Palosaari | 668aa63 | 2014-07-13 16:09:03 -0300 | [diff] [blame] | 745 | MODULE_FIRMWARE(SI2168_A30_FIRMWARE); |
Olli Salonen | c9cb082 | 2014-07-13 10:52:18 -0300 | [diff] [blame] | 746 | MODULE_FIRMWARE(SI2168_B40_FIRMWARE); |