Antti Palosaari | ba92ae0 | 2014-04-14 21:51:32 -0300 | [diff] [blame] | 1 | /* |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 2 | * Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver |
Antti Palosaari | ba92ae0 | 2014-04-14 21:51:32 -0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2014 Antti Palosaari <crope@iki.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 17 | #include "si2157_priv.h" |
| 18 | |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 19 | static const struct dvb_tuner_ops si2157_ops; |
| 20 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 21 | /* execute firmware command */ |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 22 | static int si2157_cmd_execute(struct si2157_dev *dev, struct si2157_cmd *cmd) |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 23 | { |
| 24 | int ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 25 | unsigned long timeout; |
| 26 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 27 | mutex_lock(&dev->i2c_mutex); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 28 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 29 | if (cmd->wlen) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 30 | /* write cmd and args for firmware */ |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 31 | ret = i2c_master_send(dev->client, cmd->args, cmd->wlen); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 32 | if (ret < 0) { |
| 33 | goto err_mutex_unlock; |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 34 | } else if (ret != cmd->wlen) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 35 | ret = -EREMOTEIO; |
| 36 | goto err_mutex_unlock; |
| 37 | } |
| 38 | } |
| 39 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 40 | if (cmd->rlen) { |
| 41 | /* wait cmd execution terminate */ |
| 42 | #define TIMEOUT 80 |
| 43 | timeout = jiffies + msecs_to_jiffies(TIMEOUT); |
| 44 | while (!time_after(jiffies, timeout)) { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 45 | ret = i2c_master_recv(dev->client, cmd->args, cmd->rlen); |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 46 | if (ret < 0) { |
| 47 | goto err_mutex_unlock; |
| 48 | } else if (ret != cmd->rlen) { |
| 49 | ret = -EREMOTEIO; |
| 50 | goto err_mutex_unlock; |
| 51 | } |
| 52 | |
| 53 | /* firmware ready? */ |
| 54 | if ((cmd->args[0] >> 7) & 0x01) |
| 55 | break; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 56 | } |
| 57 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 58 | dev_dbg(&dev->client->dev, "cmd execution took %d ms\n", |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 59 | jiffies_to_msecs(jiffies) - |
| 60 | (jiffies_to_msecs(timeout) - TIMEOUT)); |
| 61 | |
| 62 | if (!((cmd->args[0] >> 7) & 0x01)) { |
| 63 | ret = -ETIMEDOUT; |
| 64 | goto err_mutex_unlock; |
| 65 | } |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 66 | } |
| 67 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 68 | ret = 0; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 69 | |
| 70 | err_mutex_unlock: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 71 | mutex_unlock(&dev->i2c_mutex); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 72 | if (ret) |
| 73 | goto err; |
| 74 | |
| 75 | return 0; |
| 76 | err: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 77 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static int si2157_init(struct dvb_frontend *fe) |
| 82 | { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 83 | struct si2157_dev *dev = fe->tuner_priv; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 84 | int ret, len, remaining; |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 85 | struct si2157_cmd cmd; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 86 | const struct firmware *fw = NULL; |
| 87 | u8 *fw_file; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 88 | unsigned int chip_id; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 89 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 90 | dev_dbg(&dev->client->dev, "\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 91 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 92 | if (dev->fw_loaded) |
Olli Salonen | 4cbf6ed | 2014-08-25 15:07:03 -0300 | [diff] [blame] | 93 | goto warm; |
| 94 | |
| 95 | /* power up */ |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 96 | if (dev->chiptype == SI2157_CHIPTYPE_SI2146) { |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 97 | memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9); |
| 98 | cmd.wlen = 9; |
| 99 | } else { |
| 100 | memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15); |
| 101 | cmd.wlen = 15; |
| 102 | } |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 103 | cmd.rlen = 1; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 104 | ret = si2157_cmd_execute(dev, &cmd); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 105 | if (ret) |
| 106 | goto err; |
| 107 | |
| 108 | /* query chip revision */ |
| 109 | memcpy(cmd.args, "\x02", 1); |
| 110 | cmd.wlen = 1; |
| 111 | cmd.rlen = 13; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 112 | ret = si2157_cmd_execute(dev, &cmd); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 113 | if (ret) |
| 114 | goto err; |
| 115 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 116 | chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 | |
| 117 | cmd.args[4] << 0; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 118 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 119 | #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0) |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 120 | #define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0) |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 121 | #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0) |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 122 | #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0) |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 123 | #define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0) |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 124 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 125 | switch (chip_id) { |
| 126 | case SI2158_A20: |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 127 | case SI2148_A20: |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 128 | fw_file = SI2158_A20_FIRMWARE; |
| 129 | break; |
| 130 | case SI2157_A30: |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 131 | case SI2147_A30: |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 132 | case SI2146_A10: |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 133 | goto skip_fw_download; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 134 | default: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 135 | dev_err(&dev->client->dev, |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 136 | "unknown chip version Si21%d-%c%c%c\n", |
| 137 | cmd.args[2], cmd.args[1], |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 138 | cmd.args[3], cmd.args[4]); |
| 139 | ret = -EINVAL; |
| 140 | goto err; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 141 | } |
| 142 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 143 | /* cold state - try to download firmware */ |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 144 | dev_info(&dev->client->dev, "found a '%s' in cold state\n", |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 145 | si2157_ops.info.name); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 146 | |
| 147 | /* request the firmware, this will block and timeout */ |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 148 | ret = request_firmware(&fw, fw_file, &dev->client->dev); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 149 | if (ret) { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 150 | dev_err(&dev->client->dev, "firmware file '%s' not found\n", |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 151 | fw_file); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 152 | goto err; |
| 153 | } |
| 154 | |
| 155 | /* firmware should be n chunks of 17 bytes */ |
| 156 | if (fw->size % 17 != 0) { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 157 | dev_err(&dev->client->dev, "firmware file '%s' is invalid\n", |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 158 | fw_file); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 159 | ret = -EINVAL; |
Markus Elfring | e358c8a | 2014-11-30 16:48:24 -0300 | [diff] [blame] | 160 | goto fw_release_exit; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 161 | } |
| 162 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 163 | dev_info(&dev->client->dev, "downloading firmware from file '%s'\n", |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 164 | fw_file); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 165 | |
| 166 | for (remaining = fw->size; remaining > 0; remaining -= 17) { |
| 167 | len = fw->data[fw->size - remaining]; |
| 168 | memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len); |
| 169 | cmd.wlen = len; |
| 170 | cmd.rlen = 1; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 171 | ret = si2157_cmd_execute(dev, &cmd); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 172 | if (ret) { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 173 | dev_err(&dev->client->dev, |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 174 | "firmware download failed=%d\n", |
| 175 | ret); |
Markus Elfring | e358c8a | 2014-11-30 16:48:24 -0300 | [diff] [blame] | 176 | goto fw_release_exit; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | release_firmware(fw); |
| 181 | fw = NULL; |
| 182 | |
| 183 | skip_fw_download: |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 184 | /* reboot the tuner with new firmware? */ |
| 185 | memcpy(cmd.args, "\x01\x01", 2); |
| 186 | cmd.wlen = 2; |
| 187 | cmd.rlen = 1; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 188 | ret = si2157_cmd_execute(dev, &cmd); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 189 | if (ret) |
| 190 | goto err; |
| 191 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 192 | dev->fw_loaded = true; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 193 | |
Olli Salonen | 4cbf6ed | 2014-08-25 15:07:03 -0300 | [diff] [blame] | 194 | warm: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 195 | dev->active = true; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 196 | return 0; |
Olli Salonen | 4cbf6ed | 2014-08-25 15:07:03 -0300 | [diff] [blame] | 197 | |
Markus Elfring | e358c8a | 2014-11-30 16:48:24 -0300 | [diff] [blame] | 198 | fw_release_exit: |
Markus Elfring | dc89cfc | 2014-11-30 15:05:48 -0300 | [diff] [blame] | 199 | release_firmware(fw); |
Markus Elfring | e358c8a | 2014-11-30 16:48:24 -0300 | [diff] [blame] | 200 | err: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 201 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 202 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static int si2157_sleep(struct dvb_frontend *fe) |
| 206 | { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 207 | struct si2157_dev *dev = fe->tuner_priv; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 208 | int ret; |
| 209 | struct si2157_cmd cmd; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 210 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 211 | dev_dbg(&dev->client->dev, "\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 212 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 213 | dev->active = false; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 214 | |
Olli Salonen | 0e38233 | 2014-08-25 15:07:02 -0300 | [diff] [blame] | 215 | /* standby */ |
| 216 | memcpy(cmd.args, "\x16\x00", 2); |
| 217 | cmd.wlen = 2; |
| 218 | cmd.rlen = 1; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 219 | ret = si2157_cmd_execute(dev, &cmd); |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 220 | if (ret) |
| 221 | goto err; |
| 222 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 223 | return 0; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 224 | err: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 225 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 226 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int si2157_set_params(struct dvb_frontend *fe) |
| 230 | { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 231 | struct si2157_dev *dev = fe->tuner_priv; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 232 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 233 | int ret; |
| 234 | struct si2157_cmd cmd; |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 235 | u8 bandwidth, delivery_system; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 236 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 237 | dev_dbg(&dev->client->dev, |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 238 | "delivery_system=%d frequency=%u bandwidth_hz=%u\n", |
| 239 | c->delivery_system, c->frequency, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 240 | c->bandwidth_hz); |
| 241 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 242 | if (!dev->active) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 243 | ret = -EAGAIN; |
| 244 | goto err; |
| 245 | } |
| 246 | |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 247 | if (c->bandwidth_hz <= 6000000) |
| 248 | bandwidth = 0x06; |
| 249 | else if (c->bandwidth_hz <= 7000000) |
| 250 | bandwidth = 0x07; |
| 251 | else if (c->bandwidth_hz <= 8000000) |
| 252 | bandwidth = 0x08; |
| 253 | else |
| 254 | bandwidth = 0x0f; |
| 255 | |
| 256 | switch (c->delivery_system) { |
Olli Salonen | 5cd62db | 2014-08-17 02:24:49 -0300 | [diff] [blame] | 257 | case SYS_ATSC: |
| 258 | delivery_system = 0x00; |
| 259 | break; |
Olli Salonen | 0d1165f | 2014-10-30 07:43:16 -0300 | [diff] [blame] | 260 | case SYS_DVBC_ANNEX_B: |
| 261 | delivery_system = 0x10; |
| 262 | break; |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 263 | case SYS_DVBT: |
| 264 | case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */ |
| 265 | delivery_system = 0x20; |
| 266 | break; |
| 267 | case SYS_DVBC_ANNEX_A: |
| 268 | delivery_system = 0x30; |
| 269 | break; |
| 270 | default: |
| 271 | ret = -EINVAL; |
| 272 | goto err; |
| 273 | } |
| 274 | |
| 275 | memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6); |
| 276 | cmd.args[4] = delivery_system | bandwidth; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 277 | if (dev->inversion) |
Matthias Schwarzott | 05024ef | 2014-07-15 16:34:36 -0300 | [diff] [blame] | 278 | cmd.args[5] = 0x01; |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 279 | cmd.wlen = 6; |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 280 | cmd.rlen = 4; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 281 | ret = si2157_cmd_execute(dev, &cmd); |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 282 | if (ret) |
| 283 | goto err; |
| 284 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 285 | if (dev->chiptype == SI2157_CHIPTYPE_SI2146) |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 286 | memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6); |
| 287 | else |
| 288 | memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6); |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 289 | cmd.wlen = 6; |
| 290 | cmd.rlen = 4; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 291 | ret = si2157_cmd_execute(dev, &cmd); |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 292 | if (ret) |
| 293 | goto err; |
| 294 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 295 | /* set frequency */ |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 296 | memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 297 | cmd.args[4] = (c->frequency >> 0) & 0xff; |
| 298 | cmd.args[5] = (c->frequency >> 8) & 0xff; |
| 299 | cmd.args[6] = (c->frequency >> 16) & 0xff; |
| 300 | cmd.args[7] = (c->frequency >> 24) & 0xff; |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 301 | cmd.wlen = 8; |
| 302 | cmd.rlen = 1; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 303 | ret = si2157_cmd_execute(dev, &cmd); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 304 | if (ret) |
| 305 | goto err; |
| 306 | |
| 307 | return 0; |
| 308 | err: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 309 | dev_dbg(&dev->client->dev, "failed=%d\n", ret); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 310 | return ret; |
| 311 | } |
| 312 | |
Matthias Schwarzott | 1140540 | 2014-07-15 04:58:40 -0300 | [diff] [blame] | 313 | static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 314 | { |
| 315 | *frequency = 5000000; /* default value of property 0x0706 */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
Olli Salonen | 6cc8a35 | 2014-07-18 02:41:12 -0300 | [diff] [blame] | 319 | static const struct dvb_tuner_ops si2157_ops = { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 320 | .info = { |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 321 | .name = "Silicon Labs Si2146/2147/2148/2157/2158", |
Antti Palosaari | ae4c891 | 2014-04-12 01:53:51 -0300 | [diff] [blame] | 322 | .frequency_min = 110000000, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 323 | .frequency_max = 862000000, |
| 324 | }, |
| 325 | |
| 326 | .init = si2157_init, |
| 327 | .sleep = si2157_sleep, |
| 328 | .set_params = si2157_set_params, |
Matthias Schwarzott | 1140540 | 2014-07-15 04:58:40 -0300 | [diff] [blame] | 329 | .get_if_frequency = si2157_get_if_frequency, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | static int si2157_probe(struct i2c_client *client, |
| 333 | const struct i2c_device_id *id) |
| 334 | { |
| 335 | struct si2157_config *cfg = client->dev.platform_data; |
| 336 | struct dvb_frontend *fe = cfg->fe; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 337 | struct si2157_dev *dev; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 338 | struct si2157_cmd cmd; |
| 339 | int ret; |
| 340 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 341 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 342 | if (!dev) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 343 | ret = -ENOMEM; |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 344 | dev_err(&client->dev, "kzalloc() failed\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 345 | goto err; |
| 346 | } |
| 347 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 348 | dev->client = client; |
| 349 | dev->fe = cfg->fe; |
| 350 | dev->inversion = cfg->inversion; |
| 351 | dev->fw_loaded = false; |
| 352 | dev->chiptype = (u8)id->driver_data; |
| 353 | mutex_init(&dev->i2c_mutex); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 354 | |
| 355 | /* check if the tuner is there */ |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 356 | cmd.wlen = 0; |
| 357 | cmd.rlen = 1; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 358 | ret = si2157_cmd_execute(dev, &cmd); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 359 | if (ret) |
| 360 | goto err; |
| 361 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 362 | fe->tuner_priv = dev; |
| 363 | memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops)); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 364 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 365 | i2c_set_clientdata(client, dev); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 366 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 367 | dev_info(&dev->client->dev, |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 368 | "Silicon Labs %s successfully attached\n", |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 369 | dev->chiptype == SI2157_CHIPTYPE_SI2146 ? |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 370 | "Si2146" : "Si2147/2148/2157/2158"); |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 371 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 372 | return 0; |
| 373 | err: |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 374 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 375 | kfree(dev); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | static int si2157_remove(struct i2c_client *client) |
| 380 | { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 381 | struct si2157_dev *dev = i2c_get_clientdata(client); |
| 382 | struct dvb_frontend *fe = dev->fe; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 383 | |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 384 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 385 | |
| 386 | memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); |
| 387 | fe->tuner_priv = NULL; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame^] | 388 | kfree(dev); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static const struct i2c_device_id si2157_id[] = { |
| 394 | {"si2157", 0}, |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 395 | {"si2146", 1}, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 396 | {} |
| 397 | }; |
| 398 | MODULE_DEVICE_TABLE(i2c, si2157_id); |
| 399 | |
| 400 | static struct i2c_driver si2157_driver = { |
| 401 | .driver = { |
| 402 | .owner = THIS_MODULE, |
| 403 | .name = "si2157", |
| 404 | }, |
| 405 | .probe = si2157_probe, |
| 406 | .remove = si2157_remove, |
| 407 | .id_table = si2157_id, |
| 408 | }; |
| 409 | |
| 410 | module_i2c_driver(si2157_driver); |
| 411 | |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 412 | MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 413 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 414 | MODULE_LICENSE("GPL"); |
Antti Palosaari | bac53a2 | 2014-07-13 16:13:49 -0300 | [diff] [blame] | 415 | MODULE_FIRMWARE(SI2158_A20_FIRMWARE); |