cx18-gpio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * cx18 gpio functions
  3. *
  4. * Derived from ivtv-gpio.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include "cx18-driver.h"
  20. #include "cx18-io.h"
  21. #include "cx18-cards.h"
  22. #include "cx18-gpio.h"
  23. #include "tuner-xc2028.h"
  24. /********************* GPIO stuffs *********************/
  25. /* GPIO registers */
  26. #define CX18_REG_GPIO_IN 0xc72010
  27. #define CX18_REG_GPIO_OUT1 0xc78100
  28. #define CX18_REG_GPIO_DIR1 0xc78108
  29. #define CX18_REG_GPIO_OUT2 0xc78104
  30. #define CX18_REG_GPIO_DIR2 0xc7810c
  31. /*
  32. * HVR-1600 GPIO pins, courtesy of Hauppauge:
  33. *
  34. * gpio0: zilog ir process reset pin
  35. * gpio1: zilog programming pin (you should never use this)
  36. * gpio12: cx24227 reset pin
  37. * gpio13: cs5345 reset pin
  38. */
  39. /*
  40. * File scope utility functions
  41. */
  42. static void gpio_write(struct cx18 *cx)
  43. {
  44. u32 dir_lo = cx->gpio_dir & 0xffff;
  45. u32 val_lo = cx->gpio_val & 0xffff;
  46. u32 dir_hi = cx->gpio_dir >> 16;
  47. u32 val_hi = cx->gpio_val >> 16;
  48. cx18_write_reg_expect(cx, dir_lo << 16,
  49. CX18_REG_GPIO_DIR1, ~dir_lo, dir_lo);
  50. cx18_write_reg_expect(cx, (dir_lo << 16) | val_lo,
  51. CX18_REG_GPIO_OUT1, val_lo, dir_lo);
  52. cx18_write_reg_expect(cx, dir_hi << 16,
  53. CX18_REG_GPIO_DIR2, ~dir_hi, dir_hi);
  54. cx18_write_reg_expect(cx, (dir_hi << 16) | val_hi,
  55. CX18_REG_GPIO_OUT2, val_hi, dir_hi);
  56. }
  57. static void gpio_update(struct cx18 *cx, u32 mask, u32 data)
  58. {
  59. if (mask == 0)
  60. return;
  61. mutex_lock(&cx->gpio_lock);
  62. cx->gpio_val = (cx->gpio_val & ~mask) | (data & mask);
  63. gpio_write(cx);
  64. mutex_unlock(&cx->gpio_lock);
  65. }
  66. static void gpio_reset_seq(struct cx18 *cx, u32 active_lo, u32 active_hi,
  67. unsigned int assert_msecs,
  68. unsigned int recovery_msecs)
  69. {
  70. u32 mask;
  71. mask = active_lo | active_hi;
  72. if (mask == 0)
  73. return;
  74. /*
  75. * Assuming that active_hi and active_lo are a subsets of the bits in
  76. * gpio_dir. Also assumes that active_lo and active_hi don't overlap
  77. * in any bit position
  78. */
  79. /* Assert */
  80. gpio_update(cx, mask, ~active_lo);
  81. schedule_timeout_uninterruptible(msecs_to_jiffies(assert_msecs));
  82. /* Deassert */
  83. gpio_update(cx, mask, ~active_hi);
  84. schedule_timeout_uninterruptible(msecs_to_jiffies(recovery_msecs));
  85. }
  86. /*
  87. * GPIO Multiplexer - logical device
  88. */
  89. static int gpiomux_log_status(struct v4l2_subdev *sd)
  90. {
  91. struct cx18 *cx = v4l2_get_subdevdata(sd);
  92. mutex_lock(&cx->gpio_lock);
  93. CX18_INFO_DEV(sd, "GPIO: direction 0x%08x, value 0x%08x\n",
  94. cx->gpio_dir, cx->gpio_val);
  95. mutex_unlock(&cx->gpio_lock);
  96. return 0;
  97. }
  98. static int gpiomux_s_radio(struct v4l2_subdev *sd)
  99. {
  100. struct cx18 *cx = v4l2_get_subdevdata(sd);
  101. /*
  102. * FIXME - work out the cx->active/audio_input mess - this is
  103. * intended to handle the switch to radio mode and set the
  104. * audio routing, but we need to update the state in cx
  105. */
  106. gpio_update(cx, cx->card->gpio_audio_input.mask,
  107. cx->card->gpio_audio_input.radio);
  108. return 0;
  109. }
  110. static int gpiomux_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  111. {
  112. struct cx18 *cx = v4l2_get_subdevdata(sd);
  113. u32 data;
  114. switch (cx->card->audio_inputs[cx->audio_input].muxer_input) {
  115. case 1:
  116. data = cx->card->gpio_audio_input.linein;
  117. break;
  118. case 0:
  119. data = cx->card->gpio_audio_input.tuner;
  120. break;
  121. default:
  122. /*
  123. * FIXME - work out the cx->active/audio_input mess - this is
  124. * intended to handle the switch from radio mode and set the
  125. * audio routing, but we need to update the state in cx
  126. */
  127. data = cx->card->gpio_audio_input.tuner;
  128. break;
  129. }
  130. gpio_update(cx, cx->card->gpio_audio_input.mask, data);
  131. return 0;
  132. }
  133. static int gpiomux_s_audio_routing(struct v4l2_subdev *sd,
  134. u32 input, u32 output, u32 config)
  135. {
  136. struct cx18 *cx = v4l2_get_subdevdata(sd);
  137. u32 data;
  138. switch (input) {
  139. case 0:
  140. data = cx->card->gpio_audio_input.tuner;
  141. break;
  142. case 1:
  143. data = cx->card->gpio_audio_input.linein;
  144. break;
  145. case 2:
  146. data = cx->card->gpio_audio_input.radio;
  147. break;
  148. default:
  149. return -EINVAL;
  150. }
  151. gpio_update(cx, cx->card->gpio_audio_input.mask, data);
  152. return 0;
  153. }
  154. static const struct v4l2_subdev_core_ops gpiomux_core_ops = {
  155. .log_status = gpiomux_log_status,
  156. };
  157. static const struct v4l2_subdev_tuner_ops gpiomux_tuner_ops = {
  158. .s_radio = gpiomux_s_radio,
  159. };
  160. static const struct v4l2_subdev_audio_ops gpiomux_audio_ops = {
  161. .s_routing = gpiomux_s_audio_routing,
  162. };
  163. static const struct v4l2_subdev_video_ops gpiomux_video_ops = {
  164. .s_std = gpiomux_s_std,
  165. };
  166. static const struct v4l2_subdev_ops gpiomux_ops = {
  167. .core = &gpiomux_core_ops,
  168. .tuner = &gpiomux_tuner_ops,
  169. .audio = &gpiomux_audio_ops,
  170. .video = &gpiomux_video_ops,
  171. };
  172. /*
  173. * GPIO Reset Controller - logical device
  174. */
  175. static int resetctrl_log_status(struct v4l2_subdev *sd)
  176. {
  177. struct cx18 *cx = v4l2_get_subdevdata(sd);
  178. mutex_lock(&cx->gpio_lock);
  179. CX18_INFO_DEV(sd, "GPIO: direction 0x%08x, value 0x%08x\n",
  180. cx->gpio_dir, cx->gpio_val);
  181. mutex_unlock(&cx->gpio_lock);
  182. return 0;
  183. }
  184. static int resetctrl_reset(struct v4l2_subdev *sd, u32 val)
  185. {
  186. struct cx18 *cx = v4l2_get_subdevdata(sd);
  187. const struct cx18_gpio_i2c_slave_reset *p;
  188. p = &cx->card->gpio_i2c_slave_reset;
  189. switch (val) {
  190. case CX18_GPIO_RESET_I2C:
  191. gpio_reset_seq(cx, p->active_lo_mask, p->active_hi_mask,
  192. p->msecs_asserted, p->msecs_recovery);
  193. break;
  194. case CX18_GPIO_RESET_Z8F0811:
  195. /*
  196. * Assert timing for the Z8F0811 on HVR-1600 boards:
  197. * 1. Assert RESET for min of 4 clock cycles at 18.432 MHz to
  198. * initiate
  199. * 2. Reset then takes 66 WDT cycles at 10 kHz + 16 xtal clock
  200. * cycles (6,601,085 nanoseconds ~= 7 milliseconds)
  201. * 3. DBG pin must be high before chip exits reset for normal
  202. * operation. DBG is open drain and hopefully pulled high
  203. * since we don't normally drive it (GPIO 1?) for the
  204. * HVR-1600
  205. * 4. Z8F0811 won't exit reset until RESET is deasserted
  206. * 5. Zilog comes out of reset, loads reset vector address and
  207. * executes from there. Required recovery delay unknown.
  208. */
  209. gpio_reset_seq(cx, p->ir_reset_mask, 0,
  210. p->msecs_asserted, p->msecs_recovery);
  211. break;
  212. case CX18_GPIO_RESET_XC2028:
  213. if (cx->card->tuners[0].tuner == TUNER_XC2028)
  214. gpio_reset_seq(cx, (1 << cx->card->xceive_pin), 0,
  215. 1, 1);
  216. break;
  217. }
  218. return 0;
  219. }
  220. static const struct v4l2_subdev_core_ops resetctrl_core_ops = {
  221. .log_status = resetctrl_log_status,
  222. .reset = resetctrl_reset,
  223. };
  224. static const struct v4l2_subdev_ops resetctrl_ops = {
  225. .core = &resetctrl_core_ops,
  226. };
  227. /*
  228. * External entry points
  229. */
  230. void cx18_gpio_init(struct cx18 *cx)
  231. {
  232. mutex_lock(&cx->gpio_lock);
  233. cx->gpio_dir = cx->card->gpio_init.direction;
  234. cx->gpio_val = cx->card->gpio_init.initial_value;
  235. if (cx->card->tuners[0].tuner == TUNER_XC2028) {
  236. cx->gpio_dir |= 1 << cx->card->xceive_pin;
  237. cx->gpio_val |= 1 << cx->card->xceive_pin;
  238. }
  239. if (cx->gpio_dir == 0) {
  240. mutex_unlock(&cx->gpio_lock);
  241. return;
  242. }
  243. CX18_DEBUG_INFO("GPIO initial dir: %08x/%08x out: %08x/%08x\n",
  244. cx18_read_reg(cx, CX18_REG_GPIO_DIR1),
  245. cx18_read_reg(cx, CX18_REG_GPIO_DIR2),
  246. cx18_read_reg(cx, CX18_REG_GPIO_OUT1),
  247. cx18_read_reg(cx, CX18_REG_GPIO_OUT2));
  248. gpio_write(cx);
  249. mutex_unlock(&cx->gpio_lock);
  250. }
  251. int cx18_gpio_register(struct cx18 *cx, u32 hw)
  252. {
  253. struct v4l2_subdev *sd;
  254. const struct v4l2_subdev_ops *ops;
  255. char *str;
  256. switch (hw) {
  257. case CX18_HW_GPIO_MUX:
  258. sd = &cx->sd_gpiomux;
  259. ops = &gpiomux_ops;
  260. str = "gpio-mux";
  261. break;
  262. case CX18_HW_GPIO_RESET_CTRL:
  263. sd = &cx->sd_resetctrl;
  264. ops = &resetctrl_ops;
  265. str = "gpio-reset-ctrl";
  266. break;
  267. default:
  268. return -EINVAL;
  269. }
  270. v4l2_subdev_init(sd, ops);
  271. v4l2_set_subdevdata(sd, cx);
  272. snprintf(sd->name, sizeof(sd->name), "%s %s", cx->v4l2_dev.name, str);
  273. sd->grp_id = hw;
  274. return v4l2_device_register_subdev(&cx->v4l2_dev, sd);
  275. }
  276. void cx18_reset_ir_gpio(void *data)
  277. {
  278. struct cx18 *cx = to_cx18((struct v4l2_device *)data);
  279. if (cx->card->gpio_i2c_slave_reset.ir_reset_mask == 0)
  280. return;
  281. CX18_DEBUG_INFO("Resetting IR microcontroller\n");
  282. v4l2_subdev_call(&cx->sd_resetctrl,
  283. core, reset, CX18_GPIO_RESET_Z8F0811);
  284. }
  285. EXPORT_SYMBOL(cx18_reset_ir_gpio);
  286. /* This symbol is exported for use by lirc_pvr150 for the IR-blaster */
  287. /* Xceive tuner reset function */
  288. int cx18_reset_tuner_gpio(void *dev, int component, int cmd, int value)
  289. {
  290. struct i2c_algo_bit_data *algo = dev;
  291. struct cx18_i2c_algo_callback_data *cb_data = algo->data;
  292. struct cx18 *cx = cb_data->cx;
  293. if (cmd != XC2028_TUNER_RESET ||
  294. cx->card->tuners[0].tuner != TUNER_XC2028)
  295. return 0;
  296. CX18_DEBUG_INFO("Resetting XCeive tuner\n");
  297. return v4l2_subdev_call(&cx->sd_resetctrl,
  298. core, reset, CX18_GPIO_RESET_XC2028);
  299. }