cx18-gpio.c 9.2 KB

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