Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     TDA8261 8PSK/QPSK tuner driver
0004     Copyright (C) Manu Abraham (abraham.manu@gmail.com)
0005 
0006 */
0007 
0008 
0009 #include <linux/init.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 
0014 #include <media/dvb_frontend.h>
0015 #include "tda8261.h"
0016 
0017 struct tda8261_state {
0018     struct dvb_frontend     *fe;
0019     struct i2c_adapter      *i2c;
0020     const struct tda8261_config *config;
0021 
0022     /* state cache */
0023     u32 frequency;
0024     u32 bandwidth;
0025 };
0026 
0027 static int tda8261_read(struct tda8261_state *state, u8 *buf)
0028 {
0029     const struct tda8261_config *config = state->config;
0030     int err = 0;
0031     struct i2c_msg msg = { .addr    = config->addr, .flags = I2C_M_RD,.buf = buf,  .len = 1 };
0032 
0033     if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
0034         pr_err("%s: read error, err=%d\n", __func__, err);
0035 
0036     return err;
0037 }
0038 
0039 static int tda8261_write(struct tda8261_state *state, u8 *buf)
0040 {
0041     const struct tda8261_config *config = state->config;
0042     int err = 0;
0043     struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 };
0044 
0045     if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
0046         pr_err("%s: write error, err=%d\n", __func__, err);
0047 
0048     return err;
0049 }
0050 
0051 static int tda8261_get_status(struct dvb_frontend *fe, u32 *status)
0052 {
0053     struct tda8261_state *state = fe->tuner_priv;
0054     u8 result = 0;
0055     int err = 0;
0056 
0057     *status = 0;
0058 
0059     if ((err = tda8261_read(state, &result)) < 0) {
0060         pr_err("%s: I/O Error\n", __func__);
0061         return err;
0062     }
0063     if ((result >> 6) & 0x01) {
0064         pr_debug("%s: Tuner Phase Locked\n", __func__);
0065         *status = 1;
0066     }
0067 
0068     return err;
0069 }
0070 
0071 static const u32 div_tab[] = { 2000, 1000,  500,  250,  125 }; /* kHz */
0072 static const u8  ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 };
0073 
0074 static int tda8261_get_frequency(struct dvb_frontend *fe, u32 *frequency)
0075 {
0076     struct tda8261_state *state = fe->tuner_priv;
0077 
0078     *frequency = state->frequency;
0079 
0080     return 0;
0081 }
0082 
0083 static int tda8261_set_params(struct dvb_frontend *fe)
0084 {
0085     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0086     struct tda8261_state *state = fe->tuner_priv;
0087     const struct tda8261_config *config = state->config;
0088     u32 frequency, N, status = 0;
0089     u8 buf[4];
0090     int err = 0;
0091 
0092     /*
0093      * N = Max VCO Frequency / Channel Spacing
0094      * Max VCO Frequency = VCO frequency + (channel spacing - 1)
0095      * (to account for half channel spacing on either side)
0096      */
0097     frequency = c->frequency;
0098     if ((frequency < 950000) || (frequency > 2150000)) {
0099         pr_warn("%s: Frequency beyond limits, frequency=%d\n",
0100             __func__, frequency);
0101         return -EINVAL;
0102     }
0103     N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
0104     pr_debug("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
0105         __func__, config->step_size, div_tab[config->step_size], N, N);
0106 
0107     buf[0] = (N >> 8) & 0xff;
0108     buf[1] = N & 0xff;
0109     buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
0110 
0111     if (frequency < 1450000)
0112         buf[3] = 0x00;
0113     else if (frequency < 2000000)
0114         buf[3] = 0x40;
0115     else if (frequency < 2150000)
0116         buf[3] = 0x80;
0117 
0118     /* Set params */
0119     err = tda8261_write(state, buf);
0120     if (err < 0) {
0121         pr_err("%s: I/O Error\n", __func__);
0122         return err;
0123     }
0124     /* sleep for some time */
0125     pr_debug("%s: Waiting to Phase LOCK\n", __func__);
0126     msleep(20);
0127     /* check status */
0128     if ((err = tda8261_get_status(fe, &status)) < 0) {
0129         pr_err("%s: I/O Error\n", __func__);
0130         return err;
0131     }
0132     if (status == 1) {
0133         pr_debug("%s: Tuner Phase locked: status=%d\n", __func__,
0134              status);
0135         state->frequency = frequency; /* cache successful state */
0136     } else {
0137         pr_debug("%s: No Phase lock: status=%d\n", __func__, status);
0138     }
0139 
0140     return 0;
0141 }
0142 
0143 static void tda8261_release(struct dvb_frontend *fe)
0144 {
0145     struct tda8261_state *state = fe->tuner_priv;
0146 
0147     fe->tuner_priv = NULL;
0148     kfree(state);
0149 }
0150 
0151 static const struct dvb_tuner_ops tda8261_ops = {
0152 
0153     .info = {
0154         .name          = "TDA8261",
0155         .frequency_min_hz  =  950 * MHz,
0156         .frequency_max_hz  = 2150 * MHz,
0157     },
0158 
0159     .set_params = tda8261_set_params,
0160     .get_frequency  = tda8261_get_frequency,
0161     .get_status = tda8261_get_status,
0162     .release    = tda8261_release
0163 };
0164 
0165 struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
0166                     const struct tda8261_config *config,
0167                     struct i2c_adapter *i2c)
0168 {
0169     struct tda8261_state *state = NULL;
0170 
0171     if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
0172         goto exit;
0173 
0174     state->config       = config;
0175     state->i2c      = i2c;
0176     state->fe       = fe;
0177     fe->tuner_priv      = state;
0178     fe->ops.tuner_ops   = tda8261_ops;
0179 
0180     fe->ops.tuner_ops.info.frequency_step_hz = div_tab[config->step_size] * kHz;
0181 
0182     pr_info("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
0183 
0184     return fe;
0185 
0186 exit:
0187     kfree(state);
0188     return NULL;
0189 }
0190 
0191 EXPORT_SYMBOL(tda8261_attach);
0192 
0193 MODULE_AUTHOR("Manu Abraham");
0194 MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
0195 MODULE_LICENSE("GPL");