zr36060.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * Zoran ZR36060 basic configuration functions
  3. *
  4. * Copyright (C) 2002 Laurent Pinchart <laurent.pinchart@skynet.be>
  5. *
  6. * $Id: zr36060.c,v 1.1.2.22 2003/05/06 09:35:36 rbultje Exp $
  7. *
  8. * ------------------------------------------------------------------------
  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. * ------------------------------------------------------------------------
  21. */
  22. #define ZR060_VERSION "v0.7"
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/types.h>
  28. #include <linux/wait.h>
  29. /* I/O commands, error codes */
  30. #include <asm/io.h>
  31. /* headerfile of this module */
  32. #include "zr36060.h"
  33. /* codec io API */
  34. #include "videocodec.h"
  35. /* it doesn't make sense to have more than 20 or so,
  36. just to prevent some unwanted loops */
  37. #define MAX_CODECS 20
  38. /* amount of chips attached via this driver */
  39. static int zr36060_codecs;
  40. static bool low_bitrate;
  41. module_param(low_bitrate, bool, 0);
  42. MODULE_PARM_DESC(low_bitrate, "Buz compatibility option, halves bitrate");
  43. /* debugging is available via module parameter */
  44. static int debug;
  45. module_param(debug, int, 0);
  46. MODULE_PARM_DESC(debug, "Debug level (0-4)");
  47. #define dprintk(num, format, args...) \
  48. do { \
  49. if (debug >= num) \
  50. printk(format, ##args); \
  51. } while (0)
  52. /* =========================================================================
  53. Local hardware I/O functions:
  54. read/write via codec layer (registers are located in the master device)
  55. ========================================================================= */
  56. /* read and write functions */
  57. static u8
  58. zr36060_read (struct zr36060 *ptr,
  59. u16 reg)
  60. {
  61. u8 value = 0;
  62. // just in case something is wrong...
  63. if (ptr->codec->master_data->readreg)
  64. value = (ptr->codec->master_data->readreg(ptr->codec,
  65. reg)) & 0xff;
  66. else
  67. dprintk(1,
  68. KERN_ERR "%s: invalid I/O setup, nothing read!\n",
  69. ptr->name);
  70. //dprintk(4, "%s: reading from 0x%04x: %02x\n",ptr->name,reg,value);
  71. return value;
  72. }
  73. static void
  74. zr36060_write(struct zr36060 *ptr,
  75. u16 reg,
  76. u8 value)
  77. {
  78. //dprintk(4, "%s: writing 0x%02x to 0x%04x\n",ptr->name,value,reg);
  79. dprintk(4, "0x%02x @0x%04x\n", value, reg);
  80. // just in case something is wrong...
  81. if (ptr->codec->master_data->writereg)
  82. ptr->codec->master_data->writereg(ptr->codec, reg, value);
  83. else
  84. dprintk(1,
  85. KERN_ERR
  86. "%s: invalid I/O setup, nothing written!\n",
  87. ptr->name);
  88. }
  89. /* =========================================================================
  90. Local helper function:
  91. status read
  92. ========================================================================= */
  93. /* status is kept in datastructure */
  94. static u8
  95. zr36060_read_status (struct zr36060 *ptr)
  96. {
  97. ptr->status = zr36060_read(ptr, ZR060_CFSR);
  98. zr36060_read(ptr, 0);
  99. return ptr->status;
  100. }
  101. /* =========================================================================
  102. Local helper function:
  103. scale factor read
  104. ========================================================================= */
  105. /* scale factor is kept in datastructure */
  106. static u16
  107. zr36060_read_scalefactor (struct zr36060 *ptr)
  108. {
  109. ptr->scalefact = (zr36060_read(ptr, ZR060_SF_HI) << 8) |
  110. (zr36060_read(ptr, ZR060_SF_LO) & 0xFF);
  111. /* leave 0 selected for an eventually GO from master */
  112. zr36060_read(ptr, 0);
  113. return ptr->scalefact;
  114. }
  115. /* =========================================================================
  116. Local helper function:
  117. wait if codec is ready to proceed (end of processing) or time is over
  118. ========================================================================= */
  119. static void
  120. zr36060_wait_end (struct zr36060 *ptr)
  121. {
  122. int i = 0;
  123. while (zr36060_read_status(ptr) & ZR060_CFSR_Busy) {
  124. udelay(1);
  125. if (i++ > 200000) { // 200ms, there is for sure something wrong!!!
  126. dprintk(1,
  127. "%s: timeout at wait_end (last status: 0x%02x)\n",
  128. ptr->name, ptr->status);
  129. break;
  130. }
  131. }
  132. }
  133. /* =========================================================================
  134. Local helper function:
  135. basic test of "connectivity", writes/reads to/from memory the SOF marker
  136. ========================================================================= */
  137. static int
  138. zr36060_basic_test (struct zr36060 *ptr)
  139. {
  140. if ((zr36060_read(ptr, ZR060_IDR_DEV) != 0x33) &&
  141. (zr36060_read(ptr, ZR060_IDR_REV) != 0x01)) {
  142. dprintk(1,
  143. KERN_ERR
  144. "%s: attach failed, can't connect to jpeg processor!\n",
  145. ptr->name);
  146. return -ENXIO;
  147. }
  148. zr36060_wait_end(ptr);
  149. if (ptr->status & ZR060_CFSR_Busy) {
  150. dprintk(1,
  151. KERN_ERR
  152. "%s: attach failed, jpeg processor failed (end flag)!\n",
  153. ptr->name);
  154. return -EBUSY;
  155. }
  156. return 0; /* looks good! */
  157. }
  158. /* =========================================================================
  159. Local helper function:
  160. simple loop for pushing the init datasets
  161. ========================================================================= */
  162. static int
  163. zr36060_pushit (struct zr36060 *ptr,
  164. u16 startreg,
  165. u16 len,
  166. const char *data)
  167. {
  168. int i = 0;
  169. dprintk(4, "%s: write data block to 0x%04x (len=%d)\n", ptr->name,
  170. startreg, len);
  171. while (i < len) {
  172. zr36060_write(ptr, startreg++, data[i++]);
  173. }
  174. return i;
  175. }
  176. /* =========================================================================
  177. Basic datasets:
  178. jpeg baseline setup data (you find it on lots places in internet, or just
  179. extract it from any regular .jpg image...)
  180. Could be variable, but until it's not needed it they are just fixed to save
  181. memory. Otherwise expand zr36060 structure with arrays, push the values to
  182. it and initialize from there, as e.g. the linux zr36057/60 driver does it.
  183. ========================================================================= */
  184. static const char zr36060_dqt[0x86] = {
  185. 0xff, 0xdb, //Marker: DQT
  186. 0x00, 0x84, //Length: 2*65+2
  187. 0x00, //Pq,Tq first table
  188. 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
  189. 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
  190. 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
  191. 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
  192. 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
  193. 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
  194. 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
  195. 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
  196. 0x01, //Pq,Tq second table
  197. 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
  198. 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
  199. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  200. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  201. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  202. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  203. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  204. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
  205. };
  206. static const char zr36060_dht[0x1a4] = {
  207. 0xff, 0xc4, //Marker: DHT
  208. 0x01, 0xa2, //Length: 2*AC, 2*DC
  209. 0x00, //DC first table
  210. 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
  211. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  212. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  213. 0x01, //DC second table
  214. 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  215. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  216. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  217. 0x10, //AC first table
  218. 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
  219. 0x05, 0x05, 0x04, 0x04, 0x00, 0x00,
  220. 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11,
  221. 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
  222. 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
  223. 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24,
  224. 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17,
  225. 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34,
  226. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  227. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
  228. 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
  229. 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  230. 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
  231. 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
  232. 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
  233. 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9,
  234. 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
  235. 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
  236. 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
  237. 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  238. 0xF8, 0xF9, 0xFA,
  239. 0x11, //AC second table
  240. 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
  241. 0x07, 0x05, 0x04, 0x04, 0x00, 0x01,
  242. 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
  243. 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  244. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  245. 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62,
  246. 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25,
  247. 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,
  248. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  249. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
  250. 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
  251. 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  252. 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  253. 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  254. 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  255. 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
  256. 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  257. 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
  258. 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
  259. 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  260. 0xF9, 0xFA
  261. };
  262. /* jpeg baseline setup, this is just fixed in this driver (YUV pictures) */
  263. #define NO_OF_COMPONENTS 0x3 //Y,U,V
  264. #define BASELINE_PRECISION 0x8 //MCU size (?)
  265. static const char zr36060_tq[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's QT
  266. static const char zr36060_td[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's DC
  267. static const char zr36060_ta[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's AC
  268. /* horizontal 422 decimation setup (maybe we support 411 or so later, too) */
  269. static const char zr36060_decimation_h[8] = { 2, 1, 1, 0, 0, 0, 0, 0 };
  270. static const char zr36060_decimation_v[8] = { 1, 1, 1, 0, 0, 0, 0, 0 };
  271. /* =========================================================================
  272. Local helper functions:
  273. calculation and setup of parameter-dependent JPEG baseline segments
  274. (needed for compression only)
  275. ========================================================================= */
  276. /* ------------------------------------------------------------------------- */
  277. /* SOF (start of frame) segment depends on width, height and sampling ratio
  278. of each color component */
  279. static int
  280. zr36060_set_sof (struct zr36060 *ptr)
  281. {
  282. char sof_data[34]; // max. size of register set
  283. int i;
  284. dprintk(3, "%s: write SOF (%dx%d, %d components)\n", ptr->name,
  285. ptr->width, ptr->height, NO_OF_COMPONENTS);
  286. sof_data[0] = 0xff;
  287. sof_data[1] = 0xc0;
  288. sof_data[2] = 0x00;
  289. sof_data[3] = (3 * NO_OF_COMPONENTS) + 8;
  290. sof_data[4] = BASELINE_PRECISION; // only '8' possible with zr36060
  291. sof_data[5] = (ptr->height) >> 8;
  292. sof_data[6] = (ptr->height) & 0xff;
  293. sof_data[7] = (ptr->width) >> 8;
  294. sof_data[8] = (ptr->width) & 0xff;
  295. sof_data[9] = NO_OF_COMPONENTS;
  296. for (i = 0; i < NO_OF_COMPONENTS; i++) {
  297. sof_data[10 + (i * 3)] = i; // index identifier
  298. sof_data[11 + (i * 3)] = (ptr->h_samp_ratio[i] << 4) |
  299. (ptr->v_samp_ratio[i]); // sampling ratios
  300. sof_data[12 + (i * 3)] = zr36060_tq[i]; // Q table selection
  301. }
  302. return zr36060_pushit(ptr, ZR060_SOF_IDX,
  303. (3 * NO_OF_COMPONENTS) + 10, sof_data);
  304. }
  305. /* ------------------------------------------------------------------------- */
  306. /* SOS (start of scan) segment depends on the used scan components
  307. of each color component */
  308. static int
  309. zr36060_set_sos (struct zr36060 *ptr)
  310. {
  311. char sos_data[16]; // max. size of register set
  312. int i;
  313. dprintk(3, "%s: write SOS\n", ptr->name);
  314. sos_data[0] = 0xff;
  315. sos_data[1] = 0xda;
  316. sos_data[2] = 0x00;
  317. sos_data[3] = 2 + 1 + (2 * NO_OF_COMPONENTS) + 3;
  318. sos_data[4] = NO_OF_COMPONENTS;
  319. for (i = 0; i < NO_OF_COMPONENTS; i++) {
  320. sos_data[5 + (i * 2)] = i; // index
  321. sos_data[6 + (i * 2)] = (zr36060_td[i] << 4) |
  322. zr36060_ta[i]; // AC/DC tbl.sel.
  323. }
  324. sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 2] = 00; // scan start
  325. sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 3] = 0x3f;
  326. sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 4] = 00;
  327. return zr36060_pushit(ptr, ZR060_SOS_IDX,
  328. 4 + 1 + (2 * NO_OF_COMPONENTS) + 3,
  329. sos_data);
  330. }
  331. /* ------------------------------------------------------------------------- */
  332. /* DRI (define restart interval) */
  333. static int
  334. zr36060_set_dri (struct zr36060 *ptr)
  335. {
  336. char dri_data[6]; // max. size of register set
  337. dprintk(3, "%s: write DRI\n", ptr->name);
  338. dri_data[0] = 0xff;
  339. dri_data[1] = 0xdd;
  340. dri_data[2] = 0x00;
  341. dri_data[3] = 0x04;
  342. dri_data[4] = (ptr->dri) >> 8;
  343. dri_data[5] = (ptr->dri) & 0xff;
  344. return zr36060_pushit(ptr, ZR060_DRI_IDX, 6, dri_data);
  345. }
  346. /* =========================================================================
  347. Setup function:
  348. Setup compression/decompression of Zoran's JPEG processor
  349. ( see also zoran 36060 manual )
  350. ... sorry for the spaghetti code ...
  351. ========================================================================= */
  352. static void
  353. zr36060_init (struct zr36060 *ptr)
  354. {
  355. int sum = 0;
  356. long bitcnt, tmp;
  357. if (ptr->mode == CODEC_DO_COMPRESSION) {
  358. dprintk(2, "%s: COMPRESSION SETUP\n", ptr->name);
  359. zr36060_write(ptr, ZR060_LOAD, ZR060_LOAD_SyncRst);
  360. /* 060 communicates with 067 in master mode */
  361. zr36060_write(ptr, ZR060_CIR, ZR060_CIR_CodeMstr);
  362. /* Compression with or without variable scale factor */
  363. /*FIXME: What about ptr->bitrate_ctrl? */
  364. zr36060_write(ptr, ZR060_CMR,
  365. ZR060_CMR_Comp | ZR060_CMR_Pass2 |
  366. ZR060_CMR_BRB);
  367. /* Must be zero */
  368. zr36060_write(ptr, ZR060_MBZ, 0x00);
  369. zr36060_write(ptr, ZR060_TCR_HI, 0x00);
  370. zr36060_write(ptr, ZR060_TCR_LO, 0x00);
  371. /* Disable all IRQs - no DataErr means autoreset */
  372. zr36060_write(ptr, ZR060_IMR, 0);
  373. /* volume control settings */
  374. zr36060_write(ptr, ZR060_SF_HI, ptr->scalefact >> 8);
  375. zr36060_write(ptr, ZR060_SF_LO, ptr->scalefact & 0xff);
  376. zr36060_write(ptr, ZR060_AF_HI, 0xff);
  377. zr36060_write(ptr, ZR060_AF_M, 0xff);
  378. zr36060_write(ptr, ZR060_AF_LO, 0xff);
  379. /* setup the variable jpeg tables */
  380. sum += zr36060_set_sof(ptr);
  381. sum += zr36060_set_sos(ptr);
  382. sum += zr36060_set_dri(ptr);
  383. /* setup the fixed jpeg tables - maybe variable, though -
  384. * (see table init section above) */
  385. sum +=
  386. zr36060_pushit(ptr, ZR060_DQT_IDX, sizeof(zr36060_dqt),
  387. zr36060_dqt);
  388. sum +=
  389. zr36060_pushit(ptr, ZR060_DHT_IDX, sizeof(zr36060_dht),
  390. zr36060_dht);
  391. zr36060_write(ptr, ZR060_APP_IDX, 0xff);
  392. zr36060_write(ptr, ZR060_APP_IDX + 1, 0xe0 + ptr->app.appn);
  393. zr36060_write(ptr, ZR060_APP_IDX + 2, 0x00);
  394. zr36060_write(ptr, ZR060_APP_IDX + 3, ptr->app.len + 2);
  395. sum += zr36060_pushit(ptr, ZR060_APP_IDX + 4, 60,
  396. ptr->app.data) + 4;
  397. zr36060_write(ptr, ZR060_COM_IDX, 0xff);
  398. zr36060_write(ptr, ZR060_COM_IDX + 1, 0xfe);
  399. zr36060_write(ptr, ZR060_COM_IDX + 2, 0x00);
  400. zr36060_write(ptr, ZR060_COM_IDX + 3, ptr->com.len + 2);
  401. sum += zr36060_pushit(ptr, ZR060_COM_IDX + 4, 60,
  402. ptr->com.data) + 4;
  403. /* setup misc. data for compression (target code sizes) */
  404. /* size of compressed code to reach without header data */
  405. sum = ptr->real_code_vol - sum;
  406. bitcnt = sum << 3; /* need the size in bits */
  407. tmp = bitcnt >> 16;
  408. dprintk(3,
  409. "%s: code: csize=%d, tot=%d, bit=%ld, highbits=%ld\n",
  410. ptr->name, sum, ptr->real_code_vol, bitcnt, tmp);
  411. zr36060_write(ptr, ZR060_TCV_NET_HI, tmp >> 8);
  412. zr36060_write(ptr, ZR060_TCV_NET_MH, tmp & 0xff);
  413. tmp = bitcnt & 0xffff;
  414. zr36060_write(ptr, ZR060_TCV_NET_ML, tmp >> 8);
  415. zr36060_write(ptr, ZR060_TCV_NET_LO, tmp & 0xff);
  416. bitcnt -= bitcnt >> 7; // bits without stuffing
  417. bitcnt -= ((bitcnt * 5) >> 6); // bits without eob
  418. tmp = bitcnt >> 16;
  419. dprintk(3, "%s: code: nettobit=%ld, highnettobits=%ld\n",
  420. ptr->name, bitcnt, tmp);
  421. zr36060_write(ptr, ZR060_TCV_DATA_HI, tmp >> 8);
  422. zr36060_write(ptr, ZR060_TCV_DATA_MH, tmp & 0xff);
  423. tmp = bitcnt & 0xffff;
  424. zr36060_write(ptr, ZR060_TCV_DATA_ML, tmp >> 8);
  425. zr36060_write(ptr, ZR060_TCV_DATA_LO, tmp & 0xff);
  426. /* JPEG markers to be included in the compressed stream */
  427. zr36060_write(ptr, ZR060_MER,
  428. ZR060_MER_DQT | ZR060_MER_DHT |
  429. ((ptr->com.len > 0) ? ZR060_MER_Com : 0) |
  430. ((ptr->app.len > 0) ? ZR060_MER_App : 0));
  431. /* Setup the Video Frontend */
  432. /* Limit pixel range to 16..235 as per CCIR-601 */
  433. zr36060_write(ptr, ZR060_VCR, ZR060_VCR_Range);
  434. } else {
  435. dprintk(2, "%s: EXPANSION SETUP\n", ptr->name);
  436. zr36060_write(ptr, ZR060_LOAD, ZR060_LOAD_SyncRst);
  437. /* 060 communicates with 067 in master mode */
  438. zr36060_write(ptr, ZR060_CIR, ZR060_CIR_CodeMstr);
  439. /* Decompression */
  440. zr36060_write(ptr, ZR060_CMR, 0);
  441. /* Must be zero */
  442. zr36060_write(ptr, ZR060_MBZ, 0x00);
  443. zr36060_write(ptr, ZR060_TCR_HI, 0x00);
  444. zr36060_write(ptr, ZR060_TCR_LO, 0x00);
  445. /* Disable all IRQs - no DataErr means autoreset */
  446. zr36060_write(ptr, ZR060_IMR, 0);
  447. /* setup misc. data for expansion */
  448. zr36060_write(ptr, ZR060_MER, 0);
  449. /* setup the fixed jpeg tables - maybe variable, though -
  450. * (see table init section above) */
  451. zr36060_pushit(ptr, ZR060_DHT_IDX, sizeof(zr36060_dht),
  452. zr36060_dht);
  453. /* Setup the Video Frontend */
  454. //zr36060_write(ptr, ZR060_VCR, ZR060_VCR_FIExt);
  455. //this doesn't seem right and doesn't work...
  456. zr36060_write(ptr, ZR060_VCR, ZR060_VCR_Range);
  457. }
  458. /* Load the tables */
  459. zr36060_write(ptr, ZR060_LOAD,
  460. ZR060_LOAD_SyncRst | ZR060_LOAD_Load);
  461. zr36060_wait_end(ptr);
  462. dprintk(2, "%s: Status after table preload: 0x%02x\n", ptr->name,
  463. ptr->status);
  464. if (ptr->status & ZR060_CFSR_Busy) {
  465. dprintk(1, KERN_ERR "%s: init aborted!\n", ptr->name);
  466. return; // something is wrong, its timed out!!!!
  467. }
  468. }
  469. /* =========================================================================
  470. CODEC API FUNCTIONS
  471. this functions are accessed by the master via the API structure
  472. ========================================================================= */
  473. /* set compression/expansion mode and launches codec -
  474. this should be the last call from the master before starting processing */
  475. static int
  476. zr36060_set_mode (struct videocodec *codec,
  477. int mode)
  478. {
  479. struct zr36060 *ptr = (struct zr36060 *) codec->data;
  480. dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
  481. if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
  482. return -EINVAL;
  483. ptr->mode = mode;
  484. zr36060_init(ptr);
  485. return 0;
  486. }
  487. /* set picture size (norm is ignored as the codec doesn't know about it) */
  488. static int
  489. zr36060_set_video (struct videocodec *codec,
  490. struct tvnorm *norm,
  491. struct vfe_settings *cap,
  492. struct vfe_polarity *pol)
  493. {
  494. struct zr36060 *ptr = (struct zr36060 *) codec->data;
  495. u32 reg;
  496. int size;
  497. dprintk(2, "%s: set_video %d/%d-%dx%d (%%%d) call\n", ptr->name,
  498. cap->x, cap->y, cap->width, cap->height, cap->decimation);
  499. /* if () return -EINVAL;
  500. * trust the master driver that it knows what it does - so
  501. * we allow invalid startx/y and norm for now ... */
  502. ptr->width = cap->width / (cap->decimation & 0xff);
  503. ptr->height = cap->height / (cap->decimation >> 8);
  504. zr36060_write(ptr, ZR060_LOAD, ZR060_LOAD_SyncRst);
  505. /* Note that VSPol/HSPol bits in zr36060 have the opposite
  506. * meaning of their zr360x7 counterparts with the same names
  507. * N.b. for VSPol this is only true if FIVEdge = 0 (default,
  508. * left unchanged here - in accordance with datasheet).
  509. */
  510. reg = (!pol->vsync_pol ? ZR060_VPR_VSPol : 0)
  511. | (!pol->hsync_pol ? ZR060_VPR_HSPol : 0)
  512. | (pol->field_pol ? ZR060_VPR_FIPol : 0)
  513. | (pol->blank_pol ? ZR060_VPR_BLPol : 0)
  514. | (pol->subimg_pol ? ZR060_VPR_SImgPol : 0)
  515. | (pol->poe_pol ? ZR060_VPR_PoePol : 0)
  516. | (pol->pvalid_pol ? ZR060_VPR_PValPol : 0)
  517. | (pol->vclk_pol ? ZR060_VPR_VCLKPol : 0);
  518. zr36060_write(ptr, ZR060_VPR, reg);
  519. reg = 0;
  520. switch (cap->decimation & 0xff) {
  521. default:
  522. case 1:
  523. break;
  524. case 2:
  525. reg |= ZR060_SR_HScale2;
  526. break;
  527. case 4:
  528. reg |= ZR060_SR_HScale4;
  529. break;
  530. }
  531. switch (cap->decimation >> 8) {
  532. default:
  533. case 1:
  534. break;
  535. case 2:
  536. reg |= ZR060_SR_VScale;
  537. break;
  538. }
  539. zr36060_write(ptr, ZR060_SR, reg);
  540. zr36060_write(ptr, ZR060_BCR_Y, 0x00);
  541. zr36060_write(ptr, ZR060_BCR_U, 0x80);
  542. zr36060_write(ptr, ZR060_BCR_V, 0x80);
  543. /* sync generator */
  544. reg = norm->Ht - 1; /* Vtotal */
  545. zr36060_write(ptr, ZR060_SGR_VTOTAL_HI, (reg >> 8) & 0xff);
  546. zr36060_write(ptr, ZR060_SGR_VTOTAL_LO, (reg >> 0) & 0xff);
  547. reg = norm->Wt - 1; /* Htotal */
  548. zr36060_write(ptr, ZR060_SGR_HTOTAL_HI, (reg >> 8) & 0xff);
  549. zr36060_write(ptr, ZR060_SGR_HTOTAL_LO, (reg >> 0) & 0xff);
  550. reg = 6 - 1; /* VsyncSize */
  551. zr36060_write(ptr, ZR060_SGR_VSYNC, reg);
  552. //reg = 30 - 1; /* HsyncSize */
  553. ///*CP*/ reg = (zr->params.norm == 1 ? 57 : 68);
  554. reg = 68;
  555. zr36060_write(ptr, ZR060_SGR_HSYNC, reg);
  556. reg = norm->VStart - 1; /* BVstart */
  557. zr36060_write(ptr, ZR060_SGR_BVSTART, reg);
  558. reg += norm->Ha / 2; /* BVend */
  559. zr36060_write(ptr, ZR060_SGR_BVEND_HI, (reg >> 8) & 0xff);
  560. zr36060_write(ptr, ZR060_SGR_BVEND_LO, (reg >> 0) & 0xff);
  561. reg = norm->HStart - 1; /* BHstart */
  562. zr36060_write(ptr, ZR060_SGR_BHSTART, reg);
  563. reg += norm->Wa; /* BHend */
  564. zr36060_write(ptr, ZR060_SGR_BHEND_HI, (reg >> 8) & 0xff);
  565. zr36060_write(ptr, ZR060_SGR_BHEND_LO, (reg >> 0) & 0xff);
  566. /* active area */
  567. reg = cap->y + norm->VStart; /* Vstart */
  568. zr36060_write(ptr, ZR060_AAR_VSTART_HI, (reg >> 8) & 0xff);
  569. zr36060_write(ptr, ZR060_AAR_VSTART_LO, (reg >> 0) & 0xff);
  570. reg += cap->height; /* Vend */
  571. zr36060_write(ptr, ZR060_AAR_VEND_HI, (reg >> 8) & 0xff);
  572. zr36060_write(ptr, ZR060_AAR_VEND_LO, (reg >> 0) & 0xff);
  573. reg = cap->x + norm->HStart; /* Hstart */
  574. zr36060_write(ptr, ZR060_AAR_HSTART_HI, (reg >> 8) & 0xff);
  575. zr36060_write(ptr, ZR060_AAR_HSTART_LO, (reg >> 0) & 0xff);
  576. reg += cap->width; /* Hend */
  577. zr36060_write(ptr, ZR060_AAR_HEND_HI, (reg >> 8) & 0xff);
  578. zr36060_write(ptr, ZR060_AAR_HEND_LO, (reg >> 0) & 0xff);
  579. /* subimage area */
  580. reg = norm->VStart - 4; /* SVstart */
  581. zr36060_write(ptr, ZR060_SWR_VSTART_HI, (reg >> 8) & 0xff);
  582. zr36060_write(ptr, ZR060_SWR_VSTART_LO, (reg >> 0) & 0xff);
  583. reg += norm->Ha / 2 + 8; /* SVend */
  584. zr36060_write(ptr, ZR060_SWR_VEND_HI, (reg >> 8) & 0xff);
  585. zr36060_write(ptr, ZR060_SWR_VEND_LO, (reg >> 0) & 0xff);
  586. reg = norm->HStart /*+ 64 */ - 4; /* SHstart */
  587. zr36060_write(ptr, ZR060_SWR_HSTART_HI, (reg >> 8) & 0xff);
  588. zr36060_write(ptr, ZR060_SWR_HSTART_LO, (reg >> 0) & 0xff);
  589. reg += norm->Wa + 8; /* SHend */
  590. zr36060_write(ptr, ZR060_SWR_HEND_HI, (reg >> 8) & 0xff);
  591. zr36060_write(ptr, ZR060_SWR_HEND_LO, (reg >> 0) & 0xff);
  592. size = ptr->width * ptr->height;
  593. /* Target compressed field size in bits: */
  594. size = size * 16; /* uncompressed size in bits */
  595. /* (Ronald) by default, quality = 100 is a compression
  596. * ratio 1:2. Setting low_bitrate (insmod option) sets
  597. * it to 1:4 (instead of 1:2, zr36060 max) as limit because the
  598. * buz can't handle more at decimation=1... Use low_bitrate if
  599. * you have a Buz, unless you know what you're doing */
  600. size = size * cap->quality / (low_bitrate ? 400 : 200);
  601. /* Lower limit (arbitrary, 1 KB) */
  602. if (size < 8192)
  603. size = 8192;
  604. /* Upper limit: 7/8 of the code buffers */
  605. if (size > ptr->total_code_vol * 7)
  606. size = ptr->total_code_vol * 7;
  607. ptr->real_code_vol = size >> 3; /* in bytes */
  608. /* the MBCVR is the *maximum* block volume, according to the
  609. * JPEG ISO specs, this shouldn't be used, since that allows
  610. * for the best encoding quality. So set it to it's max value */
  611. reg = ptr->max_block_vol;
  612. zr36060_write(ptr, ZR060_MBCVR, reg);
  613. return 0;
  614. }
  615. /* additional control functions */
  616. static int
  617. zr36060_control (struct videocodec *codec,
  618. int type,
  619. int size,
  620. void *data)
  621. {
  622. struct zr36060 *ptr = (struct zr36060 *) codec->data;
  623. int *ival = (int *) data;
  624. dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
  625. size);
  626. switch (type) {
  627. case CODEC_G_STATUS: /* get last status */
  628. if (size != sizeof(int))
  629. return -EFAULT;
  630. zr36060_read_status(ptr);
  631. *ival = ptr->status;
  632. break;
  633. case CODEC_G_CODEC_MODE:
  634. if (size != sizeof(int))
  635. return -EFAULT;
  636. *ival = CODEC_MODE_BJPG;
  637. break;
  638. case CODEC_S_CODEC_MODE:
  639. if (size != sizeof(int))
  640. return -EFAULT;
  641. if (*ival != CODEC_MODE_BJPG)
  642. return -EINVAL;
  643. /* not needed, do nothing */
  644. return 0;
  645. case CODEC_G_VFE:
  646. case CODEC_S_VFE:
  647. /* not needed, do nothing */
  648. return 0;
  649. case CODEC_S_MMAP:
  650. /* not available, give an error */
  651. return -ENXIO;
  652. case CODEC_G_JPEG_TDS_BYTE: /* get target volume in byte */
  653. if (size != sizeof(int))
  654. return -EFAULT;
  655. *ival = ptr->total_code_vol;
  656. break;
  657. case CODEC_S_JPEG_TDS_BYTE: /* get target volume in byte */
  658. if (size != sizeof(int))
  659. return -EFAULT;
  660. ptr->total_code_vol = *ival;
  661. ptr->real_code_vol = (ptr->total_code_vol * 6) >> 3;
  662. break;
  663. case CODEC_G_JPEG_SCALE: /* get scaling factor */
  664. if (size != sizeof(int))
  665. return -EFAULT;
  666. *ival = zr36060_read_scalefactor(ptr);
  667. break;
  668. case CODEC_S_JPEG_SCALE: /* set scaling factor */
  669. if (size != sizeof(int))
  670. return -EFAULT;
  671. ptr->scalefact = *ival;
  672. break;
  673. case CODEC_G_JPEG_APP_DATA: { /* get appn marker data */
  674. struct jpeg_app_marker *app = data;
  675. if (size != sizeof(struct jpeg_app_marker))
  676. return -EFAULT;
  677. *app = ptr->app;
  678. break;
  679. }
  680. case CODEC_S_JPEG_APP_DATA: { /* set appn marker data */
  681. struct jpeg_app_marker *app = data;
  682. if (size != sizeof(struct jpeg_app_marker))
  683. return -EFAULT;
  684. ptr->app = *app;
  685. break;
  686. }
  687. case CODEC_G_JPEG_COM_DATA: { /* get comment marker data */
  688. struct jpeg_com_marker *com = data;
  689. if (size != sizeof(struct jpeg_com_marker))
  690. return -EFAULT;
  691. *com = ptr->com;
  692. break;
  693. }
  694. case CODEC_S_JPEG_COM_DATA: { /* set comment marker data */
  695. struct jpeg_com_marker *com = data;
  696. if (size != sizeof(struct jpeg_com_marker))
  697. return -EFAULT;
  698. ptr->com = *com;
  699. break;
  700. }
  701. default:
  702. return -EINVAL;
  703. }
  704. return size;
  705. }
  706. /* =========================================================================
  707. Exit and unregister function:
  708. Deinitializes Zoran's JPEG processor
  709. ========================================================================= */
  710. static int
  711. zr36060_unset (struct videocodec *codec)
  712. {
  713. struct zr36060 *ptr = codec->data;
  714. if (ptr) {
  715. /* do wee need some codec deinit here, too ???? */
  716. dprintk(1, "%s: finished codec #%d\n", ptr->name,
  717. ptr->num);
  718. kfree(ptr);
  719. codec->data = NULL;
  720. zr36060_codecs--;
  721. return 0;
  722. }
  723. return -EFAULT;
  724. }
  725. /* =========================================================================
  726. Setup and registry function:
  727. Initializes Zoran's JPEG processor
  728. Also sets pixel size, average code size, mode (compr./decompr.)
  729. (the given size is determined by the processor with the video interface)
  730. ========================================================================= */
  731. static int
  732. zr36060_setup (struct videocodec *codec)
  733. {
  734. struct zr36060 *ptr;
  735. int res;
  736. dprintk(2, "zr36060: initializing MJPEG subsystem #%d.\n",
  737. zr36060_codecs);
  738. if (zr36060_codecs == MAX_CODECS) {
  739. dprintk(1,
  740. KERN_ERR "zr36060: Can't attach more codecs!\n");
  741. return -ENOSPC;
  742. }
  743. //mem structure init
  744. codec->data = ptr = kzalloc(sizeof(struct zr36060), GFP_KERNEL);
  745. if (NULL == ptr) {
  746. dprintk(1, KERN_ERR "zr36060: Can't get enough memory!\n");
  747. return -ENOMEM;
  748. }
  749. snprintf(ptr->name, sizeof(ptr->name), "zr36060[%d]",
  750. zr36060_codecs);
  751. ptr->num = zr36060_codecs++;
  752. ptr->codec = codec;
  753. //testing
  754. res = zr36060_basic_test(ptr);
  755. if (res < 0) {
  756. zr36060_unset(codec);
  757. return res;
  758. }
  759. //final setup
  760. memcpy(ptr->h_samp_ratio, zr36060_decimation_h, 8);
  761. memcpy(ptr->v_samp_ratio, zr36060_decimation_v, 8);
  762. ptr->bitrate_ctrl = 0; /* 0 or 1 - fixed file size flag
  763. * (what is the difference?) */
  764. ptr->mode = CODEC_DO_COMPRESSION;
  765. ptr->width = 384;
  766. ptr->height = 288;
  767. ptr->total_code_vol = 16000; /* CHECKME */
  768. ptr->real_code_vol = (ptr->total_code_vol * 6) >> 3;
  769. ptr->max_block_vol = 240; /* CHECKME, was 120 is 240 */
  770. ptr->scalefact = 0x100;
  771. ptr->dri = 1; /* CHECKME, was 8 is 1 */
  772. /* by default, no COM or APP markers - app should set those */
  773. ptr->com.len = 0;
  774. ptr->app.appn = 0;
  775. ptr->app.len = 0;
  776. zr36060_init(ptr);
  777. dprintk(1, KERN_INFO "%s: codec attached and running\n",
  778. ptr->name);
  779. return 0;
  780. }
  781. static const struct videocodec zr36060_codec = {
  782. .owner = THIS_MODULE,
  783. .name = "zr36060",
  784. .magic = 0L, // magic not used
  785. .flags =
  786. CODEC_FLAG_JPEG | CODEC_FLAG_HARDWARE | CODEC_FLAG_ENCODER |
  787. CODEC_FLAG_DECODER | CODEC_FLAG_VFE,
  788. .type = CODEC_TYPE_ZR36060,
  789. .setup = zr36060_setup, // functionality
  790. .unset = zr36060_unset,
  791. .set_mode = zr36060_set_mode,
  792. .set_video = zr36060_set_video,
  793. .control = zr36060_control,
  794. // others are not used
  795. };
  796. /* =========================================================================
  797. HOOK IN DRIVER AS KERNEL MODULE
  798. ========================================================================= */
  799. static int __init
  800. zr36060_init_module (void)
  801. {
  802. //dprintk(1, "zr36060 driver %s\n",ZR060_VERSION);
  803. zr36060_codecs = 0;
  804. return videocodec_register(&zr36060_codec);
  805. }
  806. static void __exit
  807. zr36060_cleanup_module (void)
  808. {
  809. if (zr36060_codecs) {
  810. dprintk(1,
  811. "zr36060: something's wrong - %d codecs left somehow.\n",
  812. zr36060_codecs);
  813. }
  814. /* however, we can't just stay alive */
  815. videocodec_unregister(&zr36060_codec);
  816. }
  817. module_init(zr36060_init_module);
  818. module_exit(zr36060_cleanup_module);
  819. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@skynet.be>");
  820. MODULE_DESCRIPTION("Driver module for ZR36060 jpeg processors "
  821. ZR060_VERSION);
  822. MODULE_LICENSE("GPL");