saa7134-i2c.c 12 KB

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