PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / tuners / m88ts2022.c
blob40c42dec721b9de556dacc6cadc12a80ba3cc06b
1 /*
2 * Montage M88TS2022 silicon tuner driver
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
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.
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.
16 * Some calculations are taken from existing TS2020 driver.
19 #include "m88ts2022_priv.h"
21 /* write multiple registers */
22 static int m88ts2022_wr_regs(struct m88ts2022_priv *priv,
23 u8 reg, const u8 *val, int len)
25 #define MAX_WR_LEN 3
26 #define MAX_WR_XFER_LEN (MAX_WR_LEN + 1)
27 int ret;
28 u8 buf[MAX_WR_XFER_LEN];
29 struct i2c_msg msg[1] = {
31 .addr = priv->client->addr,
32 .flags = 0,
33 .len = 1 + len,
34 .buf = buf,
38 if (WARN_ON(len > MAX_WR_LEN))
39 return -EINVAL;
41 buf[0] = reg;
42 memcpy(&buf[1], val, len);
44 ret = i2c_transfer(priv->client->adapter, msg, 1);
45 if (ret == 1) {
46 ret = 0;
47 } else {
48 dev_warn(&priv->client->dev,
49 "%s: i2c wr failed=%d reg=%02x len=%d\n",
50 KBUILD_MODNAME, ret, reg, len);
51 ret = -EREMOTEIO;
54 return ret;
57 /* read multiple registers */
58 static int m88ts2022_rd_regs(struct m88ts2022_priv *priv, u8 reg,
59 u8 *val, int len)
61 #define MAX_RD_LEN 1
62 #define MAX_RD_XFER_LEN (MAX_RD_LEN)
63 int ret;
64 u8 buf[MAX_RD_XFER_LEN];
65 struct i2c_msg msg[2] = {
67 .addr = priv->client->addr,
68 .flags = 0,
69 .len = 1,
70 .buf = &reg,
71 }, {
72 .addr = priv->client->addr,
73 .flags = I2C_M_RD,
74 .len = len,
75 .buf = buf,
79 if (WARN_ON(len > MAX_RD_LEN))
80 return -EINVAL;
82 ret = i2c_transfer(priv->client->adapter, msg, 2);
83 if (ret == 2) {
84 memcpy(val, buf, len);
85 ret = 0;
86 } else {
87 dev_warn(&priv->client->dev,
88 "%s: i2c rd failed=%d reg=%02x len=%d\n",
89 KBUILD_MODNAME, ret, reg, len);
90 ret = -EREMOTEIO;
93 return ret;
96 /* write single register */
97 static int m88ts2022_wr_reg(struct m88ts2022_priv *priv, u8 reg, u8 val)
99 return m88ts2022_wr_regs(priv, reg, &val, 1);
102 /* read single register */
103 static int m88ts2022_rd_reg(struct m88ts2022_priv *priv, u8 reg, u8 *val)
105 return m88ts2022_rd_regs(priv, reg, val, 1);
108 /* write single register with mask */
109 static int m88ts2022_wr_reg_mask(struct m88ts2022_priv *priv,
110 u8 reg, u8 val, u8 mask)
112 int ret;
113 u8 u8tmp;
115 /* no need for read if whole reg is written */
116 if (mask != 0xff) {
117 ret = m88ts2022_rd_regs(priv, reg, &u8tmp, 1);
118 if (ret)
119 return ret;
121 val &= mask;
122 u8tmp &= ~mask;
123 val |= u8tmp;
126 return m88ts2022_wr_regs(priv, reg, &val, 1);
129 static int m88ts2022_cmd(struct dvb_frontend *fe,
130 int op, int sleep, u8 reg, u8 mask, u8 val, u8 *reg_val)
132 struct m88ts2022_priv *priv = fe->tuner_priv;
133 int ret, i;
134 u8 u8tmp;
135 struct m88ts2022_reg_val reg_vals[] = {
136 {0x51, 0x1f - op},
137 {0x51, 0x1f},
138 {0x50, 0x00 + op},
139 {0x50, 0x00},
142 for (i = 0; i < 2; i++) {
143 dev_dbg(&priv->client->dev,
144 "%s: i=%d op=%02x reg=%02x mask=%02x val=%02x\n",
145 __func__, i, op, reg, mask, val);
147 for (i = 0; i < ARRAY_SIZE(reg_vals); i++) {
148 ret = m88ts2022_wr_reg(priv, reg_vals[i].reg,
149 reg_vals[i].val);
150 if (ret)
151 goto err;
154 usleep_range(sleep * 1000, sleep * 10000);
156 ret = m88ts2022_rd_reg(priv, reg, &u8tmp);
157 if (ret)
158 goto err;
160 if ((u8tmp & mask) != val)
161 break;
164 if (reg_val)
165 *reg_val = u8tmp;
166 err:
167 return ret;
170 static int m88ts2022_set_params(struct dvb_frontend *fe)
172 struct m88ts2022_priv *priv = fe->tuner_priv;
173 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
174 int ret;
175 unsigned int frequency_khz, frequency_offset_khz, f_3db_hz;
176 unsigned int f_ref_khz, f_vco_khz, div_ref, div_out, pll_n, gdiv28;
177 u8 buf[3], u8tmp, cap_code, lpf_gm, lpf_mxdiv, div_max, div_min;
178 u16 u16tmp;
179 dev_dbg(&priv->client->dev,
180 "%s: frequency=%d symbol_rate=%d rolloff=%d\n",
181 __func__, c->frequency, c->symbol_rate, c->rolloff);
183 * Integer-N PLL synthesizer
184 * kHz is used for all calculations to keep calculations within 32-bit
186 f_ref_khz = DIV_ROUND_CLOSEST(priv->cfg.clock, 1000);
187 div_ref = DIV_ROUND_CLOSEST(f_ref_khz, 2000);
189 if (c->symbol_rate < 5000000)
190 frequency_offset_khz = 3000; /* 3 MHz */
191 else
192 frequency_offset_khz = 0;
194 frequency_khz = c->frequency + frequency_offset_khz;
196 if (frequency_khz < 1103000) {
197 div_out = 4;
198 u8tmp = 0x1b;
199 } else {
200 div_out = 2;
201 u8tmp = 0x0b;
204 buf[0] = u8tmp;
205 buf[1] = 0x40;
206 ret = m88ts2022_wr_regs(priv, 0x10, buf, 2);
207 if (ret)
208 goto err;
210 f_vco_khz = frequency_khz * div_out;
211 pll_n = f_vco_khz * div_ref / f_ref_khz;
212 pll_n += pll_n % 2;
213 priv->frequency_khz = pll_n * f_ref_khz / div_ref / div_out;
215 if (pll_n < 4095)
216 u16tmp = pll_n - 1024;
217 else if (pll_n < 6143)
218 u16tmp = pll_n + 1024;
219 else
220 u16tmp = pll_n + 3072;
222 buf[0] = (u16tmp >> 8) & 0x3f;
223 buf[1] = (u16tmp >> 0) & 0xff;
224 buf[2] = div_ref - 8;
225 ret = m88ts2022_wr_regs(priv, 0x01, buf, 3);
226 if (ret)
227 goto err;
229 dev_dbg(&priv->client->dev,
230 "%s: frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
231 __func__, priv->frequency_khz,
232 priv->frequency_khz - c->frequency, f_vco_khz, pll_n,
233 div_ref, div_out);
235 ret = m88ts2022_cmd(fe, 0x10, 5, 0x15, 0x40, 0x00, NULL);
236 if (ret)
237 goto err;
239 ret = m88ts2022_rd_reg(priv, 0x14, &u8tmp);
240 if (ret)
241 goto err;
243 u8tmp &= 0x7f;
244 if (u8tmp < 64) {
245 ret = m88ts2022_wr_reg_mask(priv, 0x10, 0x80, 0x80);
246 if (ret)
247 goto err;
249 ret = m88ts2022_wr_reg(priv, 0x11, 0x6f);
250 if (ret)
251 goto err;
253 ret = m88ts2022_cmd(fe, 0x10, 5, 0x15, 0x40, 0x00, NULL);
254 if (ret)
255 goto err;
258 ret = m88ts2022_rd_reg(priv, 0x14, &u8tmp);
259 if (ret)
260 goto err;
262 u8tmp &= 0x1f;
263 if (u8tmp > 19) {
264 ret = m88ts2022_wr_reg_mask(priv, 0x10, 0x00, 0x02);
265 if (ret)
266 goto err;
269 ret = m88ts2022_cmd(fe, 0x08, 5, 0x3c, 0xff, 0x00, NULL);
270 if (ret)
271 goto err;
273 ret = m88ts2022_wr_reg(priv, 0x25, 0x00);
274 if (ret)
275 goto err;
277 ret = m88ts2022_wr_reg(priv, 0x27, 0x70);
278 if (ret)
279 goto err;
281 ret = m88ts2022_wr_reg(priv, 0x41, 0x09);
282 if (ret)
283 goto err;
285 ret = m88ts2022_wr_reg(priv, 0x08, 0x0b);
286 if (ret)
287 goto err;
289 /* filters */
290 gdiv28 = DIV_ROUND_CLOSEST(f_ref_khz * 1694U, 1000000U);
292 ret = m88ts2022_wr_reg(priv, 0x04, gdiv28);
293 if (ret)
294 goto err;
296 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
297 if (ret)
298 goto err;
300 cap_code = u8tmp & 0x3f;
302 ret = m88ts2022_wr_reg(priv, 0x41, 0x0d);
303 if (ret)
304 goto err;
306 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
307 if (ret)
308 goto err;
310 u8tmp &= 0x3f;
311 cap_code = (cap_code + u8tmp) / 2;
312 gdiv28 = gdiv28 * 207 / (cap_code * 2 + 151);
313 div_max = gdiv28 * 135 / 100;
314 div_min = gdiv28 * 78 / 100;
315 div_max = clamp_val(div_max, 0U, 63U);
317 f_3db_hz = c->symbol_rate * 135UL / 200UL;
318 f_3db_hz += 2000000U + (frequency_offset_khz * 1000U);
319 f_3db_hz = clamp(f_3db_hz, 7000000U, 40000000U);
321 #define LPF_COEFF 3200U
322 lpf_gm = DIV_ROUND_CLOSEST(f_3db_hz * gdiv28, LPF_COEFF * f_ref_khz);
323 lpf_gm = clamp_val(lpf_gm, 1U, 23U);
325 lpf_mxdiv = DIV_ROUND_CLOSEST(lpf_gm * LPF_COEFF * f_ref_khz, f_3db_hz);
326 if (lpf_mxdiv < div_min)
327 lpf_mxdiv = DIV_ROUND_CLOSEST(++lpf_gm * LPF_COEFF * f_ref_khz, f_3db_hz);
328 lpf_mxdiv = clamp_val(lpf_mxdiv, 0U, div_max);
330 ret = m88ts2022_wr_reg(priv, 0x04, lpf_mxdiv);
331 if (ret)
332 goto err;
334 ret = m88ts2022_wr_reg(priv, 0x06, lpf_gm);
335 if (ret)
336 goto err;
338 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
339 if (ret)
340 goto err;
342 cap_code = u8tmp & 0x3f;
344 ret = m88ts2022_wr_reg(priv, 0x41, 0x09);
345 if (ret)
346 goto err;
348 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
349 if (ret)
350 goto err;
352 u8tmp &= 0x3f;
353 cap_code = (cap_code + u8tmp) / 2;
355 u8tmp = cap_code | 0x80;
356 ret = m88ts2022_wr_reg(priv, 0x25, u8tmp);
357 if (ret)
358 goto err;
360 ret = m88ts2022_wr_reg(priv, 0x27, 0x30);
361 if (ret)
362 goto err;
364 ret = m88ts2022_wr_reg(priv, 0x08, 0x09);
365 if (ret)
366 goto err;
368 ret = m88ts2022_cmd(fe, 0x01, 20, 0x21, 0xff, 0x00, NULL);
369 if (ret)
370 goto err;
371 err:
372 if (ret)
373 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
375 return ret;
378 static int m88ts2022_init(struct dvb_frontend *fe)
380 struct m88ts2022_priv *priv = fe->tuner_priv;
381 int ret, i;
382 u8 u8tmp;
383 static const struct m88ts2022_reg_val reg_vals[] = {
384 {0x7d, 0x9d},
385 {0x7c, 0x9a},
386 {0x7a, 0x76},
387 {0x3b, 0x01},
388 {0x63, 0x88},
389 {0x61, 0x85},
390 {0x22, 0x30},
391 {0x30, 0x40},
392 {0x20, 0x23},
393 {0x24, 0x02},
394 {0x12, 0xa0},
396 dev_dbg(&priv->client->dev, "%s:\n", __func__);
398 ret = m88ts2022_wr_reg(priv, 0x00, 0x01);
399 if (ret)
400 goto err;
402 ret = m88ts2022_wr_reg(priv, 0x00, 0x03);
403 if (ret)
404 goto err;
406 switch (priv->cfg.clock_out) {
407 case M88TS2022_CLOCK_OUT_DISABLED:
408 u8tmp = 0x60;
409 break;
410 case M88TS2022_CLOCK_OUT_ENABLED:
411 u8tmp = 0x70;
412 ret = m88ts2022_wr_reg(priv, 0x05, priv->cfg.clock_out_div);
413 if (ret)
414 goto err;
415 break;
416 case M88TS2022_CLOCK_OUT_ENABLED_XTALOUT:
417 u8tmp = 0x6c;
418 break;
419 default:
420 goto err;
423 ret = m88ts2022_wr_reg(priv, 0x42, u8tmp);
424 if (ret)
425 goto err;
427 if (priv->cfg.loop_through)
428 u8tmp = 0xec;
429 else
430 u8tmp = 0x6c;
432 ret = m88ts2022_wr_reg(priv, 0x62, u8tmp);
433 if (ret)
434 goto err;
436 for (i = 0; i < ARRAY_SIZE(reg_vals); i++) {
437 ret = m88ts2022_wr_reg(priv, reg_vals[i].reg, reg_vals[i].val);
438 if (ret)
439 goto err;
441 err:
442 if (ret)
443 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
444 return ret;
447 static int m88ts2022_sleep(struct dvb_frontend *fe)
449 struct m88ts2022_priv *priv = fe->tuner_priv;
450 int ret;
451 dev_dbg(&priv->client->dev, "%s:\n", __func__);
453 ret = m88ts2022_wr_reg(priv, 0x00, 0x00);
454 if (ret)
455 goto err;
456 err:
457 if (ret)
458 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
459 return ret;
462 static int m88ts2022_get_frequency(struct dvb_frontend *fe, u32 *frequency)
464 struct m88ts2022_priv *priv = fe->tuner_priv;
465 dev_dbg(&priv->client->dev, "%s:\n", __func__);
467 *frequency = priv->frequency_khz;
468 return 0;
471 static int m88ts2022_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
473 struct m88ts2022_priv *priv = fe->tuner_priv;
474 dev_dbg(&priv->client->dev, "%s:\n", __func__);
476 *frequency = 0; /* Zero-IF */
477 return 0;
480 static int m88ts2022_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
482 struct m88ts2022_priv *priv = fe->tuner_priv;
483 int ret;
484 u8 u8tmp;
485 u16 gain, u16tmp;
486 unsigned int gain1, gain2, gain3;
488 ret = m88ts2022_rd_reg(priv, 0x3d, &u8tmp);
489 if (ret)
490 goto err;
492 gain1 = (u8tmp >> 0) & 0x1f;
493 gain1 = clamp(gain1, 0U, 15U);
495 ret = m88ts2022_rd_reg(priv, 0x21, &u8tmp);
496 if (ret)
497 goto err;
499 gain2 = (u8tmp >> 0) & 0x1f;
500 gain2 = clamp(gain2, 2U, 16U);
502 ret = m88ts2022_rd_reg(priv, 0x66, &u8tmp);
503 if (ret)
504 goto err;
506 gain3 = (u8tmp >> 3) & 0x07;
507 gain3 = clamp(gain3, 0U, 6U);
509 gain = gain1 * 265 + gain2 * 338 + gain3 * 285;
511 /* scale value to 0x0000-0xffff */
512 u16tmp = (0xffff - gain);
513 u16tmp = clamp_val(u16tmp, 59000U, 61500U);
515 *strength = (u16tmp - 59000) * 0xffff / (61500 - 59000);
516 err:
517 if (ret)
518 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
519 return ret;
522 static const struct dvb_tuner_ops m88ts2022_tuner_ops = {
523 .info = {
524 .name = "Montage M88TS2022",
525 .frequency_min = 950000,
526 .frequency_max = 2150000,
529 .init = m88ts2022_init,
530 .sleep = m88ts2022_sleep,
531 .set_params = m88ts2022_set_params,
533 .get_frequency = m88ts2022_get_frequency,
534 .get_if_frequency = m88ts2022_get_if_frequency,
535 .get_rf_strength = m88ts2022_get_rf_strength,
538 static int m88ts2022_probe(struct i2c_client *client,
539 const struct i2c_device_id *id)
541 struct m88ts2022_config *cfg = client->dev.platform_data;
542 struct dvb_frontend *fe = cfg->fe;
543 struct m88ts2022_priv *priv;
544 int ret;
545 u8 chip_id, u8tmp;
547 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
548 if (!priv) {
549 ret = -ENOMEM;
550 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
551 goto err;
554 memcpy(&priv->cfg, cfg, sizeof(struct m88ts2022_config));
555 priv->client = client;
557 /* check if the tuner is there */
558 ret = m88ts2022_rd_reg(priv, 0x00, &u8tmp);
559 if (ret)
560 goto err;
562 if ((u8tmp & 0x03) == 0x00) {
563 ret = m88ts2022_wr_reg(priv, 0x00, 0x01);
564 if (ret < 0)
565 goto err;
567 usleep_range(2000, 50000);
570 ret = m88ts2022_wr_reg(priv, 0x00, 0x03);
571 if (ret)
572 goto err;
574 usleep_range(2000, 50000);
576 ret = m88ts2022_rd_reg(priv, 0x00, &chip_id);
577 if (ret)
578 goto err;
580 dev_dbg(&priv->client->dev, "%s: chip_id=%02x\n", __func__, chip_id);
582 switch (chip_id) {
583 case 0xc3:
584 case 0x83:
585 break;
586 default:
587 goto err;
590 switch (priv->cfg.clock_out) {
591 case M88TS2022_CLOCK_OUT_DISABLED:
592 u8tmp = 0x60;
593 break;
594 case M88TS2022_CLOCK_OUT_ENABLED:
595 u8tmp = 0x70;
596 ret = m88ts2022_wr_reg(priv, 0x05, priv->cfg.clock_out_div);
597 if (ret)
598 goto err;
599 break;
600 case M88TS2022_CLOCK_OUT_ENABLED_XTALOUT:
601 u8tmp = 0x6c;
602 break;
603 default:
604 goto err;
607 ret = m88ts2022_wr_reg(priv, 0x42, u8tmp);
608 if (ret)
609 goto err;
611 if (priv->cfg.loop_through)
612 u8tmp = 0xec;
613 else
614 u8tmp = 0x6c;
616 ret = m88ts2022_wr_reg(priv, 0x62, u8tmp);
617 if (ret)
618 goto err;
620 /* sleep */
621 ret = m88ts2022_wr_reg(priv, 0x00, 0x00);
622 if (ret)
623 goto err;
625 dev_info(&priv->client->dev,
626 "%s: Montage M88TS2022 successfully identified\n",
627 KBUILD_MODNAME);
629 fe->tuner_priv = priv;
630 memcpy(&fe->ops.tuner_ops, &m88ts2022_tuner_ops,
631 sizeof(struct dvb_tuner_ops));
633 i2c_set_clientdata(client, priv);
634 return 0;
635 err:
636 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
637 kfree(priv);
638 return ret;
641 static int m88ts2022_remove(struct i2c_client *client)
643 struct m88ts2022_priv *priv = i2c_get_clientdata(client);
644 struct dvb_frontend *fe = priv->cfg.fe;
645 dev_dbg(&client->dev, "%s:\n", __func__);
647 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
648 fe->tuner_priv = NULL;
649 kfree(priv);
651 return 0;
654 static const struct i2c_device_id m88ts2022_id[] = {
655 {"m88ts2022", 0},
658 MODULE_DEVICE_TABLE(i2c, m88ts2022_id);
660 static struct i2c_driver m88ts2022_driver = {
661 .driver = {
662 .owner = THIS_MODULE,
663 .name = "m88ts2022",
665 .probe = m88ts2022_probe,
666 .remove = m88ts2022_remove,
667 .id_table = m88ts2022_id,
670 module_i2c_driver(m88ts2022_driver);
672 MODULE_DESCRIPTION("Montage M88TS2022 silicon tuner driver");
673 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
674 MODULE_LICENSE("GPL");