cx18-gpio.c 8.5 KB

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