saa7134-i2c.c 11 KB

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