via-camera.c 37 KB

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