via-camera.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the VIA Chrome integrated camera controller.
  4. *
  5. * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
  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/platform_device.h>
  17. #include <linux/videodev2.h>
  18. #include <media/v4l2-device.h>
  19. #include <media/v4l2-ioctl.h>
  20. #include <media/v4l2-ctrls.h>
  21. #include <media/v4l2-event.h>
  22. #include <media/v4l2-image-sizes.h>
  23. #include <media/i2c/ov7670.h>
  24. #include <media/videobuf2-dma-sg.h>
  25. #include <linux/delay.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/pm_qos.h>
  28. #include <linux/via-core.h>
  29. #include <linux/via-gpio.h>
  30. #include <linux/via_i2c.h>
  31. #ifdef CONFIG_X86
  32. #include <asm/olpc.h>
  33. #else
  34. #define machine_is_olpc(x) 0
  35. #endif
  36. #include "via-camera.h"
  37. MODULE_ALIAS("platform:viafb-camera");
  38. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  39. MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
  40. MODULE_LICENSE("GPL");
  41. static bool flip_image;
  42. module_param(flip_image, bool, 0444);
  43. MODULE_PARM_DESC(flip_image,
  44. "If set, the sensor will be instructed to flip the image vertically.");
  45. static bool override_serial;
  46. module_param(override_serial, bool, 0444);
  47. MODULE_PARM_DESC(override_serial,
  48. "The camera driver will normally refuse to load if the XO 1.5 serial port is enabled. Set this option to force-enable the camera.");
  49. /*
  50. * The structure describing our camera.
  51. */
  52. enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
  53. struct via_camera {
  54. struct v4l2_device v4l2_dev;
  55. struct v4l2_ctrl_handler ctrl_handler;
  56. struct video_device vdev;
  57. struct v4l2_subdev *sensor;
  58. struct platform_device *platdev;
  59. struct viafb_dev *viadev;
  60. struct mutex lock;
  61. enum viacam_opstate opstate;
  62. unsigned long flags;
  63. struct pm_qos_request qos_request;
  64. /*
  65. * GPIO info for power/reset management
  66. */
  67. int power_gpio;
  68. int reset_gpio;
  69. /*
  70. * I/O memory stuff.
  71. */
  72. void __iomem *mmio; /* Where the registers live */
  73. void __iomem *fbmem; /* Frame buffer memory */
  74. u32 fb_offset; /* Reserved memory offset (FB) */
  75. /*
  76. * Capture buffers and related. The controller supports
  77. * up to three, so that's what we have here. These buffers
  78. * live in frame buffer memory, so we don't call them "DMA".
  79. */
  80. unsigned int cb_offsets[3]; /* offsets into fb mem */
  81. u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */
  82. int n_cap_bufs; /* How many are we using? */
  83. struct vb2_queue vq;
  84. struct list_head buffer_queue;
  85. u32 sequence;
  86. /*
  87. * Video format information. sensor_format is kept in a form
  88. * that we can use to pass to the sensor. We always run the
  89. * sensor in VGA resolution, though, and let the controller
  90. * downscale things if need be. So we keep the "real*
  91. * dimensions separately.
  92. */
  93. struct v4l2_pix_format sensor_format;
  94. struct v4l2_pix_format user_format;
  95. u32 mbus_code;
  96. };
  97. /* buffer for one video frame */
  98. struct via_buffer {
  99. /* common v4l buffer stuff -- must be first */
  100. struct vb2_v4l2_buffer vbuf;
  101. struct list_head queue;
  102. };
  103. /*
  104. * Yes, this is a hack, but there's only going to be one of these
  105. * on any system we know of.
  106. */
  107. static struct via_camera *via_cam_info;
  108. /*
  109. * Flag values, manipulated with bitops
  110. */
  111. #define CF_DMA_ACTIVE 0 /* A frame is incoming */
  112. #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
  113. /*
  114. * Nasty ugly v4l2 boilerplate.
  115. */
  116. #define sensor_call(cam, optype, func, args...) \
  117. v4l2_subdev_call(cam->sensor, optype, func, ##args)
  118. /*
  119. * Debugging and related.
  120. */
  121. #define cam_err(cam, fmt, arg...) \
  122. dev_err(&(cam)->platdev->dev, fmt, ##arg);
  123. #define cam_warn(cam, fmt, arg...) \
  124. dev_warn(&(cam)->platdev->dev, fmt, ##arg);
  125. #define cam_dbg(cam, fmt, arg...) \
  126. dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
  127. /*
  128. * Format handling. This is ripped almost directly from Hans's changes
  129. * to cafe_ccic.c. It's a little unfortunate; until this change, we
  130. * didn't need to know anything about the format except its byte depth;
  131. * now this information must be managed at this level too.
  132. */
  133. static struct via_format {
  134. __u32 pixelformat;
  135. int bpp; /* Bytes per pixel */
  136. u32 mbus_code;
  137. } via_formats[] = {
  138. {
  139. .pixelformat = V4L2_PIX_FMT_YUYV,
  140. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  141. .bpp = 2,
  142. },
  143. /* RGB444 and Bayer should be doable, but have never been
  144. tested with this driver. RGB565 seems to work at the default
  145. resolution, but results in color corruption when being scaled by
  146. viacam_set_scaled(), and is disabled as a result. */
  147. };
  148. #define N_VIA_FMTS ARRAY_SIZE(via_formats)
  149. static struct via_format *via_find_format(u32 pixelformat)
  150. {
  151. unsigned i;
  152. for (i = 0; i < N_VIA_FMTS; i++)
  153. if (via_formats[i].pixelformat == pixelformat)
  154. return via_formats + i;
  155. /* Not found? Then return the first format. */
  156. return via_formats;
  157. }
  158. /*--------------------------------------------------------------------------*/
  159. /*
  160. * Sensor power/reset management. This piece is OLPC-specific for
  161. * sure; other configurations will have things connected differently.
  162. */
  163. static int via_sensor_power_setup(struct via_camera *cam)
  164. {
  165. int ret;
  166. cam->power_gpio = viafb_gpio_lookup("VGPIO3");
  167. cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
  168. if (!gpio_is_valid(cam->power_gpio) || !gpio_is_valid(cam->reset_gpio)) {
  169. dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
  170. return -EINVAL;
  171. }
  172. ret = gpio_request(cam->power_gpio, "viafb-camera");
  173. if (ret) {
  174. dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
  175. return ret;
  176. }
  177. ret = gpio_request(cam->reset_gpio, "viafb-camera");
  178. if (ret) {
  179. dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
  180. gpio_free(cam->power_gpio);
  181. return ret;
  182. }
  183. gpio_direction_output(cam->power_gpio, 0);
  184. gpio_direction_output(cam->reset_gpio, 0);
  185. return 0;
  186. }
  187. /*
  188. * Power up the sensor and perform the reset dance.
  189. */
  190. static void via_sensor_power_up(struct via_camera *cam)
  191. {
  192. gpio_set_value(cam->power_gpio, 1);
  193. gpio_set_value(cam->reset_gpio, 0);
  194. msleep(20); /* Probably excessive */
  195. gpio_set_value(cam->reset_gpio, 1);
  196. msleep(20);
  197. }
  198. static void via_sensor_power_down(struct via_camera *cam)
  199. {
  200. gpio_set_value(cam->power_gpio, 0);
  201. gpio_set_value(cam->reset_gpio, 0);
  202. }
  203. static void via_sensor_power_release(struct via_camera *cam)
  204. {
  205. via_sensor_power_down(cam);
  206. gpio_free(cam->power_gpio);
  207. gpio_free(cam->reset_gpio);
  208. }
  209. /* --------------------------------------------------------------------------*/
  210. /* Sensor ops */
  211. /*
  212. * Manage the ov7670 "flip" bit, which needs special help.
  213. */
  214. static int viacam_set_flip(struct via_camera *cam)
  215. {
  216. struct v4l2_control ctrl;
  217. memset(&ctrl, 0, sizeof(ctrl));
  218. ctrl.id = V4L2_CID_VFLIP;
  219. ctrl.value = flip_image;
  220. return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
  221. }
  222. /*
  223. * Configure the sensor. It's up to the caller to ensure
  224. * that the camera is in the correct operating state.
  225. */
  226. static int viacam_configure_sensor(struct via_camera *cam)
  227. {
  228. struct v4l2_subdev_format format = {
  229. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  230. };
  231. int ret;
  232. v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code);
  233. ret = sensor_call(cam, core, init, 0);
  234. if (ret == 0)
  235. ret = sensor_call(cam, pad, set_fmt, NULL, &format);
  236. /*
  237. * OV7670 does weird things if flip is set *before* format...
  238. */
  239. if (ret == 0)
  240. ret = viacam_set_flip(cam);
  241. return ret;
  242. }
  243. /* --------------------------------------------------------------------------*/
  244. /*
  245. * Some simple register accessors; they assume that the lock is held.
  246. *
  247. * Should we want to support the second capture engine, we could
  248. * hide the register difference by adding 0x1000 to registers in the
  249. * 0x300-350 range.
  250. */
  251. static inline void viacam_write_reg(struct via_camera *cam,
  252. int reg, int value)
  253. {
  254. iowrite32(value, cam->mmio + reg);
  255. }
  256. static inline int viacam_read_reg(struct via_camera *cam, int reg)
  257. {
  258. return ioread32(cam->mmio + reg);
  259. }
  260. static inline void viacam_write_reg_mask(struct via_camera *cam,
  261. int reg, int value, int mask)
  262. {
  263. int tmp = viacam_read_reg(cam, reg);
  264. tmp = (tmp & ~mask) | (value & mask);
  265. viacam_write_reg(cam, reg, tmp);
  266. }
  267. /* --------------------------------------------------------------------------*/
  268. /* Interrupt management and handling */
  269. static irqreturn_t viacam_quick_irq(int irq, void *data)
  270. {
  271. struct via_camera *cam = data;
  272. irqreturn_t ret = IRQ_NONE;
  273. int icv;
  274. /*
  275. * All we do here is to clear the interrupts and tell
  276. * the handler thread to wake up.
  277. */
  278. spin_lock(&cam->viadev->reg_lock);
  279. icv = viacam_read_reg(cam, VCR_INTCTRL);
  280. if (icv & VCR_IC_EAV) {
  281. icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
  282. viacam_write_reg(cam, VCR_INTCTRL, icv);
  283. ret = IRQ_WAKE_THREAD;
  284. }
  285. spin_unlock(&cam->viadev->reg_lock);
  286. return ret;
  287. }
  288. /*
  289. * Find the next buffer which has somebody waiting on it.
  290. */
  291. static struct via_buffer *viacam_next_buffer(struct via_camera *cam)
  292. {
  293. if (cam->opstate != S_RUNNING)
  294. return NULL;
  295. if (list_empty(&cam->buffer_queue))
  296. return NULL;
  297. return list_entry(cam->buffer_queue.next, struct via_buffer, queue);
  298. }
  299. /*
  300. * The threaded IRQ handler.
  301. */
  302. static irqreturn_t viacam_irq(int irq, void *data)
  303. {
  304. struct via_camera *cam = data;
  305. struct via_buffer *vb;
  306. int bufn;
  307. struct sg_table *sgt;
  308. mutex_lock(&cam->lock);
  309. /*
  310. * If there is no place to put the data frame, don't bother
  311. * with anything else.
  312. */
  313. vb = viacam_next_buffer(cam);
  314. if (vb == NULL)
  315. goto done;
  316. /*
  317. * Figure out which buffer we just completed.
  318. */
  319. bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
  320. bufn -= 1;
  321. if (bufn < 0)
  322. bufn = cam->n_cap_bufs - 1;
  323. /*
  324. * Copy over the data and let any waiters know.
  325. */
  326. sgt = vb2_dma_sg_plane_desc(&vb->vbuf.vb2_buf, 0);
  327. vb->vbuf.vb2_buf.timestamp = ktime_get_ns();
  328. viafb_dma_copy_out_sg(cam->cb_offsets[bufn], sgt->sgl, sgt->nents);
  329. vb->vbuf.sequence = cam->sequence++;
  330. vb->vbuf.field = V4L2_FIELD_NONE;
  331. list_del(&vb->queue);
  332. vb2_buffer_done(&vb->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
  333. done:
  334. mutex_unlock(&cam->lock);
  335. return IRQ_HANDLED;
  336. }
  337. /*
  338. * These functions must mess around with the general interrupt
  339. * control register, which is relevant to much more than just the
  340. * camera. Nothing else uses interrupts, though, as of this writing.
  341. * Should that situation change, we'll have to improve support at
  342. * the via-core level.
  343. */
  344. static void viacam_int_enable(struct via_camera *cam)
  345. {
  346. viacam_write_reg(cam, VCR_INTCTRL,
  347. VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
  348. viafb_irq_enable(VDE_I_C0AVEN);
  349. }
  350. static void viacam_int_disable(struct via_camera *cam)
  351. {
  352. viafb_irq_disable(VDE_I_C0AVEN);
  353. viacam_write_reg(cam, VCR_INTCTRL, 0);
  354. }
  355. /* --------------------------------------------------------------------------*/
  356. /* Controller operations */
  357. /*
  358. * Set up our capture buffers in framebuffer memory.
  359. */
  360. static int viacam_ctlr_cbufs(struct via_camera *cam)
  361. {
  362. int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
  363. int i;
  364. unsigned int offset;
  365. /*
  366. * See how many buffers we can work with.
  367. */
  368. if (nbuf >= 3) {
  369. cam->n_cap_bufs = 3;
  370. viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
  371. VCR_CI_3BUFS);
  372. } else if (nbuf == 2) {
  373. cam->n_cap_bufs = 2;
  374. viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
  375. } else {
  376. cam_warn(cam, "Insufficient frame buffer memory\n");
  377. return -ENOMEM;
  378. }
  379. /*
  380. * Set them up.
  381. */
  382. offset = cam->fb_offset;
  383. for (i = 0; i < cam->n_cap_bufs; i++) {
  384. cam->cb_offsets[i] = offset;
  385. cam->cb_addrs[i] = cam->fbmem + offset;
  386. viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
  387. offset += cam->sensor_format.sizeimage;
  388. }
  389. return 0;
  390. }
  391. /*
  392. * Set the scaling register for downscaling the image.
  393. *
  394. * This register works like this... Vertical scaling is enabled
  395. * by bit 26; if that bit is set, downscaling is controlled by the
  396. * value in bits 16:25. Those bits are divided by 1024 to get
  397. * the scaling factor; setting just bit 25 thus cuts the height
  398. * in half.
  399. *
  400. * Horizontal scaling works about the same, but it's enabled by
  401. * bit 11, with bits 0:10 giving the numerator of a fraction
  402. * (over 2048) for the scaling value.
  403. *
  404. * This function is naive in that, if the user departs from
  405. * the 3x4 VGA scaling factor, the image will distort. We
  406. * could work around that if it really seemed important.
  407. */
  408. static void viacam_set_scale(struct via_camera *cam)
  409. {
  410. unsigned int avscale;
  411. int sf;
  412. if (cam->user_format.width == VGA_WIDTH)
  413. avscale = 0;
  414. else {
  415. sf = (cam->user_format.width*2048)/VGA_WIDTH;
  416. avscale = VCR_AVS_HEN | sf;
  417. }
  418. if (cam->user_format.height < VGA_HEIGHT) {
  419. sf = (1024*cam->user_format.height)/VGA_HEIGHT;
  420. avscale |= VCR_AVS_VEN | (sf << 16);
  421. }
  422. viacam_write_reg(cam, VCR_AVSCALE, avscale);
  423. }
  424. /*
  425. * Configure image-related information into the capture engine.
  426. */
  427. static void viacam_ctlr_image(struct via_camera *cam)
  428. {
  429. int cicreg;
  430. /*
  431. * Disable clock before messing with stuff - from the via
  432. * sample driver.
  433. */
  434. viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
  435. /*
  436. * Set up the controller for VGA resolution, modulo magic
  437. * offsets from the via sample driver.
  438. */
  439. viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
  440. viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
  441. viacam_set_scale(cam);
  442. /*
  443. * Image size info.
  444. */
  445. viacam_write_reg(cam, VCR_MAXDATA,
  446. (cam->sensor_format.height << 16) |
  447. (cam->sensor_format.bytesperline >> 3));
  448. viacam_write_reg(cam, VCR_MAXVBI, 0);
  449. viacam_write_reg(cam, VCR_VSTRIDE,
  450. cam->user_format.bytesperline & VCR_VS_STRIDE);
  451. /*
  452. * Set up the capture interface control register,
  453. * everything but the "go" bit.
  454. *
  455. * The FIFO threshold is a bit of a magic number; 8 is what
  456. * VIA's sample code uses.
  457. */
  458. cicreg = VCR_CI_CLKEN |
  459. 0x08000000 | /* FIFO threshold */
  460. VCR_CI_FLDINV | /* OLPC-specific? */
  461. VCR_CI_VREFINV | /* OLPC-specific? */
  462. VCR_CI_DIBOTH | /* Capture both fields */
  463. VCR_CI_CCIR601_8;
  464. if (cam->n_cap_bufs == 3)
  465. cicreg |= VCR_CI_3BUFS;
  466. /*
  467. * YUV formats need different byte swapping than RGB.
  468. */
  469. if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
  470. cicreg |= VCR_CI_YUYV;
  471. else
  472. cicreg |= VCR_CI_UYVY;
  473. viacam_write_reg(cam, VCR_CAPINTC, cicreg);
  474. }
  475. static int viacam_config_controller(struct via_camera *cam)
  476. {
  477. int ret;
  478. unsigned long flags;
  479. spin_lock_irqsave(&cam->viadev->reg_lock, flags);
  480. ret = viacam_ctlr_cbufs(cam);
  481. if (!ret)
  482. viacam_ctlr_image(cam);
  483. spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
  484. clear_bit(CF_CONFIG_NEEDED, &cam->flags);
  485. return ret;
  486. }
  487. /*
  488. * Make it start grabbing data.
  489. */
  490. static void viacam_start_engine(struct via_camera *cam)
  491. {
  492. spin_lock_irq(&cam->viadev->reg_lock);
  493. viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
  494. viacam_int_enable(cam);
  495. (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
  496. cam->opstate = S_RUNNING;
  497. spin_unlock_irq(&cam->viadev->reg_lock);
  498. }
  499. static void viacam_stop_engine(struct via_camera *cam)
  500. {
  501. spin_lock_irq(&cam->viadev->reg_lock);
  502. viacam_int_disable(cam);
  503. viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
  504. (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
  505. cam->opstate = S_IDLE;
  506. spin_unlock_irq(&cam->viadev->reg_lock);
  507. }
  508. /* --------------------------------------------------------------------------*/
  509. /* vb2 callback ops */
  510. static struct via_buffer *vb2_to_via_buffer(struct vb2_buffer *vb)
  511. {
  512. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  513. return container_of(vbuf, struct via_buffer, vbuf);
  514. }
  515. static void viacam_vb2_queue(struct vb2_buffer *vb)
  516. {
  517. struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  518. struct via_buffer *via = vb2_to_via_buffer(vb);
  519. list_add_tail(&via->queue, &cam->buffer_queue);
  520. }
  521. static int viacam_vb2_prepare(struct vb2_buffer *vb)
  522. {
  523. struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  524. if (vb2_plane_size(vb, 0) < cam->user_format.sizeimage) {
  525. cam_dbg(cam,
  526. "Plane size too small (%lu < %u)\n",
  527. vb2_plane_size(vb, 0),
  528. cam->user_format.sizeimage);
  529. return -EINVAL;
  530. }
  531. vb2_set_plane_payload(vb, 0, cam->user_format.sizeimage);
  532. return 0;
  533. }
  534. static int viacam_vb2_queue_setup(struct vb2_queue *vq,
  535. unsigned int *nbufs,
  536. unsigned int *num_planes, unsigned int sizes[],
  537. struct device *alloc_devs[])
  538. {
  539. struct via_camera *cam = vb2_get_drv_priv(vq);
  540. int size = cam->user_format.sizeimage;
  541. if (*num_planes)
  542. return sizes[0] < size ? -EINVAL : 0;
  543. *num_planes = 1;
  544. sizes[0] = size;
  545. return 0;
  546. }
  547. static int viacam_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
  548. {
  549. struct via_camera *cam = vb2_get_drv_priv(vq);
  550. struct via_buffer *buf, *tmp;
  551. int ret = 0;
  552. if (cam->opstate != S_IDLE) {
  553. ret = -EBUSY;
  554. goto out;
  555. }
  556. /*
  557. * Configure things if need be.
  558. */
  559. if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
  560. ret = viacam_configure_sensor(cam);
  561. if (ret)
  562. goto out;
  563. ret = viacam_config_controller(cam);
  564. if (ret)
  565. goto out;
  566. }
  567. cam->sequence = 0;
  568. /*
  569. * If the CPU goes into C3, the DMA transfer gets corrupted and
  570. * users start filing unsightly bug reports. Put in a "latency"
  571. * requirement which will keep the CPU out of the deeper sleep
  572. * states.
  573. */
  574. pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
  575. viacam_start_engine(cam);
  576. return 0;
  577. out:
  578. list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) {
  579. list_del(&buf->queue);
  580. vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
  581. }
  582. return ret;
  583. }
  584. static void viacam_vb2_stop_streaming(struct vb2_queue *vq)
  585. {
  586. struct via_camera *cam = vb2_get_drv_priv(vq);
  587. struct via_buffer *buf, *tmp;
  588. pm_qos_remove_request(&cam->qos_request);
  589. viacam_stop_engine(cam);
  590. list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) {
  591. list_del(&buf->queue);
  592. vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
  593. }
  594. }
  595. static const struct vb2_ops viacam_vb2_ops = {
  596. .queue_setup = viacam_vb2_queue_setup,
  597. .buf_queue = viacam_vb2_queue,
  598. .buf_prepare = viacam_vb2_prepare,
  599. .start_streaming = viacam_vb2_start_streaming,
  600. .stop_streaming = viacam_vb2_stop_streaming,
  601. .wait_prepare = vb2_ops_wait_prepare,
  602. .wait_finish = vb2_ops_wait_finish,
  603. };
  604. /* --------------------------------------------------------------------------*/
  605. /* File operations */
  606. static int viacam_open(struct file *filp)
  607. {
  608. struct via_camera *cam = video_drvdata(filp);
  609. int ret;
  610. /*
  611. * Note the new user. If this is the first one, we'll also
  612. * need to power up the sensor.
  613. */
  614. mutex_lock(&cam->lock);
  615. ret = v4l2_fh_open(filp);
  616. if (ret)
  617. goto out;
  618. if (v4l2_fh_is_singular_file(filp)) {
  619. ret = viafb_request_dma();
  620. if (ret) {
  621. v4l2_fh_release(filp);
  622. goto out;
  623. }
  624. via_sensor_power_up(cam);
  625. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  626. }
  627. out:
  628. mutex_unlock(&cam->lock);
  629. return ret;
  630. }
  631. static int viacam_release(struct file *filp)
  632. {
  633. struct via_camera *cam = video_drvdata(filp);
  634. bool last_open;
  635. mutex_lock(&cam->lock);
  636. last_open = v4l2_fh_is_singular_file(filp);
  637. _vb2_fop_release(filp, NULL);
  638. /*
  639. * Last one out needs to turn out the lights.
  640. */
  641. if (last_open) {
  642. via_sensor_power_down(cam);
  643. viafb_release_dma();
  644. }
  645. mutex_unlock(&cam->lock);
  646. return 0;
  647. }
  648. static const struct v4l2_file_operations viacam_fops = {
  649. .owner = THIS_MODULE,
  650. .open = viacam_open,
  651. .release = viacam_release,
  652. .read = vb2_fop_read,
  653. .poll = vb2_fop_poll,
  654. .mmap = vb2_fop_mmap,
  655. .unlocked_ioctl = video_ioctl2,
  656. };
  657. /*----------------------------------------------------------------------------*/
  658. /*
  659. * The long list of v4l2 ioctl ops
  660. */
  661. /*
  662. * Only one input.
  663. */
  664. static int viacam_enum_input(struct file *filp, void *priv,
  665. struct v4l2_input *input)
  666. {
  667. if (input->index != 0)
  668. return -EINVAL;
  669. input->type = V4L2_INPUT_TYPE_CAMERA;
  670. strscpy(input->name, "Camera", sizeof(input->name));
  671. return 0;
  672. }
  673. static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
  674. {
  675. *i = 0;
  676. return 0;
  677. }
  678. static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
  679. {
  680. if (i != 0)
  681. return -EINVAL;
  682. return 0;
  683. }
  684. /*
  685. * Video format stuff. Here is our default format until
  686. * user space messes with things.
  687. */
  688. static const struct v4l2_pix_format viacam_def_pix_format = {
  689. .width = VGA_WIDTH,
  690. .height = VGA_HEIGHT,
  691. .pixelformat = V4L2_PIX_FMT_YUYV,
  692. .field = V4L2_FIELD_NONE,
  693. .bytesperline = VGA_WIDTH * 2,
  694. .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
  695. .colorspace = V4L2_COLORSPACE_SRGB,
  696. };
  697. static const u32 via_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
  698. static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
  699. struct v4l2_fmtdesc *fmt)
  700. {
  701. if (fmt->index >= N_VIA_FMTS)
  702. return -EINVAL;
  703. fmt->pixelformat = via_formats[fmt->index].pixelformat;
  704. return 0;
  705. }
  706. /*
  707. * Figure out proper image dimensions, but always force the
  708. * sensor to VGA.
  709. */
  710. static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
  711. struct v4l2_pix_format *sensorfmt)
  712. {
  713. *sensorfmt = *userfmt;
  714. if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
  715. userfmt->width = QCIF_WIDTH;
  716. userfmt->height = QCIF_HEIGHT;
  717. }
  718. if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
  719. userfmt->width = VGA_WIDTH;
  720. userfmt->height = VGA_HEIGHT;
  721. }
  722. sensorfmt->width = VGA_WIDTH;
  723. sensorfmt->height = VGA_HEIGHT;
  724. }
  725. static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
  726. struct v4l2_pix_format *sensorfmt)
  727. {
  728. struct via_format *f = via_find_format(userfmt->pixelformat);
  729. sensorfmt->bytesperline = sensorfmt->width * f->bpp;
  730. sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
  731. userfmt->pixelformat = sensorfmt->pixelformat;
  732. userfmt->field = sensorfmt->field;
  733. userfmt->bytesperline = 2 * userfmt->width;
  734. userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
  735. userfmt->colorspace = sensorfmt->colorspace;
  736. userfmt->ycbcr_enc = sensorfmt->ycbcr_enc;
  737. userfmt->quantization = sensorfmt->quantization;
  738. userfmt->xfer_func = sensorfmt->xfer_func;
  739. }
  740. /*
  741. * The real work of figuring out a workable format.
  742. */
  743. static int viacam_do_try_fmt(struct via_camera *cam,
  744. struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
  745. {
  746. int ret;
  747. struct v4l2_subdev_pad_config pad_cfg;
  748. struct v4l2_subdev_format format = {
  749. .which = V4L2_SUBDEV_FORMAT_TRY,
  750. };
  751. struct via_format *f = via_find_format(upix->pixelformat);
  752. upix->pixelformat = f->pixelformat;
  753. viacam_fmt_pre(upix, spix);
  754. v4l2_fill_mbus_format(&format.format, spix, f->mbus_code);
  755. ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format);
  756. v4l2_fill_pix_format(spix, &format.format);
  757. viacam_fmt_post(upix, spix);
  758. return ret;
  759. }
  760. static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
  761. struct v4l2_format *fmt)
  762. {
  763. struct via_camera *cam = video_drvdata(filp);
  764. struct v4l2_format sfmt;
  765. return viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
  766. }
  767. static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
  768. struct v4l2_format *fmt)
  769. {
  770. struct via_camera *cam = video_drvdata(filp);
  771. fmt->fmt.pix = cam->user_format;
  772. return 0;
  773. }
  774. static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
  775. struct v4l2_format *fmt)
  776. {
  777. struct via_camera *cam = video_drvdata(filp);
  778. int ret;
  779. struct v4l2_format sfmt;
  780. struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
  781. /*
  782. * Camera must be idle or we can't mess with the
  783. * video setup.
  784. */
  785. if (cam->opstate != S_IDLE)
  786. return -EBUSY;
  787. /*
  788. * Let the sensor code look over and tweak the
  789. * requested formatting.
  790. */
  791. ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
  792. if (ret)
  793. return ret;
  794. /*
  795. * OK, let's commit to the new format.
  796. */
  797. cam->user_format = fmt->fmt.pix;
  798. cam->sensor_format = sfmt.fmt.pix;
  799. cam->mbus_code = f->mbus_code;
  800. ret = viacam_configure_sensor(cam);
  801. if (!ret)
  802. ret = viacam_config_controller(cam);
  803. return ret;
  804. }
  805. static int viacam_querycap(struct file *filp, void *priv,
  806. struct v4l2_capability *cap)
  807. {
  808. strscpy(cap->driver, "via-camera", sizeof(cap->driver));
  809. strscpy(cap->card, "via-camera", sizeof(cap->card));
  810. strscpy(cap->bus_info, "platform:via-camera", sizeof(cap->bus_info));
  811. return 0;
  812. }
  813. /* G/S_PARM */
  814. static int viacam_g_parm(struct file *filp, void *priv,
  815. struct v4l2_streamparm *parm)
  816. {
  817. struct via_camera *cam = video_drvdata(filp);
  818. return v4l2_g_parm_cap(video_devdata(filp), cam->sensor, parm);
  819. }
  820. static int viacam_s_parm(struct file *filp, void *priv,
  821. struct v4l2_streamparm *parm)
  822. {
  823. struct via_camera *cam = video_drvdata(filp);
  824. return v4l2_s_parm_cap(video_devdata(filp), cam->sensor, parm);
  825. }
  826. static int viacam_enum_framesizes(struct file *filp, void *priv,
  827. struct v4l2_frmsizeenum *sizes)
  828. {
  829. unsigned int i;
  830. if (sizes->index != 0)
  831. return -EINVAL;
  832. for (i = 0; i < N_VIA_FMTS; i++)
  833. if (sizes->pixel_format == via_formats[i].pixelformat)
  834. break;
  835. if (i >= N_VIA_FMTS)
  836. return -EINVAL;
  837. sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  838. sizes->stepwise.min_width = QCIF_WIDTH;
  839. sizes->stepwise.min_height = QCIF_HEIGHT;
  840. sizes->stepwise.max_width = VGA_WIDTH;
  841. sizes->stepwise.max_height = VGA_HEIGHT;
  842. sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
  843. return 0;
  844. }
  845. static int viacam_enum_frameintervals(struct file *filp, void *priv,
  846. struct v4l2_frmivalenum *interval)
  847. {
  848. struct via_camera *cam = video_drvdata(filp);
  849. struct v4l2_subdev_frame_interval_enum fie = {
  850. .index = interval->index,
  851. .code = cam->mbus_code,
  852. .width = cam->sensor_format.width,
  853. .height = cam->sensor_format.height,
  854. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  855. };
  856. unsigned int i;
  857. int ret;
  858. for (i = 0; i < N_VIA_FMTS; i++)
  859. if (interval->pixel_format == via_formats[i].pixelformat)
  860. break;
  861. if (i >= N_VIA_FMTS)
  862. return -EINVAL;
  863. if (interval->width < QCIF_WIDTH || interval->width > VGA_WIDTH ||
  864. interval->height < QCIF_HEIGHT || interval->height > VGA_HEIGHT)
  865. return -EINVAL;
  866. ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
  867. if (ret)
  868. return ret;
  869. interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  870. interval->discrete = fie.interval;
  871. return 0;
  872. }
  873. static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
  874. .vidioc_enum_input = viacam_enum_input,
  875. .vidioc_g_input = viacam_g_input,
  876. .vidioc_s_input = viacam_s_input,
  877. .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
  878. .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
  879. .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
  880. .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
  881. .vidioc_querycap = viacam_querycap,
  882. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  883. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  884. .vidioc_querybuf = vb2_ioctl_querybuf,
  885. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  886. .vidioc_qbuf = vb2_ioctl_qbuf,
  887. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  888. .vidioc_expbuf = vb2_ioctl_expbuf,
  889. .vidioc_streamon = vb2_ioctl_streamon,
  890. .vidioc_streamoff = vb2_ioctl_streamoff,
  891. .vidioc_g_parm = viacam_g_parm,
  892. .vidioc_s_parm = viacam_s_parm,
  893. .vidioc_enum_framesizes = viacam_enum_framesizes,
  894. .vidioc_enum_frameintervals = viacam_enum_frameintervals,
  895. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  896. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  897. };
  898. /*----------------------------------------------------------------------------*/
  899. /*
  900. * Power management.
  901. */
  902. #ifdef CONFIG_PM
  903. static int viacam_suspend(void *priv)
  904. {
  905. struct via_camera *cam = priv;
  906. enum viacam_opstate state = cam->opstate;
  907. if (cam->opstate != S_IDLE) {
  908. viacam_stop_engine(cam);
  909. cam->opstate = state; /* So resume restarts */
  910. }
  911. return 0;
  912. }
  913. static int viacam_resume(void *priv)
  914. {
  915. struct via_camera *cam = priv;
  916. int ret = 0;
  917. /*
  918. * Get back to a reasonable operating state.
  919. */
  920. via_write_reg_mask(VIASR, 0x78, 0, 0x80);
  921. via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
  922. viacam_int_disable(cam);
  923. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  924. /*
  925. * Make sure the sensor's power state is correct
  926. */
  927. if (!list_empty(&cam->vdev.fh_list))
  928. via_sensor_power_up(cam);
  929. else
  930. via_sensor_power_down(cam);
  931. /*
  932. * If it was operating, try to restart it.
  933. */
  934. if (cam->opstate != S_IDLE) {
  935. mutex_lock(&cam->lock);
  936. ret = viacam_configure_sensor(cam);
  937. if (!ret)
  938. ret = viacam_config_controller(cam);
  939. mutex_unlock(&cam->lock);
  940. if (!ret)
  941. viacam_start_engine(cam);
  942. }
  943. return ret;
  944. }
  945. static struct viafb_pm_hooks viacam_pm_hooks = {
  946. .suspend = viacam_suspend,
  947. .resume = viacam_resume
  948. };
  949. #endif /* CONFIG_PM */
  950. /*
  951. * Setup stuff.
  952. */
  953. static const struct video_device viacam_v4l_template = {
  954. .name = "via-camera",
  955. .minor = -1,
  956. .fops = &viacam_fops,
  957. .ioctl_ops = &viacam_ioctl_ops,
  958. .release = video_device_release_empty, /* Check this */
  959. .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  960. V4L2_CAP_STREAMING,
  961. };
  962. /*
  963. * The OLPC folks put the serial port on the same pin as
  964. * the camera. They also get grumpy if we break the
  965. * serial port and keep them from using it. So we have
  966. * to check the serial enable bit and not step on it.
  967. */
  968. #define VIACAM_SERIAL_DEVFN 0x88
  969. #define VIACAM_SERIAL_CREG 0x46
  970. #define VIACAM_SERIAL_BIT 0x40
  971. static bool viacam_serial_is_enabled(void)
  972. {
  973. struct pci_bus *pbus = pci_find_bus(0, 0);
  974. u8 cbyte;
  975. if (!pbus)
  976. return false;
  977. pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
  978. VIACAM_SERIAL_CREG, &cbyte);
  979. if ((cbyte & VIACAM_SERIAL_BIT) == 0)
  980. return false; /* Not enabled */
  981. if (!override_serial) {
  982. printk(KERN_NOTICE "Via camera: serial port is enabled, " \
  983. "refusing to load.\n");
  984. printk(KERN_NOTICE "Specify override_serial=1 to force " \
  985. "module loading.\n");
  986. return true;
  987. }
  988. printk(KERN_NOTICE "Via camera: overriding serial port\n");
  989. pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
  990. VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
  991. return false;
  992. }
  993. static struct ov7670_config sensor_cfg = {
  994. /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
  995. .clock_speed = 90,
  996. };
  997. static int viacam_probe(struct platform_device *pdev)
  998. {
  999. int ret;
  1000. struct i2c_adapter *sensor_adapter;
  1001. struct viafb_dev *viadev = pdev->dev.platform_data;
  1002. struct vb2_queue *vq;
  1003. struct i2c_board_info ov7670_info = {
  1004. .type = "ov7670",
  1005. .addr = 0x42 >> 1,
  1006. .platform_data = &sensor_cfg,
  1007. };
  1008. /*
  1009. * Note that there are actually two capture channels on
  1010. * the device. We only deal with one for now. That
  1011. * is encoded here; nothing else assumes it's dealing with
  1012. * a unique capture device.
  1013. */
  1014. struct via_camera *cam;
  1015. /*
  1016. * Ensure that frame buffer memory has been set aside for
  1017. * this purpose. As an arbitrary limit, refuse to work
  1018. * with less than two frames of VGA 16-bit data.
  1019. *
  1020. * If we ever support the second port, we'll need to set
  1021. * aside more memory.
  1022. */
  1023. if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
  1024. printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
  1025. return -ENOMEM;
  1026. }
  1027. if (viadev->engine_mmio == NULL) {
  1028. printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
  1029. return -ENOMEM;
  1030. }
  1031. if (machine_is_olpc() && viacam_serial_is_enabled())
  1032. return -EBUSY;
  1033. /*
  1034. * Basic structure initialization.
  1035. */
  1036. cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
  1037. if (cam == NULL)
  1038. return -ENOMEM;
  1039. via_cam_info = cam;
  1040. cam->platdev = pdev;
  1041. cam->viadev = viadev;
  1042. cam->opstate = S_IDLE;
  1043. cam->user_format = cam->sensor_format = viacam_def_pix_format;
  1044. mutex_init(&cam->lock);
  1045. INIT_LIST_HEAD(&cam->buffer_queue);
  1046. cam->mmio = viadev->engine_mmio;
  1047. cam->fbmem = viadev->fbmem;
  1048. cam->fb_offset = viadev->camera_fbmem_offset;
  1049. cam->flags = 1 << CF_CONFIG_NEEDED;
  1050. cam->mbus_code = via_def_mbus_code;
  1051. /*
  1052. * Tell V4L that we exist.
  1053. */
  1054. ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
  1055. if (ret) {
  1056. dev_err(&pdev->dev, "Unable to register v4l2 device\n");
  1057. goto out_free;
  1058. }
  1059. ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
  1060. if (ret)
  1061. goto out_unregister;
  1062. cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
  1063. /*
  1064. * Convince the system that we can do DMA.
  1065. */
  1066. pdev->dev.dma_mask = &viadev->pdev->dma_mask;
  1067. dma_set_mask(&pdev->dev, 0xffffffff);
  1068. /*
  1069. * Fire up the capture port. The write to 0x78 looks purely
  1070. * OLPCish; any system will need to tweak 0x1e.
  1071. */
  1072. via_write_reg_mask(VIASR, 0x78, 0, 0x80);
  1073. via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
  1074. /*
  1075. * Get the sensor powered up.
  1076. */
  1077. ret = via_sensor_power_setup(cam);
  1078. if (ret)
  1079. goto out_ctrl_hdl_free;
  1080. via_sensor_power_up(cam);
  1081. /*
  1082. * See if we can't find it on the bus. The VIA_PORT_31 assumption
  1083. * is OLPC-specific. 0x42 assumption is ov7670-specific.
  1084. */
  1085. sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
  1086. cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
  1087. &ov7670_info, NULL);
  1088. if (cam->sensor == NULL) {
  1089. dev_err(&pdev->dev, "Unable to find the sensor!\n");
  1090. ret = -ENODEV;
  1091. goto out_power_down;
  1092. }
  1093. /*
  1094. * Get the IRQ.
  1095. */
  1096. viacam_int_disable(cam);
  1097. ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
  1098. viacam_irq, IRQF_SHARED, "via-camera", cam);
  1099. if (ret)
  1100. goto out_power_down;
  1101. vq = &cam->vq;
  1102. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1103. vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1104. vq->drv_priv = cam;
  1105. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1106. vq->buf_struct_size = sizeof(struct via_buffer);
  1107. vq->dev = cam->v4l2_dev.dev;
  1108. vq->ops = &viacam_vb2_ops;
  1109. vq->mem_ops = &vb2_dma_sg_memops;
  1110. vq->lock = &cam->lock;
  1111. ret = vb2_queue_init(vq);
  1112. /*
  1113. * Tell V4l2 that we exist.
  1114. */
  1115. cam->vdev = viacam_v4l_template;
  1116. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1117. cam->vdev.lock = &cam->lock;
  1118. cam->vdev.queue = vq;
  1119. video_set_drvdata(&cam->vdev, cam);
  1120. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1121. if (ret)
  1122. goto out_irq;
  1123. #ifdef CONFIG_PM
  1124. /*
  1125. * Hook into PM events
  1126. */
  1127. viacam_pm_hooks.private = cam;
  1128. viafb_pm_register(&viacam_pm_hooks);
  1129. #endif
  1130. /* Power the sensor down until somebody opens the device */
  1131. via_sensor_power_down(cam);
  1132. return 0;
  1133. out_irq:
  1134. free_irq(viadev->pdev->irq, cam);
  1135. out_power_down:
  1136. via_sensor_power_release(cam);
  1137. out_ctrl_hdl_free:
  1138. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1139. out_unregister:
  1140. v4l2_device_unregister(&cam->v4l2_dev);
  1141. out_free:
  1142. kfree(cam);
  1143. return ret;
  1144. }
  1145. static int viacam_remove(struct platform_device *pdev)
  1146. {
  1147. struct via_camera *cam = via_cam_info;
  1148. struct viafb_dev *viadev = pdev->dev.platform_data;
  1149. video_unregister_device(&cam->vdev);
  1150. v4l2_device_unregister(&cam->v4l2_dev);
  1151. #ifdef CONFIG_PM
  1152. viafb_pm_unregister(&viacam_pm_hooks);
  1153. #endif
  1154. free_irq(viadev->pdev->irq, cam);
  1155. via_sensor_power_release(cam);
  1156. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1157. kfree(cam);
  1158. via_cam_info = NULL;
  1159. return 0;
  1160. }
  1161. static struct platform_driver viacam_driver = {
  1162. .driver = {
  1163. .name = "viafb-camera",
  1164. },
  1165. .probe = viacam_probe,
  1166. .remove = viacam_remove,
  1167. };
  1168. module_platform_driver(viacam_driver);