Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 1 | /* |
| 2 | Driver for Philips tda8262/tda8263 DVBS Silicon tuners |
| 3 | |
| 4 | (c) 2006 Andrew de Quincey |
| 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 | |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | |
| 21 | */ |
| 22 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/dvb/frontend.h> |
| 26 | #include <asm/types.h> |
| 27 | |
| 28 | #include "tda826x.h" |
| 29 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 30 | static int debug; |
Andrew de Quincey | 8b9a466 | 2006-08-08 09:10:11 -0300 | [diff] [blame] | 31 | #define dprintk(args...) \ |
| 32 | do { \ |
| 33 | if (debug) printk(KERN_DEBUG "tda826x: " args); \ |
| 34 | } while (0) |
| 35 | |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 36 | struct tda826x_priv { |
| 37 | /* i2c details */ |
| 38 | int i2c_address; |
| 39 | struct i2c_adapter *i2c; |
| 40 | u8 has_loopthrough:1; |
| 41 | u32 frequency; |
| 42 | }; |
| 43 | |
| 44 | static int tda826x_release(struct dvb_frontend *fe) |
| 45 | { |
Michael Krufky | 2213918 | 2006-11-19 19:49:11 -0300 | [diff] [blame] | 46 | kfree(fe->tuner_priv); |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 47 | fe->tuner_priv = NULL; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int tda826x_sleep(struct dvb_frontend *fe) |
| 52 | { |
| 53 | struct tda826x_priv *priv = fe->tuner_priv; |
| 54 | int ret; |
| 55 | u8 buf [] = { 0x00, 0x8d }; |
| 56 | struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 }; |
| 57 | |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 58 | dprintk("%s:\n", __func__); |
Andrew de Quincey | 8b9a466 | 2006-08-08 09:10:11 -0300 | [diff] [blame] | 59 | |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 60 | if (!priv->has_loopthrough) |
| 61 | buf[1] = 0xad; |
| 62 | |
| 63 | if (fe->ops.i2c_gate_ctrl) |
| 64 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 65 | if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) { |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 66 | dprintk("%s: i2c error\n", __func__); |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 67 | } |
| 68 | if (fe->ops.i2c_gate_ctrl) |
| 69 | fe->ops.i2c_gate_ctrl(fe, 0); |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 70 | |
| 71 | return (ret == 1) ? 0 : ret; |
| 72 | } |
| 73 | |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 74 | static int tda826x_set_params(struct dvb_frontend *fe) |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 75 | { |
Mauro Carvalho Chehab | 66e6cd5 | 2011-12-23 18:43:29 -0300 | [diff] [blame] | 76 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 77 | struct tda826x_priv *priv = fe->tuner_priv; |
| 78 | int ret; |
| 79 | u32 div; |
Hartmut Hackmann | 3ff9a81 | 2008-04-13 22:41:19 -0300 | [diff] [blame] | 80 | u32 ksyms; |
| 81 | u32 bandwidth; |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 82 | u8 buf [11]; |
| 83 | struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 }; |
| 84 | |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 85 | dprintk("%s:\n", __func__); |
Andrew de Quincey | 8b9a466 | 2006-08-08 09:10:11 -0300 | [diff] [blame] | 86 | |
Mauro Carvalho Chehab | 66e6cd5 | 2011-12-23 18:43:29 -0300 | [diff] [blame] | 87 | div = (p->frequency + (1000-1)) / 1000; |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 88 | |
Hartmut Hackmann | 3ff9a81 | 2008-04-13 22:41:19 -0300 | [diff] [blame] | 89 | /* BW = ((1 + RO) * SR/2 + 5) * 1.3 [SR in MSPS, BW in MHz] */ |
| 90 | /* with R0 = 0.35 and some transformations: */ |
Mauro Carvalho Chehab | 66e6cd5 | 2011-12-23 18:43:29 -0300 | [diff] [blame] | 91 | ksyms = p->symbol_rate / 1000; |
Hartmut Hackmann | 3ff9a81 | 2008-04-13 22:41:19 -0300 | [diff] [blame] | 92 | bandwidth = (878 * ksyms + 6500000) / 1000000 + 1; |
| 93 | if (bandwidth < 5) |
| 94 | bandwidth = 5; |
| 95 | else if (bandwidth > 36) |
| 96 | bandwidth = 36; |
| 97 | |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 98 | buf[0] = 0x00; // subaddress |
| 99 | buf[1] = 0x09; // powerdown RSSI + the magic value 1 |
| 100 | if (!priv->has_loopthrough) |
| 101 | buf[1] |= 0x20; // power down loopthrough if not needed |
| 102 | buf[2] = (1<<5) | 0x0b; // 1Mhz + 0.45 VCO |
| 103 | buf[3] = div >> 7; |
| 104 | buf[4] = div << 1; |
Hartmut Hackmann | 3ff9a81 | 2008-04-13 22:41:19 -0300 | [diff] [blame] | 105 | buf[5] = ((bandwidth - 5) << 3) | 7; /* baseband cut-off */ |
Oliver Endriss | c660415 | 2007-05-28 18:06:27 -0300 | [diff] [blame] | 106 | buf[6] = 0xfe; // baseband gain 9 db + no RF attenuation |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 107 | buf[7] = 0x83; // charge pumps at high, tests off |
| 108 | buf[8] = 0x80; // recommended value 4 for AMPVCO + disable ports. |
| 109 | buf[9] = 0x1a; // normal caltime + recommended values for SELTH + SELVTL |
| 110 | buf[10] = 0xd4; // recommended value 13 for BBIAS + unknown bit set on |
| 111 | |
| 112 | if (fe->ops.i2c_gate_ctrl) |
| 113 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 114 | if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) { |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 115 | dprintk("%s: i2c error\n", __func__); |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 116 | } |
| 117 | if (fe->ops.i2c_gate_ctrl) |
| 118 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 119 | |
| 120 | priv->frequency = div * 1000; |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 121 | |
| 122 | return (ret == 1) ? 0 : ret; |
| 123 | } |
| 124 | |
| 125 | static int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 126 | { |
| 127 | struct tda826x_priv *priv = fe->tuner_priv; |
| 128 | *frequency = priv->frequency; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static struct dvb_tuner_ops tda826x_tuner_ops = { |
Trent Piepho | ef09c07 | 2006-08-08 09:10:11 -0300 | [diff] [blame] | 133 | .info = { |
| 134 | .name = "Philips TDA826X", |
| 135 | .frequency_min = 950000, |
Alexey Dobriyan | d027c4d | 2006-11-03 07:14:32 -0300 | [diff] [blame] | 136 | .frequency_max = 2175000 |
Trent Piepho | ef09c07 | 2006-08-08 09:10:11 -0300 | [diff] [blame] | 137 | }, |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 138 | .release = tda826x_release, |
| 139 | .sleep = tda826x_sleep, |
| 140 | .set_params = tda826x_set_params, |
| 141 | .get_frequency = tda826x_get_frequency, |
| 142 | }; |
| 143 | |
| 144 | struct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough) |
| 145 | { |
| 146 | struct tda826x_priv *priv = NULL; |
| 147 | u8 b1 [] = { 0, 0 }; |
Patrick Boettcher | 8949f1a | 2006-11-21 05:35:10 -0300 | [diff] [blame] | 148 | struct i2c_msg msg[2] = { |
| 149 | { .addr = addr, .flags = 0, .buf = NULL, .len = 0 }, |
| 150 | { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 } |
| 151 | }; |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 152 | int ret; |
| 153 | |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 154 | dprintk("%s:\n", __func__); |
Andrew de Quincey | 8b9a466 | 2006-08-08 09:10:11 -0300 | [diff] [blame] | 155 | |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 156 | if (fe->ops.i2c_gate_ctrl) |
| 157 | fe->ops.i2c_gate_ctrl(fe, 1); |
Patrick Boettcher | 8949f1a | 2006-11-21 05:35:10 -0300 | [diff] [blame] | 158 | ret = i2c_transfer (i2c, msg, 2); |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 159 | if (fe->ops.i2c_gate_ctrl) |
| 160 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 161 | |
Patrick Boettcher | 8949f1a | 2006-11-21 05:35:10 -0300 | [diff] [blame] | 162 | if (ret != 2) |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 163 | return NULL; |
| 164 | if (!(b1[1] & 0x80)) |
| 165 | return NULL; |
| 166 | |
| 167 | priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL); |
| 168 | if (priv == NULL) |
| 169 | return NULL; |
| 170 | |
| 171 | priv->i2c_address = addr; |
| 172 | priv->i2c = i2c; |
| 173 | priv->has_loopthrough = has_loopthrough; |
| 174 | |
| 175 | memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops)); |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 176 | |
| 177 | fe->tuner_priv = priv; |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 178 | |
| 179 | return fe; |
| 180 | } |
| 181 | EXPORT_SYMBOL(tda826x_attach); |
| 182 | |
Andrew de Quincey | 8b9a466 | 2006-08-08 09:10:11 -0300 | [diff] [blame] | 183 | module_param(debug, int, 0644); |
| 184 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 185 | |
Andrew de Quincey | 6bca3580 | 2006-08-08 09:10:10 -0300 | [diff] [blame] | 186 | MODULE_DESCRIPTION("DVB TDA826x driver"); |
| 187 | MODULE_AUTHOR("Andrew de Quincey"); |
| 188 | MODULE_LICENSE("GPL"); |