cimax2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * cimax2.c
  3. *
  4. * CIMax2(R) SP2 driver in conjunction with NetUp Dual DVB-S2 CI card
  5. *
  6. * Copyright (C) 2009 NetUP Inc.
  7. * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
  8. * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. *
  19. * GNU General Public License for more details.
  20. */
  21. #include "cx23885.h"
  22. #include "cimax2.h"
  23. #include "dvb_ca_en50221.h"
  24. /* Max transfer size done by I2C transfer functions */
  25. #define MAX_XFER_SIZE 64
  26. /**** Bit definitions for MC417_RWD and MC417_OEN registers ***
  27. bits 31-16
  28. +-----------+
  29. | Reserved |
  30. +-----------+
  31. bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
  32. +-------+-------+-------+-------+-------+-------+-------+-------+
  33. | WR# | RD# | | ACK# | ADHI | ADLO | CS1# | CS0# |
  34. +-------+-------+-------+-------+-------+-------+-------+-------+
  35. bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
  36. +-------+-------+-------+-------+-------+-------+-------+-------+
  37. | DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0|
  38. +-------+-------+-------+-------+-------+-------+-------+-------+
  39. ***/
  40. /* MC417 */
  41. #define NETUP_DATA 0x000000ff
  42. #define NETUP_WR 0x00008000
  43. #define NETUP_RD 0x00004000
  44. #define NETUP_ACK 0x00001000
  45. #define NETUP_ADHI 0x00000800
  46. #define NETUP_ADLO 0x00000400
  47. #define NETUP_CS1 0x00000200
  48. #define NETUP_CS0 0x00000100
  49. #define NETUP_EN_ALL 0x00001000
  50. #define NETUP_CTRL_OFF (NETUP_CS1 | NETUP_CS0 | NETUP_WR | NETUP_RD)
  51. #define NETUP_CI_CTL 0x04
  52. #define NETUP_CI_RD 1
  53. #define NETUP_IRQ_DETAM 0x1
  54. #define NETUP_IRQ_IRQAM 0x4
  55. static unsigned int ci_dbg;
  56. module_param(ci_dbg, int, 0644);
  57. MODULE_PARM_DESC(ci_dbg, "Enable CI debugging");
  58. static unsigned int ci_irq_enable;
  59. module_param(ci_irq_enable, int, 0644);
  60. MODULE_PARM_DESC(ci_irq_enable, "Enable IRQ from CAM");
  61. #define ci_dbg_print(args...) \
  62. do { \
  63. if (ci_dbg) \
  64. printk(KERN_DEBUG args); \
  65. } while (0)
  66. #define ci_irq_flags() (ci_irq_enable ? NETUP_IRQ_IRQAM : 0)
  67. /* stores all private variables for communication with CI */
  68. struct netup_ci_state {
  69. struct dvb_ca_en50221 ca;
  70. struct mutex ca_mutex;
  71. struct i2c_adapter *i2c_adap;
  72. u8 ci_i2c_addr;
  73. int status;
  74. struct work_struct work;
  75. void *priv;
  76. u8 current_irq_mode;
  77. int current_ci_flag;
  78. unsigned long next_status_checked_time;
  79. };
  80. static int netup_read_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg,
  81. u8 *buf, int len)
  82. {
  83. int ret;
  84. struct i2c_msg msg[] = {
  85. {
  86. .addr = addr,
  87. .flags = 0,
  88. .buf = &reg,
  89. .len = 1
  90. }, {
  91. .addr = addr,
  92. .flags = I2C_M_RD,
  93. .buf = buf,
  94. .len = len
  95. }
  96. };
  97. ret = i2c_transfer(i2c_adap, msg, 2);
  98. if (ret != 2) {
  99. ci_dbg_print("%s: i2c read error, Reg = 0x%02x, Status = %d\n",
  100. __func__, reg, ret);
  101. return -1;
  102. }
  103. ci_dbg_print("%s: i2c read Addr=0x%04x, Reg = 0x%02x, data = %02x\n",
  104. __func__, addr, reg, buf[0]);
  105. return 0;
  106. }
  107. static int netup_write_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg,
  108. u8 *buf, int len)
  109. {
  110. int ret;
  111. u8 buffer[MAX_XFER_SIZE];
  112. struct i2c_msg msg = {
  113. .addr = addr,
  114. .flags = 0,
  115. .buf = &buffer[0],
  116. .len = len + 1
  117. };
  118. if (1 + len > sizeof(buffer)) {
  119. printk(KERN_WARNING
  120. "%s: i2c wr reg=%04x: len=%d is too big!\n",
  121. KBUILD_MODNAME, reg, len);
  122. return -EINVAL;
  123. }
  124. buffer[0] = reg;
  125. memcpy(&buffer[1], buf, len);
  126. ret = i2c_transfer(i2c_adap, &msg, 1);
  127. if (ret != 1) {
  128. ci_dbg_print("%s: i2c write error, Reg=[0x%02x], Status=%d\n",
  129. __func__, reg, ret);
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. static int netup_ci_get_mem(struct cx23885_dev *dev)
  135. {
  136. int mem;
  137. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  138. for (;;) {
  139. mem = cx_read(MC417_RWD);
  140. if ((mem & NETUP_ACK) == 0)
  141. break;
  142. if (time_after(jiffies, timeout))
  143. break;
  144. udelay(1);
  145. }
  146. cx_set(MC417_RWD, NETUP_CTRL_OFF);
  147. return mem & 0xff;
  148. }
  149. static int netup_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot,
  150. u8 flag, u8 read, int addr, u8 data)
  151. {
  152. struct netup_ci_state *state = en50221->data;
  153. struct cx23885_tsport *port = state->priv;
  154. struct cx23885_dev *dev = port->dev;
  155. u8 store;
  156. int mem;
  157. int ret;
  158. if (0 != slot)
  159. return -EINVAL;
  160. if (state->current_ci_flag != flag) {
  161. ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,
  162. 0, &store, 1);
  163. if (ret != 0)
  164. return ret;
  165. store &= ~0x0c;
  166. store |= flag;
  167. ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  168. 0, &store, 1);
  169. if (ret != 0)
  170. return ret;
  171. }
  172. state->current_ci_flag = flag;
  173. mutex_lock(&dev->gpio_lock);
  174. /* write addr */
  175. cx_write(MC417_OEN, NETUP_EN_ALL);
  176. cx_write(MC417_RWD, NETUP_CTRL_OFF |
  177. NETUP_ADLO | (0xff & addr));
  178. cx_clear(MC417_RWD, NETUP_ADLO);
  179. cx_write(MC417_RWD, NETUP_CTRL_OFF |
  180. NETUP_ADHI | (0xff & (addr >> 8)));
  181. cx_clear(MC417_RWD, NETUP_ADHI);
  182. if (read) { /* data in */
  183. cx_write(MC417_OEN, NETUP_EN_ALL | NETUP_DATA);
  184. } else /* data out */
  185. cx_write(MC417_RWD, NETUP_CTRL_OFF | data);
  186. /* choose chip */
  187. cx_clear(MC417_RWD,
  188. (state->ci_i2c_addr == 0x40) ? NETUP_CS0 : NETUP_CS1);
  189. /* read/write */
  190. cx_clear(MC417_RWD, (read) ? NETUP_RD : NETUP_WR);
  191. mem = netup_ci_get_mem(dev);
  192. mutex_unlock(&dev->gpio_lock);
  193. if (!read)
  194. if (mem < 0)
  195. return -EREMOTEIO;
  196. ci_dbg_print("%s: %s: chipaddr=[0x%x] addr=[0x%02x], %s=%x\n", __func__,
  197. (read) ? "read" : "write", state->ci_i2c_addr, addr,
  198. (flag == NETUP_CI_CTL) ? "ctl" : "mem",
  199. (read) ? mem : data);
  200. if (read)
  201. return mem;
  202. return 0;
  203. }
  204. int netup_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
  205. int slot, int addr)
  206. {
  207. return netup_ci_op_cam(en50221, slot, 0, NETUP_CI_RD, addr, 0);
  208. }
  209. int netup_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
  210. int slot, int addr, u8 data)
  211. {
  212. return netup_ci_op_cam(en50221, slot, 0, 0, addr, data);
  213. }
  214. int netup_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,
  215. u8 addr)
  216. {
  217. return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL,
  218. NETUP_CI_RD, addr, 0);
  219. }
  220. int netup_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,
  221. u8 addr, u8 data)
  222. {
  223. return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL, 0, addr, data);
  224. }
  225. int netup_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
  226. {
  227. struct netup_ci_state *state = en50221->data;
  228. u8 buf = 0x80;
  229. int ret;
  230. if (0 != slot)
  231. return -EINVAL;
  232. udelay(500);
  233. ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  234. 0, &buf, 1);
  235. if (ret != 0)
  236. return ret;
  237. udelay(500);
  238. buf = 0x00;
  239. ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  240. 0, &buf, 1);
  241. msleep(1000);
  242. dvb_ca_en50221_camready_irq(&state->ca, 0);
  243. return 0;
  244. }
  245. int netup_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
  246. {
  247. /* not implemented */
  248. return 0;
  249. }
  250. static int netup_ci_set_irq(struct dvb_ca_en50221 *en50221, u8 irq_mode)
  251. {
  252. struct netup_ci_state *state = en50221->data;
  253. int ret;
  254. if (irq_mode == state->current_irq_mode)
  255. return 0;
  256. ci_dbg_print("%s: chipaddr=[0x%x] setting ci IRQ to [0x%x] \n",
  257. __func__, state->ci_i2c_addr, irq_mode);
  258. ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  259. 0x1b, &irq_mode, 1);
  260. if (ret != 0)
  261. return ret;
  262. state->current_irq_mode = irq_mode;
  263. return 0;
  264. }
  265. int netup_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot)
  266. {
  267. struct netup_ci_state *state = en50221->data;
  268. u8 buf;
  269. if (0 != slot)
  270. return -EINVAL;
  271. netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,
  272. 0, &buf, 1);
  273. buf |= 0x60;
  274. return netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  275. 0, &buf, 1);
  276. }
  277. /* work handler */
  278. static void netup_read_ci_status(struct work_struct *work)
  279. {
  280. struct netup_ci_state *state =
  281. container_of(work, struct netup_ci_state, work);
  282. u8 buf[33];
  283. int ret;
  284. /* CAM module IRQ processing. fast operation */
  285. dvb_ca_en50221_frda_irq(&state->ca, 0);
  286. /* CAM module INSERT/REMOVE processing. slow operation because of i2c
  287. * transfers */
  288. if (time_after(jiffies, state->next_status_checked_time)
  289. || !state->status) {
  290. ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,
  291. 0, &buf[0], 33);
  292. state->next_status_checked_time = jiffies
  293. + msecs_to_jiffies(1000);
  294. if (ret != 0)
  295. return;
  296. ci_dbg_print("%s: Slot Status Addr=[0x%04x], "
  297. "Reg=[0x%02x], data=%02x, "
  298. "TS config = %02x\n", __func__,
  299. state->ci_i2c_addr, 0, buf[0],
  300. buf[0]);
  301. if (buf[0] & 1)
  302. state->status = DVB_CA_EN50221_POLL_CAM_PRESENT |
  303. DVB_CA_EN50221_POLL_CAM_READY;
  304. else
  305. state->status = 0;
  306. }
  307. }
  308. /* CI irq handler */
  309. int netup_ci_slot_status(struct cx23885_dev *dev, u32 pci_status)
  310. {
  311. struct cx23885_tsport *port = NULL;
  312. struct netup_ci_state *state = NULL;
  313. ci_dbg_print("%s:\n", __func__);
  314. if (0 == (pci_status & (PCI_MSK_GPIO0 | PCI_MSK_GPIO1)))
  315. return 0;
  316. if (pci_status & PCI_MSK_GPIO0) {
  317. port = &dev->ts1;
  318. state = port->port_priv;
  319. schedule_work(&state->work);
  320. ci_dbg_print("%s: Wakeup CI0\n", __func__);
  321. }
  322. if (pci_status & PCI_MSK_GPIO1) {
  323. port = &dev->ts2;
  324. state = port->port_priv;
  325. schedule_work(&state->work);
  326. ci_dbg_print("%s: Wakeup CI1\n", __func__);
  327. }
  328. return 1;
  329. }
  330. int netup_poll_ci_slot_status(struct dvb_ca_en50221 *en50221,
  331. int slot, int open)
  332. {
  333. struct netup_ci_state *state = en50221->data;
  334. if (0 != slot)
  335. return -EINVAL;
  336. netup_ci_set_irq(en50221, open ? (NETUP_IRQ_DETAM | ci_irq_flags())
  337. : NETUP_IRQ_DETAM);
  338. return state->status;
  339. }
  340. int netup_ci_init(struct cx23885_tsport *port)
  341. {
  342. struct netup_ci_state *state;
  343. u8 cimax_init[34] = {
  344. 0x00, /* module A control*/
  345. 0x00, /* auto select mask high A */
  346. 0x00, /* auto select mask low A */
  347. 0x00, /* auto select pattern high A */
  348. 0x00, /* auto select pattern low A */
  349. 0x44, /* memory access time A */
  350. 0x00, /* invert input A */
  351. 0x00, /* RFU */
  352. 0x00, /* RFU */
  353. 0x00, /* module B control*/
  354. 0x00, /* auto select mask high B */
  355. 0x00, /* auto select mask low B */
  356. 0x00, /* auto select pattern high B */
  357. 0x00, /* auto select pattern low B */
  358. 0x44, /* memory access time B */
  359. 0x00, /* invert input B */
  360. 0x00, /* RFU */
  361. 0x00, /* RFU */
  362. 0x00, /* auto select mask high Ext */
  363. 0x00, /* auto select mask low Ext */
  364. 0x00, /* auto select pattern high Ext */
  365. 0x00, /* auto select pattern low Ext */
  366. 0x00, /* RFU */
  367. 0x02, /* destination - module A */
  368. 0x01, /* power on (use it like store place) */
  369. 0x00, /* RFU */
  370. 0x00, /* int status read only */
  371. ci_irq_flags() | NETUP_IRQ_DETAM, /* DETAM, IRQAM unmasked */
  372. 0x05, /* EXTINT=active-high, INT=push-pull */
  373. 0x00, /* USCG1 */
  374. 0x04, /* ack active low */
  375. 0x00, /* LOCK = 0 */
  376. 0x33, /* serial mode, rising in, rising out, MSB first*/
  377. 0x31, /* synchronization */
  378. };
  379. int ret;
  380. ci_dbg_print("%s\n", __func__);
  381. state = kzalloc(sizeof(struct netup_ci_state), GFP_KERNEL);
  382. if (!state) {
  383. ci_dbg_print("%s: Unable create CI structure!\n", __func__);
  384. ret = -ENOMEM;
  385. goto err;
  386. }
  387. port->port_priv = state;
  388. switch (port->nr) {
  389. case 1:
  390. state->ci_i2c_addr = 0x40;
  391. break;
  392. case 2:
  393. state->ci_i2c_addr = 0x41;
  394. break;
  395. }
  396. state->i2c_adap = &port->dev->i2c_bus[0].i2c_adap;
  397. state->ca.owner = THIS_MODULE;
  398. state->ca.read_attribute_mem = netup_ci_read_attribute_mem;
  399. state->ca.write_attribute_mem = netup_ci_write_attribute_mem;
  400. state->ca.read_cam_control = netup_ci_read_cam_ctl;
  401. state->ca.write_cam_control = netup_ci_write_cam_ctl;
  402. state->ca.slot_reset = netup_ci_slot_reset;
  403. state->ca.slot_shutdown = netup_ci_slot_shutdown;
  404. state->ca.slot_ts_enable = netup_ci_slot_ts_ctl;
  405. state->ca.poll_slot_status = netup_poll_ci_slot_status;
  406. state->ca.data = state;
  407. state->priv = port;
  408. state->current_irq_mode = ci_irq_flags() | NETUP_IRQ_DETAM;
  409. ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  410. 0, &cimax_init[0], 34);
  411. /* lock registers */
  412. ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  413. 0x1f, &cimax_init[0x18], 1);
  414. /* power on slots */
  415. ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
  416. 0x18, &cimax_init[0x18], 1);
  417. if (0 != ret)
  418. goto err;
  419. ret = dvb_ca_en50221_init(&port->frontends.adapter,
  420. &state->ca,
  421. /* flags */ 0,
  422. /* n_slots */ 1);
  423. if (0 != ret)
  424. goto err;
  425. INIT_WORK(&state->work, netup_read_ci_status);
  426. schedule_work(&state->work);
  427. ci_dbg_print("%s: CI initialized!\n", __func__);
  428. return 0;
  429. err:
  430. ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret);
  431. kfree(state);
  432. return ret;
  433. }
  434. void netup_ci_exit(struct cx23885_tsport *port)
  435. {
  436. struct netup_ci_state *state;
  437. if (NULL == port)
  438. return;
  439. state = (struct netup_ci_state *)port->port_priv;
  440. if (NULL == state)
  441. return;
  442. if (NULL == state->ca.data)
  443. return;
  444. dvb_ca_en50221_release(&state->ca);
  445. kfree(state);
  446. }