blob: 0d2e2b21712124ad832b48cff515b7339b43db11 [file] [log] [blame]
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -03001/*
2 * Earthsoft PT3 driver
3 *
4 * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
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.
15 */
16
17#include <linux/freezer.h>
18#include <linux/kernel.h>
19#include <linux/kthread.h>
20#include <linux/mutex.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/string.h>
24
25#include "dmxdev.h"
26#include "dvbdev.h"
27#include "dvb_demux.h"
28#include "dvb_frontend.h"
29
30#include "pt3.h"
31
32DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33
34static bool one_adapter;
35module_param(one_adapter, bool, 0444);
36MODULE_PARM_DESC(one_adapter, "Place FE's together under one adapter.");
37
38static int num_bufs = 4;
39module_param(num_bufs, int, 0444);
40MODULE_PARM_DESC(num_bufs, "Number of DMA buffer (188KiB) per FE.");
41
42
43static const struct i2c_algorithm pt3_i2c_algo = {
44 .master_xfer = &pt3_i2c_master_xfer,
45 .functionality = &pt3_i2c_functionality,
46};
47
48static const struct pt3_adap_config adap_conf[PT3_NUM_FE] = {
49 {
50 .demod_info = {
51 I2C_BOARD_INFO(TC90522_I2C_DEV_SAT, 0x11),
52 },
53 .tuner_info = {
54 I2C_BOARD_INFO("qm1d1c0042", 0x63),
55 },
56 .tuner_cfg.qm1d1c0042 = {
57 .lpf = 1,
58 },
59 .init_freq = 1049480 - 300,
60 },
61 {
62 .demod_info = {
63 I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x10),
64 },
65 .tuner_info = {
66 I2C_BOARD_INFO("mxl301rf", 0x62),
67 },
68 .init_freq = 515142857,
69 },
70 {
71 .demod_info = {
72 I2C_BOARD_INFO(TC90522_I2C_DEV_SAT, 0x13),
73 },
74 .tuner_info = {
75 I2C_BOARD_INFO("qm1d1c0042", 0x60),
76 },
77 .tuner_cfg.qm1d1c0042 = {
78 .lpf = 1,
79 },
80 .init_freq = 1049480 + 300,
81 },
82 {
83 .demod_info = {
84 I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x12),
85 },
86 .tuner_info = {
87 I2C_BOARD_INFO("mxl301rf", 0x61),
88 },
89 .init_freq = 521142857,
90 },
91};
92
93
94struct reg_val {
95 u8 reg;
96 u8 val;
97};
98
99static int
100pt3_demod_write(struct pt3_adapter *adap, const struct reg_val *data, int num)
101{
102 struct i2c_msg msg;
103 int i, ret;
104
105 ret = 0;
106 msg.addr = adap->i2c_demod->addr;
107 msg.flags = 0;
108 msg.len = 2;
109 for (i = 0; i < num; i++) {
110 msg.buf = (u8 *)&data[i];
111 ret = i2c_transfer(adap->i2c_demod->adapter, &msg, 1);
112 if (ret == 0)
113 ret = -EREMOTE;
114 if (ret < 0)
115 return ret;
116 }
117 return 0;
118}
119
120static inline void pt3_lnb_ctrl(struct pt3_board *pt3, bool on)
121{
122 iowrite32((on ? 0x0f : 0x0c), pt3->regs[0] + REG_SYSTEM_W);
123}
124
125static inline struct pt3_adapter *pt3_find_adapter(struct dvb_frontend *fe)
126{
127 struct pt3_board *pt3;
128 int i;
129
130 if (one_adapter) {
131 pt3 = fe->dvb->priv;
132 for (i = 0; i < PT3_NUM_FE; i++)
133 if (pt3->adaps[i]->fe == fe)
134 return pt3->adaps[i];
135 }
136 return container_of(fe->dvb, struct pt3_adapter, dvb_adap);
137}
138
139/*
140 * all 4 tuners in PT3 are packaged in a can module (Sharp VA4M6JC2103).
141 * it seems that they share the power lines and Amp power line and
142 * adaps[3] controls those powers.
143 */
144static int
145pt3_set_tuner_power(struct pt3_board *pt3, bool tuner_on, bool amp_on)
146{
147 struct reg_val rv = { 0x1e, 0x99 };
148
149 if (tuner_on)
150 rv.val |= 0x40;
151 if (amp_on)
152 rv.val |= 0x04;
153 return pt3_demod_write(pt3->adaps[PT3_NUM_FE - 1], &rv, 1);
154}
155
156static int pt3_set_lna(struct dvb_frontend *fe)
157{
158 struct pt3_adapter *adap;
159 struct pt3_board *pt3;
160 u32 val;
161 int ret;
162
163 /* LNA is shared btw. 2 TERR-tuners */
164
165 adap = pt3_find_adapter(fe);
166 val = fe->dtv_property_cache.lna;
167 if (val == LNA_AUTO || val == adap->cur_lna)
168 return 0;
169
170 pt3 = adap->dvb_adap.priv;
171 if (mutex_lock_interruptible(&pt3->lock))
172 return -ERESTARTSYS;
173 if (val)
174 pt3->lna_on_cnt++;
175 else
176 pt3->lna_on_cnt--;
177
178 if (val && pt3->lna_on_cnt <= 1) {
179 pt3->lna_on_cnt = 1;
180 ret = pt3_set_tuner_power(pt3, true, true);
181 } else if (!val && pt3->lna_on_cnt <= 0) {
182 pt3->lna_on_cnt = 0;
183 ret = pt3_set_tuner_power(pt3, true, false);
184 } else
185 ret = 0;
186 mutex_unlock(&pt3->lock);
187 adap->cur_lna = (val != 0);
188 return ret;
189}
190
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300191static int pt3_set_voltage(struct dvb_frontend *fe, enum fe_sec_voltage volt)
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300192{
193 struct pt3_adapter *adap;
194 struct pt3_board *pt3;
195 bool on;
196
197 /* LNB power is shared btw. 2 SAT-tuners */
198
199 adap = pt3_find_adapter(fe);
200 on = (volt != SEC_VOLTAGE_OFF);
201 if (on == adap->cur_lnb)
202 return 0;
203 adap->cur_lnb = on;
204 pt3 = adap->dvb_adap.priv;
205 if (mutex_lock_interruptible(&pt3->lock))
206 return -ERESTARTSYS;
207 if (on)
208 pt3->lnb_on_cnt++;
209 else
210 pt3->lnb_on_cnt--;
211
212 if (on && pt3->lnb_on_cnt <= 1) {
213 pt3->lnb_on_cnt = 1;
214 pt3_lnb_ctrl(pt3, true);
215 } else if (!on && pt3->lnb_on_cnt <= 0) {
216 pt3->lnb_on_cnt = 0;
217 pt3_lnb_ctrl(pt3, false);
218 }
219 mutex_unlock(&pt3->lock);
220 return 0;
221}
222
223/* register values used in pt3_fe_init() */
224
225static const struct reg_val init0_sat[] = {
226 { 0x03, 0x01 },
227 { 0x1e, 0x10 },
228};
229static const struct reg_val init0_ter[] = {
230 { 0x01, 0x40 },
231 { 0x1c, 0x10 },
232};
233static const struct reg_val cfg_sat[] = {
234 { 0x1c, 0x15 },
235 { 0x1f, 0x04 },
236};
237static const struct reg_val cfg_ter[] = {
238 { 0x1d, 0x01 },
239};
240
241/*
242 * pt3_fe_init: initialize demod sub modules and ISDB-T tuners all at once.
243 *
244 * As for demod IC (TC90522) and ISDB-T tuners (MxL301RF),
245 * the i2c sequences for init'ing them are not public and hidden in a ROM,
246 * and include the board specific configurations as well.
247 * They are stored in a lump and cannot be taken out / accessed separately,
248 * thus cannot be moved to the FE/tuner driver.
249 */
250static int pt3_fe_init(struct pt3_board *pt3)
251{
252 int i, ret;
253 struct dvb_frontend *fe;
254
255 pt3_i2c_reset(pt3);
256 ret = pt3_init_all_demods(pt3);
257 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300258 dev_warn(&pt3->pdev->dev, "Failed to init demod chips\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300259 return ret;
260 }
261
262 /* additional config? */
263 for (i = 0; i < PT3_NUM_FE; i++) {
264 fe = pt3->adaps[i]->fe;
265
266 if (fe->ops.delsys[0] == SYS_ISDBS)
267 ret = pt3_demod_write(pt3->adaps[i],
268 init0_sat, ARRAY_SIZE(init0_sat));
269 else
270 ret = pt3_demod_write(pt3->adaps[i],
271 init0_ter, ARRAY_SIZE(init0_ter));
272 if (ret < 0) {
273 dev_warn(&pt3->pdev->dev,
Joe Perches509cd822014-10-10 18:10:49 -0300274 "demod[%d] failed in init sequence0\n", i);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300275 return ret;
276 }
277 ret = fe->ops.init(fe);
278 if (ret < 0)
279 return ret;
280 }
281
282 usleep_range(2000, 4000);
283 ret = pt3_set_tuner_power(pt3, true, false);
284 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300285 dev_warn(&pt3->pdev->dev, "Failed to control tuner module\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300286 return ret;
287 }
288
289 /* output pin configuration */
290 for (i = 0; i < PT3_NUM_FE; i++) {
291 fe = pt3->adaps[i]->fe;
292 if (fe->ops.delsys[0] == SYS_ISDBS)
293 ret = pt3_demod_write(pt3->adaps[i],
294 cfg_sat, ARRAY_SIZE(cfg_sat));
295 else
296 ret = pt3_demod_write(pt3->adaps[i],
297 cfg_ter, ARRAY_SIZE(cfg_ter));
298 if (ret < 0) {
299 dev_warn(&pt3->pdev->dev,
Joe Perches509cd822014-10-10 18:10:49 -0300300 "demod[%d] failed in init sequence1\n", i);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300301 return ret;
302 }
303 }
304 usleep_range(4000, 6000);
305
306 for (i = 0; i < PT3_NUM_FE; i++) {
307 fe = pt3->adaps[i]->fe;
308 if (fe->ops.delsys[0] != SYS_ISDBS)
309 continue;
310 /* init and wake-up ISDB-S tuners */
311 ret = fe->ops.tuner_ops.init(fe);
312 if (ret < 0) {
313 dev_warn(&pt3->pdev->dev,
Joe Perches509cd822014-10-10 18:10:49 -0300314 "Failed to init SAT-tuner[%d]\n", i);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300315 return ret;
316 }
317 }
318 ret = pt3_init_all_mxl301rf(pt3);
319 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300320 dev_warn(&pt3->pdev->dev, "Failed to init TERR-tuners\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300321 return ret;
322 }
323
324 ret = pt3_set_tuner_power(pt3, true, true);
325 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300326 dev_warn(&pt3->pdev->dev, "Failed to control tuner module\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300327 return ret;
328 }
329
330 /* Wake up all tuners and make an initial tuning,
331 * in order to avoid interference among the tuners in the module,
332 * according to the doc from the manufacturer.
333 */
334 for (i = 0; i < PT3_NUM_FE; i++) {
335 fe = pt3->adaps[i]->fe;
336 ret = 0;
337 if (fe->ops.delsys[0] == SYS_ISDBT)
338 ret = fe->ops.tuner_ops.init(fe);
339 /* set only when called from pt3_probe(), not resume() */
340 if (ret == 0 && fe->dtv_property_cache.frequency == 0) {
341 fe->dtv_property_cache.frequency =
342 adap_conf[i].init_freq;
343 ret = fe->ops.tuner_ops.set_params(fe);
344 }
345 if (ret < 0) {
346 dev_warn(&pt3->pdev->dev,
Joe Perches509cd822014-10-10 18:10:49 -0300347 "Failed in initial tuning of tuner[%d]\n", i);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300348 return ret;
349 }
350 }
351
352 /* and sleep again, waiting to be opened by users. */
353 for (i = 0; i < PT3_NUM_FE; i++) {
354 fe = pt3->adaps[i]->fe;
355 if (fe->ops.tuner_ops.sleep)
356 ret = fe->ops.tuner_ops.sleep(fe);
357 if (ret < 0)
358 break;
359 if (fe->ops.sleep)
360 ret = fe->ops.sleep(fe);
361 if (ret < 0)
362 break;
363 if (fe->ops.delsys[0] == SYS_ISDBS)
364 fe->ops.set_voltage = &pt3_set_voltage;
365 else
366 fe->ops.set_lna = &pt3_set_lna;
367 }
368 if (i < PT3_NUM_FE) {
Joe Perches509cd822014-10-10 18:10:49 -0300369 dev_warn(&pt3->pdev->dev, "FE[%d] failed to standby\n", i);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300370 return ret;
371 }
372 return 0;
373}
374
375
376static int pt3_attach_fe(struct pt3_board *pt3, int i)
377{
378 struct i2c_board_info info;
379 struct tc90522_config cfg;
380 struct i2c_client *cl;
381 struct dvb_adapter *dvb_adap;
382 int ret;
383
384 info = adap_conf[i].demod_info;
385 cfg = adap_conf[i].demod_cfg;
386 cfg.tuner_i2c = NULL;
387 info.platform_data = &cfg;
388
389 ret = -ENODEV;
390 request_module("tc90522");
391 cl = i2c_new_device(&pt3->i2c_adap, &info);
392 if (!cl || !cl->dev.driver)
393 return -ENODEV;
394 pt3->adaps[i]->i2c_demod = cl;
395 if (!try_module_get(cl->dev.driver->owner))
Antti Palosaaricf3167c2014-09-26 22:45:36 -0300396 goto err_demod_i2c_unregister_device;
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300397
398 if (!strncmp(cl->name, TC90522_I2C_DEV_SAT, sizeof(cl->name))) {
399 struct qm1d1c0042_config tcfg;
400
401 tcfg = adap_conf[i].tuner_cfg.qm1d1c0042;
402 tcfg.fe = cfg.fe;
403 info = adap_conf[i].tuner_info;
404 info.platform_data = &tcfg;
405 request_module("qm1d1c0042");
406 cl = i2c_new_device(cfg.tuner_i2c, &info);
407 } else {
408 struct mxl301rf_config tcfg;
409
410 tcfg = adap_conf[i].tuner_cfg.mxl301rf;
411 tcfg.fe = cfg.fe;
412 info = adap_conf[i].tuner_info;
413 info.platform_data = &tcfg;
414 request_module("mxl301rf");
415 cl = i2c_new_device(cfg.tuner_i2c, &info);
416 }
417 if (!cl || !cl->dev.driver)
Antti Palosaaricf3167c2014-09-26 22:45:36 -0300418 goto err_demod_module_put;
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300419 pt3->adaps[i]->i2c_tuner = cl;
420 if (!try_module_get(cl->dev.driver->owner))
Antti Palosaaricf3167c2014-09-26 22:45:36 -0300421 goto err_tuner_i2c_unregister_device;
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300422
423 dvb_adap = &pt3->adaps[one_adapter ? 0 : i]->dvb_adap;
424 ret = dvb_register_frontend(dvb_adap, cfg.fe);
425 if (ret < 0)
Antti Palosaaricf3167c2014-09-26 22:45:36 -0300426 goto err_tuner_module_put;
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300427 pt3->adaps[i]->fe = cfg.fe;
428 return 0;
429
Antti Palosaaricf3167c2014-09-26 22:45:36 -0300430err_tuner_module_put:
431 module_put(pt3->adaps[i]->i2c_tuner->dev.driver->owner);
432err_tuner_i2c_unregister_device:
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300433 i2c_unregister_device(pt3->adaps[i]->i2c_tuner);
Antti Palosaaricf3167c2014-09-26 22:45:36 -0300434err_demod_module_put:
435 module_put(pt3->adaps[i]->i2c_demod->dev.driver->owner);
436err_demod_i2c_unregister_device:
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300437 i2c_unregister_device(pt3->adaps[i]->i2c_demod);
Antti Palosaaricf3167c2014-09-26 22:45:36 -0300438
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300439 return ret;
440}
441
442
443static int pt3_fetch_thread(void *data)
444{
445 struct pt3_adapter *adap = data;
446 ktime_t delay;
447 bool was_frozen;
448
449#define PT3_INITIAL_BUF_DROPS 4
450#define PT3_FETCH_DELAY 10
451#define PT3_FETCH_DELAY_DELTA 2
452
453 pt3_init_dmabuf(adap);
454 adap->num_discard = PT3_INITIAL_BUF_DROPS;
455
Joe Perches509cd822014-10-10 18:10:49 -0300456 dev_dbg(adap->dvb_adap.device, "PT3: [%s] started\n",
457 adap->thread->comm);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300458 set_freezable();
459 while (!kthread_freezable_should_stop(&was_frozen)) {
460 if (was_frozen)
461 adap->num_discard = PT3_INITIAL_BUF_DROPS;
462
463 pt3_proc_dma(adap);
464
465 delay = ktime_set(0, PT3_FETCH_DELAY * NSEC_PER_MSEC);
466 set_current_state(TASK_UNINTERRUPTIBLE);
467 freezable_schedule_hrtimeout_range(&delay,
468 PT3_FETCH_DELAY_DELTA * NSEC_PER_MSEC,
469 HRTIMER_MODE_REL);
470 }
Joe Perches509cd822014-10-10 18:10:49 -0300471 dev_dbg(adap->dvb_adap.device, "PT3: [%s] exited\n",
472 adap->thread->comm);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300473 adap->thread = NULL;
474 return 0;
475}
476
477static int pt3_start_streaming(struct pt3_adapter *adap)
478{
479 struct task_struct *thread;
480
481 /* start fetching thread */
482 thread = kthread_run(pt3_fetch_thread, adap, "pt3-ad%i-dmx%i",
483 adap->dvb_adap.num, adap->dmxdev.dvbdev->id);
484 if (IS_ERR(thread)) {
485 int ret = PTR_ERR(thread);
486
487 dev_warn(adap->dvb_adap.device,
Joe Perches509cd822014-10-10 18:10:49 -0300488 "PT3 (adap:%d, dmx:%d): failed to start kthread\n",
489 adap->dvb_adap.num, adap->dmxdev.dvbdev->id);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300490 return ret;
491 }
492 adap->thread = thread;
493
494 return pt3_start_dma(adap);
495}
496
497static int pt3_stop_streaming(struct pt3_adapter *adap)
498{
499 int ret;
500
501 ret = pt3_stop_dma(adap);
502 if (ret)
503 dev_warn(adap->dvb_adap.device,
Joe Perches509cd822014-10-10 18:10:49 -0300504 "PT3: failed to stop streaming of adap:%d/FE:%d\n",
505 adap->dvb_adap.num, adap->fe->id);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300506
507 /* kill the fetching thread */
508 ret = kthread_stop(adap->thread);
509 return ret;
510}
511
512static int pt3_start_feed(struct dvb_demux_feed *feed)
513{
514 struct pt3_adapter *adap;
515
516 if (signal_pending(current))
517 return -EINTR;
518
519 adap = container_of(feed->demux, struct pt3_adapter, demux);
520 adap->num_feeds++;
521 if (adap->thread)
522 return 0;
523 if (adap->num_feeds != 1) {
524 dev_warn(adap->dvb_adap.device,
Joe Perches509cd822014-10-10 18:10:49 -0300525 "%s: unmatched start/stop_feed in adap:%i/dmx:%i\n",
526 __func__, adap->dvb_adap.num, adap->dmxdev.dvbdev->id);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300527 adap->num_feeds = 1;
528 }
529
530 return pt3_start_streaming(adap);
531
532}
533
534static int pt3_stop_feed(struct dvb_demux_feed *feed)
535{
536 struct pt3_adapter *adap;
537
538 adap = container_of(feed->demux, struct pt3_adapter, demux);
539
540 adap->num_feeds--;
541 if (adap->num_feeds > 0 || !adap->thread)
542 return 0;
543 adap->num_feeds = 0;
544
545 return pt3_stop_streaming(adap);
546}
547
548
549static int pt3_alloc_adapter(struct pt3_board *pt3, int index)
550{
551 int ret;
552 struct pt3_adapter *adap;
553 struct dvb_adapter *da;
554
555 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
Joe Perches509cd822014-10-10 18:10:49 -0300556 if (!adap)
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300557 return -ENOMEM;
Joe Perches509cd822014-10-10 18:10:49 -0300558
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300559 pt3->adaps[index] = adap;
560 adap->adap_idx = index;
561
562 if (index == 0 || !one_adapter) {
563 ret = dvb_register_adapter(&adap->dvb_adap, "PT3 DVB",
564 THIS_MODULE, &pt3->pdev->dev, adapter_nr);
565 if (ret < 0) {
566 dev_err(&pt3->pdev->dev,
Joe Perches509cd822014-10-10 18:10:49 -0300567 "failed to register adapter dev\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300568 goto err_mem;
569 }
570 da = &adap->dvb_adap;
571 } else
572 da = &pt3->adaps[0]->dvb_adap;
573
574 adap->dvb_adap.priv = pt3;
575 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
576 adap->demux.priv = adap;
577 adap->demux.feednum = 256;
578 adap->demux.filternum = 256;
579 adap->demux.start_feed = pt3_start_feed;
580 adap->demux.stop_feed = pt3_stop_feed;
581 ret = dvb_dmx_init(&adap->demux);
582 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300583 dev_err(&pt3->pdev->dev, "failed to init dmx dev\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300584 goto err_adap;
585 }
586
587 adap->dmxdev.filternum = 256;
588 adap->dmxdev.demux = &adap->demux.dmx;
589 ret = dvb_dmxdev_init(&adap->dmxdev, da);
590 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300591 dev_err(&pt3->pdev->dev, "failed to init dmxdev\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300592 goto err_demux;
593 }
594
595 ret = pt3_alloc_dmabuf(adap);
596 if (ret) {
Joe Perches509cd822014-10-10 18:10:49 -0300597 dev_err(&pt3->pdev->dev, "failed to alloc DMA buffers\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300598 goto err_dmabuf;
599 }
600
601 return 0;
602
603err_dmabuf:
604 pt3_free_dmabuf(adap);
605 dvb_dmxdev_release(&adap->dmxdev);
606err_demux:
607 dvb_dmx_release(&adap->demux);
608err_adap:
609 if (index == 0 || !one_adapter)
610 dvb_unregister_adapter(da);
611err_mem:
612 kfree(adap);
613 pt3->adaps[index] = NULL;
614 return ret;
615}
616
617static void pt3_cleanup_adapter(struct pt3_board *pt3, int index)
618{
619 struct pt3_adapter *adap;
620 struct dmx_demux *dmx;
621
622 adap = pt3->adaps[index];
623 if (adap == NULL)
624 return;
625
626 /* stop demux kthread */
627 if (adap->thread)
628 pt3_stop_streaming(adap);
629
630 dmx = &adap->demux.dmx;
631 dmx->close(dmx);
632 if (adap->fe) {
633 adap->fe->callback = NULL;
634 if (adap->fe->frontend_priv)
635 dvb_unregister_frontend(adap->fe);
636 if (adap->i2c_tuner) {
637 module_put(adap->i2c_tuner->dev.driver->owner);
638 i2c_unregister_device(adap->i2c_tuner);
639 }
640 if (adap->i2c_demod) {
641 module_put(adap->i2c_demod->dev.driver->owner);
642 i2c_unregister_device(adap->i2c_demod);
643 }
644 }
645 pt3_free_dmabuf(adap);
646 dvb_dmxdev_release(&adap->dmxdev);
647 dvb_dmx_release(&adap->demux);
648 if (index == 0 || !one_adapter)
649 dvb_unregister_adapter(&adap->dvb_adap);
650 kfree(adap);
651 pt3->adaps[index] = NULL;
652}
653
654#ifdef CONFIG_PM_SLEEP
655
656static int pt3_suspend(struct device *dev)
657{
658 struct pci_dev *pdev = to_pci_dev(dev);
659 struct pt3_board *pt3 = pci_get_drvdata(pdev);
660 int i;
661 struct pt3_adapter *adap;
662
663 for (i = 0; i < PT3_NUM_FE; i++) {
664 adap = pt3->adaps[i];
665 if (adap->num_feeds > 0)
666 pt3_stop_dma(adap);
667 dvb_frontend_suspend(adap->fe);
668 pt3_free_dmabuf(adap);
669 }
670
671 pt3_lnb_ctrl(pt3, false);
672 pt3_set_tuner_power(pt3, false, false);
673 return 0;
674}
675
676static int pt3_resume(struct device *dev)
677{
678 struct pci_dev *pdev = to_pci_dev(dev);
679 struct pt3_board *pt3 = pci_get_drvdata(pdev);
680 int i, ret;
681 struct pt3_adapter *adap;
682
683 ret = pt3_fe_init(pt3);
684 if (ret)
685 return ret;
686
687 if (pt3->lna_on_cnt > 0)
688 pt3_set_tuner_power(pt3, true, true);
689 if (pt3->lnb_on_cnt > 0)
690 pt3_lnb_ctrl(pt3, true);
691
692 for (i = 0; i < PT3_NUM_FE; i++) {
693 adap = pt3->adaps[i];
694 dvb_frontend_resume(adap->fe);
695 ret = pt3_alloc_dmabuf(adap);
696 if (ret) {
Joe Perches509cd822014-10-10 18:10:49 -0300697 dev_err(&pt3->pdev->dev, "failed to alloc DMA bufs\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300698 continue;
699 }
700 if (adap->num_feeds > 0)
701 pt3_start_dma(adap);
702 }
703
704 return 0;
705}
706
707#endif /* CONFIG_PM_SLEEP */
708
709
710static void pt3_remove(struct pci_dev *pdev)
711{
712 struct pt3_board *pt3;
713 int i;
714
715 pt3 = pci_get_drvdata(pdev);
716 for (i = PT3_NUM_FE - 1; i >= 0; i--)
717 pt3_cleanup_adapter(pt3, i);
718 i2c_del_adapter(&pt3->i2c_adap);
719 kfree(pt3->i2c_buf);
720 pci_iounmap(pt3->pdev, pt3->regs[0]);
721 pci_iounmap(pt3->pdev, pt3->regs[1]);
722 pci_release_regions(pdev);
723 pci_disable_device(pdev);
724 kfree(pt3);
725}
726
727static int pt3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
728{
729 u8 rev;
730 u32 ver;
731 int i, ret;
732 struct pt3_board *pt3;
733 struct i2c_adapter *i2c;
734
735 if (pci_read_config_byte(pdev, PCI_REVISION_ID, &rev) || rev != 1)
736 return -ENODEV;
737
738 ret = pci_enable_device(pdev);
739 if (ret < 0)
740 return -ENODEV;
741 pci_set_master(pdev);
742
743 ret = pci_request_regions(pdev, DRV_NAME);
744 if (ret < 0)
745 goto err_disable_device;
746
747 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
748 if (ret == 0)
749 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
750 else {
751 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
752 if (ret == 0)
753 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
754 else {
Joe Perches509cd822014-10-10 18:10:49 -0300755 dev_err(&pdev->dev, "Failed to set DMA mask\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300756 goto err_release_regions;
757 }
Joe Perches509cd822014-10-10 18:10:49 -0300758 dev_info(&pdev->dev, "Use 32bit DMA\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300759 }
760
761 pt3 = kzalloc(sizeof(*pt3), GFP_KERNEL);
762 if (!pt3) {
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300763 ret = -ENOMEM;
764 goto err_release_regions;
765 }
766 pci_set_drvdata(pdev, pt3);
767 pt3->pdev = pdev;
768 mutex_init(&pt3->lock);
769 pt3->regs[0] = pci_ioremap_bar(pdev, 0);
770 pt3->regs[1] = pci_ioremap_bar(pdev, 2);
771 if (pt3->regs[0] == NULL || pt3->regs[1] == NULL) {
Joe Perches509cd822014-10-10 18:10:49 -0300772 dev_err(&pdev->dev, "Failed to ioremap\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300773 ret = -ENOMEM;
774 goto err_kfree;
775 }
776
777 ver = ioread32(pt3->regs[0] + REG_VERSION);
778 if ((ver >> 16) != 0x0301) {
Joe Perches509cd822014-10-10 18:10:49 -0300779 dev_warn(&pdev->dev, "PT%d, I/F-ver.:%d not supported\n",
780 ver >> 24, (ver & 0x00ff0000) >> 16);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300781 ret = -ENODEV;
782 goto err_iounmap;
783 }
784
785 pt3->num_bufs = clamp_val(num_bufs, MIN_DATA_BUFS, MAX_DATA_BUFS);
786
787 pt3->i2c_buf = kmalloc(sizeof(*pt3->i2c_buf), GFP_KERNEL);
788 if (pt3->i2c_buf == NULL) {
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300789 ret = -ENOMEM;
790 goto err_iounmap;
791 }
792 i2c = &pt3->i2c_adap;
793 i2c->owner = THIS_MODULE;
794 i2c->algo = &pt3_i2c_algo;
795 i2c->algo_data = NULL;
796 i2c->dev.parent = &pdev->dev;
797 strlcpy(i2c->name, DRV_NAME, sizeof(i2c->name));
798 i2c_set_adapdata(i2c, pt3);
799 ret = i2c_add_adapter(i2c);
800 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300801 dev_err(&pdev->dev, "Failed to add i2c adapter\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300802 goto err_i2cbuf;
803 }
804
805 for (i = 0; i < PT3_NUM_FE; i++) {
806 ret = pt3_alloc_adapter(pt3, i);
807 if (ret < 0)
808 break;
809
810 ret = pt3_attach_fe(pt3, i);
811 if (ret < 0)
812 break;
813 }
814 if (i < PT3_NUM_FE) {
Joe Perches509cd822014-10-10 18:10:49 -0300815 dev_err(&pdev->dev, "Failed to create FE%d\n", i);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300816 goto err_cleanup_adapters;
817 }
818
819 ret = pt3_fe_init(pt3);
820 if (ret < 0) {
Joe Perches509cd822014-10-10 18:10:49 -0300821 dev_err(&pdev->dev, "Failed to init frontends\n");
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300822 i = PT3_NUM_FE - 1;
823 goto err_cleanup_adapters;
824 }
825
826 dev_info(&pdev->dev,
Joe Perches509cd822014-10-10 18:10:49 -0300827 "successfully init'ed PT%d (fw:0x%02x, I/F:0x%02x)\n",
828 ver >> 24, (ver >> 8) & 0xff, (ver >> 16) & 0xff);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300829 return 0;
830
831err_cleanup_adapters:
832 while (i >= 0)
833 pt3_cleanup_adapter(pt3, i--);
834 i2c_del_adapter(i2c);
835err_i2cbuf:
836 kfree(pt3->i2c_buf);
837err_iounmap:
838 if (pt3->regs[0])
839 pci_iounmap(pdev, pt3->regs[0]);
840 if (pt3->regs[1])
841 pci_iounmap(pdev, pt3->regs[1]);
842err_kfree:
843 kfree(pt3);
844err_release_regions:
845 pci_release_regions(pdev);
846err_disable_device:
847 pci_disable_device(pdev);
848 return ret;
849
850}
851
852static const struct pci_device_id pt3_id_table[] = {
853 { PCI_DEVICE_SUB(0x1172, 0x4c15, 0xee8d, 0x0368) },
854 { },
855};
856MODULE_DEVICE_TABLE(pci, pt3_id_table);
857
Mauro Carvalho Chehab49310ed2014-09-23 17:05:02 -0300858static SIMPLE_DEV_PM_OPS(pt3_pm_ops, pt3_suspend, pt3_resume);
Akihiro Tsukadaf5a98f32014-09-08 14:20:43 -0300859
860static struct pci_driver pt3_driver = {
861 .name = DRV_NAME,
862 .probe = pt3_probe,
863 .remove = pt3_remove,
864 .id_table = pt3_id_table,
865
866 .driver.pm = &pt3_pm_ops,
867};
868
869module_pci_driver(pt3_driver);
870
871MODULE_DESCRIPTION("Earthsoft PT3 Driver");
872MODULE_AUTHOR("Akihiro TSUKADA");
873MODULE_LICENSE("GPL");