1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2868736adSOlli Salonen /* 3868736adSOlli Salonen * CIMaX SP2/SP2HF (Atmel T90FJR) CI driver 4868736adSOlli Salonen * 5868736adSOlli Salonen * Copyright (C) 2014 Olli Salonen <olli.salonen@iki.fi> 6868736adSOlli Salonen * 7868736adSOlli Salonen * Heavily based on CIMax2(R) SP2 driver in conjunction with NetUp Dual 8868736adSOlli Salonen * DVB-S2 CI card (cimax2) with following copyrights: 9868736adSOlli Salonen * 10868736adSOlli Salonen * Copyright (C) 2009 NetUP Inc. 11868736adSOlli Salonen * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru> 12868736adSOlli Salonen * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru> 13868736adSOlli Salonen */ 14868736adSOlli Salonen 15868736adSOlli Salonen #include "sp2_priv.h" 16868736adSOlli Salonen 17868736adSOlli Salonen static int sp2_read_i2c(struct sp2 *s, u8 reg, u8 *buf, int len) 18868736adSOlli Salonen { 19868736adSOlli Salonen int ret; 20868736adSOlli Salonen struct i2c_client *client = s->client; 21868736adSOlli Salonen struct i2c_adapter *adap = client->adapter; 22868736adSOlli Salonen struct i2c_msg msg[] = { 23868736adSOlli Salonen { 24868736adSOlli Salonen .addr = client->addr, 25868736adSOlli Salonen .flags = 0, 26868736adSOlli Salonen .buf = ®, 27868736adSOlli Salonen .len = 1 28868736adSOlli Salonen }, { 29868736adSOlli Salonen .addr = client->addr, 30868736adSOlli Salonen .flags = I2C_M_RD, 31868736adSOlli Salonen .buf = buf, 32868736adSOlli Salonen .len = len 33868736adSOlli Salonen } 34868736adSOlli Salonen }; 35868736adSOlli Salonen 36868736adSOlli Salonen ret = i2c_transfer(adap, msg, 2); 37868736adSOlli Salonen 38868736adSOlli Salonen if (ret != 2) { 39868736adSOlli Salonen dev_err(&client->dev, "i2c read error, reg = 0x%02x, status = %d\n", 40868736adSOlli Salonen reg, ret); 41868736adSOlli Salonen if (ret < 0) 42868736adSOlli Salonen return ret; 43868736adSOlli Salonen else 44868736adSOlli Salonen return -EIO; 45868736adSOlli Salonen } 46868736adSOlli Salonen 47868736adSOlli Salonen dev_dbg(&s->client->dev, "addr=0x%04x, reg = 0x%02x, data = %02x\n", 48868736adSOlli Salonen client->addr, reg, buf[0]); 49868736adSOlli Salonen 50868736adSOlli Salonen return 0; 51868736adSOlli Salonen } 52868736adSOlli Salonen 53868736adSOlli Salonen static int sp2_write_i2c(struct sp2 *s, u8 reg, u8 *buf, int len) 54868736adSOlli Salonen { 55868736adSOlli Salonen int ret; 56868736adSOlli Salonen u8 buffer[35]; 57868736adSOlli Salonen struct i2c_client *client = s->client; 58868736adSOlli Salonen struct i2c_adapter *adap = client->adapter; 59868736adSOlli Salonen struct i2c_msg msg = { 60868736adSOlli Salonen .addr = client->addr, 61868736adSOlli Salonen .flags = 0, 62868736adSOlli Salonen .buf = &buffer[0], 63868736adSOlli Salonen .len = len + 1 64868736adSOlli Salonen }; 65868736adSOlli Salonen 66868736adSOlli Salonen if ((len + 1) > sizeof(buffer)) { 67868736adSOlli Salonen dev_err(&client->dev, "i2c wr reg=%02x: len=%d is too big!\n", 68868736adSOlli Salonen reg, len); 69868736adSOlli Salonen return -EINVAL; 70868736adSOlli Salonen } 71868736adSOlli Salonen 72868736adSOlli Salonen buffer[0] = reg; 73868736adSOlli Salonen memcpy(&buffer[1], buf, len); 74868736adSOlli Salonen 75868736adSOlli Salonen ret = i2c_transfer(adap, &msg, 1); 76868736adSOlli Salonen 77868736adSOlli Salonen if (ret != 1) { 78868736adSOlli Salonen dev_err(&client->dev, "i2c write error, reg = 0x%02x, status = %d\n", 79868736adSOlli Salonen reg, ret); 80868736adSOlli Salonen if (ret < 0) 81868736adSOlli Salonen return ret; 82868736adSOlli Salonen else 83868736adSOlli Salonen return -EIO; 84868736adSOlli Salonen } 85868736adSOlli Salonen 86b1cb7ad2SOlli Salonen dev_dbg(&s->client->dev, "addr=0x%04x, reg = 0x%02x, data = %*ph\n", 87b1cb7ad2SOlli Salonen client->addr, reg, len, buf); 88b1cb7ad2SOlli Salonen 89868736adSOlli Salonen return 0; 90868736adSOlli Salonen } 91868736adSOlli Salonen 92868736adSOlli Salonen static int sp2_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot, u8 acs, 93868736adSOlli Salonen u8 read, int addr, u8 data) 94868736adSOlli Salonen { 95868736adSOlli Salonen struct sp2 *s = en50221->data; 96868736adSOlli Salonen u8 store; 97868736adSOlli Salonen int mem, ret; 98868736adSOlli Salonen int (*ci_op_cam)(void*, u8, int, u8, int*) = s->ci_control; 99868736adSOlli Salonen 100868736adSOlli Salonen if (slot != 0) 101868736adSOlli Salonen return -EINVAL; 102868736adSOlli Salonen 103868736adSOlli Salonen /* 104868736adSOlli Salonen * change module access type between IO space and attribute memory 105868736adSOlli Salonen * when needed 106868736adSOlli Salonen */ 107868736adSOlli Salonen if (s->module_access_type != acs) { 108868736adSOlli Salonen ret = sp2_read_i2c(s, 0x00, &store, 1); 109868736adSOlli Salonen 110868736adSOlli Salonen if (ret) 111868736adSOlli Salonen return ret; 112868736adSOlli Salonen 113868736adSOlli Salonen store &= ~(SP2_MOD_CTL_ACS1 | SP2_MOD_CTL_ACS0); 114868736adSOlli Salonen store |= acs; 115868736adSOlli Salonen 116868736adSOlli Salonen ret = sp2_write_i2c(s, 0x00, &store, 1); 117868736adSOlli Salonen if (ret) 118868736adSOlli Salonen return ret; 119868736adSOlli Salonen } 120868736adSOlli Salonen 121868736adSOlli Salonen s->module_access_type = acs; 122868736adSOlli Salonen 123868736adSOlli Salonen /* implementation of ci_op_cam is device specific */ 124868736adSOlli Salonen if (ci_op_cam) { 125868736adSOlli Salonen ret = ci_op_cam(s->priv, read, addr, data, &mem); 126868736adSOlli Salonen } else { 127868736adSOlli Salonen dev_err(&s->client->dev, "callback not defined"); 128868736adSOlli Salonen return -EINVAL; 129868736adSOlli Salonen } 130868736adSOlli Salonen 131868736adSOlli Salonen if (ret) 132868736adSOlli Salonen return ret; 133868736adSOlli Salonen 134b1cb7ad2SOlli Salonen dev_dbg(&s->client->dev, "%s: slot=%d, addr=0x%04x, %s, data=%x", 135b1cb7ad2SOlli Salonen (read) ? "read" : "write", slot, addr, 136b1cb7ad2SOlli Salonen (acs == SP2_CI_ATTR_ACS) ? "attr" : "io", 137b1cb7ad2SOlli Salonen (read) ? mem : data); 138b1cb7ad2SOlli Salonen 139b1cb7ad2SOlli Salonen if (read) 140868736adSOlli Salonen return mem; 141b1cb7ad2SOlli Salonen else 142868736adSOlli Salonen return 0; 143b1cb7ad2SOlli Salonen 144868736adSOlli Salonen } 145868736adSOlli Salonen 146868736adSOlli Salonen int sp2_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221, 147868736adSOlli Salonen int slot, int addr) 148868736adSOlli Salonen { 149868736adSOlli Salonen return sp2_ci_op_cam(en50221, slot, SP2_CI_ATTR_ACS, 150868736adSOlli Salonen SP2_CI_RD, addr, 0); 151868736adSOlli Salonen } 152868736adSOlli Salonen 153868736adSOlli Salonen int sp2_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221, 154868736adSOlli Salonen int slot, int addr, u8 data) 155868736adSOlli Salonen { 156868736adSOlli Salonen return sp2_ci_op_cam(en50221, slot, SP2_CI_ATTR_ACS, 157868736adSOlli Salonen SP2_CI_WR, addr, data); 158868736adSOlli Salonen } 159868736adSOlli Salonen 160868736adSOlli Salonen int sp2_ci_read_cam_control(struct dvb_ca_en50221 *en50221, 161868736adSOlli Salonen int slot, u8 addr) 162868736adSOlli Salonen { 163868736adSOlli Salonen return sp2_ci_op_cam(en50221, slot, SP2_CI_IO_ACS, 164868736adSOlli Salonen SP2_CI_RD, addr, 0); 165868736adSOlli Salonen } 166868736adSOlli Salonen 167868736adSOlli Salonen int sp2_ci_write_cam_control(struct dvb_ca_en50221 *en50221, 168868736adSOlli Salonen int slot, u8 addr, u8 data) 169868736adSOlli Salonen { 170868736adSOlli Salonen return sp2_ci_op_cam(en50221, slot, SP2_CI_IO_ACS, 171868736adSOlli Salonen SP2_CI_WR, addr, data); 172868736adSOlli Salonen } 173868736adSOlli Salonen 174868736adSOlli Salonen int sp2_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot) 175868736adSOlli Salonen { 176868736adSOlli Salonen struct sp2 *s = en50221->data; 177868736adSOlli Salonen u8 buf; 178868736adSOlli Salonen int ret; 179868736adSOlli Salonen 180868736adSOlli Salonen dev_dbg(&s->client->dev, "slot: %d\n", slot); 181868736adSOlli Salonen 182868736adSOlli Salonen if (slot != 0) 183868736adSOlli Salonen return -EINVAL; 184868736adSOlli Salonen 185868736adSOlli Salonen /* RST on */ 186868736adSOlli Salonen buf = SP2_MOD_CTL_RST; 187868736adSOlli Salonen ret = sp2_write_i2c(s, 0x00, &buf, 1); 188868736adSOlli Salonen 189868736adSOlli Salonen if (ret) 190868736adSOlli Salonen return ret; 191868736adSOlli Salonen 192868736adSOlli Salonen usleep_range(500, 600); 193868736adSOlli Salonen 194868736adSOlli Salonen /* RST off */ 195868736adSOlli Salonen buf = 0x00; 196868736adSOlli Salonen ret = sp2_write_i2c(s, 0x00, &buf, 1); 197868736adSOlli Salonen 198868736adSOlli Salonen if (ret) 199868736adSOlli Salonen return ret; 200868736adSOlli Salonen 201868736adSOlli Salonen msleep(1000); 202868736adSOlli Salonen 203868736adSOlli Salonen return 0; 204868736adSOlli Salonen } 205868736adSOlli Salonen 206868736adSOlli Salonen int sp2_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot) 207868736adSOlli Salonen { 208868736adSOlli Salonen struct sp2 *s = en50221->data; 209868736adSOlli Salonen 210868736adSOlli Salonen dev_dbg(&s->client->dev, "slot:%d\n", slot); 211868736adSOlli Salonen 212868736adSOlli Salonen /* not implemented */ 213868736adSOlli Salonen return 0; 214868736adSOlli Salonen } 215868736adSOlli Salonen 216868736adSOlli Salonen int sp2_ci_slot_ts_enable(struct dvb_ca_en50221 *en50221, int slot) 217868736adSOlli Salonen { 218868736adSOlli Salonen struct sp2 *s = en50221->data; 219868736adSOlli Salonen u8 buf; 220868736adSOlli Salonen 221868736adSOlli Salonen dev_dbg(&s->client->dev, "slot:%d\n", slot); 222868736adSOlli Salonen 223868736adSOlli Salonen if (slot != 0) 224868736adSOlli Salonen return -EINVAL; 225868736adSOlli Salonen 226868736adSOlli Salonen sp2_read_i2c(s, 0x00, &buf, 1); 227868736adSOlli Salonen 228868736adSOlli Salonen /* disable bypass and enable TS */ 229868736adSOlli Salonen buf |= (SP2_MOD_CTL_TSOEN | SP2_MOD_CTL_TSIEN); 230868736adSOlli Salonen return sp2_write_i2c(s, 0, &buf, 1); 231868736adSOlli Salonen } 232868736adSOlli Salonen 233868736adSOlli Salonen int sp2_ci_poll_slot_status(struct dvb_ca_en50221 *en50221, 234868736adSOlli Salonen int slot, int open) 235868736adSOlli Salonen { 236868736adSOlli Salonen struct sp2 *s = en50221->data; 237868736adSOlli Salonen u8 buf[2]; 238868736adSOlli Salonen int ret; 239868736adSOlli Salonen 240868736adSOlli Salonen dev_dbg(&s->client->dev, "slot:%d open:%d\n", slot, open); 241868736adSOlli Salonen 242868736adSOlli Salonen /* 243868736adSOlli Salonen * CAM module INSERT/REMOVE processing. Slow operation because of i2c 244868736adSOlli Salonen * transfers. Throttle read to one per sec. 245868736adSOlli Salonen */ 246868736adSOlli Salonen if (time_after(jiffies, s->next_status_checked_time)) { 247868736adSOlli Salonen ret = sp2_read_i2c(s, 0x00, buf, 1); 248868736adSOlli Salonen s->next_status_checked_time = jiffies + msecs_to_jiffies(1000); 249868736adSOlli Salonen 250868736adSOlli Salonen if (ret) 251868736adSOlli Salonen return 0; 252868736adSOlli Salonen 253868736adSOlli Salonen if (buf[0] & SP2_MOD_CTL_DET) 254868736adSOlli Salonen s->status = DVB_CA_EN50221_POLL_CAM_PRESENT | 255868736adSOlli Salonen DVB_CA_EN50221_POLL_CAM_READY; 256868736adSOlli Salonen else 257868736adSOlli Salonen s->status = 0; 258868736adSOlli Salonen } 259868736adSOlli Salonen 260868736adSOlli Salonen return s->status; 261868736adSOlli Salonen } 262868736adSOlli Salonen 263167921cbSFengguang Wu static int sp2_init(struct sp2 *s) 264868736adSOlli Salonen { 265868736adSOlli Salonen int ret = 0; 266868736adSOlli Salonen u8 buf; 267868736adSOlli Salonen u8 cimax_init[34] = { 268868736adSOlli Salonen 0x00, /* module A control*/ 269868736adSOlli Salonen 0x00, /* auto select mask high A */ 270868736adSOlli Salonen 0x00, /* auto select mask low A */ 271868736adSOlli Salonen 0x00, /* auto select pattern high A */ 272868736adSOlli Salonen 0x00, /* auto select pattern low A */ 273868736adSOlli Salonen 0x44, /* memory access time A, 600 ns */ 274868736adSOlli Salonen 0x00, /* invert input A */ 275868736adSOlli Salonen 0x00, /* RFU */ 276868736adSOlli Salonen 0x00, /* RFU */ 277868736adSOlli Salonen 0x00, /* module B control*/ 278868736adSOlli Salonen 0x00, /* auto select mask high B */ 279868736adSOlli Salonen 0x00, /* auto select mask low B */ 280868736adSOlli Salonen 0x00, /* auto select pattern high B */ 281868736adSOlli Salonen 0x00, /* auto select pattern low B */ 282868736adSOlli Salonen 0x44, /* memory access time B, 600 ns */ 283868736adSOlli Salonen 0x00, /* invert input B */ 284868736adSOlli Salonen 0x00, /* RFU */ 285868736adSOlli Salonen 0x00, /* RFU */ 286868736adSOlli Salonen 0x00, /* auto select mask high Ext */ 287868736adSOlli Salonen 0x00, /* auto select mask low Ext */ 288868736adSOlli Salonen 0x00, /* auto select pattern high Ext */ 289868736adSOlli Salonen 0x00, /* auto select pattern low Ext */ 290868736adSOlli Salonen 0x00, /* RFU */ 291868736adSOlli Salonen 0x02, /* destination - module A */ 292868736adSOlli Salonen 0x01, /* power control reg, VCC power on */ 293868736adSOlli Salonen 0x00, /* RFU */ 294868736adSOlli Salonen 0x00, /* int status read only */ 295868736adSOlli Salonen 0x00, /* Interrupt Mask Register */ 296868736adSOlli Salonen 0x05, /* EXTINT=active-high, INT=push-pull */ 297868736adSOlli Salonen 0x00, /* USCG1 */ 298868736adSOlli Salonen 0x04, /* ack active low */ 299868736adSOlli Salonen 0x00, /* LOCK = 0 */ 300868736adSOlli Salonen 0x22, /* unknown */ 301868736adSOlli Salonen 0x00, /* synchronization? */ 302868736adSOlli Salonen }; 303868736adSOlli Salonen 304868736adSOlli Salonen dev_dbg(&s->client->dev, "\n"); 305868736adSOlli Salonen 306868736adSOlli Salonen s->ca.owner = THIS_MODULE; 307868736adSOlli Salonen s->ca.read_attribute_mem = sp2_ci_read_attribute_mem; 308868736adSOlli Salonen s->ca.write_attribute_mem = sp2_ci_write_attribute_mem; 309868736adSOlli Salonen s->ca.read_cam_control = sp2_ci_read_cam_control; 310868736adSOlli Salonen s->ca.write_cam_control = sp2_ci_write_cam_control; 311868736adSOlli Salonen s->ca.slot_reset = sp2_ci_slot_reset; 312868736adSOlli Salonen s->ca.slot_shutdown = sp2_ci_slot_shutdown; 313868736adSOlli Salonen s->ca.slot_ts_enable = sp2_ci_slot_ts_enable; 314868736adSOlli Salonen s->ca.poll_slot_status = sp2_ci_poll_slot_status; 315868736adSOlli Salonen s->ca.data = s; 316868736adSOlli Salonen s->module_access_type = 0; 317868736adSOlli Salonen 318868736adSOlli Salonen /* initialize all regs */ 319868736adSOlli Salonen ret = sp2_write_i2c(s, 0x00, &cimax_init[0], 34); 320868736adSOlli Salonen if (ret) 321868736adSOlli Salonen goto err; 322868736adSOlli Salonen 323868736adSOlli Salonen /* lock registers */ 324868736adSOlli Salonen buf = 1; 325868736adSOlli Salonen ret = sp2_write_i2c(s, 0x1f, &buf, 1); 326868736adSOlli Salonen if (ret) 327868736adSOlli Salonen goto err; 328868736adSOlli Salonen 329868736adSOlli Salonen /* power on slots */ 330868736adSOlli Salonen ret = sp2_write_i2c(s, 0x18, &buf, 1); 331868736adSOlli Salonen if (ret) 332868736adSOlli Salonen goto err; 333868736adSOlli Salonen 334868736adSOlli Salonen ret = dvb_ca_en50221_init(s->dvb_adap, &s->ca, 0, 1); 335868736adSOlli Salonen if (ret) 336868736adSOlli Salonen goto err; 337868736adSOlli Salonen 338868736adSOlli Salonen return 0; 339868736adSOlli Salonen 340868736adSOlli Salonen err: 341868736adSOlli Salonen dev_dbg(&s->client->dev, "init failed=%d\n", ret); 342868736adSOlli Salonen return ret; 343868736adSOlli Salonen } 344868736adSOlli Salonen 345167921cbSFengguang Wu static int sp2_exit(struct i2c_client *client) 346868736adSOlli Salonen { 347868736adSOlli Salonen struct sp2 *s; 348868736adSOlli Salonen 349868736adSOlli Salonen dev_dbg(&client->dev, "\n"); 350868736adSOlli Salonen 351af28c996SMarkus Elfring if (!client) 352868736adSOlli Salonen return 0; 353868736adSOlli Salonen 354868736adSOlli Salonen s = i2c_get_clientdata(client); 355af28c996SMarkus Elfring if (!s) 356868736adSOlli Salonen return 0; 357868736adSOlli Salonen 358af28c996SMarkus Elfring if (!s->ca.data) 359868736adSOlli Salonen return 0; 360868736adSOlli Salonen 361868736adSOlli Salonen dvb_ca_en50221_release(&s->ca); 362868736adSOlli Salonen 363868736adSOlli Salonen return 0; 364868736adSOlli Salonen } 365868736adSOlli Salonen 366868736adSOlli Salonen static int sp2_probe(struct i2c_client *client, 367868736adSOlli Salonen const struct i2c_device_id *id) 368868736adSOlli Salonen { 369868736adSOlli Salonen struct sp2_config *cfg = client->dev.platform_data; 370868736adSOlli Salonen struct sp2 *s; 371868736adSOlli Salonen int ret; 372868736adSOlli Salonen 373868736adSOlli Salonen dev_dbg(&client->dev, "\n"); 374868736adSOlli Salonen 3752d3da59fSMarkus Elfring s = kzalloc(sizeof(*s), GFP_KERNEL); 376868736adSOlli Salonen if (!s) { 377868736adSOlli Salonen ret = -ENOMEM; 378868736adSOlli Salonen goto err; 379868736adSOlli Salonen } 380868736adSOlli Salonen 381868736adSOlli Salonen s->client = client; 382868736adSOlli Salonen s->dvb_adap = cfg->dvb_adap; 383868736adSOlli Salonen s->priv = cfg->priv; 384868736adSOlli Salonen s->ci_control = cfg->ci_control; 385868736adSOlli Salonen 386868736adSOlli Salonen i2c_set_clientdata(client, s); 387868736adSOlli Salonen 388868736adSOlli Salonen ret = sp2_init(s); 389868736adSOlli Salonen if (ret) 390868736adSOlli Salonen goto err; 391868736adSOlli Salonen 392868736adSOlli Salonen dev_info(&s->client->dev, "CIMaX SP2 successfully attached\n"); 393868736adSOlli Salonen return 0; 394868736adSOlli Salonen err: 395868736adSOlli Salonen dev_dbg(&client->dev, "init failed=%d\n", ret); 396868736adSOlli Salonen kfree(s); 397868736adSOlli Salonen 398868736adSOlli Salonen return ret; 399868736adSOlli Salonen } 400868736adSOlli Salonen 401*ed5c2f5fSUwe Kleine-König static void sp2_remove(struct i2c_client *client) 402868736adSOlli Salonen { 4039a25d410SOlli Salonen struct sp2 *s = i2c_get_clientdata(client); 404868736adSOlli Salonen 405868736adSOlli Salonen dev_dbg(&client->dev, "\n"); 406868736adSOlli Salonen sp2_exit(client); 407868736adSOlli Salonen kfree(s); 408868736adSOlli Salonen } 409868736adSOlli Salonen 410868736adSOlli Salonen static const struct i2c_device_id sp2_id[] = { 411868736adSOlli Salonen {"sp2", 0}, 412868736adSOlli Salonen {} 413868736adSOlli Salonen }; 414868736adSOlli Salonen MODULE_DEVICE_TABLE(i2c, sp2_id); 415868736adSOlli Salonen 416868736adSOlli Salonen static struct i2c_driver sp2_driver = { 417868736adSOlli Salonen .driver = { 418868736adSOlli Salonen .name = "sp2", 419868736adSOlli Salonen }, 420868736adSOlli Salonen .probe = sp2_probe, 421868736adSOlli Salonen .remove = sp2_remove, 422868736adSOlli Salonen .id_table = sp2_id, 423868736adSOlli Salonen }; 424868736adSOlli Salonen 425868736adSOlli Salonen module_i2c_driver(sp2_driver); 426868736adSOlli Salonen 427868736adSOlli Salonen MODULE_DESCRIPTION("CIMaX SP2/HF CI driver"); 428868736adSOlli Salonen MODULE_AUTHOR("Olli Salonen <olli.salonen@iki.fi>"); 429868736adSOlli Salonen MODULE_LICENSE("GPL"); 430