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