| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 1 | /* | 
|  | 2 | TDA8261 8PSK/QPSK tuner driver | 
|  | 3 | Copyright (C) Manu Abraham (abraham.manu@gmail.com) | 
|  | 4 |  | 
|  | 5 | This program is free software; you can redistribute it and/or modify | 
|  | 6 | it under the terms of the GNU General Public License as published by | 
|  | 7 | the Free Software Foundation; either version 2 of the License, or | 
|  | 8 | (at your option) any later version. | 
|  | 9 |  | 
|  | 10 | This program is distributed in the hope that it will be useful, | 
|  | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 13 | GNU General Public License for more details. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU General Public License | 
|  | 16 | along with this program; if not, write to the Free Software | 
|  | 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 18 | */ | 
|  | 19 |  | 
|  | 20 |  | 
|  | 21 | #include <linux/init.h> | 
|  | 22 | #include <linux/kernel.h> | 
|  | 23 | #include <linux/module.h> | 
|  | 24 |  | 
|  | 25 | #include "dvb_frontend.h" | 
|  | 26 | #include "tda8261.h" | 
|  | 27 |  | 
|  | 28 | struct tda8261_state { | 
| Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 29 | struct dvb_frontend		*fe; | 
|  | 30 | struct i2c_adapter		*i2c; | 
|  | 31 | const struct tda8261_config	*config; | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 32 |  | 
|  | 33 | /* state cache */ | 
|  | 34 | u32 frequency; | 
|  | 35 | u32 bandwidth; | 
|  | 36 | }; | 
|  | 37 |  | 
|  | 38 | static int tda8261_read(struct tda8261_state *state, u8 *buf) | 
|  | 39 | { | 
| Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 40 | const struct tda8261_config *config = state->config; | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 41 | int err = 0; | 
| Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 42 | struct i2c_msg msg = { .addr	= config->addr, .flags = I2C_M_RD,.buf = buf,  .len = 2 }; | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 43 |  | 
| Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 44 | if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 45 | printk("%s: read error, err=%d\n", __func__, err); | 
|  | 46 |  | 
|  | 47 | return err; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | static int tda8261_write(struct tda8261_state *state, u8 *buf) | 
|  | 51 | { | 
| Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 52 | const struct tda8261_config *config = state->config; | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 53 | int err = 0; | 
|  | 54 | struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 }; | 
|  | 55 |  | 
|  | 56 | if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) | 
| Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 57 | printk("%s: write error, err=%d\n", __func__, err); | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 58 |  | 
|  | 59 | return err; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | static int tda8261_get_status(struct dvb_frontend *fe, u32 *status) | 
|  | 63 | { | 
|  | 64 | struct tda8261_state *state = fe->tuner_priv; | 
|  | 65 | u8 result = 0; | 
|  | 66 | int err = 0; | 
|  | 67 |  | 
| Manu Abraham | f6e6382 | 2007-09-28 19:06:06 -0300 | [diff] [blame] | 68 | *status = 0; | 
|  | 69 |  | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 70 | if ((err = tda8261_read(state, &result)) < 0) { | 
|  | 71 | printk("%s: I/O Error\n", __func__); | 
|  | 72 | return err; | 
|  | 73 | } | 
|  | 74 | if ((result >> 6) & 0x01) { | 
|  | 75 | printk("%s: Tuner Phase Locked\n", __func__); | 
|  | 76 | *status = 1; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | return err; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | static const u32 div_tab[] = { 2000, 1000,  500,  250,  125 }; /* kHz */ | 
|  | 83 | static const u8  ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 }; | 
|  | 84 |  | 
|  | 85 | static int tda8261_get_state(struct dvb_frontend *fe, | 
|  | 86 | enum tuner_param param, | 
|  | 87 | struct tuner_state *tstate) | 
|  | 88 | { | 
|  | 89 | struct tda8261_state *state = fe->tuner_priv; | 
|  | 90 | int err = 0; | 
|  | 91 |  | 
|  | 92 | switch (param) { | 
|  | 93 | case DVBFE_TUNER_FREQUENCY: | 
|  | 94 | tstate->frequency = state->frequency; | 
|  | 95 | break; | 
|  | 96 | case DVBFE_TUNER_BANDWIDTH: | 
| Arvo Jarve | 1e3d8ab | 2007-10-30 10:21:33 -0300 | [diff] [blame] | 97 | tstate->bandwidth = 40000000; /* FIXME! need to calculate Bandwidth */ | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 98 | break; | 
|  | 99 | default: | 
|  | 100 | printk("%s: Unknown parameter (param=%d)\n", __func__, param); | 
|  | 101 | err = -EINVAL; | 
|  | 102 | break; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | return err; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | static int tda8261_set_state(struct dvb_frontend *fe, | 
|  | 109 | enum tuner_param param, | 
|  | 110 | struct tuner_state *tstate) | 
|  | 111 | { | 
|  | 112 | struct tda8261_state *state = fe->tuner_priv; | 
| Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 113 | const struct tda8261_config *config = state->config; | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 114 | u32 frequency, N, status = 0; | 
|  | 115 | u8 buf[4]; | 
|  | 116 | int err = 0; | 
|  | 117 |  | 
|  | 118 | if (param & DVBFE_TUNER_FREQUENCY) { | 
|  | 119 | /** | 
|  | 120 | * N = Max VCO Frequency / Channel Spacing | 
|  | 121 | * Max VCO Frequency = VCO frequency + (channel spacing - 1) | 
|  | 122 | * (to account for half channel spacing on either side) | 
|  | 123 | */ | 
|  | 124 | frequency = tstate->frequency; | 
| Manu Abraham | cf6efeb | 2007-10-08 19:04:03 -0300 | [diff] [blame] | 125 | if ((frequency < 950000) || (frequency > 2150000)) { | 
|  | 126 | printk("%s: Frequency beyond limits, frequency=%d\n", __func__, frequency); | 
|  | 127 | return -EINVAL; | 
|  | 128 | } | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 129 | N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size]; | 
| Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 130 | printk("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n", | 
|  | 131 | __func__, config->step_size, div_tab[config->step_size], N, N); | 
|  | 132 |  | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 133 | buf[0] = (N >> 8) & 0xff; | 
|  | 134 | buf[1] = N & 0xff; | 
| Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 135 | buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1); | 
| Manu Abraham | cf6efeb | 2007-10-08 19:04:03 -0300 | [diff] [blame] | 136 |  | 
|  | 137 | if (frequency < 1450000) | 
|  | 138 | buf[3] = 0x00; | 
|  | 139 | if (frequency < 2000000) | 
|  | 140 | buf[3] = 0x40; | 
|  | 141 | if (frequency < 2150000) | 
|  | 142 | buf[3] = 0x80; | 
|  | 143 |  | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 144 | /* Set params */ | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 145 | if ((err = tda8261_write(state, buf)) < 0) { | 
|  | 146 | printk("%s: I/O Error\n", __func__); | 
|  | 147 | return err; | 
|  | 148 | } | 
|  | 149 | /* sleep for some time */ | 
| Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 150 | printk("%s: Waiting to Phase LOCK\n", __func__); | 
|  | 151 | msleep(20); | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 152 | /* check status */ | 
|  | 153 | if ((err = tda8261_get_status(fe, &status)) < 0) { | 
|  | 154 | printk("%s: I/O Error\n", __func__); | 
|  | 155 | return err; | 
|  | 156 | } | 
| Manu Abraham | 0b8f15d | 2007-09-22 13:36:34 -0300 | [diff] [blame] | 157 | if (status == 1) { | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 158 | printk("%s: Tuner Phase locked: status=%d\n", __func__, status); | 
| Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 159 | state->frequency = frequency; /* cache successful state */ | 
| Manu Abraham | 0b8f15d | 2007-09-22 13:36:34 -0300 | [diff] [blame] | 160 | } else { | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 161 | printk("%s: No Phase lock: status=%d\n", __func__, status); | 
| Manu Abraham | 0b8f15d | 2007-09-22 13:36:34 -0300 | [diff] [blame] | 162 | } | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 163 | } else { | 
|  | 164 | printk("%s: Unknown parameter (param=%d)\n", __func__, param); | 
|  | 165 | return -EINVAL; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | return 0; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | static int tda8261_release(struct dvb_frontend *fe) | 
|  | 172 | { | 
|  | 173 | struct tda8261_state *state = fe->tuner_priv; | 
|  | 174 |  | 
|  | 175 | fe->tuner_priv = NULL; | 
|  | 176 | kfree(state); | 
|  | 177 | return 0; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | static struct dvb_tuner_ops tda8261_ops = { | 
|  | 181 |  | 
|  | 182 | .info = { | 
|  | 183 | .name		= "TDA8261", | 
|  | 184 | //		.tuner_name	= NULL, | 
|  | 185 | .frequency_min	=  950000, | 
|  | 186 | .frequency_max	= 2150000, | 
|  | 187 | .frequency_step = 0 | 
|  | 188 | }, | 
|  | 189 |  | 
|  | 190 | .set_state	= tda8261_set_state, | 
|  | 191 | .get_state	= tda8261_get_state, | 
| Manu Abraham | f6e6382 | 2007-09-28 19:06:06 -0300 | [diff] [blame] | 192 | .get_status	= tda8261_get_status, | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 193 | .release	= tda8261_release | 
|  | 194 | }; | 
|  | 195 |  | 
|  | 196 | struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe, | 
| Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 197 | const struct tda8261_config *config, | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 198 | struct i2c_adapter *i2c) | 
|  | 199 | { | 
|  | 200 | struct tda8261_state *state = NULL; | 
|  | 201 |  | 
|  | 202 | if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL) | 
|  | 203 | goto exit; | 
|  | 204 |  | 
|  | 205 | state->config		= config; | 
|  | 206 | state->i2c		= i2c; | 
|  | 207 | state->fe		= fe; | 
|  | 208 | fe->tuner_priv		= state; | 
|  | 209 | fe->ops.tuner_ops	= tda8261_ops; | 
|  | 210 |  | 
|  | 211 | fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size]; | 
|  | 212 | //	fe->ops.tuner_ops.tuner_name	 = &config->buf; | 
|  | 213 |  | 
|  | 214 | //	printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n", | 
|  | 215 | //		__func__, fe->ops.tuner_ops.tuner_name); | 
|  | 216 | printk("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__); | 
|  | 217 |  | 
| Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 218 | return fe; | 
|  | 219 |  | 
|  | 220 | exit: | 
|  | 221 | kfree(state); | 
|  | 222 | return NULL; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | EXPORT_SYMBOL(tda8261_attach); | 
|  | 226 | MODULE_PARM_DESC(verbose, "Set verbosity level"); | 
|  | 227 |  | 
|  | 228 | MODULE_AUTHOR("Manu Abraham"); | 
|  | 229 | MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner"); | 
|  | 230 | MODULE_LICENSE("GPL"); |