via-camera.c 37 KB

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