mcam-core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /*
  2. * The Marvell camera core. This device appears in a number of settings,
  3. * so it needs platform-specific support outside of the core.
  4. *
  5. * Copyright 2011 Jonathan Corbet corbet@lwn.net
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/slab.h>
  15. #include <linux/device.h>
  16. #include <linux/wait.h>
  17. #include <linux/list.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/delay.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/io.h>
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include <media/v4l2-chip-ident.h>
  26. #include <media/ov7670.h>
  27. #include <media/videobuf2-vmalloc.h>
  28. #include "mcam-core.h"
  29. /*
  30. * Internal DMA buffer management. Since the controller cannot do S/G I/O,
  31. * we must have physically contiguous buffers to bring frames into.
  32. * These parameters control how many buffers we use, whether we
  33. * allocate them at load time (better chance of success, but nails down
  34. * memory) or when somebody tries to use the camera (riskier), and,
  35. * for load-time allocation, how big they should be.
  36. *
  37. * The controller can cycle through three buffers. We could use
  38. * more by flipping pointers around, but it probably makes little
  39. * sense.
  40. */
  41. static int alloc_bufs_at_read;
  42. module_param(alloc_bufs_at_read, bool, 0444);
  43. MODULE_PARM_DESC(alloc_bufs_at_read,
  44. "Non-zero value causes DMA buffers to be allocated when the "
  45. "video capture device is read, rather than at module load "
  46. "time. This saves memory, but decreases the chances of "
  47. "successfully getting those buffers.");
  48. static int n_dma_bufs = 3;
  49. module_param(n_dma_bufs, uint, 0644);
  50. MODULE_PARM_DESC(n_dma_bufs,
  51. "The number of DMA buffers to allocate. Can be either two "
  52. "(saves memory, makes timing tighter) or three.");
  53. static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
  54. module_param(dma_buf_size, uint, 0444);
  55. MODULE_PARM_DESC(dma_buf_size,
  56. "The size of the allocated DMA buffers. If actual operating "
  57. "parameters require larger buffers, an attempt to reallocate "
  58. "will be made.");
  59. static int min_buffers = 1;
  60. module_param(min_buffers, uint, 0644);
  61. MODULE_PARM_DESC(min_buffers,
  62. "The minimum number of streaming I/O buffers we are willing "
  63. "to work with.");
  64. static int max_buffers = 10;
  65. module_param(max_buffers, uint, 0644);
  66. MODULE_PARM_DESC(max_buffers,
  67. "The maximum number of streaming I/O buffers an application "
  68. "will be allowed to allocate. These buffers are big and live "
  69. "in vmalloc space.");
  70. static int flip;
  71. module_param(flip, bool, 0444);
  72. MODULE_PARM_DESC(flip,
  73. "If set, the sensor will be instructed to flip the image "
  74. "vertically.");
  75. /*
  76. * Status flags. Always manipulated with bit operations.
  77. */
  78. #define CF_BUF0_VALID 0 /* Buffers valid - first three */
  79. #define CF_BUF1_VALID 1
  80. #define CF_BUF2_VALID 2
  81. #define CF_DMA_ACTIVE 3 /* A frame is incoming */
  82. #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
  83. #define sensor_call(cam, o, f, args...) \
  84. v4l2_subdev_call(cam->sensor, o, f, ##args)
  85. static struct mcam_format_struct {
  86. __u8 *desc;
  87. __u32 pixelformat;
  88. int bpp; /* Bytes per pixel */
  89. enum v4l2_mbus_pixelcode mbus_code;
  90. } mcam_formats[] = {
  91. {
  92. .desc = "YUYV 4:2:2",
  93. .pixelformat = V4L2_PIX_FMT_YUYV,
  94. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  95. .bpp = 2,
  96. },
  97. {
  98. .desc = "RGB 444",
  99. .pixelformat = V4L2_PIX_FMT_RGB444,
  100. .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
  101. .bpp = 2,
  102. },
  103. {
  104. .desc = "RGB 565",
  105. .pixelformat = V4L2_PIX_FMT_RGB565,
  106. .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  107. .bpp = 2,
  108. },
  109. {
  110. .desc = "Raw RGB Bayer",
  111. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  112. .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
  113. .bpp = 1
  114. },
  115. };
  116. #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
  117. static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
  118. {
  119. unsigned i;
  120. for (i = 0; i < N_MCAM_FMTS; i++)
  121. if (mcam_formats[i].pixelformat == pixelformat)
  122. return mcam_formats + i;
  123. /* Not found? Then return the first format. */
  124. return mcam_formats;
  125. }
  126. /*
  127. * Start over with DMA buffers - dev_lock needed.
  128. */
  129. static void mcam_reset_buffers(struct mcam_camera *cam)
  130. {
  131. int i;
  132. cam->next_buf = -1;
  133. for (i = 0; i < cam->nbufs; i++)
  134. clear_bit(i, &cam->flags);
  135. }
  136. static inline int mcam_needs_config(struct mcam_camera *cam)
  137. {
  138. return test_bit(CF_CONFIG_NEEDED, &cam->flags);
  139. }
  140. static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
  141. {
  142. if (needed)
  143. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  144. else
  145. clear_bit(CF_CONFIG_NEEDED, &cam->flags);
  146. }
  147. /*
  148. * Our buffer type for working with videobuf2. Note that the vb2
  149. * developers have decreed that struct vb2_buffer must be at the
  150. * beginning of this structure.
  151. */
  152. struct mcam_vb_buffer {
  153. struct vb2_buffer vb_buf;
  154. struct list_head queue;
  155. };
  156. static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
  157. {
  158. return container_of(vb, struct mcam_vb_buffer, vb_buf);
  159. }
  160. /*
  161. * Debugging and related.
  162. */
  163. #define cam_err(cam, fmt, arg...) \
  164. dev_err((cam)->dev, fmt, ##arg);
  165. #define cam_warn(cam, fmt, arg...) \
  166. dev_warn((cam)->dev, fmt, ##arg);
  167. #define cam_dbg(cam, fmt, arg...) \
  168. dev_dbg((cam)->dev, fmt, ##arg);
  169. /* ------------------------------------------------------------------- */
  170. /*
  171. * Deal with the controller.
  172. */
  173. /*
  174. * Do everything we think we need to have the interface operating
  175. * according to the desired format.
  176. */
  177. static void mcam_ctlr_dma(struct mcam_camera *cam)
  178. {
  179. /*
  180. * Store the first two Y buffers (we aren't supporting
  181. * planar formats for now, so no UV bufs). Then either
  182. * set the third if it exists, or tell the controller
  183. * to just use two.
  184. */
  185. mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
  186. mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
  187. if (cam->nbufs > 2) {
  188. mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
  189. mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
  190. } else
  191. mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
  192. if (cam->chip_id == V4L2_IDENT_CAFE)
  193. mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
  194. }
  195. static void mcam_ctlr_image(struct mcam_camera *cam)
  196. {
  197. int imgsz;
  198. struct v4l2_pix_format *fmt = &cam->pix_format;
  199. imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
  200. (fmt->bytesperline & IMGSZ_H_MASK);
  201. mcam_reg_write(cam, REG_IMGSIZE, imgsz);
  202. mcam_reg_write(cam, REG_IMGOFFSET, 0);
  203. /* YPITCH just drops the last two bits */
  204. mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
  205. IMGP_YP_MASK);
  206. /*
  207. * Tell the controller about the image format we are using.
  208. */
  209. switch (cam->pix_format.pixelformat) {
  210. case V4L2_PIX_FMT_YUYV:
  211. mcam_reg_write_mask(cam, REG_CTRL0,
  212. C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
  213. C0_DF_MASK);
  214. break;
  215. case V4L2_PIX_FMT_RGB444:
  216. mcam_reg_write_mask(cam, REG_CTRL0,
  217. C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
  218. C0_DF_MASK);
  219. /* Alpha value? */
  220. break;
  221. case V4L2_PIX_FMT_RGB565:
  222. mcam_reg_write_mask(cam, REG_CTRL0,
  223. C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
  224. C0_DF_MASK);
  225. break;
  226. default:
  227. cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
  228. break;
  229. }
  230. /*
  231. * Make sure it knows we want to use hsync/vsync.
  232. */
  233. mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
  234. C0_SIFM_MASK);
  235. }
  236. /*
  237. * Configure the controller for operation; caller holds the
  238. * device mutex.
  239. */
  240. static int mcam_ctlr_configure(struct mcam_camera *cam)
  241. {
  242. unsigned long flags;
  243. spin_lock_irqsave(&cam->dev_lock, flags);
  244. mcam_ctlr_dma(cam);
  245. mcam_ctlr_image(cam);
  246. mcam_set_config_needed(cam, 0);
  247. spin_unlock_irqrestore(&cam->dev_lock, flags);
  248. return 0;
  249. }
  250. static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
  251. {
  252. /*
  253. * Clear any pending interrupts, since we do not
  254. * expect to have I/O active prior to enabling.
  255. */
  256. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
  257. mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
  258. }
  259. static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
  260. {
  261. mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
  262. }
  263. /*
  264. * Make the controller start grabbing images. Everything must
  265. * be set up before doing this.
  266. */
  267. static void mcam_ctlr_start(struct mcam_camera *cam)
  268. {
  269. /* set_bit performs a read, so no other barrier should be
  270. needed here */
  271. mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
  272. }
  273. static void mcam_ctlr_stop(struct mcam_camera *cam)
  274. {
  275. mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  276. }
  277. static void mcam_ctlr_init(struct mcam_camera *cam)
  278. {
  279. unsigned long flags;
  280. spin_lock_irqsave(&cam->dev_lock, flags);
  281. /*
  282. * Make sure it's not powered down.
  283. */
  284. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  285. /*
  286. * Turn off the enable bit. It sure should be off anyway,
  287. * but it's good to be sure.
  288. */
  289. mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  290. /*
  291. * Clock the sensor appropriately. Controller clock should
  292. * be 48MHz, sensor "typical" value is half that.
  293. */
  294. mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
  295. spin_unlock_irqrestore(&cam->dev_lock, flags);
  296. }
  297. /*
  298. * Stop the controller, and don't return until we're really sure that no
  299. * further DMA is going on.
  300. */
  301. static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
  302. {
  303. unsigned long flags;
  304. /*
  305. * Theory: stop the camera controller (whether it is operating
  306. * or not). Delay briefly just in case we race with the SOF
  307. * interrupt, then wait until no DMA is active.
  308. */
  309. spin_lock_irqsave(&cam->dev_lock, flags);
  310. mcam_ctlr_stop(cam);
  311. spin_unlock_irqrestore(&cam->dev_lock, flags);
  312. msleep(10);
  313. if (test_bit(CF_DMA_ACTIVE, &cam->flags))
  314. cam_err(cam, "Timeout waiting for DMA to end\n");
  315. /* This would be bad news - what now? */
  316. spin_lock_irqsave(&cam->dev_lock, flags);
  317. cam->state = S_IDLE;
  318. mcam_ctlr_irq_disable(cam);
  319. spin_unlock_irqrestore(&cam->dev_lock, flags);
  320. }
  321. /*
  322. * Power up and down.
  323. */
  324. static void mcam_ctlr_power_up(struct mcam_camera *cam)
  325. {
  326. unsigned long flags;
  327. spin_lock_irqsave(&cam->dev_lock, flags);
  328. cam->plat_power_up(cam);
  329. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  330. spin_unlock_irqrestore(&cam->dev_lock, flags);
  331. msleep(5); /* Just to be sure */
  332. }
  333. static void mcam_ctlr_power_down(struct mcam_camera *cam)
  334. {
  335. unsigned long flags;
  336. spin_lock_irqsave(&cam->dev_lock, flags);
  337. /*
  338. * School of hard knocks department: be sure we do any register
  339. * twiddling on the controller *before* calling the platform
  340. * power down routine.
  341. */
  342. mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
  343. cam->plat_power_down(cam);
  344. spin_unlock_irqrestore(&cam->dev_lock, flags);
  345. }
  346. /* -------------------------------------------------------------------- */
  347. /*
  348. * Communications with the sensor.
  349. */
  350. static int __mcam_cam_reset(struct mcam_camera *cam)
  351. {
  352. return sensor_call(cam, core, reset, 0);
  353. }
  354. /*
  355. * We have found the sensor on the i2c. Let's try to have a
  356. * conversation.
  357. */
  358. static int mcam_cam_init(struct mcam_camera *cam)
  359. {
  360. struct v4l2_dbg_chip_ident chip;
  361. int ret;
  362. mutex_lock(&cam->s_mutex);
  363. if (cam->state != S_NOTREADY)
  364. cam_warn(cam, "Cam init with device in funky state %d",
  365. cam->state);
  366. ret = __mcam_cam_reset(cam);
  367. if (ret)
  368. goto out;
  369. chip.ident = V4L2_IDENT_NONE;
  370. chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
  371. chip.match.addr = cam->sensor_addr;
  372. ret = sensor_call(cam, core, g_chip_ident, &chip);
  373. if (ret)
  374. goto out;
  375. cam->sensor_type = chip.ident;
  376. if (cam->sensor_type != V4L2_IDENT_OV7670) {
  377. cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
  378. ret = -EINVAL;
  379. goto out;
  380. }
  381. /* Get/set parameters? */
  382. ret = 0;
  383. cam->state = S_IDLE;
  384. out:
  385. mcam_ctlr_power_down(cam);
  386. mutex_unlock(&cam->s_mutex);
  387. return ret;
  388. }
  389. /*
  390. * Configure the sensor to match the parameters we have. Caller should
  391. * hold s_mutex
  392. */
  393. static int mcam_cam_set_flip(struct mcam_camera *cam)
  394. {
  395. struct v4l2_control ctrl;
  396. memset(&ctrl, 0, sizeof(ctrl));
  397. ctrl.id = V4L2_CID_VFLIP;
  398. ctrl.value = flip;
  399. return sensor_call(cam, core, s_ctrl, &ctrl);
  400. }
  401. static int mcam_cam_configure(struct mcam_camera *cam)
  402. {
  403. struct v4l2_mbus_framefmt mbus_fmt;
  404. int ret;
  405. v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
  406. ret = sensor_call(cam, core, init, 0);
  407. if (ret == 0)
  408. ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
  409. /*
  410. * OV7670 does weird things if flip is set *before* format...
  411. */
  412. ret += mcam_cam_set_flip(cam);
  413. return ret;
  414. }
  415. /* -------------------------------------------------------------------- */
  416. /*
  417. * DMA buffer management. These functions need s_mutex held.
  418. */
  419. /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
  420. * does a get_free_pages() call, and we waste a good chunk of an orderN
  421. * allocation. Should try to allocate the whole set in one chunk.
  422. */
  423. static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
  424. {
  425. int i;
  426. mcam_set_config_needed(cam, 1);
  427. if (loadtime)
  428. cam->dma_buf_size = dma_buf_size;
  429. else
  430. cam->dma_buf_size = cam->pix_format.sizeimage;
  431. if (n_dma_bufs > 3)
  432. n_dma_bufs = 3;
  433. cam->nbufs = 0;
  434. for (i = 0; i < n_dma_bufs; i++) {
  435. cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
  436. cam->dma_buf_size, cam->dma_handles + i,
  437. GFP_KERNEL);
  438. if (cam->dma_bufs[i] == NULL) {
  439. cam_warn(cam, "Failed to allocate DMA buffer\n");
  440. break;
  441. }
  442. (cam->nbufs)++;
  443. }
  444. switch (cam->nbufs) {
  445. case 1:
  446. dma_free_coherent(cam->dev, cam->dma_buf_size,
  447. cam->dma_bufs[0], cam->dma_handles[0]);
  448. cam->nbufs = 0;
  449. case 0:
  450. cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
  451. return -ENOMEM;
  452. case 2:
  453. if (n_dma_bufs > 2)
  454. cam_warn(cam, "Will limp along with only 2 buffers\n");
  455. break;
  456. }
  457. return 0;
  458. }
  459. static void mcam_free_dma_bufs(struct mcam_camera *cam)
  460. {
  461. int i;
  462. for (i = 0; i < cam->nbufs; i++) {
  463. dma_free_coherent(cam->dev, cam->dma_buf_size,
  464. cam->dma_bufs[i], cam->dma_handles[i]);
  465. cam->dma_bufs[i] = NULL;
  466. }
  467. cam->nbufs = 0;
  468. }
  469. /* ----------------------------------------------------------------------- */
  470. /*
  471. * Here starts the V4L2 interface code.
  472. */
  473. /*
  474. * Get everything ready, and start grabbing frames.
  475. */
  476. static int mcam_read_setup(struct mcam_camera *cam, enum mcam_state state)
  477. {
  478. int ret;
  479. unsigned long flags;
  480. /*
  481. * Configuration. If we still don't have DMA buffers,
  482. * make one last, desperate attempt.
  483. */
  484. if (cam->nbufs == 0)
  485. if (mcam_alloc_dma_bufs(cam, 0))
  486. return -ENOMEM;
  487. if (mcam_needs_config(cam)) {
  488. mcam_cam_configure(cam);
  489. ret = mcam_ctlr_configure(cam);
  490. if (ret)
  491. return ret;
  492. }
  493. /*
  494. * Turn it loose.
  495. */
  496. spin_lock_irqsave(&cam->dev_lock, flags);
  497. mcam_reset_buffers(cam);
  498. mcam_ctlr_irq_enable(cam);
  499. cam->state = state;
  500. mcam_ctlr_start(cam);
  501. spin_unlock_irqrestore(&cam->dev_lock, flags);
  502. return 0;
  503. }
  504. /* ----------------------------------------------------------------------- */
  505. /*
  506. * Videobuf2 interface code.
  507. */
  508. static int mcam_vb_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
  509. unsigned int *num_planes, unsigned long sizes[],
  510. void *alloc_ctxs[])
  511. {
  512. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  513. sizes[0] = cam->pix_format.sizeimage;
  514. *num_planes = 1; /* Someday we have to support planar formats... */
  515. if (*nbufs < 2 || *nbufs > 32)
  516. *nbufs = 6; /* semi-arbitrary numbers */
  517. return 0;
  518. }
  519. static int mcam_vb_buf_init(struct vb2_buffer *vb)
  520. {
  521. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  522. INIT_LIST_HEAD(&mvb->queue);
  523. return 0;
  524. }
  525. static void mcam_vb_buf_queue(struct vb2_buffer *vb)
  526. {
  527. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  528. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  529. unsigned long flags;
  530. spin_lock_irqsave(&cam->dev_lock, flags);
  531. list_add(&cam->buffers, &mvb->queue);
  532. spin_unlock_irqrestore(&cam->dev_lock, flags);
  533. }
  534. /*
  535. * vb2 uses these to release the mutex when waiting in dqbuf. I'm
  536. * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
  537. * to be called with the mutex held), but better safe than sorry.
  538. */
  539. static void mcam_vb_wait_prepare(struct vb2_queue *vq)
  540. {
  541. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  542. mutex_unlock(&cam->s_mutex);
  543. }
  544. static void mcam_vb_wait_finish(struct vb2_queue *vq)
  545. {
  546. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  547. mutex_lock(&cam->s_mutex);
  548. }
  549. /*
  550. * These need to be called with the mutex held from vb2
  551. */
  552. static int mcam_vb_start_streaming(struct vb2_queue *vq)
  553. {
  554. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  555. int ret = -EINVAL;
  556. if (cam->state == S_IDLE) {
  557. cam->sequence = 0;
  558. ret = mcam_read_setup(cam, S_STREAMING);
  559. }
  560. return ret;
  561. }
  562. static int mcam_vb_stop_streaming(struct vb2_queue *vq)
  563. {
  564. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  565. unsigned long flags;
  566. if (cam->state != S_STREAMING)
  567. return -EINVAL;
  568. mcam_ctlr_stop_dma(cam);
  569. /*
  570. * VB2 reclaims the buffers, so we need to forget
  571. * about them.
  572. */
  573. spin_lock_irqsave(&cam->dev_lock, flags);
  574. INIT_LIST_HEAD(&cam->buffers);
  575. spin_unlock_irqrestore(&cam->dev_lock, flags);
  576. return 0;
  577. }
  578. static const struct vb2_ops mcam_vb2_ops = {
  579. .queue_setup = mcam_vb_queue_setup,
  580. .buf_init = mcam_vb_buf_init,
  581. .buf_queue = mcam_vb_buf_queue,
  582. .start_streaming = mcam_vb_start_streaming,
  583. .stop_streaming = mcam_vb_stop_streaming,
  584. .wait_prepare = mcam_vb_wait_prepare,
  585. .wait_finish = mcam_vb_wait_finish,
  586. };
  587. static int mcam_setup_vb2(struct mcam_camera *cam)
  588. {
  589. struct vb2_queue *vq = &cam->vb_queue;
  590. memset(vq, 0, sizeof(*vq));
  591. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  592. vq->io_modes = VB2_MMAP; /* Add userptr */
  593. vq->drv_priv = cam;
  594. vq->ops = &mcam_vb2_ops;
  595. vq->mem_ops = &vb2_vmalloc_memops;
  596. vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
  597. return vb2_queue_init(vq);
  598. }
  599. static void mcam_cleanup_vb2(struct mcam_camera *cam)
  600. {
  601. vb2_queue_release(&cam->vb_queue);
  602. }
  603. static ssize_t mcam_v4l_read(struct file *filp,
  604. char __user *buffer, size_t len, loff_t *pos)
  605. {
  606. struct mcam_camera *cam = filp->private_data;
  607. int ret;
  608. mutex_lock(&cam->s_mutex);
  609. ret = vb2_read(&cam->vb_queue, buffer, len, pos,
  610. filp->f_flags & O_NONBLOCK);
  611. mutex_unlock(&cam->s_mutex);
  612. return ret;
  613. }
  614. /*
  615. * Streaming I/O support.
  616. */
  617. static int mcam_vidioc_streamon(struct file *filp, void *priv,
  618. enum v4l2_buf_type type)
  619. {
  620. struct mcam_camera *cam = filp->private_data;
  621. int ret;
  622. mutex_lock(&cam->s_mutex);
  623. ret = vb2_streamon(&cam->vb_queue, type);
  624. mutex_unlock(&cam->s_mutex);
  625. return ret;
  626. }
  627. static int mcam_vidioc_streamoff(struct file *filp, void *priv,
  628. enum v4l2_buf_type type)
  629. {
  630. struct mcam_camera *cam = filp->private_data;
  631. int ret;
  632. mutex_lock(&cam->s_mutex);
  633. ret = vb2_streamoff(&cam->vb_queue, type);
  634. mutex_unlock(&cam->s_mutex);
  635. return ret;
  636. }
  637. static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
  638. struct v4l2_requestbuffers *req)
  639. {
  640. struct mcam_camera *cam = filp->private_data;
  641. int ret;
  642. mutex_lock(&cam->s_mutex);
  643. ret = vb2_reqbufs(&cam->vb_queue, req);
  644. mutex_unlock(&cam->s_mutex);
  645. return ret;
  646. }
  647. static int mcam_vidioc_querybuf(struct file *filp, void *priv,
  648. struct v4l2_buffer *buf)
  649. {
  650. struct mcam_camera *cam = filp->private_data;
  651. int ret;
  652. mutex_lock(&cam->s_mutex);
  653. ret = vb2_querybuf(&cam->vb_queue, buf);
  654. mutex_unlock(&cam->s_mutex);
  655. return ret;
  656. }
  657. static int mcam_vidioc_qbuf(struct file *filp, void *priv,
  658. struct v4l2_buffer *buf)
  659. {
  660. struct mcam_camera *cam = filp->private_data;
  661. int ret;
  662. mutex_lock(&cam->s_mutex);
  663. ret = vb2_qbuf(&cam->vb_queue, buf);
  664. mutex_unlock(&cam->s_mutex);
  665. return ret;
  666. }
  667. static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
  668. struct v4l2_buffer *buf)
  669. {
  670. struct mcam_camera *cam = filp->private_data;
  671. int ret;
  672. mutex_lock(&cam->s_mutex);
  673. ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
  674. mutex_unlock(&cam->s_mutex);
  675. return ret;
  676. }
  677. static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  678. {
  679. struct mcam_camera *cam = filp->private_data;
  680. int ret;
  681. mutex_lock(&cam->s_mutex);
  682. ret = vb2_mmap(&cam->vb_queue, vma);
  683. mutex_unlock(&cam->s_mutex);
  684. return ret;
  685. }
  686. static int mcam_v4l_open(struct file *filp)
  687. {
  688. struct mcam_camera *cam = video_drvdata(filp);
  689. int ret = 0;
  690. filp->private_data = cam;
  691. mutex_lock(&cam->s_mutex);
  692. if (cam->users == 0) {
  693. ret = mcam_setup_vb2(cam);
  694. if (ret)
  695. goto out;
  696. mcam_ctlr_power_up(cam);
  697. __mcam_cam_reset(cam);
  698. mcam_set_config_needed(cam, 1);
  699. }
  700. (cam->users)++;
  701. out:
  702. mutex_unlock(&cam->s_mutex);
  703. return ret;
  704. }
  705. static int mcam_v4l_release(struct file *filp)
  706. {
  707. struct mcam_camera *cam = filp->private_data;
  708. mutex_lock(&cam->s_mutex);
  709. (cam->users)--;
  710. if (filp == cam->owner) {
  711. mcam_ctlr_stop_dma(cam);
  712. cam->owner = NULL;
  713. }
  714. if (cam->users == 0) {
  715. mcam_cleanup_vb2(cam);
  716. mcam_ctlr_power_down(cam);
  717. if (alloc_bufs_at_read)
  718. mcam_free_dma_bufs(cam);
  719. }
  720. mutex_unlock(&cam->s_mutex);
  721. return 0;
  722. }
  723. static unsigned int mcam_v4l_poll(struct file *filp,
  724. struct poll_table_struct *pt)
  725. {
  726. struct mcam_camera *cam = filp->private_data;
  727. int ret;
  728. mutex_lock(&cam->s_mutex);
  729. ret = vb2_poll(&cam->vb_queue, filp, pt);
  730. mutex_unlock(&cam->s_mutex);
  731. return ret;
  732. }
  733. static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
  734. struct v4l2_queryctrl *qc)
  735. {
  736. struct mcam_camera *cam = priv;
  737. int ret;
  738. mutex_lock(&cam->s_mutex);
  739. ret = sensor_call(cam, core, queryctrl, qc);
  740. mutex_unlock(&cam->s_mutex);
  741. return ret;
  742. }
  743. static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
  744. struct v4l2_control *ctrl)
  745. {
  746. struct mcam_camera *cam = priv;
  747. int ret;
  748. mutex_lock(&cam->s_mutex);
  749. ret = sensor_call(cam, core, g_ctrl, ctrl);
  750. mutex_unlock(&cam->s_mutex);
  751. return ret;
  752. }
  753. static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
  754. struct v4l2_control *ctrl)
  755. {
  756. struct mcam_camera *cam = priv;
  757. int ret;
  758. mutex_lock(&cam->s_mutex);
  759. ret = sensor_call(cam, core, s_ctrl, ctrl);
  760. mutex_unlock(&cam->s_mutex);
  761. return ret;
  762. }
  763. static int mcam_vidioc_querycap(struct file *file, void *priv,
  764. struct v4l2_capability *cap)
  765. {
  766. strcpy(cap->driver, "marvell_ccic");
  767. strcpy(cap->card, "marvell_ccic");
  768. cap->version = 1;
  769. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  770. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  771. return 0;
  772. }
  773. /*
  774. * The default format we use until somebody says otherwise.
  775. */
  776. static const struct v4l2_pix_format mcam_def_pix_format = {
  777. .width = VGA_WIDTH,
  778. .height = VGA_HEIGHT,
  779. .pixelformat = V4L2_PIX_FMT_YUYV,
  780. .field = V4L2_FIELD_NONE,
  781. .bytesperline = VGA_WIDTH*2,
  782. .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
  783. };
  784. static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
  785. V4L2_MBUS_FMT_YUYV8_2X8;
  786. static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
  787. void *priv, struct v4l2_fmtdesc *fmt)
  788. {
  789. if (fmt->index >= N_MCAM_FMTS)
  790. return -EINVAL;
  791. strlcpy(fmt->description, mcam_formats[fmt->index].desc,
  792. sizeof(fmt->description));
  793. fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
  794. return 0;
  795. }
  796. static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  797. struct v4l2_format *fmt)
  798. {
  799. struct mcam_camera *cam = priv;
  800. struct mcam_format_struct *f;
  801. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  802. struct v4l2_mbus_framefmt mbus_fmt;
  803. int ret;
  804. f = mcam_find_format(pix->pixelformat);
  805. pix->pixelformat = f->pixelformat;
  806. v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
  807. mutex_lock(&cam->s_mutex);
  808. ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
  809. mutex_unlock(&cam->s_mutex);
  810. v4l2_fill_pix_format(pix, &mbus_fmt);
  811. pix->bytesperline = pix->width * f->bpp;
  812. pix->sizeimage = pix->height * pix->bytesperline;
  813. return ret;
  814. }
  815. static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  816. struct v4l2_format *fmt)
  817. {
  818. struct mcam_camera *cam = priv;
  819. struct mcam_format_struct *f;
  820. int ret;
  821. /*
  822. * Can't do anything if the device is not idle
  823. * Also can't if there are streaming buffers in place.
  824. */
  825. if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
  826. return -EBUSY;
  827. f = mcam_find_format(fmt->fmt.pix.pixelformat);
  828. /*
  829. * See if the formatting works in principle.
  830. */
  831. ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
  832. if (ret)
  833. return ret;
  834. /*
  835. * Now we start to change things for real, so let's do it
  836. * under lock.
  837. */
  838. mutex_lock(&cam->s_mutex);
  839. cam->pix_format = fmt->fmt.pix;
  840. cam->mbus_code = f->mbus_code;
  841. /*
  842. * Make sure we have appropriate DMA buffers.
  843. */
  844. ret = -ENOMEM;
  845. if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
  846. mcam_free_dma_bufs(cam);
  847. if (cam->nbufs == 0) {
  848. if (mcam_alloc_dma_bufs(cam, 0))
  849. goto out;
  850. }
  851. /*
  852. * It looks like this might work, so let's program the sensor.
  853. */
  854. ret = mcam_cam_configure(cam);
  855. if (!ret)
  856. ret = mcam_ctlr_configure(cam);
  857. out:
  858. mutex_unlock(&cam->s_mutex);
  859. return ret;
  860. }
  861. /*
  862. * Return our stored notion of how the camera is/should be configured.
  863. * The V4l2 spec wants us to be smarter, and actually get this from
  864. * the camera (and not mess with it at open time). Someday.
  865. */
  866. static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  867. struct v4l2_format *f)
  868. {
  869. struct mcam_camera *cam = priv;
  870. f->fmt.pix = cam->pix_format;
  871. return 0;
  872. }
  873. /*
  874. * We only have one input - the sensor - so minimize the nonsense here.
  875. */
  876. static int mcam_vidioc_enum_input(struct file *filp, void *priv,
  877. struct v4l2_input *input)
  878. {
  879. if (input->index != 0)
  880. return -EINVAL;
  881. input->type = V4L2_INPUT_TYPE_CAMERA;
  882. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  883. strcpy(input->name, "Camera");
  884. return 0;
  885. }
  886. static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  887. {
  888. *i = 0;
  889. return 0;
  890. }
  891. static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  892. {
  893. if (i != 0)
  894. return -EINVAL;
  895. return 0;
  896. }
  897. /* from vivi.c */
  898. static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  899. {
  900. return 0;
  901. }
  902. /*
  903. * G/S_PARM. Most of this is done by the sensor, but we are
  904. * the level which controls the number of read buffers.
  905. */
  906. static int mcam_vidioc_g_parm(struct file *filp, void *priv,
  907. struct v4l2_streamparm *parms)
  908. {
  909. struct mcam_camera *cam = priv;
  910. int ret;
  911. mutex_lock(&cam->s_mutex);
  912. ret = sensor_call(cam, video, g_parm, parms);
  913. mutex_unlock(&cam->s_mutex);
  914. parms->parm.capture.readbuffers = n_dma_bufs;
  915. return ret;
  916. }
  917. static int mcam_vidioc_s_parm(struct file *filp, void *priv,
  918. struct v4l2_streamparm *parms)
  919. {
  920. struct mcam_camera *cam = priv;
  921. int ret;
  922. mutex_lock(&cam->s_mutex);
  923. ret = sensor_call(cam, video, s_parm, parms);
  924. mutex_unlock(&cam->s_mutex);
  925. parms->parm.capture.readbuffers = n_dma_bufs;
  926. return ret;
  927. }
  928. static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
  929. struct v4l2_dbg_chip_ident *chip)
  930. {
  931. struct mcam_camera *cam = priv;
  932. chip->ident = V4L2_IDENT_NONE;
  933. chip->revision = 0;
  934. if (v4l2_chip_match_host(&chip->match)) {
  935. chip->ident = cam->chip_id;
  936. return 0;
  937. }
  938. return sensor_call(cam, core, g_chip_ident, chip);
  939. }
  940. static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
  941. struct v4l2_frmsizeenum *sizes)
  942. {
  943. struct mcam_camera *cam = priv;
  944. int ret;
  945. mutex_lock(&cam->s_mutex);
  946. ret = sensor_call(cam, video, enum_framesizes, sizes);
  947. mutex_unlock(&cam->s_mutex);
  948. return ret;
  949. }
  950. static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
  951. struct v4l2_frmivalenum *interval)
  952. {
  953. struct mcam_camera *cam = priv;
  954. int ret;
  955. mutex_lock(&cam->s_mutex);
  956. ret = sensor_call(cam, video, enum_frameintervals, interval);
  957. mutex_unlock(&cam->s_mutex);
  958. return ret;
  959. }
  960. #ifdef CONFIG_VIDEO_ADV_DEBUG
  961. static int mcam_vidioc_g_register(struct file *file, void *priv,
  962. struct v4l2_dbg_register *reg)
  963. {
  964. struct mcam_camera *cam = priv;
  965. if (v4l2_chip_match_host(&reg->match)) {
  966. reg->val = mcam_reg_read(cam, reg->reg);
  967. reg->size = 4;
  968. return 0;
  969. }
  970. return sensor_call(cam, core, g_register, reg);
  971. }
  972. static int mcam_vidioc_s_register(struct file *file, void *priv,
  973. struct v4l2_dbg_register *reg)
  974. {
  975. struct mcam_camera *cam = priv;
  976. if (v4l2_chip_match_host(&reg->match)) {
  977. mcam_reg_write(cam, reg->reg, reg->val);
  978. return 0;
  979. }
  980. return sensor_call(cam, core, s_register, reg);
  981. }
  982. #endif
  983. /*
  984. * This template device holds all of those v4l2 methods; we
  985. * clone it for specific real devices.
  986. */
  987. static const struct v4l2_file_operations mcam_v4l_fops = {
  988. .owner = THIS_MODULE,
  989. .open = mcam_v4l_open,
  990. .release = mcam_v4l_release,
  991. .read = mcam_v4l_read,
  992. .poll = mcam_v4l_poll,
  993. .mmap = mcam_v4l_mmap,
  994. .unlocked_ioctl = video_ioctl2,
  995. };
  996. static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
  997. .vidioc_querycap = mcam_vidioc_querycap,
  998. .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
  999. .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
  1000. .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
  1001. .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
  1002. .vidioc_enum_input = mcam_vidioc_enum_input,
  1003. .vidioc_g_input = mcam_vidioc_g_input,
  1004. .vidioc_s_input = mcam_vidioc_s_input,
  1005. .vidioc_s_std = mcam_vidioc_s_std,
  1006. .vidioc_reqbufs = mcam_vidioc_reqbufs,
  1007. .vidioc_querybuf = mcam_vidioc_querybuf,
  1008. .vidioc_qbuf = mcam_vidioc_qbuf,
  1009. .vidioc_dqbuf = mcam_vidioc_dqbuf,
  1010. .vidioc_streamon = mcam_vidioc_streamon,
  1011. .vidioc_streamoff = mcam_vidioc_streamoff,
  1012. .vidioc_queryctrl = mcam_vidioc_queryctrl,
  1013. .vidioc_g_ctrl = mcam_vidioc_g_ctrl,
  1014. .vidioc_s_ctrl = mcam_vidioc_s_ctrl,
  1015. .vidioc_g_parm = mcam_vidioc_g_parm,
  1016. .vidioc_s_parm = mcam_vidioc_s_parm,
  1017. .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
  1018. .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
  1019. .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident,
  1020. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1021. .vidioc_g_register = mcam_vidioc_g_register,
  1022. .vidioc_s_register = mcam_vidioc_s_register,
  1023. #endif
  1024. };
  1025. static struct video_device mcam_v4l_template = {
  1026. .name = "mcam",
  1027. .tvnorms = V4L2_STD_NTSC_M,
  1028. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1029. .fops = &mcam_v4l_fops,
  1030. .ioctl_ops = &mcam_v4l_ioctl_ops,
  1031. .release = video_device_release_empty,
  1032. };
  1033. /* ---------------------------------------------------------------------- */
  1034. /*
  1035. * Interrupt handler stuff
  1036. */
  1037. static void mcam_frame_tasklet(unsigned long data)
  1038. {
  1039. struct mcam_camera *cam = (struct mcam_camera *) data;
  1040. int i;
  1041. unsigned long flags;
  1042. struct mcam_vb_buffer *buf;
  1043. spin_lock_irqsave(&cam->dev_lock, flags);
  1044. for (i = 0; i < cam->nbufs; i++) {
  1045. int bufno = cam->next_buf;
  1046. if (cam->state != S_STREAMING || bufno < 0)
  1047. break; /* I/O got stopped */
  1048. if (++(cam->next_buf) >= cam->nbufs)
  1049. cam->next_buf = 0;
  1050. if (!test_bit(bufno, &cam->flags))
  1051. continue;
  1052. if (list_empty(&cam->buffers))
  1053. break; /* Leave it valid, hope for better later */
  1054. clear_bit(bufno, &cam->flags);
  1055. buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
  1056. queue);
  1057. list_del_init(&buf->queue);
  1058. /*
  1059. * Drop the lock during the big copy. This *should* be safe...
  1060. */
  1061. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1062. memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
  1063. cam->pix_format.sizeimage);
  1064. buf->vb_buf.v4l2_buf.bytesused = cam->pix_format.sizeimage;
  1065. buf->vb_buf.v4l2_buf.sequence = cam->buf_seq[bufno];
  1066. buf->vb_buf.v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1067. buf->vb_buf.v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
  1068. vb2_set_plane_payload(&buf->vb_buf, 0,
  1069. cam->pix_format.sizeimage);
  1070. vb2_buffer_done(&buf->vb_buf, VB2_BUF_STATE_DONE);
  1071. spin_lock_irqsave(&cam->dev_lock, flags);
  1072. }
  1073. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1074. }
  1075. static void mcam_frame_complete(struct mcam_camera *cam, int frame)
  1076. {
  1077. /*
  1078. * Basic frame housekeeping.
  1079. */
  1080. set_bit(frame, &cam->flags);
  1081. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1082. if (cam->next_buf < 0)
  1083. cam->next_buf = frame;
  1084. cam->buf_seq[frame] = ++(cam->sequence);
  1085. switch (cam->state) {
  1086. /*
  1087. * For the streaming case, we defer the real work to the
  1088. * camera tasklet.
  1089. *
  1090. * FIXME: if the application is not consuming the buffers,
  1091. * we should eventually put things on hold and restart in
  1092. * vidioc_dqbuf().
  1093. */
  1094. case S_STREAMING:
  1095. tasklet_schedule(&cam->s_tasklet);
  1096. break;
  1097. default:
  1098. cam_err(cam, "Frame interrupt in non-operational state\n");
  1099. break;
  1100. }
  1101. }
  1102. int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
  1103. {
  1104. unsigned int frame, handled = 0;
  1105. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1106. /*
  1107. * Handle any frame completions. There really should
  1108. * not be more than one of these, or we have fallen
  1109. * far behind.
  1110. */
  1111. for (frame = 0; frame < cam->nbufs; frame++)
  1112. if (irqs & (IRQ_EOF0 << frame)) {
  1113. mcam_frame_complete(cam, frame);
  1114. handled = 1;
  1115. }
  1116. /*
  1117. * If a frame starts, note that we have DMA active. This
  1118. * code assumes that we won't get multiple frame interrupts
  1119. * at once; may want to rethink that.
  1120. */
  1121. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
  1122. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1123. handled = 1;
  1124. }
  1125. return handled;
  1126. }
  1127. /*
  1128. * Registration and such.
  1129. */
  1130. static struct ov7670_config sensor_cfg = {
  1131. /*
  1132. * Exclude QCIF mode, because it only captures a tiny portion
  1133. * of the sensor FOV
  1134. */
  1135. .min_width = 320,
  1136. .min_height = 240,
  1137. };
  1138. int mccic_register(struct mcam_camera *cam)
  1139. {
  1140. struct i2c_board_info ov7670_info = {
  1141. .type = "ov7670",
  1142. .addr = 0x42 >> 1,
  1143. .platform_data = &sensor_cfg,
  1144. };
  1145. int ret;
  1146. /*
  1147. * Register with V4L
  1148. */
  1149. ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
  1150. if (ret)
  1151. return ret;
  1152. mutex_init(&cam->s_mutex);
  1153. cam->state = S_NOTREADY;
  1154. mcam_set_config_needed(cam, 1);
  1155. cam->pix_format = mcam_def_pix_format;
  1156. cam->mbus_code = mcam_def_mbus_code;
  1157. INIT_LIST_HEAD(&cam->dev_list);
  1158. INIT_LIST_HEAD(&cam->buffers);
  1159. tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam);
  1160. mcam_ctlr_init(cam);
  1161. /*
  1162. * Try to find the sensor.
  1163. */
  1164. sensor_cfg.clock_speed = cam->clock_speed;
  1165. sensor_cfg.use_smbus = cam->use_smbus;
  1166. cam->sensor_addr = ov7670_info.addr;
  1167. cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
  1168. cam->i2c_adapter, &ov7670_info, NULL);
  1169. if (cam->sensor == NULL) {
  1170. ret = -ENODEV;
  1171. goto out_unregister;
  1172. }
  1173. ret = mcam_cam_init(cam);
  1174. if (ret)
  1175. goto out_unregister;
  1176. /*
  1177. * Get the v4l2 setup done.
  1178. */
  1179. mutex_lock(&cam->s_mutex);
  1180. cam->vdev = mcam_v4l_template;
  1181. cam->vdev.debug = 0;
  1182. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1183. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1184. if (ret)
  1185. goto out;
  1186. video_set_drvdata(&cam->vdev, cam);
  1187. /*
  1188. * If so requested, try to get our DMA buffers now.
  1189. */
  1190. if (!alloc_bufs_at_read) {
  1191. if (mcam_alloc_dma_bufs(cam, 1))
  1192. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1193. " will try again later.");
  1194. }
  1195. out:
  1196. mutex_unlock(&cam->s_mutex);
  1197. return ret;
  1198. out_unregister:
  1199. v4l2_device_unregister(&cam->v4l2_dev);
  1200. return ret;
  1201. }
  1202. void mccic_shutdown(struct mcam_camera *cam)
  1203. {
  1204. /*
  1205. * If we have no users (and we really, really should have no
  1206. * users) the device will already be powered down. Trying to
  1207. * take it down again will wedge the machine, which is frowned
  1208. * upon.
  1209. */
  1210. if (cam->users > 0) {
  1211. cam_warn(cam, "Removing a device with users!\n");
  1212. mcam_ctlr_power_down(cam);
  1213. }
  1214. vb2_queue_release(&cam->vb_queue);
  1215. mcam_free_dma_bufs(cam);
  1216. video_unregister_device(&cam->vdev);
  1217. v4l2_device_unregister(&cam->v4l2_dev);
  1218. }
  1219. /*
  1220. * Power management
  1221. */
  1222. #ifdef CONFIG_PM
  1223. void mccic_suspend(struct mcam_camera *cam)
  1224. {
  1225. enum mcam_state cstate = cam->state;
  1226. mcam_ctlr_stop_dma(cam);
  1227. mcam_ctlr_power_down(cam);
  1228. cam->state = cstate;
  1229. }
  1230. int mccic_resume(struct mcam_camera *cam)
  1231. {
  1232. int ret = 0;
  1233. mutex_lock(&cam->s_mutex);
  1234. if (cam->users > 0) {
  1235. mcam_ctlr_power_up(cam);
  1236. __mcam_cam_reset(cam);
  1237. } else {
  1238. mcam_ctlr_power_down(cam);
  1239. }
  1240. mutex_unlock(&cam->s_mutex);
  1241. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1242. if (cam->state == S_STREAMING)
  1243. ret = mcam_read_setup(cam, cam->state);
  1244. return ret;
  1245. }
  1246. #endif /* CONFIG_PM */