zr36016.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Zoran ZR36016 basic configuration functions
  3. *
  4. * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
  5. *
  6. * $Id: zr36016.c,v 1.1.2.14 2003/08/20 19:46:55 rbultje Exp $
  7. *
  8. * ------------------------------------------------------------------------
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * ------------------------------------------------------------------------
  21. */
  22. #define ZR016_VERSION "v0.7"
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/types.h>
  28. #include <linux/wait.h>
  29. /* I/O commands, error codes */
  30. #include <asm/io.h>
  31. /* v4l API */
  32. /* headerfile of this module */
  33. #include "zr36016.h"
  34. /* codec io API */
  35. #include "videocodec.h"
  36. /* it doesn't make sense to have more than 20 or so,
  37. just to prevent some unwanted loops */
  38. #define MAX_CODECS 20
  39. /* amount of chips attached via this driver */
  40. static int zr36016_codecs;
  41. /* debugging is available via module parameter */
  42. static int debug;
  43. module_param(debug, int, 0);
  44. MODULE_PARM_DESC(debug, "Debug level (0-4)");
  45. #define dprintk(num, format, args...) \
  46. do { \
  47. if (debug >= num) \
  48. printk(format, ##args); \
  49. } while (0)
  50. /* =========================================================================
  51. Local hardware I/O functions:
  52. read/write via codec layer (registers are located in the master device)
  53. ========================================================================= */
  54. /* read and write functions */
  55. static u8
  56. zr36016_read (struct zr36016 *ptr,
  57. u16 reg)
  58. {
  59. u8 value = 0;
  60. // just in case something is wrong...
  61. if (ptr->codec->master_data->readreg)
  62. value =
  63. (ptr->codec->master_data->
  64. readreg(ptr->codec, reg)) & 0xFF;
  65. else
  66. dprintk(1,
  67. KERN_ERR "%s: invalid I/O setup, nothing read!\n",
  68. ptr->name);
  69. dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg,
  70. value);
  71. return value;
  72. }
  73. static void
  74. zr36016_write (struct zr36016 *ptr,
  75. u16 reg,
  76. u8 value)
  77. {
  78. dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value,
  79. reg);
  80. // just in case something is wrong...
  81. if (ptr->codec->master_data->writereg) {
  82. ptr->codec->master_data->writereg(ptr->codec, reg, value);
  83. } else
  84. dprintk(1,
  85. KERN_ERR
  86. "%s: invalid I/O setup, nothing written!\n",
  87. ptr->name);
  88. }
  89. /* indirect read and write functions */
  90. /* the 016 supports auto-addr-increment, but
  91. * writing it all time cost not much and is safer... */
  92. static u8
  93. zr36016_readi (struct zr36016 *ptr,
  94. u16 reg)
  95. {
  96. u8 value = 0;
  97. // just in case something is wrong...
  98. if ((ptr->codec->master_data->writereg) &&
  99. (ptr->codec->master_data->readreg)) {
  100. ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
  101. value = (ptr->codec->master_data->readreg(ptr->codec, ZR016_IDATA)) & 0xFF; // DATA
  102. } else
  103. dprintk(1,
  104. KERN_ERR
  105. "%s: invalid I/O setup, nothing read (i)!\n",
  106. ptr->name);
  107. dprintk(4, "%s: reading indirect from 0x%04x: %02x\n", ptr->name,
  108. reg, value);
  109. return value;
  110. }
  111. static void
  112. zr36016_writei (struct zr36016 *ptr,
  113. u16 reg,
  114. u8 value)
  115. {
  116. dprintk(4, "%s: writing indirect 0x%02x to 0x%04x\n", ptr->name,
  117. value, reg);
  118. // just in case something is wrong...
  119. if (ptr->codec->master_data->writereg) {
  120. ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
  121. ptr->codec->master_data->writereg(ptr->codec, ZR016_IDATA, value & 0x0FF); // DATA
  122. } else
  123. dprintk(1,
  124. KERN_ERR
  125. "%s: invalid I/O setup, nothing written (i)!\n",
  126. ptr->name);
  127. }
  128. /* =========================================================================
  129. Local helper function:
  130. version read
  131. ========================================================================= */
  132. /* version kept in datastructure */
  133. static u8
  134. zr36016_read_version (struct zr36016 *ptr)
  135. {
  136. ptr->version = zr36016_read(ptr, 0) >> 4;
  137. return ptr->version;
  138. }
  139. /* =========================================================================
  140. Local helper function:
  141. basic test of "connectivity", writes/reads to/from PAX-Lo register
  142. ========================================================================= */
  143. static int
  144. zr36016_basic_test (struct zr36016 *ptr)
  145. {
  146. if (debug) {
  147. int i;
  148. zr36016_writei(ptr, ZR016I_PAX_LO, 0x55);
  149. dprintk(1, KERN_INFO "%s: registers: ", ptr->name);
  150. for (i = 0; i <= 0x0b; i++)
  151. dprintk(1, "%02x ", zr36016_readi(ptr, i));
  152. dprintk(1, "\n");
  153. }
  154. // for testing just write 0, then the default value to a register and read
  155. // it back in both cases
  156. zr36016_writei(ptr, ZR016I_PAX_LO, 0x00);
  157. if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0) {
  158. dprintk(1,
  159. KERN_ERR
  160. "%s: attach failed, can't connect to vfe processor!\n",
  161. ptr->name);
  162. return -ENXIO;
  163. }
  164. zr36016_writei(ptr, ZR016I_PAX_LO, 0x0d0);
  165. if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0d0) {
  166. dprintk(1,
  167. KERN_ERR
  168. "%s: attach failed, can't connect to vfe processor!\n",
  169. ptr->name);
  170. return -ENXIO;
  171. }
  172. // we allow version numbers from 0-3, should be enough, though
  173. zr36016_read_version(ptr);
  174. if (ptr->version & 0x0c) {
  175. dprintk(1,
  176. KERN_ERR
  177. "%s: attach failed, suspicious version %d found...\n",
  178. ptr->name, ptr->version);
  179. return -ENXIO;
  180. }
  181. return 0; /* looks good! */
  182. }
  183. /* =========================================================================
  184. Local helper function:
  185. simple loop for pushing the init datasets - NO USE --
  186. ========================================================================= */
  187. #if 0
  188. static int zr36016_pushit (struct zr36016 *ptr,
  189. u16 startreg,
  190. u16 len,
  191. const char *data)
  192. {
  193. int i=0;
  194. dprintk(4, "%s: write data block to 0x%04x (len=%d)\n",
  195. ptr->name, startreg,len);
  196. while (i<len) {
  197. zr36016_writei(ptr, startreg++, data[i++]);
  198. }
  199. return i;
  200. }
  201. #endif
  202. /* =========================================================================
  203. Basic datasets & init:
  204. //TODO//
  205. ========================================================================= */
  206. static void
  207. zr36016_init (struct zr36016 *ptr)
  208. {
  209. // stop any processing
  210. zr36016_write(ptr, ZR016_GOSTOP, 0);
  211. // mode setup (yuv422 in and out, compression/expansuon due to mode)
  212. zr36016_write(ptr, ZR016_MODE,
  213. ZR016_YUV422 | ZR016_YUV422_YUV422 |
  214. (ptr->mode == CODEC_DO_COMPRESSION ?
  215. ZR016_COMPRESSION : ZR016_EXPANSION));
  216. // misc setup
  217. zr36016_writei(ptr, ZR016I_SETUP1,
  218. (ptr->xdec ? (ZR016_HRFL | ZR016_HORZ) : 0) |
  219. (ptr->ydec ? ZR016_VERT : 0) | ZR016_CNTI);
  220. zr36016_writei(ptr, ZR016I_SETUP2, ZR016_CCIR);
  221. // Window setup
  222. // (no extra offset for now, norm defines offset, default width height)
  223. zr36016_writei(ptr, ZR016I_PAX_HI, ptr->width >> 8);
  224. zr36016_writei(ptr, ZR016I_PAX_LO, ptr->width & 0xFF);
  225. zr36016_writei(ptr, ZR016I_PAY_HI, ptr->height >> 8);
  226. zr36016_writei(ptr, ZR016I_PAY_LO, ptr->height & 0xFF);
  227. zr36016_writei(ptr, ZR016I_NAX_HI, ptr->xoff >> 8);
  228. zr36016_writei(ptr, ZR016I_NAX_LO, ptr->xoff & 0xFF);
  229. zr36016_writei(ptr, ZR016I_NAY_HI, ptr->yoff >> 8);
  230. zr36016_writei(ptr, ZR016I_NAY_LO, ptr->yoff & 0xFF);
  231. /* shall we continue now, please? */
  232. zr36016_write(ptr, ZR016_GOSTOP, 1);
  233. }
  234. /* =========================================================================
  235. CODEC API FUNCTIONS
  236. this functions are accessed by the master via the API structure
  237. ========================================================================= */
  238. /* set compression/expansion mode and launches codec -
  239. this should be the last call from the master before starting processing */
  240. static int
  241. zr36016_set_mode (struct videocodec *codec,
  242. int mode)
  243. {
  244. struct zr36016 *ptr = (struct zr36016 *) codec->data;
  245. dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
  246. if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
  247. return -EINVAL;
  248. ptr->mode = mode;
  249. zr36016_init(ptr);
  250. return 0;
  251. }
  252. /* set picture size */
  253. static int
  254. zr36016_set_video (struct videocodec *codec,
  255. struct tvnorm *norm,
  256. struct vfe_settings *cap,
  257. struct vfe_polarity *pol)
  258. {
  259. struct zr36016 *ptr = (struct zr36016 *) codec->data;
  260. dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) call\n",
  261. ptr->name, norm->HStart, norm->VStart,
  262. cap->x, cap->y, cap->width, cap->height,
  263. cap->decimation);
  264. /* if () return -EINVAL;
  265. * trust the master driver that it knows what it does - so
  266. * we allow invalid startx/y for now ... */
  267. ptr->width = cap->width;
  268. ptr->height = cap->height;
  269. /* (Ronald) This is ugly. zoran_device.c, line 387
  270. * already mentions what happens if HStart is even
  271. * (blue faces, etc., cr/cb inversed). There's probably
  272. * some good reason why HStart is 0 instead of 1, so I'm
  273. * leaving it to this for now, but really... This can be
  274. * done a lot simpler */
  275. ptr->xoff = (norm->HStart ? norm->HStart : 1) + cap->x;
  276. /* Something to note here (I don't understand it), setting
  277. * VStart too high will cause the codec to 'not work'. I
  278. * really don't get it. values of 16 (VStart) already break
  279. * it here. Just '0' seems to work. More testing needed! */
  280. ptr->yoff = norm->VStart + cap->y;
  281. /* (Ronald) dzjeeh, can't this thing do hor_decimation = 4? */
  282. ptr->xdec = ((cap->decimation & 0xff) == 1) ? 0 : 1;
  283. ptr->ydec = (((cap->decimation >> 8) & 0xff) == 1) ? 0 : 1;
  284. return 0;
  285. }
  286. /* additional control functions */
  287. static int
  288. zr36016_control (struct videocodec *codec,
  289. int type,
  290. int size,
  291. void *data)
  292. {
  293. struct zr36016 *ptr = (struct zr36016 *) codec->data;
  294. int *ival = (int *) data;
  295. dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
  296. size);
  297. switch (type) {
  298. case CODEC_G_STATUS: /* get last status - we don't know it ... */
  299. if (size != sizeof(int))
  300. return -EFAULT;
  301. *ival = 0;
  302. break;
  303. case CODEC_G_CODEC_MODE:
  304. if (size != sizeof(int))
  305. return -EFAULT;
  306. *ival = 0;
  307. break;
  308. case CODEC_S_CODEC_MODE:
  309. if (size != sizeof(int))
  310. return -EFAULT;
  311. if (*ival != 0)
  312. return -EINVAL;
  313. /* not needed, do nothing */
  314. return 0;
  315. case CODEC_G_VFE:
  316. case CODEC_S_VFE:
  317. return 0;
  318. case CODEC_S_MMAP:
  319. /* not available, give an error */
  320. return -ENXIO;
  321. default:
  322. return -EINVAL;
  323. }
  324. return size;
  325. }
  326. /* =========================================================================
  327. Exit and unregister function:
  328. Deinitializes Zoran's JPEG processor
  329. ========================================================================= */
  330. static int
  331. zr36016_unset (struct videocodec *codec)
  332. {
  333. struct zr36016 *ptr = codec->data;
  334. if (ptr) {
  335. /* do wee need some codec deinit here, too ???? */
  336. dprintk(1, "%s: finished codec #%d\n", ptr->name,
  337. ptr->num);
  338. kfree(ptr);
  339. codec->data = NULL;
  340. zr36016_codecs--;
  341. return 0;
  342. }
  343. return -EFAULT;
  344. }
  345. /* =========================================================================
  346. Setup and registry function:
  347. Initializes Zoran's JPEG processor
  348. Also sets pixel size, average code size, mode (compr./decompr.)
  349. (the given size is determined by the processor with the video interface)
  350. ========================================================================= */
  351. static int
  352. zr36016_setup (struct videocodec *codec)
  353. {
  354. struct zr36016 *ptr;
  355. int res;
  356. dprintk(2, "zr36016: initializing VFE subsystem #%d.\n",
  357. zr36016_codecs);
  358. if (zr36016_codecs == MAX_CODECS) {
  359. dprintk(1,
  360. KERN_ERR "zr36016: Can't attach more codecs!\n");
  361. return -ENOSPC;
  362. }
  363. //mem structure init
  364. codec->data = ptr = kzalloc(sizeof(struct zr36016), GFP_KERNEL);
  365. if (NULL == ptr) {
  366. dprintk(1, KERN_ERR "zr36016: Can't get enough memory!\n");
  367. return -ENOMEM;
  368. }
  369. snprintf(ptr->name, sizeof(ptr->name), "zr36016[%d]",
  370. zr36016_codecs);
  371. ptr->num = zr36016_codecs++;
  372. ptr->codec = codec;
  373. //testing
  374. res = zr36016_basic_test(ptr);
  375. if (res < 0) {
  376. zr36016_unset(codec);
  377. return res;
  378. }
  379. //final setup
  380. ptr->mode = CODEC_DO_COMPRESSION;
  381. ptr->width = 768;
  382. ptr->height = 288;
  383. ptr->xdec = 1;
  384. ptr->ydec = 0;
  385. zr36016_init(ptr);
  386. dprintk(1, KERN_INFO "%s: codec v%d attached and running\n",
  387. ptr->name, ptr->version);
  388. return 0;
  389. }
  390. static const struct videocodec zr36016_codec = {
  391. .owner = THIS_MODULE,
  392. .name = "zr36016",
  393. .magic = 0L, // magic not used
  394. .flags =
  395. CODEC_FLAG_HARDWARE | CODEC_FLAG_VFE | CODEC_FLAG_ENCODER |
  396. CODEC_FLAG_DECODER,
  397. .type = CODEC_TYPE_ZR36016,
  398. .setup = zr36016_setup, // functionality
  399. .unset = zr36016_unset,
  400. .set_mode = zr36016_set_mode,
  401. .set_video = zr36016_set_video,
  402. .control = zr36016_control,
  403. // others are not used
  404. };
  405. /* =========================================================================
  406. HOOK IN DRIVER AS KERNEL MODULE
  407. ========================================================================= */
  408. static int __init
  409. zr36016_init_module (void)
  410. {
  411. //dprintk(1, "ZR36016 driver %s\n",ZR016_VERSION);
  412. zr36016_codecs = 0;
  413. return videocodec_register(&zr36016_codec);
  414. }
  415. static void __exit
  416. zr36016_cleanup_module (void)
  417. {
  418. if (zr36016_codecs) {
  419. dprintk(1,
  420. "zr36016: something's wrong - %d codecs left somehow.\n",
  421. zr36016_codecs);
  422. }
  423. videocodec_unregister(&zr36016_codec);
  424. }
  425. module_init(zr36016_init_module);
  426. module_exit(zr36016_cleanup_module);
  427. MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
  428. MODULE_DESCRIPTION("Driver module for ZR36016 video frontends "
  429. ZR016_VERSION);
  430. MODULE_LICENSE("GPL");