Path: blob/master/drivers/media/video/cx23885/cimax2.c
17687 views
/*1* cimax2.c2*3* CIMax2(R) SP2 driver in conjunction with NetUp Dual DVB-S2 CI card4*5* Copyright (C) 2009 NetUP Inc.6* Copyright (C) 2009 Igor M. Liplianin <[email protected]>7* Copyright (C) 2009 Abylay Ospan <[email protected]>8*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License as published by11* the Free Software Foundation; either version 2 of the License, or12* (at your option) any later version.13*14* This program is distributed in the hope that it will be useful,15* but WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17*18* GNU General Public License for more details.19*20* You should have received a copy of the GNU General Public License21* along with this program; if not, write to the Free Software22* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.23*/2425#include "cx23885.h"26#include "dvb_ca_en50221.h"27/**** Bit definitions for MC417_RWD and MC417_OEN registers ***28bits 31-1629+-----------+30| Reserved |31+-----------+32bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 833+-------+-------+-------+-------+-------+-------+-------+-------+34| WR# | RD# | | ACK# | ADHI | ADLO | CS1# | CS0# |35+-------+-------+-------+-------+-------+-------+-------+-------+36bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 037+-------+-------+-------+-------+-------+-------+-------+-------+38| DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0|39+-------+-------+-------+-------+-------+-------+-------+-------+40***/41/* MC417 */42#define NETUP_DATA 0x000000ff43#define NETUP_WR 0x0000800044#define NETUP_RD 0x0000400045#define NETUP_ACK 0x0000100046#define NETUP_ADHI 0x0000080047#define NETUP_ADLO 0x0000040048#define NETUP_CS1 0x0000020049#define NETUP_CS0 0x0000010050#define NETUP_EN_ALL 0x0000100051#define NETUP_CTRL_OFF (NETUP_CS1 | NETUP_CS0 | NETUP_WR | NETUP_RD)52#define NETUP_CI_CTL 0x0453#define NETUP_CI_RD 15455#define NETUP_IRQ_DETAM 0x156#define NETUP_IRQ_IRQAM 0x45758static unsigned int ci_dbg;59module_param(ci_dbg, int, 0644);60MODULE_PARM_DESC(ci_dbg, "Enable CI debugging");6162static unsigned int ci_irq_enable;63module_param(ci_irq_enable, int, 0644);64MODULE_PARM_DESC(ci_irq_enable, "Enable IRQ from CAM");6566#define ci_dbg_print(args...) \67do { \68if (ci_dbg) \69printk(KERN_DEBUG args); \70} while (0)7172#define ci_irq_flags() (ci_irq_enable ? NETUP_IRQ_IRQAM : 0)7374/* stores all private variables for communication with CI */75struct netup_ci_state {76struct dvb_ca_en50221 ca;77struct mutex ca_mutex;78struct i2c_adapter *i2c_adap;79u8 ci_i2c_addr;80int status;81struct work_struct work;82void *priv;83u8 current_irq_mode;84int current_ci_flag;85unsigned long next_status_checked_time;86};878889int netup_read_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg,90u8 *buf, int len)91{92int ret;93struct i2c_msg msg[] = {94{95.addr = addr,96.flags = 0,97.buf = ®,98.len = 199}, {100.addr = addr,101.flags = I2C_M_RD,102.buf = buf,103.len = len104}105};106107ret = i2c_transfer(i2c_adap, msg, 2);108109if (ret != 2) {110ci_dbg_print("%s: i2c read error, Reg = 0x%02x, Status = %d\n",111__func__, reg, ret);112113return -1;114}115116ci_dbg_print("%s: i2c read Addr=0x%04x, Reg = 0x%02x, data = %02x\n",117__func__, addr, reg, buf[0]);118119return 0;120}121122int netup_write_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg,123u8 *buf, int len)124{125int ret;126u8 buffer[len + 1];127128struct i2c_msg msg = {129.addr = addr,130.flags = 0,131.buf = &buffer[0],132.len = len + 1133};134135buffer[0] = reg;136memcpy(&buffer[1], buf, len);137138ret = i2c_transfer(i2c_adap, &msg, 1);139140if (ret != 1) {141ci_dbg_print("%s: i2c write error, Reg=[0x%02x], Status=%d\n",142__func__, reg, ret);143return -1;144}145146return 0;147}148149int netup_ci_get_mem(struct cx23885_dev *dev)150{151int mem;152unsigned long timeout = jiffies + msecs_to_jiffies(1);153154for (;;) {155mem = cx_read(MC417_RWD);156if ((mem & NETUP_ACK) == 0)157break;158if (time_after(jiffies, timeout))159break;160udelay(1);161}162163cx_set(MC417_RWD, NETUP_CTRL_OFF);164165return mem & 0xff;166}167168int netup_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot,169u8 flag, u8 read, int addr, u8 data)170{171struct netup_ci_state *state = en50221->data;172struct cx23885_tsport *port = state->priv;173struct cx23885_dev *dev = port->dev;174175u8 store;176int mem;177int ret;178179if (0 != slot)180return -EINVAL;181182if (state->current_ci_flag != flag) {183ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,1840, &store, 1);185if (ret != 0)186return ret;187188store &= ~0x0c;189store |= flag;190191ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,1920, &store, 1);193if (ret != 0)194return ret;195};196state->current_ci_flag = flag;197198mutex_lock(&dev->gpio_lock);199200/* write addr */201cx_write(MC417_OEN, NETUP_EN_ALL);202cx_write(MC417_RWD, NETUP_CTRL_OFF |203NETUP_ADLO | (0xff & addr));204cx_clear(MC417_RWD, NETUP_ADLO);205cx_write(MC417_RWD, NETUP_CTRL_OFF |206NETUP_ADHI | (0xff & (addr >> 8)));207cx_clear(MC417_RWD, NETUP_ADHI);208209if (read) { /* data in */210cx_write(MC417_OEN, NETUP_EN_ALL | NETUP_DATA);211} else /* data out */212cx_write(MC417_RWD, NETUP_CTRL_OFF | data);213214/* choose chip */215cx_clear(MC417_RWD,216(state->ci_i2c_addr == 0x40) ? NETUP_CS0 : NETUP_CS1);217/* read/write */218cx_clear(MC417_RWD, (read) ? NETUP_RD : NETUP_WR);219mem = netup_ci_get_mem(dev);220221mutex_unlock(&dev->gpio_lock);222223if (!read)224if (mem < 0)225return -EREMOTEIO;226227ci_dbg_print("%s: %s: chipaddr=[0x%x] addr=[0x%02x], %s=%x\n", __func__,228(read) ? "read" : "write", state->ci_i2c_addr, addr,229(flag == NETUP_CI_CTL) ? "ctl" : "mem",230(read) ? mem : data);231232if (read)233return mem;234235return 0;236}237238int netup_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,239int slot, int addr)240{241return netup_ci_op_cam(en50221, slot, 0, NETUP_CI_RD, addr, 0);242}243244int netup_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,245int slot, int addr, u8 data)246{247return netup_ci_op_cam(en50221, slot, 0, 0, addr, data);248}249250int netup_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr)251{252return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL,253NETUP_CI_RD, addr, 0);254}255256int netup_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,257u8 addr, u8 data)258{259return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL, 0, addr, data);260}261262int netup_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)263{264struct netup_ci_state *state = en50221->data;265u8 buf = 0x80;266int ret;267268if (0 != slot)269return -EINVAL;270271udelay(500);272ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,2730, &buf, 1);274275if (ret != 0)276return ret;277278udelay(500);279280buf = 0x00;281ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,2820, &buf, 1);283284msleep(1000);285dvb_ca_en50221_camready_irq(&state->ca, 0);286287return 0;288289}290291int netup_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)292{293/* not implemented */294return 0;295}296297int netup_ci_set_irq(struct dvb_ca_en50221 *en50221, u8 irq_mode)298{299struct netup_ci_state *state = en50221->data;300int ret;301302if (irq_mode == state->current_irq_mode)303return 0;304305ci_dbg_print("%s: chipaddr=[0x%x] setting ci IRQ to [0x%x] \n",306__func__, state->ci_i2c_addr, irq_mode);307ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,3080x1b, &irq_mode, 1);309310if (ret != 0)311return ret;312313state->current_irq_mode = irq_mode;314315return 0;316}317318int netup_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot)319{320struct netup_ci_state *state = en50221->data;321u8 buf;322323if (0 != slot)324return -EINVAL;325326netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,3270, &buf, 1);328buf |= 0x60;329330return netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,3310, &buf, 1);332}333334/* work handler */335static void netup_read_ci_status(struct work_struct *work)336{337struct netup_ci_state *state =338container_of(work, struct netup_ci_state, work);339u8 buf[33];340int ret;341342/* CAM module IRQ processing. fast operation */343dvb_ca_en50221_frda_irq(&state->ca, 0);344345/* CAM module INSERT/REMOVE processing. slow operation because of i2c346* transfers */347if (time_after(jiffies, state->next_status_checked_time)348|| !state->status) {349ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,3500, &buf[0], 33);351352state->next_status_checked_time = jiffies353+ msecs_to_jiffies(1000);354355if (ret != 0)356return;357358ci_dbg_print("%s: Slot Status Addr=[0x%04x], "359"Reg=[0x%02x], data=%02x, "360"TS config = %02x\n", __func__,361state->ci_i2c_addr, 0, buf[0],362buf[0]);363364365if (buf[0] & 1)366state->status = DVB_CA_EN50221_POLL_CAM_PRESENT |367DVB_CA_EN50221_POLL_CAM_READY;368else369state->status = 0;370}371}372373/* CI irq handler */374int netup_ci_slot_status(struct cx23885_dev *dev, u32 pci_status)375{376struct cx23885_tsport *port = NULL;377struct netup_ci_state *state = NULL;378379ci_dbg_print("%s:\n", __func__);380381if (0 == (pci_status & (PCI_MSK_GPIO0 | PCI_MSK_GPIO1)))382return 0;383384if (pci_status & PCI_MSK_GPIO0) {385port = &dev->ts1;386state = port->port_priv;387schedule_work(&state->work);388ci_dbg_print("%s: Wakeup CI0\n", __func__);389}390391if (pci_status & PCI_MSK_GPIO1) {392port = &dev->ts2;393state = port->port_priv;394schedule_work(&state->work);395ci_dbg_print("%s: Wakeup CI1\n", __func__);396}397398return 1;399}400401int netup_poll_ci_slot_status(struct dvb_ca_en50221 *en50221, int slot, int open)402{403struct netup_ci_state *state = en50221->data;404405if (0 != slot)406return -EINVAL;407408netup_ci_set_irq(en50221, open ? (NETUP_IRQ_DETAM | ci_irq_flags())409: NETUP_IRQ_DETAM);410411return state->status;412}413414int netup_ci_init(struct cx23885_tsport *port)415{416struct netup_ci_state *state;417u8 cimax_init[34] = {4180x00, /* module A control*/4190x00, /* auto select mask high A */4200x00, /* auto select mask low A */4210x00, /* auto select pattern high A */4220x00, /* auto select pattern low A */4230x44, /* memory access time A */4240x00, /* invert input A */4250x00, /* RFU */4260x00, /* RFU */4270x00, /* module B control*/4280x00, /* auto select mask high B */4290x00, /* auto select mask low B */4300x00, /* auto select pattern high B */4310x00, /* auto select pattern low B */4320x44, /* memory access time B */4330x00, /* invert input B */4340x00, /* RFU */4350x00, /* RFU */4360x00, /* auto select mask high Ext */4370x00, /* auto select mask low Ext */4380x00, /* auto select pattern high Ext */4390x00, /* auto select pattern low Ext */4400x00, /* RFU */4410x02, /* destination - module A */4420x01, /* power on (use it like store place) */4430x00, /* RFU */4440x00, /* int status read only */445ci_irq_flags() | NETUP_IRQ_DETAM, /* DETAM, IRQAM unmasked */4460x05, /* EXTINT=active-high, INT=push-pull */4470x00, /* USCG1 */4480x04, /* ack active low */4490x00, /* LOCK = 0 */4500x33, /* serial mode, rising in, rising out, MSB first*/4510x31, /* synchronization */452};453int ret;454455ci_dbg_print("%s\n", __func__);456state = kzalloc(sizeof(struct netup_ci_state), GFP_KERNEL);457if (!state) {458ci_dbg_print("%s: Unable create CI structure!\n", __func__);459ret = -ENOMEM;460goto err;461}462463port->port_priv = state;464465switch (port->nr) {466case 1:467state->ci_i2c_addr = 0x40;468break;469case 2:470state->ci_i2c_addr = 0x41;471break;472}473474state->i2c_adap = &port->dev->i2c_bus[0].i2c_adap;475state->ca.owner = THIS_MODULE;476state->ca.read_attribute_mem = netup_ci_read_attribute_mem;477state->ca.write_attribute_mem = netup_ci_write_attribute_mem;478state->ca.read_cam_control = netup_ci_read_cam_ctl;479state->ca.write_cam_control = netup_ci_write_cam_ctl;480state->ca.slot_reset = netup_ci_slot_reset;481state->ca.slot_shutdown = netup_ci_slot_shutdown;482state->ca.slot_ts_enable = netup_ci_slot_ts_ctl;483state->ca.poll_slot_status = netup_poll_ci_slot_status;484state->ca.data = state;485state->priv = port;486state->current_irq_mode = ci_irq_flags() | NETUP_IRQ_DETAM;487488ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,4890, &cimax_init[0], 34);490/* lock registers */491ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,4920x1f, &cimax_init[0x18], 1);493/* power on slots */494ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,4950x18, &cimax_init[0x18], 1);496497if (0 != ret)498goto err;499500ret = dvb_ca_en50221_init(&port->frontends.adapter,501&state->ca,502/* flags */ 0,503/* n_slots */ 1);504if (0 != ret)505goto err;506507INIT_WORK(&state->work, netup_read_ci_status);508schedule_work(&state->work);509510ci_dbg_print("%s: CI initialized!\n", __func__);511512return 0;513err:514ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret);515kfree(state);516return ret;517}518519void netup_ci_exit(struct cx23885_tsport *port)520{521struct netup_ci_state *state;522523if (NULL == port)524return;525526state = (struct netup_ci_state *)port->port_priv;527if (NULL == state)528return;529530if (NULL == state->ca.data)531return;532533dvb_ca_en50221_release(&state->ca);534kfree(state);535}536537538