cx88-i2c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <asm/io.h>
  25. #include "cx88.h"
  26. #include <media/v4l2-common.h>
  27. static unsigned int i2c_debug = 0;
  28. module_param(i2c_debug, int, 0644);
  29. MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
  30. static unsigned int i2c_scan = 0;
  31. module_param(i2c_scan, int, 0444);
  32. MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
  33. static unsigned int i2c_udelay = 5;
  34. module_param(i2c_udelay, int, 0644);
  35. MODULE_PARM_DESC(i2c_udelay,"i2c delay at insmod time, in usecs "
  36. "(should be 5 or higher). Lower value means higher bus speed.");
  37. #define dprintk(level,fmt, arg...) if (i2c_debug >= level) \
  38. printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)
  39. /* ----------------------------------------------------------------------- */
  40. static void cx8800_bit_setscl(void *data, int state)
  41. {
  42. struct cx88_core *core = data;
  43. if (state)
  44. core->i2c_state |= 0x02;
  45. else
  46. core->i2c_state &= ~0x02;
  47. cx_write(MO_I2C, core->i2c_state);
  48. cx_read(MO_I2C);
  49. }
  50. static void cx8800_bit_setsda(void *data, int state)
  51. {
  52. struct cx88_core *core = data;
  53. if (state)
  54. core->i2c_state |= 0x01;
  55. else
  56. core->i2c_state &= ~0x01;
  57. cx_write(MO_I2C, core->i2c_state);
  58. cx_read(MO_I2C);
  59. }
  60. static int cx8800_bit_getscl(void *data)
  61. {
  62. struct cx88_core *core = data;
  63. u32 state;
  64. state = cx_read(MO_I2C);
  65. return state & 0x02 ? 1 : 0;
  66. }
  67. static int cx8800_bit_getsda(void *data)
  68. {
  69. struct cx88_core *core = data;
  70. u32 state;
  71. state = cx_read(MO_I2C);
  72. return state & 0x01;
  73. }
  74. /* ----------------------------------------------------------------------- */
  75. static int attach_inform(struct i2c_client *client)
  76. {
  77. struct tuner_setup tun_setup;
  78. struct cx88_core *core = i2c_get_adapdata(client->adapter);
  79. dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
  80. client->driver->driver.name, client->addr, client->name);
  81. if (!client->driver->command)
  82. return 0;
  83. if (core->radio_type != UNSET) {
  84. if ((core->radio_addr==ADDR_UNSET)||(core->radio_addr==client->addr)) {
  85. tun_setup.mode_mask = T_RADIO;
  86. tun_setup.type = core->radio_type;
  87. tun_setup.addr = core->radio_addr;
  88. client->driver->command (client, TUNER_SET_TYPE_ADDR, &tun_setup);
  89. }
  90. }
  91. if (core->tuner_type != UNSET) {
  92. if ((core->tuner_addr==ADDR_UNSET)||(core->tuner_addr==client->addr)) {
  93. tun_setup.mode_mask = T_ANALOG_TV;
  94. tun_setup.type = core->tuner_type;
  95. tun_setup.addr = core->tuner_addr;
  96. client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup);
  97. }
  98. }
  99. if (core->tda9887_conf)
  100. client->driver->command(client, TDA9887_SET_CONFIG, &core->tda9887_conf);
  101. return 0;
  102. }
  103. static int detach_inform(struct i2c_client *client)
  104. {
  105. struct cx88_core *core = i2c_get_adapdata(client->adapter);
  106. dprintk(1, "i2c detach [client=%s]\n", client->name);
  107. return 0;
  108. }
  109. void cx88_call_i2c_clients(struct cx88_core *core, unsigned int cmd, void *arg)
  110. {
  111. if (0 != core->i2c_rc)
  112. return;
  113. if ( (core->dvbdev) && (core->dvbdev->dvb.frontend) ) {
  114. if (core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl)
  115. core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl(core->dvbdev->dvb.frontend, 1);
  116. i2c_clients_command(&core->i2c_adap, cmd, arg);
  117. if (core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl)
  118. core->dvbdev->dvb.frontend->ops.i2c_gate_ctrl(core->dvbdev->dvb.frontend, 0);
  119. } else
  120. i2c_clients_command(&core->i2c_adap, cmd, arg);
  121. }
  122. static struct i2c_algo_bit_data cx8800_i2c_algo_template = {
  123. .setsda = cx8800_bit_setsda,
  124. .setscl = cx8800_bit_setscl,
  125. .getsda = cx8800_bit_getsda,
  126. .getscl = cx8800_bit_getscl,
  127. .udelay = 16,
  128. .timeout = 200,
  129. };
  130. /* ----------------------------------------------------------------------- */
  131. static struct i2c_adapter cx8800_i2c_adap_template = {
  132. .name = "cx2388x",
  133. .owner = THIS_MODULE,
  134. .id = I2C_HW_B_CX2388x,
  135. .client_register = attach_inform,
  136. .client_unregister = detach_inform,
  137. };
  138. static struct i2c_client cx8800_i2c_client_template = {
  139. .name = "cx88xx internal",
  140. };
  141. static char *i2c_devs[128] = {
  142. [ 0x1c >> 1 ] = "lgdt330x",
  143. [ 0x86 >> 1 ] = "tda9887/cx22702",
  144. [ 0xa0 >> 1 ] = "eeprom",
  145. [ 0xc0 >> 1 ] = "tuner (analog)",
  146. [ 0xc2 >> 1 ] = "tuner (analog/dvb)",
  147. };
  148. static void do_i2c_scan(char *name, struct i2c_client *c)
  149. {
  150. unsigned char buf;
  151. int i,rc;
  152. for (i = 0; i < 128; i++) {
  153. c->addr = i;
  154. rc = i2c_master_recv(c,&buf,0);
  155. if (rc < 0)
  156. continue;
  157. printk("%s: i2c scan: found device @ 0x%x [%s]\n",
  158. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  159. }
  160. }
  161. /* init + register i2c algo-bit adapter */
  162. int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci)
  163. {
  164. /* Prevents usage of invalid delay values */
  165. if (i2c_udelay<5)
  166. i2c_udelay=5;
  167. cx8800_i2c_algo_template.udelay=i2c_udelay;
  168. memcpy(&core->i2c_adap, &cx8800_i2c_adap_template,
  169. sizeof(core->i2c_adap));
  170. memcpy(&core->i2c_algo, &cx8800_i2c_algo_template,
  171. sizeof(core->i2c_algo));
  172. memcpy(&core->i2c_client, &cx8800_i2c_client_template,
  173. sizeof(core->i2c_client));
  174. if (core->tuner_type != TUNER_ABSENT)
  175. core->i2c_adap.class |= I2C_CLASS_TV_ANALOG;
  176. if (cx88_boards[core->board].mpeg & CX88_MPEG_DVB)
  177. core->i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
  178. core->i2c_adap.dev.parent = &pci->dev;
  179. strlcpy(core->i2c_adap.name,core->name,sizeof(core->i2c_adap.name));
  180. core->i2c_algo.data = core;
  181. i2c_set_adapdata(&core->i2c_adap,core);
  182. core->i2c_adap.algo_data = &core->i2c_algo;
  183. core->i2c_client.adapter = &core->i2c_adap;
  184. cx8800_bit_setscl(core,1);
  185. cx8800_bit_setsda(core,1);
  186. core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap);
  187. if (0 == core->i2c_rc) {
  188. dprintk(1, "i2c register ok\n");
  189. if (i2c_scan)
  190. do_i2c_scan(core->name,&core->i2c_client);
  191. } else
  192. printk("%s: i2c register FAILED\n", core->name);
  193. return core->i2c_rc;
  194. }
  195. /* ----------------------------------------------------------------------- */
  196. EXPORT_SYMBOL(cx88_call_i2c_clients);
  197. /*
  198. * Local variables:
  199. * c-basic-offset: 8
  200. * End:
  201. */