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 | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 40 | struct dvb_frontend *fe = state->fe; |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 41 | const struct tda8261_config *config = state->config; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 42 | int err = 0; |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 43 | 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] | 44 | |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 45 | if (fe->ops.i2c_gate_ctrl) |
| 46 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 47 | |
| 48 | if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 49 | printk("%s: read error, err=%d\n", __func__, err); |
| 50 | |
| 51 | return err; |
| 52 | } |
| 53 | |
| 54 | static int tda8261_write(struct tda8261_state *state, u8 *buf) |
| 55 | { |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 56 | struct dvb_frontend *fe = state->fe; |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 57 | const struct tda8261_config *config = state->config; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 58 | int err = 0; |
| 59 | struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 }; |
| 60 | |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 61 | if (fe->ops.i2c_gate_ctrl) |
| 62 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 63 | |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 64 | if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 65 | printk("%s: write error, err=%d\n", __func__, err); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 66 | |
| 67 | return err; |
| 68 | } |
| 69 | |
| 70 | static int tda8261_get_status(struct dvb_frontend *fe, u32 *status) |
| 71 | { |
| 72 | struct tda8261_state *state = fe->tuner_priv; |
| 73 | u8 result = 0; |
| 74 | int err = 0; |
| 75 | |
| 76 | if ((err = tda8261_read(state, &result)) < 0) { |
| 77 | printk("%s: I/O Error\n", __func__); |
| 78 | return err; |
| 79 | } |
| 80 | if ((result >> 6) & 0x01) { |
| 81 | printk("%s: Tuner Phase Locked\n", __func__); |
| 82 | *status = 1; |
| 83 | } |
| 84 | |
| 85 | return err; |
| 86 | } |
| 87 | |
| 88 | static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */ |
| 89 | static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 }; |
| 90 | |
| 91 | static int tda8261_get_state(struct dvb_frontend *fe, |
| 92 | enum tuner_param param, |
| 93 | struct tuner_state *tstate) |
| 94 | { |
| 95 | struct tda8261_state *state = fe->tuner_priv; |
| 96 | int err = 0; |
| 97 | |
| 98 | switch (param) { |
| 99 | case DVBFE_TUNER_FREQUENCY: |
| 100 | tstate->frequency = state->frequency; |
| 101 | break; |
| 102 | case DVBFE_TUNER_BANDWIDTH: |
| 103 | tstate->bandwidth = 60000000; /* FIXME! need to calculate Bandwidth */ |
| 104 | break; |
| 105 | default: |
| 106 | printk("%s: Unknown parameter (param=%d)\n", __func__, param); |
| 107 | err = -EINVAL; |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | return err; |
| 112 | } |
| 113 | |
| 114 | static int tda8261_set_state(struct dvb_frontend *fe, |
| 115 | enum tuner_param param, |
| 116 | struct tuner_state *tstate) |
| 117 | { |
| 118 | struct tda8261_state *state = fe->tuner_priv; |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 119 | const struct tda8261_config *config = state->config; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 120 | u32 frequency, N, status = 0; |
| 121 | u8 buf[4]; |
| 122 | int err = 0; |
| 123 | |
| 124 | if (param & DVBFE_TUNER_FREQUENCY) { |
| 125 | /** |
| 126 | * N = Max VCO Frequency / Channel Spacing |
| 127 | * Max VCO Frequency = VCO frequency + (channel spacing - 1) |
| 128 | * (to account for half channel spacing on either side) |
| 129 | */ |
| 130 | frequency = tstate->frequency; |
| 131 | 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^] | 132 | printk("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n", |
| 133 | __func__, config->step_size, div_tab[config->step_size], N, N); |
| 134 | |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 135 | buf[0] = (N >> 8) & 0xff; |
| 136 | buf[1] = N & 0xff; |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 137 | buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 138 | buf[3] = 0xc0; |
| 139 | /* Set params */ |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 140 | if ((err = tda8261_write(state, buf)) < 0) { |
| 141 | printk("%s: I/O Error\n", __func__); |
| 142 | return err; |
| 143 | } |
| 144 | /* sleep for some time */ |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 145 | printk("%s: Waiting to Phase LOCK\n", __func__); |
| 146 | msleep(20); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 147 | /* check status */ |
| 148 | if ((err = tda8261_get_status(fe, &status)) < 0) { |
| 149 | printk("%s: I/O Error\n", __func__); |
| 150 | return err; |
| 151 | } |
Manu Abraham | 0b8f15d | 2007-09-22 13:36:34 -0300 | [diff] [blame] | 152 | if (status == 1) { |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 153 | printk("%s: Tuner Phase locked: status=%d\n", __func__, status); |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame^] | 154 | state->frequency = frequency; /* cache successful state */ |
Manu Abraham | 0b8f15d | 2007-09-22 13:36:34 -0300 | [diff] [blame] | 155 | } else { |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 156 | printk("%s: No Phase lock: status=%d\n", __func__, status); |
Manu Abraham | 0b8f15d | 2007-09-22 13:36:34 -0300 | [diff] [blame] | 157 | } |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 158 | } else { |
| 159 | printk("%s: Unknown parameter (param=%d)\n", __func__, param); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int tda8261_release(struct dvb_frontend *fe) |
| 167 | { |
| 168 | struct tda8261_state *state = fe->tuner_priv; |
| 169 | |
| 170 | fe->tuner_priv = NULL; |
| 171 | kfree(state); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static struct dvb_tuner_ops tda8261_ops = { |
| 176 | |
| 177 | .info = { |
| 178 | .name = "TDA8261", |
| 179 | // .tuner_name = NULL, |
| 180 | .frequency_min = 950000, |
| 181 | .frequency_max = 2150000, |
| 182 | .frequency_step = 0 |
| 183 | }, |
| 184 | |
| 185 | .set_state = tda8261_set_state, |
| 186 | .get_state = tda8261_get_state, |
| 187 | .release = tda8261_release |
| 188 | }; |
| 189 | |
| 190 | struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe, |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 191 | const struct tda8261_config *config, |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 192 | struct i2c_adapter *i2c) |
| 193 | { |
| 194 | struct tda8261_state *state = NULL; |
| 195 | |
| 196 | if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL) |
| 197 | goto exit; |
| 198 | |
| 199 | state->config = config; |
| 200 | state->i2c = i2c; |
| 201 | state->fe = fe; |
| 202 | fe->tuner_priv = state; |
| 203 | fe->ops.tuner_ops = tda8261_ops; |
| 204 | |
| 205 | fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size]; |
| 206 | // fe->ops.tuner_ops.tuner_name = &config->buf; |
| 207 | |
| 208 | // printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n", |
| 209 | // __func__, fe->ops.tuner_ops.tuner_name); |
| 210 | printk("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__); |
| 211 | |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 212 | return fe; |
| 213 | |
| 214 | exit: |
| 215 | kfree(state); |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | EXPORT_SYMBOL(tda8261_attach); |
| 220 | MODULE_PARM_DESC(verbose, "Set verbosity level"); |
| 221 | |
| 222 | MODULE_AUTHOR("Manu Abraham"); |
| 223 | MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner"); |
| 224 | MODULE_LICENSE("GPL"); |