zr36050.c 26 KB

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