cx18-gpio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * cx18 gpio functions
  3. *
  4. * Derived from ivtv-gpio.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-cards.h"
  25. #include "cx18-gpio.h"
  26. #include "tuner-xc2028.h"
  27. /********************* GPIO stuffs *********************/
  28. /* GPIO registers */
  29. #define CX18_REG_GPIO_IN 0xc72010
  30. #define CX18_REG_GPIO_OUT1 0xc78100
  31. #define CX18_REG_GPIO_DIR1 0xc78108
  32. #define CX18_REG_GPIO_OUT2 0xc78104
  33. #define CX18_REG_GPIO_DIR2 0xc7810c
  34. /*
  35. * HVR-1600 GPIO pins, courtesy of Hauppauge:
  36. *
  37. * gpio0: zilog ir process reset pin
  38. * gpio1: zilog programming pin (you should never use this)
  39. * gpio12: cx24227 reset pin
  40. * gpio13: cs5345 reset pin
  41. */
  42. static void gpio_write(struct cx18 *cx)
  43. {
  44. u32 dir = cx->gpio_dir;
  45. u32 val = cx->gpio_val;
  46. write_reg((dir & 0xffff) << 16, CX18_REG_GPIO_DIR1);
  47. write_reg(((dir & 0xffff) << 16) | (val & 0xffff),
  48. CX18_REG_GPIO_OUT1);
  49. write_reg(dir & 0xffff0000, CX18_REG_GPIO_DIR2);
  50. write_reg_sync((dir & 0xffff0000) | ((val & 0xffff0000) >> 16),
  51. CX18_REG_GPIO_OUT2);
  52. }
  53. void cx18_reset_i2c_slaves_gpio(struct cx18 *cx)
  54. {
  55. const struct cx18_gpio_i2c_slave_reset *p;
  56. p = &cx->card->gpio_i2c_slave_reset;
  57. if ((p->active_lo_mask | p->active_hi_mask) == 0)
  58. return;
  59. /* Assuming that the masks are a subset of the bits in gpio_dir */
  60. /* Assert */
  61. mutex_lock(&cx->gpio_lock);
  62. cx->gpio_val =
  63. (cx->gpio_val | p->active_hi_mask) & ~(p->active_lo_mask);
  64. gpio_write(cx);
  65. schedule_timeout_uninterruptible(msecs_to_jiffies(p->msecs_asserted));
  66. /* Deassert */
  67. cx->gpio_val =
  68. (cx->gpio_val | p->active_lo_mask) & ~(p->active_hi_mask);
  69. gpio_write(cx);
  70. schedule_timeout_uninterruptible(msecs_to_jiffies(p->msecs_recovery));
  71. mutex_unlock(&cx->gpio_lock);
  72. }
  73. void cx18_gpio_init(struct cx18 *cx)
  74. {
  75. mutex_lock(&cx->gpio_lock);
  76. cx->gpio_dir = cx->card->gpio_init.direction;
  77. cx->gpio_val = cx->card->gpio_init.initial_value;
  78. if (cx->card->tuners[0].tuner == TUNER_XC2028) {
  79. cx->gpio_dir |= 1 << cx->card->xceive_pin;
  80. cx->gpio_val |= 1 << cx->card->xceive_pin;
  81. }
  82. if (cx->gpio_dir == 0) {
  83. mutex_unlock(&cx->gpio_lock);
  84. return;
  85. }
  86. CX18_DEBUG_INFO("GPIO initial dir: %08x/%08x out: %08x/%08x\n",
  87. read_reg(CX18_REG_GPIO_DIR1), read_reg(CX18_REG_GPIO_DIR2),
  88. read_reg(CX18_REG_GPIO_OUT1), read_reg(CX18_REG_GPIO_OUT2));
  89. gpio_write(cx);
  90. mutex_unlock(&cx->gpio_lock);
  91. }
  92. /* Xceive tuner reset function */
  93. int cx18_reset_tuner_gpio(void *dev, int cmd, int value)
  94. {
  95. struct i2c_algo_bit_data *algo = dev;
  96. struct cx18_i2c_algo_callback_data *cb_data = algo->data;
  97. struct cx18 *cx = cb_data->cx;
  98. if (cmd != XC2028_TUNER_RESET)
  99. return 0;
  100. CX18_DEBUG_INFO("Resetting tuner\n");
  101. mutex_lock(&cx->gpio_lock);
  102. cx->gpio_val &= ~(1 << cx->card->xceive_pin);
  103. gpio_write(cx);
  104. mutex_unlock(&cx->gpio_lock);
  105. schedule_timeout_interruptible(msecs_to_jiffies(1));
  106. mutex_lock(&cx->gpio_lock);
  107. cx->gpio_val |= 1 << cx->card->xceive_pin;
  108. gpio_write(cx);
  109. mutex_unlock(&cx->gpio_lock);
  110. schedule_timeout_interruptible(msecs_to_jiffies(1));
  111. return 0;
  112. }
  113. int cx18_gpio(struct cx18 *cx, unsigned int command, void *arg)
  114. {
  115. struct v4l2_routing *route = arg;
  116. u32 mask, data;
  117. switch (command) {
  118. case VIDIOC_INT_S_AUDIO_ROUTING:
  119. if (route->input > 2)
  120. return -EINVAL;
  121. mask = cx->card->gpio_audio_input.mask;
  122. switch (route->input) {
  123. case 0:
  124. data = cx->card->gpio_audio_input.tuner;
  125. break;
  126. case 1:
  127. data = cx->card->gpio_audio_input.linein;
  128. break;
  129. case 2:
  130. default:
  131. data = cx->card->gpio_audio_input.radio;
  132. break;
  133. }
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. if (mask) {
  139. mutex_lock(&cx->gpio_lock);
  140. cx->gpio_val = (cx->gpio_val & ~mask) | (data & mask);
  141. gpio_write(cx);
  142. mutex_unlock(&cx->gpio_lock);
  143. }
  144. return 0;
  145. }