blob: 9d70378fe2d31ebc373f75a30851cb74ab02ef4c [file] [log] [blame]
Antti Palosaarif9263742012-03-30 06:37:26 -03001/*
Antti Palosaari465433f2015-04-15 20:26:06 -03002 * Infineon TUA9001 silicon tuner driver
Antti Palosaarif9263742012-03-30 06:37:26 -03003 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Antti Palosaarif9263742012-03-30 06:37:26 -030015 */
16
Antti Palosaarif9263742012-03-30 06:37:26 -030017#include "tua9001_priv.h"
18
Antti Palosaarif9263742012-03-30 06:37:26 -030019static int tua9001_init(struct dvb_frontend *fe)
20{
Antti Palosaari465433f2015-04-15 20:26:06 -030021 struct tua9001_dev *dev = fe->tuner_priv;
22 struct i2c_client *client = dev->client;
23 int ret, i;
24 static const struct tua9001_reg_val data[] = {
25 {0x1e, 0x6512},
26 {0x25, 0xb888},
27 {0x39, 0x5460},
28 {0x3b, 0x00c0},
29 {0x3a, 0xf000},
30 {0x08, 0x0000},
31 {0x32, 0x0030},
32 {0x41, 0x703a},
33 {0x40, 0x1c78},
34 {0x2c, 0x1c00},
35 {0x36, 0xc013},
36 {0x37, 0x6f18},
37 {0x27, 0x0008},
38 {0x2a, 0x0001},
39 {0x34, 0x0a40},
Antti Palosaarif9263742012-03-30 06:37:26 -030040 };
41
Antti Palosaari465433f2015-04-15 20:26:06 -030042 dev_dbg(&client->dev, "\n");
Antti Palosaarie6211c72012-09-11 22:27:10 -030043
Antti Palosaari89054e32012-09-11 22:27:07 -030044 if (fe->callback) {
Antti Palosaari465433f2015-04-15 20:26:06 -030045 ret = fe->callback(client->adapter,
46 DVB_FRONTEND_COMPONENT_TUNER,
47 TUA9001_CMD_RESETN, 0);
48 if (ret)
Antti Palosaari89054e32012-09-11 22:27:07 -030049 goto err;
50 }
51
Antti Palosaarif9263742012-03-30 06:37:26 -030052 for (i = 0; i < ARRAY_SIZE(data); i++) {
Antti Palosaaridd219a82015-04-15 20:38:43 -030053 ret = regmap_write(dev->regmap, data[i].reg, data[i].val);
Antti Palosaari465433f2015-04-15 20:26:06 -030054 if (ret)
Antti Palosaari96676232015-04-15 19:33:13 -030055 goto err;
Antti Palosaarif9263742012-03-30 06:37:26 -030056 }
Antti Palosaari465433f2015-04-15 20:26:06 -030057 return 0;
Antti Palosaari89054e32012-09-11 22:27:07 -030058err:
Antti Palosaari465433f2015-04-15 20:26:06 -030059 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari89054e32012-09-11 22:27:07 -030060 return ret;
61}
62
63static int tua9001_sleep(struct dvb_frontend *fe)
64{
Antti Palosaari465433f2015-04-15 20:26:06 -030065 struct tua9001_dev *dev = fe->tuner_priv;
66 struct i2c_client *client = dev->client;
67 int ret;
Antti Palosaari89054e32012-09-11 22:27:07 -030068
Antti Palosaari465433f2015-04-15 20:26:06 -030069 dev_dbg(&client->dev, "\n");
Antti Palosaarie6211c72012-09-11 22:27:10 -030070
Antti Palosaari465433f2015-04-15 20:26:06 -030071 if (fe->callback) {
72 ret = fe->callback(client->adapter,
73 DVB_FRONTEND_COMPONENT_TUNER,
74 TUA9001_CMD_RESETN, 1);
75 if (ret)
76 goto err;
77 }
78 return 0;
79err:
80 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarif9263742012-03-30 06:37:26 -030081 return ret;
82}
83
84static int tua9001_set_params(struct dvb_frontend *fe)
85{
Antti Palosaari465433f2015-04-15 20:26:06 -030086 struct tua9001_dev *dev = fe->tuner_priv;
87 struct i2c_client *client = dev->client;
Antti Palosaarif9263742012-03-30 06:37:26 -030088 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari465433f2015-04-15 20:26:06 -030089 int ret, i;
Antti Palosaarif9263742012-03-30 06:37:26 -030090 u16 val;
Antti Palosaari465433f2015-04-15 20:26:06 -030091 struct tua9001_reg_val data[2];
Antti Palosaarif9263742012-03-30 06:37:26 -030092
Antti Palosaari465433f2015-04-15 20:26:06 -030093 dev_dbg(&client->dev,
94 "delivery_system=%u frequency=%u bandwidth_hz=%u\n",
95 c->delivery_system, c->frequency, c->bandwidth_hz);
Antti Palosaarif9263742012-03-30 06:37:26 -030096
97 switch (c->delivery_system) {
98 case SYS_DVBT:
99 switch (c->bandwidth_hz) {
100 case 8000000:
101 val = 0x0000;
102 break;
103 case 7000000:
104 val = 0x1000;
105 break;
106 case 6000000:
107 val = 0x2000;
108 break;
109 case 5000000:
110 val = 0x3000;
111 break;
112 default:
113 ret = -EINVAL;
114 goto err;
115 }
116 break;
117 default:
118 ret = -EINVAL;
119 goto err;
120 }
121
122 data[0].reg = 0x04;
123 data[0].val = val;
Antti Palosaarif9263742012-03-30 06:37:26 -0300124 data[1].reg = 0x1f;
Antti Palosaari47f95db2015-04-16 07:36:51 -0300125 data[1].val = div_u64((u64) (c->frequency - 150000000) * 48, 1000000);
Antti Palosaarif9263742012-03-30 06:37:26 -0300126
Antti Palosaari89054e32012-09-11 22:27:07 -0300127 if (fe->callback) {
Antti Palosaari465433f2015-04-15 20:26:06 -0300128 ret = fe->callback(client->adapter,
129 DVB_FRONTEND_COMPONENT_TUNER,
130 TUA9001_CMD_RXEN, 0);
131 if (ret)
Antti Palosaari96676232015-04-15 19:33:13 -0300132 goto err;
Antti Palosaari89054e32012-09-11 22:27:07 -0300133 }
134
Antti Palosaarif9263742012-03-30 06:37:26 -0300135 for (i = 0; i < ARRAY_SIZE(data); i++) {
Antti Palosaaridd219a82015-04-15 20:38:43 -0300136 ret = regmap_write(dev->regmap, data[i].reg, data[i].val);
Antti Palosaari465433f2015-04-15 20:26:06 -0300137 if (ret)
Antti Palosaari96676232015-04-15 19:33:13 -0300138 goto err;
Antti Palosaarif9263742012-03-30 06:37:26 -0300139 }
140
Antti Palosaari89054e32012-09-11 22:27:07 -0300141 if (fe->callback) {
Antti Palosaari465433f2015-04-15 20:26:06 -0300142 ret = fe->callback(client->adapter,
143 DVB_FRONTEND_COMPONENT_TUNER,
144 TUA9001_CMD_RXEN, 1);
145 if (ret)
Antti Palosaari96676232015-04-15 19:33:13 -0300146 goto err;
Antti Palosaari89054e32012-09-11 22:27:07 -0300147 }
Antti Palosaari465433f2015-04-15 20:26:06 -0300148 return 0;
Antti Palosaarif9263742012-03-30 06:37:26 -0300149err:
Antti Palosaari465433f2015-04-15 20:26:06 -0300150 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarif9263742012-03-30 06:37:26 -0300151 return ret;
152}
153
154static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
155{
Antti Palosaari465433f2015-04-15 20:26:06 -0300156 struct tua9001_dev *dev = fe->tuner_priv;
157 struct i2c_client *client = dev->client;
Antti Palosaarie6211c72012-09-11 22:27:10 -0300158
Antti Palosaari465433f2015-04-15 20:26:06 -0300159 dev_dbg(&client->dev, "\n");
Antti Palosaarie6211c72012-09-11 22:27:10 -0300160
Antti Palosaarif9263742012-03-30 06:37:26 -0300161 *frequency = 0; /* Zero-IF */
Antti Palosaarif9263742012-03-30 06:37:26 -0300162 return 0;
163}
164
165static const struct dvb_tuner_ops tua9001_tuner_ops = {
166 .info = {
Antti Palosaari465433f2015-04-15 20:26:06 -0300167 .name = "Infineon TUA9001",
Antti Palosaarif9263742012-03-30 06:37:26 -0300168 .frequency_min = 170000000,
169 .frequency_max = 862000000,
Antti Palosaarif9263742012-03-30 06:37:26 -0300170 },
171
Antti Palosaarif9263742012-03-30 06:37:26 -0300172 .init = tua9001_init,
Antti Palosaari89054e32012-09-11 22:27:07 -0300173 .sleep = tua9001_sleep,
Antti Palosaarif9263742012-03-30 06:37:26 -0300174 .set_params = tua9001_set_params,
175
176 .get_if_frequency = tua9001_get_if_frequency,
177};
178
Antti Palosaarifc851c62015-04-15 19:12:34 -0300179static int tua9001_probe(struct i2c_client *client,
180 const struct i2c_device_id *id)
181{
Antti Palosaari465433f2015-04-15 20:26:06 -0300182 struct tua9001_dev *dev;
Antti Palosaarifc851c62015-04-15 19:12:34 -0300183 struct tua9001_platform_data *pdata = client->dev.platform_data;
184 struct dvb_frontend *fe = pdata->dvb_frontend;
185 int ret;
Antti Palosaaridd219a82015-04-15 20:38:43 -0300186 static const struct regmap_config regmap_config = {
187 .reg_bits = 8,
188 .val_bits = 16,
189 };
Antti Palosaarifc851c62015-04-15 19:12:34 -0300190
191 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
192 if (!dev) {
193 ret = -ENOMEM;
194 goto err;
195 }
196
Antti Palosaarifc851c62015-04-15 19:12:34 -0300197 dev->fe = pdata->dvb_frontend;
Antti Palosaaridd219a82015-04-15 20:38:43 -0300198 dev->client = client;
199 dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
200 if (IS_ERR(dev->regmap)) {
201 ret = PTR_ERR(dev->regmap);
202 goto err_kfree;
203 }
Antti Palosaarifc851c62015-04-15 19:12:34 -0300204
205 if (fe->callback) {
206 ret = fe->callback(client->adapter,
207 DVB_FRONTEND_COMPONENT_TUNER,
208 TUA9001_CMD_CEN, 1);
209 if (ret)
210 goto err_kfree;
211
212 ret = fe->callback(client->adapter,
213 DVB_FRONTEND_COMPONENT_TUNER,
214 TUA9001_CMD_RXEN, 0);
215 if (ret)
216 goto err_kfree;
217
218 ret = fe->callback(client->adapter,
219 DVB_FRONTEND_COMPONENT_TUNER,
220 TUA9001_CMD_RESETN, 1);
221 if (ret)
222 goto err_kfree;
223 }
224
225 fe->tuner_priv = dev;
226 memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
227 sizeof(struct dvb_tuner_ops));
Antti Palosaarifc851c62015-04-15 19:12:34 -0300228 i2c_set_clientdata(client, dev);
229
Antti Palosaari465433f2015-04-15 20:26:06 -0300230 dev_info(&client->dev, "Infineon TUA9001 successfully attached\n");
Antti Palosaarifc851c62015-04-15 19:12:34 -0300231 return 0;
232err_kfree:
233 kfree(dev);
234err:
235 dev_dbg(&client->dev, "failed=%d\n", ret);
236 return ret;
237}
238
239static int tua9001_remove(struct i2c_client *client)
240{
Antti Palosaari465433f2015-04-15 20:26:06 -0300241 struct tua9001_dev *dev = i2c_get_clientdata(client);
Antti Palosaarifc851c62015-04-15 19:12:34 -0300242 struct dvb_frontend *fe = dev->fe;
243 int ret;
244
245 dev_dbg(&client->dev, "\n");
246
247 if (fe->callback) {
248 ret = fe->callback(client->adapter,
249 DVB_FRONTEND_COMPONENT_TUNER,
250 TUA9001_CMD_CEN, 0);
251 if (ret)
252 goto err_kfree;
253 }
254 kfree(dev);
255 return 0;
256err_kfree:
257 kfree(dev);
258 dev_dbg(&client->dev, "failed=%d\n", ret);
259 return ret;
260}
261
262static const struct i2c_device_id tua9001_id_table[] = {
263 {"tua9001", 0},
264 {}
265};
266MODULE_DEVICE_TABLE(i2c, tua9001_id_table);
267
268static struct i2c_driver tua9001_driver = {
269 .driver = {
Antti Palosaarifc851c62015-04-15 19:12:34 -0300270 .name = "tua9001",
271 .suppress_bind_attrs = true,
272 },
273 .probe = tua9001_probe,
274 .remove = tua9001_remove,
275 .id_table = tua9001_id_table,
276};
277
278module_i2c_driver(tua9001_driver);
279
Antti Palosaari465433f2015-04-15 20:26:06 -0300280MODULE_DESCRIPTION("Infineon TUA9001 silicon tuner driver");
Antti Palosaarif9263742012-03-30 06:37:26 -0300281MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
282MODULE_LICENSE("GPL");