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