via-camera.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. * Driver for the VIA Chrome integrated camera controller.
  3. *
  4. * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
  5. * Distributable under the terms of the GNU General Public License, version 2
  6. *
  7. * This work was supported by the One Laptop Per Child project
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/list.h>
  13. #include <linux/pci.h>
  14. #include <linux/gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/videodev2.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-ioctl.h>
  21. #include <media/v4l2-chip-ident.h>
  22. #include <media/videobuf-dma-sg.h>
  23. #include <linux/device.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/pm_qos_params.h>
  27. #include <linux/via-core.h>
  28. #include <linux/via-gpio.h>
  29. #include <linux/via_i2c.h>
  30. #include "via-camera.h"
  31. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  32. MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
  33. MODULE_LICENSE("GPL");
  34. static int flip_image;
  35. module_param(flip_image, bool, 0444);
  36. MODULE_PARM_DESC(flip_image,
  37. "If set, the sensor will be instructed to flip the image "
  38. "vertically.");
  39. #ifdef CONFIG_OLPC_XO_1_5
  40. static int override_serial;
  41. module_param(override_serial, bool, 0444);
  42. MODULE_PARM_DESC(override_serial,
  43. "The camera driver will normally refuse to load if "
  44. "the XO 1.5 serial port is enabled. Set this option "
  45. "to force the issue.");
  46. #endif
  47. /*
  48. * Basic window sizes.
  49. */
  50. #define VGA_WIDTH 640
  51. #define VGA_HEIGHT 480
  52. #define QCIF_WIDTH 176
  53. #define QCIF_HEIGHT 144
  54. /*
  55. * The structure describing our camera.
  56. */
  57. enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
  58. struct via_camera {
  59. struct v4l2_device v4l2_dev;
  60. struct video_device vdev;
  61. struct v4l2_subdev *sensor;
  62. struct platform_device *platdev;
  63. struct viafb_dev *viadev;
  64. struct mutex lock;
  65. enum viacam_opstate opstate;
  66. unsigned long flags;
  67. struct pm_qos_request_list qos_request;
  68. /*
  69. * GPIO info for power/reset management
  70. */
  71. int power_gpio;
  72. int reset_gpio;
  73. /*
  74. * I/O memory stuff.
  75. */
  76. void __iomem *mmio; /* Where the registers live */
  77. void __iomem *fbmem; /* Frame buffer memory */
  78. u32 fb_offset; /* Reserved memory offset (FB) */
  79. /*
  80. * Capture buffers and related. The controller supports
  81. * up to three, so that's what we have here. These buffers
  82. * live in frame buffer memory, so we don't call them "DMA".
  83. */
  84. unsigned int cb_offsets[3]; /* offsets into fb mem */
  85. u8 *cb_addrs[3]; /* Kernel-space addresses */
  86. int n_cap_bufs; /* How many are we using? */
  87. int next_buf;
  88. struct videobuf_queue vb_queue;
  89. struct list_head buffer_queue; /* prot. by reg_lock */
  90. /*
  91. * User tracking.
  92. */
  93. int users;
  94. struct file *owner;
  95. /*
  96. * Video format information. sensor_format is kept in a form
  97. * that we can use to pass to the sensor. We always run the
  98. * sensor in VGA resolution, though, and let the controller
  99. * downscale things if need be. So we keep the "real*
  100. * dimensions separately.
  101. */
  102. struct v4l2_pix_format sensor_format;
  103. struct v4l2_pix_format user_format;
  104. enum v4l2_mbus_pixelcode mbus_code;
  105. };
  106. /*
  107. * Yes, this is a hack, but there's only going to be one of these
  108. * on any system we know of.
  109. */
  110. static struct via_camera *via_cam_info;
  111. /*
  112. * Flag values, manipulated with bitops
  113. */
  114. #define CF_DMA_ACTIVE 0 /* A frame is incoming */
  115. #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
  116. /*
  117. * Nasty ugly v4l2 boilerplate.
  118. */
  119. #define sensor_call(cam, optype, func, args...) \
  120. v4l2_subdev_call(cam->sensor, optype, func, ##args)
  121. /*
  122. * Debugging and related.
  123. */
  124. #define cam_err(cam, fmt, arg...) \
  125. dev_err(&(cam)->platdev->dev, fmt, ##arg);
  126. #define cam_warn(cam, fmt, arg...) \
  127. dev_warn(&(cam)->platdev->dev, fmt, ##arg);
  128. #define cam_dbg(cam, fmt, arg...) \
  129. dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
  130. /*
  131. * Format handling. This is ripped almost directly from Hans's changes
  132. * to cafe_ccic.c. It's a little unfortunate; until this change, we
  133. * didn't need to know anything about the format except its byte depth;
  134. * now this information must be managed at this level too.
  135. */
  136. static struct via_format {
  137. __u8 *desc;
  138. __u32 pixelformat;
  139. int bpp; /* Bytes per pixel */
  140. enum v4l2_mbus_pixelcode mbus_code;
  141. } via_formats[] = {
  142. {
  143. .desc = "YUYV 4:2:2",
  144. .pixelformat = V4L2_PIX_FMT_YUYV,
  145. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  146. .bpp = 2,
  147. },
  148. {
  149. .desc = "RGB 565",
  150. .pixelformat = V4L2_PIX_FMT_RGB565,
  151. .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  152. .bpp = 2,
  153. },
  154. /* RGB444 and Bayer should be doable, but have never been
  155. tested with this driver. */
  156. };
  157. #define N_VIA_FMTS ARRAY_SIZE(via_formats)
  158. static struct via_format *via_find_format(u32 pixelformat)
  159. {
  160. unsigned i;
  161. for (i = 0; i < N_VIA_FMTS; i++)
  162. if (via_formats[i].pixelformat == pixelformat)
  163. return via_formats + i;
  164. /* Not found? Then return the first format. */
  165. return via_formats;
  166. }
  167. /*--------------------------------------------------------------------------*/
  168. /*
  169. * Sensor power/reset management. This piece is OLPC-specific for
  170. * sure; other configurations will have things connected differently.
  171. */
  172. static int via_sensor_power_setup(struct via_camera *cam)
  173. {
  174. int ret;
  175. cam->power_gpio = viafb_gpio_lookup("VGPIO3");
  176. cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
  177. if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
  178. dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
  179. return -EINVAL;
  180. }
  181. ret = gpio_request(cam->power_gpio, "viafb-camera");
  182. if (ret) {
  183. dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
  184. return ret;
  185. }
  186. ret = gpio_request(cam->reset_gpio, "viafb-camera");
  187. if (ret) {
  188. dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
  189. gpio_free(cam->power_gpio);
  190. return ret;
  191. }
  192. gpio_direction_output(cam->power_gpio, 0);
  193. gpio_direction_output(cam->reset_gpio, 0);
  194. return 0;
  195. }
  196. /*
  197. * Power up the sensor and perform the reset dance.
  198. */
  199. static void via_sensor_power_up(struct via_camera *cam)
  200. {
  201. gpio_set_value(cam->power_gpio, 1);
  202. gpio_set_value(cam->reset_gpio, 0);
  203. msleep(20); /* Probably excessive */
  204. gpio_set_value(cam->reset_gpio, 1);
  205. msleep(20);
  206. }
  207. static void via_sensor_power_down(struct via_camera *cam)
  208. {
  209. gpio_set_value(cam->power_gpio, 0);
  210. gpio_set_value(cam->reset_gpio, 0);
  211. }
  212. static void via_sensor_power_release(struct via_camera *cam)
  213. {
  214. via_sensor_power_down(cam);
  215. gpio_free(cam->power_gpio);
  216. gpio_free(cam->reset_gpio);
  217. }
  218. /* --------------------------------------------------------------------------*/
  219. /* Sensor ops */
  220. /*
  221. * Manage the ov7670 "flip" bit, which needs special help.
  222. */
  223. static int viacam_set_flip(struct via_camera *cam)
  224. {
  225. struct v4l2_control ctrl;
  226. memset(&ctrl, 0, sizeof(ctrl));
  227. ctrl.id = V4L2_CID_VFLIP;
  228. ctrl.value = flip_image;
  229. return sensor_call(cam, core, s_ctrl, &ctrl);
  230. }
  231. /*
  232. * Configure the sensor. It's up to the caller to ensure
  233. * that the camera is in the correct operating state.
  234. */
  235. static int viacam_configure_sensor(struct via_camera *cam)
  236. {
  237. struct v4l2_mbus_framefmt mbus_fmt;
  238. int ret;
  239. v4l2_fill_mbus_format(&mbus_fmt, &cam->sensor_format, cam->mbus_code);
  240. ret = sensor_call(cam, core, init, 0);
  241. if (ret == 0)
  242. ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
  243. /*
  244. * OV7670 does weird things if flip is set *before* format...
  245. */
  246. if (ret == 0)
  247. ret = viacam_set_flip(cam);
  248. return ret;
  249. }
  250. /* --------------------------------------------------------------------------*/
  251. /*
  252. * Some simple register accessors; they assume that the lock is held.
  253. *
  254. * Should we want to support the second capture engine, we could
  255. * hide the register difference by adding 0x1000 to registers in the
  256. * 0x300-350 range.
  257. */
  258. static inline void viacam_write_reg(struct via_camera *cam,
  259. int reg, int value)
  260. {
  261. iowrite32(value, cam->mmio + reg);
  262. }
  263. static inline int viacam_read_reg(struct via_camera *cam, int reg)
  264. {
  265. return ioread32(cam->mmio + reg);
  266. }
  267. static inline void viacam_write_reg_mask(struct via_camera *cam,
  268. int reg, int value, int mask)
  269. {
  270. int tmp = viacam_read_reg(cam, reg);
  271. tmp = (tmp & ~mask) | (value & mask);
  272. viacam_write_reg(cam, reg, tmp);
  273. }
  274. /* --------------------------------------------------------------------------*/
  275. /* Interrupt management and handling */
  276. static irqreturn_t viacam_quick_irq(int irq, void *data)
  277. {
  278. struct via_camera *cam = data;
  279. irqreturn_t ret = IRQ_NONE;
  280. int icv;
  281. /*
  282. * All we do here is to clear the interrupts and tell
  283. * the handler thread to wake up.
  284. */
  285. spin_lock(&cam->viadev->reg_lock);
  286. icv = viacam_read_reg(cam, VCR_INTCTRL);
  287. if (icv & VCR_IC_EAV) {
  288. icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
  289. viacam_write_reg(cam, VCR_INTCTRL, icv);
  290. ret = IRQ_WAKE_THREAD;
  291. }
  292. spin_unlock(&cam->viadev->reg_lock);
  293. return ret;
  294. }
  295. /*
  296. * Find the next videobuf buffer which has somebody waiting on it.
  297. */
  298. static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
  299. {
  300. unsigned long flags;
  301. struct videobuf_buffer *buf = NULL;
  302. spin_lock_irqsave(&cam->viadev->reg_lock, flags);
  303. if (cam->opstate != S_RUNNING)
  304. goto out;
  305. if (list_empty(&cam->buffer_queue))
  306. goto out;
  307. buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
  308. if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
  309. buf = NULL;
  310. goto out;
  311. }
  312. list_del(&buf->queue);
  313. buf->state = VIDEOBUF_ACTIVE;
  314. out:
  315. spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
  316. return buf;
  317. }
  318. /*
  319. * The threaded IRQ handler.
  320. */
  321. static irqreturn_t viacam_irq(int irq, void *data)
  322. {
  323. int bufn;
  324. struct videobuf_buffer *vb;
  325. struct via_camera *cam = data;
  326. struct videobuf_dmabuf *vdma;
  327. /*
  328. * If there is no place to put the data frame, don't bother
  329. * with anything else.
  330. */
  331. vb = viacam_next_buffer(cam);
  332. if (vb == NULL)
  333. goto done;
  334. /*
  335. * Figure out which buffer we just completed.
  336. */
  337. bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
  338. bufn -= 1;
  339. if (bufn < 0)
  340. bufn = cam->n_cap_bufs - 1;
  341. /*
  342. * Copy over the data and let any waiters know.
  343. */
  344. vdma = videobuf_to_dma(vb);
  345. viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
  346. vb->state = VIDEOBUF_DONE;
  347. vb->size = cam->user_format.sizeimage;
  348. wake_up(&vb->done);
  349. done:
  350. return IRQ_HANDLED;
  351. }
  352. /*
  353. * These functions must mess around with the general interrupt
  354. * control register, which is relevant to much more than just the
  355. * camera. Nothing else uses interrupts, though, as of this writing.
  356. * Should that situation change, we'll have to improve support at
  357. * the via-core level.
  358. */
  359. static void viacam_int_enable(struct via_camera *cam)
  360. {
  361. viacam_write_reg(cam, VCR_INTCTRL,
  362. VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
  363. viafb_irq_enable(VDE_I_C0AVEN);
  364. }
  365. static void viacam_int_disable(struct via_camera *cam)
  366. {
  367. viafb_irq_disable(VDE_I_C0AVEN);
  368. viacam_write_reg(cam, VCR_INTCTRL, 0);
  369. }
  370. /* --------------------------------------------------------------------------*/
  371. /* Controller operations */
  372. /*
  373. * Set up our capture buffers in framebuffer memory.
  374. */
  375. static int viacam_ctlr_cbufs(struct via_camera *cam)
  376. {
  377. int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
  378. int i;
  379. unsigned int offset;
  380. /*
  381. * See how many buffers we can work with.
  382. */
  383. if (nbuf >= 3) {
  384. cam->n_cap_bufs = 3;
  385. viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
  386. VCR_CI_3BUFS);
  387. } else if (nbuf == 2) {
  388. cam->n_cap_bufs = 2;
  389. viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
  390. } else {
  391. cam_warn(cam, "Insufficient frame buffer memory\n");
  392. return -ENOMEM;
  393. }
  394. /*
  395. * Set them up.
  396. */
  397. offset = cam->fb_offset;
  398. for (i = 0; i < cam->n_cap_bufs; i++) {
  399. cam->cb_offsets[i] = offset;
  400. cam->cb_addrs[i] = cam->fbmem + offset;
  401. viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
  402. offset += cam->sensor_format.sizeimage;
  403. }
  404. return 0;
  405. }
  406. /*
  407. * Set the scaling register for downscaling the image.
  408. *
  409. * This register works like this... Vertical scaling is enabled
  410. * by bit 26; if that bit is set, downscaling is controlled by the
  411. * value in bits 16:25. Those bits are divided by 1024 to get
  412. * the scaling factor; setting just bit 25 thus cuts the height
  413. * in half.
  414. *
  415. * Horizontal scaling works about the same, but it's enabled by
  416. * bit 11, with bits 0:10 giving the numerator of a fraction
  417. * (over 2048) for the scaling value.
  418. *
  419. * This function is naive in that, if the user departs from
  420. * the 3x4 VGA scaling factor, the image will distort. We
  421. * could work around that if it really seemed important.
  422. */
  423. static void viacam_set_scale(struct via_camera *cam)
  424. {
  425. unsigned int avscale;
  426. int sf;
  427. if (cam->user_format.width == VGA_WIDTH)
  428. avscale = 0;
  429. else {
  430. sf = (cam->user_format.width*2048)/VGA_WIDTH;
  431. avscale = VCR_AVS_HEN | sf;
  432. }
  433. if (cam->user_format.height < VGA_HEIGHT) {
  434. sf = (1024*cam->user_format.height)/VGA_HEIGHT;
  435. avscale |= VCR_AVS_VEN | (sf << 16);
  436. }
  437. viacam_write_reg(cam, VCR_AVSCALE, avscale);
  438. }
  439. /*
  440. * Configure image-related information into the capture engine.
  441. */
  442. static void viacam_ctlr_image(struct via_camera *cam)
  443. {
  444. int cicreg;
  445. /*
  446. * Disable clock before messing with stuff - from the via
  447. * sample driver.
  448. */
  449. viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
  450. /*
  451. * Set up the controller for VGA resolution, modulo magic
  452. * offsets from the via sample driver.
  453. */
  454. viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
  455. viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
  456. viacam_set_scale(cam);
  457. /*
  458. * Image size info.
  459. */
  460. viacam_write_reg(cam, VCR_MAXDATA,
  461. (cam->sensor_format.height << 16) |
  462. (cam->sensor_format.bytesperline >> 3));
  463. viacam_write_reg(cam, VCR_MAXVBI, 0);
  464. viacam_write_reg(cam, VCR_VSTRIDE,
  465. cam->user_format.bytesperline & VCR_VS_STRIDE);
  466. /*
  467. * Set up the capture interface control register,
  468. * everything but the "go" bit.
  469. *
  470. * The FIFO threshold is a bit of a magic number; 8 is what
  471. * VIA's sample code uses.
  472. */
  473. cicreg = VCR_CI_CLKEN |
  474. 0x08000000 | /* FIFO threshold */
  475. VCR_CI_FLDINV | /* OLPC-specific? */
  476. VCR_CI_VREFINV | /* OLPC-specific? */
  477. VCR_CI_DIBOTH | /* Capture both fields */
  478. VCR_CI_CCIR601_8;
  479. if (cam->n_cap_bufs == 3)
  480. cicreg |= VCR_CI_3BUFS;
  481. /*
  482. * YUV formats need different byte swapping than RGB.
  483. */
  484. if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
  485. cicreg |= VCR_CI_YUYV;
  486. else
  487. cicreg |= VCR_CI_UYVY;
  488. viacam_write_reg(cam, VCR_CAPINTC, cicreg);
  489. }
  490. static int viacam_config_controller(struct via_camera *cam)
  491. {
  492. int ret;
  493. unsigned long flags;
  494. spin_lock_irqsave(&cam->viadev->reg_lock, flags);
  495. ret = viacam_ctlr_cbufs(cam);
  496. if (!ret)
  497. viacam_ctlr_image(cam);
  498. spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
  499. clear_bit(CF_CONFIG_NEEDED, &cam->flags);
  500. return ret;
  501. }
  502. /*
  503. * Make it start grabbing data.
  504. */
  505. static void viacam_start_engine(struct via_camera *cam)
  506. {
  507. spin_lock_irq(&cam->viadev->reg_lock);
  508. cam->next_buf = 0;
  509. viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
  510. viacam_int_enable(cam);
  511. (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
  512. cam->opstate = S_RUNNING;
  513. spin_unlock_irq(&cam->viadev->reg_lock);
  514. }
  515. static void viacam_stop_engine(struct via_camera *cam)
  516. {
  517. spin_lock_irq(&cam->viadev->reg_lock);
  518. viacam_int_disable(cam);
  519. viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
  520. (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
  521. cam->opstate = S_IDLE;
  522. spin_unlock_irq(&cam->viadev->reg_lock);
  523. }
  524. /* --------------------------------------------------------------------------*/
  525. /* Videobuf callback ops */
  526. /*
  527. * buffer_setup. The purpose of this one would appear to be to tell
  528. * videobuf how big a single image is. It's also evidently up to us
  529. * to put some sort of limit on the maximum number of buffers allowed.
  530. */
  531. static int viacam_vb_buf_setup(struct videobuf_queue *q,
  532. unsigned int *count, unsigned int *size)
  533. {
  534. struct via_camera *cam = q->priv_data;
  535. *size = cam->user_format.sizeimage;
  536. if (*count == 0 || *count > 6) /* Arbitrary number */
  537. *count = 6;
  538. return 0;
  539. }
  540. /*
  541. * Prepare a buffer.
  542. */
  543. static int viacam_vb_buf_prepare(struct videobuf_queue *q,
  544. struct videobuf_buffer *vb, enum v4l2_field field)
  545. {
  546. struct via_camera *cam = q->priv_data;
  547. vb->size = cam->user_format.sizeimage;
  548. vb->width = cam->user_format.width; /* bytesperline???? */
  549. vb->height = cam->user_format.height;
  550. vb->field = field;
  551. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  552. int ret = videobuf_iolock(q, vb, NULL);
  553. if (ret)
  554. return ret;
  555. }
  556. vb->state = VIDEOBUF_PREPARED;
  557. return 0;
  558. }
  559. /*
  560. * We've got a buffer to put data into.
  561. *
  562. * FIXME: check for a running engine and valid buffers?
  563. */
  564. static void viacam_vb_buf_queue(struct videobuf_queue *q,
  565. struct videobuf_buffer *vb)
  566. {
  567. struct via_camera *cam = q->priv_data;
  568. /*
  569. * Note that videobuf holds the lock when it calls
  570. * us, so we need not (indeed, cannot) take it here.
  571. */
  572. vb->state = VIDEOBUF_QUEUED;
  573. list_add_tail(&vb->queue, &cam->buffer_queue);
  574. }
  575. /*
  576. * Free a buffer.
  577. */
  578. static void viacam_vb_buf_release(struct videobuf_queue *q,
  579. struct videobuf_buffer *vb)
  580. {
  581. struct via_camera *cam = q->priv_data;
  582. videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
  583. videobuf_dma_free(videobuf_to_dma(vb));
  584. vb->state = VIDEOBUF_NEEDS_INIT;
  585. }
  586. static const struct videobuf_queue_ops viacam_vb_ops = {
  587. .buf_setup = viacam_vb_buf_setup,
  588. .buf_prepare = viacam_vb_buf_prepare,
  589. .buf_queue = viacam_vb_buf_queue,
  590. .buf_release = viacam_vb_buf_release,
  591. };
  592. /* --------------------------------------------------------------------------*/
  593. /* File operations */
  594. static int viacam_open(struct file *filp)
  595. {
  596. struct via_camera *cam = video_drvdata(filp);
  597. filp->private_data = cam;
  598. /*
  599. * Note the new user. If this is the first one, we'll also
  600. * need to power up the sensor.
  601. */
  602. mutex_lock(&cam->lock);
  603. if (cam->users == 0) {
  604. int ret = viafb_request_dma();
  605. if (ret) {
  606. mutex_unlock(&cam->lock);
  607. return ret;
  608. }
  609. via_sensor_power_up(cam);
  610. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  611. /*
  612. * Hook into videobuf. Evidently this cannot fail.
  613. */
  614. videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
  615. &cam->platdev->dev, &cam->viadev->reg_lock,
  616. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  617. sizeof(struct videobuf_buffer), cam, NULL);
  618. }
  619. (cam->users)++;
  620. mutex_unlock(&cam->lock);
  621. return 0;
  622. }
  623. static int viacam_release(struct file *filp)
  624. {
  625. struct via_camera *cam = video_drvdata(filp);
  626. mutex_lock(&cam->lock);
  627. (cam->users)--;
  628. /*
  629. * If the "owner" is closing, shut down any ongoing
  630. * operations.
  631. */
  632. if (filp == cam->owner) {
  633. videobuf_stop(&cam->vb_queue);
  634. /*
  635. * We don't hold the spinlock here, but, if release()
  636. * is being called by the owner, nobody else will
  637. * be changing the state. And an extra stop would
  638. * not hurt anyway.
  639. */
  640. if (cam->opstate != S_IDLE)
  641. viacam_stop_engine(cam);
  642. cam->owner = NULL;
  643. }
  644. /*
  645. * Last one out needs to turn out the lights.
  646. */
  647. if (cam->users == 0) {
  648. videobuf_mmap_free(&cam->vb_queue);
  649. via_sensor_power_down(cam);
  650. viafb_release_dma();
  651. }
  652. mutex_unlock(&cam->lock);
  653. return 0;
  654. }
  655. /*
  656. * Read a frame from the device.
  657. */
  658. static ssize_t viacam_read(struct file *filp, char __user *buffer,
  659. size_t len, loff_t *pos)
  660. {
  661. struct via_camera *cam = video_drvdata(filp);
  662. int ret;
  663. mutex_lock(&cam->lock);
  664. /*
  665. * Enforce the V4l2 "only one owner gets to read data" rule.
  666. */
  667. if (cam->owner && cam->owner != filp) {
  668. ret = -EBUSY;
  669. goto out_unlock;
  670. }
  671. cam->owner = filp;
  672. /*
  673. * Do we need to configure the hardware?
  674. */
  675. if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
  676. ret = viacam_configure_sensor(cam);
  677. if (!ret)
  678. ret = viacam_config_controller(cam);
  679. if (ret)
  680. goto out_unlock;
  681. }
  682. /*
  683. * Fire up the capture engine, then have videobuf do
  684. * the heavy lifting. Someday it would be good to avoid
  685. * stopping and restarting the engine each time.
  686. */
  687. INIT_LIST_HEAD(&cam->buffer_queue);
  688. viacam_start_engine(cam);
  689. ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
  690. filp->f_flags & O_NONBLOCK);
  691. viacam_stop_engine(cam);
  692. /* videobuf_stop() ?? */
  693. out_unlock:
  694. mutex_unlock(&cam->lock);
  695. return ret;
  696. }
  697. static unsigned int viacam_poll(struct file *filp, struct poll_table_struct *pt)
  698. {
  699. struct via_camera *cam = video_drvdata(filp);
  700. return videobuf_poll_stream(filp, &cam->vb_queue, pt);
  701. }
  702. static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
  703. {
  704. struct via_camera *cam = video_drvdata(filp);
  705. return videobuf_mmap_mapper(&cam->vb_queue, vma);
  706. }
  707. static const struct v4l2_file_operations viacam_fops = {
  708. .owner = THIS_MODULE,
  709. .open = viacam_open,
  710. .release = viacam_release,
  711. .read = viacam_read,
  712. .poll = viacam_poll,
  713. .mmap = viacam_mmap,
  714. .unlocked_ioctl = video_ioctl2,
  715. };
  716. /*----------------------------------------------------------------------------*/
  717. /*
  718. * The long list of v4l2 ioctl ops
  719. */
  720. static int viacam_g_chip_ident(struct file *file, void *priv,
  721. struct v4l2_dbg_chip_ident *ident)
  722. {
  723. struct via_camera *cam = priv;
  724. ident->ident = V4L2_IDENT_NONE;
  725. ident->revision = 0;
  726. if (v4l2_chip_match_host(&ident->match)) {
  727. ident->ident = V4L2_IDENT_VIA_VX855;
  728. return 0;
  729. }
  730. return sensor_call(cam, core, g_chip_ident, ident);
  731. }
  732. /*
  733. * Control ops are passed through to the sensor.
  734. */
  735. static int viacam_queryctrl(struct file *filp, void *priv,
  736. struct v4l2_queryctrl *qc)
  737. {
  738. struct via_camera *cam = priv;
  739. int ret;
  740. mutex_lock(&cam->lock);
  741. ret = sensor_call(cam, core, queryctrl, qc);
  742. mutex_unlock(&cam->lock);
  743. return ret;
  744. }
  745. static int viacam_g_ctrl(struct file *filp, void *priv,
  746. struct v4l2_control *ctrl)
  747. {
  748. struct via_camera *cam = priv;
  749. int ret;
  750. mutex_lock(&cam->lock);
  751. ret = sensor_call(cam, core, g_ctrl, ctrl);
  752. mutex_unlock(&cam->lock);
  753. return ret;
  754. }
  755. static int viacam_s_ctrl(struct file *filp, void *priv,
  756. struct v4l2_control *ctrl)
  757. {
  758. struct via_camera *cam = priv;
  759. int ret;
  760. mutex_lock(&cam->lock);
  761. ret = sensor_call(cam, core, s_ctrl, ctrl);
  762. mutex_unlock(&cam->lock);
  763. return ret;
  764. }
  765. /*
  766. * Only one input.
  767. */
  768. static int viacam_enum_input(struct file *filp, void *priv,
  769. struct v4l2_input *input)
  770. {
  771. if (input->index != 0)
  772. return -EINVAL;
  773. input->type = V4L2_INPUT_TYPE_CAMERA;
  774. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  775. strcpy(input->name, "Camera");
  776. return 0;
  777. }
  778. static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
  779. {
  780. *i = 0;
  781. return 0;
  782. }
  783. static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
  784. {
  785. if (i != 0)
  786. return -EINVAL;
  787. return 0;
  788. }
  789. static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id *std)
  790. {
  791. return 0;
  792. }
  793. /*
  794. * Video format stuff. Here is our default format until
  795. * user space messes with things.
  796. */
  797. static const struct v4l2_pix_format viacam_def_pix_format = {
  798. .width = VGA_WIDTH,
  799. .height = VGA_HEIGHT,
  800. .pixelformat = V4L2_PIX_FMT_YUYV,
  801. .field = V4L2_FIELD_NONE,
  802. .bytesperline = VGA_WIDTH * 2,
  803. .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
  804. };
  805. static const enum v4l2_mbus_pixelcode via_def_mbus_code = V4L2_MBUS_FMT_YUYV8_2X8;
  806. static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
  807. struct v4l2_fmtdesc *fmt)
  808. {
  809. if (fmt->index >= N_VIA_FMTS)
  810. return -EINVAL;
  811. strlcpy(fmt->description, via_formats[fmt->index].desc,
  812. sizeof(fmt->description));
  813. fmt->pixelformat = via_formats[fmt->index].pixelformat;
  814. return 0;
  815. }
  816. /*
  817. * Figure out proper image dimensions, but always force the
  818. * sensor to VGA.
  819. */
  820. static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
  821. struct v4l2_pix_format *sensorfmt)
  822. {
  823. *sensorfmt = *userfmt;
  824. if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
  825. userfmt->width = QCIF_WIDTH;
  826. userfmt->height = QCIF_HEIGHT;
  827. }
  828. if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
  829. userfmt->width = VGA_WIDTH;
  830. userfmt->height = VGA_HEIGHT;
  831. }
  832. sensorfmt->width = VGA_WIDTH;
  833. sensorfmt->height = VGA_HEIGHT;
  834. }
  835. static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
  836. struct v4l2_pix_format *sensorfmt)
  837. {
  838. struct via_format *f = via_find_format(userfmt->pixelformat);
  839. sensorfmt->bytesperline = sensorfmt->width * f->bpp;
  840. sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
  841. userfmt->pixelformat = sensorfmt->pixelformat;
  842. userfmt->field = sensorfmt->field;
  843. userfmt->bytesperline = 2 * userfmt->width;
  844. userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
  845. }
  846. /*
  847. * The real work of figuring out a workable format.
  848. */
  849. static int viacam_do_try_fmt(struct via_camera *cam,
  850. struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
  851. {
  852. int ret;
  853. struct v4l2_mbus_framefmt mbus_fmt;
  854. struct via_format *f = via_find_format(upix->pixelformat);
  855. upix->pixelformat = f->pixelformat;
  856. viacam_fmt_pre(upix, spix);
  857. v4l2_fill_mbus_format(&mbus_fmt, upix, f->mbus_code);
  858. ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
  859. v4l2_fill_pix_format(spix, &mbus_fmt);
  860. viacam_fmt_post(upix, spix);
  861. return ret;
  862. }
  863. static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
  864. struct v4l2_format *fmt)
  865. {
  866. struct via_camera *cam = priv;
  867. struct v4l2_format sfmt;
  868. int ret;
  869. mutex_lock(&cam->lock);
  870. ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
  871. mutex_unlock(&cam->lock);
  872. return ret;
  873. }
  874. static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
  875. struct v4l2_format *fmt)
  876. {
  877. struct via_camera *cam = priv;
  878. mutex_lock(&cam->lock);
  879. fmt->fmt.pix = cam->user_format;
  880. mutex_unlock(&cam->lock);
  881. return 0;
  882. }
  883. static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
  884. struct v4l2_format *fmt)
  885. {
  886. struct via_camera *cam = priv;
  887. int ret;
  888. struct v4l2_format sfmt;
  889. struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
  890. /*
  891. * Camera must be idle or we can't mess with the
  892. * video setup.
  893. */
  894. mutex_lock(&cam->lock);
  895. if (cam->opstate != S_IDLE) {
  896. ret = -EBUSY;
  897. goto out;
  898. }
  899. /*
  900. * Let the sensor code look over and tweak the
  901. * requested formatting.
  902. */
  903. ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
  904. if (ret)
  905. goto out;
  906. /*
  907. * OK, let's commit to the new format.
  908. */
  909. cam->user_format = fmt->fmt.pix;
  910. cam->sensor_format = sfmt.fmt.pix;
  911. cam->mbus_code = f->mbus_code;
  912. ret = viacam_configure_sensor(cam);
  913. if (!ret)
  914. ret = viacam_config_controller(cam);
  915. out:
  916. mutex_unlock(&cam->lock);
  917. return ret;
  918. }
  919. static int viacam_querycap(struct file *filp, void *priv,
  920. struct v4l2_capability *cap)
  921. {
  922. strcpy(cap->driver, "via-camera");
  923. strcpy(cap->card, "via-camera");
  924. cap->version = 1;
  925. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  926. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  927. return 0;
  928. }
  929. /*
  930. * Streaming operations - pure videobuf stuff.
  931. */
  932. static int viacam_reqbufs(struct file *filp, void *priv,
  933. struct v4l2_requestbuffers *rb)
  934. {
  935. struct via_camera *cam = priv;
  936. return videobuf_reqbufs(&cam->vb_queue, rb);
  937. }
  938. static int viacam_querybuf(struct file *filp, void *priv,
  939. struct v4l2_buffer *buf)
  940. {
  941. struct via_camera *cam = priv;
  942. return videobuf_querybuf(&cam->vb_queue, buf);
  943. }
  944. static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
  945. {
  946. struct via_camera *cam = priv;
  947. return videobuf_qbuf(&cam->vb_queue, buf);
  948. }
  949. static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
  950. {
  951. struct via_camera *cam = priv;
  952. return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
  953. }
  954. static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
  955. {
  956. struct via_camera *cam = priv;
  957. int ret = 0;
  958. if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  959. return -EINVAL;
  960. mutex_lock(&cam->lock);
  961. if (cam->opstate != S_IDLE) {
  962. ret = -EBUSY;
  963. goto out;
  964. }
  965. /*
  966. * Enforce the V4l2 "only one owner gets to read data" rule.
  967. */
  968. if (cam->owner && cam->owner != filp) {
  969. ret = -EBUSY;
  970. goto out;
  971. }
  972. cam->owner = filp;
  973. /*
  974. * Configure things if need be.
  975. */
  976. if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
  977. ret = viacam_configure_sensor(cam);
  978. if (ret)
  979. goto out;
  980. ret = viacam_config_controller(cam);
  981. if (ret)
  982. goto out;
  983. }
  984. /*
  985. * If the CPU goes into C3, the DMA transfer gets corrupted and
  986. * users start filing unsightly bug reports. Put in a "latency"
  987. * requirement which will keep the CPU out of the deeper sleep
  988. * states.
  989. */
  990. pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
  991. /*
  992. * Fire things up.
  993. */
  994. INIT_LIST_HEAD(&cam->buffer_queue);
  995. ret = videobuf_streamon(&cam->vb_queue);
  996. if (!ret)
  997. viacam_start_engine(cam);
  998. out:
  999. mutex_unlock(&cam->lock);
  1000. return ret;
  1001. }
  1002. static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
  1003. {
  1004. struct via_camera *cam = priv;
  1005. int ret;
  1006. if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1007. return -EINVAL;
  1008. mutex_lock(&cam->lock);
  1009. if (cam->opstate != S_RUNNING) {
  1010. ret = -EINVAL;
  1011. goto out;
  1012. }
  1013. pm_qos_remove_request(&cam->qos_request);
  1014. viacam_stop_engine(cam);
  1015. /*
  1016. * Videobuf will recycle all of the outstanding buffers, but
  1017. * we should be sure we don't retain any references to
  1018. * any of them.
  1019. */
  1020. ret = videobuf_streamoff(&cam->vb_queue);
  1021. INIT_LIST_HEAD(&cam->buffer_queue);
  1022. out:
  1023. mutex_unlock(&cam->lock);
  1024. return ret;
  1025. }
  1026. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1027. static int viacam_vidiocgmbuf(struct file *filp, void *priv,
  1028. struct video_mbuf *mbuf)
  1029. {
  1030. struct via_camera *cam = priv;
  1031. return videobuf_cgmbuf(&cam->vb_queue, mbuf, 6);
  1032. }
  1033. #endif
  1034. /* G/S_PARM */
  1035. static int viacam_g_parm(struct file *filp, void *priv,
  1036. struct v4l2_streamparm *parm)
  1037. {
  1038. struct via_camera *cam = priv;
  1039. int ret;
  1040. mutex_lock(&cam->lock);
  1041. ret = sensor_call(cam, video, g_parm, parm);
  1042. mutex_unlock(&cam->lock);
  1043. parm->parm.capture.readbuffers = cam->n_cap_bufs;
  1044. return ret;
  1045. }
  1046. static int viacam_s_parm(struct file *filp, void *priv,
  1047. struct v4l2_streamparm *parm)
  1048. {
  1049. struct via_camera *cam = priv;
  1050. int ret;
  1051. mutex_lock(&cam->lock);
  1052. ret = sensor_call(cam, video, s_parm, parm);
  1053. mutex_unlock(&cam->lock);
  1054. parm->parm.capture.readbuffers = cam->n_cap_bufs;
  1055. return ret;
  1056. }
  1057. static int viacam_enum_framesizes(struct file *filp, void *priv,
  1058. struct v4l2_frmsizeenum *sizes)
  1059. {
  1060. if (sizes->index != 0)
  1061. return -EINVAL;
  1062. sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  1063. sizes->stepwise.min_width = QCIF_WIDTH;
  1064. sizes->stepwise.min_height = QCIF_HEIGHT;
  1065. sizes->stepwise.max_width = VGA_WIDTH;
  1066. sizes->stepwise.max_height = VGA_HEIGHT;
  1067. sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
  1068. return 0;
  1069. }
  1070. static int viacam_enum_frameintervals(struct file *filp, void *priv,
  1071. struct v4l2_frmivalenum *interval)
  1072. {
  1073. struct via_camera *cam = priv;
  1074. int ret;
  1075. mutex_lock(&cam->lock);
  1076. ret = sensor_call(cam, video, enum_frameintervals, interval);
  1077. mutex_unlock(&cam->lock);
  1078. return ret;
  1079. }
  1080. static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
  1081. .vidioc_g_chip_ident = viacam_g_chip_ident,
  1082. .vidioc_queryctrl = viacam_queryctrl,
  1083. .vidioc_g_ctrl = viacam_g_ctrl,
  1084. .vidioc_s_ctrl = viacam_s_ctrl,
  1085. .vidioc_enum_input = viacam_enum_input,
  1086. .vidioc_g_input = viacam_g_input,
  1087. .vidioc_s_input = viacam_s_input,
  1088. .vidioc_s_std = viacam_s_std,
  1089. .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
  1090. .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
  1091. .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
  1092. .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
  1093. .vidioc_querycap = viacam_querycap,
  1094. .vidioc_reqbufs = viacam_reqbufs,
  1095. .vidioc_querybuf = viacam_querybuf,
  1096. .vidioc_qbuf = viacam_qbuf,
  1097. .vidioc_dqbuf = viacam_dqbuf,
  1098. .vidioc_streamon = viacam_streamon,
  1099. .vidioc_streamoff = viacam_streamoff,
  1100. .vidioc_g_parm = viacam_g_parm,
  1101. .vidioc_s_parm = viacam_s_parm,
  1102. .vidioc_enum_framesizes = viacam_enum_framesizes,
  1103. .vidioc_enum_frameintervals = viacam_enum_frameintervals,
  1104. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1105. .vidiocgmbuf = viacam_vidiocgmbuf,
  1106. #endif
  1107. };
  1108. /*----------------------------------------------------------------------------*/
  1109. /*
  1110. * Power management.
  1111. */
  1112. /*
  1113. * Setup stuff.
  1114. */
  1115. static struct video_device viacam_v4l_template = {
  1116. .name = "via-camera",
  1117. .minor = -1,
  1118. .tvnorms = V4L2_STD_NTSC_M,
  1119. .current_norm = V4L2_STD_NTSC_M,
  1120. .fops = &viacam_fops,
  1121. .ioctl_ops = &viacam_ioctl_ops,
  1122. .release = video_device_release_empty, /* Check this */
  1123. };
  1124. static __devinit int viacam_probe(struct platform_device *pdev)
  1125. {
  1126. int ret;
  1127. struct i2c_adapter *sensor_adapter;
  1128. struct viafb_dev *viadev = pdev->dev.platform_data;
  1129. /*
  1130. * Note that there are actually two capture channels on
  1131. * the device. We only deal with one for now. That
  1132. * is encoded here; nothing else assumes it's dealing with
  1133. * a unique capture device.
  1134. */
  1135. struct via_camera *cam;
  1136. /*
  1137. * Ensure that frame buffer memory has been set aside for
  1138. * this purpose. As an arbitrary limit, refuse to work
  1139. * with less than two frames of VGA 16-bit data.
  1140. *
  1141. * If we ever support the second port, we'll need to set
  1142. * aside more memory.
  1143. */
  1144. if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
  1145. printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
  1146. return -ENOMEM;
  1147. }
  1148. if (viadev->engine_mmio == NULL) {
  1149. printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
  1150. return -ENOMEM;
  1151. }
  1152. /*
  1153. * Basic structure initialization.
  1154. */
  1155. cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
  1156. if (cam == NULL)
  1157. return -ENOMEM;
  1158. via_cam_info = cam;
  1159. cam->platdev = pdev;
  1160. cam->viadev = viadev;
  1161. cam->users = 0;
  1162. cam->owner = NULL;
  1163. cam->opstate = S_IDLE;
  1164. cam->user_format = cam->sensor_format = viacam_def_pix_format;
  1165. mutex_init(&cam->lock);
  1166. INIT_LIST_HEAD(&cam->buffer_queue);
  1167. cam->mmio = viadev->engine_mmio;
  1168. cam->fbmem = viadev->fbmem;
  1169. cam->fb_offset = viadev->camera_fbmem_offset;
  1170. cam->flags = 1 << CF_CONFIG_NEEDED;
  1171. cam->mbus_code = via_def_mbus_code;
  1172. /*
  1173. * Tell V4L that we exist.
  1174. */
  1175. ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
  1176. if (ret) {
  1177. dev_err(&pdev->dev, "Unable to register v4l2 device\n");
  1178. return ret;
  1179. }
  1180. /*
  1181. * Convince the system that we can do DMA.
  1182. */
  1183. pdev->dev.dma_mask = &viadev->pdev->dma_mask;
  1184. dma_set_mask(&pdev->dev, 0xffffffff);
  1185. /*
  1186. * Fire up the capture port. The write to 0x78 looks purely
  1187. * OLPCish; any system will need to tweak 0x1e.
  1188. */
  1189. via_write_reg_mask(VIASR, 0x78, 0, 0x80);
  1190. via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
  1191. /*
  1192. * Get the sensor powered up.
  1193. */
  1194. ret = via_sensor_power_setup(cam);
  1195. if (ret)
  1196. goto out_unregister;
  1197. via_sensor_power_up(cam);
  1198. /*
  1199. * See if we can't find it on the bus. The VIA_PORT_31 assumption
  1200. * is OLPC-specific. 0x42 assumption is ov7670-specific.
  1201. */
  1202. sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
  1203. cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, sensor_adapter,
  1204. "ov7670", "ov7670", 0x42 >> 1, NULL);
  1205. if (cam->sensor == NULL) {
  1206. dev_err(&pdev->dev, "Unable to find the sensor!\n");
  1207. ret = -ENODEV;
  1208. goto out_power_down;
  1209. }
  1210. /*
  1211. * Get the IRQ.
  1212. */
  1213. viacam_int_disable(cam);
  1214. ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
  1215. viacam_irq, IRQF_SHARED, "via-camera", cam);
  1216. if (ret)
  1217. goto out_power_down;
  1218. /*
  1219. * Tell V4l2 that we exist.
  1220. */
  1221. cam->vdev = viacam_v4l_template;
  1222. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1223. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1224. if (ret)
  1225. goto out_irq;
  1226. video_set_drvdata(&cam->vdev, cam);
  1227. /* Power the sensor down until somebody opens the device */
  1228. via_sensor_power_down(cam);
  1229. return 0;
  1230. out_irq:
  1231. free_irq(viadev->pdev->irq, cam);
  1232. out_power_down:
  1233. via_sensor_power_release(cam);
  1234. out_unregister:
  1235. v4l2_device_unregister(&cam->v4l2_dev);
  1236. return ret;
  1237. }
  1238. static __devexit int viacam_remove(struct platform_device *pdev)
  1239. {
  1240. struct via_camera *cam = via_cam_info;
  1241. struct viafb_dev *viadev = pdev->dev.platform_data;
  1242. video_unregister_device(&cam->vdev);
  1243. v4l2_device_unregister(&cam->v4l2_dev);
  1244. free_irq(viadev->pdev->irq, cam);
  1245. via_sensor_power_release(cam);
  1246. via_cam_info = NULL;
  1247. return 0;
  1248. }
  1249. static struct platform_driver viacam_driver = {
  1250. .driver = {
  1251. .name = "viafb-camera",
  1252. },
  1253. .probe = viacam_probe,
  1254. .remove = viacam_remove,
  1255. };
  1256. #ifdef CONFIG_OLPC_XO_1_5
  1257. /*
  1258. * The OLPC folks put the serial port on the same pin as
  1259. * the camera. They also get grumpy if we break the
  1260. * serial port and keep them from using it. So we have
  1261. * to check the serial enable bit and not step on it.
  1262. */
  1263. #define VIACAM_SERIAL_DEVFN 0x88
  1264. #define VIACAM_SERIAL_CREG 0x46
  1265. #define VIACAM_SERIAL_BIT 0x40
  1266. static __devinit int viacam_check_serial_port(void)
  1267. {
  1268. struct pci_bus *pbus = pci_find_bus(0, 0);
  1269. u8 cbyte;
  1270. pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
  1271. VIACAM_SERIAL_CREG, &cbyte);
  1272. if ((cbyte & VIACAM_SERIAL_BIT) == 0)
  1273. return 0; /* Not enabled */
  1274. if (override_serial == 0) {
  1275. printk(KERN_NOTICE "Via camera: serial port is enabled, " \
  1276. "refusing to load.\n");
  1277. printk(KERN_NOTICE "Specify override_serial=1 to force " \
  1278. "module loading.\n");
  1279. return -EBUSY;
  1280. }
  1281. printk(KERN_NOTICE "Via camera: overriding serial port\n");
  1282. pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
  1283. VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
  1284. return 0;
  1285. }
  1286. #endif
  1287. static int viacam_init(void)
  1288. {
  1289. #ifdef CONFIG_OLPC_XO_1_5
  1290. if (viacam_check_serial_port())
  1291. return -EBUSY;
  1292. #endif
  1293. return platform_driver_register(&viacam_driver);
  1294. }
  1295. module_init(viacam_init);
  1296. static void viacam_exit(void)
  1297. {
  1298. platform_driver_unregister(&viacam_driver);
  1299. }
  1300. module_exit(viacam_exit);