sp2.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * CIMaX SP2/SP2HF (Atmel T90FJR) CI driver
  3. *
  4. * Copyright (C) 2014 Olli Salonen <olli.salonen@iki.fi>
  5. *
  6. * Heavily based on CIMax2(R) SP2 driver in conjunction with NetUp Dual
  7. * DVB-S2 CI card (cimax2) with following copyrights:
  8. *
  9. * Copyright (C) 2009 NetUP Inc.
  10. * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
  11. * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include "sp2_priv.h"
  24. static int sp2_read_i2c(struct sp2 *s, u8 reg, u8 *buf, int len)
  25. {
  26. int ret;
  27. struct i2c_client *client = s->client;
  28. struct i2c_adapter *adap = client->adapter;
  29. struct i2c_msg msg[] = {
  30. {
  31. .addr = client->addr,
  32. .flags = 0,
  33. .buf = &reg,
  34. .len = 1
  35. }, {
  36. .addr = client->addr,
  37. .flags = I2C_M_RD,
  38. .buf = buf,
  39. .len = len
  40. }
  41. };
  42. ret = i2c_transfer(adap, msg, 2);
  43. if (ret != 2) {
  44. dev_err(&client->dev, "i2c read error, reg = 0x%02x, status = %d\n",
  45. reg, ret);
  46. if (ret < 0)
  47. return ret;
  48. else
  49. return -EIO;
  50. }
  51. dev_dbg(&s->client->dev, "addr=0x%04x, reg = 0x%02x, data = %02x\n",
  52. client->addr, reg, buf[0]);
  53. return 0;
  54. }
  55. static int sp2_write_i2c(struct sp2 *s, u8 reg, u8 *buf, int len)
  56. {
  57. int ret;
  58. u8 buffer[35];
  59. struct i2c_client *client = s->client;
  60. struct i2c_adapter *adap = client->adapter;
  61. struct i2c_msg msg = {
  62. .addr = client->addr,
  63. .flags = 0,
  64. .buf = &buffer[0],
  65. .len = len + 1
  66. };
  67. if ((len + 1) > sizeof(buffer)) {
  68. dev_err(&client->dev, "i2c wr reg=%02x: len=%d is too big!\n",
  69. reg, len);
  70. return -EINVAL;
  71. }
  72. buffer[0] = reg;
  73. memcpy(&buffer[1], buf, len);
  74. ret = i2c_transfer(adap, &msg, 1);
  75. if (ret != 1) {
  76. dev_err(&client->dev, "i2c write error, reg = 0x%02x, status = %d\n",
  77. reg, ret);
  78. if (ret < 0)
  79. return ret;
  80. else
  81. return -EIO;
  82. }
  83. dev_dbg(&s->client->dev, "addr=0x%04x, reg = 0x%02x, data = %*ph\n",
  84. client->addr, reg, len, buf);
  85. return 0;
  86. }
  87. static int sp2_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot, u8 acs,
  88. u8 read, int addr, u8 data)
  89. {
  90. struct sp2 *s = en50221->data;
  91. u8 store;
  92. int mem, ret;
  93. int (*ci_op_cam)(void*, u8, int, u8, int*) = s->ci_control;
  94. if (slot != 0)
  95. return -EINVAL;
  96. /*
  97. * change module access type between IO space and attribute memory
  98. * when needed
  99. */
  100. if (s->module_access_type != acs) {
  101. ret = sp2_read_i2c(s, 0x00, &store, 1);
  102. if (ret)
  103. return ret;
  104. store &= ~(SP2_MOD_CTL_ACS1 | SP2_MOD_CTL_ACS0);
  105. store |= acs;
  106. ret = sp2_write_i2c(s, 0x00, &store, 1);
  107. if (ret)
  108. return ret;
  109. }
  110. s->module_access_type = acs;
  111. /* implementation of ci_op_cam is device specific */
  112. if (ci_op_cam) {
  113. ret = ci_op_cam(s->priv, read, addr, data, &mem);
  114. } else {
  115. dev_err(&s->client->dev, "callback not defined");
  116. return -EINVAL;
  117. }
  118. if (ret)
  119. return ret;
  120. dev_dbg(&s->client->dev, "%s: slot=%d, addr=0x%04x, %s, data=%x",
  121. (read) ? "read" : "write", slot, addr,
  122. (acs == SP2_CI_ATTR_ACS) ? "attr" : "io",
  123. (read) ? mem : data);
  124. if (read)
  125. return mem;
  126. else
  127. return 0;
  128. }
  129. int sp2_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
  130. int slot, int addr)
  131. {
  132. return sp2_ci_op_cam(en50221, slot, SP2_CI_ATTR_ACS,
  133. SP2_CI_RD, addr, 0);
  134. }
  135. int sp2_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
  136. int slot, int addr, u8 data)
  137. {
  138. return sp2_ci_op_cam(en50221, slot, SP2_CI_ATTR_ACS,
  139. SP2_CI_WR, addr, data);
  140. }
  141. int sp2_ci_read_cam_control(struct dvb_ca_en50221 *en50221,
  142. int slot, u8 addr)
  143. {
  144. return sp2_ci_op_cam(en50221, slot, SP2_CI_IO_ACS,
  145. SP2_CI_RD, addr, 0);
  146. }
  147. int sp2_ci_write_cam_control(struct dvb_ca_en50221 *en50221,
  148. int slot, u8 addr, u8 data)
  149. {
  150. return sp2_ci_op_cam(en50221, slot, SP2_CI_IO_ACS,
  151. SP2_CI_WR, addr, data);
  152. }
  153. int sp2_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
  154. {
  155. struct sp2 *s = en50221->data;
  156. u8 buf;
  157. int ret;
  158. dev_dbg(&s->client->dev, "slot: %d\n", slot);
  159. if (slot != 0)
  160. return -EINVAL;
  161. /* RST on */
  162. buf = SP2_MOD_CTL_RST;
  163. ret = sp2_write_i2c(s, 0x00, &buf, 1);
  164. if (ret)
  165. return ret;
  166. usleep_range(500, 600);
  167. /* RST off */
  168. buf = 0x00;
  169. ret = sp2_write_i2c(s, 0x00, &buf, 1);
  170. if (ret)
  171. return ret;
  172. msleep(1000);
  173. return 0;
  174. }
  175. int sp2_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
  176. {
  177. struct sp2 *s = en50221->data;
  178. dev_dbg(&s->client->dev, "slot:%d\n", slot);
  179. /* not implemented */
  180. return 0;
  181. }
  182. int sp2_ci_slot_ts_enable(struct dvb_ca_en50221 *en50221, int slot)
  183. {
  184. struct sp2 *s = en50221->data;
  185. u8 buf;
  186. dev_dbg(&s->client->dev, "slot:%d\n", slot);
  187. if (slot != 0)
  188. return -EINVAL;
  189. sp2_read_i2c(s, 0x00, &buf, 1);
  190. /* disable bypass and enable TS */
  191. buf |= (SP2_MOD_CTL_TSOEN | SP2_MOD_CTL_TSIEN);
  192. return sp2_write_i2c(s, 0, &buf, 1);
  193. }
  194. int sp2_ci_poll_slot_status(struct dvb_ca_en50221 *en50221,
  195. int slot, int open)
  196. {
  197. struct sp2 *s = en50221->data;
  198. u8 buf[2];
  199. int ret;
  200. dev_dbg(&s->client->dev, "slot:%d open:%d\n", slot, open);
  201. /*
  202. * CAM module INSERT/REMOVE processing. Slow operation because of i2c
  203. * transfers. Throttle read to one per sec.
  204. */
  205. if (time_after(jiffies, s->next_status_checked_time)) {
  206. ret = sp2_read_i2c(s, 0x00, buf, 1);
  207. s->next_status_checked_time = jiffies + msecs_to_jiffies(1000);
  208. if (ret)
  209. return 0;
  210. if (buf[0] & SP2_MOD_CTL_DET)
  211. s->status = DVB_CA_EN50221_POLL_CAM_PRESENT |
  212. DVB_CA_EN50221_POLL_CAM_READY;
  213. else
  214. s->status = 0;
  215. }
  216. return s->status;
  217. }
  218. static int sp2_init(struct sp2 *s)
  219. {
  220. int ret = 0;
  221. u8 buf;
  222. u8 cimax_init[34] = {
  223. 0x00, /* module A control*/
  224. 0x00, /* auto select mask high A */
  225. 0x00, /* auto select mask low A */
  226. 0x00, /* auto select pattern high A */
  227. 0x00, /* auto select pattern low A */
  228. 0x44, /* memory access time A, 600 ns */
  229. 0x00, /* invert input A */
  230. 0x00, /* RFU */
  231. 0x00, /* RFU */
  232. 0x00, /* module B control*/
  233. 0x00, /* auto select mask high B */
  234. 0x00, /* auto select mask low B */
  235. 0x00, /* auto select pattern high B */
  236. 0x00, /* auto select pattern low B */
  237. 0x44, /* memory access time B, 600 ns */
  238. 0x00, /* invert input B */
  239. 0x00, /* RFU */
  240. 0x00, /* RFU */
  241. 0x00, /* auto select mask high Ext */
  242. 0x00, /* auto select mask low Ext */
  243. 0x00, /* auto select pattern high Ext */
  244. 0x00, /* auto select pattern low Ext */
  245. 0x00, /* RFU */
  246. 0x02, /* destination - module A */
  247. 0x01, /* power control reg, VCC power on */
  248. 0x00, /* RFU */
  249. 0x00, /* int status read only */
  250. 0x00, /* Interrupt Mask Register */
  251. 0x05, /* EXTINT=active-high, INT=push-pull */
  252. 0x00, /* USCG1 */
  253. 0x04, /* ack active low */
  254. 0x00, /* LOCK = 0 */
  255. 0x22, /* unknown */
  256. 0x00, /* synchronization? */
  257. };
  258. dev_dbg(&s->client->dev, "\n");
  259. s->ca.owner = THIS_MODULE;
  260. s->ca.read_attribute_mem = sp2_ci_read_attribute_mem;
  261. s->ca.write_attribute_mem = sp2_ci_write_attribute_mem;
  262. s->ca.read_cam_control = sp2_ci_read_cam_control;
  263. s->ca.write_cam_control = sp2_ci_write_cam_control;
  264. s->ca.slot_reset = sp2_ci_slot_reset;
  265. s->ca.slot_shutdown = sp2_ci_slot_shutdown;
  266. s->ca.slot_ts_enable = sp2_ci_slot_ts_enable;
  267. s->ca.poll_slot_status = sp2_ci_poll_slot_status;
  268. s->ca.data = s;
  269. s->module_access_type = 0;
  270. /* initialize all regs */
  271. ret = sp2_write_i2c(s, 0x00, &cimax_init[0], 34);
  272. if (ret)
  273. goto err;
  274. /* lock registers */
  275. buf = 1;
  276. ret = sp2_write_i2c(s, 0x1f, &buf, 1);
  277. if (ret)
  278. goto err;
  279. /* power on slots */
  280. ret = sp2_write_i2c(s, 0x18, &buf, 1);
  281. if (ret)
  282. goto err;
  283. ret = dvb_ca_en50221_init(s->dvb_adap, &s->ca, 0, 1);
  284. if (ret)
  285. goto err;
  286. return 0;
  287. err:
  288. dev_dbg(&s->client->dev, "init failed=%d\n", ret);
  289. return ret;
  290. }
  291. static int sp2_exit(struct i2c_client *client)
  292. {
  293. struct sp2 *s;
  294. dev_dbg(&client->dev, "\n");
  295. if (client == NULL)
  296. return 0;
  297. s = i2c_get_clientdata(client);
  298. if (s == NULL)
  299. return 0;
  300. if (s->ca.data == NULL)
  301. return 0;
  302. dvb_ca_en50221_release(&s->ca);
  303. return 0;
  304. }
  305. static int sp2_probe(struct i2c_client *client,
  306. const struct i2c_device_id *id)
  307. {
  308. struct sp2_config *cfg = client->dev.platform_data;
  309. struct sp2 *s;
  310. int ret;
  311. dev_dbg(&client->dev, "\n");
  312. s = kzalloc(sizeof(struct sp2), GFP_KERNEL);
  313. if (!s) {
  314. ret = -ENOMEM;
  315. dev_err(&client->dev, "kzalloc() failed\n");
  316. goto err;
  317. }
  318. s->client = client;
  319. s->dvb_adap = cfg->dvb_adap;
  320. s->priv = cfg->priv;
  321. s->ci_control = cfg->ci_control;
  322. i2c_set_clientdata(client, s);
  323. ret = sp2_init(s);
  324. if (ret)
  325. goto err;
  326. dev_info(&s->client->dev, "CIMaX SP2 successfully attached\n");
  327. return 0;
  328. err:
  329. dev_dbg(&client->dev, "init failed=%d\n", ret);
  330. kfree(s);
  331. return ret;
  332. }
  333. static int sp2_remove(struct i2c_client *client)
  334. {
  335. struct sp2 *s = i2c_get_clientdata(client);
  336. dev_dbg(&client->dev, "\n");
  337. sp2_exit(client);
  338. kfree(s);
  339. return 0;
  340. }
  341. static const struct i2c_device_id sp2_id[] = {
  342. {"sp2", 0},
  343. {}
  344. };
  345. MODULE_DEVICE_TABLE(i2c, sp2_id);
  346. static struct i2c_driver sp2_driver = {
  347. .driver = {
  348. .name = "sp2",
  349. },
  350. .probe = sp2_probe,
  351. .remove = sp2_remove,
  352. .id_table = sp2_id,
  353. };
  354. module_i2c_driver(sp2_driver);
  355. MODULE_DESCRIPTION("CIMaX SP2/HF CI driver");
  356. MODULE_AUTHOR("Olli Salonen <olli.salonen@iki.fi>");
  357. MODULE_LICENSE("GPL");