ivtv-gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 component, int cmd, int value)
  118. {
  119. struct i2c_algo_bit_data *algo = dev;
  120. struct ivtv *itv = algo->data;
  121. u32 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. curout &= ~(1 << itv->card->xceive_pin);
  127. write_reg(curout, IVTV_REG_GPIO_OUT);
  128. schedule_timeout_interruptible(msecs_to_jiffies(1));
  129. curout |= 1 << itv->card->xceive_pin;
  130. write_reg(curout, IVTV_REG_GPIO_OUT);
  131. schedule_timeout_interruptible(msecs_to_jiffies(1));
  132. return 0;
  133. }
  134. static inline struct ivtv *sd_to_ivtv(struct v4l2_subdev *sd)
  135. {
  136. return container_of(sd, struct ivtv, sd_gpio);
  137. }
  138. static struct v4l2_queryctrl gpio_ctrl_mute = {
  139. .id = V4L2_CID_AUDIO_MUTE,
  140. .type = V4L2_CTRL_TYPE_BOOLEAN,
  141. .name = "Mute",
  142. .minimum = 0,
  143. .maximum = 1,
  144. .step = 1,
  145. .default_value = 1,
  146. .flags = 0,
  147. };
  148. static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  149. {
  150. struct ivtv *itv = sd_to_ivtv(sd);
  151. u16 mask, data;
  152. mask = itv->card->gpio_audio_freq.mask;
  153. switch (freq) {
  154. case 32000:
  155. data = itv->card->gpio_audio_freq.f32000;
  156. break;
  157. case 44100:
  158. data = itv->card->gpio_audio_freq.f44100;
  159. break;
  160. case 48000:
  161. default:
  162. data = itv->card->gpio_audio_freq.f48000;
  163. break;
  164. }
  165. if (mask)
  166. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  167. return 0;
  168. }
  169. static int subdev_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  170. {
  171. struct ivtv *itv = sd_to_ivtv(sd);
  172. u16 mask;
  173. mask = itv->card->gpio_audio_detect.mask;
  174. if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
  175. vt->rxsubchans = V4L2_TUNER_SUB_STEREO |
  176. V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  177. else
  178. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  179. return 0;
  180. }
  181. static int subdev_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  182. {
  183. struct ivtv *itv = sd_to_ivtv(sd);
  184. u16 mask, data;
  185. mask = itv->card->gpio_audio_mode.mask;
  186. switch (vt->audmode) {
  187. case V4L2_TUNER_MODE_LANG1:
  188. data = itv->card->gpio_audio_mode.lang1;
  189. break;
  190. case V4L2_TUNER_MODE_LANG2:
  191. data = itv->card->gpio_audio_mode.lang2;
  192. break;
  193. case V4L2_TUNER_MODE_MONO:
  194. data = itv->card->gpio_audio_mode.mono;
  195. break;
  196. case V4L2_TUNER_MODE_STEREO:
  197. case V4L2_TUNER_MODE_LANG1_LANG2:
  198. default:
  199. data = itv->card->gpio_audio_mode.stereo;
  200. break;
  201. }
  202. if (mask)
  203. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  204. return 0;
  205. }
  206. static int subdev_s_radio(struct v4l2_subdev *sd)
  207. {
  208. struct ivtv *itv = sd_to_ivtv(sd);
  209. u16 mask, data;
  210. mask = itv->card->gpio_audio_input.mask;
  211. data = itv->card->gpio_audio_input.radio;
  212. if (mask)
  213. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  214. return 0;
  215. }
  216. static int subdev_s_audio_routing(struct v4l2_subdev *sd,
  217. u32 input, u32 output, u32 config)
  218. {
  219. struct ivtv *itv = sd_to_ivtv(sd);
  220. u16 mask, data;
  221. if (input > 2)
  222. return -EINVAL;
  223. mask = itv->card->gpio_audio_input.mask;
  224. switch (input) {
  225. case 0:
  226. data = itv->card->gpio_audio_input.tuner;
  227. break;
  228. case 1:
  229. data = itv->card->gpio_audio_input.linein;
  230. break;
  231. case 2:
  232. default:
  233. data = itv->card->gpio_audio_input.radio;
  234. break;
  235. }
  236. if (mask)
  237. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  238. return 0;
  239. }
  240. static int subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  241. {
  242. struct ivtv *itv = sd_to_ivtv(sd);
  243. u16 mask, data;
  244. if (ctrl->id != V4L2_CID_AUDIO_MUTE)
  245. return -EINVAL;
  246. mask = itv->card->gpio_audio_mute.mask;
  247. data = itv->card->gpio_audio_mute.mute;
  248. ctrl->value = (read_reg(IVTV_REG_GPIO_OUT) & mask) == data;
  249. return 0;
  250. }
  251. static int subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  252. {
  253. struct ivtv *itv = sd_to_ivtv(sd);
  254. u16 mask, data;
  255. if (ctrl->id != V4L2_CID_AUDIO_MUTE)
  256. return -EINVAL;
  257. mask = itv->card->gpio_audio_mute.mask;
  258. data = ctrl->value ? itv->card->gpio_audio_mute.mute : 0;
  259. if (mask)
  260. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  261. return 0;
  262. }
  263. static int subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  264. {
  265. if (qc->id != V4L2_CID_AUDIO_MUTE)
  266. return -EINVAL;
  267. *qc = gpio_ctrl_mute;
  268. return 0;
  269. }
  270. static int subdev_log_status(struct v4l2_subdev *sd)
  271. {
  272. struct ivtv *itv = sd_to_ivtv(sd);
  273. IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
  274. read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
  275. read_reg(IVTV_REG_GPIO_IN));
  276. return 0;
  277. }
  278. static int subdev_s_video_routing(struct v4l2_subdev *sd,
  279. u32 input, u32 output, u32 config)
  280. {
  281. struct ivtv *itv = sd_to_ivtv(sd);
  282. u16 mask, data;
  283. if (input > 2) /* 0:Tuner 1:Composite 2:S-Video */
  284. return -EINVAL;
  285. mask = itv->card->gpio_video_input.mask;
  286. if (input == 0)
  287. data = itv->card->gpio_video_input.tuner;
  288. else if (input == 1)
  289. data = itv->card->gpio_video_input.composite;
  290. else
  291. data = itv->card->gpio_video_input.svideo;
  292. if (mask)
  293. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  294. return 0;
  295. }
  296. static const struct v4l2_subdev_core_ops subdev_core_ops = {
  297. .log_status = subdev_log_status,
  298. .g_ctrl = subdev_g_ctrl,
  299. .s_ctrl = subdev_s_ctrl,
  300. .queryctrl = subdev_queryctrl,
  301. };
  302. static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = {
  303. .s_radio = subdev_s_radio,
  304. .g_tuner = subdev_g_tuner,
  305. .s_tuner = subdev_s_tuner,
  306. };
  307. static const struct v4l2_subdev_audio_ops subdev_audio_ops = {
  308. .s_clock_freq = subdev_s_clock_freq,
  309. .s_routing = subdev_s_audio_routing,
  310. };
  311. static const struct v4l2_subdev_video_ops subdev_video_ops = {
  312. .s_routing = subdev_s_video_routing,
  313. };
  314. static const struct v4l2_subdev_ops subdev_ops = {
  315. .core = &subdev_core_ops,
  316. .tuner = &subdev_tuner_ops,
  317. .audio = &subdev_audio_ops,
  318. .video = &subdev_video_ops,
  319. };
  320. int ivtv_gpio_init(struct ivtv *itv)
  321. {
  322. u16 pin = 0;
  323. if (itv->card->xceive_pin)
  324. pin = 1 << itv->card->xceive_pin;
  325. if ((itv->card->gpio_init.direction | pin) == 0)
  326. return 0;
  327. IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
  328. read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
  329. /* init output data then direction */
  330. write_reg(itv->card->gpio_init.initial_value | pin, IVTV_REG_GPIO_OUT);
  331. write_reg(itv->card->gpio_init.direction | pin, IVTV_REG_GPIO_DIR);
  332. v4l2_subdev_init(&itv->sd_gpio, &subdev_ops);
  333. snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name);
  334. itv->sd_gpio.grp_id = IVTV_HW_GPIO;
  335. return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio);
  336. }