cxd2099.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cxd2099.c: Driver for the Sony CXD2099AR Common Interface Controller
  4. *
  5. * Copyright (C) 2010-2013 Digital Devices GmbH
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/regmap.h>
  21. #include <linux/wait.h>
  22. #include <linux/delay.h>
  23. #include <linux/mutex.h>
  24. #include <linux/io.h>
  25. #include "cxd2099.h"
  26. static int buffermode;
  27. module_param(buffermode, int, 0444);
  28. MODULE_PARM_DESC(buffermode, "Enable CXD2099AR buffer mode (default: disabled)");
  29. static int read_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount);
  30. struct cxd {
  31. struct dvb_ca_en50221 en;
  32. struct cxd2099_cfg cfg;
  33. struct i2c_client *client;
  34. struct regmap *regmap;
  35. u8 regs[0x23];
  36. u8 lastaddress;
  37. u8 clk_reg_f;
  38. u8 clk_reg_b;
  39. int mode;
  40. int ready;
  41. int dr;
  42. int write_busy;
  43. int slot_stat;
  44. u8 amem[1024];
  45. int amem_read;
  46. int cammode;
  47. struct mutex lock; /* device access lock */
  48. u8 rbuf[1028];
  49. u8 wbuf[1028];
  50. };
  51. static int read_block(struct cxd *ci, u8 adr, u8 *data, u16 n)
  52. {
  53. int status = 0;
  54. if (ci->lastaddress != adr)
  55. status = regmap_write(ci->regmap, 0, adr);
  56. if (!status) {
  57. ci->lastaddress = adr;
  58. while (n) {
  59. int len = n;
  60. if (ci->cfg.max_i2c && len > ci->cfg.max_i2c)
  61. len = ci->cfg.max_i2c;
  62. status = regmap_raw_read(ci->regmap, 1, data, len);
  63. if (status)
  64. return status;
  65. data += len;
  66. n -= len;
  67. }
  68. }
  69. return status;
  70. }
  71. static int read_reg(struct cxd *ci, u8 reg, u8 *val)
  72. {
  73. return read_block(ci, reg, val, 1);
  74. }
  75. static int read_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
  76. {
  77. int status;
  78. u8 addr[2] = {address & 0xff, address >> 8};
  79. status = regmap_raw_write(ci->regmap, 2, addr, 2);
  80. if (!status)
  81. status = regmap_raw_read(ci->regmap, 3, data, n);
  82. return status;
  83. }
  84. static int write_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
  85. {
  86. int status;
  87. u8 addr[2] = {address & 0xff, address >> 8};
  88. status = regmap_raw_write(ci->regmap, 2, addr, 2);
  89. if (!status) {
  90. u8 buf[256];
  91. memcpy(buf, data, n);
  92. status = regmap_raw_write(ci->regmap, 3, buf, n);
  93. }
  94. return status;
  95. }
  96. static int read_io(struct cxd *ci, u16 address, unsigned int *val)
  97. {
  98. int status;
  99. u8 addr[2] = {address & 0xff, address >> 8};
  100. status = regmap_raw_write(ci->regmap, 2, addr, 2);
  101. if (!status)
  102. status = regmap_read(ci->regmap, 3, val);
  103. return status;
  104. }
  105. static int write_io(struct cxd *ci, u16 address, u8 val)
  106. {
  107. int status;
  108. u8 addr[2] = {address & 0xff, address >> 8};
  109. status = regmap_raw_write(ci->regmap, 2, addr, 2);
  110. if (!status)
  111. status = regmap_write(ci->regmap, 3, val);
  112. return status;
  113. }
  114. static int write_regm(struct cxd *ci, u8 reg, u8 val, u8 mask)
  115. {
  116. int status = 0;
  117. unsigned int regval;
  118. if (ci->lastaddress != reg)
  119. status = regmap_write(ci->regmap, 0, reg);
  120. if (!status && reg >= 6 && reg <= 8 && mask != 0xff) {
  121. status = regmap_read(ci->regmap, 1, &regval);
  122. ci->regs[reg] = regval;
  123. }
  124. ci->lastaddress = reg;
  125. ci->regs[reg] = (ci->regs[reg] & (~mask)) | val;
  126. if (!status)
  127. status = regmap_write(ci->regmap, 1, ci->regs[reg]);
  128. if (reg == 0x20)
  129. ci->regs[reg] &= 0x7f;
  130. return status;
  131. }
  132. static int write_reg(struct cxd *ci, u8 reg, u8 val)
  133. {
  134. return write_regm(ci, reg, val, 0xff);
  135. }
  136. static int write_block(struct cxd *ci, u8 adr, u8 *data, u16 n)
  137. {
  138. int status = 0;
  139. u8 *buf = ci->wbuf;
  140. if (ci->lastaddress != adr)
  141. status = regmap_write(ci->regmap, 0, adr);
  142. if (status)
  143. return status;
  144. ci->lastaddress = adr;
  145. while (n) {
  146. int len = n;
  147. if (ci->cfg.max_i2c && (len + 1 > ci->cfg.max_i2c))
  148. len = ci->cfg.max_i2c - 1;
  149. memcpy(buf, data, len);
  150. status = regmap_raw_write(ci->regmap, 1, buf, len);
  151. if (status)
  152. return status;
  153. n -= len;
  154. data += len;
  155. }
  156. return status;
  157. }
  158. static void set_mode(struct cxd *ci, int mode)
  159. {
  160. if (mode == ci->mode)
  161. return;
  162. switch (mode) {
  163. case 0x00: /* IO mem */
  164. write_regm(ci, 0x06, 0x00, 0x07);
  165. break;
  166. case 0x01: /* ATT mem */
  167. write_regm(ci, 0x06, 0x02, 0x07);
  168. break;
  169. default:
  170. break;
  171. }
  172. ci->mode = mode;
  173. }
  174. static void cam_mode(struct cxd *ci, int mode)
  175. {
  176. u8 dummy;
  177. if (mode == ci->cammode)
  178. return;
  179. switch (mode) {
  180. case 0x00:
  181. write_regm(ci, 0x20, 0x80, 0x80);
  182. break;
  183. case 0x01:
  184. if (!ci->en.read_data)
  185. return;
  186. ci->write_busy = 0;
  187. dev_info(&ci->client->dev, "enable cam buffer mode\n");
  188. write_reg(ci, 0x0d, 0x00);
  189. write_reg(ci, 0x0e, 0x01);
  190. write_regm(ci, 0x08, 0x40, 0x40);
  191. read_reg(ci, 0x12, &dummy);
  192. write_regm(ci, 0x08, 0x80, 0x80);
  193. break;
  194. default:
  195. break;
  196. }
  197. ci->cammode = mode;
  198. }
  199. static int init(struct cxd *ci)
  200. {
  201. int status;
  202. mutex_lock(&ci->lock);
  203. ci->mode = -1;
  204. do {
  205. status = write_reg(ci, 0x00, 0x00);
  206. if (status < 0)
  207. break;
  208. status = write_reg(ci, 0x01, 0x00);
  209. if (status < 0)
  210. break;
  211. status = write_reg(ci, 0x02, 0x10);
  212. if (status < 0)
  213. break;
  214. status = write_reg(ci, 0x03, 0x00);
  215. if (status < 0)
  216. break;
  217. status = write_reg(ci, 0x05, 0xFF);
  218. if (status < 0)
  219. break;
  220. status = write_reg(ci, 0x06, 0x1F);
  221. if (status < 0)
  222. break;
  223. status = write_reg(ci, 0x07, 0x1F);
  224. if (status < 0)
  225. break;
  226. status = write_reg(ci, 0x08, 0x28);
  227. if (status < 0)
  228. break;
  229. status = write_reg(ci, 0x14, 0x20);
  230. if (status < 0)
  231. break;
  232. /* TOSTRT = 8, Mode B (gated clock), falling Edge,
  233. * Serial, POL=HIGH, MSB
  234. */
  235. status = write_reg(ci, 0x0A, 0xA7);
  236. if (status < 0)
  237. break;
  238. status = write_reg(ci, 0x0B, 0x33);
  239. if (status < 0)
  240. break;
  241. status = write_reg(ci, 0x0C, 0x33);
  242. if (status < 0)
  243. break;
  244. status = write_regm(ci, 0x14, 0x00, 0x0F);
  245. if (status < 0)
  246. break;
  247. status = write_reg(ci, 0x15, ci->clk_reg_b);
  248. if (status < 0)
  249. break;
  250. status = write_regm(ci, 0x16, 0x00, 0x0F);
  251. if (status < 0)
  252. break;
  253. status = write_reg(ci, 0x17, ci->clk_reg_f);
  254. if (status < 0)
  255. break;
  256. if (ci->cfg.clock_mode == 2) {
  257. /* bitrate*2^13/ 72000 */
  258. u32 reg = ((ci->cfg.bitrate << 13) + 71999) / 72000;
  259. if (ci->cfg.polarity) {
  260. status = write_reg(ci, 0x09, 0x6f);
  261. if (status < 0)
  262. break;
  263. } else {
  264. status = write_reg(ci, 0x09, 0x6d);
  265. if (status < 0)
  266. break;
  267. }
  268. status = write_reg(ci, 0x20, 0x08);
  269. if (status < 0)
  270. break;
  271. status = write_reg(ci, 0x21, (reg >> 8) & 0xff);
  272. if (status < 0)
  273. break;
  274. status = write_reg(ci, 0x22, reg & 0xff);
  275. if (status < 0)
  276. break;
  277. } else if (ci->cfg.clock_mode == 1) {
  278. if (ci->cfg.polarity) {
  279. status = write_reg(ci, 0x09, 0x6f); /* D */
  280. if (status < 0)
  281. break;
  282. } else {
  283. status = write_reg(ci, 0x09, 0x6d);
  284. if (status < 0)
  285. break;
  286. }
  287. status = write_reg(ci, 0x20, 0x68);
  288. if (status < 0)
  289. break;
  290. status = write_reg(ci, 0x21, 0x00);
  291. if (status < 0)
  292. break;
  293. status = write_reg(ci, 0x22, 0x02);
  294. if (status < 0)
  295. break;
  296. } else {
  297. if (ci->cfg.polarity) {
  298. status = write_reg(ci, 0x09, 0x4f); /* C */
  299. if (status < 0)
  300. break;
  301. } else {
  302. status = write_reg(ci, 0x09, 0x4d);
  303. if (status < 0)
  304. break;
  305. }
  306. status = write_reg(ci, 0x20, 0x28);
  307. if (status < 0)
  308. break;
  309. status = write_reg(ci, 0x21, 0x00);
  310. if (status < 0)
  311. break;
  312. status = write_reg(ci, 0x22, 0x07);
  313. if (status < 0)
  314. break;
  315. }
  316. status = write_regm(ci, 0x20, 0x80, 0x80);
  317. if (status < 0)
  318. break;
  319. status = write_regm(ci, 0x03, 0x02, 0x02);
  320. if (status < 0)
  321. break;
  322. status = write_reg(ci, 0x01, 0x04);
  323. if (status < 0)
  324. break;
  325. status = write_reg(ci, 0x00, 0x31);
  326. if (status < 0)
  327. break;
  328. /* Put TS in bypass */
  329. status = write_regm(ci, 0x09, 0x08, 0x08);
  330. if (status < 0)
  331. break;
  332. ci->cammode = -1;
  333. cam_mode(ci, 0);
  334. } while (0);
  335. mutex_unlock(&ci->lock);
  336. return 0;
  337. }
  338. static int read_attribute_mem(struct dvb_ca_en50221 *ca,
  339. int slot, int address)
  340. {
  341. struct cxd *ci = ca->data;
  342. u8 val;
  343. mutex_lock(&ci->lock);
  344. set_mode(ci, 1);
  345. read_pccard(ci, address, &val, 1);
  346. mutex_unlock(&ci->lock);
  347. return val;
  348. }
  349. static int write_attribute_mem(struct dvb_ca_en50221 *ca, int slot,
  350. int address, u8 value)
  351. {
  352. struct cxd *ci = ca->data;
  353. mutex_lock(&ci->lock);
  354. set_mode(ci, 1);
  355. write_pccard(ci, address, &value, 1);
  356. mutex_unlock(&ci->lock);
  357. return 0;
  358. }
  359. static int read_cam_control(struct dvb_ca_en50221 *ca,
  360. int slot, u8 address)
  361. {
  362. struct cxd *ci = ca->data;
  363. unsigned int val;
  364. mutex_lock(&ci->lock);
  365. set_mode(ci, 0);
  366. read_io(ci, address, &val);
  367. mutex_unlock(&ci->lock);
  368. return val;
  369. }
  370. static int write_cam_control(struct dvb_ca_en50221 *ca, int slot,
  371. u8 address, u8 value)
  372. {
  373. struct cxd *ci = ca->data;
  374. mutex_lock(&ci->lock);
  375. set_mode(ci, 0);
  376. write_io(ci, address, value);
  377. mutex_unlock(&ci->lock);
  378. return 0;
  379. }
  380. static int slot_reset(struct dvb_ca_en50221 *ca, int slot)
  381. {
  382. struct cxd *ci = ca->data;
  383. if (ci->cammode)
  384. read_data(ca, slot, ci->rbuf, 0);
  385. mutex_lock(&ci->lock);
  386. cam_mode(ci, 0);
  387. write_reg(ci, 0x00, 0x21);
  388. write_reg(ci, 0x06, 0x1F);
  389. write_reg(ci, 0x00, 0x31);
  390. write_regm(ci, 0x20, 0x80, 0x80);
  391. write_reg(ci, 0x03, 0x02);
  392. ci->ready = 0;
  393. ci->mode = -1;
  394. {
  395. int i;
  396. for (i = 0; i < 100; i++) {
  397. usleep_range(10000, 11000);
  398. if (ci->ready)
  399. break;
  400. }
  401. }
  402. mutex_unlock(&ci->lock);
  403. return 0;
  404. }
  405. static int slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
  406. {
  407. struct cxd *ci = ca->data;
  408. dev_dbg(&ci->client->dev, "%s\n", __func__);
  409. if (ci->cammode)
  410. read_data(ca, slot, ci->rbuf, 0);
  411. mutex_lock(&ci->lock);
  412. write_reg(ci, 0x00, 0x21);
  413. write_reg(ci, 0x06, 0x1F);
  414. msleep(300);
  415. write_regm(ci, 0x09, 0x08, 0x08);
  416. write_regm(ci, 0x20, 0x80, 0x80); /* Reset CAM Mode */
  417. write_regm(ci, 0x06, 0x07, 0x07); /* Clear IO Mode */
  418. ci->mode = -1;
  419. ci->write_busy = 0;
  420. mutex_unlock(&ci->lock);
  421. return 0;
  422. }
  423. static int slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
  424. {
  425. struct cxd *ci = ca->data;
  426. mutex_lock(&ci->lock);
  427. write_regm(ci, 0x09, 0x00, 0x08);
  428. set_mode(ci, 0);
  429. cam_mode(ci, 1);
  430. mutex_unlock(&ci->lock);
  431. return 0;
  432. }
  433. static int campoll(struct cxd *ci)
  434. {
  435. u8 istat;
  436. read_reg(ci, 0x04, &istat);
  437. if (!istat)
  438. return 0;
  439. write_reg(ci, 0x05, istat);
  440. if (istat & 0x40)
  441. ci->dr = 1;
  442. if (istat & 0x20)
  443. ci->write_busy = 0;
  444. if (istat & 2) {
  445. u8 slotstat;
  446. read_reg(ci, 0x01, &slotstat);
  447. if (!(2 & slotstat)) {
  448. if (!ci->slot_stat) {
  449. ci->slot_stat |=
  450. DVB_CA_EN50221_POLL_CAM_PRESENT;
  451. write_regm(ci, 0x03, 0x08, 0x08);
  452. }
  453. } else {
  454. if (ci->slot_stat) {
  455. ci->slot_stat = 0;
  456. write_regm(ci, 0x03, 0x00, 0x08);
  457. dev_info(&ci->client->dev, "NO CAM\n");
  458. ci->ready = 0;
  459. }
  460. }
  461. if ((istat & 8) &&
  462. ci->slot_stat == DVB_CA_EN50221_POLL_CAM_PRESENT) {
  463. ci->ready = 1;
  464. ci->slot_stat |= DVB_CA_EN50221_POLL_CAM_READY;
  465. }
  466. }
  467. return 0;
  468. }
  469. static int poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
  470. {
  471. struct cxd *ci = ca->data;
  472. u8 slotstat;
  473. mutex_lock(&ci->lock);
  474. campoll(ci);
  475. read_reg(ci, 0x01, &slotstat);
  476. mutex_unlock(&ci->lock);
  477. return ci->slot_stat;
  478. }
  479. static int read_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
  480. {
  481. struct cxd *ci = ca->data;
  482. u8 msb, lsb;
  483. u16 len;
  484. mutex_lock(&ci->lock);
  485. campoll(ci);
  486. mutex_unlock(&ci->lock);
  487. if (!ci->dr)
  488. return 0;
  489. mutex_lock(&ci->lock);
  490. read_reg(ci, 0x0f, &msb);
  491. read_reg(ci, 0x10, &lsb);
  492. len = ((u16)msb << 8) | lsb;
  493. if (len > ecount || len < 2) {
  494. /* read it anyway or cxd may hang */
  495. read_block(ci, 0x12, ci->rbuf, len);
  496. mutex_unlock(&ci->lock);
  497. return -EIO;
  498. }
  499. read_block(ci, 0x12, ebuf, len);
  500. ci->dr = 0;
  501. mutex_unlock(&ci->lock);
  502. return len;
  503. }
  504. static int write_data(struct dvb_ca_en50221 *ca, int slot, u8 *ebuf, int ecount)
  505. {
  506. struct cxd *ci = ca->data;
  507. if (ci->write_busy)
  508. return -EAGAIN;
  509. mutex_lock(&ci->lock);
  510. write_reg(ci, 0x0d, ecount >> 8);
  511. write_reg(ci, 0x0e, ecount & 0xff);
  512. write_block(ci, 0x11, ebuf, ecount);
  513. ci->write_busy = 1;
  514. mutex_unlock(&ci->lock);
  515. return ecount;
  516. }
  517. static const struct dvb_ca_en50221 en_templ = {
  518. .read_attribute_mem = read_attribute_mem,
  519. .write_attribute_mem = write_attribute_mem,
  520. .read_cam_control = read_cam_control,
  521. .write_cam_control = write_cam_control,
  522. .slot_reset = slot_reset,
  523. .slot_shutdown = slot_shutdown,
  524. .slot_ts_enable = slot_ts_enable,
  525. .poll_slot_status = poll_slot_status,
  526. .read_data = read_data,
  527. .write_data = write_data,
  528. };
  529. static int cxd2099_probe(struct i2c_client *client,
  530. const struct i2c_device_id *id)
  531. {
  532. struct cxd *ci;
  533. struct cxd2099_cfg *cfg = client->dev.platform_data;
  534. static const struct regmap_config rm_cfg = {
  535. .reg_bits = 8,
  536. .val_bits = 8,
  537. };
  538. unsigned int val;
  539. int ret;
  540. ci = kzalloc(sizeof(*ci), GFP_KERNEL);
  541. if (!ci) {
  542. ret = -ENOMEM;
  543. goto err;
  544. }
  545. ci->client = client;
  546. memcpy(&ci->cfg, cfg, sizeof(ci->cfg));
  547. ci->regmap = regmap_init_i2c(client, &rm_cfg);
  548. if (IS_ERR(ci->regmap)) {
  549. ret = PTR_ERR(ci->regmap);
  550. goto err_kfree;
  551. }
  552. ret = regmap_read(ci->regmap, 0x00, &val);
  553. if (ret < 0) {
  554. dev_info(&client->dev, "No CXD2099AR detected at 0x%02x\n",
  555. client->addr);
  556. goto err_rmexit;
  557. }
  558. mutex_init(&ci->lock);
  559. ci->lastaddress = 0xff;
  560. ci->clk_reg_b = 0x4a;
  561. ci->clk_reg_f = 0x1b;
  562. ci->en = en_templ;
  563. ci->en.data = ci;
  564. init(ci);
  565. dev_info(&client->dev, "Attached CXD2099AR at 0x%02x\n", client->addr);
  566. *cfg->en = &ci->en;
  567. if (!buffermode) {
  568. ci->en.read_data = NULL;
  569. ci->en.write_data = NULL;
  570. } else {
  571. dev_info(&client->dev, "Using CXD2099AR buffer mode");
  572. }
  573. i2c_set_clientdata(client, ci);
  574. return 0;
  575. err_rmexit:
  576. regmap_exit(ci->regmap);
  577. err_kfree:
  578. kfree(ci);
  579. err:
  580. return ret;
  581. }
  582. static int cxd2099_remove(struct i2c_client *client)
  583. {
  584. struct cxd *ci = i2c_get_clientdata(client);
  585. regmap_exit(ci->regmap);
  586. kfree(ci);
  587. return 0;
  588. }
  589. static const struct i2c_device_id cxd2099_id[] = {
  590. {"cxd2099", 0},
  591. {}
  592. };
  593. MODULE_DEVICE_TABLE(i2c, cxd2099_id);
  594. static struct i2c_driver cxd2099_driver = {
  595. .driver = {
  596. .name = "cxd2099",
  597. },
  598. .probe = cxd2099_probe,
  599. .remove = cxd2099_remove,
  600. .id_table = cxd2099_id,
  601. };
  602. module_i2c_driver(cxd2099_driver);
  603. MODULE_DESCRIPTION("Sony CXD2099AR Common Interface controller driver");
  604. MODULE_AUTHOR("Ralph Metzler");
  605. MODULE_LICENSE("GPL v2");