pt3.c 19 KB

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