Merge branch 'pm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[firefly-linux-kernel-4.4.55.git] / drivers / media / common / tuners / tda18212.c
1 /*
2  * NXP TDA18212HN silicon tuner 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  *
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include "tda18212.h"
24
25 struct tda18212_priv {
26         struct tda18212_config *cfg;
27         struct i2c_adapter *i2c;
28
29         u32 if_frequency;
30 };
31
32 #define dbg(fmt, arg...)                                        \
33 do {                                                            \
34         if (debug)                                              \
35                 pr_info("%s: " fmt, __func__, ##arg);           \
36 } while (0)
37
38 static int debug;
39 module_param(debug, int, 0644);
40 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
41
42 /* write multiple registers */
43 static int tda18212_wr_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
44         int len)
45 {
46         int ret;
47         u8 buf[len+1];
48         struct i2c_msg msg[1] = {
49                 {
50                         .addr = priv->cfg->i2c_address,
51                         .flags = 0,
52                         .len = sizeof(buf),
53                         .buf = buf,
54                 }
55         };
56
57         buf[0] = reg;
58         memcpy(&buf[1], val, len);
59
60         ret = i2c_transfer(priv->i2c, msg, 1);
61         if (ret == 1) {
62                 ret = 0;
63         } else {
64                 pr_warn("i2c wr failed ret:%d reg:%02x len:%d\n",
65                         ret, reg, len);
66                 ret = -EREMOTEIO;
67         }
68         return ret;
69 }
70
71 /* read multiple registers */
72 static int tda18212_rd_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
73         int len)
74 {
75         int ret;
76         u8 buf[len];
77         struct i2c_msg msg[2] = {
78                 {
79                         .addr = priv->cfg->i2c_address,
80                         .flags = 0,
81                         .len = 1,
82                         .buf = &reg,
83                 }, {
84                         .addr = priv->cfg->i2c_address,
85                         .flags = I2C_M_RD,
86                         .len = sizeof(buf),
87                         .buf = buf,
88                 }
89         };
90
91         ret = i2c_transfer(priv->i2c, msg, 2);
92         if (ret == 2) {
93                 memcpy(val, buf, len);
94                 ret = 0;
95         } else {
96                 pr_warn("i2c rd failed ret:%d reg:%02x len:%d\n",
97                         ret, reg, len);
98                 ret = -EREMOTEIO;
99         }
100
101         return ret;
102 }
103
104 /* write single register */
105 static int tda18212_wr_reg(struct tda18212_priv *priv, u8 reg, u8 val)
106 {
107         return tda18212_wr_regs(priv, reg, &val, 1);
108 }
109
110 /* read single register */
111 static int tda18212_rd_reg(struct tda18212_priv *priv, u8 reg, u8 *val)
112 {
113         return tda18212_rd_regs(priv, reg, val, 1);
114 }
115
116 #if 0 /* keep, useful when developing driver */
117 static void tda18212_dump_regs(struct tda18212_priv *priv)
118 {
119         int i;
120         u8 buf[256];
121
122         #define TDA18212_RD_LEN 32
123         for (i = 0; i < sizeof(buf); i += TDA18212_RD_LEN)
124                 tda18212_rd_regs(priv, i, &buf[i], TDA18212_RD_LEN);
125
126         print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf,
127                 sizeof(buf), true);
128
129         return;
130 }
131 #endif
132
133 static int tda18212_set_params(struct dvb_frontend *fe)
134 {
135         struct tda18212_priv *priv = fe->tuner_priv;
136         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
137         int ret, i;
138         u32 if_khz;
139         u8 buf[9];
140         #define DVBT_6   0
141         #define DVBT_7   1
142         #define DVBT_8   2
143         #define DVBT2_6  3
144         #define DVBT2_7  4
145         #define DVBT2_8  5
146         #define DVBC_6   6
147         #define DVBC_8   7
148         static const u8 bw_params[][3] = {
149                      /* reg:   0f    13    23 */
150                 [DVBT_6]  = { 0xb3, 0x20, 0x03 },
151                 [DVBT_7]  = { 0xb3, 0x31, 0x01 },
152                 [DVBT_8]  = { 0xb3, 0x22, 0x01 },
153                 [DVBT2_6] = { 0xbc, 0x20, 0x03 },
154                 [DVBT2_7] = { 0xbc, 0x72, 0x03 },
155                 [DVBT2_8] = { 0xbc, 0x22, 0x01 },
156                 [DVBC_6]  = { 0x92, 0x50, 0x03 },
157                 [DVBC_8]  = { 0x92, 0x53, 0x03 },
158         };
159
160         dbg("delsys=%d RF=%d BW=%d\n",
161             c->delivery_system, c->frequency, c->bandwidth_hz);
162
163         if (fe->ops.i2c_gate_ctrl)
164                 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
165
166         switch (c->delivery_system) {
167         case SYS_DVBT:
168                 switch (c->bandwidth_hz) {
169                 case 6000000:
170                         if_khz = priv->cfg->if_dvbt_6;
171                         i = DVBT_6;
172                         break;
173                 case 7000000:
174                         if_khz = priv->cfg->if_dvbt_7;
175                         i = DVBT_7;
176                         break;
177                 case 8000000:
178                         if_khz = priv->cfg->if_dvbt_8;
179                         i = DVBT_8;
180                         break;
181                 default:
182                         ret = -EINVAL;
183                         goto error;
184                 }
185                 break;
186         case SYS_DVBT2:
187                 switch (c->bandwidth_hz) {
188                 case 6000000:
189                         if_khz = priv->cfg->if_dvbt2_6;
190                         i = DVBT2_6;
191                         break;
192                 case 7000000:
193                         if_khz = priv->cfg->if_dvbt2_7;
194                         i = DVBT2_7;
195                         break;
196                 case 8000000:
197                         if_khz = priv->cfg->if_dvbt2_8;
198                         i = DVBT2_8;
199                         break;
200                 default:
201                         ret = -EINVAL;
202                         goto error;
203                 }
204                 break;
205         case SYS_DVBC_ANNEX_A:
206         case SYS_DVBC_ANNEX_C:
207                 if_khz = priv->cfg->if_dvbc;
208                 i = DVBC_8;
209                 break;
210         default:
211                 ret = -EINVAL;
212                 goto error;
213         }
214
215         ret = tda18212_wr_reg(priv, 0x23, bw_params[i][2]);
216         if (ret)
217                 goto error;
218
219         ret = tda18212_wr_reg(priv, 0x06, 0x00);
220         if (ret)
221                 goto error;
222
223         ret = tda18212_wr_reg(priv, 0x0f, bw_params[i][0]);
224         if (ret)
225                 goto error;
226
227         buf[0] = 0x02;
228         buf[1] = bw_params[i][1];
229         buf[2] = 0x03; /* default value */
230         buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
231         buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
232         buf[5] = ((c->frequency / 1000) >>  8) & 0xff;
233         buf[6] = ((c->frequency / 1000) >>  0) & 0xff;
234         buf[7] = 0xc1;
235         buf[8] = 0x01;
236         ret = tda18212_wr_regs(priv, 0x12, buf, sizeof(buf));
237         if (ret)
238                 goto error;
239
240         /* actual IF rounded as it is on register */
241         priv->if_frequency = buf[3] * 50 * 1000;
242
243 exit:
244         if (fe->ops.i2c_gate_ctrl)
245                 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
246
247         return ret;
248
249 error:
250         dbg("failed:%d\n", ret);
251         goto exit;
252 }
253
254 static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
255 {
256         struct tda18212_priv *priv = fe->tuner_priv;
257
258         *frequency = priv->if_frequency;
259
260         return 0;
261 }
262
263 static int tda18212_release(struct dvb_frontend *fe)
264 {
265         kfree(fe->tuner_priv);
266         fe->tuner_priv = NULL;
267         return 0;
268 }
269
270 static const struct dvb_tuner_ops tda18212_tuner_ops = {
271         .info = {
272                 .name           = "NXP TDA18212",
273
274                 .frequency_min  =  48000000,
275                 .frequency_max  = 864000000,
276                 .frequency_step =      1000,
277         },
278
279         .release       = tda18212_release,
280
281         .set_params    = tda18212_set_params,
282         .get_if_frequency = tda18212_get_if_frequency,
283 };
284
285 struct dvb_frontend *tda18212_attach(struct dvb_frontend *fe,
286         struct i2c_adapter *i2c, struct tda18212_config *cfg)
287 {
288         struct tda18212_priv *priv = NULL;
289         int ret;
290         u8 val;
291
292         priv = kzalloc(sizeof(struct tda18212_priv), GFP_KERNEL);
293         if (priv == NULL)
294                 return NULL;
295
296         priv->cfg = cfg;
297         priv->i2c = i2c;
298         fe->tuner_priv = priv;
299
300         if (fe->ops.i2c_gate_ctrl)
301                 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
302
303         /* check if the tuner is there */
304         ret = tda18212_rd_reg(priv, 0x00, &val);
305
306         if (fe->ops.i2c_gate_ctrl)
307                 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
308
309         dbg("ret:%d chip ID:%02x\n", ret, val);
310         if (ret || val != 0xc7) {
311                 kfree(priv);
312                 return NULL;
313         }
314
315         pr_info("NXP TDA18212HN successfully identified\n");
316
317         memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
318                 sizeof(struct dvb_tuner_ops));
319
320         return fe;
321 }
322 EXPORT_SYMBOL(tda18212_attach);
323
324 MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
325 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
326 MODULE_LICENSE("GPL");