Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 2 | /* |
| 3 | TDA8261 8PSK/QPSK tuner driver |
| 4 | Copyright (C) Manu Abraham (abraham.manu@gmail.com) |
| 5 | |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 13 | |
Mauro Carvalho Chehab | fada193 | 2017-12-28 13:03:51 -0500 | [diff] [blame] | 14 | #include <media/dvb_frontend.h> |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 15 | #include "tda8261.h" |
| 16 | |
| 17 | struct tda8261_state { |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 18 | struct dvb_frontend *fe; |
| 19 | struct i2c_adapter *i2c; |
| 20 | const struct tda8261_config *config; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 21 | |
| 22 | /* state cache */ |
| 23 | u32 frequency; |
| 24 | u32 bandwidth; |
| 25 | }; |
| 26 | |
| 27 | static int tda8261_read(struct tda8261_state *state, u8 *buf) |
| 28 | { |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 29 | const struct tda8261_config *config = state->config; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 30 | int err = 0; |
Arvo Järve | 225ee0c | 2010-01-10 17:15:57 -0300 | [diff] [blame] | 31 | 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] | 32 | |
Manu Abraham | c7d85a2 | 2007-09-24 13:27:06 -0300 | [diff] [blame] | 33 | if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) |
Alan Cox | eb2e265 | 2012-09-02 03:30:15 -0300 | [diff] [blame] | 34 | pr_err("%s: read error, err=%d\n", __func__, err); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 35 | |
| 36 | return err; |
| 37 | } |
| 38 | |
| 39 | static int tda8261_write(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; |
| 43 | struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 }; |
| 44 | |
| 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: write 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_get_status(struct dvb_frontend *fe, u32 *status) |
| 52 | { |
| 53 | struct tda8261_state *state = fe->tuner_priv; |
| 54 | u8 result = 0; |
| 55 | int err = 0; |
| 56 | |
Manu Abraham | f6e6382 | 2007-09-28 19:06:06 -0300 | [diff] [blame] | 57 | *status = 0; |
| 58 | |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 59 | if ((err = tda8261_read(state, &result)) < 0) { |
Alan Cox | eb2e265 | 2012-09-02 03:30:15 -0300 | [diff] [blame] | 60 | pr_err("%s: I/O Error\n", __func__); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 61 | return err; |
| 62 | } |
| 63 | if ((result >> 6) & 0x01) { |
Alan Cox | eb2e265 | 2012-09-02 03:30:15 -0300 | [diff] [blame] | 64 | pr_debug("%s: Tuner Phase Locked\n", __func__); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 65 | *status = 1; |
| 66 | } |
| 67 | |
| 68 | return err; |
| 69 | } |
| 70 | |
| 71 | static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */ |
| 72 | static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 }; |
| 73 | |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 74 | static int tda8261_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 75 | { |
| 76 | struct tda8261_state *state = fe->tuner_priv; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 77 | |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 78 | *frequency = state->frequency; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 79 | |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 80 | return 0; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 81 | } |
| 82 | |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 83 | static int tda8261_set_params(struct dvb_frontend *fe) |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 84 | { |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 85 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 86 | struct tda8261_state *state = fe->tuner_priv; |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 87 | const struct tda8261_config *config = state->config; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 88 | u32 frequency, N, status = 0; |
| 89 | u8 buf[4]; |
| 90 | int err = 0; |
| 91 | |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 92 | /* |
| 93 | * N = Max VCO Frequency / Channel Spacing |
| 94 | * Max VCO Frequency = VCO frequency + (channel spacing - 1) |
| 95 | * (to account for half channel spacing on either side) |
| 96 | */ |
| 97 | frequency = c->frequency; |
| 98 | if ((frequency < 950000) || (frequency > 2150000)) { |
| 99 | pr_warn("%s: Frequency beyond limits, frequency=%d\n", |
| 100 | __func__, frequency); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 101 | return -EINVAL; |
| 102 | } |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 103 | N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size]; |
| 104 | pr_debug("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n", |
| 105 | __func__, config->step_size, div_tab[config->step_size], N, N); |
| 106 | |
| 107 | buf[0] = (N >> 8) & 0xff; |
| 108 | buf[1] = N & 0xff; |
| 109 | buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1); |
| 110 | |
| 111 | if (frequency < 1450000) |
| 112 | buf[3] = 0x00; |
| 113 | else if (frequency < 2000000) |
| 114 | buf[3] = 0x40; |
| 115 | else if (frequency < 2150000) |
| 116 | buf[3] = 0x80; |
| 117 | |
| 118 | /* Set params */ |
| 119 | err = tda8261_write(state, buf); |
| 120 | if (err < 0) { |
| 121 | pr_err("%s: I/O Error\n", __func__); |
| 122 | return err; |
| 123 | } |
| 124 | /* sleep for some time */ |
| 125 | pr_debug("%s: Waiting to Phase LOCK\n", __func__); |
| 126 | msleep(20); |
| 127 | /* check status */ |
| 128 | if ((err = tda8261_get_status(fe, &status)) < 0) { |
| 129 | pr_err("%s: I/O Error\n", __func__); |
| 130 | return err; |
| 131 | } |
| 132 | if (status == 1) { |
| 133 | pr_debug("%s: Tuner Phase locked: status=%d\n", __func__, |
| 134 | status); |
| 135 | state->frequency = frequency; /* cache successful state */ |
| 136 | } else { |
| 137 | pr_debug("%s: No Phase lock: status=%d\n", __func__, status); |
| 138 | } |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Mauro Carvalho Chehab | f2709c2 | 2016-11-18 20:30:51 -0200 | [diff] [blame] | 143 | static void tda8261_release(struct dvb_frontend *fe) |
| 144 | { |
| 145 | struct tda8261_state *state = fe->tuner_priv; |
| 146 | |
| 147 | fe->tuner_priv = NULL; |
| 148 | kfree(state); |
| 149 | } |
| 150 | |
Julia Lawall | 14c4bf3 | 2016-09-11 11:44:12 -0300 | [diff] [blame] | 151 | static const struct dvb_tuner_ops tda8261_ops = { |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 152 | |
| 153 | .info = { |
Mauro Carvalho Chehab | a3f90c7 | 2018-07-05 18:59:35 -0400 | [diff] [blame] | 154 | .name = "TDA8261", |
| 155 | .frequency_min_hz = 950 * MHz, |
| 156 | .frequency_max_hz = 2150 * MHz, |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 157 | }, |
| 158 | |
Mauro Carvalho Chehab | e417668 | 2015-11-11 18:07:44 -0200 | [diff] [blame] | 159 | .set_params = tda8261_set_params, |
| 160 | .get_frequency = tda8261_get_frequency, |
Manu Abraham | f6e6382 | 2007-09-28 19:06:06 -0300 | [diff] [blame] | 161 | .get_status = tda8261_get_status, |
Mauro Carvalho Chehab | f2709c2 | 2016-11-18 20:30:51 -0200 | [diff] [blame] | 162 | .release = tda8261_release |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe, |
Manu Abraham | 41e1151 | 2007-09-22 21:28:11 -0300 | [diff] [blame] | 166 | const struct tda8261_config *config, |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 167 | struct i2c_adapter *i2c) |
| 168 | { |
| 169 | struct tda8261_state *state = NULL; |
| 170 | |
| 171 | if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL) |
| 172 | goto exit; |
| 173 | |
| 174 | state->config = config; |
| 175 | state->i2c = i2c; |
| 176 | state->fe = fe; |
| 177 | fe->tuner_priv = state; |
| 178 | fe->ops.tuner_ops = tda8261_ops; |
| 179 | |
Mauro Carvalho Chehab | a3f90c7 | 2018-07-05 18:59:35 -0400 | [diff] [blame] | 180 | fe->ops.tuner_ops.info.frequency_step_hz = div_tab[config->step_size] * kHz; |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 181 | |
Alan Cox | eb2e265 | 2012-09-02 03:30:15 -0300 | [diff] [blame] | 182 | pr_info("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 183 | |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 184 | return fe; |
| 185 | |
| 186 | exit: |
| 187 | kfree(state); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | EXPORT_SYMBOL(tda8261_attach); |
Manu Abraham | 0036020 | 2007-09-22 13:30:09 -0300 | [diff] [blame] | 192 | |
| 193 | MODULE_AUTHOR("Manu Abraham"); |
| 194 | MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner"); |
| 195 | MODULE_LICENSE("GPL"); |