zr36060.c 30 KB

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