PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/video/cx23885/cimax2.c

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