cx88-vp3054-i2c.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. *
  6. * (c) 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  7. *
  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. *
  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. */
  18. #include "cx88.h"
  19. #include "cx88-vp3054-i2c.h"
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. MODULE_DESCRIPTION("driver for cx2388x VP3054 design");
  25. MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
  26. MODULE_LICENSE("GPL");
  27. /* ----------------------------------------------------------------------- */
  28. static void vp3054_bit_setscl(void *data, int state)
  29. {
  30. struct cx8802_dev *dev = data;
  31. struct cx88_core *core = dev->core;
  32. struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
  33. if (state) {
  34. vp3054_i2c->state |= 0x0001; /* SCL high */
  35. vp3054_i2c->state &= ~0x0100; /* external pullup */
  36. } else {
  37. vp3054_i2c->state &= ~0x0001; /* SCL low */
  38. vp3054_i2c->state |= 0x0100; /* drive pin */
  39. }
  40. cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state);
  41. cx_read(MO_GP0_IO);
  42. }
  43. static void vp3054_bit_setsda(void *data, int state)
  44. {
  45. struct cx8802_dev *dev = data;
  46. struct cx88_core *core = dev->core;
  47. struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
  48. if (state) {
  49. vp3054_i2c->state |= 0x0002; /* SDA high */
  50. vp3054_i2c->state &= ~0x0200; /* tristate pin */
  51. } else {
  52. vp3054_i2c->state &= ~0x0002; /* SDA low */
  53. vp3054_i2c->state |= 0x0200; /* drive pin */
  54. }
  55. cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state);
  56. cx_read(MO_GP0_IO);
  57. }
  58. static int vp3054_bit_getscl(void *data)
  59. {
  60. struct cx8802_dev *dev = data;
  61. struct cx88_core *core = dev->core;
  62. u32 state;
  63. state = cx_read(MO_GP0_IO);
  64. return (state & 0x01) ? 1 : 0;
  65. }
  66. static int vp3054_bit_getsda(void *data)
  67. {
  68. struct cx8802_dev *dev = data;
  69. struct cx88_core *core = dev->core;
  70. u32 state;
  71. state = cx_read(MO_GP0_IO);
  72. return (state & 0x02) ? 1 : 0;
  73. }
  74. /* ----------------------------------------------------------------------- */
  75. static const struct i2c_algo_bit_data vp3054_i2c_algo_template = {
  76. .setsda = vp3054_bit_setsda,
  77. .setscl = vp3054_bit_setscl,
  78. .getsda = vp3054_bit_getsda,
  79. .getscl = vp3054_bit_getscl,
  80. .udelay = 16,
  81. .timeout = 200,
  82. };
  83. /* ----------------------------------------------------------------------- */
  84. int vp3054_i2c_probe(struct cx8802_dev *dev)
  85. {
  86. struct cx88_core *core = dev->core;
  87. struct vp3054_i2c_state *vp3054_i2c;
  88. int rc;
  89. if (core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
  90. return 0;
  91. vp3054_i2c = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
  92. if (!vp3054_i2c)
  93. return -ENOMEM;
  94. dev->vp3054 = vp3054_i2c;
  95. vp3054_i2c->algo = vp3054_i2c_algo_template;
  96. vp3054_i2c->adap.dev.parent = &dev->pci->dev;
  97. strlcpy(vp3054_i2c->adap.name, core->name,
  98. sizeof(vp3054_i2c->adap.name));
  99. vp3054_i2c->adap.owner = THIS_MODULE;
  100. vp3054_i2c->algo.data = dev;
  101. i2c_set_adapdata(&vp3054_i2c->adap, dev);
  102. vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
  103. vp3054_bit_setscl(dev, 1);
  104. vp3054_bit_setsda(dev, 1);
  105. rc = i2c_bit_add_bus(&vp3054_i2c->adap);
  106. if (rc != 0) {
  107. pr_err("vp3054_i2c register FAILED\n");
  108. kfree(dev->vp3054);
  109. dev->vp3054 = NULL;
  110. }
  111. return rc;
  112. }
  113. EXPORT_SYMBOL(vp3054_i2c_probe);
  114. void vp3054_i2c_remove(struct cx8802_dev *dev)
  115. {
  116. struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
  117. if (!vp3054_i2c ||
  118. dev->core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
  119. return;
  120. i2c_del_adapter(&vp3054_i2c->adap);
  121. kfree(vp3054_i2c);
  122. }
  123. EXPORT_SYMBOL(vp3054_i2c_remove);