saa7134-i2c.c 11 KB

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