Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Realtek RTL2830 DVB-T demodulator driver |
| 3 | * |
| 4 | * Copyright (C) 2011 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 | * |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "rtl2830_priv.h" |
| 19 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 20 | /* Our regmap is bypassing I2C adapter lock, thus we do it! */ |
Mauro Carvalho Chehab | d858b0e | 2015-02-03 16:32:34 -0200 | [diff] [blame] | 21 | static int rtl2830_bulk_write(struct i2c_client *client, unsigned int reg, |
| 22 | const void *val, size_t val_count) |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 23 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 24 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 25 | int ret; |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 26 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 27 | i2c_lock_adapter(client->adapter); |
| 28 | ret = regmap_bulk_write(dev->regmap, reg, val, val_count); |
| 29 | i2c_unlock_adapter(client->adapter); |
Antti Palosaari | fd4cfa8 | 2014-12-09 16:14:41 -0300 | [diff] [blame] | 30 | return ret; |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 31 | } |
| 32 | |
Mauro Carvalho Chehab | d858b0e | 2015-02-03 16:32:34 -0200 | [diff] [blame] | 33 | static int rtl2830_update_bits(struct i2c_client *client, unsigned int reg, |
| 34 | unsigned int mask, unsigned int val) |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 35 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 36 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 37 | int ret; |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 38 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 39 | i2c_lock_adapter(client->adapter); |
| 40 | ret = regmap_update_bits(dev->regmap, reg, mask, val); |
| 41 | i2c_unlock_adapter(client->adapter); |
Antti Palosaari | fd4cfa8 | 2014-12-09 16:14:41 -0300 | [diff] [blame] | 42 | return ret; |
Antti Palosaari | 0485a70 | 2011-08-04 20:27:19 -0300 | [diff] [blame] | 43 | } |
| 44 | |
Mauro Carvalho Chehab | d858b0e | 2015-02-03 16:32:34 -0200 | [diff] [blame] | 45 | static int rtl2830_bulk_read(struct i2c_client *client, unsigned int reg, |
| 46 | void *val, size_t val_count) |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 47 | { |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 48 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 49 | int ret; |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 50 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 51 | i2c_lock_adapter(client->adapter); |
| 52 | ret = regmap_bulk_read(dev->regmap, reg, val, val_count); |
| 53 | i2c_unlock_adapter(client->adapter); |
| 54 | return ret; |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static int rtl2830_init(struct dvb_frontend *fe) |
| 58 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 59 | struct i2c_client *client = fe->demodulator_priv; |
| 60 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 47b4dbf | 2014-12-09 06:14:36 -0300 | [diff] [blame] | 61 | struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 62 | int ret, i; |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 63 | struct rtl2830_reg_val_mask tab[] = { |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 64 | {0x00d, 0x01, 0x03}, |
| 65 | {0x00d, 0x10, 0x10}, |
| 66 | {0x104, 0x00, 0x1e}, |
| 67 | {0x105, 0x80, 0x80}, |
| 68 | {0x110, 0x02, 0x03}, |
| 69 | {0x110, 0x08, 0x0c}, |
| 70 | {0x17b, 0x00, 0x40}, |
| 71 | {0x17d, 0x05, 0x0f}, |
| 72 | {0x17d, 0x50, 0xf0}, |
| 73 | {0x18c, 0x08, 0x0f}, |
| 74 | {0x18d, 0x00, 0xc0}, |
| 75 | {0x188, 0x05, 0x0f}, |
| 76 | {0x189, 0x00, 0xfc}, |
| 77 | {0x2d5, 0x02, 0x02}, |
| 78 | {0x2f1, 0x02, 0x06}, |
| 79 | {0x2f1, 0x20, 0xf8}, |
| 80 | {0x16d, 0x00, 0x01}, |
| 81 | {0x1a6, 0x00, 0x80}, |
| 82 | {0x106, dev->pdata->vtop, 0x3f}, |
| 83 | {0x107, dev->pdata->krf, 0x3f}, |
| 84 | {0x112, 0x28, 0xff}, |
| 85 | {0x103, dev->pdata->agc_targ_val, 0xff}, |
| 86 | {0x00a, 0x02, 0x07}, |
| 87 | {0x140, 0x0c, 0x3c}, |
| 88 | {0x140, 0x40, 0xc0}, |
| 89 | {0x15b, 0x05, 0x07}, |
| 90 | {0x15b, 0x28, 0x38}, |
| 91 | {0x15c, 0x05, 0x07}, |
| 92 | {0x15c, 0x28, 0x38}, |
| 93 | {0x115, dev->pdata->spec_inv, 0x01}, |
| 94 | {0x16f, 0x01, 0x07}, |
| 95 | {0x170, 0x18, 0x38}, |
| 96 | {0x172, 0x0f, 0x0f}, |
| 97 | {0x173, 0x08, 0x38}, |
| 98 | {0x175, 0x01, 0x07}, |
| 99 | {0x176, 0x00, 0xc0}, |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 103 | ret = rtl2830_update_bits(client, tab[i].reg, tab[i].mask, |
| 104 | tab[i].val); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 105 | if (ret) |
| 106 | goto err; |
| 107 | } |
| 108 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 109 | ret = rtl2830_bulk_write(client, 0x18f, "\x28\x00", 2); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 110 | if (ret) |
| 111 | goto err; |
| 112 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 113 | ret = rtl2830_bulk_write(client, 0x195, |
| 114 | "\x04\x06\x0a\x12\x0a\x12\x1e\x28", 8); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 115 | if (ret) |
| 116 | goto err; |
| 117 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 118 | /* TODO: spec init */ |
| 119 | |
| 120 | /* soft reset */ |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 121 | ret = rtl2830_update_bits(client, 0x101, 0x04, 0x04); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 122 | if (ret) |
| 123 | goto err; |
| 124 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 125 | ret = rtl2830_update_bits(client, 0x101, 0x04, 0x00); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 126 | if (ret) |
| 127 | goto err; |
| 128 | |
Antti Palosaari | 47b4dbf | 2014-12-09 06:14:36 -0300 | [diff] [blame] | 129 | /* init stats here in order signal app which stats are supported */ |
Antti Palosaari | 871f702 | 2014-12-09 08:49:44 -0300 | [diff] [blame] | 130 | c->strength.len = 1; |
| 131 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
Antti Palosaari | 47b4dbf | 2014-12-09 06:14:36 -0300 | [diff] [blame] | 132 | c->cnr.len = 1; |
| 133 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
Antti Palosaari | 5bb11ca | 2014-12-09 09:45:16 -0300 | [diff] [blame] | 134 | c->post_bit_error.len = 1; |
| 135 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 136 | c->post_bit_count.len = 1; |
| 137 | c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
Antti Palosaari | 47b4dbf | 2014-12-09 06:14:36 -0300 | [diff] [blame] | 138 | |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 139 | dev->sleeping = false; |
Antti Palosaari | a8567cf | 2012-01-21 22:40:58 -0300 | [diff] [blame] | 140 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 141 | return ret; |
| 142 | err: |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 143 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
Antti Palosaari | a8567cf | 2012-01-21 22:40:58 -0300 | [diff] [blame] | 147 | static int rtl2830_sleep(struct dvb_frontend *fe) |
| 148 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 149 | struct i2c_client *client = fe->demodulator_priv; |
| 150 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 151 | |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 152 | dev->sleeping = true; |
Antti Palosaari | 47b4dbf | 2014-12-09 06:14:36 -0300 | [diff] [blame] | 153 | dev->fe_status = 0; |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 154 | |
Antti Palosaari | a8567cf | 2012-01-21 22:40:58 -0300 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Mauro Carvalho Chehab | a17ff2e | 2012-10-27 11:24:08 -0300 | [diff] [blame] | 158 | static int rtl2830_get_tune_settings(struct dvb_frontend *fe, |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 159 | struct dvb_frontend_tune_settings *s) |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 160 | { |
| 161 | s->min_delay_ms = 500; |
| 162 | s->step_size = fe->ops.info.frequency_stepsize * 2; |
| 163 | s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int rtl2830_set_frontend(struct dvb_frontend *fe) |
| 169 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 170 | struct i2c_client *client = fe->demodulator_priv; |
| 171 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 172 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 173 | int ret, i; |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 174 | u64 num; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 175 | u8 buf[3], u8tmp; |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 176 | u32 if_ctl, if_frequency; |
Antti Palosaari | 3a2fca2 | 2012-09-12 20:23:49 -0300 | [diff] [blame] | 177 | static const u8 bw_params1[3][34] = { |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 178 | { |
| 179 | 0x1f, 0xf0, 0x1f, 0xf0, 0x1f, 0xfa, 0x00, 0x17, 0x00, 0x41, |
| 180 | 0x00, 0x64, 0x00, 0x67, 0x00, 0x38, 0x1f, 0xde, 0x1f, 0x7a, |
| 181 | 0x1f, 0x47, 0x1f, 0x7c, 0x00, 0x30, 0x01, 0x4b, 0x02, 0x82, |
| 182 | 0x03, 0x73, 0x03, 0xcf, /* 6 MHz */ |
| 183 | }, { |
| 184 | 0x1f, 0xfa, 0x1f, 0xda, 0x1f, 0xc1, 0x1f, 0xb3, 0x1f, 0xca, |
| 185 | 0x00, 0x07, 0x00, 0x4d, 0x00, 0x6d, 0x00, 0x40, 0x1f, 0xca, |
| 186 | 0x1f, 0x4d, 0x1f, 0x2a, 0x1f, 0xb2, 0x00, 0xec, 0x02, 0x7e, |
| 187 | 0x03, 0xd0, 0x04, 0x53, /* 7 MHz */ |
| 188 | }, { |
| 189 | 0x00, 0x10, 0x00, 0x0e, 0x1f, 0xf7, 0x1f, 0xc9, 0x1f, 0xa0, |
| 190 | 0x1f, 0xa6, 0x1f, 0xec, 0x00, 0x4e, 0x00, 0x7d, 0x00, 0x3a, |
| 191 | 0x1f, 0x98, 0x1f, 0x10, 0x1f, 0x40, 0x00, 0x75, 0x02, 0x5f, |
| 192 | 0x04, 0x24, 0x04, 0xdb, /* 8 MHz */ |
| 193 | }, |
| 194 | }; |
Antti Palosaari | 3a2fca2 | 2012-09-12 20:23:49 -0300 | [diff] [blame] | 195 | static const u8 bw_params2[3][6] = { |
| 196 | {0xc3, 0x0c, 0x44, 0x33, 0x33, 0x30}, /* 6 MHz */ |
| 197 | {0xb8, 0xe3, 0x93, 0x99, 0x99, 0x98}, /* 7 MHz */ |
| 198 | {0xae, 0xba, 0xf3, 0x26, 0x66, 0x64}, /* 8 MHz */ |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 199 | }; |
| 200 | |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 201 | dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u inversion=%u\n", |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 202 | c->frequency, c->bandwidth_hz, c->inversion); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 203 | |
| 204 | /* program tuner */ |
| 205 | if (fe->ops.tuner_ops.set_params) |
| 206 | fe->ops.tuner_ops.set_params(fe); |
| 207 | |
| 208 | switch (c->bandwidth_hz) { |
| 209 | case 6000000: |
| 210 | i = 0; |
| 211 | break; |
| 212 | case 7000000: |
| 213 | i = 1; |
| 214 | break; |
| 215 | case 8000000: |
| 216 | i = 2; |
| 217 | break; |
| 218 | default: |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 219 | dev_err(&client->dev, "invalid bandwidth_hz %u\n", |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 220 | c->bandwidth_hz); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 221 | return -EINVAL; |
| 222 | } |
| 223 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 224 | ret = rtl2830_update_bits(client, 0x008, 0x06, i << 1); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 225 | if (ret) |
| 226 | goto err; |
| 227 | |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 228 | /* program if frequency */ |
| 229 | if (fe->ops.tuner_ops.get_if_frequency) |
| 230 | ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); |
| 231 | else |
| 232 | ret = -EINVAL; |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 233 | if (ret) |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 234 | goto err; |
| 235 | |
Antti Palosaari | b8cb50d2 | 2014-12-09 00:24:13 -0300 | [diff] [blame] | 236 | num = if_frequency % dev->pdata->clk; |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 237 | num *= 0x400000; |
Antti Palosaari | b8cb50d2 | 2014-12-09 00:24:13 -0300 | [diff] [blame] | 238 | num = div_u64(num, dev->pdata->clk); |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 239 | num = -num; |
| 240 | if_ctl = num & 0x3fffff; |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 241 | dev_dbg(&client->dev, "if_frequency=%d if_ctl=%08x\n", |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 242 | if_frequency, if_ctl); |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 243 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 244 | buf[0] = (if_ctl >> 16) & 0x3f; |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 245 | buf[1] = (if_ctl >> 8) & 0xff; |
| 246 | buf[2] = (if_ctl >> 0) & 0xff; |
| 247 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 248 | ret = rtl2830_bulk_read(client, 0x119, &u8tmp, 1); |
| 249 | if (ret) |
| 250 | goto err; |
| 251 | |
| 252 | buf[0] |= u8tmp & 0xc0; /* [7:6] */ |
| 253 | |
| 254 | ret = rtl2830_bulk_write(client, 0x119, buf, 3); |
Antti Palosaari | 66b3c4d | 2012-09-12 20:23:48 -0300 | [diff] [blame] | 255 | if (ret) |
| 256 | goto err; |
| 257 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 258 | /* 1/2 split I2C write */ |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 259 | ret = rtl2830_bulk_write(client, 0x11c, &bw_params1[i][0], 17); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 260 | if (ret) |
| 261 | goto err; |
| 262 | |
| 263 | /* 2/2 split I2C write */ |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 264 | ret = rtl2830_bulk_write(client, 0x12d, &bw_params1[i][17], 17); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 265 | if (ret) |
| 266 | goto err; |
| 267 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 268 | ret = rtl2830_bulk_write(client, 0x19d, bw_params2[i], 6); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 269 | if (ret) |
| 270 | goto err; |
| 271 | |
| 272 | return ret; |
| 273 | err: |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 274 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 278 | static int rtl2830_get_frontend(struct dvb_frontend *fe, |
| 279 | struct dtv_frontend_properties *c) |
Antti Palosaari | 631a2b6 | 2012-05-18 15:58:57 -0300 | [diff] [blame] | 280 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 281 | struct i2c_client *client = fe->demodulator_priv; |
| 282 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 631a2b6 | 2012-05-18 15:58:57 -0300 | [diff] [blame] | 283 | int ret; |
| 284 | u8 buf[3]; |
| 285 | |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 286 | if (dev->sleeping) |
Antti Palosaari | c188637 | 2012-05-18 16:02:55 -0300 | [diff] [blame] | 287 | return 0; |
| 288 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 289 | ret = rtl2830_bulk_read(client, 0x33c, buf, 2); |
Antti Palosaari | 631a2b6 | 2012-05-18 15:58:57 -0300 | [diff] [blame] | 290 | if (ret) |
| 291 | goto err; |
| 292 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 293 | ret = rtl2830_bulk_read(client, 0x351, &buf[2], 1); |
Antti Palosaari | 631a2b6 | 2012-05-18 15:58:57 -0300 | [diff] [blame] | 294 | if (ret) |
| 295 | goto err; |
| 296 | |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 297 | dev_dbg(&client->dev, "TPS=%*ph\n", 3, buf); |
Antti Palosaari | 631a2b6 | 2012-05-18 15:58:57 -0300 | [diff] [blame] | 298 | |
| 299 | switch ((buf[0] >> 2) & 3) { |
| 300 | case 0: |
| 301 | c->modulation = QPSK; |
| 302 | break; |
| 303 | case 1: |
| 304 | c->modulation = QAM_16; |
| 305 | break; |
| 306 | case 2: |
| 307 | c->modulation = QAM_64; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | switch ((buf[2] >> 2) & 1) { |
| 312 | case 0: |
| 313 | c->transmission_mode = TRANSMISSION_MODE_2K; |
| 314 | break; |
| 315 | case 1: |
| 316 | c->transmission_mode = TRANSMISSION_MODE_8K; |
| 317 | } |
| 318 | |
| 319 | switch ((buf[2] >> 0) & 3) { |
| 320 | case 0: |
| 321 | c->guard_interval = GUARD_INTERVAL_1_32; |
| 322 | break; |
| 323 | case 1: |
| 324 | c->guard_interval = GUARD_INTERVAL_1_16; |
| 325 | break; |
| 326 | case 2: |
| 327 | c->guard_interval = GUARD_INTERVAL_1_8; |
| 328 | break; |
| 329 | case 3: |
| 330 | c->guard_interval = GUARD_INTERVAL_1_4; |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | switch ((buf[0] >> 4) & 7) { |
| 335 | case 0: |
| 336 | c->hierarchy = HIERARCHY_NONE; |
| 337 | break; |
| 338 | case 1: |
| 339 | c->hierarchy = HIERARCHY_1; |
| 340 | break; |
| 341 | case 2: |
| 342 | c->hierarchy = HIERARCHY_2; |
| 343 | break; |
| 344 | case 3: |
| 345 | c->hierarchy = HIERARCHY_4; |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | switch ((buf[1] >> 3) & 7) { |
| 350 | case 0: |
| 351 | c->code_rate_HP = FEC_1_2; |
| 352 | break; |
| 353 | case 1: |
| 354 | c->code_rate_HP = FEC_2_3; |
| 355 | break; |
| 356 | case 2: |
| 357 | c->code_rate_HP = FEC_3_4; |
| 358 | break; |
| 359 | case 3: |
| 360 | c->code_rate_HP = FEC_5_6; |
| 361 | break; |
| 362 | case 4: |
| 363 | c->code_rate_HP = FEC_7_8; |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | switch ((buf[1] >> 0) & 7) { |
| 368 | case 0: |
| 369 | c->code_rate_LP = FEC_1_2; |
| 370 | break; |
| 371 | case 1: |
| 372 | c->code_rate_LP = FEC_2_3; |
| 373 | break; |
| 374 | case 2: |
| 375 | c->code_rate_LP = FEC_3_4; |
| 376 | break; |
| 377 | case 3: |
| 378 | c->code_rate_LP = FEC_5_6; |
| 379 | break; |
| 380 | case 4: |
| 381 | c->code_rate_LP = FEC_7_8; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | err: |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 387 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 631a2b6 | 2012-05-18 15:58:57 -0300 | [diff] [blame] | 388 | return ret; |
| 389 | } |
| 390 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 391 | static int rtl2830_read_status(struct dvb_frontend *fe, enum fe_status *status) |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 392 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 393 | struct i2c_client *client = fe->demodulator_priv; |
Antti Palosaari | b8cb50d2 | 2014-12-09 00:24:13 -0300 | [diff] [blame] | 394 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 4a7e445 | 2016-06-29 20:40:56 -0300 | [diff] [blame] | 395 | struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; |
| 396 | int ret, stmp; |
| 397 | unsigned int utmp; |
| 398 | u8 u8tmp, buf[2]; |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 399 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 400 | *status = 0; |
| 401 | |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 402 | if (dev->sleeping) |
Antti Palosaari | a8567cf | 2012-01-21 22:40:58 -0300 | [diff] [blame] | 403 | return 0; |
| 404 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 405 | ret = rtl2830_bulk_read(client, 0x351, &u8tmp, 1); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 406 | if (ret) |
| 407 | goto err; |
| 408 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 409 | u8tmp = (u8tmp >> 3) & 0x0f; /* [6:3] */ |
| 410 | if (u8tmp == 11) { |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 411 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 412 | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 413 | } else if (u8tmp == 10) { |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 414 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 415 | FE_HAS_VITERBI; |
| 416 | } |
| 417 | |
Antti Palosaari | 47b4dbf | 2014-12-09 06:14:36 -0300 | [diff] [blame] | 418 | dev->fe_status = *status; |
| 419 | |
Antti Palosaari | 4a7e445 | 2016-06-29 20:40:56 -0300 | [diff] [blame] | 420 | /* Signal strength */ |
| 421 | if (dev->fe_status & FE_HAS_SIGNAL) { |
| 422 | /* Read IF AGC */ |
| 423 | ret = rtl2830_bulk_read(client, 0x359, buf, 2); |
| 424 | if (ret) |
| 425 | goto err; |
| 426 | |
| 427 | stmp = buf[0] << 8 | buf[1] << 0; |
| 428 | stmp = sign_extend32(stmp, 13); |
| 429 | utmp = clamp_val(-4 * stmp + 32767, 0x0000, 0xffff); |
| 430 | |
| 431 | dev_dbg(&client->dev, "IF AGC=%d\n", stmp); |
| 432 | |
| 433 | c->strength.stat[0].scale = FE_SCALE_RELATIVE; |
| 434 | c->strength.stat[0].uvalue = utmp; |
| 435 | } else { |
| 436 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 437 | } |
| 438 | |
| 439 | /* CNR */ |
| 440 | if (dev->fe_status & FE_HAS_VITERBI) { |
| 441 | unsigned int hierarchy, constellation; |
| 442 | #define CONSTELLATION_NUM 3 |
| 443 | #define HIERARCHY_NUM 4 |
| 444 | static const u32 constant[CONSTELLATION_NUM][HIERARCHY_NUM] = { |
| 445 | {70705899, 70705899, 70705899, 70705899}, |
| 446 | {82433173, 82433173, 87483115, 94445660}, |
| 447 | {92888734, 92888734, 95487525, 99770748}, |
| 448 | }; |
| 449 | |
| 450 | ret = rtl2830_bulk_read(client, 0x33c, &u8tmp, 1); |
| 451 | if (ret) |
| 452 | goto err; |
| 453 | |
| 454 | constellation = (u8tmp >> 2) & 0x03; /* [3:2] */ |
| 455 | if (constellation > CONSTELLATION_NUM - 1) |
| 456 | goto err; |
| 457 | |
| 458 | hierarchy = (u8tmp >> 4) & 0x07; /* [6:4] */ |
| 459 | if (hierarchy > HIERARCHY_NUM - 1) |
| 460 | goto err; |
| 461 | |
| 462 | ret = rtl2830_bulk_read(client, 0x40c, buf, 2); |
| 463 | if (ret) |
| 464 | goto err; |
| 465 | |
| 466 | utmp = buf[0] << 8 | buf[1] << 0; |
| 467 | if (utmp) |
| 468 | stmp = (constant[constellation][hierarchy] - |
| 469 | intlog10(utmp)) / ((1 << 24) / 10000); |
| 470 | else |
| 471 | stmp = 0; |
| 472 | |
| 473 | dev_dbg(&client->dev, "CNR raw=%u\n", utmp); |
| 474 | |
| 475 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 476 | c->cnr.stat[0].svalue = stmp; |
| 477 | } else { |
| 478 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 479 | } |
| 480 | |
| 481 | /* BER */ |
| 482 | if (dev->fe_status & FE_HAS_LOCK) { |
| 483 | ret = rtl2830_bulk_read(client, 0x34e, buf, 2); |
| 484 | if (ret) |
| 485 | goto err; |
| 486 | |
| 487 | utmp = buf[0] << 8 | buf[1] << 0; |
| 488 | dev->post_bit_error += utmp; |
| 489 | dev->post_bit_count += 1000000; |
| 490 | |
| 491 | dev_dbg(&client->dev, "BER errors=%u total=1000000\n", utmp); |
| 492 | |
| 493 | c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; |
| 494 | c->post_bit_error.stat[0].uvalue = dev->post_bit_error; |
| 495 | c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; |
| 496 | c->post_bit_count.stat[0].uvalue = dev->post_bit_count; |
| 497 | } else { |
| 498 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 499 | c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 500 | } |
| 501 | |
| 502 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 503 | return ret; |
| 504 | err: |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 505 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static int rtl2830_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 510 | { |
Antti Palosaari | 6dcfe3c | 2014-12-09 10:48:10 -0300 | [diff] [blame] | 511 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | eba672a | 2012-05-15 18:32:33 -0300 | [diff] [blame] | 512 | |
Antti Palosaari | 6dcfe3c | 2014-12-09 10:48:10 -0300 | [diff] [blame] | 513 | if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) |
| 514 | *snr = div_s64(c->cnr.stat[0].svalue, 100); |
Antti Palosaari | eba672a | 2012-05-15 18:32:33 -0300 | [diff] [blame] | 515 | else |
| 516 | *snr = 0; |
| 517 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int rtl2830_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 522 | { |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 523 | struct i2c_client *client = fe->demodulator_priv; |
| 524 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 525ffc1 | 2012-05-18 12:23:42 -0300 | [diff] [blame] | 525 | |
Antti Palosaari | f491391 | 2014-12-09 10:27:32 -0300 | [diff] [blame] | 526 | *ber = (dev->post_bit_error - dev->post_bit_error_prev); |
| 527 | dev->post_bit_error_prev = dev->post_bit_error; |
Antti Palosaari | 525ffc1 | 2012-05-18 12:23:42 -0300 | [diff] [blame] | 528 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int rtl2830_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 533 | { |
| 534 | *ucblocks = 0; |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 535 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static int rtl2830_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 540 | { |
Antti Palosaari | d512e286 | 2014-12-09 10:20:01 -0300 | [diff] [blame] | 541 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 78e7507 | 2012-05-18 15:17:51 -0300 | [diff] [blame] | 542 | |
Antti Palosaari | d512e286 | 2014-12-09 10:20:01 -0300 | [diff] [blame] | 543 | if (c->strength.stat[0].scale == FE_SCALE_RELATIVE) |
| 544 | *strength = c->strength.stat[0].uvalue; |
Antti Palosaari | 78e7507 | 2012-05-18 15:17:51 -0300 | [diff] [blame] | 545 | else |
Antti Palosaari | d512e286 | 2014-12-09 10:20:01 -0300 | [diff] [blame] | 546 | *strength = 0; |
Antti Palosaari | 78e7507 | 2012-05-18 15:17:51 -0300 | [diff] [blame] | 547 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 551 | static struct dvb_frontend_ops rtl2830_ops = { |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 552 | .delsys = {SYS_DVBT}, |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 553 | .info = { |
| 554 | .name = "Realtek RTL2830 (DVB-T)", |
| 555 | .caps = FE_CAN_FEC_1_2 | |
| 556 | FE_CAN_FEC_2_3 | |
| 557 | FE_CAN_FEC_3_4 | |
| 558 | FE_CAN_FEC_5_6 | |
| 559 | FE_CAN_FEC_7_8 | |
| 560 | FE_CAN_FEC_AUTO | |
| 561 | FE_CAN_QPSK | |
| 562 | FE_CAN_QAM_16 | |
| 563 | FE_CAN_QAM_64 | |
| 564 | FE_CAN_QAM_AUTO | |
| 565 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 566 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 567 | FE_CAN_HIERARCHY_AUTO | |
| 568 | FE_CAN_RECOVER | |
| 569 | FE_CAN_MUTE_TS |
| 570 | }, |
| 571 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 572 | .init = rtl2830_init, |
Antti Palosaari | a8567cf | 2012-01-21 22:40:58 -0300 | [diff] [blame] | 573 | .sleep = rtl2830_sleep, |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 574 | |
| 575 | .get_tune_settings = rtl2830_get_tune_settings, |
| 576 | |
| 577 | .set_frontend = rtl2830_set_frontend, |
Antti Palosaari | 631a2b6 | 2012-05-18 15:58:57 -0300 | [diff] [blame] | 578 | .get_frontend = rtl2830_get_frontend, |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 579 | |
| 580 | .read_status = rtl2830_read_status, |
| 581 | .read_snr = rtl2830_read_snr, |
| 582 | .read_ber = rtl2830_read_ber, |
| 583 | .read_ucblocks = rtl2830_read_ucblocks, |
| 584 | .read_signal_strength = rtl2830_read_signal_strength, |
| 585 | }; |
| 586 | |
Antti Palosaari | df70dda | 2014-12-09 16:08:44 -0300 | [diff] [blame] | 587 | static int rtl2830_pid_filter_ctrl(struct dvb_frontend *fe, int onoff) |
| 588 | { |
| 589 | struct i2c_client *client = fe->demodulator_priv; |
| 590 | int ret; |
| 591 | u8 u8tmp; |
| 592 | |
| 593 | dev_dbg(&client->dev, "onoff=%d\n", onoff); |
| 594 | |
| 595 | /* enable / disable PID filter */ |
| 596 | if (onoff) |
| 597 | u8tmp = 0x80; |
| 598 | else |
| 599 | u8tmp = 0x00; |
| 600 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 601 | ret = rtl2830_update_bits(client, 0x061, 0x80, u8tmp); |
Antti Palosaari | df70dda | 2014-12-09 16:08:44 -0300 | [diff] [blame] | 602 | if (ret) |
| 603 | goto err; |
| 604 | |
| 605 | return 0; |
| 606 | err: |
| 607 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | static int rtl2830_pid_filter(struct dvb_frontend *fe, u8 index, u16 pid, int onoff) |
| 612 | { |
| 613 | struct i2c_client *client = fe->demodulator_priv; |
| 614 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
| 615 | int ret; |
| 616 | u8 buf[4]; |
| 617 | |
| 618 | dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n", |
| 619 | index, pid, onoff); |
| 620 | |
| 621 | /* skip invalid PIDs (0x2000) */ |
| 622 | if (pid > 0x1fff || index > 32) |
| 623 | return 0; |
| 624 | |
| 625 | if (onoff) |
| 626 | set_bit(index, &dev->filters); |
| 627 | else |
| 628 | clear_bit(index, &dev->filters); |
| 629 | |
| 630 | /* enable / disable PIDs */ |
| 631 | buf[0] = (dev->filters >> 0) & 0xff; |
| 632 | buf[1] = (dev->filters >> 8) & 0xff; |
| 633 | buf[2] = (dev->filters >> 16) & 0xff; |
| 634 | buf[3] = (dev->filters >> 24) & 0xff; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 635 | ret = rtl2830_bulk_write(client, 0x062, buf, 4); |
Antti Palosaari | df70dda | 2014-12-09 16:08:44 -0300 | [diff] [blame] | 636 | if (ret) |
| 637 | goto err; |
| 638 | |
| 639 | /* add PID */ |
| 640 | buf[0] = (pid >> 8) & 0xff; |
| 641 | buf[1] = (pid >> 0) & 0xff; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 642 | ret = rtl2830_bulk_write(client, 0x066 + 2 * index, buf, 2); |
Antti Palosaari | df70dda | 2014-12-09 16:08:44 -0300 | [diff] [blame] | 643 | if (ret) |
| 644 | goto err; |
| 645 | |
| 646 | return 0; |
| 647 | err: |
| 648 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 649 | return ret; |
| 650 | } |
| 651 | |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 652 | /* |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 653 | * I2C gate/mux/repeater logic |
| 654 | * We must use unlocked __i2c_transfer() here (through regmap) because of I2C |
| 655 | * adapter lock is already taken by tuner driver. |
| 656 | * Gate is closed automatically after single I2C transfer. |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 657 | */ |
Peter Rosin | a011915 | 2016-04-20 08:41:13 +0200 | [diff] [blame] | 658 | static int rtl2830_select(struct i2c_mux_core *muxc, u32 chan_id) |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 659 | { |
Peter Rosin | a011915 | 2016-04-20 08:41:13 +0200 | [diff] [blame] | 660 | struct i2c_client *client = i2c_mux_priv(muxc); |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 661 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 662 | int ret; |
| 663 | |
Antti Palosaari | fd4cfa8 | 2014-12-09 16:14:41 -0300 | [diff] [blame] | 664 | dev_dbg(&client->dev, "\n"); |
| 665 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 666 | /* open I2C repeater for 1 transfer, closes automatically */ |
| 667 | /* XXX: regmap_update_bits() does not lock I2C adapter */ |
| 668 | ret = regmap_update_bits(dev->regmap, 0x101, 0x08, 0x08); |
| 669 | if (ret) |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 670 | goto err; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 671 | |
| 672 | return 0; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 673 | err: |
Antti Palosaari | 7cc3932 | 2014-12-08 23:32:19 -0300 | [diff] [blame] | 674 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | static struct dvb_frontend *rtl2830_get_dvb_frontend(struct i2c_client *client) |
| 679 | { |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 680 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 681 | |
| 682 | dev_dbg(&client->dev, "\n"); |
| 683 | |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 684 | return &dev->fe; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static struct i2c_adapter *rtl2830_get_i2c_adapter(struct i2c_client *client) |
| 688 | { |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 689 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 690 | |
| 691 | dev_dbg(&client->dev, "\n"); |
| 692 | |
Peter Rosin | a011915 | 2016-04-20 08:41:13 +0200 | [diff] [blame] | 693 | return dev->muxc->adapter[0]; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 694 | } |
| 695 | |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 696 | /* |
| 697 | * We implement own I2C access routines for regmap in order to get manual access |
| 698 | * to I2C adapter lock, which is needed for I2C mux adapter. |
| 699 | */ |
| 700 | static int rtl2830_regmap_read(void *context, const void *reg_buf, |
| 701 | size_t reg_size, void *val_buf, size_t val_size) |
| 702 | { |
| 703 | struct i2c_client *client = context; |
| 704 | int ret; |
| 705 | struct i2c_msg msg[2] = { |
| 706 | { |
| 707 | .addr = client->addr, |
| 708 | .flags = 0, |
| 709 | .len = reg_size, |
| 710 | .buf = (u8 *)reg_buf, |
| 711 | }, { |
| 712 | .addr = client->addr, |
| 713 | .flags = I2C_M_RD, |
| 714 | .len = val_size, |
| 715 | .buf = val_buf, |
| 716 | } |
| 717 | }; |
| 718 | |
| 719 | ret = __i2c_transfer(client->adapter, msg, 2); |
| 720 | if (ret != 2) { |
| 721 | dev_warn(&client->dev, "i2c reg read failed %d\n", ret); |
| 722 | if (ret >= 0) |
| 723 | ret = -EREMOTEIO; |
| 724 | return ret; |
| 725 | } |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static int rtl2830_regmap_write(void *context, const void *data, size_t count) |
| 730 | { |
| 731 | struct i2c_client *client = context; |
| 732 | int ret; |
| 733 | struct i2c_msg msg[1] = { |
| 734 | { |
| 735 | .addr = client->addr, |
| 736 | .flags = 0, |
| 737 | .len = count, |
| 738 | .buf = (u8 *)data, |
| 739 | } |
| 740 | }; |
| 741 | |
| 742 | ret = __i2c_transfer(client->adapter, msg, 1); |
| 743 | if (ret != 1) { |
| 744 | dev_warn(&client->dev, "i2c reg write failed %d\n", ret); |
| 745 | if (ret >= 0) |
| 746 | ret = -EREMOTEIO; |
| 747 | return ret; |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int rtl2830_regmap_gather_write(void *context, const void *reg, |
| 753 | size_t reg_len, const void *val, |
| 754 | size_t val_len) |
| 755 | { |
| 756 | struct i2c_client *client = context; |
| 757 | int ret; |
| 758 | u8 buf[256]; |
| 759 | struct i2c_msg msg[1] = { |
| 760 | { |
| 761 | .addr = client->addr, |
| 762 | .flags = 0, |
| 763 | .len = 1 + val_len, |
| 764 | .buf = buf, |
| 765 | } |
| 766 | }; |
| 767 | |
| 768 | buf[0] = *(u8 const *)reg; |
| 769 | memcpy(&buf[1], val, val_len); |
| 770 | |
| 771 | ret = __i2c_transfer(client->adapter, msg, 1); |
| 772 | if (ret != 1) { |
| 773 | dev_warn(&client->dev, "i2c reg write failed %d\n", ret); |
| 774 | if (ret >= 0) |
| 775 | ret = -EREMOTEIO; |
| 776 | return ret; |
| 777 | } |
| 778 | return 0; |
| 779 | } |
| 780 | |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 781 | static int rtl2830_probe(struct i2c_client *client, |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 782 | const struct i2c_device_id *id) |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 783 | { |
| 784 | struct rtl2830_platform_data *pdata = client->dev.platform_data; |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 785 | struct rtl2830_dev *dev; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 786 | int ret; |
| 787 | u8 u8tmp; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 788 | static const struct regmap_bus regmap_bus = { |
| 789 | .read = rtl2830_regmap_read, |
| 790 | .write = rtl2830_regmap_write, |
| 791 | .gather_write = rtl2830_regmap_gather_write, |
| 792 | .val_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 793 | }; |
| 794 | static const struct regmap_range_cfg regmap_range_cfg[] = { |
| 795 | { |
| 796 | .selector_reg = 0x00, |
| 797 | .selector_mask = 0xff, |
| 798 | .selector_shift = 0, |
| 799 | .window_start = 0, |
| 800 | .window_len = 0x100, |
| 801 | .range_min = 0 * 0x100, |
| 802 | .range_max = 5 * 0x100, |
| 803 | }, |
| 804 | }; |
| 805 | static const struct regmap_config regmap_config = { |
| 806 | .reg_bits = 8, |
| 807 | .val_bits = 8, |
| 808 | .max_register = 5 * 0x100, |
| 809 | .ranges = regmap_range_cfg, |
| 810 | .num_ranges = ARRAY_SIZE(regmap_range_cfg), |
| 811 | }; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 812 | |
| 813 | dev_dbg(&client->dev, "\n"); |
| 814 | |
| 815 | if (pdata == NULL) { |
| 816 | ret = -EINVAL; |
| 817 | goto err; |
| 818 | } |
| 819 | |
| 820 | /* allocate memory for the internal state */ |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 821 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 822 | if (dev == NULL) { |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 823 | ret = -ENOMEM; |
| 824 | goto err; |
| 825 | } |
| 826 | |
| 827 | /* setup the state */ |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 828 | i2c_set_clientdata(client, dev); |
Antti Palosaari | 47b4dbf | 2014-12-09 06:14:36 -0300 | [diff] [blame] | 829 | dev->client = client; |
Antti Palosaari | b8cb50d2 | 2014-12-09 00:24:13 -0300 | [diff] [blame] | 830 | dev->pdata = client->dev.platform_data; |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 831 | dev->sleeping = true; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 832 | dev->regmap = regmap_init(&client->dev, ®map_bus, client, |
| 833 | ®map_config); |
| 834 | if (IS_ERR(dev->regmap)) { |
| 835 | ret = PTR_ERR(dev->regmap); |
| 836 | goto err_kfree; |
| 837 | } |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 838 | |
| 839 | /* check if the demod is there */ |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 840 | ret = rtl2830_bulk_read(client, 0x000, &u8tmp, 1); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 841 | if (ret) |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 842 | goto err_regmap_exit; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 843 | |
| 844 | /* create muxed i2c adapter for tuner */ |
Peter Rosin | a011915 | 2016-04-20 08:41:13 +0200 | [diff] [blame] | 845 | dev->muxc = i2c_mux_alloc(client->adapter, &client->dev, 1, 0, 0, |
| 846 | rtl2830_select, NULL); |
| 847 | if (!dev->muxc) { |
| 848 | ret = -ENOMEM; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 849 | goto err_regmap_exit; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 850 | } |
Peter Rosin | a011915 | 2016-04-20 08:41:13 +0200 | [diff] [blame] | 851 | dev->muxc->priv = client; |
| 852 | ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0); |
| 853 | if (ret) |
| 854 | goto err_regmap_exit; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 855 | |
| 856 | /* create dvb frontend */ |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 857 | memcpy(&dev->fe.ops, &rtl2830_ops, sizeof(dev->fe.ops)); |
Antti Palosaari | 1f153c4 | 2014-12-08 22:47:21 -0300 | [diff] [blame] | 858 | dev->fe.demodulator_priv = client; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 859 | |
| 860 | /* setup callbacks */ |
| 861 | pdata->get_dvb_frontend = rtl2830_get_dvb_frontend; |
| 862 | pdata->get_i2c_adapter = rtl2830_get_i2c_adapter; |
Antti Palosaari | df70dda | 2014-12-09 16:08:44 -0300 | [diff] [blame] | 863 | pdata->pid_filter = rtl2830_pid_filter; |
| 864 | pdata->pid_filter_ctrl = rtl2830_pid_filter_ctrl; |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 865 | |
| 866 | dev_info(&client->dev, "Realtek RTL2830 successfully attached\n"); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 867 | |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 868 | return 0; |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 869 | err_regmap_exit: |
| 870 | regmap_exit(dev->regmap); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 871 | err_kfree: |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 872 | kfree(dev); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 873 | err: |
| 874 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | static int rtl2830_remove(struct i2c_client *client) |
| 879 | { |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 880 | struct rtl2830_dev *dev = i2c_get_clientdata(client); |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 881 | |
| 882 | dev_dbg(&client->dev, "\n"); |
| 883 | |
Peter Rosin | a011915 | 2016-04-20 08:41:13 +0200 | [diff] [blame] | 884 | i2c_mux_del_adapters(dev->muxc); |
Antti Palosaari | 15d37f3 | 2014-12-12 01:03:51 -0300 | [diff] [blame] | 885 | regmap_exit(dev->regmap); |
Antti Palosaari | f544f10 | 2014-12-08 22:31:28 -0300 | [diff] [blame] | 886 | kfree(dev); |
Antti Palosaari | 947debb | 2014-12-09 02:31:53 -0300 | [diff] [blame] | 887 | |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | static const struct i2c_device_id rtl2830_id_table[] = { |
| 892 | {"rtl2830", 0}, |
| 893 | {} |
| 894 | }; |
| 895 | MODULE_DEVICE_TABLE(i2c, rtl2830_id_table); |
| 896 | |
| 897 | static struct i2c_driver rtl2830_driver = { |
| 898 | .driver = { |
Antti Palosaari | 95e7cdb | 2016-06-29 20:40:55 -0300 | [diff] [blame] | 899 | .name = "rtl2830", |
| 900 | .suppress_bind_attrs = true, |
Antti Palosaari | 28c0879 | 2014-12-07 04:07:29 -0300 | [diff] [blame] | 901 | }, |
| 902 | .probe = rtl2830_probe, |
| 903 | .remove = rtl2830_remove, |
| 904 | .id_table = rtl2830_id_table, |
| 905 | }; |
| 906 | |
| 907 | module_i2c_driver(rtl2830_driver); |
| 908 | |
Antti Palosaari | c0adca7 | 2011-07-08 23:34:09 -0300 | [diff] [blame] | 909 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 910 | MODULE_DESCRIPTION("Realtek RTL2830 DVB-T demodulator driver"); |
| 911 | MODULE_LICENSE("GPL"); |