zr36050.c 27 KB

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