Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mauro Carvalho Chehab | cba862d | 2017-11-29 08:33:45 -0500 | [diff] [blame] | 2 | /* |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 3 | * Driver for Infineon tua6100 pll. |
| 4 | * |
| 5 | * (c) 2006 Andrew de Quincey |
| 6 | * |
| 7 | * Based on code found in budget-av.c, which has the following: |
| 8 | * Compiled from various sources by Michael Hunold <michael@mihu.de> |
| 9 | * |
| 10 | * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> & |
| 11 | * Andrew de Quincey <adq_dvb@lidskialf.net> |
| 12 | * |
| 13 | * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de> |
| 14 | * |
| 15 | * Copyright (C) 1999-2002 Ralph Metzler |
| 16 | * & Marcus Metzler for convergence integrated media GmbH |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 17 | */ |
| 18 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/dvb/frontend.h> |
| 22 | #include <asm/types.h> |
| 23 | |
| 24 | #include "tua6100.h" |
| 25 | |
| 26 | struct tua6100_priv { |
| 27 | /* i2c details */ |
| 28 | int i2c_address; |
| 29 | struct i2c_adapter *i2c; |
| 30 | u32 frequency; |
| 31 | }; |
| 32 | |
Mauro Carvalho Chehab | f2709c2 | 2016-11-18 20:30:51 -0200 | [diff] [blame] | 33 | static void tua6100_release(struct dvb_frontend *fe) |
| 34 | { |
| 35 | kfree(fe->tuner_priv); |
| 36 | fe->tuner_priv = NULL; |
| 37 | } |
| 38 | |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 39 | static int tua6100_sleep(struct dvb_frontend *fe) |
| 40 | { |
| 41 | struct tua6100_priv *priv = fe->tuner_priv; |
| 42 | int ret; |
| 43 | u8 reg0[] = { 0x00, 0x00 }; |
| 44 | struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 }; |
| 45 | |
| 46 | if (fe->ops.i2c_gate_ctrl) |
| 47 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 48 | if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) { |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 49 | printk("%s: i2c error\n", __func__); |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 50 | } |
| 51 | if (fe->ops.i2c_gate_ctrl) |
| 52 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 53 | |
| 54 | return (ret == 1) ? 0 : ret; |
| 55 | } |
| 56 | |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 57 | static int tua6100_set_params(struct dvb_frontend *fe) |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 58 | { |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 59 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 60 | struct tua6100_priv *priv = fe->tuner_priv; |
| 61 | u32 div; |
| 62 | u32 prediv; |
| 63 | u8 reg0[] = { 0x00, 0x00 }; |
| 64 | u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 }; |
| 65 | u8 reg2[] = { 0x02, 0x00, 0x00 }; |
| 66 | struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 }; |
| 67 | struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 }; |
| 68 | struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 }; |
| 69 | |
| 70 | #define _R 4 |
| 71 | #define _P 32 |
| 72 | #define _ri 4000000 |
| 73 | |
| 74 | // setup register 0 |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 75 | if (c->frequency < 2000000) |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 76 | reg0[1] = 0x03; |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 77 | else |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 78 | reg0[1] = 0x07; |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 79 | |
| 80 | // setup register 1 |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 81 | if (c->frequency < 1630000) |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 82 | reg1[1] = 0x2c; |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 83 | else |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 84 | reg1[1] = 0x0c; |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 85 | |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 86 | if (_P == 64) |
| 87 | reg1[1] |= 0x40; |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 88 | if (c->frequency >= 1525000) |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 89 | reg1[1] |= 0x80; |
| 90 | |
| 91 | // register 2 |
| 92 | reg2[1] = (_R >> 8) & 0x03; |
| 93 | reg2[2] = _R; |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 94 | if (c->frequency < 1455000) |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 95 | reg2[1] |= 0x1c; |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 96 | else if (c->frequency < 1630000) |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 97 | reg2[1] |= 0x0c; |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 98 | else |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 99 | reg2[1] |= 0x1c; |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 100 | |
Mauro Carvalho Chehab | 5918288 | 2011-12-23 17:56:57 -0300 | [diff] [blame] | 101 | /* |
| 102 | * The N divisor ratio (note: c->frequency is in kHz, but we |
| 103 | * need it in Hz) |
| 104 | */ |
| 105 | prediv = (c->frequency * _R) / (_ri / 1000); |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 106 | div = prediv / _P; |
| 107 | reg1[1] |= (div >> 9) & 0x03; |
| 108 | reg1[2] = div >> 1; |
| 109 | reg1[3] = (div << 7); |
| 110 | priv->frequency = ((div * _P) * (_ri / 1000)) / _R; |
| 111 | |
| 112 | // Finally, calculate and store the value for A |
| 113 | reg1[3] |= (prediv - (div*_P)) & 0x7f; |
| 114 | |
| 115 | #undef _R |
| 116 | #undef _P |
| 117 | #undef _ri |
| 118 | |
| 119 | if (fe->ops.i2c_gate_ctrl) |
| 120 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 121 | if (i2c_transfer(priv->i2c, &msg0, 1) != 1) |
| 122 | return -EIO; |
| 123 | |
| 124 | if (fe->ops.i2c_gate_ctrl) |
| 125 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 126 | if (i2c_transfer(priv->i2c, &msg2, 1) != 1) |
| 127 | return -EIO; |
| 128 | |
| 129 | if (fe->ops.i2c_gate_ctrl) |
| 130 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 131 | if (i2c_transfer(priv->i2c, &msg1, 1) != 1) |
| 132 | return -EIO; |
| 133 | |
| 134 | if (fe->ops.i2c_gate_ctrl) |
| 135 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 141 | { |
| 142 | struct tua6100_priv *priv = fe->tuner_priv; |
| 143 | *frequency = priv->frequency; |
| 144 | return 0; |
| 145 | } |
| 146 | |
Julia Lawall | 14c4bf3 | 2016-09-11 11:44:12 -0300 | [diff] [blame] | 147 | static const struct dvb_tuner_ops tua6100_tuner_ops = { |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 148 | .info = { |
| 149 | .name = "Infineon TUA6100", |
Mauro Carvalho Chehab | a3f90c7 | 2018-07-05 18:59:35 -0400 | [diff] [blame] | 150 | .frequency_min_hz = 950 * MHz, |
| 151 | .frequency_max_hz = 2150 * MHz, |
| 152 | .frequency_step_hz = 1 * MHz, |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 153 | }, |
Mauro Carvalho Chehab | f2709c2 | 2016-11-18 20:30:51 -0200 | [diff] [blame] | 154 | .release = tua6100_release, |
Andrew de Quincey | 00be2e7 | 2006-09-05 17:53:06 -0300 | [diff] [blame] | 155 | .sleep = tua6100_sleep, |
| 156 | .set_params = tua6100_set_params, |
| 157 | .get_frequency = tua6100_get_frequency, |
| 158 | }; |
| 159 | |
| 160 | struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c) |
| 161 | { |
| 162 | struct tua6100_priv *priv = NULL; |
| 163 | u8 b1 [] = { 0x80 }; |
| 164 | u8 b2 [] = { 0x00 }; |
| 165 | struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 }, |
| 166 | { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } }; |
| 167 | int ret; |
| 168 | |
| 169 | if (fe->ops.i2c_gate_ctrl) |
| 170 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 171 | ret = i2c_transfer (i2c, msg, 2); |
| 172 | if (fe->ops.i2c_gate_ctrl) |
| 173 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 174 | |
| 175 | if (ret != 2) |
| 176 | return NULL; |
| 177 | |
| 178 | priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL); |
| 179 | if (priv == NULL) |
| 180 | return NULL; |
| 181 | |
| 182 | priv->i2c_address = addr; |
| 183 | priv->i2c = i2c; |
| 184 | |
| 185 | memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops)); |
| 186 | fe->tuner_priv = priv; |
| 187 | return fe; |
| 188 | } |
| 189 | EXPORT_SYMBOL(tua6100_attach); |
| 190 | |
| 191 | MODULE_DESCRIPTION("DVB tua6100 driver"); |
| 192 | MODULE_AUTHOR("Andrew de Quincey"); |
| 193 | MODULE_LICENSE("GPL"); |