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