ivtv-gpio.c 9.5 KB

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