solo6x10-enc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/font.h>
  22. #include <linux/bitrev.h>
  23. #include <linux/slab.h>
  24. #include "solo6x10.h"
  25. #define VI_PROG_HSIZE (1280 - 16)
  26. #define VI_PROG_VSIZE (1024 - 16)
  27. #define IRQ_LEVEL 2
  28. static void solo_capture_config(struct solo_dev *solo_dev)
  29. {
  30. unsigned long height;
  31. unsigned long width;
  32. void *buf;
  33. int i;
  34. solo_reg_write(solo_dev, SOLO_CAP_BASE,
  35. SOLO_CAP_MAX_PAGE((SOLO_CAP_EXT_SIZE(solo_dev)
  36. - SOLO_CAP_PAGE_SIZE) >> 16)
  37. | SOLO_CAP_BASE_ADDR(SOLO_CAP_EXT_ADDR(solo_dev) >> 16));
  38. /* XXX: Undocumented bits at b17 and b24 */
  39. if (solo_dev->type == SOLO_DEV_6110) {
  40. /* NOTE: Ref driver has (62 << 24) here as well, but it causes
  41. * wacked out frame timing on 4-port 6110. */
  42. solo_reg_write(solo_dev, SOLO_CAP_BTW,
  43. (1 << 17) | SOLO_CAP_PROG_BANDWIDTH(2) |
  44. SOLO_CAP_MAX_BANDWIDTH(36));
  45. } else {
  46. solo_reg_write(solo_dev, SOLO_CAP_BTW,
  47. (1 << 17) | SOLO_CAP_PROG_BANDWIDTH(2) |
  48. SOLO_CAP_MAX_BANDWIDTH(32));
  49. }
  50. /* Set scale 1, 9 dimension */
  51. width = solo_dev->video_hsize;
  52. height = solo_dev->video_vsize;
  53. solo_reg_write(solo_dev, SOLO_DIM_SCALE1,
  54. SOLO_DIM_H_MB_NUM(width / 16) |
  55. SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
  56. SOLO_DIM_V_MB_NUM_FIELD(height / 16));
  57. /* Set scale 2, 10 dimension */
  58. width = solo_dev->video_hsize / 2;
  59. height = solo_dev->video_vsize;
  60. solo_reg_write(solo_dev, SOLO_DIM_SCALE2,
  61. SOLO_DIM_H_MB_NUM(width / 16) |
  62. SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
  63. SOLO_DIM_V_MB_NUM_FIELD(height / 16));
  64. /* Set scale 3, 11 dimension */
  65. width = solo_dev->video_hsize / 2;
  66. height = solo_dev->video_vsize / 2;
  67. solo_reg_write(solo_dev, SOLO_DIM_SCALE3,
  68. SOLO_DIM_H_MB_NUM(width / 16) |
  69. SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
  70. SOLO_DIM_V_MB_NUM_FIELD(height / 16));
  71. /* Set scale 4, 12 dimension */
  72. width = solo_dev->video_hsize / 3;
  73. height = solo_dev->video_vsize / 3;
  74. solo_reg_write(solo_dev, SOLO_DIM_SCALE4,
  75. SOLO_DIM_H_MB_NUM(width / 16) |
  76. SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
  77. SOLO_DIM_V_MB_NUM_FIELD(height / 16));
  78. /* Set scale 5, 13 dimension */
  79. width = solo_dev->video_hsize / 4;
  80. height = solo_dev->video_vsize / 2;
  81. solo_reg_write(solo_dev, SOLO_DIM_SCALE5,
  82. SOLO_DIM_H_MB_NUM(width / 16) |
  83. SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
  84. SOLO_DIM_V_MB_NUM_FIELD(height / 16));
  85. /* Progressive */
  86. width = VI_PROG_HSIZE;
  87. height = VI_PROG_VSIZE;
  88. solo_reg_write(solo_dev, SOLO_DIM_PROG,
  89. SOLO_DIM_H_MB_NUM(width / 16) |
  90. SOLO_DIM_V_MB_NUM_FRAME(height / 16) |
  91. SOLO_DIM_V_MB_NUM_FIELD(height / 16));
  92. /* Clear OSD */
  93. solo_reg_write(solo_dev, SOLO_VE_OSD_CH, 0);
  94. solo_reg_write(solo_dev, SOLO_VE_OSD_BASE, SOLO_EOSD_EXT_ADDR >> 16);
  95. solo_reg_write(solo_dev, SOLO_VE_OSD_CLR,
  96. 0xF0 << 16 | 0x80 << 8 | 0x80);
  97. if (solo_dev->type == SOLO_DEV_6010)
  98. solo_reg_write(solo_dev, SOLO_VE_OSD_OPT,
  99. SOLO_VE_OSD_H_SHADOW | SOLO_VE_OSD_V_SHADOW);
  100. else
  101. solo_reg_write(solo_dev, SOLO_VE_OSD_OPT, SOLO_VE_OSD_V_DOUBLE
  102. | SOLO_VE_OSD_H_SHADOW | SOLO_VE_OSD_V_SHADOW);
  103. /* Clear OSG buffer */
  104. buf = kzalloc(SOLO_EOSD_EXT_SIZE(solo_dev), GFP_KERNEL);
  105. if (!buf)
  106. return;
  107. for (i = 0; i < solo_dev->nr_chans; i++) {
  108. solo_p2m_dma(solo_dev, 1, buf,
  109. SOLO_EOSD_EXT_ADDR +
  110. (SOLO_EOSD_EXT_SIZE(solo_dev) * i),
  111. SOLO_EOSD_EXT_SIZE(solo_dev), 0, 0);
  112. }
  113. kfree(buf);
  114. }
  115. #define SOLO_OSD_WRITE_SIZE (16 * OSD_TEXT_MAX)
  116. /* Should be called with enable_lock held */
  117. int solo_osd_print(struct solo_enc_dev *solo_enc)
  118. {
  119. struct solo_dev *solo_dev = solo_enc->solo_dev;
  120. u8 *str = solo_enc->osd_text;
  121. u8 *buf = solo_enc->osd_buf;
  122. u32 reg;
  123. const struct font_desc *vga = find_font("VGA8x16");
  124. const u8 *vga_data;
  125. int i, j;
  126. if (WARN_ON_ONCE(!vga))
  127. return -ENODEV;
  128. reg = solo_reg_read(solo_dev, SOLO_VE_OSD_CH);
  129. if (!*str) {
  130. /* Disable OSD on this channel */
  131. reg &= ~(1 << solo_enc->ch);
  132. goto out;
  133. }
  134. memset(buf, 0, SOLO_OSD_WRITE_SIZE);
  135. vga_data = (const u8 *)vga->data;
  136. for (i = 0; *str; i++, str++) {
  137. for (j = 0; j < 16; j++) {
  138. buf[(j << 1) | (i & 1) | ((i & ~1) << 4)] =
  139. bitrev8(vga_data[(*str << 4) | j]);
  140. }
  141. }
  142. solo_p2m_dma(solo_dev, 1, buf,
  143. SOLO_EOSD_EXT_ADDR_CHAN(solo_dev, solo_enc->ch),
  144. SOLO_OSD_WRITE_SIZE, 0, 0);
  145. /* Enable OSD on this channel */
  146. reg |= (1 << solo_enc->ch);
  147. out:
  148. solo_reg_write(solo_dev, SOLO_VE_OSD_CH, reg);
  149. return 0;
  150. }
  151. /*
  152. * Set channel Quality Profile (0-3).
  153. */
  154. void solo_s_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch,
  155. unsigned int qp)
  156. {
  157. unsigned long flags;
  158. unsigned int idx, reg;
  159. if ((ch > 31) || (qp > 3))
  160. return;
  161. if (solo_dev->type == SOLO_DEV_6010)
  162. return;
  163. if (ch < 16) {
  164. idx = 0;
  165. reg = SOLO_VE_JPEG_QP_CH_L;
  166. } else {
  167. ch -= 16;
  168. idx = 1;
  169. reg = SOLO_VE_JPEG_QP_CH_H;
  170. }
  171. ch *= 2;
  172. spin_lock_irqsave(&solo_dev->jpeg_qp_lock, flags);
  173. solo_dev->jpeg_qp[idx] &= ~(3 << ch);
  174. solo_dev->jpeg_qp[idx] |= (qp & 3) << ch;
  175. solo_reg_write(solo_dev, reg, solo_dev->jpeg_qp[idx]);
  176. spin_unlock_irqrestore(&solo_dev->jpeg_qp_lock, flags);
  177. }
  178. int solo_g_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch)
  179. {
  180. int idx;
  181. if (solo_dev->type == SOLO_DEV_6010)
  182. return 2;
  183. if (WARN_ON_ONCE(ch > 31))
  184. return 2;
  185. if (ch < 16) {
  186. idx = 0;
  187. } else {
  188. ch -= 16;
  189. idx = 1;
  190. }
  191. ch *= 2;
  192. return (solo_dev->jpeg_qp[idx] >> ch) & 3;
  193. }
  194. #define SOLO_QP_INIT 0xaaaaaaaa
  195. static void solo_jpeg_config(struct solo_dev *solo_dev)
  196. {
  197. if (solo_dev->type == SOLO_DEV_6010) {
  198. solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_TBL,
  199. (2 << 24) | (2 << 16) | (2 << 8) | 2);
  200. } else {
  201. solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_TBL,
  202. (4 << 24) | (3 << 16) | (2 << 8) | 1);
  203. }
  204. spin_lock_init(&solo_dev->jpeg_qp_lock);
  205. /* Initialize Quality Profile for all channels */
  206. solo_dev->jpeg_qp[0] = solo_dev->jpeg_qp[1] = SOLO_QP_INIT;
  207. solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_CH_L, SOLO_QP_INIT);
  208. solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_CH_H, SOLO_QP_INIT);
  209. solo_reg_write(solo_dev, SOLO_VE_JPEG_CFG,
  210. (SOLO_JPEG_EXT_SIZE(solo_dev) & 0xffff0000) |
  211. ((SOLO_JPEG_EXT_ADDR(solo_dev) >> 16) & 0x0000ffff));
  212. solo_reg_write(solo_dev, SOLO_VE_JPEG_CTRL, 0xffffffff);
  213. if (solo_dev->type == SOLO_DEV_6110) {
  214. solo_reg_write(solo_dev, SOLO_VE_JPEG_CFG1,
  215. (0 << 16) | (30 << 8) | 60);
  216. }
  217. }
  218. static void solo_mp4e_config(struct solo_dev *solo_dev)
  219. {
  220. int i;
  221. u32 cfg;
  222. solo_reg_write(solo_dev, SOLO_VE_CFG0,
  223. SOLO_VE_INTR_CTRL(IRQ_LEVEL) |
  224. SOLO_VE_BLOCK_SIZE(SOLO_MP4E_EXT_SIZE(solo_dev) >> 16) |
  225. SOLO_VE_BLOCK_BASE(SOLO_MP4E_EXT_ADDR(solo_dev) >> 16));
  226. cfg = SOLO_VE_BYTE_ALIGN(2) | SOLO_VE_INSERT_INDEX
  227. | SOLO_VE_MOTION_MODE(0);
  228. if (solo_dev->type != SOLO_DEV_6010) {
  229. cfg |= SOLO_VE_MPEG_SIZE_H(
  230. (SOLO_MP4E_EXT_SIZE(solo_dev) >> 24) & 0x0f);
  231. cfg |= SOLO_VE_JPEG_SIZE_H(
  232. (SOLO_JPEG_EXT_SIZE(solo_dev) >> 24) & 0x0f);
  233. }
  234. solo_reg_write(solo_dev, SOLO_VE_CFG1, cfg);
  235. solo_reg_write(solo_dev, SOLO_VE_WMRK_POLY, 0);
  236. solo_reg_write(solo_dev, SOLO_VE_VMRK_INIT_KEY, 0);
  237. solo_reg_write(solo_dev, SOLO_VE_WMRK_STRL, 0);
  238. if (solo_dev->type == SOLO_DEV_6110)
  239. solo_reg_write(solo_dev, SOLO_VE_WMRK_ENABLE, 0);
  240. solo_reg_write(solo_dev, SOLO_VE_ENCRYP_POLY, 0);
  241. solo_reg_write(solo_dev, SOLO_VE_ENCRYP_INIT, 0);
  242. solo_reg_write(solo_dev, SOLO_VE_ATTR,
  243. SOLO_VE_LITTLE_ENDIAN |
  244. SOLO_COMP_ATTR_FCODE(1) |
  245. SOLO_COMP_TIME_INC(0) |
  246. SOLO_COMP_TIME_WIDTH(15) |
  247. SOLO_DCT_INTERVAL(solo_dev->type == SOLO_DEV_6010 ? 9 : 10));
  248. for (i = 0; i < solo_dev->nr_chans; i++) {
  249. solo_reg_write(solo_dev, SOLO_VE_CH_REF_BASE(i),
  250. (SOLO_EREF_EXT_ADDR(solo_dev) +
  251. (i * SOLO_EREF_EXT_SIZE)) >> 16);
  252. solo_reg_write(solo_dev, SOLO_VE_CH_REF_BASE_E(i),
  253. (SOLO_EREF_EXT_ADDR(solo_dev) +
  254. ((i + 16) * SOLO_EREF_EXT_SIZE)) >> 16);
  255. }
  256. if (solo_dev->type == SOLO_DEV_6110) {
  257. solo_reg_write(solo_dev, SOLO_VE_COMPT_MOT, 0x00040008);
  258. } else {
  259. for (i = 0; i < solo_dev->nr_chans; i++)
  260. solo_reg_write(solo_dev, SOLO_VE_CH_MOT(i), 0x100);
  261. }
  262. }
  263. int solo_enc_init(struct solo_dev *solo_dev)
  264. {
  265. int i;
  266. solo_capture_config(solo_dev);
  267. solo_mp4e_config(solo_dev);
  268. solo_jpeg_config(solo_dev);
  269. for (i = 0; i < solo_dev->nr_chans; i++) {
  270. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(i), 0);
  271. solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(i), 0);
  272. }
  273. return 0;
  274. }
  275. void solo_enc_exit(struct solo_dev *solo_dev)
  276. {
  277. int i;
  278. for (i = 0; i < solo_dev->nr_chans; i++) {
  279. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(i), 0);
  280. solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(i), 0);
  281. }
  282. }