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