zr36060.c 30 KB

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