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