ivtv-gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include <media/v4l2-ctrls.h>
  24. /*
  25. * GPIO assignment of Yuan MPG600/MPG160
  26. *
  27. * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
  28. * OUTPUT IN1 IN0 AM3 AM2 AM1 AM0
  29. * INPUT DM1 DM0
  30. *
  31. * IN* : Input selection
  32. * IN1 IN0
  33. * 1 1 N/A
  34. * 1 0 Line
  35. * 0 1 N/A
  36. * 0 0 Tuner
  37. *
  38. * AM* : Audio Mode
  39. * AM3 0: Normal 1: Mixed(Sub+Main channel)
  40. * AM2 0: Subchannel 1: Main channel
  41. * AM1 0: Stereo 1: Mono
  42. * AM0 0: Normal 1: Mute
  43. *
  44. * DM* : Detected tuner audio Mode
  45. * DM1 0: Stereo 1: Mono
  46. * DM0 0: Multiplex 1: Normal
  47. *
  48. * GPIO Initial Settings
  49. * MPG600 MPG160
  50. * DIR 0x3080 0x7080
  51. * OUTPUT 0x000C 0x400C
  52. *
  53. * Special thanks to Makoto Iguchi <iguchi@tahoo.org> and Mr. Anonymous
  54. * for analyzing GPIO of MPG160.
  55. *
  56. *****************************************************************************
  57. *
  58. * GPIO assignment of Avermedia M179 (per information direct from AVerMedia)
  59. *
  60. * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
  61. * OUTPUT IN0 AM0 IN1 AM1 AM2 IN2 BR0 BR1
  62. * INPUT
  63. *
  64. * IN* : Input selection
  65. * IN0 IN1 IN2
  66. * * 1 * Mute
  67. * 0 0 0 Line-In
  68. * 1 0 0 TV Tuner Audio
  69. * 0 0 1 FM Audio
  70. * 1 0 1 Mute
  71. *
  72. * AM* : Audio Mode
  73. * AM0 AM1 AM2
  74. * 0 0 0 TV Tuner Audio: L_OUT=(L+R)/2, R_OUT=SAP
  75. * 0 0 1 TV Tuner Audio: L_OUT=R_OUT=SAP (SAP)
  76. * 0 1 0 TV Tuner Audio: L_OUT=L, R_OUT=R (stereo)
  77. * 0 1 1 TV Tuner Audio: mute
  78. * 1 * * TV Tuner Audio: L_OUT=R_OUT=(L+R)/2 (mono)
  79. *
  80. * BR* : Audio Sample Rate (BR stands for bitrate for some reason)
  81. * BR0 BR1
  82. * 0 0 32 kHz
  83. * 0 1 44.1 kHz
  84. * 1 0 48 kHz
  85. *
  86. * DM* : Detected tuner audio Mode
  87. * Unknown currently
  88. *
  89. * Special thanks to AVerMedia Technologies, Inc. and Jiun-Kuei Jung at
  90. * AVerMedia for providing the GPIO information used to add support
  91. * for the M179 cards.
  92. */
  93. /********************* GPIO stuffs *********************/
  94. /* GPIO registers */
  95. #define IVTV_REG_GPIO_IN 0x9008
  96. #define IVTV_REG_GPIO_OUT 0x900c
  97. #define IVTV_REG_GPIO_DIR 0x9020
  98. void ivtv_reset_ir_gpio(struct ivtv *itv)
  99. {
  100. int curdir, curout;
  101. if (itv->card->type != IVTV_CARD_PVR_150)
  102. return;
  103. IVTV_DEBUG_INFO("Resetting PVR150 IR\n");
  104. curout = read_reg(IVTV_REG_GPIO_OUT);
  105. curdir = read_reg(IVTV_REG_GPIO_DIR);
  106. curdir |= 0x80;
  107. write_reg(curdir, IVTV_REG_GPIO_DIR);
  108. curout = (curout & ~0xF) | 1;
  109. write_reg(curout, IVTV_REG_GPIO_OUT);
  110. /* We could use something else for smaller time */
  111. schedule_timeout_interruptible(msecs_to_jiffies(1));
  112. curout |= 2;
  113. write_reg(curout, IVTV_REG_GPIO_OUT);
  114. curdir &= ~0x80;
  115. write_reg(curdir, IVTV_REG_GPIO_DIR);
  116. }
  117. /* Xceive tuner reset function */
  118. int ivtv_reset_tuner_gpio(void *dev, int component, int cmd, int value)
  119. {
  120. struct i2c_algo_bit_data *algo = dev;
  121. struct ivtv *itv = algo->data;
  122. u32 curout;
  123. if (cmd != XC2028_TUNER_RESET)
  124. return 0;
  125. IVTV_DEBUG_INFO("Resetting tuner\n");
  126. curout = read_reg(IVTV_REG_GPIO_OUT);
  127. curout &= ~(1 << itv->card->xceive_pin);
  128. write_reg(curout, IVTV_REG_GPIO_OUT);
  129. schedule_timeout_interruptible(msecs_to_jiffies(1));
  130. curout |= 1 << itv->card->xceive_pin;
  131. write_reg(curout, IVTV_REG_GPIO_OUT);
  132. schedule_timeout_interruptible(msecs_to_jiffies(1));
  133. return 0;
  134. }
  135. static inline struct ivtv *sd_to_ivtv(struct v4l2_subdev *sd)
  136. {
  137. return container_of(sd, struct ivtv, sd_gpio);
  138. }
  139. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  140. {
  141. return &container_of(ctrl->handler, struct ivtv, hdl_gpio)->sd_gpio;
  142. }
  143. static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  144. {
  145. struct ivtv *itv = sd_to_ivtv(sd);
  146. u16 mask, data;
  147. mask = itv->card->gpio_audio_freq.mask;
  148. switch (freq) {
  149. case 32000:
  150. data = itv->card->gpio_audio_freq.f32000;
  151. break;
  152. case 44100:
  153. data = itv->card->gpio_audio_freq.f44100;
  154. break;
  155. case 48000:
  156. default:
  157. data = itv->card->gpio_audio_freq.f48000;
  158. break;
  159. }
  160. if (mask)
  161. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  162. return 0;
  163. }
  164. static int subdev_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  165. {
  166. struct ivtv *itv = sd_to_ivtv(sd);
  167. u16 mask;
  168. mask = itv->card->gpio_audio_detect.mask;
  169. if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
  170. vt->rxsubchans = V4L2_TUNER_SUB_STEREO |
  171. V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  172. else
  173. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  174. return 0;
  175. }
  176. static int subdev_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  177. {
  178. struct ivtv *itv = sd_to_ivtv(sd);
  179. u16 mask, data;
  180. mask = itv->card->gpio_audio_mode.mask;
  181. switch (vt->audmode) {
  182. case V4L2_TUNER_MODE_LANG1:
  183. data = itv->card->gpio_audio_mode.lang1;
  184. break;
  185. case V4L2_TUNER_MODE_LANG2:
  186. data = itv->card->gpio_audio_mode.lang2;
  187. break;
  188. case V4L2_TUNER_MODE_MONO:
  189. data = itv->card->gpio_audio_mode.mono;
  190. break;
  191. case V4L2_TUNER_MODE_STEREO:
  192. case V4L2_TUNER_MODE_LANG1_LANG2:
  193. default:
  194. data = itv->card->gpio_audio_mode.stereo;
  195. break;
  196. }
  197. if (mask)
  198. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  199. return 0;
  200. }
  201. static int subdev_s_radio(struct v4l2_subdev *sd)
  202. {
  203. struct ivtv *itv = sd_to_ivtv(sd);
  204. u16 mask, data;
  205. mask = itv->card->gpio_audio_input.mask;
  206. data = itv->card->gpio_audio_input.radio;
  207. if (mask)
  208. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  209. return 0;
  210. }
  211. static int subdev_s_audio_routing(struct v4l2_subdev *sd,
  212. u32 input, u32 output, u32 config)
  213. {
  214. struct ivtv *itv = sd_to_ivtv(sd);
  215. u16 mask, data;
  216. if (input > 2)
  217. return -EINVAL;
  218. mask = itv->card->gpio_audio_input.mask;
  219. switch (input) {
  220. case 0:
  221. data = itv->card->gpio_audio_input.tuner;
  222. break;
  223. case 1:
  224. data = itv->card->gpio_audio_input.linein;
  225. break;
  226. case 2:
  227. default:
  228. data = itv->card->gpio_audio_input.radio;
  229. break;
  230. }
  231. if (mask)
  232. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  233. return 0;
  234. }
  235. static int subdev_s_ctrl(struct v4l2_ctrl *ctrl)
  236. {
  237. struct v4l2_subdev *sd = to_sd(ctrl);
  238. struct ivtv *itv = sd_to_ivtv(sd);
  239. u16 mask, data;
  240. switch (ctrl->id) {
  241. case V4L2_CID_AUDIO_MUTE:
  242. mask = itv->card->gpio_audio_mute.mask;
  243. data = ctrl->val ? itv->card->gpio_audio_mute.mute : 0;
  244. if (mask)
  245. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) |
  246. (data & mask), IVTV_REG_GPIO_OUT);
  247. return 0;
  248. }
  249. return -EINVAL;
  250. }
  251. static int subdev_log_status(struct v4l2_subdev *sd)
  252. {
  253. struct ivtv *itv = sd_to_ivtv(sd);
  254. IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
  255. read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
  256. read_reg(IVTV_REG_GPIO_IN));
  257. v4l2_ctrl_handler_log_status(&itv->hdl_gpio, sd->name);
  258. return 0;
  259. }
  260. static int subdev_s_video_routing(struct v4l2_subdev *sd,
  261. u32 input, u32 output, u32 config)
  262. {
  263. struct ivtv *itv = sd_to_ivtv(sd);
  264. u16 mask, data;
  265. if (input > 2) /* 0:Tuner 1:Composite 2:S-Video */
  266. return -EINVAL;
  267. mask = itv->card->gpio_video_input.mask;
  268. if (input == 0)
  269. data = itv->card->gpio_video_input.tuner;
  270. else if (input == 1)
  271. data = itv->card->gpio_video_input.composite;
  272. else
  273. data = itv->card->gpio_video_input.svideo;
  274. if (mask)
  275. write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
  276. return 0;
  277. }
  278. static const struct v4l2_ctrl_ops gpio_ctrl_ops = {
  279. .s_ctrl = subdev_s_ctrl,
  280. };
  281. static const struct v4l2_subdev_core_ops subdev_core_ops = {
  282. .log_status = subdev_log_status,
  283. };
  284. static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = {
  285. .s_radio = subdev_s_radio,
  286. .g_tuner = subdev_g_tuner,
  287. .s_tuner = subdev_s_tuner,
  288. };
  289. static const struct v4l2_subdev_audio_ops subdev_audio_ops = {
  290. .s_clock_freq = subdev_s_clock_freq,
  291. .s_routing = subdev_s_audio_routing,
  292. };
  293. static const struct v4l2_subdev_video_ops subdev_video_ops = {
  294. .s_routing = subdev_s_video_routing,
  295. };
  296. static const struct v4l2_subdev_ops subdev_ops = {
  297. .core = &subdev_core_ops,
  298. .tuner = &subdev_tuner_ops,
  299. .audio = &subdev_audio_ops,
  300. .video = &subdev_video_ops,
  301. };
  302. int ivtv_gpio_init(struct ivtv *itv)
  303. {
  304. u16 pin = 0;
  305. if (itv->card->xceive_pin)
  306. pin = 1 << itv->card->xceive_pin;
  307. if ((itv->card->gpio_init.direction | pin) == 0)
  308. return 0;
  309. IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
  310. read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
  311. /* init output data then direction */
  312. write_reg(itv->card->gpio_init.initial_value | pin, IVTV_REG_GPIO_OUT);
  313. write_reg(itv->card->gpio_init.direction | pin, IVTV_REG_GPIO_DIR);
  314. v4l2_subdev_init(&itv->sd_gpio, &subdev_ops);
  315. snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name);
  316. itv->sd_gpio.grp_id = IVTV_HW_GPIO;
  317. v4l2_ctrl_handler_init(&itv->hdl_gpio, 1);
  318. v4l2_ctrl_new_std(&itv->hdl_gpio, &gpio_ctrl_ops,
  319. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  320. if (itv->hdl_gpio.error)
  321. return itv->hdl_gpio.error;
  322. itv->sd_gpio.ctrl_handler = &itv->hdl_gpio;
  323. v4l2_ctrl_handler_setup(&itv->hdl_gpio);
  324. return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio);
  325. }