via-camera.c 37 KB

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