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, sizeof(cl->name))) {
  355. struct qm1d1c0042_config tcfg;
  356. tcfg = adap_conf[i].tuner_cfg.qm1d1c0042;
  357. tcfg.fe = cfg.fe;
  358. info = adap_conf[i].tuner_info;
  359. info.platform_data = &tcfg;
  360. request_module("qm1d1c0042");
  361. cl = i2c_new_device(cfg.tuner_i2c, &info);
  362. } else {
  363. struct mxl301rf_config tcfg;
  364. tcfg = adap_conf[i].tuner_cfg.mxl301rf;
  365. tcfg.fe = cfg.fe;
  366. info = adap_conf[i].tuner_info;
  367. info.platform_data = &tcfg;
  368. request_module("mxl301rf");
  369. cl = i2c_new_device(cfg.tuner_i2c, &info);
  370. }
  371. if (!cl || !cl->dev.driver)
  372. goto err_demod_module_put;
  373. pt3->adaps[i]->i2c_tuner = cl;
  374. if (!try_module_get(cl->dev.driver->owner))
  375. goto err_tuner_i2c_unregister_device;
  376. dvb_adap = &pt3->adaps[one_adapter ? 0 : i]->dvb_adap;
  377. ret = dvb_register_frontend(dvb_adap, cfg.fe);
  378. if (ret < 0)
  379. goto err_tuner_module_put;
  380. pt3->adaps[i]->fe = cfg.fe;
  381. return 0;
  382. err_tuner_module_put:
  383. module_put(pt3->adaps[i]->i2c_tuner->dev.driver->owner);
  384. err_tuner_i2c_unregister_device:
  385. i2c_unregister_device(pt3->adaps[i]->i2c_tuner);
  386. err_demod_module_put:
  387. module_put(pt3->adaps[i]->i2c_demod->dev.driver->owner);
  388. err_demod_i2c_unregister_device:
  389. i2c_unregister_device(pt3->adaps[i]->i2c_demod);
  390. return ret;
  391. }
  392. static int pt3_fetch_thread(void *data)
  393. {
  394. struct pt3_adapter *adap = data;
  395. ktime_t delay;
  396. bool was_frozen;
  397. #define PT3_INITIAL_BUF_DROPS 4
  398. #define PT3_FETCH_DELAY 10
  399. #define PT3_FETCH_DELAY_DELTA 2
  400. pt3_init_dmabuf(adap);
  401. adap->num_discard = PT3_INITIAL_BUF_DROPS;
  402. dev_dbg(adap->dvb_adap.device, "PT3: [%s] started\n",
  403. adap->thread->comm);
  404. set_freezable();
  405. while (!kthread_freezable_should_stop(&was_frozen)) {
  406. if (was_frozen)
  407. adap->num_discard = PT3_INITIAL_BUF_DROPS;
  408. pt3_proc_dma(adap);
  409. delay = ktime_set(0, PT3_FETCH_DELAY * NSEC_PER_MSEC);
  410. set_current_state(TASK_UNINTERRUPTIBLE);
  411. freezable_schedule_hrtimeout_range(&delay,
  412. PT3_FETCH_DELAY_DELTA * NSEC_PER_MSEC,
  413. HRTIMER_MODE_REL);
  414. }
  415. dev_dbg(adap->dvb_adap.device, "PT3: [%s] exited\n",
  416. adap->thread->comm);
  417. adap->thread = NULL;
  418. return 0;
  419. }
  420. static int pt3_start_streaming(struct pt3_adapter *adap)
  421. {
  422. struct task_struct *thread;
  423. /* start fetching thread */
  424. thread = kthread_run(pt3_fetch_thread, adap, "pt3-ad%i-dmx%i",
  425. adap->dvb_adap.num, adap->dmxdev.dvbdev->id);
  426. if (IS_ERR(thread)) {
  427. int ret = PTR_ERR(thread);
  428. dev_warn(adap->dvb_adap.device,
  429. "PT3 (adap:%d, dmx:%d): failed to start kthread\n",
  430. adap->dvb_adap.num, adap->dmxdev.dvbdev->id);
  431. return ret;
  432. }
  433. adap->thread = thread;
  434. return pt3_start_dma(adap);
  435. }
  436. static int pt3_stop_streaming(struct pt3_adapter *adap)
  437. {
  438. int ret;
  439. ret = pt3_stop_dma(adap);
  440. if (ret)
  441. dev_warn(adap->dvb_adap.device,
  442. "PT3: failed to stop streaming of adap:%d/FE:%d\n",
  443. adap->dvb_adap.num, adap->fe->id);
  444. /* kill the fetching thread */
  445. ret = kthread_stop(adap->thread);
  446. return ret;
  447. }
  448. static int pt3_start_feed(struct dvb_demux_feed *feed)
  449. {
  450. struct pt3_adapter *adap;
  451. if (signal_pending(current))
  452. return -EINTR;
  453. adap = container_of(feed->demux, struct pt3_adapter, demux);
  454. adap->num_feeds++;
  455. if (adap->thread)
  456. return 0;
  457. if (adap->num_feeds != 1) {
  458. dev_warn(adap->dvb_adap.device,
  459. "%s: unmatched start/stop_feed in adap:%i/dmx:%i\n",
  460. __func__, adap->dvb_adap.num, adap->dmxdev.dvbdev->id);
  461. adap->num_feeds = 1;
  462. }
  463. return pt3_start_streaming(adap);
  464. }
  465. static int pt3_stop_feed(struct dvb_demux_feed *feed)
  466. {
  467. struct pt3_adapter *adap;
  468. adap = container_of(feed->demux, struct pt3_adapter, demux);
  469. adap->num_feeds--;
  470. if (adap->num_feeds > 0 || !adap->thread)
  471. return 0;
  472. adap->num_feeds = 0;
  473. return pt3_stop_streaming(adap);
  474. }
  475. static int pt3_alloc_adapter(struct pt3_board *pt3, int index)
  476. {
  477. int ret;
  478. struct pt3_adapter *adap;
  479. struct dvb_adapter *da;
  480. adap = kzalloc(sizeof(*adap), GFP_KERNEL);
  481. if (!adap)
  482. return -ENOMEM;
  483. pt3->adaps[index] = adap;
  484. adap->adap_idx = index;
  485. if (index == 0 || !one_adapter) {
  486. ret = dvb_register_adapter(&adap->dvb_adap, "PT3 DVB",
  487. THIS_MODULE, &pt3->pdev->dev, adapter_nr);
  488. if (ret < 0) {
  489. dev_err(&pt3->pdev->dev,
  490. "failed to register adapter dev\n");
  491. goto err_mem;
  492. }
  493. da = &adap->dvb_adap;
  494. } else
  495. da = &pt3->adaps[0]->dvb_adap;
  496. adap->dvb_adap.priv = pt3;
  497. adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  498. adap->demux.priv = adap;
  499. adap->demux.feednum = 256;
  500. adap->demux.filternum = 256;
  501. adap->demux.start_feed = pt3_start_feed;
  502. adap->demux.stop_feed = pt3_stop_feed;
  503. ret = dvb_dmx_init(&adap->demux);
  504. if (ret < 0) {
  505. dev_err(&pt3->pdev->dev, "failed to init dmx dev\n");
  506. goto err_adap;
  507. }
  508. adap->dmxdev.filternum = 256;
  509. adap->dmxdev.demux = &adap->demux.dmx;
  510. ret = dvb_dmxdev_init(&adap->dmxdev, da);
  511. if (ret < 0) {
  512. dev_err(&pt3->pdev->dev, "failed to init dmxdev\n");
  513. goto err_demux;
  514. }
  515. ret = pt3_alloc_dmabuf(adap);
  516. if (ret) {
  517. dev_err(&pt3->pdev->dev, "failed to alloc DMA buffers\n");
  518. goto err_dmabuf;
  519. }
  520. return 0;
  521. err_dmabuf:
  522. pt3_free_dmabuf(adap);
  523. dvb_dmxdev_release(&adap->dmxdev);
  524. err_demux:
  525. dvb_dmx_release(&adap->demux);
  526. err_adap:
  527. if (index == 0 || !one_adapter)
  528. dvb_unregister_adapter(da);
  529. err_mem:
  530. kfree(adap);
  531. pt3->adaps[index] = NULL;
  532. return ret;
  533. }
  534. static void pt3_cleanup_adapter(struct pt3_board *pt3, int index)
  535. {
  536. struct pt3_adapter *adap;
  537. struct dmx_demux *dmx;
  538. adap = pt3->adaps[index];
  539. if (adap == NULL)
  540. return;
  541. /* stop demux kthread */
  542. if (adap->thread)
  543. pt3_stop_streaming(adap);
  544. dmx = &adap->demux.dmx;
  545. dmx->close(dmx);
  546. if (adap->fe) {
  547. adap->fe->callback = NULL;
  548. if (adap->fe->frontend_priv)
  549. dvb_unregister_frontend(adap->fe);
  550. if (adap->i2c_tuner) {
  551. module_put(adap->i2c_tuner->dev.driver->owner);
  552. i2c_unregister_device(adap->i2c_tuner);
  553. }
  554. if (adap->i2c_demod) {
  555. module_put(adap->i2c_demod->dev.driver->owner);
  556. i2c_unregister_device(adap->i2c_demod);
  557. }
  558. }
  559. pt3_free_dmabuf(adap);
  560. dvb_dmxdev_release(&adap->dmxdev);
  561. dvb_dmx_release(&adap->demux);
  562. if (index == 0 || !one_adapter)
  563. dvb_unregister_adapter(&adap->dvb_adap);
  564. kfree(adap);
  565. pt3->adaps[index] = NULL;
  566. }
  567. #ifdef CONFIG_PM_SLEEP
  568. static int pt3_suspend(struct device *dev)
  569. {
  570. struct pci_dev *pdev = to_pci_dev(dev);
  571. struct pt3_board *pt3 = pci_get_drvdata(pdev);
  572. int i;
  573. struct pt3_adapter *adap;
  574. for (i = 0; i < PT3_NUM_FE; i++) {
  575. adap = pt3->adaps[i];
  576. if (adap->num_feeds > 0)
  577. pt3_stop_dma(adap);
  578. dvb_frontend_suspend(adap->fe);
  579. pt3_free_dmabuf(adap);
  580. }
  581. pt3_lnb_ctrl(pt3, false);
  582. pt3_set_tuner_power(pt3, false, false);
  583. return 0;
  584. }
  585. static int pt3_resume(struct device *dev)
  586. {
  587. struct pci_dev *pdev = to_pci_dev(dev);
  588. struct pt3_board *pt3 = pci_get_drvdata(pdev);
  589. int i, ret;
  590. struct pt3_adapter *adap;
  591. ret = pt3_fe_init(pt3);
  592. if (ret)
  593. return ret;
  594. if (pt3->lna_on_cnt > 0)
  595. pt3_set_tuner_power(pt3, true, true);
  596. if (pt3->lnb_on_cnt > 0)
  597. pt3_lnb_ctrl(pt3, true);
  598. for (i = 0; i < PT3_NUM_FE; i++) {
  599. adap = pt3->adaps[i];
  600. dvb_frontend_resume(adap->fe);
  601. ret = pt3_alloc_dmabuf(adap);
  602. if (ret) {
  603. dev_err(&pt3->pdev->dev, "failed to alloc DMA bufs\n");
  604. continue;
  605. }
  606. if (adap->num_feeds > 0)
  607. pt3_start_dma(adap);
  608. }
  609. return 0;
  610. }
  611. #endif /* CONFIG_PM_SLEEP */
  612. static void pt3_remove(struct pci_dev *pdev)
  613. {
  614. struct pt3_board *pt3;
  615. int i;
  616. pt3 = pci_get_drvdata(pdev);
  617. for (i = PT3_NUM_FE - 1; i >= 0; i--)
  618. pt3_cleanup_adapter(pt3, i);
  619. i2c_del_adapter(&pt3->i2c_adap);
  620. kfree(pt3->i2c_buf);
  621. pci_iounmap(pt3->pdev, pt3->regs[0]);
  622. pci_iounmap(pt3->pdev, pt3->regs[1]);
  623. pci_release_regions(pdev);
  624. pci_disable_device(pdev);
  625. kfree(pt3);
  626. }
  627. static int pt3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  628. {
  629. u8 rev;
  630. u32 ver;
  631. int i, ret;
  632. struct pt3_board *pt3;
  633. struct i2c_adapter *i2c;
  634. if (pci_read_config_byte(pdev, PCI_REVISION_ID, &rev) || rev != 1)
  635. return -ENODEV;
  636. ret = pci_enable_device(pdev);
  637. if (ret < 0)
  638. return -ENODEV;
  639. pci_set_master(pdev);
  640. ret = pci_request_regions(pdev, DRV_NAME);
  641. if (ret < 0)
  642. goto err_disable_device;
  643. ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  644. if (ret == 0)
  645. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  646. else {
  647. ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  648. if (ret == 0)
  649. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  650. else {
  651. dev_err(&pdev->dev, "Failed to set DMA mask\n");
  652. goto err_release_regions;
  653. }
  654. dev_info(&pdev->dev, "Use 32bit DMA\n");
  655. }
  656. pt3 = kzalloc(sizeof(*pt3), GFP_KERNEL);
  657. if (!pt3) {
  658. ret = -ENOMEM;
  659. goto err_release_regions;
  660. }
  661. pci_set_drvdata(pdev, pt3);
  662. pt3->pdev = pdev;
  663. mutex_init(&pt3->lock);
  664. pt3->regs[0] = pci_ioremap_bar(pdev, 0);
  665. pt3->regs[1] = pci_ioremap_bar(pdev, 2);
  666. if (pt3->regs[0] == NULL || pt3->regs[1] == NULL) {
  667. dev_err(&pdev->dev, "Failed to ioremap\n");
  668. ret = -ENOMEM;
  669. goto err_kfree;
  670. }
  671. ver = ioread32(pt3->regs[0] + REG_VERSION);
  672. if ((ver >> 16) != 0x0301) {
  673. dev_warn(&pdev->dev, "PT%d, I/F-ver.:%d not supported\n",
  674. ver >> 24, (ver & 0x00ff0000) >> 16);
  675. ret = -ENODEV;
  676. goto err_iounmap;
  677. }
  678. pt3->num_bufs = clamp_val(num_bufs, MIN_DATA_BUFS, MAX_DATA_BUFS);
  679. pt3->i2c_buf = kmalloc(sizeof(*pt3->i2c_buf), GFP_KERNEL);
  680. if (pt3->i2c_buf == NULL) {
  681. ret = -ENOMEM;
  682. goto err_iounmap;
  683. }
  684. i2c = &pt3->i2c_adap;
  685. i2c->owner = THIS_MODULE;
  686. i2c->algo = &pt3_i2c_algo;
  687. i2c->algo_data = NULL;
  688. i2c->dev.parent = &pdev->dev;
  689. strlcpy(i2c->name, DRV_NAME, sizeof(i2c->name));
  690. i2c_set_adapdata(i2c, pt3);
  691. ret = i2c_add_adapter(i2c);
  692. if (ret < 0) {
  693. dev_err(&pdev->dev, "Failed to add i2c adapter\n");
  694. goto err_i2cbuf;
  695. }
  696. for (i = 0; i < PT3_NUM_FE; i++) {
  697. ret = pt3_alloc_adapter(pt3, i);
  698. if (ret < 0)
  699. break;
  700. ret = pt3_attach_fe(pt3, i);
  701. if (ret < 0)
  702. break;
  703. }
  704. if (i < PT3_NUM_FE) {
  705. dev_err(&pdev->dev, "Failed to create FE%d\n", i);
  706. goto err_cleanup_adapters;
  707. }
  708. ret = pt3_fe_init(pt3);
  709. if (ret < 0) {
  710. dev_err(&pdev->dev, "Failed to init frontends\n");
  711. i = PT3_NUM_FE - 1;
  712. goto err_cleanup_adapters;
  713. }
  714. dev_info(&pdev->dev,
  715. "successfully init'ed PT%d (fw:0x%02x, I/F:0x%02x)\n",
  716. ver >> 24, (ver >> 8) & 0xff, (ver >> 16) & 0xff);
  717. return 0;
  718. err_cleanup_adapters:
  719. while (i >= 0)
  720. pt3_cleanup_adapter(pt3, i--);
  721. i2c_del_adapter(i2c);
  722. err_i2cbuf:
  723. kfree(pt3->i2c_buf);
  724. err_iounmap:
  725. if (pt3->regs[0])
  726. pci_iounmap(pdev, pt3->regs[0]);
  727. if (pt3->regs[1])
  728. pci_iounmap(pdev, pt3->regs[1]);
  729. err_kfree:
  730. kfree(pt3);
  731. err_release_regions:
  732. pci_release_regions(pdev);
  733. err_disable_device:
  734. pci_disable_device(pdev);
  735. return ret;
  736. }
  737. static const struct pci_device_id pt3_id_table[] = {
  738. { PCI_DEVICE_SUB(0x1172, 0x4c15, 0xee8d, 0x0368) },
  739. { },
  740. };
  741. MODULE_DEVICE_TABLE(pci, pt3_id_table);
  742. static SIMPLE_DEV_PM_OPS(pt3_pm_ops, pt3_suspend, pt3_resume);
  743. static struct pci_driver pt3_driver = {
  744. .name = DRV_NAME,
  745. .probe = pt3_probe,
  746. .remove = pt3_remove,
  747. .id_table = pt3_id_table,
  748. .driver.pm = &pt3_pm_ops,
  749. };
  750. module_pci_driver(pt3_driver);
  751. MODULE_DESCRIPTION("Earthsoft PT3 Driver");
  752. MODULE_AUTHOR("Akihiro TSUKADA");
  753. MODULE_LICENSE("GPL");