saa7134-i2c.c 11 KB

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