mcam-core.c 35 KB

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