pt3.c 20 KB

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