1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * CI driver in conjunction with NetUp Dual DVB-T/C RF CI card
7 * Copyright (C) 2010,2011 NetUP Inc.
8 * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
12 * currently cx23885 GPIO's used.
15 * GPIO-2 ~reset chips out
16 * GPIO-3 to GPIO-10 data/addr for CA in/out
27 * Bit definitions for MC417_RWD and MC417_OEN registers
32 * bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
33 * +-------+-------+-------+-------+-------+-------+-------+-------+
34 * | TDI | TDO | TCK | RDY# | #RD | #WR | AD_RG | #CS |
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 * +-------+-------+-------+-------+-------+-------+-------+-------+
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 #include <media/dvb_demux.h>
45 #include <media/dvb_frontend.h>
46 #include "altera-ci.h"
47 #include <media/dvb_ca_en50221.h>
50 #define NETUP_CI_INT_CTRL 0x00
51 #define NETUP_CI_BUSCTRL2 0x01
52 #define NETUP_CI_ADDR0 0x04
53 #define NETUP_CI_ADDR1 0x05
54 #define NETUP_CI_DATA 0x06
55 #define NETUP_CI_BUSCTRL 0x07
56 #define NETUP_CI_PID_ADDR0 0x08
57 #define NETUP_CI_PID_ADDR1 0x09
58 #define NETUP_CI_PID_DATA 0x0a
59 #define NETUP_CI_TSA_DIV 0x0c
60 #define NETUP_CI_TSB_DIV 0x0d
61 #define NETUP_CI_REVISION 0x0f
64 #define NETUP_CI_FLG_CTL 1
65 #define NETUP_CI_FLG_RD 1
66 #define NETUP_CI_FLG_AD 1
68 static unsigned int ci_dbg;
69 module_param(ci_dbg, int, 0644);
70 MODULE_PARM_DESC(ci_dbg, "Enable CI debugging");
72 static unsigned int pid_dbg;
73 module_param(pid_dbg, int, 0644);
74 MODULE_PARM_DESC(pid_dbg, "Enable PID filtering debugging");
76 MODULE_DESCRIPTION("altera FPGA CI module");
77 MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
78 MODULE_LICENSE("GPL");
80 #define ci_dbg_print(fmt, args...) \
83 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
87 #define pid_dbg_print(fmt, args...) \
90 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
94 struct altera_ci_state;
95 struct netup_hw_pid_filter;
97 struct fpga_internal {
99 struct mutex fpga_mutex;/* two CI's on the same fpga */
100 struct netup_hw_pid_filter *pid_filt[2];
101 struct altera_ci_state *state[2];
102 struct work_struct work;
103 int (*fpga_rw) (void *dev, int flag, int data, int rw);
109 /* stores all private variables for communication with CI */
110 struct altera_ci_state {
111 struct fpga_internal *internal;
112 struct dvb_ca_en50221 ca;
117 /* stores all private variables for hardware pid filtering */
118 struct netup_hw_pid_filter {
119 struct fpga_internal *internal;
120 struct dvb_demux *demux;
121 /* save old functions */
122 int (*start_feed)(struct dvb_demux_feed *feed);
123 int (*stop_feed)(struct dvb_demux_feed *feed);
129 /* internal params node */
131 /* pointer for internal params, one for each pair of CI's */
132 struct fpga_internal *internal;
133 struct fpga_inode *next_inode;
136 /* first internal params */
137 static struct fpga_inode *fpga_first_inode;
139 /* find chip by dev */
140 static struct fpga_inode *find_inode(void *dev)
142 struct fpga_inode *temp_chip = fpga_first_inode;
144 if (temp_chip == NULL)
148 Search for the last fpga CI chip or
150 while ((temp_chip != NULL) &&
151 (temp_chip->internal->dev != dev))
152 temp_chip = temp_chip->next_inode;
157 static struct fpga_internal *check_filter(struct fpga_internal *temp_int,
158 void *demux_dev, int filt_nr)
160 if (temp_int == NULL)
163 if ((temp_int->pid_filt[filt_nr]) == NULL)
166 if (temp_int->pid_filt[filt_nr]->demux == demux_dev)
172 /* find chip by demux */
173 static struct fpga_inode *find_dinode(void *demux_dev)
175 struct fpga_inode *temp_chip = fpga_first_inode;
176 struct fpga_internal *temp_int;
179 * Search of the last fpga CI chip or
182 while (temp_chip != NULL) {
183 if (temp_chip->internal != NULL) {
184 temp_int = temp_chip->internal;
185 if (check_filter(temp_int, demux_dev, 0))
187 if (check_filter(temp_int, demux_dev, 1))
191 temp_chip = temp_chip->next_inode;
197 /* deallocating chip */
198 static void remove_inode(struct fpga_internal *internal)
200 struct fpga_inode *prev_node = fpga_first_inode;
201 struct fpga_inode *del_node = find_inode(internal->dev);
203 if (del_node != NULL) {
204 if (del_node == fpga_first_inode) {
205 fpga_first_inode = del_node->next_inode;
207 while (prev_node->next_inode != del_node)
208 prev_node = prev_node->next_inode;
210 if (del_node->next_inode == NULL)
211 prev_node->next_inode = NULL;
213 prev_node->next_inode =
214 prev_node->next_inode->next_inode;
221 /* allocating new chip */
222 static struct fpga_inode *append_internal(struct fpga_internal *internal)
224 struct fpga_inode *new_node = fpga_first_inode;
226 if (new_node == NULL) {
227 new_node = kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
228 fpga_first_inode = new_node;
230 while (new_node->next_inode != NULL)
231 new_node = new_node->next_inode;
233 new_node->next_inode =
234 kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
235 if (new_node->next_inode != NULL)
236 new_node = new_node->next_inode;
241 if (new_node != NULL) {
242 new_node->internal = internal;
243 new_node->next_inode = NULL;
249 static int netup_fpga_op_rw(struct fpga_internal *inter, int addr,
252 inter->fpga_rw(inter->dev, NETUP_CI_FLG_AD, addr, 0);
253 return inter->fpga_rw(inter->dev, 0, val, read);
256 /* flag - mem/io, read - read/write */
257 static int altera_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot,
258 u8 flag, u8 read, int addr, u8 val)
261 struct altera_ci_state *state = en50221->data;
262 struct fpga_internal *inter = state->internal;
270 mutex_lock(&inter->fpga_mutex);
272 netup_fpga_op_rw(inter, NETUP_CI_ADDR0, ((addr << 1) & 0xfe), 0);
273 netup_fpga_op_rw(inter, NETUP_CI_ADDR1, ((addr >> 7) & 0x7f), 0);
274 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
277 store |= ((state->nr << 7) | (flag << 6));
279 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, store, 0);
280 mem = netup_fpga_op_rw(inter, NETUP_CI_DATA, val, read);
282 mutex_unlock(&inter->fpga_mutex);
284 ci_dbg_print("%s: %s: addr=[0x%02x], %s=%x\n", __func__,
285 (read) ? "read" : "write", addr,
286 (flag == NETUP_CI_FLG_CTL) ? "ctl" : "mem",
292 static int altera_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
295 return altera_ci_op_cam(en50221, slot, 0, NETUP_CI_FLG_RD, addr, 0);
298 static int altera_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
299 int slot, int addr, u8 data)
301 return altera_ci_op_cam(en50221, slot, 0, 0, addr, data);
304 static int altera_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221,
307 return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL,
308 NETUP_CI_FLG_RD, addr, 0);
311 static int altera_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,
314 return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL, 0, addr, data);
317 static int altera_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
319 struct altera_ci_state *state = en50221->data;
320 struct fpga_internal *inter = state->internal;
321 /* reasonable timeout for CI reset is 10 seconds */
322 unsigned long t_out = jiffies + msecs_to_jiffies(9999);
325 ci_dbg_print("%s\n", __func__);
330 mutex_lock(&inter->fpga_mutex);
332 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
333 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
334 (ret & 0xcf) | (1 << (5 - state->nr)), 0);
336 mutex_unlock(&inter->fpga_mutex);
341 mutex_lock(&inter->fpga_mutex);
343 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
345 mutex_unlock(&inter->fpga_mutex);
347 if ((ret & (1 << (5 - state->nr))) == 0)
349 if (time_after(jiffies, t_out))
354 ci_dbg_print("%s: %d msecs\n", __func__,
355 jiffies_to_msecs(jiffies + msecs_to_jiffies(9999) - t_out));
360 static int altera_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
362 /* not implemented */
366 static int altera_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot)
368 struct altera_ci_state *state = en50221->data;
369 struct fpga_internal *inter = state->internal;
372 ci_dbg_print("%s\n", __func__);
377 mutex_lock(&inter->fpga_mutex);
379 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
380 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
381 (ret & 0x0f) | (1 << (3 - state->nr)), 0);
383 mutex_unlock(&inter->fpga_mutex);
389 static void netup_read_ci_status(struct work_struct *work)
391 struct fpga_internal *inter =
392 container_of(work, struct fpga_internal, work);
395 ci_dbg_print("%s\n", __func__);
397 mutex_lock(&inter->fpga_mutex);
399 ret = netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0, NETUP_CI_FLG_RD);
400 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
402 mutex_unlock(&inter->fpga_mutex);
404 if (inter->state[1] != NULL) {
405 inter->state[1]->status =
407 DVB_CA_EN50221_POLL_CAM_PRESENT |
408 DVB_CA_EN50221_POLL_CAM_READY : 0);
409 ci_dbg_print("%s: setting CI[1] status = 0x%x\n",
410 __func__, inter->state[1]->status);
413 if (inter->state[0] != NULL) {
414 inter->state[0]->status =
416 DVB_CA_EN50221_POLL_CAM_PRESENT |
417 DVB_CA_EN50221_POLL_CAM_READY : 0);
418 ci_dbg_print("%s: setting CI[0] status = 0x%x\n",
419 __func__, inter->state[0]->status);
424 int altera_ci_irq(void *dev)
426 struct fpga_inode *temp_int = NULL;
427 struct fpga_internal *inter = NULL;
429 ci_dbg_print("%s\n", __func__);
432 temp_int = find_inode(dev);
433 if (temp_int != NULL) {
434 inter = temp_int->internal;
435 schedule_work(&inter->work);
441 EXPORT_SYMBOL(altera_ci_irq);
443 static int altera_poll_ci_slot_status(struct dvb_ca_en50221 *en50221,
446 struct altera_ci_state *state = en50221->data;
451 return state->status;
454 static void altera_hw_filt_release(void *main_dev, int filt_nr)
456 struct fpga_inode *temp_int = find_inode(main_dev);
457 struct netup_hw_pid_filter *pid_filt = NULL;
459 ci_dbg_print("%s\n", __func__);
461 if (temp_int != NULL) {
462 pid_filt = temp_int->internal->pid_filt[filt_nr - 1];
463 /* stored old feed controls */
464 pid_filt->demux->start_feed = pid_filt->start_feed;
465 pid_filt->demux->stop_feed = pid_filt->stop_feed;
467 if (((--(temp_int->internal->filts_used)) <= 0) &&
468 ((temp_int->internal->cis_used) <= 0)) {
470 ci_dbg_print("%s: Actually removing\n", __func__);
472 remove_inode(temp_int->internal);
473 kfree(pid_filt->internal);
482 void altera_ci_release(void *dev, int ci_nr)
484 struct fpga_inode *temp_int = find_inode(dev);
485 struct altera_ci_state *state = NULL;
487 ci_dbg_print("%s\n", __func__);
489 if (temp_int != NULL) {
490 state = temp_int->internal->state[ci_nr - 1];
491 altera_hw_filt_release(dev, ci_nr);
494 if (((temp_int->internal->filts_used) <= 0) &&
495 ((--(temp_int->internal->cis_used)) <= 0)) {
497 ci_dbg_print("%s: Actually removing\n", __func__);
499 remove_inode(temp_int->internal);
500 kfree(state->internal);
504 if (state->ca.data != NULL)
505 dvb_ca_en50221_release(&state->ca);
512 EXPORT_SYMBOL(altera_ci_release);
514 static void altera_pid_control(struct netup_hw_pid_filter *pid_filt,
517 struct fpga_internal *inter = pid_filt->internal;
520 /* pid 0-0x1f always enabled, don't touch them */
521 if ((pid == 0x2000) || (pid < 0x20))
524 mutex_lock(&inter->fpga_mutex);
526 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, (pid >> 3) & 0xff, 0);
527 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
528 ((pid >> 11) & 0x03) | (pid_filt->nr << 2), 0);
530 store = netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, 0, NETUP_CI_FLG_RD);
532 if (onoff)/* 0 - on, 1 - off */
533 store |= (1 << (pid & 7));
535 store &= ~(1 << (pid & 7));
537 netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, store, 0);
539 mutex_unlock(&inter->fpga_mutex);
541 pid_dbg_print("%s: (%d) set pid: %5d 0x%04x '%s'\n", __func__,
542 pid_filt->nr, pid, pid, onoff ? "off" : "on");
545 static void altera_toggle_fullts_streaming(struct netup_hw_pid_filter *pid_filt,
546 int filt_nr, int onoff)
548 struct fpga_internal *inter = pid_filt->internal;
552 pid_dbg_print("%s: pid_filt->nr[%d] now %s\n", __func__, pid_filt->nr,
553 onoff ? "off" : "on");
555 if (onoff)/* 0 - on, 1 - off */
556 store = 0xff;/* ignore pid */
558 store = 0;/* enable pid */
560 mutex_lock(&inter->fpga_mutex);
562 for (i = 0; i < 1024; i++) {
563 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, i & 0xff, 0);
565 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
566 ((i >> 8) & 0x03) | (pid_filt->nr << 2), 0);
567 /* pid 0-0x1f always enabled */
568 netup_fpga_op_rw(inter, NETUP_CI_PID_DATA,
569 (i > 3 ? store : 0), 0);
572 mutex_unlock(&inter->fpga_mutex);
575 static int altera_pid_feed_control(void *demux_dev, int filt_nr,
576 struct dvb_demux_feed *feed, int onoff)
578 struct fpga_inode *temp_int = find_dinode(demux_dev);
579 struct fpga_internal *inter = temp_int->internal;
580 struct netup_hw_pid_filter *pid_filt = inter->pid_filt[filt_nr - 1];
582 altera_pid_control(pid_filt, feed->pid, onoff ? 0 : 1);
583 /* call old feed proc's */
585 pid_filt->start_feed(feed);
587 pid_filt->stop_feed(feed);
589 if (feed->pid == 0x2000)
590 altera_toggle_fullts_streaming(pid_filt, filt_nr,
596 static int altera_ci_start_feed(struct dvb_demux_feed *feed, int num)
598 altera_pid_feed_control(feed->demux, num, feed, 1);
603 static int altera_ci_stop_feed(struct dvb_demux_feed *feed, int num)
605 altera_pid_feed_control(feed->demux, num, feed, 0);
610 static int altera_ci_start_feed_1(struct dvb_demux_feed *feed)
612 return altera_ci_start_feed(feed, 1);
615 static int altera_ci_stop_feed_1(struct dvb_demux_feed *feed)
617 return altera_ci_stop_feed(feed, 1);
620 static int altera_ci_start_feed_2(struct dvb_demux_feed *feed)
622 return altera_ci_start_feed(feed, 2);
625 static int altera_ci_stop_feed_2(struct dvb_demux_feed *feed)
627 return altera_ci_stop_feed(feed, 2);
630 static int altera_hw_filt_init(struct altera_ci_config *config, int hw_filt_nr)
632 struct netup_hw_pid_filter *pid_filt = NULL;
633 struct fpga_inode *temp_int = find_inode(config->dev);
634 struct fpga_internal *inter = NULL;
637 pid_filt = kzalloc(sizeof(struct netup_hw_pid_filter), GFP_KERNEL);
639 ci_dbg_print("%s\n", __func__);
646 if (temp_int != NULL) {
647 inter = temp_int->internal;
648 (inter->filts_used)++;
649 ci_dbg_print("%s: Find Internal Structure!\n", __func__);
651 inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
657 temp_int = append_internal(inter);
662 inter->filts_used = 1;
663 inter->dev = config->dev;
664 inter->fpga_rw = config->fpga_rw;
665 mutex_init(&inter->fpga_mutex);
667 ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
670 ci_dbg_print("%s: setting hw pid filter = %p for ci = %d\n", __func__,
671 pid_filt, hw_filt_nr - 1);
672 inter->pid_filt[hw_filt_nr - 1] = pid_filt;
673 pid_filt->demux = config->demux;
674 pid_filt->internal = inter;
675 pid_filt->nr = hw_filt_nr - 1;
676 /* store old feed controls */
677 pid_filt->start_feed = config->demux->start_feed;
678 pid_filt->stop_feed = config->demux->stop_feed;
679 /* replace with new feed controls */
680 if (hw_filt_nr == 1) {
681 pid_filt->demux->start_feed = altera_ci_start_feed_1;
682 pid_filt->demux->stop_feed = altera_ci_stop_feed_1;
683 } else if (hw_filt_nr == 2) {
684 pid_filt->demux->start_feed = altera_ci_start_feed_2;
685 pid_filt->demux->stop_feed = altera_ci_stop_feed_2;
688 altera_toggle_fullts_streaming(pid_filt, 0, 1);
692 ci_dbg_print("%s: Can't init hardware filter: Error %d\n",
701 int altera_ci_init(struct altera_ci_config *config, int ci_nr)
703 struct altera_ci_state *state;
704 struct fpga_inode *temp_int = find_inode(config->dev);
705 struct fpga_internal *inter = NULL;
709 state = kzalloc(sizeof(struct altera_ci_state), GFP_KERNEL);
711 ci_dbg_print("%s\n", __func__);
718 if (temp_int != NULL) {
719 inter = temp_int->internal;
721 inter->fpga_rw = config->fpga_rw;
722 ci_dbg_print("%s: Find Internal Structure!\n", __func__);
724 inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
730 temp_int = append_internal(inter);
736 inter->dev = config->dev;
737 inter->fpga_rw = config->fpga_rw;
738 mutex_init(&inter->fpga_mutex);
740 ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
743 ci_dbg_print("%s: setting state = %p for ci = %d\n", __func__,
745 state->internal = inter;
746 state->nr = ci_nr - 1;
748 state->ca.owner = THIS_MODULE;
749 state->ca.read_attribute_mem = altera_ci_read_attribute_mem;
750 state->ca.write_attribute_mem = altera_ci_write_attribute_mem;
751 state->ca.read_cam_control = altera_ci_read_cam_ctl;
752 state->ca.write_cam_control = altera_ci_write_cam_ctl;
753 state->ca.slot_reset = altera_ci_slot_reset;
754 state->ca.slot_shutdown = altera_ci_slot_shutdown;
755 state->ca.slot_ts_enable = altera_ci_slot_ts_ctl;
756 state->ca.poll_slot_status = altera_poll_ci_slot_status;
757 state->ca.data = state;
759 ret = dvb_ca_en50221_init(config->adapter,
766 inter->state[ci_nr - 1] = state;
768 altera_hw_filt_init(config, ci_nr);
770 if (inter->strt_wrk) {
771 INIT_WORK(&inter->work, netup_read_ci_status);
775 ci_dbg_print("%s: CI initialized!\n", __func__);
777 mutex_lock(&inter->fpga_mutex);
780 netup_fpga_op_rw(inter, NETUP_CI_TSA_DIV, 0x0, 0);
781 netup_fpga_op_rw(inter, NETUP_CI_TSB_DIV, 0x0, 0);
784 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
786 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
788 ret = netup_fpga_op_rw(inter, NETUP_CI_REVISION, 0, NETUP_CI_FLG_RD);
790 netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0x44, 0);
792 mutex_unlock(&inter->fpga_mutex);
794 ci_dbg_print("%s: NetUP CI Revision = 0x%x\n", __func__, ret);
796 schedule_work(&inter->work);
800 ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret);
807 EXPORT_SYMBOL(altera_ci_init);
809 int altera_ci_tuner_reset(void *dev, int ci_nr)
811 struct fpga_inode *temp_int = find_inode(dev);
812 struct fpga_internal *inter = NULL;
815 ci_dbg_print("%s\n", __func__);
817 if (temp_int == NULL)
820 if (temp_int->internal == NULL)
823 inter = temp_int->internal;
825 mutex_lock(&inter->fpga_mutex);
827 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
828 store &= ~(4 << (2 - ci_nr));
829 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
831 store |= (4 << (2 - ci_nr));
832 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
834 mutex_unlock(&inter->fpga_mutex);
838 EXPORT_SYMBOL(altera_ci_tuner_reset);