zr36050.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Zoran ZR36050 basic configuration functions
  3. *
  4. * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
  5. *
  6. * $Id: zr36050.c,v 1.1.2.11 2003/08/03 14:54:53 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 ZR050_VERSION "v0.7.1"
  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 "zr36050.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 zr36050_codecs;
  40. /* debugging is available via module parameter */
  41. static int debug;
  42. module_param(debug, int, 0);
  43. MODULE_PARM_DESC(debug, "Debug level (0-4)");
  44. #define dprintk(num, format, args...) \
  45. do { \
  46. if (debug >= num) \
  47. printk(format, ##args); \
  48. } while (0)
  49. /* =========================================================================
  50. Local hardware I/O functions:
  51. read/write via codec layer (registers are located in the master device)
  52. ========================================================================= */
  53. /* read and write functions */
  54. static u8
  55. zr36050_read (struct zr36050 *ptr,
  56. u16 reg)
  57. {
  58. u8 value = 0;
  59. // just in case something is wrong...
  60. if (ptr->codec->master_data->readreg)
  61. value = (ptr->codec->master_data->readreg(ptr->codec,
  62. reg)) & 0xFF;
  63. else
  64. dprintk(1,
  65. KERN_ERR "%s: invalid I/O setup, nothing read!\n",
  66. ptr->name);
  67. dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg,
  68. value);
  69. return value;
  70. }
  71. static void
  72. zr36050_write (struct zr36050 *ptr,
  73. u16 reg,
  74. u8 value)
  75. {
  76. dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value,
  77. reg);
  78. // just in case something is wrong...
  79. if (ptr->codec->master_data->writereg)
  80. ptr->codec->master_data->writereg(ptr->codec, reg, value);
  81. else
  82. dprintk(1,
  83. KERN_ERR
  84. "%s: invalid I/O setup, nothing written!\n",
  85. ptr->name);
  86. }
  87. /* =========================================================================
  88. Local helper function:
  89. status read
  90. ========================================================================= */
  91. /* status is kept in datastructure */
  92. static u8
  93. zr36050_read_status1 (struct zr36050 *ptr)
  94. {
  95. ptr->status1 = zr36050_read(ptr, ZR050_STATUS_1);
  96. zr36050_read(ptr, 0);
  97. return ptr->status1;
  98. }
  99. /* =========================================================================
  100. Local helper function:
  101. scale factor read
  102. ========================================================================= */
  103. /* scale factor is kept in datastructure */
  104. static u16
  105. zr36050_read_scalefactor (struct zr36050 *ptr)
  106. {
  107. ptr->scalefact = (zr36050_read(ptr, ZR050_SF_HI) << 8) |
  108. (zr36050_read(ptr, ZR050_SF_LO) & 0xFF);
  109. /* leave 0 selected for an eventually GO from master */
  110. zr36050_read(ptr, 0);
  111. return ptr->scalefact;
  112. }
  113. /* =========================================================================
  114. Local helper function:
  115. wait if codec is ready to proceed (end of processing) or time is over
  116. ========================================================================= */
  117. static void
  118. zr36050_wait_end (struct zr36050 *ptr)
  119. {
  120. int i = 0;
  121. while (!(zr36050_read_status1(ptr) & 0x4)) {
  122. udelay(1);
  123. if (i++ > 200000) { // 200ms, there is for sure something wrong!!!
  124. dprintk(1,
  125. "%s: timeout at wait_end (last status: 0x%02x)\n",
  126. ptr->name, ptr->status1);
  127. break;
  128. }
  129. }
  130. }
  131. /* =========================================================================
  132. Local helper function:
  133. basic test of "connectivity", writes/reads to/from memory the SOF marker
  134. ========================================================================= */
  135. static int
  136. zr36050_basic_test (struct zr36050 *ptr)
  137. {
  138. zr36050_write(ptr, ZR050_SOF_IDX, 0x00);
  139. zr36050_write(ptr, ZR050_SOF_IDX + 1, 0x00);
  140. if ((zr36050_read(ptr, ZR050_SOF_IDX) |
  141. zr36050_read(ptr, ZR050_SOF_IDX + 1)) != 0x0000) {
  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. zr36050_write(ptr, ZR050_SOF_IDX, 0xff);
  149. zr36050_write(ptr, ZR050_SOF_IDX + 1, 0xc0);
  150. if (((zr36050_read(ptr, ZR050_SOF_IDX) << 8) |
  151. zr36050_read(ptr, ZR050_SOF_IDX + 1)) != 0xffc0) {
  152. dprintk(1,
  153. KERN_ERR
  154. "%s: attach failed, can't connect to jpeg processor!\n",
  155. ptr->name);
  156. return -ENXIO;
  157. }
  158. zr36050_wait_end(ptr);
  159. if ((ptr->status1 & 0x4) == 0) {
  160. dprintk(1,
  161. KERN_ERR
  162. "%s: attach failed, jpeg processor failed (end flag)!\n",
  163. ptr->name);
  164. return -EBUSY;
  165. }
  166. return 0; /* looks good! */
  167. }
  168. /* =========================================================================
  169. Local helper function:
  170. simple loop for pushing the init datasets
  171. ========================================================================= */
  172. static int
  173. zr36050_pushit (struct zr36050 *ptr,
  174. u16 startreg,
  175. u16 len,
  176. const char *data)
  177. {
  178. int i = 0;
  179. dprintk(4, "%s: write data block to 0x%04x (len=%d)\n", ptr->name,
  180. startreg, len);
  181. while (i < len) {
  182. zr36050_write(ptr, startreg++, data[i++]);
  183. }
  184. return i;
  185. }
  186. /* =========================================================================
  187. Basic datasets:
  188. jpeg baseline setup data (you find it on lots places in internet, or just
  189. extract it from any regular .jpg image...)
  190. Could be variable, but until it's not needed it they are just fixed to save
  191. memory. Otherwise expand zr36050 structure with arrays, push the values to
  192. it and initialize from there, as e.g. the linux zr36057/60 driver does it.
  193. ========================================================================= */
  194. static const char zr36050_dqt[0x86] = {
  195. 0xff, 0xdb, //Marker: DQT
  196. 0x00, 0x84, //Length: 2*65+2
  197. 0x00, //Pq,Tq first table
  198. 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
  199. 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
  200. 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
  201. 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
  202. 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
  203. 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
  204. 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
  205. 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
  206. 0x01, //Pq,Tq second table
  207. 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
  208. 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
  209. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  210. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  211. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  212. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  213. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  214. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
  215. };
  216. static const char zr36050_dht[0x1a4] = {
  217. 0xff, 0xc4, //Marker: DHT
  218. 0x01, 0xa2, //Length: 2*AC, 2*DC
  219. 0x00, //DC first table
  220. 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
  221. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  222. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  223. 0x01, //DC second table
  224. 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  225. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  226. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  227. 0x10, //AC first table
  228. 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
  229. 0x05, 0x05, 0x04, 0x04, 0x00, 0x00,
  230. 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11,
  231. 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
  232. 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
  233. 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24,
  234. 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17,
  235. 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34,
  236. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  237. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
  238. 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
  239. 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  240. 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
  241. 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
  242. 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
  243. 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9,
  244. 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
  245. 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
  246. 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
  247. 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  248. 0xF8, 0xF9, 0xFA,
  249. 0x11, //AC second table
  250. 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
  251. 0x07, 0x05, 0x04, 0x04, 0x00, 0x01,
  252. 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
  253. 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  254. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  255. 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62,
  256. 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25,
  257. 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,
  258. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  259. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
  260. 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
  261. 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  262. 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  263. 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  264. 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  265. 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
  266. 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  267. 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
  268. 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
  269. 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  270. 0xF9, 0xFA
  271. };
  272. /* jpeg baseline setup, this is just fixed in this driver (YUV pictures) */
  273. #define NO_OF_COMPONENTS 0x3 //Y,U,V
  274. #define BASELINE_PRECISION 0x8 //MCU size (?)
  275. static const char zr36050_tq[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's QT
  276. static const char zr36050_td[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's DC
  277. static const char zr36050_ta[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's AC
  278. /* horizontal 422 decimation setup (maybe we support 411 or so later, too) */
  279. static const char zr36050_decimation_h[8] = { 2, 1, 1, 0, 0, 0, 0, 0 };
  280. static const char zr36050_decimation_v[8] = { 1, 1, 1, 0, 0, 0, 0, 0 };
  281. /* =========================================================================
  282. Local helper functions:
  283. calculation and setup of parameter-dependent JPEG baseline segments
  284. (needed for compression only)
  285. ========================================================================= */
  286. /* ------------------------------------------------------------------------- */
  287. /* SOF (start of frame) segment depends on width, height and sampling ratio
  288. of each color component */
  289. static int
  290. zr36050_set_sof (struct zr36050 *ptr)
  291. {
  292. char sof_data[34]; // max. size of register set
  293. int i;
  294. dprintk(3, "%s: write SOF (%dx%d, %d components)\n", ptr->name,
  295. ptr->width, ptr->height, NO_OF_COMPONENTS);
  296. sof_data[0] = 0xff;
  297. sof_data[1] = 0xc0;
  298. sof_data[2] = 0x00;
  299. sof_data[3] = (3 * NO_OF_COMPONENTS) + 8;
  300. sof_data[4] = BASELINE_PRECISION; // only '8' possible with zr36050
  301. sof_data[5] = (ptr->height) >> 8;
  302. sof_data[6] = (ptr->height) & 0xff;
  303. sof_data[7] = (ptr->width) >> 8;
  304. sof_data[8] = (ptr->width) & 0xff;
  305. sof_data[9] = NO_OF_COMPONENTS;
  306. for (i = 0; i < NO_OF_COMPONENTS; i++) {
  307. sof_data[10 + (i * 3)] = i; // index identifier
  308. sof_data[11 + (i * 3)] = (ptr->h_samp_ratio[i] << 4) | (ptr->v_samp_ratio[i]); // sampling ratios
  309. sof_data[12 + (i * 3)] = zr36050_tq[i]; // Q table selection
  310. }
  311. return zr36050_pushit(ptr, ZR050_SOF_IDX,
  312. (3 * NO_OF_COMPONENTS) + 10, sof_data);
  313. }
  314. /* ------------------------------------------------------------------------- */
  315. /* SOS (start of scan) segment depends on the used scan components
  316. of each color component */
  317. static int
  318. zr36050_set_sos (struct zr36050 *ptr)
  319. {
  320. char sos_data[16]; // max. size of register set
  321. int i;
  322. dprintk(3, "%s: write SOS\n", ptr->name);
  323. sos_data[0] = 0xff;
  324. sos_data[1] = 0xda;
  325. sos_data[2] = 0x00;
  326. sos_data[3] = 2 + 1 + (2 * NO_OF_COMPONENTS) + 3;
  327. sos_data[4] = NO_OF_COMPONENTS;
  328. for (i = 0; i < NO_OF_COMPONENTS; i++) {
  329. sos_data[5 + (i * 2)] = i; // index
  330. sos_data[6 + (i * 2)] = (zr36050_td[i] << 4) | zr36050_ta[i]; // AC/DC tbl.sel.
  331. }
  332. sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 2] = 00; // scan start
  333. sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 3] = 0x3F;
  334. sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 4] = 00;
  335. return zr36050_pushit(ptr, ZR050_SOS1_IDX,
  336. 4 + 1 + (2 * NO_OF_COMPONENTS) + 3,
  337. sos_data);
  338. }
  339. /* ------------------------------------------------------------------------- */
  340. /* DRI (define restart interval) */
  341. static int
  342. zr36050_set_dri (struct zr36050 *ptr)
  343. {
  344. char dri_data[6]; // max. size of register set
  345. dprintk(3, "%s: write DRI\n", ptr->name);
  346. dri_data[0] = 0xff;
  347. dri_data[1] = 0xdd;
  348. dri_data[2] = 0x00;
  349. dri_data[3] = 0x04;
  350. dri_data[4] = ptr->dri >> 8;
  351. dri_data[5] = ptr->dri & 0xff;
  352. return zr36050_pushit(ptr, ZR050_DRI_IDX, 6, dri_data);
  353. }
  354. /* =========================================================================
  355. Setup function:
  356. Setup compression/decompression of Zoran's JPEG processor
  357. ( see also zoran 36050 manual )
  358. ... sorry for the spaghetti code ...
  359. ========================================================================= */
  360. static void
  361. zr36050_init (struct zr36050 *ptr)
  362. {
  363. int sum = 0;
  364. long bitcnt, tmp;
  365. if (ptr->mode == CODEC_DO_COMPRESSION) {
  366. dprintk(2, "%s: COMPRESSION SETUP\n", ptr->name);
  367. /* 050 communicates with 057 in master mode */
  368. zr36050_write(ptr, ZR050_HARDWARE, ZR050_HW_MSTR);
  369. /* encoding table preload for compression */
  370. zr36050_write(ptr, ZR050_MODE,
  371. ZR050_MO_COMP | ZR050_MO_TLM);
  372. zr36050_write(ptr, ZR050_OPTIONS, 0);
  373. /* disable all IRQs */
  374. zr36050_write(ptr, ZR050_INT_REQ_0, 0);
  375. zr36050_write(ptr, ZR050_INT_REQ_1, 3); // low 2 bits always 1
  376. /* volume control settings */
  377. /*zr36050_write(ptr, ZR050_MBCV, ptr->max_block_vol);*/
  378. zr36050_write(ptr, ZR050_SF_HI, ptr->scalefact >> 8);
  379. zr36050_write(ptr, ZR050_SF_LO, ptr->scalefact & 0xff);
  380. zr36050_write(ptr, ZR050_AF_HI, 0xff);
  381. zr36050_write(ptr, ZR050_AF_M, 0xff);
  382. zr36050_write(ptr, ZR050_AF_LO, 0xff);
  383. /* setup the variable jpeg tables */
  384. sum += zr36050_set_sof(ptr);
  385. sum += zr36050_set_sos(ptr);
  386. sum += zr36050_set_dri(ptr);
  387. /* setup the fixed jpeg tables - maybe variable, though -
  388. * (see table init section above) */
  389. dprintk(3, "%s: write DQT, DHT, APP\n", ptr->name);
  390. sum += zr36050_pushit(ptr, ZR050_DQT_IDX,
  391. sizeof(zr36050_dqt), zr36050_dqt);
  392. sum += zr36050_pushit(ptr, ZR050_DHT_IDX,
  393. sizeof(zr36050_dht), zr36050_dht);
  394. zr36050_write(ptr, ZR050_APP_IDX, 0xff);
  395. zr36050_write(ptr, ZR050_APP_IDX + 1, 0xe0 + ptr->app.appn);
  396. zr36050_write(ptr, ZR050_APP_IDX + 2, 0x00);
  397. zr36050_write(ptr, ZR050_APP_IDX + 3, ptr->app.len + 2);
  398. sum += zr36050_pushit(ptr, ZR050_APP_IDX + 4, 60,
  399. ptr->app.data) + 4;
  400. zr36050_write(ptr, ZR050_COM_IDX, 0xff);
  401. zr36050_write(ptr, ZR050_COM_IDX + 1, 0xfe);
  402. zr36050_write(ptr, ZR050_COM_IDX + 2, 0x00);
  403. zr36050_write(ptr, ZR050_COM_IDX + 3, ptr->com.len + 2);
  404. sum += zr36050_pushit(ptr, ZR050_COM_IDX + 4, 60,
  405. ptr->com.data) + 4;
  406. /* do the internal huffman table preload */
  407. zr36050_write(ptr, ZR050_MARKERS_EN, ZR050_ME_DHTI);
  408. zr36050_write(ptr, ZR050_GO, 1); // launch codec
  409. zr36050_wait_end(ptr);
  410. dprintk(2, "%s: Status after table preload: 0x%02x\n",
  411. ptr->name, ptr->status1);
  412. if ((ptr->status1 & 0x4) == 0) {
  413. dprintk(1, KERN_ERR "%s: init aborted!\n",
  414. ptr->name);
  415. return; // something is wrong, its timed out!!!!
  416. }
  417. /* setup misc. data for compression (target code sizes) */
  418. /* size of compressed code to reach without header data */
  419. sum = ptr->real_code_vol - sum;
  420. bitcnt = sum << 3; /* need the size in bits */
  421. tmp = bitcnt >> 16;
  422. dprintk(3,
  423. "%s: code: csize=%d, tot=%d, bit=%ld, highbits=%ld\n",
  424. ptr->name, sum, ptr->real_code_vol, bitcnt, tmp);
  425. zr36050_write(ptr, ZR050_TCV_NET_HI, tmp >> 8);
  426. zr36050_write(ptr, ZR050_TCV_NET_MH, tmp & 0xff);
  427. tmp = bitcnt & 0xffff;
  428. zr36050_write(ptr, ZR050_TCV_NET_ML, tmp >> 8);
  429. zr36050_write(ptr, ZR050_TCV_NET_LO, tmp & 0xff);
  430. bitcnt -= bitcnt >> 7; // bits without stuffing
  431. bitcnt -= ((bitcnt * 5) >> 6); // bits without eob
  432. tmp = bitcnt >> 16;
  433. dprintk(3, "%s: code: nettobit=%ld, highnettobits=%ld\n",
  434. ptr->name, bitcnt, tmp);
  435. zr36050_write(ptr, ZR050_TCV_DATA_HI, tmp >> 8);
  436. zr36050_write(ptr, ZR050_TCV_DATA_MH, tmp & 0xff);
  437. tmp = bitcnt & 0xffff;
  438. zr36050_write(ptr, ZR050_TCV_DATA_ML, tmp >> 8);
  439. zr36050_write(ptr, ZR050_TCV_DATA_LO, tmp & 0xff);
  440. /* compression setup with or without bitrate control */
  441. zr36050_write(ptr, ZR050_MODE,
  442. ZR050_MO_COMP | ZR050_MO_PASS2 |
  443. (ptr->bitrate_ctrl ? ZR050_MO_BRC : 0));
  444. /* this headers seem to deliver "valid AVI" jpeg frames */
  445. zr36050_write(ptr, ZR050_MARKERS_EN,
  446. ZR050_ME_DQT | ZR050_ME_DHT |
  447. ((ptr->app.len > 0) ? ZR050_ME_APP : 0) |
  448. ((ptr->com.len > 0) ? ZR050_ME_COM : 0));
  449. } else {
  450. dprintk(2, "%s: EXPANSION SETUP\n", ptr->name);
  451. /* 050 communicates with 055 in master mode */
  452. zr36050_write(ptr, ZR050_HARDWARE,
  453. ZR050_HW_MSTR | ZR050_HW_CFIS_2_CLK);
  454. /* encoding table preload */
  455. zr36050_write(ptr, ZR050_MODE, ZR050_MO_TLM);
  456. /* disable all IRQs */
  457. zr36050_write(ptr, ZR050_INT_REQ_0, 0);
  458. zr36050_write(ptr, ZR050_INT_REQ_1, 3); // low 2 bits always 1
  459. dprintk(3, "%s: write DHT\n", ptr->name);
  460. zr36050_pushit(ptr, ZR050_DHT_IDX, sizeof(zr36050_dht),
  461. zr36050_dht);
  462. /* do the internal huffman table preload */
  463. zr36050_write(ptr, ZR050_MARKERS_EN, ZR050_ME_DHTI);
  464. zr36050_write(ptr, ZR050_GO, 1); // launch codec
  465. zr36050_wait_end(ptr);
  466. dprintk(2, "%s: Status after table preload: 0x%02x\n",
  467. ptr->name, ptr->status1);
  468. if ((ptr->status1 & 0x4) == 0) {
  469. dprintk(1, KERN_ERR "%s: init aborted!\n",
  470. ptr->name);
  471. return; // something is wrong, its timed out!!!!
  472. }
  473. /* setup misc. data for expansion */
  474. zr36050_write(ptr, ZR050_MODE, 0);
  475. zr36050_write(ptr, ZR050_MARKERS_EN, 0);
  476. }
  477. /* adr on selected, to allow GO from master */
  478. zr36050_read(ptr, 0);
  479. }
  480. /* =========================================================================
  481. CODEC API FUNCTIONS
  482. this functions are accessed by the master via the API structure
  483. ========================================================================= */
  484. /* set compression/expansion mode and launches codec -
  485. this should be the last call from the master before starting processing */
  486. static int
  487. zr36050_set_mode (struct videocodec *codec,
  488. int mode)
  489. {
  490. struct zr36050 *ptr = (struct zr36050 *) codec->data;
  491. dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
  492. if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
  493. return -EINVAL;
  494. ptr->mode = mode;
  495. zr36050_init(ptr);
  496. return 0;
  497. }
  498. /* set picture size (norm is ignored as the codec doesn't know about it) */
  499. static int
  500. zr36050_set_video (struct videocodec *codec,
  501. struct tvnorm *norm,
  502. struct vfe_settings *cap,
  503. struct vfe_polarity *pol)
  504. {
  505. struct zr36050 *ptr = (struct zr36050 *) codec->data;
  506. int size;
  507. dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) q%d call\n",
  508. ptr->name, norm->HStart, norm->VStart,
  509. cap->x, cap->y, cap->width, cap->height,
  510. cap->decimation, cap->quality);
  511. /* if () return -EINVAL;
  512. * trust the master driver that it knows what it does - so
  513. * we allow invalid startx/y and norm for now ... */
  514. ptr->width = cap->width / (cap->decimation & 0xff);
  515. ptr->height = cap->height / ((cap->decimation >> 8) & 0xff);
  516. /* (KM) JPEG quality */
  517. size = ptr->width * ptr->height;
  518. size *= 16; /* size in bits */
  519. /* apply quality setting */
  520. size = size * cap->quality / 200;
  521. /* Minimum: 1kb */
  522. if (size < 8192)
  523. size = 8192;
  524. /* Maximum: 7/8 of code buffer */
  525. if (size > ptr->total_code_vol * 7)
  526. size = ptr->total_code_vol * 7;
  527. ptr->real_code_vol = size >> 3; /* in bytes */
  528. /* Set max_block_vol here (previously in zr36050_init, moved
  529. * here for consistency with zr36060 code */
  530. zr36050_write(ptr, ZR050_MBCV, ptr->max_block_vol);
  531. return 0;
  532. }
  533. /* additional control functions */
  534. static int
  535. zr36050_control (struct videocodec *codec,
  536. int type,
  537. int size,
  538. void *data)
  539. {
  540. struct zr36050 *ptr = (struct zr36050 *) codec->data;
  541. int *ival = (int *) data;
  542. dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
  543. size);
  544. switch (type) {
  545. case CODEC_G_STATUS: /* get last status */
  546. if (size != sizeof(int))
  547. return -EFAULT;
  548. zr36050_read_status1(ptr);
  549. *ival = ptr->status1;
  550. break;
  551. case CODEC_G_CODEC_MODE:
  552. if (size != sizeof(int))
  553. return -EFAULT;
  554. *ival = CODEC_MODE_BJPG;
  555. break;
  556. case CODEC_S_CODEC_MODE:
  557. if (size != sizeof(int))
  558. return -EFAULT;
  559. if (*ival != CODEC_MODE_BJPG)
  560. return -EINVAL;
  561. /* not needed, do nothing */
  562. return 0;
  563. case CODEC_G_VFE:
  564. case CODEC_S_VFE:
  565. /* not needed, do nothing */
  566. return 0;
  567. case CODEC_S_MMAP:
  568. /* not available, give an error */
  569. return -ENXIO;
  570. case CODEC_G_JPEG_TDS_BYTE: /* get target volume in byte */
  571. if (size != sizeof(int))
  572. return -EFAULT;
  573. *ival = ptr->total_code_vol;
  574. break;
  575. case CODEC_S_JPEG_TDS_BYTE: /* get target volume in byte */
  576. if (size != sizeof(int))
  577. return -EFAULT;
  578. ptr->total_code_vol = *ival;
  579. /* (Kieran Morrissey)
  580. * code copied from zr36060.c to ensure proper bitrate */
  581. ptr->real_code_vol = (ptr->total_code_vol * 6) >> 3;
  582. break;
  583. case CODEC_G_JPEG_SCALE: /* get scaling factor */
  584. if (size != sizeof(int))
  585. return -EFAULT;
  586. *ival = zr36050_read_scalefactor(ptr);
  587. break;
  588. case CODEC_S_JPEG_SCALE: /* set scaling factor */
  589. if (size != sizeof(int))
  590. return -EFAULT;
  591. ptr->scalefact = *ival;
  592. break;
  593. case CODEC_G_JPEG_APP_DATA: { /* get appn marker data */
  594. struct jpeg_app_marker *app = data;
  595. if (size != sizeof(struct jpeg_app_marker))
  596. return -EFAULT;
  597. *app = ptr->app;
  598. break;
  599. }
  600. case CODEC_S_JPEG_APP_DATA: { /* set appn marker data */
  601. struct jpeg_app_marker *app = data;
  602. if (size != sizeof(struct jpeg_app_marker))
  603. return -EFAULT;
  604. ptr->app = *app;
  605. break;
  606. }
  607. case CODEC_G_JPEG_COM_DATA: { /* get comment marker data */
  608. struct jpeg_com_marker *com = data;
  609. if (size != sizeof(struct jpeg_com_marker))
  610. return -EFAULT;
  611. *com = ptr->com;
  612. break;
  613. }
  614. case CODEC_S_JPEG_COM_DATA: { /* set comment marker data */
  615. struct jpeg_com_marker *com = data;
  616. if (size != sizeof(struct jpeg_com_marker))
  617. return -EFAULT;
  618. ptr->com = *com;
  619. break;
  620. }
  621. default:
  622. return -EINVAL;
  623. }
  624. return size;
  625. }
  626. /* =========================================================================
  627. Exit and unregister function:
  628. Deinitializes Zoran's JPEG processor
  629. ========================================================================= */
  630. static int
  631. zr36050_unset (struct videocodec *codec)
  632. {
  633. struct zr36050 *ptr = codec->data;
  634. if (ptr) {
  635. /* do wee need some codec deinit here, too ???? */
  636. dprintk(1, "%s: finished codec #%d\n", ptr->name,
  637. ptr->num);
  638. kfree(ptr);
  639. codec->data = NULL;
  640. zr36050_codecs--;
  641. return 0;
  642. }
  643. return -EFAULT;
  644. }
  645. /* =========================================================================
  646. Setup and registry function:
  647. Initializes Zoran's JPEG processor
  648. Also sets pixel size, average code size, mode (compr./decompr.)
  649. (the given size is determined by the processor with the video interface)
  650. ========================================================================= */
  651. static int
  652. zr36050_setup (struct videocodec *codec)
  653. {
  654. struct zr36050 *ptr;
  655. int res;
  656. dprintk(2, "zr36050: initializing MJPEG subsystem #%d.\n",
  657. zr36050_codecs);
  658. if (zr36050_codecs == MAX_CODECS) {
  659. dprintk(1,
  660. KERN_ERR "zr36050: Can't attach more codecs!\n");
  661. return -ENOSPC;
  662. }
  663. //mem structure init
  664. codec->data = ptr = kzalloc(sizeof(struct zr36050), GFP_KERNEL);
  665. if (NULL == ptr) {
  666. dprintk(1, KERN_ERR "zr36050: Can't get enough memory!\n");
  667. return -ENOMEM;
  668. }
  669. snprintf(ptr->name, sizeof(ptr->name), "zr36050[%d]",
  670. zr36050_codecs);
  671. ptr->num = zr36050_codecs++;
  672. ptr->codec = codec;
  673. //testing
  674. res = zr36050_basic_test(ptr);
  675. if (res < 0) {
  676. zr36050_unset(codec);
  677. return res;
  678. }
  679. //final setup
  680. memcpy(ptr->h_samp_ratio, zr36050_decimation_h, 8);
  681. memcpy(ptr->v_samp_ratio, zr36050_decimation_v, 8);
  682. ptr->bitrate_ctrl = 0; /* 0 or 1 - fixed file size flag
  683. * (what is the difference?) */
  684. ptr->mode = CODEC_DO_COMPRESSION;
  685. ptr->width = 384;
  686. ptr->height = 288;
  687. ptr->total_code_vol = 16000;
  688. ptr->max_block_vol = 240;
  689. ptr->scalefact = 0x100;
  690. ptr->dri = 1;
  691. /* no app/com marker by default */
  692. ptr->app.appn = 0;
  693. ptr->app.len = 0;
  694. ptr->com.len = 0;
  695. zr36050_init(ptr);
  696. dprintk(1, KERN_INFO "%s: codec attached and running\n",
  697. ptr->name);
  698. return 0;
  699. }
  700. static const struct videocodec zr36050_codec = {
  701. .owner = THIS_MODULE,
  702. .name = "zr36050",
  703. .magic = 0L, // magic not used
  704. .flags =
  705. CODEC_FLAG_JPEG | CODEC_FLAG_HARDWARE | CODEC_FLAG_ENCODER |
  706. CODEC_FLAG_DECODER,
  707. .type = CODEC_TYPE_ZR36050,
  708. .setup = zr36050_setup, // functionality
  709. .unset = zr36050_unset,
  710. .set_mode = zr36050_set_mode,
  711. .set_video = zr36050_set_video,
  712. .control = zr36050_control,
  713. // others are not used
  714. };
  715. /* =========================================================================
  716. HOOK IN DRIVER AS KERNEL MODULE
  717. ========================================================================= */
  718. static int __init
  719. zr36050_init_module (void)
  720. {
  721. //dprintk(1, "ZR36050 driver %s\n",ZR050_VERSION);
  722. zr36050_codecs = 0;
  723. return videocodec_register(&zr36050_codec);
  724. }
  725. static void __exit
  726. zr36050_cleanup_module (void)
  727. {
  728. if (zr36050_codecs) {
  729. dprintk(1,
  730. "zr36050: something's wrong - %d codecs left somehow.\n",
  731. zr36050_codecs);
  732. }
  733. videocodec_unregister(&zr36050_codec);
  734. }
  735. module_init(zr36050_init_module);
  736. module_exit(zr36050_cleanup_module);
  737. MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
  738. MODULE_DESCRIPTION("Driver module for ZR36050 jpeg processors "
  739. ZR050_VERSION);
  740. MODULE_LICENSE("GPL");