cx88-i2c.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. $Id: cx88-i2c.c,v 1.30 2005/07/25 05:10:13 mkrufky Exp $
  3. cx88-i2c.c -- all the i2c code is here
  4. Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  5. & Marcus Metzler (mocm@thp.uni-koeln.de)
  6. (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
  7. (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
  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. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/init.h>
  23. #include <asm/io.h>
  24. #include "cx88.h"
  25. static unsigned int i2c_debug = 0;
  26. module_param(i2c_debug, int, 0644);
  27. MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
  28. static unsigned int i2c_scan = 0;
  29. module_param(i2c_scan, int, 0444);
  30. MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
  31. #define dprintk(level,fmt, arg...) if (i2c_debug >= level) \
  32. printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)
  33. /* ----------------------------------------------------------------------- */
  34. static void cx8800_bit_setscl(void *data, int state)
  35. {
  36. struct cx88_core *core = data;
  37. if (state)
  38. core->i2c_state |= 0x02;
  39. else
  40. core->i2c_state &= ~0x02;
  41. cx_write(MO_I2C, core->i2c_state);
  42. cx_read(MO_I2C);
  43. }
  44. static void cx8800_bit_setsda(void *data, int state)
  45. {
  46. struct cx88_core *core = data;
  47. if (state)
  48. core->i2c_state |= 0x01;
  49. else
  50. core->i2c_state &= ~0x01;
  51. cx_write(MO_I2C, core->i2c_state);
  52. cx_read(MO_I2C);
  53. }
  54. static int cx8800_bit_getscl(void *data)
  55. {
  56. struct cx88_core *core = data;
  57. u32 state;
  58. state = cx_read(MO_I2C);
  59. return state & 0x02 ? 1 : 0;
  60. }
  61. static int cx8800_bit_getsda(void *data)
  62. {
  63. struct cx88_core *core = data;
  64. u32 state;
  65. state = cx_read(MO_I2C);
  66. return state & 0x01;
  67. }
  68. /* ----------------------------------------------------------------------- */
  69. static int attach_inform(struct i2c_client *client)
  70. {
  71. struct tuner_setup tun_setup;
  72. struct cx88_core *core = i2c_get_adapdata(client->adapter);
  73. dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
  74. client->driver->name, client->addr, client->name);
  75. if (!client->driver->command)
  76. return 0;
  77. if (core->radio_type != UNSET) {
  78. if ((core->radio_addr==ADDR_UNSET)||(core->radio_addr==client->addr)) {
  79. tun_setup.mode_mask = T_RADIO;
  80. tun_setup.type = core->radio_type;
  81. tun_setup.addr = core->radio_addr;
  82. client->driver->command (client, TUNER_SET_TYPE_ADDR, &tun_setup);
  83. }
  84. }
  85. if (core->tuner_type != UNSET) {
  86. if ((core->tuner_addr==ADDR_UNSET)||(core->tuner_addr==client->addr)) {
  87. tun_setup.mode_mask = T_ANALOG_TV;
  88. tun_setup.type = core->tuner_type;
  89. tun_setup.addr = core->tuner_addr;
  90. client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup);
  91. }
  92. }
  93. if (core->tda9887_conf)
  94. client->driver->command(client, TDA9887_SET_CONFIG, &core->tda9887_conf);
  95. return 0;
  96. }
  97. static int detach_inform(struct i2c_client *client)
  98. {
  99. struct cx88_core *core = i2c_get_adapdata(client->adapter);
  100. dprintk(1, "i2c detach [client=%s]\n", client->name);
  101. return 0;
  102. }
  103. void cx88_call_i2c_clients(struct cx88_core *core, unsigned int cmd, void *arg)
  104. {
  105. if (0 != core->i2c_rc)
  106. return;
  107. i2c_clients_command(&core->i2c_adap, cmd, arg);
  108. }
  109. static struct i2c_algo_bit_data cx8800_i2c_algo_template = {
  110. .setsda = cx8800_bit_setsda,
  111. .setscl = cx8800_bit_setscl,
  112. .getsda = cx8800_bit_getsda,
  113. .getscl = cx8800_bit_getscl,
  114. .udelay = 16,
  115. .mdelay = 10,
  116. .timeout = 200,
  117. };
  118. /* ----------------------------------------------------------------------- */
  119. static struct i2c_adapter cx8800_i2c_adap_template = {
  120. .name = "cx2388x",
  121. .owner = THIS_MODULE,
  122. .id = I2C_HW_B_CX2388x,
  123. .client_register = attach_inform,
  124. .client_unregister = detach_inform,
  125. };
  126. static struct i2c_client cx8800_i2c_client_template = {
  127. .name = "cx88xx internal",
  128. };
  129. static char *i2c_devs[128] = {
  130. [ 0x1c >> 1 ] = "lgdt330x",
  131. [ 0x86 >> 1 ] = "tda9887/cx22702",
  132. [ 0xa0 >> 1 ] = "eeprom",
  133. [ 0xc0 >> 1 ] = "tuner (analog)",
  134. [ 0xc2 >> 1 ] = "tuner (analog/dvb)",
  135. };
  136. static void do_i2c_scan(char *name, struct i2c_client *c)
  137. {
  138. unsigned char buf;
  139. int i,rc;
  140. for (i = 0; i < 128; i++) {
  141. c->addr = i;
  142. rc = i2c_master_recv(c,&buf,0);
  143. if (rc < 0)
  144. continue;
  145. printk("%s: i2c scan: found device @ 0x%x [%s]\n",
  146. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  147. }
  148. }
  149. /* init + register i2c algo-bit adapter */
  150. int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci)
  151. {
  152. memcpy(&core->i2c_adap, &cx8800_i2c_adap_template,
  153. sizeof(core->i2c_adap));
  154. memcpy(&core->i2c_algo, &cx8800_i2c_algo_template,
  155. sizeof(core->i2c_algo));
  156. memcpy(&core->i2c_client, &cx8800_i2c_client_template,
  157. sizeof(core->i2c_client));
  158. if (core->tuner_type != TUNER_ABSENT)
  159. core->i2c_adap.class |= I2C_CLASS_TV_ANALOG;
  160. if (cx88_boards[core->board].dvb)
  161. core->i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
  162. core->i2c_adap.dev.parent = &pci->dev;
  163. strlcpy(core->i2c_adap.name,core->name,sizeof(core->i2c_adap.name));
  164. core->i2c_algo.data = core;
  165. i2c_set_adapdata(&core->i2c_adap,core);
  166. core->i2c_adap.algo_data = &core->i2c_algo;
  167. core->i2c_client.adapter = &core->i2c_adap;
  168. cx8800_bit_setscl(core,1);
  169. cx8800_bit_setsda(core,1);
  170. core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap);
  171. if (0 == core->i2c_rc) {
  172. dprintk(1, "i2c register ok\n");
  173. if (i2c_scan)
  174. do_i2c_scan(core->name,&core->i2c_client);
  175. } else
  176. printk("%s: i2c register FAILED\n", core->name);
  177. return core->i2c_rc;
  178. }
  179. /* ----------------------------------------------------------------------- */
  180. EXPORT_SYMBOL(cx88_call_i2c_clients);
  181. EXPORT_SYMBOL(cx88_i2c_init);
  182. /*
  183. * Local variables:
  184. * c-basic-offset: 8
  185. * End:
  186. */