ivtv-gpio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. gpio functions.
  3. Merging GPIO support into driver:
  4. Copyright (C) 2004 Chris Kennedy <c@groovy.org>
  5. Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "ivtv-driver.h"
  19. #include "ivtv-cards.h"
  20. #include "ivtv-gpio.h"
  21. #include <media/tuner.h>
  22. /*
  23. * GPIO assignment of Yuan MPG600/MPG160
  24. *
  25. * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
  26. * OUTPUT IN1 IN0 AM3 AM2 AM1 AM0
  27. * INPUT DM1 DM0
  28. *
  29. * IN* : Input selection
  30. * IN1 IN0
  31. * 1 1 N/A
  32. * 1 0 Line
  33. * 0 1 N/A
  34. * 0 0 Tuner
  35. *
  36. * AM* : Audio Mode
  37. * AM3 0: Normal 1: Mixed(Sub+Main channel)
  38. * AM2 0: Subchannel 1: Main channel
  39. * AM1 0: Stereo 1: Mono
  40. * AM0 0: Normal 1: Mute
  41. *
  42. * DM* : Detected tuner audio Mode
  43. * DM1 0: Stereo 1: Mono
  44. * DM0 0: Multiplex 1: Normal
  45. *
  46. * GPIO Initial Settings
  47. * MPG600 MPG160
  48. * DIR 0x3080 0x7080
  49. * OUTPUT 0x000C 0x400C
  50. *
  51. * Special thanks to Makoto Iguchi <iguchi@tahoo.org> and Mr. Anonymous
  52. * for analyzing GPIO of MPG160.
  53. *
  54. *****************************************************************************
  55. *
  56. * GPIO assignment of Avermedia M179 (per information direct from AVerMedia)
  57. *
  58. * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
  59. * OUTPUT IN0 AM0 IN1 AM1 AM2 IN2 BR0 BR1
  60. * INPUT
  61. *
  62. * IN* : Input selection
  63. * IN0 IN1 IN2
  64. * * 1 * Mute
  65. * 0 0 0 Line-In
  66. * 1 0 0 TV Tuner Audio
  67. * 0 0 1 FM Audio
  68. * 1 0 1 Mute
  69. *
  70. * AM* : Audio Mode
  71. * AM0 AM1 AM2
  72. * 0 0 0 TV Tuner Audio: L_OUT=(L+R)/2, R_OUT=SAP
  73. * 0 0 1 TV Tuner Audio: L_OUT=R_OUT=SAP (SAP)
  74. * 0 1 0 TV Tuner Audio: L_OUT=L, R_OUT=R (stereo)
  75. * 0 1 1 TV Tuner Audio: mute
  76. * 1 * * TV Tuner Audio: L_OUT=R_OUT=(L+R)/2 (mono)
  77. *
  78. * BR* : Audio Sample Rate (BR stands for bitrate for some reason)
  79. * BR0 BR1
  80. * 0 0 32 kHz
  81. * 0 1 44.1 kHz
  82. * 1 0 48 kHz
  83. *
  84. * DM* : Detected tuner audio Mode
  85. * Unknown currently
  86. *
  87. * Special thanks to AVerMedia Technologies, Inc. and Jiun-Kuei Jung at
  88. * AVerMedia for providing the GPIO information used to add support
  89. * for the M179 cards.
  90. */
  91. /********************* GPIO stuffs *********************/
  92. /* GPIO registers */
  93. #define IVTV_REG_GPIO_IN 0x9008
  94. #define IVTV_REG_GPIO_OUT 0x900c
  95. #define IVTV_REG_GPIO_DIR 0x9020
  96. void ivtv_reset_ir_gpio(struct ivtv *itv)
  97. {
  98. int curdir, curout;
  99. if (itv->card->type != IVTV_CARD_PVR_150)
  100. return;
  101. IVTV_DEBUG_INFO("Resetting PVR150 IR\n");
  102. curout = read_reg(IVTV_REG_GPIO_OUT);
  103. curdir = read_reg(IVTV_REG_GPIO_DIR);
  104. curdir |= 0x80;
  105. write_reg(curdir, IVTV_REG_GPIO_DIR);
  106. curout = (curout & ~0xF) | 1;
  107. write_reg(curout, IVTV_REG_GPIO_OUT);
  108. /* We could use something else for smaller time */
  109. schedule_timeout_interruptible(msecs_to_jiffies(1));
  110. curout |= 2;
  111. write_reg(curout, IVTV_REG_GPIO_OUT);
  112. curdir &= ~0x80;
  113. write_reg(curdir, IVTV_REG_GPIO_DIR);
  114. }
  115. #ifdef HAVE_XC3028
  116. int ivtv_reset_tuner_gpio(enum v4l2_tuner_type mode, void *priv, int ptr)
  117. {
  118. int curdir, curout;
  119. struct ivtv *itv = (struct ivtv *) priv;
  120. if (itv->card->type != IVTV_CARD_PG600V2 || itv->options.tuner != TUNER_XCEIVE_XC3028)
  121. return -EINVAL;
  122. IVTV_INFO("Resetting tuner\n");
  123. curout = read_reg(IVTV_REG_GPIO_OUT);
  124. curdir = read_reg(IVTV_REG_GPIO_DIR);
  125. curdir |= (1 << 12); /* GPIO bit 12 */
  126. curout &= ~(1 << 12);
  127. write_reg(curout, IVTV_REG_GPIO_OUT);
  128. schedule_timeout_interruptible(msecs_to_jiffies(1));
  129. curout |= (1 << 12);
  130. write_reg(curout, IVTV_REG_GPIO_OUT);
  131. schedule_timeout_interruptible(msecs_to_jiffies(1));
  132. return 0;
  133. }
  134. #endif
  135. void ivtv_gpio_init(struct ivtv *itv)
  136. {
  137. if (itv->card->gpio_init.direction == 0)
  138. return;
  139. IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
  140. read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
  141. /* init output data then direction */
  142. write_reg(itv->card->gpio_init.initial_value, IVTV_REG_GPIO_OUT);
  143. write_reg(itv->card->gpio_init.direction, IVTV_REG_GPIO_DIR);
  144. }
  145. static struct v4l2_queryctrl gpio_ctrl_mute = {
  146. .id = V4L2_CID_AUDIO_MUTE,
  147. .type = V4L2_CTRL_TYPE_BOOLEAN,
  148. .name = "Mute",
  149. .minimum = 0,
  150. .maximum = 1,
  151. .step = 1,
  152. .default_value = 1,
  153. .flags = 0,
  154. };
  155. int ivtv_gpio(struct ivtv *itv, unsigned int command, void *arg)
  156. {
  157. struct v4l2_tuner *tuner = arg;
  158. struct v4l2_control *ctrl = arg;
  159. struct v4l2_routing *route = arg;
  160. u16 mask, data;
  161. switch (command) {
  162. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  163. mask = itv->card->gpio_audio_freq.mask;
  164. switch (*(u32 *)arg) {
  165. case 32000:
  166. data = itv->card->gpio_audio_freq.f32000;
  167. break;
  168. case 44100:
  169. data = itv->card->gpio_audio_freq.f44100;
  170. break;
  171. case 48000:
  172. default:
  173. data = itv->card->gpio_audio_freq.f48000;
  174. break;
  175. }
  176. break;
  177. case VIDIOC_G_TUNER:
  178. mask = itv->card->gpio_audio_detect.mask;
  179. if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
  180. tuner->rxsubchans = V4L2_TUNER_MODE_STEREO |
  181. V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
  182. else
  183. tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
  184. return 0;
  185. case VIDIOC_S_TUNER:
  186. mask = itv->card->gpio_audio_mode.mask;
  187. switch (tuner->audmode) {
  188. case V4L2_TUNER_MODE_LANG1:
  189. data = itv->card->gpio_audio_mode.lang1;
  190. break;
  191. case V4L2_TUNER_MODE_LANG2:
  192. data = itv->card->gpio_audio_mode.lang2;
  193. break;
  194. case V4L2_TUNER_MODE_MONO:
  195. data = itv->card->gpio_audio_mode.mono;
  196. break;
  197. case V4L2_TUNER_MODE_STEREO:
  198. case V4L2_TUNER_MODE_LANG1_LANG2:
  199. default:
  200. data = itv->card->gpio_audio_mode.stereo;
  201. break;
  202. }
  203. break;
  204. case AUDC_SET_RADIO:
  205. mask = itv->card->gpio_audio_input.mask;
  206. data = itv->card->gpio_audio_input.radio;
  207. break;
  208. case VIDIOC_S_STD:
  209. mask = itv->card->gpio_audio_input.mask;
  210. data = itv->card->gpio_audio_input.tuner;
  211. break;
  212. case VIDIOC_INT_S_AUDIO_ROUTING:
  213. if (route->input > 2)
  214. return -EINVAL;
  215. mask = itv->card->gpio_audio_input.mask;
  216. switch (route->input) {
  217. case 0:
  218. data = itv->card->gpio_audio_input.tuner;
  219. break;
  220. case 1:
  221. data = itv->card->gpio_audio_input.linein;
  222. break;
  223. case 2:
  224. default:
  225. data = itv->card->gpio_audio_input.radio;
  226. break;
  227. }
  228. break;
  229. case VIDIOC_G_CTRL:
  230. if (ctrl->id != V4L2_CID_AUDIO_MUTE)
  231. return -EINVAL;
  232. mask = itv->card->gpio_audio_mute.mask;
  233. data = itv->card->gpio_audio_mute.mute;
  234. ctrl->value = (read_reg(IVTV_REG_GPIO_OUT) & mask) == data;
  235. return 0;
  236. case VIDIOC_S_CTRL:
  237. if (ctrl->id != V4L2_CID_AUDIO_MUTE)
  238. return -EINVAL;
  239. mask = itv->card->gpio_audio_mute.mask;
  240. data = ctrl->value ? itv->card->gpio_audio_mute.mute : 0;
  241. break;
  242. case VIDIOC_QUERYCTRL:
  243. {
  244. struct v4l2_queryctrl *qc = arg;
  245. if (qc->id != V4L2_CID_AUDIO_MUTE)
  246. return -EINVAL;
  247. *qc = gpio_ctrl_mute;
  248. return 0;
  249. }
  250. case VIDIOC_LOG_STATUS:
  251. IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
  252. read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
  253. read_reg(IVTV_REG_GPIO_IN));
  254. return 0;
  255. case VIDIOC_INT_S_VIDEO_ROUTING:
  256. if (route->input > 2) /* 0:Tuner 1:Composite 2:S-Video */
  257. return -EINVAL;
  258. mask = itv->card->gpio_video_input.mask;
  259. if (route->input == 0)
  260. data = itv->card->gpio_video_input.tuner;
  261. else if (route->input == 1)
  262. data = itv->card->gpio_video_input.composite;
  263. else
  264. data = itv->card->gpio_video_input.svideo;
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. if (mask)
  270. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  271. return 0;
  272. }