cx88-vp3054-i2c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cx88-vp3054-i2c.c -- support for the secondary I2C bus of the
  4. * DNTV Live! DVB-T Pro (VP-3054), wired as:
  5. * GPIO[0] -> SCL, GPIO[1] -> SDA
  6. *
  7. * (c) 2005 Chris Pascoe <[email protected]>
  8. */
  9. #include "cx88.h"
  10. #include "cx88-vp3054-i2c.h"
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. MODULE_DESCRIPTION("driver for cx2388x VP3054 design");
  16. MODULE_AUTHOR("Chris Pascoe <[email protected]>");
  17. MODULE_LICENSE("GPL");
  18. /* ----------------------------------------------------------------------- */
  19. static void vp3054_bit_setscl(void *data, int state)
  20. {
  21. struct cx8802_dev *dev = data;
  22. struct cx88_core *core = dev->core;
  23. struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
  24. if (state) {
  25. vp3054_i2c->state |= 0x0001; /* SCL high */
  26. vp3054_i2c->state &= ~0x0100; /* external pullup */
  27. } else {
  28. vp3054_i2c->state &= ~0x0001; /* SCL low */
  29. vp3054_i2c->state |= 0x0100; /* drive pin */
  30. }
  31. cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state);
  32. cx_read(MO_GP0_IO);
  33. }
  34. static void vp3054_bit_setsda(void *data, int state)
  35. {
  36. struct cx8802_dev *dev = data;
  37. struct cx88_core *core = dev->core;
  38. struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
  39. if (state) {
  40. vp3054_i2c->state |= 0x0002; /* SDA high */
  41. vp3054_i2c->state &= ~0x0200; /* tristate pin */
  42. } else {
  43. vp3054_i2c->state &= ~0x0002; /* SDA low */
  44. vp3054_i2c->state |= 0x0200; /* drive pin */
  45. }
  46. cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state);
  47. cx_read(MO_GP0_IO);
  48. }
  49. static int vp3054_bit_getscl(void *data)
  50. {
  51. struct cx8802_dev *dev = data;
  52. struct cx88_core *core = dev->core;
  53. u32 state;
  54. state = cx_read(MO_GP0_IO);
  55. return (state & 0x01) ? 1 : 0;
  56. }
  57. static int vp3054_bit_getsda(void *data)
  58. {
  59. struct cx8802_dev *dev = data;
  60. struct cx88_core *core = dev->core;
  61. u32 state;
  62. state = cx_read(MO_GP0_IO);
  63. return (state & 0x02) ? 1 : 0;
  64. }
  65. /* ----------------------------------------------------------------------- */
  66. static const struct i2c_algo_bit_data vp3054_i2c_algo_template = {
  67. .setsda = vp3054_bit_setsda,
  68. .setscl = vp3054_bit_setscl,
  69. .getsda = vp3054_bit_getsda,
  70. .getscl = vp3054_bit_getscl,
  71. .udelay = 16,
  72. .timeout = 200,
  73. };
  74. /* ----------------------------------------------------------------------- */
  75. int vp3054_i2c_probe(struct cx8802_dev *dev)
  76. {
  77. struct cx88_core *core = dev->core;
  78. struct vp3054_i2c_state *vp3054_i2c;
  79. int rc;
  80. if (core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
  81. return 0;
  82. vp3054_i2c = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
  83. if (!vp3054_i2c)
  84. return -ENOMEM;
  85. dev->vp3054 = vp3054_i2c;
  86. vp3054_i2c->algo = vp3054_i2c_algo_template;
  87. vp3054_i2c->adap.dev.parent = &dev->pci->dev;
  88. strscpy(vp3054_i2c->adap.name, core->name,
  89. sizeof(vp3054_i2c->adap.name));
  90. vp3054_i2c->adap.owner = THIS_MODULE;
  91. vp3054_i2c->algo.data = dev;
  92. i2c_set_adapdata(&vp3054_i2c->adap, dev);
  93. vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
  94. vp3054_bit_setscl(dev, 1);
  95. vp3054_bit_setsda(dev, 1);
  96. rc = i2c_bit_add_bus(&vp3054_i2c->adap);
  97. if (rc != 0) {
  98. pr_err("vp3054_i2c register FAILED\n");
  99. kfree(dev->vp3054);
  100. dev->vp3054 = NULL;
  101. }
  102. return rc;
  103. }
  104. EXPORT_SYMBOL(vp3054_i2c_probe);
  105. void vp3054_i2c_remove(struct cx8802_dev *dev)
  106. {
  107. struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
  108. if (!vp3054_i2c ||
  109. dev->core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
  110. return;
  111. i2c_del_adapter(&vp3054_i2c->adap);
  112. kfree(vp3054_i2c);
  113. }
  114. EXPORT_SYMBOL(vp3054_i2c_remove);