Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Sony CXD2820R demodulator driver |
| 3 | * |
| 4 | * Copyright (C) 2010 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 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #include "cxd2820r_priv.h" |
| 23 | |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 24 | /* Write register table */ |
| 25 | int cxd2820r_wr_reg_val_mask_tab(struct cxd2820r_priv *priv, |
| 26 | const struct reg_val_mask *tab, int tab_len) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 27 | { |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 28 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 29 | int ret; |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 30 | unsigned int i, reg, mask, val; |
| 31 | struct regmap *regmap; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 32 | |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 33 | dev_dbg(&client->dev, "tab_len=%d\n", tab_len); |
| 34 | |
| 35 | for (i = 0; i < tab_len; i++) { |
| 36 | if ((tab[i].reg >> 16) & 0x1) |
| 37 | regmap = priv->regmap[1]; |
| 38 | else |
| 39 | regmap = priv->regmap[0]; |
| 40 | |
| 41 | reg = (tab[i].reg >> 0) & 0xffff; |
| 42 | val = tab[i].val; |
| 43 | mask = tab[i].mask; |
| 44 | |
| 45 | if (mask == 0xff) |
| 46 | ret = regmap_write(regmap, reg, val); |
| 47 | else |
| 48 | ret = regmap_write_bits(regmap, reg, mask, val); |
| 49 | if (ret) |
| 50 | goto error; |
Mauro Carvalho Chehab | 37ebaf6 | 2013-11-02 05:11:47 -0300 | [diff] [blame] | 51 | } |
| 52 | |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 53 | return 0; |
| 54 | error: |
| 55 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 56 | return ret; |
| 57 | } |
| 58 | |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 59 | int cxd2820r_gpio(struct dvb_frontend *fe, u8 *gpio) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 60 | { |
| 61 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 62 | struct i2c_client *client = priv->client[0]; |
| 63 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 64 | int ret, i; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 65 | u8 tmp0, tmp1; |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 66 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 67 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 68 | |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 69 | /* update GPIOs only when needed */ |
| 70 | if (!memcmp(gpio, priv->gpio, sizeof(priv->gpio))) |
| 71 | return 0; |
| 72 | |
| 73 | tmp0 = 0x00; |
| 74 | tmp1 = 0x00; |
| 75 | for (i = 0; i < sizeof(priv->gpio); i++) { |
| 76 | /* enable / disable */ |
| 77 | if (gpio[i] & CXD2820R_GPIO_E) |
| 78 | tmp0 |= (2 << 6) >> (2 * i); |
| 79 | else |
| 80 | tmp0 |= (1 << 6) >> (2 * i); |
| 81 | |
| 82 | /* input / output */ |
| 83 | if (gpio[i] & CXD2820R_GPIO_I) |
| 84 | tmp1 |= (1 << (3 + i)); |
| 85 | else |
| 86 | tmp1 |= (0 << (3 + i)); |
| 87 | |
| 88 | /* high / low */ |
| 89 | if (gpio[i] & CXD2820R_GPIO_H) |
| 90 | tmp1 |= (1 << (0 + i)); |
| 91 | else |
| 92 | tmp1 |= (0 << (0 + i)); |
| 93 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 94 | dev_dbg(&client->dev, "gpio i=%d %02x %02x\n", i, tmp0, tmp1); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 95 | } |
| 96 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 97 | dev_dbg(&client->dev, "wr gpio=%02x %02x\n", tmp0, tmp1); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 98 | |
| 99 | /* write bits [7:2] */ |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 100 | ret = regmap_update_bits(priv->regmap[0], 0x0089, 0xfc, tmp0); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 101 | if (ret) |
| 102 | goto error; |
| 103 | |
| 104 | /* write bits [5:0] */ |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 105 | ret = regmap_update_bits(priv->regmap[0], 0x008e, 0x3f, tmp1); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 106 | if (ret) |
| 107 | goto error; |
| 108 | |
| 109 | memcpy(priv->gpio, gpio, sizeof(priv->gpio)); |
| 110 | |
| 111 | return ret; |
| 112 | error: |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 113 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 114 | return ret; |
| 115 | } |
| 116 | |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 117 | static int cxd2820r_set_frontend(struct dvb_frontend *fe) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 118 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 119 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 120 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 121 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 122 | int ret; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 123 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 124 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 125 | |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 126 | switch (c->delivery_system) { |
| 127 | case SYS_DVBT: |
| 128 | ret = cxd2820r_init_t(fe); |
| 129 | if (ret < 0) |
| 130 | goto err; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 131 | ret = cxd2820r_set_frontend_t(fe); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 132 | if (ret < 0) |
| 133 | goto err; |
| 134 | break; |
| 135 | case SYS_DVBT2: |
| 136 | ret = cxd2820r_init_t(fe); |
| 137 | if (ret < 0) |
| 138 | goto err; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 139 | ret = cxd2820r_set_frontend_t2(fe); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 140 | if (ret < 0) |
| 141 | goto err; |
| 142 | break; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 143 | case SYS_DVBC_ANNEX_A: |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 144 | ret = cxd2820r_init_c(fe); |
| 145 | if (ret < 0) |
| 146 | goto err; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 147 | ret = cxd2820r_set_frontend_c(fe); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 148 | if (ret < 0) |
| 149 | goto err; |
| 150 | break; |
| 151 | default: |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 152 | dev_dbg(&client->dev, "invalid delivery_system\n"); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 153 | ret = -EINVAL; |
| 154 | break; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 155 | } |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 156 | err: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 157 | return ret; |
| 158 | } |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 159 | |
| 160 | static int cxd2820r_read_status(struct dvb_frontend *fe, enum fe_status *status) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 161 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 162 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 163 | struct i2c_client *client = priv->client[0]; |
| 164 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 165 | int ret; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 166 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 167 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 168 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 169 | switch (c->delivery_system) { |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 170 | case SYS_DVBT: |
| 171 | ret = cxd2820r_read_status_t(fe, status); |
| 172 | break; |
| 173 | case SYS_DVBT2: |
| 174 | ret = cxd2820r_read_status_t2(fe, status); |
| 175 | break; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 176 | case SYS_DVBC_ANNEX_A: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 177 | ret = cxd2820r_read_status_c(fe, status); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 178 | break; |
| 179 | default: |
| 180 | ret = -EINVAL; |
| 181 | break; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 182 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 186 | static int cxd2820r_get_frontend(struct dvb_frontend *fe, |
| 187 | struct dtv_frontend_properties *p) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 188 | { |
Antti Palosaari | 3b6a567 | 2012-01-15 18:30:36 -0300 | [diff] [blame] | 189 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 190 | struct i2c_client *client = priv->client[0]; |
| 191 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 192 | int ret; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 193 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 194 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 3b6a567 | 2012-01-15 18:30:36 -0300 | [diff] [blame] | 195 | |
| 196 | if (priv->delivery_system == SYS_UNDEFINED) |
| 197 | return 0; |
| 198 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 199 | switch (c->delivery_system) { |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 200 | case SYS_DVBT: |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 201 | ret = cxd2820r_get_frontend_t(fe, p); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 202 | break; |
| 203 | case SYS_DVBT2: |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 204 | ret = cxd2820r_get_frontend_t2(fe, p); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 205 | break; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 206 | case SYS_DVBC_ANNEX_A: |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 207 | ret = cxd2820r_get_frontend_c(fe, p); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 208 | break; |
| 209 | default: |
| 210 | ret = -EINVAL; |
| 211 | break; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 212 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static int cxd2820r_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 217 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 218 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 219 | struct i2c_client *client = priv->client[0]; |
| 220 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 221 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 222 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 223 | |
Antti Palosaari | 4aa4fd8 | 2016-08-09 14:58:21 -0300 | [diff] [blame] | 224 | *ber = (priv->post_bit_error - priv->post_bit_error_prev_dvbv3); |
| 225 | priv->post_bit_error_prev_dvbv3 = priv->post_bit_error; |
| 226 | |
| 227 | return 0; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static int cxd2820r_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 231 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 232 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 233 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 4aa4fd8 | 2016-08-09 14:58:21 -0300 | [diff] [blame] | 234 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 235 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 236 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 237 | |
Antti Palosaari | 4aa4fd8 | 2016-08-09 14:58:21 -0300 | [diff] [blame] | 238 | if (c->strength.stat[0].scale == FE_SCALE_RELATIVE) |
| 239 | *strength = c->strength.stat[0].uvalue; |
| 240 | else |
| 241 | *strength = 0; |
| 242 | |
| 243 | return 0; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int cxd2820r_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 247 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 248 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 249 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 4aa4fd8 | 2016-08-09 14:58:21 -0300 | [diff] [blame] | 250 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 251 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 252 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 253 | |
Antti Palosaari | 4aa4fd8 | 2016-08-09 14:58:21 -0300 | [diff] [blame] | 254 | if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) |
| 255 | *snr = div_s64(c->cnr.stat[0].svalue, 100); |
| 256 | else |
| 257 | *snr = 0; |
| 258 | |
| 259 | return 0; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static int cxd2820r_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 263 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 264 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 265 | struct i2c_client *client = priv->client[0]; |
| 266 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 267 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 268 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 269 | |
Antti Palosaari | 4aa4fd8 | 2016-08-09 14:58:21 -0300 | [diff] [blame] | 270 | *ucblocks = 0; |
| 271 | |
| 272 | return 0; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int cxd2820r_init(struct dvb_frontend *fe) |
| 276 | { |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 277 | return 0; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int cxd2820r_sleep(struct dvb_frontend *fe) |
| 281 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 282 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 283 | struct i2c_client *client = priv->client[0]; |
| 284 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 285 | int ret; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 286 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 287 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 288 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 289 | switch (c->delivery_system) { |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 290 | case SYS_DVBT: |
| 291 | ret = cxd2820r_sleep_t(fe); |
| 292 | break; |
| 293 | case SYS_DVBT2: |
| 294 | ret = cxd2820r_sleep_t2(fe); |
| 295 | break; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 296 | case SYS_DVBC_ANNEX_A: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 297 | ret = cxd2820r_sleep_c(fe); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 298 | break; |
| 299 | default: |
| 300 | ret = -EINVAL; |
| 301 | break; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 302 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static int cxd2820r_get_tune_settings(struct dvb_frontend *fe, |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 307 | struct dvb_frontend_tune_settings *s) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 308 | { |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 309 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 310 | struct i2c_client *client = priv->client[0]; |
| 311 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 312 | int ret; |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 313 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 314 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 315 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 316 | switch (c->delivery_system) { |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 317 | case SYS_DVBT: |
| 318 | ret = cxd2820r_get_tune_settings_t(fe, s); |
| 319 | break; |
| 320 | case SYS_DVBT2: |
| 321 | ret = cxd2820r_get_tune_settings_t2(fe, s); |
| 322 | break; |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 323 | case SYS_DVBC_ANNEX_A: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 324 | ret = cxd2820r_get_tune_settings_c(fe, s); |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 325 | break; |
| 326 | default: |
| 327 | ret = -EINVAL; |
| 328 | break; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 329 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
Mauro Carvalho Chehab | 41da532 | 2011-12-26 18:03:12 -0300 | [diff] [blame] | 333 | static enum dvbfe_search cxd2820r_search(struct dvb_frontend *fe) |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 334 | { |
| 335 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 336 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 337 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 338 | int ret, i; |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 339 | enum fe_status status = 0; |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 340 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 341 | dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system); |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 342 | |
| 343 | /* switch between DVB-T and DVB-T2 when tune fails */ |
Antti Palosaari | d04ca8d | 2012-01-15 15:08:30 -0300 | [diff] [blame] | 344 | if (priv->last_tune_failed) { |
Antti Palosaari | 7256522 | 2012-01-20 19:48:28 -0300 | [diff] [blame] | 345 | if (priv->delivery_system == SYS_DVBT) { |
| 346 | ret = cxd2820r_sleep_t(fe); |
| 347 | if (ret) |
| 348 | goto error; |
| 349 | |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 350 | c->delivery_system = SYS_DVBT2; |
Antti Palosaari | 7256522 | 2012-01-20 19:48:28 -0300 | [diff] [blame] | 351 | } else if (priv->delivery_system == SYS_DVBT2) { |
| 352 | ret = cxd2820r_sleep_t2(fe); |
| 353 | if (ret) |
| 354 | goto error; |
| 355 | |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 356 | c->delivery_system = SYS_DVBT; |
Antti Palosaari | 7256522 | 2012-01-20 19:48:28 -0300 | [diff] [blame] | 357 | } |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* set frontend */ |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 361 | ret = cxd2820r_set_frontend(fe); |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 362 | if (ret) |
| 363 | goto error; |
| 364 | |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 365 | /* frontend lock wait loop count */ |
| 366 | switch (priv->delivery_system) { |
| 367 | case SYS_DVBT: |
Antti Palosaari | 1f64972 | 2012-01-15 16:50:11 -0300 | [diff] [blame] | 368 | case SYS_DVBC_ANNEX_A: |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 369 | i = 20; |
| 370 | break; |
| 371 | case SYS_DVBT2: |
| 372 | i = 40; |
| 373 | break; |
| 374 | case SYS_UNDEFINED: |
| 375 | default: |
| 376 | i = 0; |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | /* wait frontend lock */ |
| 381 | for (; i > 0; i--) { |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 382 | dev_dbg(&client->dev, "loop=%d\n", i); |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 383 | msleep(50); |
| 384 | ret = cxd2820r_read_status(fe, &status); |
| 385 | if (ret) |
| 386 | goto error; |
| 387 | |
Gianluca Gennari | b115f40 | 2012-03-15 13:33:47 -0300 | [diff] [blame] | 388 | if (status & FE_HAS_LOCK) |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 389 | break; |
| 390 | } |
| 391 | |
| 392 | /* check if we have a valid signal */ |
Gianluca Gennari | b115f40 | 2012-03-15 13:33:47 -0300 | [diff] [blame] | 393 | if (status & FE_HAS_LOCK) { |
Mauro Carvalho Chehab | 285c0b0 | 2014-09-03 15:22:02 -0300 | [diff] [blame] | 394 | priv->last_tune_failed = false; |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 395 | return DVBFE_ALGO_SEARCH_SUCCESS; |
| 396 | } else { |
Mauro Carvalho Chehab | 285c0b0 | 2014-09-03 15:22:02 -0300 | [diff] [blame] | 397 | priv->last_tune_failed = true; |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 398 | return DVBFE_ALGO_SEARCH_AGAIN; |
| 399 | } |
| 400 | |
| 401 | error: |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 402 | dev_dbg(&client->dev, "failed=%d\n", ret); |
Antti Palosaari | e47b78f | 2011-05-03 20:31:36 -0300 | [diff] [blame] | 403 | return DVBFE_ALGO_SEARCH_ERROR; |
| 404 | } |
| 405 | |
| 406 | static int cxd2820r_get_frontend_algo(struct dvb_frontend *fe) |
| 407 | { |
| 408 | return DVBFE_ALGO_CUSTOM; |
| 409 | } |
| 410 | |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 411 | static void cxd2820r_release(struct dvb_frontend *fe) |
| 412 | { |
| 413 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 414 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 415 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 416 | dev_dbg(&client->dev, "\n"); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 417 | |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 418 | i2c_unregister_device(client); |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 419 | |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | |
Steve Kerrison | 0db4bf4 | 2011-08-09 07:16:21 -0300 | [diff] [blame] | 423 | static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 424 | { |
| 425 | struct cxd2820r_priv *priv = fe->demodulator_priv; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 426 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 75aeafc | 2012-07-19 13:10:12 -0300 | [diff] [blame] | 427 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 428 | dev_dbg_ratelimited(&client->dev, "enable=%d\n", enable); |
Steve Kerrison | 0db4bf4 | 2011-08-09 07:16:21 -0300 | [diff] [blame] | 429 | |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 430 | return regmap_update_bits(priv->regmap[0], 0x00db, 0x01, enable ? 1 : 0); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 431 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 432 | |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 433 | #ifdef CONFIG_GPIOLIB |
| 434 | static int cxd2820r_gpio_direction_output(struct gpio_chip *chip, unsigned nr, |
| 435 | int val) |
| 436 | { |
Linus Walleij | 27524ff | 2015-12-09 11:46:55 -0200 | [diff] [blame] | 437 | struct cxd2820r_priv *priv = gpiochip_get_data(chip); |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 438 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 439 | u8 gpio[GPIO_COUNT]; |
| 440 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 441 | dev_dbg(&client->dev, "nr=%u val=%d\n", nr, val); |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 442 | |
| 443 | memcpy(gpio, priv->gpio, sizeof(gpio)); |
| 444 | gpio[nr] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | (val << 2); |
| 445 | |
| 446 | return cxd2820r_gpio(&priv->fe, gpio); |
| 447 | } |
| 448 | |
| 449 | static void cxd2820r_gpio_set(struct gpio_chip *chip, unsigned nr, int val) |
| 450 | { |
Linus Walleij | 27524ff | 2015-12-09 11:46:55 -0200 | [diff] [blame] | 451 | struct cxd2820r_priv *priv = gpiochip_get_data(chip); |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 452 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 453 | u8 gpio[GPIO_COUNT]; |
| 454 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 455 | dev_dbg(&client->dev, "nr=%u val=%d\n", nr, val); |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 456 | |
| 457 | memcpy(gpio, priv->gpio, sizeof(gpio)); |
| 458 | gpio[nr] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | (val << 2); |
| 459 | |
| 460 | (void) cxd2820r_gpio(&priv->fe, gpio); |
| 461 | |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | static int cxd2820r_gpio_get(struct gpio_chip *chip, unsigned nr) |
| 466 | { |
Linus Walleij | 27524ff | 2015-12-09 11:46:55 -0200 | [diff] [blame] | 467 | struct cxd2820r_priv *priv = gpiochip_get_data(chip); |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 468 | struct i2c_client *client = priv->client[0]; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 469 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 470 | dev_dbg(&client->dev, "nr=%u\n", nr); |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 471 | |
| 472 | return (priv->gpio[nr] >> 2) & 0x01; |
| 473 | } |
| 474 | #endif |
| 475 | |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 476 | static const struct dvb_frontend_ops cxd2820r_ops = { |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 477 | .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A }, |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 478 | /* default: DVB-T/T2 */ |
| 479 | .info = { |
Antti Palosaari | 9bf31ef | 2012-01-18 13:57:33 -0300 | [diff] [blame] | 480 | .name = "Sony CXD2820R", |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 481 | |
| 482 | .caps = FE_CAN_FEC_1_2 | |
| 483 | FE_CAN_FEC_2_3 | |
| 484 | FE_CAN_FEC_3_4 | |
| 485 | FE_CAN_FEC_5_6 | |
| 486 | FE_CAN_FEC_7_8 | |
| 487 | FE_CAN_FEC_AUTO | |
| 488 | FE_CAN_QPSK | |
| 489 | FE_CAN_QAM_16 | |
Antti Palosaari | 9bf31ef | 2012-01-18 13:57:33 -0300 | [diff] [blame] | 490 | FE_CAN_QAM_32 | |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 491 | FE_CAN_QAM_64 | |
Antti Palosaari | 9bf31ef | 2012-01-18 13:57:33 -0300 | [diff] [blame] | 492 | FE_CAN_QAM_128 | |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 493 | FE_CAN_QAM_256 | |
| 494 | FE_CAN_QAM_AUTO | |
| 495 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 496 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 497 | FE_CAN_HIERARCHY_AUTO | |
| 498 | FE_CAN_MUTE_TS | |
Evgeny Plehov | 4a74772 | 2013-04-07 23:56:46 -0300 | [diff] [blame] | 499 | FE_CAN_2G_MODULATION | |
| 500 | FE_CAN_MULTISTREAM |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 501 | }, |
| 502 | |
| 503 | .release = cxd2820r_release, |
| 504 | .init = cxd2820r_init, |
| 505 | .sleep = cxd2820r_sleep, |
| 506 | |
| 507 | .get_tune_settings = cxd2820r_get_tune_settings, |
| 508 | .i2c_gate_ctrl = cxd2820r_i2c_gate_ctrl, |
| 509 | |
Mauro Carvalho Chehab | f311f68 | 2011-12-30 22:22:10 -0300 | [diff] [blame] | 510 | .get_frontend = cxd2820r_get_frontend, |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 511 | |
| 512 | .get_frontend_algo = cxd2820r_get_frontend_algo, |
| 513 | .search = cxd2820r_search, |
| 514 | |
| 515 | .read_status = cxd2820r_read_status, |
| 516 | .read_snr = cxd2820r_read_snr, |
| 517 | .read_ber = cxd2820r_read_ber, |
| 518 | .read_ucblocks = cxd2820r_read_ucblocks, |
| 519 | .read_signal_strength = cxd2820r_read_signal_strength, |
Manu Abraham | 14c0386 | 2011-11-24 11:59:53 -0300 | [diff] [blame] | 520 | }; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 521 | |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 522 | /* |
| 523 | * XXX: That is wrapper to cxd2820r_probe() via driver core in order to provide |
| 524 | * proper I2C client for legacy media attach binding. |
| 525 | * New users must use I2C client binding directly! |
| 526 | */ |
| 527 | struct dvb_frontend *cxd2820r_attach(const struct cxd2820r_config *config, |
| 528 | struct i2c_adapter *adapter, |
| 529 | int *gpio_chip_base) |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 530 | { |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 531 | struct i2c_client *client; |
| 532 | struct i2c_board_info board_info; |
| 533 | struct cxd2820r_platform_data pdata; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 534 | |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 535 | pdata.ts_mode = config->ts_mode; |
| 536 | pdata.ts_clk_inv = config->ts_clock_inv; |
| 537 | pdata.if_agc_polarity = config->if_agc_polarity; |
| 538 | pdata.spec_inv = config->spec_inv; |
| 539 | pdata.gpio_chip_base = &gpio_chip_base; |
| 540 | pdata.attach_in_use = true; |
| 541 | |
| 542 | memset(&board_info, 0, sizeof(board_info)); |
| 543 | strlcpy(board_info.type, "cxd2820r", I2C_NAME_SIZE); |
| 544 | board_info.addr = config->i2c_address; |
| 545 | board_info.platform_data = &pdata; |
| 546 | client = i2c_new_device(adapter, &board_info); |
| 547 | if (!client || !client->dev.driver) |
| 548 | return NULL; |
| 549 | |
| 550 | return pdata.get_dvb_frontend(client); |
| 551 | } |
| 552 | EXPORT_SYMBOL(cxd2820r_attach); |
| 553 | |
| 554 | static struct dvb_frontend *cxd2820r_get_dvb_frontend(struct i2c_client *client) |
| 555 | { |
| 556 | struct cxd2820r_priv *priv = i2c_get_clientdata(client); |
| 557 | |
| 558 | dev_dbg(&client->dev, "\n"); |
| 559 | |
| 560 | return &priv->fe; |
| 561 | } |
| 562 | |
| 563 | static int cxd2820r_probe(struct i2c_client *client, |
| 564 | const struct i2c_device_id *id) |
| 565 | { |
| 566 | struct cxd2820r_platform_data *pdata = client->dev.platform_data; |
| 567 | struct cxd2820r_priv *priv; |
| 568 | int ret, *gpio_chip_base; |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 569 | unsigned int utmp; |
| 570 | static const struct regmap_range_cfg regmap_range_cfg0[] = { |
| 571 | { |
| 572 | .range_min = 0x0000, |
| 573 | .range_max = 0x3fff, |
| 574 | .selector_reg = 0x00, |
| 575 | .selector_mask = 0xff, |
| 576 | .selector_shift = 0, |
| 577 | .window_start = 0x00, |
| 578 | .window_len = 0x100, |
| 579 | }, |
| 580 | }; |
| 581 | static const struct regmap_range_cfg regmap_range_cfg1[] = { |
| 582 | { |
| 583 | .range_min = 0x0000, |
| 584 | .range_max = 0x01ff, |
| 585 | .selector_reg = 0x00, |
| 586 | .selector_mask = 0xff, |
| 587 | .selector_shift = 0, |
| 588 | .window_start = 0x00, |
| 589 | .window_len = 0x100, |
| 590 | }, |
| 591 | }; |
| 592 | static const struct regmap_config regmap_config0 = { |
| 593 | .reg_bits = 8, |
| 594 | .val_bits = 8, |
| 595 | .max_register = 0x3fff, |
| 596 | .ranges = regmap_range_cfg0, |
| 597 | .num_ranges = ARRAY_SIZE(regmap_range_cfg0), |
| 598 | .cache_type = REGCACHE_NONE, |
| 599 | }; |
| 600 | static const struct regmap_config regmap_config1 = { |
| 601 | .reg_bits = 8, |
| 602 | .val_bits = 8, |
| 603 | .max_register = 0x01ff, |
| 604 | .ranges = regmap_range_cfg1, |
| 605 | .num_ranges = ARRAY_SIZE(regmap_range_cfg1), |
| 606 | .cache_type = REGCACHE_NONE, |
| 607 | }; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 608 | |
| 609 | dev_dbg(&client->dev, "\n"); |
| 610 | |
| 611 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 612 | if (!priv) { |
| 613 | ret = -ENOMEM; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 614 | goto err; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 615 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 616 | |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 617 | priv->client[0] = client; |
Antti Palosaari | 238442c | 2017-01-16 19:27:41 -0200 | [diff] [blame] | 618 | priv->fe.demodulator_priv = priv; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 619 | priv->i2c = client->adapter; |
| 620 | priv->ts_mode = pdata->ts_mode; |
| 621 | priv->ts_clk_inv = pdata->ts_clk_inv; |
| 622 | priv->if_agc_polarity = pdata->if_agc_polarity; |
| 623 | priv->spec_inv = pdata->spec_inv; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 624 | gpio_chip_base = *pdata->gpio_chip_base; |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 625 | priv->regmap[0] = regmap_init_i2c(priv->client[0], ®map_config0); |
| 626 | if (IS_ERR(priv->regmap[0])) { |
| 627 | ret = PTR_ERR(priv->regmap[0]); |
| 628 | goto err_kfree; |
| 629 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 630 | |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 631 | /* Check demod answers with correct chip id */ |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 632 | ret = regmap_read(priv->regmap[0], 0x00fd, &utmp); |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 633 | if (ret) |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 634 | goto err_regmap_0_regmap_exit; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 635 | |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 636 | dev_dbg(&client->dev, "chip_id=%02x\n", utmp); |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 637 | |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 638 | if (utmp != 0xe1) { |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 639 | ret = -ENODEV; |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 640 | goto err_regmap_0_regmap_exit; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Chip has two I2C addresses for different register banks. We register |
| 645 | * one dummy I2C client in in order to get own I2C client for each |
| 646 | * register bank. |
| 647 | */ |
| 648 | priv->client[1] = i2c_new_dummy(client->adapter, client->addr | (1 << 1)); |
| 649 | if (!priv->client[1]) { |
| 650 | ret = -ENODEV; |
| 651 | dev_err(&client->dev, "I2C registration failed\n"); |
| 652 | if (ret) |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 653 | goto err_regmap_0_regmap_exit; |
| 654 | } |
| 655 | |
| 656 | priv->regmap[1] = regmap_init_i2c(priv->client[1], ®map_config1); |
| 657 | if (IS_ERR(priv->regmap[1])) { |
| 658 | ret = PTR_ERR(priv->regmap[1]); |
| 659 | goto err_client_1_i2c_unregister_device; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 660 | } |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 661 | |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 662 | if (gpio_chip_base) { |
Antti Palosaari | a36a66d | 2012-08-16 21:07:23 -0300 | [diff] [blame] | 663 | #ifdef CONFIG_GPIOLIB |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 664 | /* Add GPIOs */ |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 665 | priv->gpio_chip.label = KBUILD_MODNAME; |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 666 | priv->gpio_chip.parent = &client->dev; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 667 | priv->gpio_chip.owner = THIS_MODULE; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 668 | priv->gpio_chip.direction_output = cxd2820r_gpio_direction_output; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 669 | priv->gpio_chip.set = cxd2820r_gpio_set; |
| 670 | priv->gpio_chip.get = cxd2820r_gpio_get; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 671 | priv->gpio_chip.base = -1; /* Dynamic allocation */ |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 672 | priv->gpio_chip.ngpio = GPIO_COUNT; |
| 673 | priv->gpio_chip.can_sleep = 1; |
Linus Walleij | 27524ff | 2015-12-09 11:46:55 -0200 | [diff] [blame] | 674 | ret = gpiochip_add_data(&priv->gpio_chip, priv); |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 675 | if (ret) |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 676 | goto err_regmap_1_regmap_exit; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 677 | |
Antti Palosaari | c98975f | 2016-08-09 22:00:37 -0300 | [diff] [blame] | 678 | dev_dbg(&client->dev, "gpio_chip.base=%d\n", |
| 679 | priv->gpio_chip.base); |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 680 | |
| 681 | *gpio_chip_base = priv->gpio_chip.base; |
Antti Palosaari | a36a66d | 2012-08-16 21:07:23 -0300 | [diff] [blame] | 682 | #else |
| 683 | /* |
| 684 | * Use static GPIO configuration if GPIOLIB is undefined. |
| 685 | * This is fallback condition. |
| 686 | */ |
Antti Palosaari | 44e0d7d | 2012-10-05 18:50:12 -0300 | [diff] [blame] | 687 | u8 gpio[GPIO_COUNT]; |
Antti Palosaari | a36a66d | 2012-08-16 21:07:23 -0300 | [diff] [blame] | 688 | gpio[0] = (*gpio_chip_base >> 0) & 0x07; |
| 689 | gpio[1] = (*gpio_chip_base >> 3) & 0x07; |
| 690 | gpio[2] = 0; |
| 691 | ret = cxd2820r_gpio(&priv->fe, gpio); |
| 692 | if (ret) |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 693 | goto err_regmap_1_regmap_exit; |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 694 | #endif |
Antti Palosaari | a36a66d | 2012-08-16 21:07:23 -0300 | [diff] [blame] | 695 | } |
Antti Palosaari | 1e8f31f | 2012-07-19 21:10:36 -0300 | [diff] [blame] | 696 | |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 697 | /* Create dvb frontend */ |
| 698 | memcpy(&priv->fe.ops, &cxd2820r_ops, sizeof(priv->fe.ops)); |
| 699 | if (!pdata->attach_in_use) |
| 700 | priv->fe.ops.release = NULL; |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 701 | i2c_set_clientdata(client, priv); |
| 702 | |
| 703 | /* Setup callbacks */ |
| 704 | pdata->get_dvb_frontend = cxd2820r_get_dvb_frontend; |
| 705 | |
| 706 | dev_info(&client->dev, "Sony CXD2820R successfully identified\n"); |
| 707 | |
| 708 | return 0; |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 709 | err_regmap_1_regmap_exit: |
| 710 | regmap_exit(priv->regmap[1]); |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 711 | err_client_1_i2c_unregister_device: |
| 712 | i2c_unregister_device(priv->client[1]); |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 713 | err_regmap_0_regmap_exit: |
| 714 | regmap_exit(priv->regmap[0]); |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 715 | err_kfree: |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 716 | kfree(priv); |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 717 | err: |
| 718 | dev_dbg(&client->dev, "failed=%d\n", ret); |
| 719 | return ret; |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 720 | } |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 721 | |
| 722 | static int cxd2820r_remove(struct i2c_client *client) |
| 723 | { |
| 724 | struct cxd2820r_priv *priv = i2c_get_clientdata(client); |
| 725 | |
| 726 | dev_dbg(&client->dev, "\n"); |
| 727 | |
| 728 | #ifdef CONFIG_GPIOLIB |
| 729 | if (priv->gpio_chip.label) |
| 730 | gpiochip_remove(&priv->gpio_chip); |
| 731 | #endif |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 732 | regmap_exit(priv->regmap[1]); |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 733 | i2c_unregister_device(priv->client[1]); |
Antti Palosaari | 43e2ea6 | 2016-08-13 13:19:05 -0300 | [diff] [blame] | 734 | |
| 735 | regmap_exit(priv->regmap[0]); |
| 736 | |
Antti Palosaari | 07fdf7d | 2016-08-09 20:49:09 -0300 | [diff] [blame] | 737 | kfree(priv); |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | static const struct i2c_device_id cxd2820r_id_table[] = { |
| 743 | {"cxd2820r", 0}, |
| 744 | {} |
| 745 | }; |
| 746 | MODULE_DEVICE_TABLE(i2c, cxd2820r_id_table); |
| 747 | |
| 748 | static struct i2c_driver cxd2820r_driver = { |
| 749 | .driver = { |
| 750 | .name = "cxd2820r", |
| 751 | .suppress_bind_attrs = true, |
| 752 | }, |
| 753 | .probe = cxd2820r_probe, |
| 754 | .remove = cxd2820r_remove, |
| 755 | .id_table = cxd2820r_id_table, |
| 756 | }; |
| 757 | |
| 758 | module_i2c_driver(cxd2820r_driver); |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 759 | |
Antti Palosaari | 27cfc85 | 2011-04-07 16:27:43 -0300 | [diff] [blame] | 760 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 761 | MODULE_DESCRIPTION("Sony CXD2820R demodulator driver"); |
| 762 | MODULE_LICENSE("GPL"); |