cx88-i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. cx88-i2c.c -- all the i2c code is here
  3. Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  4. & Marcus Metzler (mocm@thp.uni-koeln.de)
  5. (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
  6. (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
  7. (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
  8. - Multituner support and i2c address binding
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <asm/io.h>
  24. #include "cx88.h"
  25. #include <media/v4l2-common.h>
  26. static unsigned int i2c_debug;
  27. module_param(i2c_debug, int, 0644);
  28. MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
  29. static unsigned int i2c_scan;
  30. module_param(i2c_scan, int, 0444);
  31. MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
  32. static unsigned int i2c_udelay = 5;
  33. module_param(i2c_udelay, int, 0644);
  34. MODULE_PARM_DESC(i2c_udelay,"i2c delay at insmod time, in usecs "
  35. "(should be 5 or higher). Lower value means higher bus speed.");
  36. #define dprintk(level,fmt, arg...) if (i2c_debug >= level) \
  37. printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)
  38. /* ----------------------------------------------------------------------- */
  39. static void cx8800_bit_setscl(void *data, int state)
  40. {
  41. struct cx88_core *core = data;
  42. if (state)
  43. core->i2c_state |= 0x02;
  44. else
  45. core->i2c_state &= ~0x02;
  46. cx_write(MO_I2C, core->i2c_state);
  47. cx_read(MO_I2C);
  48. }
  49. static void cx8800_bit_setsda(void *data, int state)
  50. {
  51. struct cx88_core *core = data;
  52. if (state)
  53. core->i2c_state |= 0x01;
  54. else
  55. core->i2c_state &= ~0x01;
  56. cx_write(MO_I2C, core->i2c_state);
  57. cx_read(MO_I2C);
  58. }
  59. static int cx8800_bit_getscl(void *data)
  60. {
  61. struct cx88_core *core = data;
  62. u32 state;
  63. state = cx_read(MO_I2C);
  64. return state & 0x02 ? 1 : 0;
  65. }
  66. static int cx8800_bit_getsda(void *data)
  67. {
  68. struct cx88_core *core = data;
  69. u32 state;
  70. state = cx_read(MO_I2C);
  71. return state & 0x01;
  72. }
  73. /* ----------------------------------------------------------------------- */
  74. static const struct i2c_algo_bit_data cx8800_i2c_algo_template = {
  75. .setsda = cx8800_bit_setsda,
  76. .setscl = cx8800_bit_setscl,
  77. .getsda = cx8800_bit_getsda,
  78. .getscl = cx8800_bit_getscl,
  79. .udelay = 16,
  80. .timeout = 200,
  81. };
  82. /* ----------------------------------------------------------------------- */
  83. static const char * const i2c_devs[128] = {
  84. [ 0x1c >> 1 ] = "lgdt330x",
  85. [ 0x86 >> 1 ] = "tda9887/cx22702",
  86. [ 0xa0 >> 1 ] = "eeprom",
  87. [ 0xc0 >> 1 ] = "tuner (analog)",
  88. [ 0xc2 >> 1 ] = "tuner (analog/dvb)",
  89. [ 0xc8 >> 1 ] = "xc5000",
  90. };
  91. static void do_i2c_scan(const char *name, struct i2c_client *c)
  92. {
  93. unsigned char buf;
  94. int i,rc;
  95. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  96. c->addr = i;
  97. rc = i2c_master_recv(c,&buf,0);
  98. if (rc < 0)
  99. continue;
  100. printk("%s: i2c scan: found device @ 0x%x [%s]\n",
  101. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  102. }
  103. }
  104. /* init + register i2c adapter */
  105. int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci)
  106. {
  107. /* Prevents usage of invalid delay values */
  108. if (i2c_udelay<5)
  109. i2c_udelay=5;
  110. core->i2c_algo = cx8800_i2c_algo_template;
  111. core->i2c_adap.dev.parent = &pci->dev;
  112. strlcpy(core->i2c_adap.name,core->name,sizeof(core->i2c_adap.name));
  113. core->i2c_adap.owner = THIS_MODULE;
  114. core->i2c_algo.udelay = i2c_udelay;
  115. core->i2c_algo.data = core;
  116. i2c_set_adapdata(&core->i2c_adap, &core->v4l2_dev);
  117. core->i2c_adap.algo_data = &core->i2c_algo;
  118. core->i2c_client.adapter = &core->i2c_adap;
  119. strlcpy(core->i2c_client.name, "cx88xx internal", I2C_NAME_SIZE);
  120. cx8800_bit_setscl(core,1);
  121. cx8800_bit_setsda(core,1);
  122. core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap);
  123. if (0 == core->i2c_rc) {
  124. static u8 tuner_data[] =
  125. { 0x0b, 0xdc, 0x86, 0x52 };
  126. static struct i2c_msg tuner_msg =
  127. { .flags = 0, .addr = 0xc2 >> 1, .buf = tuner_data, .len = 4 };
  128. dprintk(1, "i2c register ok\n");
  129. switch( core->boardnr ) {
  130. case CX88_BOARD_HAUPPAUGE_HVR1300:
  131. case CX88_BOARD_HAUPPAUGE_HVR3000:
  132. case CX88_BOARD_HAUPPAUGE_HVR4000:
  133. printk("%s: i2c init: enabling analog demod on HVR1300/3000/4000 tuner\n",
  134. core->name);
  135. i2c_transfer(core->i2c_client.adapter, &tuner_msg, 1);
  136. break;
  137. default:
  138. break;
  139. }
  140. if (i2c_scan)
  141. do_i2c_scan(core->name,&core->i2c_client);
  142. } else
  143. printk("%s: i2c register FAILED\n", core->name);
  144. return core->i2c_rc;
  145. }