saa7134-i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * device driver for philips saa7134 based TV cards
  5. * i2c interface support
  6. *
  7. * (c) 2001,02 Gerd Knorr <[email protected]> [SuSE Labs]
  8. */
  9. #include "saa7134.h"
  10. #include "saa7134-reg.h"
  11. #include <linux/init.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <media/v4l2-common.h>
  17. /* ----------------------------------------------------------- */
  18. static unsigned int i2c_debug;
  19. module_param(i2c_debug, int, 0644);
  20. MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
  21. static unsigned int i2c_scan;
  22. module_param(i2c_scan, int, 0444);
  23. MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
  24. #define i2c_dbg(level, fmt, arg...) do { \
  25. if (i2c_debug == level) \
  26. printk(KERN_DEBUG pr_fmt("i2c: " fmt), ## arg); \
  27. } while (0)
  28. #define i2c_cont(level, fmt, arg...) do { \
  29. if (i2c_debug == level) \
  30. pr_cont(fmt, ## arg); \
  31. } while (0)
  32. #define I2C_WAIT_DELAY 32
  33. #define I2C_WAIT_RETRY 16
  34. /* ----------------------------------------------------------- */
  35. static char *str_i2c_status[] = {
  36. "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
  37. "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
  38. "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
  39. };
  40. enum i2c_status {
  41. IDLE = 0, // no I2C command pending
  42. DONE_STOP = 1, // I2C command done and STOP executed
  43. BUSY = 2, // executing I2C command
  44. TO_SCL = 3, // executing I2C command, time out on clock stretching
  45. TO_ARB = 4, // time out on arbitration trial, still trying
  46. DONE_WRITE = 5, // I2C command done and awaiting next write command
  47. DONE_READ = 6, // I2C command done and awaiting next read command
  48. DONE_WRITE_TO = 7, // see 5, and time out on status echo
  49. DONE_READ_TO = 8, // see 6, and time out on status echo
  50. NO_DEVICE = 9, // no acknowledge on device slave address
  51. NO_ACKN = 10, // no acknowledge after data byte transfer
  52. BUS_ERR = 11, // bus error
  53. ARB_LOST = 12, // arbitration lost during transfer
  54. SEQ_ERR = 13, // erroneous programming sequence
  55. ST_ERR = 14, // wrong status echoing
  56. SW_ERR = 15 // software error
  57. };
  58. static char *str_i2c_attr[] = {
  59. "NOP", "STOP", "CONTINUE", "START"
  60. };
  61. enum i2c_attr {
  62. NOP = 0, // no operation on I2C bus
  63. STOP = 1, // stop condition, no associated byte transfer
  64. CONTINUE = 2, // continue with byte transfer
  65. START = 3 // start condition with byte transfer
  66. };
  67. static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
  68. {
  69. enum i2c_status status;
  70. status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
  71. i2c_dbg(2, "i2c stat <= %s\n", str_i2c_status[status]);
  72. return status;
  73. }
  74. static inline void i2c_set_status(struct saa7134_dev *dev,
  75. enum i2c_status status)
  76. {
  77. i2c_dbg(2, "i2c stat => %s\n", str_i2c_status[status]);
  78. saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
  79. }
  80. static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
  81. {
  82. i2c_dbg(2, "i2c attr => %s\n", str_i2c_attr[attr]);
  83. saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
  84. }
  85. static inline int i2c_is_error(enum i2c_status status)
  86. {
  87. switch (status) {
  88. case NO_DEVICE:
  89. case NO_ACKN:
  90. case BUS_ERR:
  91. case ARB_LOST:
  92. case SEQ_ERR:
  93. case ST_ERR:
  94. return true;
  95. default:
  96. return false;
  97. }
  98. }
  99. static inline int i2c_is_idle(enum i2c_status status)
  100. {
  101. switch (status) {
  102. case IDLE:
  103. case DONE_STOP:
  104. return true;
  105. default:
  106. return false;
  107. }
  108. }
  109. static inline int i2c_is_busy(enum i2c_status status)
  110. {
  111. switch (status) {
  112. case BUSY:
  113. case TO_SCL:
  114. case TO_ARB:
  115. return true;
  116. default:
  117. return false;
  118. }
  119. }
  120. static int i2c_is_busy_wait(struct saa7134_dev *dev)
  121. {
  122. enum i2c_status status;
  123. int count;
  124. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  125. status = i2c_get_status(dev);
  126. if (!i2c_is_busy(status))
  127. break;
  128. saa_wait(I2C_WAIT_DELAY);
  129. }
  130. if (I2C_WAIT_RETRY == count)
  131. return false;
  132. return true;
  133. }
  134. static int i2c_reset(struct saa7134_dev *dev)
  135. {
  136. enum i2c_status status;
  137. int count;
  138. i2c_dbg(2, "i2c reset\n");
  139. status = i2c_get_status(dev);
  140. if (!i2c_is_error(status))
  141. return true;
  142. i2c_set_status(dev,status);
  143. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  144. status = i2c_get_status(dev);
  145. if (!i2c_is_error(status))
  146. break;
  147. udelay(I2C_WAIT_DELAY);
  148. }
  149. if (I2C_WAIT_RETRY == count)
  150. return false;
  151. if (!i2c_is_idle(status))
  152. return false;
  153. i2c_set_attr(dev,NOP);
  154. return true;
  155. }
  156. static inline int i2c_send_byte(struct saa7134_dev *dev,
  157. enum i2c_attr attr,
  158. unsigned char data)
  159. {
  160. enum i2c_status status;
  161. __u32 dword;
  162. /* have to write both attr + data in one 32bit word */
  163. dword = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
  164. dword &= 0x0f;
  165. dword |= (attr << 6);
  166. dword |= ((__u32)data << 8);
  167. dword |= 0x00 << 16; /* 100 kHz */
  168. // dword |= 0x40 << 16; /* 400 kHz */
  169. dword |= 0xf0 << 24;
  170. saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
  171. i2c_dbg(2, "i2c data => 0x%x\n", data);
  172. if (!i2c_is_busy_wait(dev))
  173. return -EIO;
  174. status = i2c_get_status(dev);
  175. if (i2c_is_error(status))
  176. return -EIO;
  177. return 0;
  178. }
  179. static inline int i2c_recv_byte(struct saa7134_dev *dev)
  180. {
  181. enum i2c_status status;
  182. unsigned char data;
  183. i2c_set_attr(dev,CONTINUE);
  184. if (!i2c_is_busy_wait(dev))
  185. return -EIO;
  186. status = i2c_get_status(dev);
  187. if (i2c_is_error(status))
  188. return -EIO;
  189. data = saa_readb(SAA7134_I2C_DATA);
  190. i2c_dbg(2, "i2c data <= 0x%x\n", data);
  191. return data;
  192. }
  193. static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
  194. struct i2c_msg *msgs, int num)
  195. {
  196. struct saa7134_dev *dev = i2c_adap->algo_data;
  197. enum i2c_status status;
  198. unsigned char data;
  199. int addr,rc,i,byte;
  200. status = i2c_get_status(dev);
  201. if (!i2c_is_idle(status))
  202. if (!i2c_reset(dev))
  203. return -EIO;
  204. i2c_dbg(2, "start xfer\n");
  205. i2c_dbg(1, "i2c xfer:");
  206. for (i = 0; i < num; i++) {
  207. if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
  208. /* send address */
  209. i2c_dbg(2, "send address\n");
  210. addr = msgs[i].addr << 1;
  211. if (msgs[i].flags & I2C_M_RD)
  212. addr |= 1;
  213. if (i > 0 && msgs[i].flags &
  214. I2C_M_RD && msgs[i].addr != 0x40 &&
  215. msgs[i].addr != 0x41 &&
  216. msgs[i].addr != 0x19) {
  217. /* workaround for a saa7134 i2c bug
  218. * needed to talk to the mt352 demux
  219. * thanks to pinnacle for the hint */
  220. int quirk = 0xfe;
  221. i2c_cont(1, " [%02x quirk]", quirk);
  222. i2c_send_byte(dev,START,quirk);
  223. i2c_recv_byte(dev);
  224. }
  225. i2c_cont(1, " < %02x", addr);
  226. rc = i2c_send_byte(dev,START,addr);
  227. if (rc < 0)
  228. goto err;
  229. }
  230. if (msgs[i].flags & I2C_M_RD) {
  231. /* read bytes */
  232. i2c_dbg(2, "read bytes\n");
  233. for (byte = 0; byte < msgs[i].len; byte++) {
  234. i2c_cont(1, " =");
  235. rc = i2c_recv_byte(dev);
  236. if (rc < 0)
  237. goto err;
  238. i2c_cont(1, "%02x", rc);
  239. msgs[i].buf[byte] = rc;
  240. }
  241. /* discard mysterious extra byte when reading
  242. from Samsung S5H1411. i2c bus gets error
  243. if we do not. */
  244. if (0x19 == msgs[i].addr) {
  245. i2c_cont(1, " ?");
  246. rc = i2c_recv_byte(dev);
  247. if (rc < 0)
  248. goto err;
  249. i2c_cont(1, "%02x", rc);
  250. }
  251. } else {
  252. /* write bytes */
  253. i2c_dbg(2, "write bytes\n");
  254. for (byte = 0; byte < msgs[i].len; byte++) {
  255. data = msgs[i].buf[byte];
  256. i2c_cont(1, " %02x", data);
  257. rc = i2c_send_byte(dev,CONTINUE,data);
  258. if (rc < 0)
  259. goto err;
  260. }
  261. }
  262. }
  263. i2c_dbg(2, "xfer done\n");
  264. i2c_cont(1, " >");
  265. i2c_set_attr(dev,STOP);
  266. rc = -EIO;
  267. if (!i2c_is_busy_wait(dev))
  268. goto err;
  269. status = i2c_get_status(dev);
  270. if (i2c_is_error(status))
  271. goto err;
  272. /* ensure that the bus is idle for at least one bit slot */
  273. msleep(1);
  274. i2c_cont(1, "\n");
  275. return num;
  276. err:
  277. if (1 == i2c_debug) {
  278. status = i2c_get_status(dev);
  279. i2c_cont(1, " ERROR: %s\n", str_i2c_status[status]);
  280. }
  281. return rc;
  282. }
  283. /* ----------------------------------------------------------- */
  284. static u32 functionality(struct i2c_adapter *adap)
  285. {
  286. return I2C_FUNC_SMBUS_EMUL;
  287. }
  288. static const struct i2c_algorithm saa7134_algo = {
  289. .master_xfer = saa7134_i2c_xfer,
  290. .functionality = functionality,
  291. };
  292. static const struct i2c_adapter saa7134_adap_template = {
  293. .owner = THIS_MODULE,
  294. .name = "saa7134",
  295. .algo = &saa7134_algo,
  296. };
  297. static const struct i2c_client saa7134_client_template = {
  298. .name = "saa7134 internal",
  299. };
  300. /* ----------------------------------------------------------- */
  301. /*
  302. * On Medion 7134 reading the SAA7134 chip config EEPROM needs DVB-T
  303. * demod i2c gate closed due to an address clash between this EEPROM
  304. * and the demod one.
  305. */
  306. static void saa7134_i2c_eeprom_md7134_gate(struct saa7134_dev *dev)
  307. {
  308. u8 subaddr = 0x7, dmdregval;
  309. u8 data[2];
  310. int ret;
  311. struct i2c_msg i2cgatemsg_r[] = { {.addr = 0x08, .flags = 0,
  312. .buf = &subaddr, .len = 1},
  313. {.addr = 0x08,
  314. .flags = I2C_M_RD,
  315. .buf = &dmdregval, .len = 1}
  316. };
  317. struct i2c_msg i2cgatemsg_w[] = { {.addr = 0x08, .flags = 0,
  318. .buf = data, .len = 2} };
  319. ret = i2c_transfer(&dev->i2c_adap, i2cgatemsg_r, 2);
  320. if ((ret == 2) && (dmdregval & 0x2)) {
  321. pr_debug("%s: DVB-T demod i2c gate was left open\n",
  322. dev->name);
  323. data[0] = subaddr;
  324. data[1] = (dmdregval & ~0x2);
  325. if (i2c_transfer(&dev->i2c_adap, i2cgatemsg_w, 1) != 1)
  326. pr_err("%s: EEPROM i2c gate close failure\n",
  327. dev->name);
  328. }
  329. }
  330. static int
  331. saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
  332. {
  333. unsigned char buf;
  334. int i,err;
  335. if (dev->board == SAA7134_BOARD_MD7134)
  336. saa7134_i2c_eeprom_md7134_gate(dev);
  337. dev->i2c_client.addr = 0xa0 >> 1;
  338. buf = 0;
  339. if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
  340. pr_info("%s: Huh, no eeprom present (err=%d)?\n",
  341. dev->name,err);
  342. return -1;
  343. }
  344. if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
  345. pr_warn("%s: i2c eeprom read error (err=%d)\n",
  346. dev->name,err);
  347. return -1;
  348. }
  349. for (i = 0; i < len; i += 16) {
  350. int size = (len - i) > 16 ? 16 : len - i;
  351. pr_info("i2c eeprom %02x: %*ph\n", i, size, &eedata[i]);
  352. }
  353. return 0;
  354. }
  355. static char *i2c_devs[128] = {
  356. [ 0x20 ] = "mpeg encoder (saa6752hs)",
  357. [ 0xa0 >> 1 ] = "eeprom",
  358. [ 0xc0 >> 1 ] = "tuner (analog)",
  359. [ 0x86 >> 1 ] = "tda9887",
  360. [ 0x5a >> 1 ] = "remote control",
  361. };
  362. static void do_i2c_scan(struct i2c_client *c)
  363. {
  364. unsigned char buf;
  365. int i,rc;
  366. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  367. c->addr = i;
  368. rc = i2c_master_recv(c,&buf,0);
  369. if (rc < 0)
  370. continue;
  371. pr_info("i2c scan: found device @ 0x%x [%s]\n",
  372. i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  373. }
  374. }
  375. int saa7134_i2c_register(struct saa7134_dev *dev)
  376. {
  377. dev->i2c_adap = saa7134_adap_template;
  378. dev->i2c_adap.dev.parent = &dev->pci->dev;
  379. strscpy(dev->i2c_adap.name, dev->name, sizeof(dev->i2c_adap.name));
  380. dev->i2c_adap.algo_data = dev;
  381. i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
  382. i2c_add_adapter(&dev->i2c_adap);
  383. dev->i2c_client = saa7134_client_template;
  384. dev->i2c_client.adapter = &dev->i2c_adap;
  385. saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
  386. if (i2c_scan)
  387. do_i2c_scan(&dev->i2c_client);
  388. /* Instantiate the IR receiver device, if present */
  389. saa7134_probe_i2c_ir(dev);
  390. return 0;
  391. }
  392. int saa7134_i2c_unregister(struct saa7134_dev *dev)
  393. {
  394. i2c_del_adapter(&dev->i2c_adap);
  395. return 0;
  396. }