cx88-vp3054-i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. cx88-vp3054-i2c.c -- support for the secondary I2C bus of the
  3. DNTV Live! DVB-T Pro (VP-3054), wired as:
  4. GPIO[0] -> SCL, GPIO[1] -> SDA
  5. (c) 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/init.h>
  21. #include <asm/io.h>
  22. #include "cx88.h"
  23. #include "cx88-vp3054-i2c.h"
  24. /* ----------------------------------------------------------------------- */
  25. static void vp3054_bit_setscl(void *data, int state)
  26. {
  27. struct cx8802_dev *dev = data;
  28. struct cx88_core *core = dev->core;
  29. struct vp3054_i2c_state *vp3054_i2c = dev->card_priv;
  30. if (state) {
  31. vp3054_i2c->state |= 0x0001; /* SCL high */
  32. vp3054_i2c->state &= ~0x0100; /* external pullup */
  33. } else {
  34. vp3054_i2c->state &= ~0x0001; /* SCL low */
  35. vp3054_i2c->state |= 0x0100; /* drive pin */
  36. }
  37. cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state);
  38. cx_read(MO_GP0_IO);
  39. }
  40. static void vp3054_bit_setsda(void *data, int state)
  41. {
  42. struct cx8802_dev *dev = data;
  43. struct cx88_core *core = dev->core;
  44. struct vp3054_i2c_state *vp3054_i2c = dev->card_priv;
  45. if (state) {
  46. vp3054_i2c->state |= 0x0002; /* SDA high */
  47. vp3054_i2c->state &= ~0x0200; /* tristate pin */
  48. } else {
  49. vp3054_i2c->state &= ~0x0002; /* SDA low */
  50. vp3054_i2c->state |= 0x0200; /* drive pin */
  51. }
  52. cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state);
  53. cx_read(MO_GP0_IO);
  54. }
  55. static int vp3054_bit_getscl(void *data)
  56. {
  57. struct cx8802_dev *dev = data;
  58. struct cx88_core *core = dev->core;
  59. u32 state;
  60. state = cx_read(MO_GP0_IO);
  61. return (state & 0x01) ? 1 : 0;
  62. }
  63. static int vp3054_bit_getsda(void *data)
  64. {
  65. struct cx8802_dev *dev = data;
  66. struct cx88_core *core = dev->core;
  67. u32 state;
  68. state = cx_read(MO_GP0_IO);
  69. return (state & 0x02) ? 1 : 0;
  70. }
  71. /* ----------------------------------------------------------------------- */
  72. static struct i2c_algo_bit_data vp3054_i2c_algo_template = {
  73. .setsda = vp3054_bit_setsda,
  74. .setscl = vp3054_bit_setscl,
  75. .getsda = vp3054_bit_getsda,
  76. .getscl = vp3054_bit_getscl,
  77. .udelay = 16,
  78. .mdelay = 10,
  79. .timeout = 200,
  80. };
  81. /* ----------------------------------------------------------------------- */
  82. static struct i2c_adapter vp3054_i2c_adap_template = {
  83. .name = "cx2388x",
  84. .owner = THIS_MODULE,
  85. .id = I2C_HW_B_CX2388x,
  86. };
  87. static struct i2c_client vp3054_i2c_client_template = {
  88. .name = "VP-3054",
  89. };
  90. int vp3054_i2c_probe(struct cx8802_dev *dev)
  91. {
  92. struct cx88_core *core = dev->core;
  93. struct vp3054_i2c_state *vp3054_i2c;
  94. int rc;
  95. if (core->board != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
  96. return 0;
  97. dev->card_priv = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
  98. if (dev->card_priv == NULL)
  99. return -ENOMEM;
  100. vp3054_i2c = dev->card_priv;
  101. memcpy(&vp3054_i2c->adap, &vp3054_i2c_adap_template,
  102. sizeof(vp3054_i2c->adap));
  103. memcpy(&vp3054_i2c->algo, &vp3054_i2c_algo_template,
  104. sizeof(vp3054_i2c->algo));
  105. memcpy(&vp3054_i2c->client, &vp3054_i2c_client_template,
  106. sizeof(vp3054_i2c->client));
  107. vp3054_i2c->adap.class |= I2C_CLASS_TV_DIGITAL;
  108. vp3054_i2c->adap.dev.parent = &dev->pci->dev;
  109. strlcpy(vp3054_i2c->adap.name, core->name,
  110. sizeof(vp3054_i2c->adap.name));
  111. vp3054_i2c->algo.data = dev;
  112. i2c_set_adapdata(&vp3054_i2c->adap, dev);
  113. vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
  114. vp3054_i2c->client.adapter = &vp3054_i2c->adap;
  115. vp3054_bit_setscl(dev,1);
  116. vp3054_bit_setsda(dev,1);
  117. rc = i2c_bit_add_bus(&vp3054_i2c->adap);
  118. if (0 != rc) {
  119. printk("%s: vp3054_i2c register FAILED\n", core->name);
  120. kfree(dev->card_priv);
  121. dev->card_priv = NULL;
  122. }
  123. return rc;
  124. }
  125. void vp3054_i2c_remove(struct cx8802_dev *dev)
  126. {
  127. struct vp3054_i2c_state *vp3054_i2c = dev->card_priv;
  128. if (vp3054_i2c == NULL ||
  129. dev->core->board != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
  130. return;
  131. i2c_bit_del_bus(&vp3054_i2c->adap);
  132. kfree(vp3054_i2c);
  133. }
  134. EXPORT_SYMBOL(vp3054_i2c_probe);
  135. EXPORT_SYMBOL(vp3054_i2c_remove);