cxd2099.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * cxd2099.c: Driver for the CXD2099AR Common Interface Controller
  3. *
  4. * Copyright (C) 2010 DigitalDevices UG
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 only, as published by the Free Software Foundation.
  10. *
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA
  22. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  23. */
  24. #include <linux/version.h>
  25. #include <linux/slab.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/init.h>
  30. #include <linux/i2c.h>
  31. #include <linux/wait.h>
  32. #include <linux/delay.h>
  33. #include <linux/mutex.h>
  34. #include <linux/io.h>
  35. #include "cxd2099.h"
  36. #define MAX_BUFFER_SIZE 248
  37. struct cxd {
  38. struct dvb_ca_en50221 en;
  39. struct i2c_adapter *i2c;
  40. u8 adr;
  41. u8 regs[0x23];
  42. u8 lastaddress;
  43. u8 clk_reg_f;
  44. u8 clk_reg_b;
  45. int mode;
  46. u32 bitrate;
  47. int ready;
  48. int dr;
  49. int slot_stat;
  50. u8 amem[1024];
  51. int amem_read;
  52. int cammode;
  53. struct mutex lock;
  54. };
  55. static int i2c_write_reg(struct i2c_adapter *adapter, u8 adr,
  56. u8 reg, u8 data)
  57. {
  58. u8 m[2] = {reg, data};
  59. struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = m, .len = 2};
  60. if (i2c_transfer(adapter, &msg, 1) != 1) {
  61. printk(KERN_ERR "Failed to write to I2C register %02x@%02x!\n",
  62. reg, adr);
  63. return -1;
  64. }
  65. return 0;
  66. }
  67. static int i2c_write(struct i2c_adapter *adapter, u8 adr,
  68. u8 *data, u8 len)
  69. {
  70. struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len};
  71. if (i2c_transfer(adapter, &msg, 1) != 1) {
  72. printk(KERN_ERR "Failed to write to I2C!\n");
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. static int i2c_read_reg(struct i2c_adapter *adapter, u8 adr,
  78. u8 reg, u8 *val)
  79. {
  80. struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
  81. .buf = &reg, .len = 1 },
  82. {.addr = adr, .flags = I2C_M_RD,
  83. .buf = val, .len = 1 } };
  84. if (i2c_transfer(adapter, msgs, 2) != 2) {
  85. printk(KERN_ERR "error in i2c_read_reg\n");
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. static int i2c_read(struct i2c_adapter *adapter, u8 adr,
  91. u8 reg, u8 *data, u8 n)
  92. {
  93. struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
  94. .buf = &reg, .len = 1 },
  95. {.addr = adr, .flags = I2C_M_RD,
  96. .buf = data, .len = n } };
  97. if (i2c_transfer(adapter, msgs, 2) != 2) {
  98. printk(KERN_ERR "error in i2c_read\n");
  99. return -1;
  100. }
  101. return 0;
  102. }
  103. static int read_block(struct cxd *ci, u8 adr, u8 *data, u8 n)
  104. {
  105. int status;
  106. status = i2c_write_reg(ci->i2c, ci->adr, 0, adr);
  107. if (!status) {
  108. ci->lastaddress = adr;
  109. status = i2c_read(ci->i2c, ci->adr, 1, data, n);
  110. }
  111. return status;
  112. }
  113. static int read_reg(struct cxd *ci, u8 reg, u8 *val)
  114. {
  115. return read_block(ci, reg, val, 1);
  116. }
  117. static int read_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
  118. {
  119. int status;
  120. u8 addr[3] = { 2, address&0xff, address>>8 };
  121. status = i2c_write(ci->i2c, ci->adr, addr, 3);
  122. if (!status)
  123. status = i2c_read(ci->i2c, ci->adr, 3, data, n);
  124. return status;
  125. }
  126. static int write_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
  127. {
  128. int status;
  129. u8 addr[3] = { 2, address&0xff, address>>8 };
  130. status = i2c_write(ci->i2c, ci->adr, addr, 3);
  131. if (!status) {
  132. u8 buf[256] = {3};
  133. memcpy(buf+1, data, n);
  134. status = i2c_write(ci->i2c, ci->adr, buf, n+1);
  135. }
  136. return status;
  137. }
  138. static int read_io(struct cxd *ci, u16 address, u8 *val)
  139. {
  140. int status;
  141. u8 addr[3] = { 2, address&0xff, address>>8 };
  142. status = i2c_write(ci->i2c, ci->adr, addr, 3);
  143. if (!status)
  144. status = i2c_read(ci->i2c, ci->adr, 3, val, 1);
  145. return status;
  146. }
  147. static int write_io(struct cxd *ci, u16 address, u8 val)
  148. {
  149. int status;
  150. u8 addr[3] = { 2, address&0xff, address>>8 };
  151. u8 buf[2] = { 3, val };
  152. status = i2c_write(ci->i2c, ci->adr, addr, 3);
  153. if (!status)
  154. status = i2c_write(ci->i2c, ci->adr, buf, 2);
  155. return status;
  156. }
  157. static int write_regm(struct cxd *ci, u8 reg, u8 val, u8 mask)
  158. {
  159. int status;
  160. status = i2c_write_reg(ci->i2c, ci->adr, 0, reg);
  161. if (!status && reg >= 6 && reg <= 8 && mask != 0xff)
  162. status = i2c_read_reg(ci->i2c, ci->adr, 1, &ci->regs[reg]);
  163. ci->regs[reg] = (ci->regs[reg]&(~mask))|val;
  164. if (!status) {
  165. ci->lastaddress = reg;
  166. status = i2c_write_reg(ci->i2c, ci->adr, 1, ci->regs[reg]);
  167. }
  168. if (reg == 0x20)
  169. ci->regs[reg] &= 0x7f;
  170. return status;
  171. }
  172. static int write_reg(struct cxd *ci, u8 reg, u8 val)
  173. {
  174. return write_regm(ci, reg, val, 0xff);
  175. }
  176. #ifdef BUFFER_MODE
  177. static int write_block(struct cxd *ci, u8 adr, u8 *data, int n)
  178. {
  179. int status;
  180. u8 buf[256] = {1};
  181. status = i2c_write_reg(ci->i2c, ci->adr, 0, adr);
  182. if (!status) {
  183. ci->lastaddress = adr;
  184. memcpy(buf+1, data, n);
  185. status = i2c_write(ci->i2c, ci->adr, buf, n+1);
  186. }
  187. return status;
  188. }
  189. #endif
  190. static void set_mode(struct cxd *ci, int mode)
  191. {
  192. if (mode == ci->mode)
  193. return;
  194. switch (mode) {
  195. case 0x00: /* IO mem */
  196. write_regm(ci, 0x06, 0x00, 0x07);
  197. break;
  198. case 0x01: /* ATT mem */
  199. write_regm(ci, 0x06, 0x02, 0x07);
  200. break;
  201. default:
  202. break;
  203. }
  204. ci->mode = mode;
  205. }
  206. static void cam_mode(struct cxd *ci, int mode)
  207. {
  208. if (mode == ci->cammode)
  209. return;
  210. switch (mode) {
  211. case 0x00:
  212. write_regm(ci, 0x20, 0x80, 0x80);
  213. break;
  214. case 0x01:
  215. printk(KERN_INFO "enable cam buffer mode\n");
  216. /* write_reg(ci, 0x0d, 0x00); */
  217. /* write_reg(ci, 0x0e, 0x01); */
  218. write_regm(ci, 0x08, 0x40, 0x40);
  219. /* read_reg(ci, 0x12, &dummy); */
  220. write_regm(ci, 0x08, 0x80, 0x80);
  221. break;
  222. default:
  223. break;
  224. }
  225. ci->cammode = mode;
  226. }
  227. #define CHK_ERROR(s) if ((status = s)) break
  228. static int init(struct cxd *ci)
  229. {
  230. int status;
  231. mutex_lock(&ci->lock);
  232. ci->mode = -1;
  233. do {
  234. CHK_ERROR(write_reg(ci, 0x00, 0x00));
  235. CHK_ERROR(write_reg(ci, 0x01, 0x00));
  236. CHK_ERROR(write_reg(ci, 0x02, 0x10));
  237. CHK_ERROR(write_reg(ci, 0x03, 0x00));
  238. CHK_ERROR(write_reg(ci, 0x05, 0xFF));
  239. CHK_ERROR(write_reg(ci, 0x06, 0x1F));
  240. CHK_ERROR(write_reg(ci, 0x07, 0x1F));
  241. CHK_ERROR(write_reg(ci, 0x08, 0x28));
  242. CHK_ERROR(write_reg(ci, 0x14, 0x20));
  243. CHK_ERROR(write_reg(ci, 0x09, 0x4D)); /* Input Mode C, BYPass Serial, TIVAL = low, MSB */
  244. CHK_ERROR(write_reg(ci, 0x0A, 0xA7)); /* TOSTRT = 8, Mode B (gated clock), falling Edge, Serial, POL=HIGH, MSB */
  245. /* Sync detector */
  246. CHK_ERROR(write_reg(ci, 0x0B, 0x33));
  247. CHK_ERROR(write_reg(ci, 0x0C, 0x33));
  248. CHK_ERROR(write_regm(ci, 0x14, 0x00, 0x0F));
  249. CHK_ERROR(write_reg(ci, 0x15, ci->clk_reg_b));
  250. CHK_ERROR(write_regm(ci, 0x16, 0x00, 0x0F));
  251. CHK_ERROR(write_reg(ci, 0x17, ci->clk_reg_f));
  252. CHK_ERROR(write_reg(ci, 0x20, 0x28)); /* Integer Divider, Falling Edge, Internal Sync, */
  253. CHK_ERROR(write_reg(ci, 0x21, 0x00)); /* MCLKI = TICLK/8 */
  254. CHK_ERROR(write_reg(ci, 0x22, 0x07)); /* MCLKI = TICLK/8 */
  255. CHK_ERROR(write_regm(ci, 0x20, 0x80, 0x80)); /* Reset CAM state machine */
  256. CHK_ERROR(write_regm(ci, 0x03, 0x02, 02)); /* Enable IREQA Interrupt */
  257. CHK_ERROR(write_reg(ci, 0x01, 0x04)); /* Enable CD Interrupt */
  258. CHK_ERROR(write_reg(ci, 0x00, 0x31)); /* Enable TS1,Hot Swap,Slot A */
  259. CHK_ERROR(write_regm(ci, 0x09, 0x08, 0x08)); /* Put TS in bypass */
  260. ci->cammode = -1;
  261. #ifdef BUFFER_MODE
  262. cam_mode(ci, 0);
  263. #endif
  264. } while (0);
  265. mutex_unlock(&ci->lock);
  266. return 0;
  267. }
  268. static int read_attribute_mem(struct dvb_ca_en50221 *ca,
  269. int slot, int address)
  270. {
  271. struct cxd *ci = ca->data;
  272. u8 val;
  273. mutex_lock(&ci->lock);
  274. set_mode(ci, 1);
  275. read_pccard(ci, address, &val, 1);
  276. mutex_unlock(&ci->lock);
  277. return val;
  278. }
  279. static int write_attribute_mem(struct dvb_ca_en50221 *ca, int slot,
  280. int address, u8 value)
  281. {
  282. struct cxd *ci = ca->data;
  283. mutex_lock(&ci->lock);
  284. set_mode(ci, 1);
  285. write_pccard(ci, address, &value, 1);
  286. mutex_unlock(&ci->lock);
  287. return 0;
  288. }
  289. static int read_cam_control(struct dvb_ca_en50221 *ca,
  290. int slot, u8 address)
  291. {
  292. struct cxd *ci = ca->data;
  293. u8 val;
  294. mutex_lock(&ci->lock);
  295. set_mode(ci, 0);
  296. read_io(ci, address, &val);
  297. mutex_unlock(&ci->lock);
  298. return val;
  299. }
  300. static int write_cam_control(struct dvb_ca_en50221 *ca, int slot,
  301. u8 address, u8 value)
  302. {
  303. struct cxd *ci = ca->data;
  304. mutex_lock(&ci->lock);
  305. set_mode(ci, 0);
  306. write_io(ci, address, value);
  307. mutex_unlock(&ci->lock);
  308. return 0;
  309. }
  310. static int slot_reset(struct dvb_ca_en50221 *ca, int slot)
  311. {
  312. struct cxd *ci = ca->data;
  313. mutex_lock(&ci->lock);
  314. cam_mode(ci, 0);
  315. write_reg(ci, 0x00, 0x21);
  316. write_reg(ci, 0x06, 0x1F);
  317. write_reg(ci, 0x00, 0x31);
  318. write_regm(ci, 0x20, 0x80, 0x80);
  319. write_reg(ci, 0x03, 0x02);
  320. ci->ready = 0;
  321. ci->mode = -1;
  322. {
  323. int i;
  324. for (i = 0; i < 100; i++) {
  325. msleep(10);
  326. if (ci->ready)
  327. break;
  328. }
  329. }
  330. mutex_unlock(&ci->lock);
  331. /* msleep(500); */
  332. return 0;
  333. }
  334. static int slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
  335. {
  336. struct cxd *ci = ca->data;
  337. printk(KERN_INFO "slot_shutdown\n");
  338. mutex_lock(&ci->lock);
  339. /* write_regm(ci, 0x09, 0x08, 0x08); */
  340. write_regm(ci, 0x20, 0x80, 0x80);
  341. write_regm(ci, 0x06, 0x07, 0x07);
  342. ci->mode = -1;
  343. mutex_unlock(&ci->lock);
  344. return 0; /* shutdown(ci); */
  345. }
  346. static int slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
  347. {
  348. struct cxd *ci = ca->data;
  349. mutex_lock(&ci->lock);
  350. write_regm(ci, 0x09, 0x00, 0x08);
  351. set_mode(ci, 0);
  352. #ifdef BUFFER_MODE
  353. cam_mode(ci, 1);
  354. #endif
  355. mutex_unlock(&ci->lock);
  356. return 0;
  357. }
  358. static int campoll(struct cxd *ci)
  359. {
  360. u8 istat;
  361. read_reg(ci, 0x04, &istat);
  362. if (!istat)
  363. return 0;
  364. write_reg(ci, 0x05, istat);
  365. if (istat&0x40) {
  366. ci->dr = 1;
  367. printk(KERN_INFO "DR\n");
  368. }
  369. if (istat&0x20)
  370. printk(KERN_INFO "WC\n");
  371. if (istat&2) {
  372. u8 slotstat;
  373. read_reg(ci, 0x01, &slotstat);
  374. if (!(2&slotstat)) {
  375. if (!ci->slot_stat) {
  376. ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_PRESENT;
  377. write_regm(ci, 0x03, 0x08, 0x08);
  378. }
  379. } else {
  380. if (ci->slot_stat) {
  381. ci->slot_stat = 0;
  382. write_regm(ci, 0x03, 0x00, 0x08);
  383. printk(KERN_INFO "NO CAM\n");
  384. ci->ready = 0;
  385. }
  386. }
  387. if (istat&8 && ci->slot_stat == DVB_CA_EN50221_POLL_CAM_PRESENT) {
  388. ci->ready = 1;
  389. ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_READY;
  390. printk(KERN_INFO "READY\n");
  391. }
  392. }
  393. return 0;
  394. }
  395. static int poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
  396. {
  397. struct cxd *ci = ca->data;
  398. u8 slotstat;
  399. mutex_lock(&ci->lock);
  400. campoll(ci);
  401. read_reg(ci, 0x01, &slotstat);
  402. mutex_unlock(&ci->lock);
  403. return ci->slot_stat;
  404. }
  405. #ifdef BUFFER_MODE
  406. static int read_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
  407. {
  408. struct cxd *ci = ca->data;
  409. u8 msb, lsb;
  410. u16 len;
  411. mutex_lock(&ci->lock);
  412. campoll(ci);
  413. mutex_unlock(&ci->lock);
  414. printk(KERN_INFO "read_data\n");
  415. if (!ci->dr)
  416. return 0;
  417. mutex_lock(&ci->lock);
  418. read_reg(ci, 0x0f, &msb);
  419. read_reg(ci, 0x10, &lsb);
  420. len = (msb<<8)|lsb;
  421. read_block(ci, 0x12, ebuf, len);
  422. ci->dr = 0;
  423. mutex_unlock(&ci->lock);
  424. return len;
  425. }
  426. static int write_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
  427. {
  428. struct cxd *ci = ca->data;
  429. mutex_lock(&ci->lock);
  430. printk(KERN_INFO "write_data %d\n", ecount);
  431. write_reg(ci, 0x0d, ecount>>8);
  432. write_reg(ci, 0x0e, ecount&0xff);
  433. write_block(ci, 0x11, ebuf, ecount);
  434. mutex_unlock(&ci->lock);
  435. return ecount;
  436. }
  437. #endif
  438. static struct dvb_ca_en50221 en_templ = {
  439. .read_attribute_mem = read_attribute_mem,
  440. .write_attribute_mem = write_attribute_mem,
  441. .read_cam_control = read_cam_control,
  442. .write_cam_control = write_cam_control,
  443. .slot_reset = slot_reset,
  444. .slot_shutdown = slot_shutdown,
  445. .slot_ts_enable = slot_ts_enable,
  446. .poll_slot_status = poll_slot_status,
  447. #ifdef BUFFER_MODE
  448. .read_data = read_data,
  449. .write_data = write_data,
  450. #endif
  451. };
  452. struct dvb_ca_en50221 *cxd2099_attach(u8 adr, void *priv,
  453. struct i2c_adapter *i2c)
  454. {
  455. struct cxd *ci = 0;
  456. u32 bitrate = 62000000;
  457. u8 val;
  458. if (i2c_read_reg(i2c, adr, 0, &val) < 0) {
  459. printk(KERN_ERR "No CXD2099 detected at %02x\n", adr);
  460. return 0;
  461. }
  462. ci = kmalloc(sizeof(struct cxd), GFP_KERNEL);
  463. if (!ci)
  464. return 0;
  465. memset(ci, 0, sizeof(*ci));
  466. mutex_init(&ci->lock);
  467. ci->i2c = i2c;
  468. ci->adr = adr;
  469. ci->lastaddress = 0xff;
  470. ci->clk_reg_b = 0x4a;
  471. ci->clk_reg_f = 0x1b;
  472. ci->bitrate = bitrate;
  473. memcpy(&ci->en, &en_templ, sizeof(en_templ));
  474. ci->en.data = ci;
  475. init(ci);
  476. printk(KERN_INFO "Attached CXD2099AR at %02x\n", ci->adr);
  477. return &ci->en;
  478. }
  479. EXPORT_SYMBOL(cxd2099_attach);
  480. MODULE_DESCRIPTION("cxd2099");
  481. MODULE_AUTHOR("Ralph Metzler <rjkm@metzlerbros.de>");
  482. MODULE_LICENSE("GPL");