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