cx88-i2c.c 4.7 KB

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