cimax2.c 13 KB

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