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 | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 22 | static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd) |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 23 | { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 24 | struct si2157_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 25 | int ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 26 | unsigned long timeout; |
| 27 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 28 | mutex_lock(&dev->i2c_mutex); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 29 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 30 | if (cmd->wlen) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 31 | /* write cmd and args for firmware */ |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 32 | ret = i2c_master_send(client, cmd->args, cmd->wlen); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 33 | if (ret < 0) { |
| 34 | goto err_mutex_unlock; |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 35 | } else if (ret != cmd->wlen) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 36 | ret = -EREMOTEIO; |
| 37 | goto err_mutex_unlock; |
| 38 | } |
| 39 | } |
| 40 | |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 41 | if (cmd->rlen) { |
| 42 | /* wait cmd execution terminate */ |
| 43 | #define TIMEOUT 80 |
| 44 | timeout = jiffies + msecs_to_jiffies(TIMEOUT); |
| 45 | while (!time_after(jiffies, timeout)) { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 46 | ret = i2c_master_recv(client, cmd->args, cmd->rlen); |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -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; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 57 | } |
| 58 | |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 59 | dev_dbg(&client->dev, "cmd execution took %d ms\n", |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 60 | jiffies_to_msecs(jiffies) - |
| 61 | (jiffies_to_msecs(timeout) - TIMEOUT)); |
| 62 | |
| 63 | if (!((cmd->args[0] >> 7) & 0x01)) { |
| 64 | ret = -ETIMEDOUT; |
| 65 | goto err_mutex_unlock; |
| 66 | } |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 67 | } |
| 68 | |
Antti Palosaari | 7cbf5d7 | 2014-12-06 14:04:05 -0300 | [diff] [blame] | 69 | mutex_unlock(&dev->i2c_mutex); |
| 70 | return 0; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 71 | |
| 72 | err_mutex_unlock: |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 73 | mutex_unlock(&dev->i2c_mutex); |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 74 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static int si2157_init(struct dvb_frontend *fe) |
| 79 | { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 80 | struct i2c_client *client = fe->tuner_priv; |
| 81 | struct si2157_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 2f1ea29 | 2014-09-07 11:20:34 -0300 | [diff] [blame] | 82 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 83 | int ret, len, remaining; |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 84 | struct si2157_cmd cmd; |
Antti Palosaari | 78dab40 | 2014-12-06 14:51:17 -0300 | [diff] [blame] | 85 | const struct firmware *fw; |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 86 | const char *fw_name; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 87 | unsigned int chip_id; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 88 | |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 89 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 90 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 91 | if (dev->fw_loaded) |
Olli Salonen | 4cbf6ed | 2014-08-25 15:07:03 -0300 | [diff] [blame] | 92 | goto warm; |
| 93 | |
| 94 | /* power up */ |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 95 | if (dev->chiptype == SI2157_CHIPTYPE_SI2146) { |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 96 | memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9); |
| 97 | cmd.wlen = 9; |
| 98 | } else { |
| 99 | memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15); |
| 100 | cmd.wlen = 15; |
| 101 | } |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 102 | cmd.rlen = 1; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 103 | ret = si2157_cmd_execute(client, &cmd); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 104 | if (ret) |
| 105 | goto err; |
| 106 | |
| 107 | /* query chip revision */ |
| 108 | memcpy(cmd.args, "\x02", 1); |
| 109 | cmd.wlen = 1; |
| 110 | cmd.rlen = 13; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 111 | ret = si2157_cmd_execute(client, &cmd); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 112 | if (ret) |
| 113 | goto err; |
| 114 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 115 | chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 | |
| 116 | cmd.args[4] << 0; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 117 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 118 | #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0) |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 119 | #define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0) |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 120 | #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0) |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 121 | #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0) |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 122 | #define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0) |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 123 | |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 124 | switch (chip_id) { |
| 125 | case SI2158_A20: |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 126 | case SI2148_A20: |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 127 | fw_name = SI2158_A20_FIRMWARE; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 128 | break; |
| 129 | case SI2157_A30: |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 130 | case SI2147_A30: |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 131 | case SI2146_A10: |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 132 | fw_name = NULL; |
Antti Palosaari | 6c37253 | 2014-12-06 18:12:39 -0300 | [diff] [blame] | 133 | break; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 134 | default: |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 135 | dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n", |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 136 | cmd.args[2], cmd.args[1], |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 137 | cmd.args[3], cmd.args[4]); |
| 138 | ret = -EINVAL; |
| 139 | goto err; |
Olli Salonen | 1b92373 | 2014-07-13 10:52:20 -0300 | [diff] [blame] | 140 | } |
| 141 | |
Antti Palosaari | 6c37253 | 2014-12-06 18:12:39 -0300 | [diff] [blame] | 142 | dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n", |
| 143 | cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]); |
| 144 | |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 145 | if (fw_name == NULL) |
Antti Palosaari | 6c37253 | 2014-12-06 18:12:39 -0300 | [diff] [blame] | 146 | goto skip_fw_download; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 147 | |
| 148 | /* request the firmware, this will block and timeout */ |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 149 | ret = request_firmware(&fw, fw_name, &client->dev); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 150 | if (ret) { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 151 | dev_err(&client->dev, "firmware file '%s' not found\n", |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 152 | fw_name); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 153 | goto err; |
| 154 | } |
| 155 | |
| 156 | /* firmware should be n chunks of 17 bytes */ |
| 157 | if (fw->size % 17 != 0) { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 158 | dev_err(&client->dev, "firmware file '%s' is invalid\n", |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 159 | fw_name); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 160 | ret = -EINVAL; |
Antti Palosaari | 78dab40 | 2014-12-06 14:51:17 -0300 | [diff] [blame] | 161 | goto err_release_firmware; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 162 | } |
| 163 | |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 164 | dev_info(&client->dev, "downloading firmware from file '%s'\n", |
Antti Palosaari | 08272db | 2014-12-06 18:15:18 -0300 | [diff] [blame] | 165 | fw_name); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 166 | |
| 167 | for (remaining = fw->size; remaining > 0; remaining -= 17) { |
| 168 | len = fw->data[fw->size - remaining]; |
| 169 | memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len); |
| 170 | cmd.wlen = len; |
| 171 | cmd.rlen = 1; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 172 | ret = si2157_cmd_execute(client, &cmd); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 173 | if (ret) { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 174 | dev_err(&client->dev, "firmware download failed %d\n", |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 175 | ret); |
Antti Palosaari | 78dab40 | 2014-12-06 14:51:17 -0300 | [diff] [blame] | 176 | goto err_release_firmware; |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | release_firmware(fw); |
Antti Palosaari | 7d6bc60 | 2014-07-13 20:05:28 -0300 | [diff] [blame] | 181 | |
| 182 | skip_fw_download: |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 183 | /* reboot the tuner with new firmware? */ |
| 184 | memcpy(cmd.args, "\x01\x01", 2); |
| 185 | cmd.wlen = 2; |
| 186 | cmd.rlen = 1; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 187 | ret = si2157_cmd_execute(client, &cmd); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 188 | if (ret) |
| 189 | goto err; |
| 190 | |
Antti Palosaari | c701184 | 2014-12-06 17:25:24 -0300 | [diff] [blame] | 191 | /* query firmware version */ |
| 192 | memcpy(cmd.args, "\x11", 1); |
| 193 | cmd.wlen = 1; |
| 194 | cmd.rlen = 10; |
| 195 | ret = si2157_cmd_execute(client, &cmd); |
| 196 | if (ret) |
| 197 | goto err; |
| 198 | |
| 199 | dev_info(&client->dev, "firmware version: %c.%c.%d\n", |
| 200 | cmd.args[6], cmd.args[7], cmd.args[8]); |
| 201 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 202 | dev->fw_loaded = true; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 203 | |
Olli Salonen | 4cbf6ed | 2014-08-25 15:07:03 -0300 | [diff] [blame] | 204 | warm: |
Antti Palosaari | 2f1ea29 | 2014-09-07 11:20:34 -0300 | [diff] [blame] | 205 | /* init statistics in order signal app which are supported */ |
| 206 | c->strength.len = 1; |
| 207 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 208 | /* start statistics polling */ |
| 209 | schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(1000)); |
| 210 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 211 | dev->active = true; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 212 | return 0; |
Antti Palosaari | 78dab40 | 2014-12-06 14:51:17 -0300 | [diff] [blame] | 213 | err_release_firmware: |
Markus Elfring | dc89cfc | 2014-11-30 15:05:48 -0300 | [diff] [blame] | 214 | release_firmware(fw); |
Markus Elfring | e358c8a | 2014-11-30 16:48:24 -0300 | [diff] [blame] | 215 | err: |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 216 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 217 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int si2157_sleep(struct dvb_frontend *fe) |
| 221 | { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 222 | struct i2c_client *client = fe->tuner_priv; |
| 223 | struct si2157_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 224 | int ret; |
| 225 | struct si2157_cmd cmd; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 226 | |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 227 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 228 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 229 | dev->active = false; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 230 | |
Antti Palosaari | 2f1ea29 | 2014-09-07 11:20:34 -0300 | [diff] [blame] | 231 | /* stop statistics polling */ |
| 232 | cancel_delayed_work_sync(&dev->stat_work); |
| 233 | |
Olli Salonen | 0e38233 | 2014-08-25 15:07:02 -0300 | [diff] [blame] | 234 | /* standby */ |
| 235 | memcpy(cmd.args, "\x16\x00", 2); |
| 236 | cmd.wlen = 2; |
| 237 | cmd.rlen = 1; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 238 | ret = si2157_cmd_execute(client, &cmd); |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 239 | if (ret) |
| 240 | goto err; |
| 241 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 242 | return 0; |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 243 | err: |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 244 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | a83d7d1 | 2014-07-09 18:37:30 -0300 | [diff] [blame] | 245 | return ret; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static int si2157_set_params(struct dvb_frontend *fe) |
| 249 | { |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 250 | struct i2c_client *client = fe->tuner_priv; |
| 251 | struct si2157_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 252 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 253 | int ret; |
| 254 | struct si2157_cmd cmd; |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 255 | u8 bandwidth, delivery_system; |
Olli Salonen | c28aebe | 2015-02-28 12:25:23 -0300 | [diff] [blame] | 256 | u32 if_frequency = 5000000; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 257 | |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 258 | dev_dbg(&client->dev, |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 259 | "delivery_system=%d frequency=%u bandwidth_hz=%u\n", |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 260 | c->delivery_system, c->frequency, c->bandwidth_hz); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 261 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 262 | if (!dev->active) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 263 | ret = -EAGAIN; |
| 264 | goto err; |
| 265 | } |
| 266 | |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 267 | if (c->bandwidth_hz <= 6000000) |
| 268 | bandwidth = 0x06; |
| 269 | else if (c->bandwidth_hz <= 7000000) |
| 270 | bandwidth = 0x07; |
| 271 | else if (c->bandwidth_hz <= 8000000) |
| 272 | bandwidth = 0x08; |
| 273 | else |
| 274 | bandwidth = 0x0f; |
| 275 | |
| 276 | switch (c->delivery_system) { |
Olli Salonen | 5cd62db | 2014-08-17 02:24:49 -0300 | [diff] [blame] | 277 | case SYS_ATSC: |
| 278 | delivery_system = 0x00; |
Olli Salonen | c28aebe | 2015-02-28 12:25:23 -0300 | [diff] [blame] | 279 | if_frequency = 3250000; |
Olli Salonen | 5cd62db | 2014-08-17 02:24:49 -0300 | [diff] [blame] | 280 | break; |
Olli Salonen | 0d1165f | 2014-10-30 07:43:16 -0300 | [diff] [blame] | 281 | case SYS_DVBC_ANNEX_B: |
| 282 | delivery_system = 0x10; |
Olli Salonen | c28aebe | 2015-02-28 12:25:23 -0300 | [diff] [blame] | 283 | if_frequency = 4000000; |
Olli Salonen | 0d1165f | 2014-10-30 07:43:16 -0300 | [diff] [blame] | 284 | break; |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 285 | case SYS_DVBT: |
| 286 | case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */ |
| 287 | delivery_system = 0x20; |
| 288 | break; |
| 289 | case SYS_DVBC_ANNEX_A: |
| 290 | delivery_system = 0x30; |
| 291 | break; |
| 292 | default: |
| 293 | ret = -EINVAL; |
| 294 | goto err; |
| 295 | } |
| 296 | |
| 297 | memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6); |
| 298 | cmd.args[4] = delivery_system | bandwidth; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 299 | if (dev->inversion) |
Matthias Schwarzott | 05024ef | 2014-07-15 16:34:36 -0300 | [diff] [blame] | 300 | cmd.args[5] = 0x01; |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 301 | cmd.wlen = 6; |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 302 | cmd.rlen = 4; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 303 | ret = si2157_cmd_execute(client, &cmd); |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 304 | if (ret) |
| 305 | goto err; |
| 306 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 307 | if (dev->chiptype == SI2157_CHIPTYPE_SI2146) |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 308 | memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6); |
| 309 | else |
Olli Salonen | ee3c3e4 | 2015-05-05 13:54:17 -0300 | [diff] [blame] | 310 | memcpy(cmd.args, "\x14\x00\x02\x07\x00\x00", 6); |
| 311 | cmd.args[4] = dev->if_port; |
Olli Salonen | d87a505 | 2014-09-11 17:01:38 -0300 | [diff] [blame] | 312 | cmd.wlen = 6; |
| 313 | cmd.rlen = 4; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 314 | ret = si2157_cmd_execute(client, &cmd); |
Olli Salonen | a1dad50 | 2014-07-13 10:52:21 -0300 | [diff] [blame] | 315 | if (ret) |
| 316 | goto err; |
| 317 | |
Olli Salonen | c28aebe | 2015-02-28 12:25:23 -0300 | [diff] [blame] | 318 | /* set if frequency if needed */ |
| 319 | if (if_frequency != dev->if_frequency) { |
| 320 | memcpy(cmd.args, "\x14\x00\x06\x07", 4); |
| 321 | cmd.args[4] = (if_frequency / 1000) & 0xff; |
| 322 | cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff; |
| 323 | cmd.wlen = 6; |
| 324 | cmd.rlen = 4; |
| 325 | ret = si2157_cmd_execute(client, &cmd); |
| 326 | if (ret) |
| 327 | goto err; |
| 328 | |
| 329 | dev->if_frequency = if_frequency; |
| 330 | } |
| 331 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 332 | /* set frequency */ |
Olli Salonen | b154121 | 2014-07-13 10:52:19 -0300 | [diff] [blame] | 333 | memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 334 | cmd.args[4] = (c->frequency >> 0) & 0xff; |
| 335 | cmd.args[5] = (c->frequency >> 8) & 0xff; |
| 336 | cmd.args[6] = (c->frequency >> 16) & 0xff; |
| 337 | cmd.args[7] = (c->frequency >> 24) & 0xff; |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 338 | cmd.wlen = 8; |
| 339 | cmd.rlen = 1; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 340 | ret = si2157_cmd_execute(client, &cmd); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 341 | if (ret) |
| 342 | goto err; |
| 343 | |
| 344 | return 0; |
| 345 | err: |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 346 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 347 | return ret; |
| 348 | } |
| 349 | |
Matthias Schwarzott | 1140540 | 2014-07-15 04:58:40 -0300 | [diff] [blame] | 350 | static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 351 | { |
Olli Salonen | c28aebe | 2015-02-28 12:25:23 -0300 | [diff] [blame] | 352 | struct i2c_client *client = fe->tuner_priv; |
| 353 | struct si2157_dev *dev = i2c_get_clientdata(client); |
| 354 | |
| 355 | *frequency = dev->if_frequency; |
Matthias Schwarzott | 1140540 | 2014-07-15 04:58:40 -0300 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Olli Salonen | 6cc8a35 | 2014-07-18 02:41:12 -0300 | [diff] [blame] | 359 | static const struct dvb_tuner_ops si2157_ops = { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 360 | .info = { |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 361 | .name = "Silicon Labs Si2146/2147/2148/2157/2158", |
Olli Salonen | 18acc6a | 2015-03-03 13:28:35 -0300 | [diff] [blame] | 362 | .frequency_min = 55000000, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 363 | .frequency_max = 862000000, |
| 364 | }, |
| 365 | |
| 366 | .init = si2157_init, |
| 367 | .sleep = si2157_sleep, |
| 368 | .set_params = si2157_set_params, |
Matthias Schwarzott | 1140540 | 2014-07-15 04:58:40 -0300 | [diff] [blame] | 369 | .get_if_frequency = si2157_get_if_frequency, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 370 | }; |
| 371 | |
Antti Palosaari | 2f1ea29 | 2014-09-07 11:20:34 -0300 | [diff] [blame] | 372 | static void si2157_stat_work(struct work_struct *work) |
| 373 | { |
| 374 | struct si2157_dev *dev = container_of(work, struct si2157_dev, stat_work.work); |
| 375 | struct dvb_frontend *fe = dev->fe; |
| 376 | struct i2c_client *client = fe->tuner_priv; |
| 377 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 378 | struct si2157_cmd cmd; |
| 379 | int ret; |
| 380 | |
| 381 | dev_dbg(&client->dev, "\n"); |
| 382 | |
| 383 | memcpy(cmd.args, "\x42\x00", 2); |
| 384 | cmd.wlen = 2; |
| 385 | cmd.rlen = 12; |
| 386 | ret = si2157_cmd_execute(client, &cmd); |
| 387 | if (ret) |
| 388 | goto err; |
| 389 | |
| 390 | c->strength.stat[0].scale = FE_SCALE_DECIBEL; |
| 391 | c->strength.stat[0].svalue = (s8) cmd.args[3] * 1000; |
| 392 | |
| 393 | schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000)); |
| 394 | return; |
| 395 | err: |
| 396 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 397 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 398 | } |
| 399 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 400 | static int si2157_probe(struct i2c_client *client, |
| 401 | const struct i2c_device_id *id) |
| 402 | { |
| 403 | struct si2157_config *cfg = client->dev.platform_data; |
| 404 | struct dvb_frontend *fe = cfg->fe; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 405 | struct si2157_dev *dev; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 406 | struct si2157_cmd cmd; |
| 407 | int ret; |
| 408 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 409 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 410 | if (!dev) { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 411 | ret = -ENOMEM; |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 412 | dev_err(&client->dev, "kzalloc() failed\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 413 | goto err; |
| 414 | } |
| 415 | |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 416 | i2c_set_clientdata(client, dev); |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 417 | dev->fe = cfg->fe; |
| 418 | dev->inversion = cfg->inversion; |
Olli Salonen | ee3c3e4 | 2015-05-05 13:54:17 -0300 | [diff] [blame] | 419 | dev->if_port = cfg->if_port; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 420 | dev->fw_loaded = false; |
| 421 | dev->chiptype = (u8)id->driver_data; |
Olli Salonen | c28aebe | 2015-02-28 12:25:23 -0300 | [diff] [blame] | 422 | dev->if_frequency = 5000000; /* default value of property 0x0706 */ |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 423 | mutex_init(&dev->i2c_mutex); |
Antti Palosaari | 2f1ea29 | 2014-09-07 11:20:34 -0300 | [diff] [blame] | 424 | INIT_DELAYED_WORK(&dev->stat_work, si2157_stat_work); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 425 | |
| 426 | /* check if the tuner is there */ |
Antti Palosaari | e6b4380 | 2014-07-10 06:02:53 -0300 | [diff] [blame] | 427 | cmd.wlen = 0; |
| 428 | cmd.rlen = 1; |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 429 | ret = si2157_cmd_execute(client, &cmd); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 430 | if (ret) |
Antti Palosaari | e7f3f9f3 | 2014-12-06 15:13:31 -0300 | [diff] [blame] | 431 | goto err_kfree; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 432 | |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 433 | memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops)); |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 434 | fe->tuner_priv = client; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 435 | |
Antti Palosaari | 8fe5172 | 2014-12-06 14:40:06 -0300 | [diff] [blame] | 436 | dev_info(&client->dev, "Silicon Labs %s successfully attached\n", |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 437 | dev->chiptype == SI2157_CHIPTYPE_SI2146 ? |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 438 | "Si2146" : "Si2147/2148/2157/2158"); |
Olli Salonen | 073f384 | 2014-11-24 03:57:33 -0300 | [diff] [blame] | 439 | |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 440 | return 0; |
Antti Palosaari | e7f3f9f3 | 2014-12-06 15:13:31 -0300 | [diff] [blame] | 441 | |
| 442 | err_kfree: |
| 443 | kfree(dev); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 444 | err: |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 445 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | static int si2157_remove(struct i2c_client *client) |
| 450 | { |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 451 | struct si2157_dev *dev = i2c_get_clientdata(client); |
| 452 | struct dvb_frontend *fe = dev->fe; |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 453 | |
Olli Salonen | 67d0113 | 2014-08-05 09:03:54 -0300 | [diff] [blame] | 454 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 455 | |
| 456 | memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); |
| 457 | fe->tuner_priv = NULL; |
Antti Palosaari | 70f13ff | 2014-12-05 17:46:30 -0300 | [diff] [blame] | 458 | kfree(dev); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
Antti Palosaari | c129223 | 2014-12-06 15:07:45 -0300 | [diff] [blame] | 463 | static const struct i2c_device_id si2157_id_table[] = { |
| 464 | {"si2157", SI2157_CHIPTYPE_SI2157}, |
| 465 | {"si2146", SI2157_CHIPTYPE_SI2146}, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 466 | {} |
| 467 | }; |
Antti Palosaari | c129223 | 2014-12-06 15:07:45 -0300 | [diff] [blame] | 468 | MODULE_DEVICE_TABLE(i2c, si2157_id_table); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 469 | |
| 470 | static struct i2c_driver si2157_driver = { |
| 471 | .driver = { |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 472 | .name = "si2157", |
| 473 | }, |
| 474 | .probe = si2157_probe, |
| 475 | .remove = si2157_remove, |
Antti Palosaari | c129223 | 2014-12-06 15:07:45 -0300 | [diff] [blame] | 476 | .id_table = si2157_id_table, |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | module_i2c_driver(si2157_driver); |
| 480 | |
CrazyCat | 17ad09f | 2014-11-14 18:19:37 -0300 | [diff] [blame] | 481 | MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver"); |
Antti Palosaari | 930a873 | 2014-04-10 21:58:10 -0300 | [diff] [blame] | 482 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 483 | MODULE_LICENSE("GPL"); |
Antti Palosaari | bac53a2 | 2014-07-13 16:13:49 -0300 | [diff] [blame] | 484 | MODULE_FIRMWARE(SI2158_A20_FIRMWARE); |