cafe_ccic.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /*
  2. * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
  3. * multifunction chip. Currently works with the Omnivision OV7670
  4. * sensor.
  5. *
  6. * The data sheet for this device can be found at:
  7. * http://www.marvell.com/products/pcconn/88ALP01.jsp
  8. *
  9. * Copyright 2006 One Laptop Per Child Association, Inc.
  10. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  11. *
  12. * Written by Jonathan Corbet, corbet@lwn.net.
  13. *
  14. * v4l2_device/v4l2_subdev conversion by:
  15. * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
  16. *
  17. * Note: this conversion is untested! Please contact the linux-media
  18. * mailinglist if you can test this, together with the test results.
  19. *
  20. * This file may be distributed under the terms of the GNU General
  21. * Public License, version 2.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/mm.h>
  28. #include <linux/pci.h>
  29. #include <linux/i2c.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/videodev2.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-chip-ident.h>
  36. #include <linux/device.h>
  37. #include <linux/wait.h>
  38. #include <linux/list.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/delay.h>
  41. #include <linux/jiffies.h>
  42. #include <linux/vmalloc.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/io.h>
  45. #include "cafe_ccic-regs.h"
  46. #define CAFE_VERSION 0x000002
  47. /*
  48. * Parameters.
  49. */
  50. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  51. MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
  52. MODULE_LICENSE("GPL");
  53. MODULE_SUPPORTED_DEVICE("Video");
  54. /*
  55. * Internal DMA buffer management. Since the controller cannot do S/G I/O,
  56. * we must have physically contiguous buffers to bring frames into.
  57. * These parameters control how many buffers we use, whether we
  58. * allocate them at load time (better chance of success, but nails down
  59. * memory) or when somebody tries to use the camera (riskier), and,
  60. * for load-time allocation, how big they should be.
  61. *
  62. * The controller can cycle through three buffers. We could use
  63. * more by flipping pointers around, but it probably makes little
  64. * sense.
  65. */
  66. #define MAX_DMA_BUFS 3
  67. static int alloc_bufs_at_read;
  68. module_param(alloc_bufs_at_read, bool, 0444);
  69. MODULE_PARM_DESC(alloc_bufs_at_read,
  70. "Non-zero value causes DMA buffers to be allocated when the "
  71. "video capture device is read, rather than at module load "
  72. "time. This saves memory, but decreases the chances of "
  73. "successfully getting those buffers.");
  74. static int n_dma_bufs = 3;
  75. module_param(n_dma_bufs, uint, 0644);
  76. MODULE_PARM_DESC(n_dma_bufs,
  77. "The number of DMA buffers to allocate. Can be either two "
  78. "(saves memory, makes timing tighter) or three.");
  79. static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
  80. module_param(dma_buf_size, uint, 0444);
  81. MODULE_PARM_DESC(dma_buf_size,
  82. "The size of the allocated DMA buffers. If actual operating "
  83. "parameters require larger buffers, an attempt to reallocate "
  84. "will be made.");
  85. static int min_buffers = 1;
  86. module_param(min_buffers, uint, 0644);
  87. MODULE_PARM_DESC(min_buffers,
  88. "The minimum number of streaming I/O buffers we are willing "
  89. "to work with.");
  90. static int max_buffers = 10;
  91. module_param(max_buffers, uint, 0644);
  92. MODULE_PARM_DESC(max_buffers,
  93. "The maximum number of streaming I/O buffers an application "
  94. "will be allowed to allocate. These buffers are big and live "
  95. "in vmalloc space.");
  96. static int flip;
  97. module_param(flip, bool, 0444);
  98. MODULE_PARM_DESC(flip,
  99. "If set, the sensor will be instructed to flip the image "
  100. "vertically.");
  101. enum cafe_state {
  102. S_NOTREADY, /* Not yet initialized */
  103. S_IDLE, /* Just hanging around */
  104. S_FLAKED, /* Some sort of problem */
  105. S_SINGLEREAD, /* In read() */
  106. S_SPECREAD, /* Speculative read (for future read()) */
  107. S_STREAMING /* Streaming data */
  108. };
  109. /*
  110. * Tracking of streaming I/O buffers.
  111. */
  112. struct cafe_sio_buffer {
  113. struct list_head list;
  114. struct v4l2_buffer v4lbuf;
  115. char *buffer; /* Where it lives in kernel space */
  116. int mapcount;
  117. struct cafe_camera *cam;
  118. };
  119. /*
  120. * A description of one of our devices.
  121. * Locking: controlled by s_mutex. Certain fields, however, require
  122. * the dev_lock spinlock; they are marked as such by comments.
  123. * dev_lock is also required for access to device registers.
  124. */
  125. struct cafe_camera
  126. {
  127. struct v4l2_device v4l2_dev;
  128. enum cafe_state state;
  129. unsigned long flags; /* Buffer status, mainly (dev_lock) */
  130. int users; /* How many open FDs */
  131. struct file *owner; /* Who has data access (v4l2) */
  132. /*
  133. * Subsystem structures.
  134. */
  135. struct pci_dev *pdev;
  136. struct video_device vdev;
  137. struct i2c_adapter i2c_adapter;
  138. struct v4l2_subdev *sensor;
  139. unsigned short sensor_addr;
  140. unsigned char __iomem *regs;
  141. struct list_head dev_list; /* link to other devices */
  142. /* DMA buffers */
  143. unsigned int nbufs; /* How many are alloc'd */
  144. int next_buf; /* Next to consume (dev_lock) */
  145. unsigned int dma_buf_size; /* allocated size */
  146. void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
  147. dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
  148. unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
  149. unsigned int sequence; /* Frame sequence number */
  150. unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
  151. /* Streaming buffers */
  152. unsigned int n_sbufs; /* How many we have */
  153. struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
  154. struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
  155. struct list_head sb_full; /* With data (user space owns) (dev_lock) */
  156. struct tasklet_struct s_tasklet;
  157. /* Current operating parameters */
  158. u32 sensor_type; /* Currently ov7670 only */
  159. struct v4l2_pix_format pix_format;
  160. /* Locks */
  161. struct mutex s_mutex; /* Access to this structure */
  162. spinlock_t dev_lock; /* Access to device */
  163. /* Misc */
  164. wait_queue_head_t smbus_wait; /* Waiting on i2c events */
  165. wait_queue_head_t iowait; /* Waiting on frame data */
  166. };
  167. /*
  168. * Status flags. Always manipulated with bit operations.
  169. */
  170. #define CF_BUF0_VALID 0 /* Buffers valid - first three */
  171. #define CF_BUF1_VALID 1
  172. #define CF_BUF2_VALID 2
  173. #define CF_DMA_ACTIVE 3 /* A frame is incoming */
  174. #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
  175. #define sensor_call(cam, o, f, args...) \
  176. v4l2_subdev_call(cam->sensor, o, f, ##args)
  177. static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
  178. {
  179. return container_of(dev, struct cafe_camera, v4l2_dev);
  180. }
  181. /*
  182. * Start over with DMA buffers - dev_lock needed.
  183. */
  184. static void cafe_reset_buffers(struct cafe_camera *cam)
  185. {
  186. int i;
  187. cam->next_buf = -1;
  188. for (i = 0; i < cam->nbufs; i++)
  189. clear_bit(i, &cam->flags);
  190. cam->specframes = 0;
  191. }
  192. static inline int cafe_needs_config(struct cafe_camera *cam)
  193. {
  194. return test_bit(CF_CONFIG_NEEDED, &cam->flags);
  195. }
  196. static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
  197. {
  198. if (needed)
  199. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  200. else
  201. clear_bit(CF_CONFIG_NEEDED, &cam->flags);
  202. }
  203. /*
  204. * Debugging and related.
  205. */
  206. #define cam_err(cam, fmt, arg...) \
  207. dev_err(&(cam)->pdev->dev, fmt, ##arg);
  208. #define cam_warn(cam, fmt, arg...) \
  209. dev_warn(&(cam)->pdev->dev, fmt, ##arg);
  210. #define cam_dbg(cam, fmt, arg...) \
  211. dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
  212. /* ---------------------------------------------------------------------*/
  213. /*
  214. * Device register I/O
  215. */
  216. static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
  217. unsigned int val)
  218. {
  219. iowrite32(val, cam->regs + reg);
  220. }
  221. static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
  222. unsigned int reg)
  223. {
  224. return ioread32(cam->regs + reg);
  225. }
  226. static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
  227. unsigned int val, unsigned int mask)
  228. {
  229. unsigned int v = cafe_reg_read(cam, reg);
  230. v = (v & ~mask) | (val & mask);
  231. cafe_reg_write(cam, reg, v);
  232. }
  233. static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
  234. unsigned int reg, unsigned int val)
  235. {
  236. cafe_reg_write_mask(cam, reg, 0, val);
  237. }
  238. static inline void cafe_reg_set_bit(struct cafe_camera *cam,
  239. unsigned int reg, unsigned int val)
  240. {
  241. cafe_reg_write_mask(cam, reg, val, val);
  242. }
  243. /* -------------------------------------------------------------------- */
  244. /*
  245. * The I2C/SMBUS interface to the camera itself starts here. The
  246. * controller handles SMBUS itself, presenting a relatively simple register
  247. * interface; all we have to do is to tell it where to route the data.
  248. */
  249. #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
  250. static int cafe_smbus_write_done(struct cafe_camera *cam)
  251. {
  252. unsigned long flags;
  253. int c1;
  254. /*
  255. * We must delay after the interrupt, or the controller gets confused
  256. * and never does give us good status. Fortunately, we don't do this
  257. * often.
  258. */
  259. udelay(20);
  260. spin_lock_irqsave(&cam->dev_lock, flags);
  261. c1 = cafe_reg_read(cam, REG_TWSIC1);
  262. spin_unlock_irqrestore(&cam->dev_lock, flags);
  263. return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
  264. }
  265. static int cafe_smbus_write_data(struct cafe_camera *cam,
  266. u16 addr, u8 command, u8 value)
  267. {
  268. unsigned int rval;
  269. unsigned long flags;
  270. DEFINE_WAIT(the_wait);
  271. spin_lock_irqsave(&cam->dev_lock, flags);
  272. rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
  273. rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
  274. /*
  275. * Marvell sez set clkdiv to all 1's for now.
  276. */
  277. rval |= TWSIC0_CLKDIV;
  278. cafe_reg_write(cam, REG_TWSIC0, rval);
  279. (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
  280. rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
  281. cafe_reg_write(cam, REG_TWSIC1, rval);
  282. spin_unlock_irqrestore(&cam->dev_lock, flags);
  283. /*
  284. * Time to wait for the write to complete. THIS IS A RACY
  285. * WAY TO DO IT, but the sad fact is that reading the TWSIC1
  286. * register too quickly after starting the operation sends
  287. * the device into a place that may be kinder and better, but
  288. * which is absolutely useless for controlling the sensor. In
  289. * practice we have plenty of time to get into our sleep state
  290. * before the interrupt hits, and the worst case is that we
  291. * time out and then see that things completed, so this seems
  292. * the best way for now.
  293. */
  294. do {
  295. prepare_to_wait(&cam->smbus_wait, &the_wait,
  296. TASK_UNINTERRUPTIBLE);
  297. schedule_timeout(1); /* even 1 jiffy is too long */
  298. finish_wait(&cam->smbus_wait, &the_wait);
  299. } while (!cafe_smbus_write_done(cam));
  300. #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
  301. wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
  302. CAFE_SMBUS_TIMEOUT);
  303. #endif
  304. spin_lock_irqsave(&cam->dev_lock, flags);
  305. rval = cafe_reg_read(cam, REG_TWSIC1);
  306. spin_unlock_irqrestore(&cam->dev_lock, flags);
  307. if (rval & TWSIC1_WSTAT) {
  308. cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
  309. command, value);
  310. return -EIO;
  311. }
  312. if (rval & TWSIC1_ERROR) {
  313. cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
  314. command, value);
  315. return -EIO;
  316. }
  317. return 0;
  318. }
  319. static int cafe_smbus_read_done(struct cafe_camera *cam)
  320. {
  321. unsigned long flags;
  322. int c1;
  323. /*
  324. * We must delay after the interrupt, or the controller gets confused
  325. * and never does give us good status. Fortunately, we don't do this
  326. * often.
  327. */
  328. udelay(20);
  329. spin_lock_irqsave(&cam->dev_lock, flags);
  330. c1 = cafe_reg_read(cam, REG_TWSIC1);
  331. spin_unlock_irqrestore(&cam->dev_lock, flags);
  332. return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
  333. }
  334. static int cafe_smbus_read_data(struct cafe_camera *cam,
  335. u16 addr, u8 command, u8 *value)
  336. {
  337. unsigned int rval;
  338. unsigned long flags;
  339. spin_lock_irqsave(&cam->dev_lock, flags);
  340. rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
  341. rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
  342. /*
  343. * Marvel sez set clkdiv to all 1's for now.
  344. */
  345. rval |= TWSIC0_CLKDIV;
  346. cafe_reg_write(cam, REG_TWSIC0, rval);
  347. (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
  348. rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
  349. cafe_reg_write(cam, REG_TWSIC1, rval);
  350. spin_unlock_irqrestore(&cam->dev_lock, flags);
  351. wait_event_timeout(cam->smbus_wait,
  352. cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
  353. spin_lock_irqsave(&cam->dev_lock, flags);
  354. rval = cafe_reg_read(cam, REG_TWSIC1);
  355. spin_unlock_irqrestore(&cam->dev_lock, flags);
  356. if (rval & TWSIC1_ERROR) {
  357. cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
  358. return -EIO;
  359. }
  360. if (! (rval & TWSIC1_RVALID)) {
  361. cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
  362. command);
  363. return -EIO;
  364. }
  365. *value = rval & 0xff;
  366. return 0;
  367. }
  368. /*
  369. * Perform a transfer over SMBUS. This thing is called under
  370. * the i2c bus lock, so we shouldn't race with ourselves...
  371. */
  372. static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  373. unsigned short flags, char rw, u8 command,
  374. int size, union i2c_smbus_data *data)
  375. {
  376. struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
  377. struct cafe_camera *cam = to_cam(v4l2_dev);
  378. int ret = -EINVAL;
  379. /*
  380. * This interface would appear to only do byte data ops. OK
  381. * it can do word too, but the cam chip has no use for that.
  382. */
  383. if (size != I2C_SMBUS_BYTE_DATA) {
  384. cam_err(cam, "funky xfer size %d\n", size);
  385. return -EINVAL;
  386. }
  387. if (rw == I2C_SMBUS_WRITE)
  388. ret = cafe_smbus_write_data(cam, addr, command, data->byte);
  389. else if (rw == I2C_SMBUS_READ)
  390. ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
  391. return ret;
  392. }
  393. static void cafe_smbus_enable_irq(struct cafe_camera *cam)
  394. {
  395. unsigned long flags;
  396. spin_lock_irqsave(&cam->dev_lock, flags);
  397. cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
  398. spin_unlock_irqrestore(&cam->dev_lock, flags);
  399. }
  400. static u32 cafe_smbus_func(struct i2c_adapter *adapter)
  401. {
  402. return I2C_FUNC_SMBUS_READ_BYTE_DATA |
  403. I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
  404. }
  405. static struct i2c_algorithm cafe_smbus_algo = {
  406. .smbus_xfer = cafe_smbus_xfer,
  407. .functionality = cafe_smbus_func
  408. };
  409. /* Somebody is on the bus */
  410. static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
  411. static void cafe_ctlr_power_down(struct cafe_camera *cam);
  412. static int cafe_smbus_setup(struct cafe_camera *cam)
  413. {
  414. struct i2c_adapter *adap = &cam->i2c_adapter;
  415. int ret;
  416. cafe_smbus_enable_irq(cam);
  417. adap->id = I2C_HW_SMBUS_CAFE;
  418. adap->owner = THIS_MODULE;
  419. adap->algo = &cafe_smbus_algo;
  420. strcpy(adap->name, "cafe_ccic");
  421. adap->dev.parent = &cam->pdev->dev;
  422. i2c_set_adapdata(adap, &cam->v4l2_dev);
  423. ret = i2c_add_adapter(adap);
  424. if (ret)
  425. printk(KERN_ERR "Unable to register cafe i2c adapter\n");
  426. return ret;
  427. }
  428. static void cafe_smbus_shutdown(struct cafe_camera *cam)
  429. {
  430. i2c_del_adapter(&cam->i2c_adapter);
  431. }
  432. /* ------------------------------------------------------------------- */
  433. /*
  434. * Deal with the controller.
  435. */
  436. /*
  437. * Do everything we think we need to have the interface operating
  438. * according to the desired format.
  439. */
  440. static void cafe_ctlr_dma(struct cafe_camera *cam)
  441. {
  442. /*
  443. * Store the first two Y buffers (we aren't supporting
  444. * planar formats for now, so no UV bufs). Then either
  445. * set the third if it exists, or tell the controller
  446. * to just use two.
  447. */
  448. cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
  449. cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
  450. if (cam->nbufs > 2) {
  451. cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
  452. cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
  453. }
  454. else
  455. cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
  456. cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
  457. }
  458. static void cafe_ctlr_image(struct cafe_camera *cam)
  459. {
  460. int imgsz;
  461. struct v4l2_pix_format *fmt = &cam->pix_format;
  462. imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
  463. (fmt->bytesperline & IMGSZ_H_MASK);
  464. cafe_reg_write(cam, REG_IMGSIZE, imgsz);
  465. cafe_reg_write(cam, REG_IMGOFFSET, 0);
  466. /* YPITCH just drops the last two bits */
  467. cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
  468. IMGP_YP_MASK);
  469. /*
  470. * Tell the controller about the image format we are using.
  471. */
  472. switch (cam->pix_format.pixelformat) {
  473. case V4L2_PIX_FMT_YUYV:
  474. cafe_reg_write_mask(cam, REG_CTRL0,
  475. C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
  476. C0_DF_MASK);
  477. break;
  478. case V4L2_PIX_FMT_RGB444:
  479. cafe_reg_write_mask(cam, REG_CTRL0,
  480. C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
  481. C0_DF_MASK);
  482. /* Alpha value? */
  483. break;
  484. case V4L2_PIX_FMT_RGB565:
  485. cafe_reg_write_mask(cam, REG_CTRL0,
  486. C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
  487. C0_DF_MASK);
  488. break;
  489. default:
  490. cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
  491. break;
  492. }
  493. /*
  494. * Make sure it knows we want to use hsync/vsync.
  495. */
  496. cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
  497. C0_SIFM_MASK);
  498. }
  499. /*
  500. * Configure the controller for operation; caller holds the
  501. * device mutex.
  502. */
  503. static int cafe_ctlr_configure(struct cafe_camera *cam)
  504. {
  505. unsigned long flags;
  506. spin_lock_irqsave(&cam->dev_lock, flags);
  507. cafe_ctlr_dma(cam);
  508. cafe_ctlr_image(cam);
  509. cafe_set_config_needed(cam, 0);
  510. spin_unlock_irqrestore(&cam->dev_lock, flags);
  511. return 0;
  512. }
  513. static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
  514. {
  515. /*
  516. * Clear any pending interrupts, since we do not
  517. * expect to have I/O active prior to enabling.
  518. */
  519. cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
  520. cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
  521. }
  522. static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
  523. {
  524. cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
  525. }
  526. /*
  527. * Make the controller start grabbing images. Everything must
  528. * be set up before doing this.
  529. */
  530. static void cafe_ctlr_start(struct cafe_camera *cam)
  531. {
  532. /* set_bit performs a read, so no other barrier should be
  533. needed here */
  534. cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
  535. }
  536. static void cafe_ctlr_stop(struct cafe_camera *cam)
  537. {
  538. cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  539. }
  540. static void cafe_ctlr_init(struct cafe_camera *cam)
  541. {
  542. unsigned long flags;
  543. spin_lock_irqsave(&cam->dev_lock, flags);
  544. /*
  545. * Added magic to bring up the hardware on the B-Test board
  546. */
  547. cafe_reg_write(cam, 0x3038, 0x8);
  548. cafe_reg_write(cam, 0x315c, 0x80008);
  549. /*
  550. * Go through the dance needed to wake the device up.
  551. * Note that these registers are global and shared
  552. * with the NAND and SD devices. Interaction between the
  553. * three still needs to be examined.
  554. */
  555. cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
  556. cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
  557. cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
  558. /*
  559. * Here we must wait a bit for the controller to come around.
  560. */
  561. spin_unlock_irqrestore(&cam->dev_lock, flags);
  562. msleep(5);
  563. spin_lock_irqsave(&cam->dev_lock, flags);
  564. cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
  565. cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
  566. /*
  567. * Make sure it's not powered down.
  568. */
  569. cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  570. /*
  571. * Turn off the enable bit. It sure should be off anyway,
  572. * but it's good to be sure.
  573. */
  574. cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  575. /*
  576. * Mask all interrupts.
  577. */
  578. cafe_reg_write(cam, REG_IRQMASK, 0);
  579. /*
  580. * Clock the sensor appropriately. Controller clock should
  581. * be 48MHz, sensor "typical" value is half that.
  582. */
  583. cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
  584. spin_unlock_irqrestore(&cam->dev_lock, flags);
  585. }
  586. /*
  587. * Stop the controller, and don't return until we're really sure that no
  588. * further DMA is going on.
  589. */
  590. static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
  591. {
  592. unsigned long flags;
  593. /*
  594. * Theory: stop the camera controller (whether it is operating
  595. * or not). Delay briefly just in case we race with the SOF
  596. * interrupt, then wait until no DMA is active.
  597. */
  598. spin_lock_irqsave(&cam->dev_lock, flags);
  599. cafe_ctlr_stop(cam);
  600. spin_unlock_irqrestore(&cam->dev_lock, flags);
  601. mdelay(1);
  602. wait_event_timeout(cam->iowait,
  603. !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
  604. if (test_bit(CF_DMA_ACTIVE, &cam->flags))
  605. cam_err(cam, "Timeout waiting for DMA to end\n");
  606. /* This would be bad news - what now? */
  607. spin_lock_irqsave(&cam->dev_lock, flags);
  608. cam->state = S_IDLE;
  609. cafe_ctlr_irq_disable(cam);
  610. spin_unlock_irqrestore(&cam->dev_lock, flags);
  611. }
  612. /*
  613. * Power up and down.
  614. */
  615. static void cafe_ctlr_power_up(struct cafe_camera *cam)
  616. {
  617. unsigned long flags;
  618. spin_lock_irqsave(&cam->dev_lock, flags);
  619. cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  620. /*
  621. * Part one of the sensor dance: turn the global
  622. * GPIO signal on.
  623. */
  624. cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
  625. cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
  626. /*
  627. * Put the sensor into operational mode (assumes OLPC-style
  628. * wiring). Control 0 is reset - set to 1 to operate.
  629. * Control 1 is power down, set to 0 to operate.
  630. */
  631. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
  632. /* mdelay(1); */ /* Marvell says 1ms will do it */
  633. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
  634. /* mdelay(1); */ /* Enough? */
  635. spin_unlock_irqrestore(&cam->dev_lock, flags);
  636. msleep(5); /* Just to be sure */
  637. }
  638. static void cafe_ctlr_power_down(struct cafe_camera *cam)
  639. {
  640. unsigned long flags;
  641. spin_lock_irqsave(&cam->dev_lock, flags);
  642. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
  643. cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
  644. cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
  645. cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
  646. spin_unlock_irqrestore(&cam->dev_lock, flags);
  647. }
  648. /* -------------------------------------------------------------------- */
  649. /*
  650. * Communications with the sensor.
  651. */
  652. static int __cafe_cam_reset(struct cafe_camera *cam)
  653. {
  654. return sensor_call(cam, core, reset, 0);
  655. }
  656. /*
  657. * We have found the sensor on the i2c. Let's try to have a
  658. * conversation.
  659. */
  660. static int cafe_cam_init(struct cafe_camera *cam)
  661. {
  662. struct v4l2_dbg_chip_ident chip;
  663. int ret;
  664. mutex_lock(&cam->s_mutex);
  665. if (cam->state != S_NOTREADY)
  666. cam_warn(cam, "Cam init with device in funky state %d",
  667. cam->state);
  668. ret = __cafe_cam_reset(cam);
  669. if (ret)
  670. goto out;
  671. chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
  672. chip.match.addr = cam->sensor_addr;
  673. ret = sensor_call(cam, core, g_chip_ident, &chip);
  674. if (ret)
  675. goto out;
  676. cam->sensor_type = chip.ident;
  677. if (cam->sensor_type != V4L2_IDENT_OV7670) {
  678. cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
  679. ret = -EINVAL;
  680. goto out;
  681. }
  682. /* Get/set parameters? */
  683. ret = 0;
  684. cam->state = S_IDLE;
  685. out:
  686. cafe_ctlr_power_down(cam);
  687. mutex_unlock(&cam->s_mutex);
  688. return ret;
  689. }
  690. /*
  691. * Configure the sensor to match the parameters we have. Caller should
  692. * hold s_mutex
  693. */
  694. static int cafe_cam_set_flip(struct cafe_camera *cam)
  695. {
  696. struct v4l2_control ctrl;
  697. memset(&ctrl, 0, sizeof(ctrl));
  698. ctrl.id = V4L2_CID_VFLIP;
  699. ctrl.value = flip;
  700. return sensor_call(cam, core, s_ctrl, &ctrl);
  701. }
  702. static int cafe_cam_configure(struct cafe_camera *cam)
  703. {
  704. struct v4l2_format fmt;
  705. int ret;
  706. if (cam->state != S_IDLE)
  707. return -EINVAL;
  708. fmt.fmt.pix = cam->pix_format;
  709. ret = sensor_call(cam, core, init, 0);
  710. if (ret == 0)
  711. ret = sensor_call(cam, video, s_fmt, &fmt);
  712. /*
  713. * OV7670 does weird things if flip is set *before* format...
  714. */
  715. ret += cafe_cam_set_flip(cam);
  716. return ret;
  717. }
  718. /* -------------------------------------------------------------------- */
  719. /*
  720. * DMA buffer management. These functions need s_mutex held.
  721. */
  722. /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
  723. * does a get_free_pages() call, and we waste a good chunk of an orderN
  724. * allocation. Should try to allocate the whole set in one chunk.
  725. */
  726. static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
  727. {
  728. int i;
  729. cafe_set_config_needed(cam, 1);
  730. if (loadtime)
  731. cam->dma_buf_size = dma_buf_size;
  732. else
  733. cam->dma_buf_size = cam->pix_format.sizeimage;
  734. if (n_dma_bufs > 3)
  735. n_dma_bufs = 3;
  736. cam->nbufs = 0;
  737. for (i = 0; i < n_dma_bufs; i++) {
  738. cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
  739. cam->dma_buf_size, cam->dma_handles + i,
  740. GFP_KERNEL);
  741. if (cam->dma_bufs[i] == NULL) {
  742. cam_warn(cam, "Failed to allocate DMA buffer\n");
  743. break;
  744. }
  745. /* For debug, remove eventually */
  746. memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
  747. (cam->nbufs)++;
  748. }
  749. switch (cam->nbufs) {
  750. case 1:
  751. dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
  752. cam->dma_bufs[0], cam->dma_handles[0]);
  753. cam->nbufs = 0;
  754. case 0:
  755. cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
  756. return -ENOMEM;
  757. case 2:
  758. if (n_dma_bufs > 2)
  759. cam_warn(cam, "Will limp along with only 2 buffers\n");
  760. break;
  761. }
  762. return 0;
  763. }
  764. static void cafe_free_dma_bufs(struct cafe_camera *cam)
  765. {
  766. int i;
  767. for (i = 0; i < cam->nbufs; i++) {
  768. dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
  769. cam->dma_bufs[i], cam->dma_handles[i]);
  770. cam->dma_bufs[i] = NULL;
  771. }
  772. cam->nbufs = 0;
  773. }
  774. /* ----------------------------------------------------------------------- */
  775. /*
  776. * Here starts the V4L2 interface code.
  777. */
  778. /*
  779. * Read an image from the device.
  780. */
  781. static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
  782. char __user *buffer, size_t len, loff_t *pos)
  783. {
  784. int bufno;
  785. unsigned long flags;
  786. spin_lock_irqsave(&cam->dev_lock, flags);
  787. if (cam->next_buf < 0) {
  788. cam_err(cam, "deliver_buffer: No next buffer\n");
  789. spin_unlock_irqrestore(&cam->dev_lock, flags);
  790. return -EIO;
  791. }
  792. bufno = cam->next_buf;
  793. clear_bit(bufno, &cam->flags);
  794. if (++(cam->next_buf) >= cam->nbufs)
  795. cam->next_buf = 0;
  796. if (! test_bit(cam->next_buf, &cam->flags))
  797. cam->next_buf = -1;
  798. cam->specframes = 0;
  799. spin_unlock_irqrestore(&cam->dev_lock, flags);
  800. if (len > cam->pix_format.sizeimage)
  801. len = cam->pix_format.sizeimage;
  802. if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
  803. return -EFAULT;
  804. (*pos) += len;
  805. return len;
  806. }
  807. /*
  808. * Get everything ready, and start grabbing frames.
  809. */
  810. static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
  811. {
  812. int ret;
  813. unsigned long flags;
  814. /*
  815. * Configuration. If we still don't have DMA buffers,
  816. * make one last, desperate attempt.
  817. */
  818. if (cam->nbufs == 0)
  819. if (cafe_alloc_dma_bufs(cam, 0))
  820. return -ENOMEM;
  821. if (cafe_needs_config(cam)) {
  822. cafe_cam_configure(cam);
  823. ret = cafe_ctlr_configure(cam);
  824. if (ret)
  825. return ret;
  826. }
  827. /*
  828. * Turn it loose.
  829. */
  830. spin_lock_irqsave(&cam->dev_lock, flags);
  831. cafe_reset_buffers(cam);
  832. cafe_ctlr_irq_enable(cam);
  833. cam->state = state;
  834. cafe_ctlr_start(cam);
  835. spin_unlock_irqrestore(&cam->dev_lock, flags);
  836. return 0;
  837. }
  838. static ssize_t cafe_v4l_read(struct file *filp,
  839. char __user *buffer, size_t len, loff_t *pos)
  840. {
  841. struct cafe_camera *cam = filp->private_data;
  842. int ret = 0;
  843. /*
  844. * Perhaps we're in speculative read mode and already
  845. * have data?
  846. */
  847. mutex_lock(&cam->s_mutex);
  848. if (cam->state == S_SPECREAD) {
  849. if (cam->next_buf >= 0) {
  850. ret = cafe_deliver_buffer(cam, buffer, len, pos);
  851. if (ret != 0)
  852. goto out_unlock;
  853. }
  854. } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
  855. ret = -EIO;
  856. goto out_unlock;
  857. } else if (cam->state != S_IDLE) {
  858. ret = -EBUSY;
  859. goto out_unlock;
  860. }
  861. /*
  862. * v4l2: multiple processes can open the device, but only
  863. * one gets to grab data from it.
  864. */
  865. if (cam->owner && cam->owner != filp) {
  866. ret = -EBUSY;
  867. goto out_unlock;
  868. }
  869. cam->owner = filp;
  870. /*
  871. * Do setup if need be.
  872. */
  873. if (cam->state != S_SPECREAD) {
  874. ret = cafe_read_setup(cam, S_SINGLEREAD);
  875. if (ret)
  876. goto out_unlock;
  877. }
  878. /*
  879. * Wait for something to happen. This should probably
  880. * be interruptible (FIXME).
  881. */
  882. wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
  883. if (cam->next_buf < 0) {
  884. cam_err(cam, "read() operation timed out\n");
  885. cafe_ctlr_stop_dma(cam);
  886. ret = -EIO;
  887. goto out_unlock;
  888. }
  889. /*
  890. * Give them their data and we should be done.
  891. */
  892. ret = cafe_deliver_buffer(cam, buffer, len, pos);
  893. out_unlock:
  894. mutex_unlock(&cam->s_mutex);
  895. return ret;
  896. }
  897. /*
  898. * Streaming I/O support.
  899. */
  900. static int cafe_vidioc_streamon(struct file *filp, void *priv,
  901. enum v4l2_buf_type type)
  902. {
  903. struct cafe_camera *cam = filp->private_data;
  904. int ret = -EINVAL;
  905. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  906. goto out;
  907. mutex_lock(&cam->s_mutex);
  908. if (cam->state != S_IDLE || cam->n_sbufs == 0)
  909. goto out_unlock;
  910. cam->sequence = 0;
  911. ret = cafe_read_setup(cam, S_STREAMING);
  912. out_unlock:
  913. mutex_unlock(&cam->s_mutex);
  914. out:
  915. return ret;
  916. }
  917. static int cafe_vidioc_streamoff(struct file *filp, void *priv,
  918. enum v4l2_buf_type type)
  919. {
  920. struct cafe_camera *cam = filp->private_data;
  921. int ret = -EINVAL;
  922. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  923. goto out;
  924. mutex_lock(&cam->s_mutex);
  925. if (cam->state != S_STREAMING)
  926. goto out_unlock;
  927. cafe_ctlr_stop_dma(cam);
  928. ret = 0;
  929. out_unlock:
  930. mutex_unlock(&cam->s_mutex);
  931. out:
  932. return ret;
  933. }
  934. static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
  935. {
  936. struct cafe_sio_buffer *buf = cam->sb_bufs + index;
  937. INIT_LIST_HEAD(&buf->list);
  938. buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
  939. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  940. if (buf->buffer == NULL)
  941. return -ENOMEM;
  942. buf->mapcount = 0;
  943. buf->cam = cam;
  944. buf->v4lbuf.index = index;
  945. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  946. buf->v4lbuf.field = V4L2_FIELD_NONE;
  947. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  948. /*
  949. * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
  950. * just uses the length times the index, but the spec warns
  951. * against doing just that - vma merging problems. So we
  952. * leave a gap between each pair of buffers.
  953. */
  954. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  955. return 0;
  956. }
  957. static int cafe_free_sio_buffers(struct cafe_camera *cam)
  958. {
  959. int i;
  960. /*
  961. * If any buffers are mapped, we cannot free them at all.
  962. */
  963. for (i = 0; i < cam->n_sbufs; i++)
  964. if (cam->sb_bufs[i].mapcount > 0)
  965. return -EBUSY;
  966. /*
  967. * OK, let's do it.
  968. */
  969. for (i = 0; i < cam->n_sbufs; i++)
  970. vfree(cam->sb_bufs[i].buffer);
  971. cam->n_sbufs = 0;
  972. kfree(cam->sb_bufs);
  973. cam->sb_bufs = NULL;
  974. INIT_LIST_HEAD(&cam->sb_avail);
  975. INIT_LIST_HEAD(&cam->sb_full);
  976. return 0;
  977. }
  978. static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
  979. struct v4l2_requestbuffers *req)
  980. {
  981. struct cafe_camera *cam = filp->private_data;
  982. int ret = 0; /* Silence warning */
  983. /*
  984. * Make sure it's something we can do. User pointers could be
  985. * implemented without great pain, but that's not been done yet.
  986. */
  987. if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  988. return -EINVAL;
  989. if (req->memory != V4L2_MEMORY_MMAP)
  990. return -EINVAL;
  991. /*
  992. * If they ask for zero buffers, they really want us to stop streaming
  993. * (if it's happening) and free everything. Should we check owner?
  994. */
  995. mutex_lock(&cam->s_mutex);
  996. if (req->count == 0) {
  997. if (cam->state == S_STREAMING)
  998. cafe_ctlr_stop_dma(cam);
  999. ret = cafe_free_sio_buffers (cam);
  1000. goto out;
  1001. }
  1002. /*
  1003. * Device needs to be idle and working. We *could* try to do the
  1004. * right thing in S_SPECREAD by shutting things down, but it
  1005. * probably doesn't matter.
  1006. */
  1007. if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
  1008. ret = -EBUSY;
  1009. goto out;
  1010. }
  1011. cam->owner = filp;
  1012. if (req->count < min_buffers)
  1013. req->count = min_buffers;
  1014. else if (req->count > max_buffers)
  1015. req->count = max_buffers;
  1016. if (cam->n_sbufs > 0) {
  1017. ret = cafe_free_sio_buffers(cam);
  1018. if (ret)
  1019. goto out;
  1020. }
  1021. cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
  1022. GFP_KERNEL);
  1023. if (cam->sb_bufs == NULL) {
  1024. ret = -ENOMEM;
  1025. goto out;
  1026. }
  1027. for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
  1028. ret = cafe_setup_siobuf(cam, cam->n_sbufs);
  1029. if (ret)
  1030. break;
  1031. }
  1032. if (cam->n_sbufs == 0) /* no luck at all - ret already set */
  1033. kfree(cam->sb_bufs);
  1034. req->count = cam->n_sbufs; /* In case of partial success */
  1035. out:
  1036. mutex_unlock(&cam->s_mutex);
  1037. return ret;
  1038. }
  1039. static int cafe_vidioc_querybuf(struct file *filp, void *priv,
  1040. struct v4l2_buffer *buf)
  1041. {
  1042. struct cafe_camera *cam = filp->private_data;
  1043. int ret = -EINVAL;
  1044. mutex_lock(&cam->s_mutex);
  1045. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1046. goto out;
  1047. if (buf->index < 0 || buf->index >= cam->n_sbufs)
  1048. goto out;
  1049. *buf = cam->sb_bufs[buf->index].v4lbuf;
  1050. ret = 0;
  1051. out:
  1052. mutex_unlock(&cam->s_mutex);
  1053. return ret;
  1054. }
  1055. static int cafe_vidioc_qbuf(struct file *filp, void *priv,
  1056. struct v4l2_buffer *buf)
  1057. {
  1058. struct cafe_camera *cam = filp->private_data;
  1059. struct cafe_sio_buffer *sbuf;
  1060. int ret = -EINVAL;
  1061. unsigned long flags;
  1062. mutex_lock(&cam->s_mutex);
  1063. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1064. goto out;
  1065. if (buf->index < 0 || buf->index >= cam->n_sbufs)
  1066. goto out;
  1067. sbuf = cam->sb_bufs + buf->index;
  1068. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
  1069. ret = 0; /* Already queued?? */
  1070. goto out;
  1071. }
  1072. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
  1073. /* Spec doesn't say anything, seems appropriate tho */
  1074. ret = -EBUSY;
  1075. goto out;
  1076. }
  1077. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  1078. spin_lock_irqsave(&cam->dev_lock, flags);
  1079. list_add(&sbuf->list, &cam->sb_avail);
  1080. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1081. ret = 0;
  1082. out:
  1083. mutex_unlock(&cam->s_mutex);
  1084. return ret;
  1085. }
  1086. static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
  1087. struct v4l2_buffer *buf)
  1088. {
  1089. struct cafe_camera *cam = filp->private_data;
  1090. struct cafe_sio_buffer *sbuf;
  1091. int ret = -EINVAL;
  1092. unsigned long flags;
  1093. mutex_lock(&cam->s_mutex);
  1094. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1095. goto out_unlock;
  1096. if (cam->state != S_STREAMING)
  1097. goto out_unlock;
  1098. if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
  1099. ret = -EAGAIN;
  1100. goto out_unlock;
  1101. }
  1102. while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
  1103. mutex_unlock(&cam->s_mutex);
  1104. if (wait_event_interruptible(cam->iowait,
  1105. !list_empty(&cam->sb_full))) {
  1106. ret = -ERESTARTSYS;
  1107. goto out;
  1108. }
  1109. mutex_lock(&cam->s_mutex);
  1110. }
  1111. if (cam->state != S_STREAMING)
  1112. ret = -EINTR;
  1113. else {
  1114. spin_lock_irqsave(&cam->dev_lock, flags);
  1115. /* Should probably recheck !list_empty() here */
  1116. sbuf = list_entry(cam->sb_full.next,
  1117. struct cafe_sio_buffer, list);
  1118. list_del_init(&sbuf->list);
  1119. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1120. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  1121. *buf = sbuf->v4lbuf;
  1122. ret = 0;
  1123. }
  1124. out_unlock:
  1125. mutex_unlock(&cam->s_mutex);
  1126. out:
  1127. return ret;
  1128. }
  1129. static void cafe_v4l_vm_open(struct vm_area_struct *vma)
  1130. {
  1131. struct cafe_sio_buffer *sbuf = vma->vm_private_data;
  1132. /*
  1133. * Locking: done under mmap_sem, so we don't need to
  1134. * go back to the camera lock here.
  1135. */
  1136. sbuf->mapcount++;
  1137. }
  1138. static void cafe_v4l_vm_close(struct vm_area_struct *vma)
  1139. {
  1140. struct cafe_sio_buffer *sbuf = vma->vm_private_data;
  1141. mutex_lock(&sbuf->cam->s_mutex);
  1142. sbuf->mapcount--;
  1143. /* Docs say we should stop I/O too... */
  1144. if (sbuf->mapcount == 0)
  1145. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  1146. mutex_unlock(&sbuf->cam->s_mutex);
  1147. }
  1148. static struct vm_operations_struct cafe_v4l_vm_ops = {
  1149. .open = cafe_v4l_vm_open,
  1150. .close = cafe_v4l_vm_close
  1151. };
  1152. static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  1153. {
  1154. struct cafe_camera *cam = filp->private_data;
  1155. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1156. int ret = -EINVAL;
  1157. int i;
  1158. struct cafe_sio_buffer *sbuf = NULL;
  1159. if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
  1160. return -EINVAL;
  1161. /*
  1162. * Find the buffer they are looking for.
  1163. */
  1164. mutex_lock(&cam->s_mutex);
  1165. for (i = 0; i < cam->n_sbufs; i++)
  1166. if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
  1167. sbuf = cam->sb_bufs + i;
  1168. break;
  1169. }
  1170. if (sbuf == NULL)
  1171. goto out;
  1172. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  1173. if (ret)
  1174. goto out;
  1175. vma->vm_flags |= VM_DONTEXPAND;
  1176. vma->vm_private_data = sbuf;
  1177. vma->vm_ops = &cafe_v4l_vm_ops;
  1178. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  1179. cafe_v4l_vm_open(vma);
  1180. ret = 0;
  1181. out:
  1182. mutex_unlock(&cam->s_mutex);
  1183. return ret;
  1184. }
  1185. static int cafe_v4l_open(struct file *filp)
  1186. {
  1187. struct cafe_camera *cam = video_drvdata(filp);
  1188. filp->private_data = cam;
  1189. mutex_lock(&cam->s_mutex);
  1190. if (cam->users == 0) {
  1191. cafe_ctlr_power_up(cam);
  1192. __cafe_cam_reset(cam);
  1193. cafe_set_config_needed(cam, 1);
  1194. /* FIXME make sure this is complete */
  1195. }
  1196. (cam->users)++;
  1197. mutex_unlock(&cam->s_mutex);
  1198. return 0;
  1199. }
  1200. static int cafe_v4l_release(struct file *filp)
  1201. {
  1202. struct cafe_camera *cam = filp->private_data;
  1203. mutex_lock(&cam->s_mutex);
  1204. (cam->users)--;
  1205. if (filp == cam->owner) {
  1206. cafe_ctlr_stop_dma(cam);
  1207. cafe_free_sio_buffers(cam);
  1208. cam->owner = NULL;
  1209. }
  1210. if (cam->users == 0) {
  1211. cafe_ctlr_power_down(cam);
  1212. if (alloc_bufs_at_read)
  1213. cafe_free_dma_bufs(cam);
  1214. }
  1215. mutex_unlock(&cam->s_mutex);
  1216. return 0;
  1217. }
  1218. static unsigned int cafe_v4l_poll(struct file *filp,
  1219. struct poll_table_struct *pt)
  1220. {
  1221. struct cafe_camera *cam = filp->private_data;
  1222. poll_wait(filp, &cam->iowait, pt);
  1223. if (cam->next_buf >= 0)
  1224. return POLLIN | POLLRDNORM;
  1225. return 0;
  1226. }
  1227. static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
  1228. struct v4l2_queryctrl *qc)
  1229. {
  1230. struct cafe_camera *cam = priv;
  1231. int ret;
  1232. mutex_lock(&cam->s_mutex);
  1233. ret = sensor_call(cam, core, queryctrl, qc);
  1234. mutex_unlock(&cam->s_mutex);
  1235. return ret;
  1236. }
  1237. static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
  1238. struct v4l2_control *ctrl)
  1239. {
  1240. struct cafe_camera *cam = priv;
  1241. int ret;
  1242. mutex_lock(&cam->s_mutex);
  1243. ret = sensor_call(cam, core, g_ctrl, ctrl);
  1244. mutex_unlock(&cam->s_mutex);
  1245. return ret;
  1246. }
  1247. static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
  1248. struct v4l2_control *ctrl)
  1249. {
  1250. struct cafe_camera *cam = priv;
  1251. int ret;
  1252. mutex_lock(&cam->s_mutex);
  1253. ret = sensor_call(cam, core, s_ctrl, ctrl);
  1254. mutex_unlock(&cam->s_mutex);
  1255. return ret;
  1256. }
  1257. static int cafe_vidioc_querycap(struct file *file, void *priv,
  1258. struct v4l2_capability *cap)
  1259. {
  1260. strcpy(cap->driver, "cafe_ccic");
  1261. strcpy(cap->card, "cafe_ccic");
  1262. cap->version = CAFE_VERSION;
  1263. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1264. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1265. return 0;
  1266. }
  1267. /*
  1268. * The default format we use until somebody says otherwise.
  1269. */
  1270. static struct v4l2_pix_format cafe_def_pix_format = {
  1271. .width = VGA_WIDTH,
  1272. .height = VGA_HEIGHT,
  1273. .pixelformat = V4L2_PIX_FMT_YUYV,
  1274. .field = V4L2_FIELD_NONE,
  1275. .bytesperline = VGA_WIDTH*2,
  1276. .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
  1277. };
  1278. static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
  1279. void *priv, struct v4l2_fmtdesc *fmt)
  1280. {
  1281. struct cafe_camera *cam = priv;
  1282. int ret;
  1283. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1284. return -EINVAL;
  1285. mutex_lock(&cam->s_mutex);
  1286. ret = sensor_call(cam, video, enum_fmt, fmt);
  1287. mutex_unlock(&cam->s_mutex);
  1288. return ret;
  1289. }
  1290. static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  1291. struct v4l2_format *fmt)
  1292. {
  1293. struct cafe_camera *cam = priv;
  1294. int ret;
  1295. mutex_lock(&cam->s_mutex);
  1296. ret = sensor_call(cam, video, try_fmt, fmt);
  1297. mutex_unlock(&cam->s_mutex);
  1298. return ret;
  1299. }
  1300. static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  1301. struct v4l2_format *fmt)
  1302. {
  1303. struct cafe_camera *cam = priv;
  1304. int ret;
  1305. /*
  1306. * Can't do anything if the device is not idle
  1307. * Also can't if there are streaming buffers in place.
  1308. */
  1309. if (cam->state != S_IDLE || cam->n_sbufs > 0)
  1310. return -EBUSY;
  1311. /*
  1312. * See if the formatting works in principle.
  1313. */
  1314. ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
  1315. if (ret)
  1316. return ret;
  1317. /*
  1318. * Now we start to change things for real, so let's do it
  1319. * under lock.
  1320. */
  1321. mutex_lock(&cam->s_mutex);
  1322. cam->pix_format = fmt->fmt.pix;
  1323. /*
  1324. * Make sure we have appropriate DMA buffers.
  1325. */
  1326. ret = -ENOMEM;
  1327. if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
  1328. cafe_free_dma_bufs(cam);
  1329. if (cam->nbufs == 0) {
  1330. if (cafe_alloc_dma_bufs(cam, 0))
  1331. goto out;
  1332. }
  1333. /*
  1334. * It looks like this might work, so let's program the sensor.
  1335. */
  1336. ret = cafe_cam_configure(cam);
  1337. if (! ret)
  1338. ret = cafe_ctlr_configure(cam);
  1339. out:
  1340. mutex_unlock(&cam->s_mutex);
  1341. return ret;
  1342. }
  1343. /*
  1344. * Return our stored notion of how the camera is/should be configured.
  1345. * The V4l2 spec wants us to be smarter, and actually get this from
  1346. * the camera (and not mess with it at open time). Someday.
  1347. */
  1348. static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  1349. struct v4l2_format *f)
  1350. {
  1351. struct cafe_camera *cam = priv;
  1352. f->fmt.pix = cam->pix_format;
  1353. return 0;
  1354. }
  1355. /*
  1356. * We only have one input - the sensor - so minimize the nonsense here.
  1357. */
  1358. static int cafe_vidioc_enum_input(struct file *filp, void *priv,
  1359. struct v4l2_input *input)
  1360. {
  1361. if (input->index != 0)
  1362. return -EINVAL;
  1363. input->type = V4L2_INPUT_TYPE_CAMERA;
  1364. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  1365. strcpy(input->name, "Camera");
  1366. return 0;
  1367. }
  1368. static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  1369. {
  1370. *i = 0;
  1371. return 0;
  1372. }
  1373. static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  1374. {
  1375. if (i != 0)
  1376. return -EINVAL;
  1377. return 0;
  1378. }
  1379. /* from vivi.c */
  1380. static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  1381. {
  1382. return 0;
  1383. }
  1384. /*
  1385. * G/S_PARM. Most of this is done by the sensor, but we are
  1386. * the level which controls the number of read buffers.
  1387. */
  1388. static int cafe_vidioc_g_parm(struct file *filp, void *priv,
  1389. struct v4l2_streamparm *parms)
  1390. {
  1391. struct cafe_camera *cam = priv;
  1392. int ret;
  1393. mutex_lock(&cam->s_mutex);
  1394. ret = sensor_call(cam, video, g_parm, parms);
  1395. mutex_unlock(&cam->s_mutex);
  1396. parms->parm.capture.readbuffers = n_dma_bufs;
  1397. return ret;
  1398. }
  1399. static int cafe_vidioc_s_parm(struct file *filp, void *priv,
  1400. struct v4l2_streamparm *parms)
  1401. {
  1402. struct cafe_camera *cam = priv;
  1403. int ret;
  1404. mutex_lock(&cam->s_mutex);
  1405. ret = sensor_call(cam, video, s_parm, parms);
  1406. mutex_unlock(&cam->s_mutex);
  1407. parms->parm.capture.readbuffers = n_dma_bufs;
  1408. return ret;
  1409. }
  1410. static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
  1411. struct v4l2_dbg_chip_ident *chip)
  1412. {
  1413. struct cafe_camera *cam = priv;
  1414. chip->ident = V4L2_IDENT_NONE;
  1415. chip->revision = 0;
  1416. if (v4l2_chip_match_host(&chip->match)) {
  1417. chip->ident = V4L2_IDENT_CAFE;
  1418. return 0;
  1419. }
  1420. return sensor_call(cam, core, g_chip_ident, chip);
  1421. }
  1422. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1423. static int cafe_vidioc_g_register(struct file *file, void *priv,
  1424. struct v4l2_dbg_register *reg)
  1425. {
  1426. struct cafe_camera *cam = priv;
  1427. if (v4l2_chip_match_host(&reg->match)) {
  1428. reg->val = cafe_reg_read(cam, reg->reg);
  1429. reg->size = 4;
  1430. return 0;
  1431. }
  1432. return sensor_call(cam, core, g_register, reg);
  1433. }
  1434. static int cafe_vidioc_s_register(struct file *file, void *priv,
  1435. struct v4l2_dbg_register *reg)
  1436. {
  1437. struct cafe_camera *cam = priv;
  1438. if (v4l2_chip_match_host(&reg->match)) {
  1439. cafe_reg_write(cam, reg->reg, reg->val);
  1440. return 0;
  1441. }
  1442. return sensor_call(cam, core, s_register, reg);
  1443. }
  1444. #endif
  1445. /*
  1446. * This template device holds all of those v4l2 methods; we
  1447. * clone it for specific real devices.
  1448. */
  1449. static const struct v4l2_file_operations cafe_v4l_fops = {
  1450. .owner = THIS_MODULE,
  1451. .open = cafe_v4l_open,
  1452. .release = cafe_v4l_release,
  1453. .read = cafe_v4l_read,
  1454. .poll = cafe_v4l_poll,
  1455. .mmap = cafe_v4l_mmap,
  1456. .ioctl = video_ioctl2,
  1457. };
  1458. static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
  1459. .vidioc_querycap = cafe_vidioc_querycap,
  1460. .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
  1461. .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
  1462. .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
  1463. .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
  1464. .vidioc_enum_input = cafe_vidioc_enum_input,
  1465. .vidioc_g_input = cafe_vidioc_g_input,
  1466. .vidioc_s_input = cafe_vidioc_s_input,
  1467. .vidioc_s_std = cafe_vidioc_s_std,
  1468. .vidioc_reqbufs = cafe_vidioc_reqbufs,
  1469. .vidioc_querybuf = cafe_vidioc_querybuf,
  1470. .vidioc_qbuf = cafe_vidioc_qbuf,
  1471. .vidioc_dqbuf = cafe_vidioc_dqbuf,
  1472. .vidioc_streamon = cafe_vidioc_streamon,
  1473. .vidioc_streamoff = cafe_vidioc_streamoff,
  1474. .vidioc_queryctrl = cafe_vidioc_queryctrl,
  1475. .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
  1476. .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
  1477. .vidioc_g_parm = cafe_vidioc_g_parm,
  1478. .vidioc_s_parm = cafe_vidioc_s_parm,
  1479. .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident,
  1480. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1481. .vidioc_g_register = cafe_vidioc_g_register,
  1482. .vidioc_s_register = cafe_vidioc_s_register,
  1483. #endif
  1484. };
  1485. static struct video_device cafe_v4l_template = {
  1486. .name = "cafe",
  1487. .minor = -1, /* Get one dynamically */
  1488. .tvnorms = V4L2_STD_NTSC_M,
  1489. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1490. .fops = &cafe_v4l_fops,
  1491. .ioctl_ops = &cafe_v4l_ioctl_ops,
  1492. .release = video_device_release_empty,
  1493. };
  1494. /* ---------------------------------------------------------------------- */
  1495. /*
  1496. * Interrupt handler stuff
  1497. */
  1498. static void cafe_frame_tasklet(unsigned long data)
  1499. {
  1500. struct cafe_camera *cam = (struct cafe_camera *) data;
  1501. int i;
  1502. unsigned long flags;
  1503. struct cafe_sio_buffer *sbuf;
  1504. spin_lock_irqsave(&cam->dev_lock, flags);
  1505. for (i = 0; i < cam->nbufs; i++) {
  1506. int bufno = cam->next_buf;
  1507. if (bufno < 0) { /* "will never happen" */
  1508. cam_err(cam, "No valid bufs in tasklet!\n");
  1509. break;
  1510. }
  1511. if (++(cam->next_buf) >= cam->nbufs)
  1512. cam->next_buf = 0;
  1513. if (! test_bit(bufno, &cam->flags))
  1514. continue;
  1515. if (list_empty(&cam->sb_avail))
  1516. break; /* Leave it valid, hope for better later */
  1517. clear_bit(bufno, &cam->flags);
  1518. sbuf = list_entry(cam->sb_avail.next,
  1519. struct cafe_sio_buffer, list);
  1520. /*
  1521. * Drop the lock during the big copy. This *should* be safe...
  1522. */
  1523. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1524. memcpy(sbuf->buffer, cam->dma_bufs[bufno],
  1525. cam->pix_format.sizeimage);
  1526. sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
  1527. sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
  1528. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1529. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1530. spin_lock_irqsave(&cam->dev_lock, flags);
  1531. list_move_tail(&sbuf->list, &cam->sb_full);
  1532. }
  1533. if (! list_empty(&cam->sb_full))
  1534. wake_up(&cam->iowait);
  1535. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1536. }
  1537. static void cafe_frame_complete(struct cafe_camera *cam, int frame)
  1538. {
  1539. /*
  1540. * Basic frame housekeeping.
  1541. */
  1542. if (test_bit(frame, &cam->flags) && printk_ratelimit())
  1543. cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
  1544. set_bit(frame, &cam->flags);
  1545. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1546. if (cam->next_buf < 0)
  1547. cam->next_buf = frame;
  1548. cam->buf_seq[frame] = ++(cam->sequence);
  1549. switch (cam->state) {
  1550. /*
  1551. * If in single read mode, try going speculative.
  1552. */
  1553. case S_SINGLEREAD:
  1554. cam->state = S_SPECREAD;
  1555. cam->specframes = 0;
  1556. wake_up(&cam->iowait);
  1557. break;
  1558. /*
  1559. * If we are already doing speculative reads, and nobody is
  1560. * reading them, just stop.
  1561. */
  1562. case S_SPECREAD:
  1563. if (++(cam->specframes) >= cam->nbufs) {
  1564. cafe_ctlr_stop(cam);
  1565. cafe_ctlr_irq_disable(cam);
  1566. cam->state = S_IDLE;
  1567. }
  1568. wake_up(&cam->iowait);
  1569. break;
  1570. /*
  1571. * For the streaming case, we defer the real work to the
  1572. * camera tasklet.
  1573. *
  1574. * FIXME: if the application is not consuming the buffers,
  1575. * we should eventually put things on hold and restart in
  1576. * vidioc_dqbuf().
  1577. */
  1578. case S_STREAMING:
  1579. tasklet_schedule(&cam->s_tasklet);
  1580. break;
  1581. default:
  1582. cam_err(cam, "Frame interrupt in non-operational state\n");
  1583. break;
  1584. }
  1585. }
  1586. static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
  1587. {
  1588. unsigned int frame;
  1589. cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1590. /*
  1591. * Handle any frame completions. There really should
  1592. * not be more than one of these, or we have fallen
  1593. * far behind.
  1594. */
  1595. for (frame = 0; frame < cam->nbufs; frame++)
  1596. if (irqs & (IRQ_EOF0 << frame))
  1597. cafe_frame_complete(cam, frame);
  1598. /*
  1599. * If a frame starts, note that we have DMA active. This
  1600. * code assumes that we won't get multiple frame interrupts
  1601. * at once; may want to rethink that.
  1602. */
  1603. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
  1604. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1605. }
  1606. static irqreturn_t cafe_irq(int irq, void *data)
  1607. {
  1608. struct cafe_camera *cam = data;
  1609. unsigned int irqs;
  1610. spin_lock(&cam->dev_lock);
  1611. irqs = cafe_reg_read(cam, REG_IRQSTAT);
  1612. if ((irqs & ALLIRQS) == 0) {
  1613. spin_unlock(&cam->dev_lock);
  1614. return IRQ_NONE;
  1615. }
  1616. if (irqs & FRAMEIRQS)
  1617. cafe_frame_irq(cam, irqs);
  1618. if (irqs & TWSIIRQS) {
  1619. cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
  1620. wake_up(&cam->smbus_wait);
  1621. }
  1622. spin_unlock(&cam->dev_lock);
  1623. return IRQ_HANDLED;
  1624. }
  1625. /* -------------------------------------------------------------------------- */
  1626. /*
  1627. * PCI interface stuff.
  1628. */
  1629. static int cafe_pci_probe(struct pci_dev *pdev,
  1630. const struct pci_device_id *id)
  1631. {
  1632. int ret;
  1633. struct cafe_camera *cam;
  1634. /*
  1635. * Start putting together one of our big camera structures.
  1636. */
  1637. ret = -ENOMEM;
  1638. cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
  1639. if (cam == NULL)
  1640. goto out;
  1641. ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
  1642. if (ret)
  1643. goto out_free;
  1644. mutex_init(&cam->s_mutex);
  1645. mutex_lock(&cam->s_mutex);
  1646. spin_lock_init(&cam->dev_lock);
  1647. cam->state = S_NOTREADY;
  1648. cafe_set_config_needed(cam, 1);
  1649. init_waitqueue_head(&cam->smbus_wait);
  1650. init_waitqueue_head(&cam->iowait);
  1651. cam->pdev = pdev;
  1652. cam->pix_format = cafe_def_pix_format;
  1653. INIT_LIST_HEAD(&cam->dev_list);
  1654. INIT_LIST_HEAD(&cam->sb_avail);
  1655. INIT_LIST_HEAD(&cam->sb_full);
  1656. tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
  1657. /*
  1658. * Get set up on the PCI bus.
  1659. */
  1660. ret = pci_enable_device(pdev);
  1661. if (ret)
  1662. goto out_unreg;
  1663. pci_set_master(pdev);
  1664. ret = -EIO;
  1665. cam->regs = pci_iomap(pdev, 0, 0);
  1666. if (! cam->regs) {
  1667. printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
  1668. goto out_unreg;
  1669. }
  1670. ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
  1671. if (ret)
  1672. goto out_iounmap;
  1673. /*
  1674. * Initialize the controller and leave it powered up. It will
  1675. * stay that way until the sensor driver shows up.
  1676. */
  1677. cafe_ctlr_init(cam);
  1678. cafe_ctlr_power_up(cam);
  1679. /*
  1680. * Set up I2C/SMBUS communications. We have to drop the mutex here
  1681. * because the sensor could attach in this call chain, leading to
  1682. * unsightly deadlocks.
  1683. */
  1684. mutex_unlock(&cam->s_mutex); /* attach can deadlock */
  1685. ret = cafe_smbus_setup(cam);
  1686. if (ret)
  1687. goto out_freeirq;
  1688. cam->sensor_addr = 0x42;
  1689. cam->sensor = v4l2_i2c_new_subdev(&cam->i2c_adapter,
  1690. "ov7670", "ov7670", cam->sensor_addr);
  1691. if (cam->sensor == NULL) {
  1692. ret = -ENODEV;
  1693. goto out_smbus;
  1694. }
  1695. ret = cafe_cam_init(cam);
  1696. if (ret)
  1697. goto out_smbus;
  1698. /*
  1699. * Get the v4l2 setup done.
  1700. */
  1701. mutex_lock(&cam->s_mutex);
  1702. cam->vdev = cafe_v4l_template;
  1703. cam->vdev.debug = 0;
  1704. /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
  1705. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1706. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1707. if (ret)
  1708. goto out_smbus;
  1709. video_set_drvdata(&cam->vdev, cam);
  1710. /*
  1711. * If so requested, try to get our DMA buffers now.
  1712. */
  1713. if (!alloc_bufs_at_read) {
  1714. if (cafe_alloc_dma_bufs(cam, 1))
  1715. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1716. " will try again later.");
  1717. }
  1718. mutex_unlock(&cam->s_mutex);
  1719. return 0;
  1720. out_smbus:
  1721. cafe_smbus_shutdown(cam);
  1722. out_freeirq:
  1723. cafe_ctlr_power_down(cam);
  1724. free_irq(pdev->irq, cam);
  1725. out_iounmap:
  1726. pci_iounmap(pdev, cam->regs);
  1727. out_free:
  1728. v4l2_device_unregister(&cam->v4l2_dev);
  1729. out_unreg:
  1730. kfree(cam);
  1731. out:
  1732. return ret;
  1733. }
  1734. /*
  1735. * Shut down an initialized device
  1736. */
  1737. static void cafe_shutdown(struct cafe_camera *cam)
  1738. {
  1739. /* FIXME: Make sure we take care of everything here */
  1740. if (cam->n_sbufs > 0)
  1741. /* What if they are still mapped? Shouldn't be, but... */
  1742. cafe_free_sio_buffers(cam);
  1743. cafe_ctlr_stop_dma(cam);
  1744. cafe_ctlr_power_down(cam);
  1745. cafe_smbus_shutdown(cam);
  1746. cafe_free_dma_bufs(cam);
  1747. free_irq(cam->pdev->irq, cam);
  1748. pci_iounmap(cam->pdev, cam->regs);
  1749. video_unregister_device(&cam->vdev);
  1750. }
  1751. static void cafe_pci_remove(struct pci_dev *pdev)
  1752. {
  1753. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  1754. struct cafe_camera *cam = to_cam(v4l2_dev);
  1755. if (cam == NULL) {
  1756. printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
  1757. return;
  1758. }
  1759. mutex_lock(&cam->s_mutex);
  1760. if (cam->users > 0)
  1761. cam_warn(cam, "Removing a device with users!\n");
  1762. cafe_shutdown(cam);
  1763. v4l2_device_unregister(&cam->v4l2_dev);
  1764. kfree(cam);
  1765. /* No unlock - it no longer exists */
  1766. }
  1767. #ifdef CONFIG_PM
  1768. /*
  1769. * Basic power management.
  1770. */
  1771. static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1772. {
  1773. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  1774. struct cafe_camera *cam = to_cam(v4l2_dev);
  1775. int ret;
  1776. enum cafe_state cstate;
  1777. ret = pci_save_state(pdev);
  1778. if (ret)
  1779. return ret;
  1780. cstate = cam->state; /* HACK - stop_dma sets to idle */
  1781. cafe_ctlr_stop_dma(cam);
  1782. cafe_ctlr_power_down(cam);
  1783. pci_disable_device(pdev);
  1784. cam->state = cstate;
  1785. return 0;
  1786. }
  1787. static int cafe_pci_resume(struct pci_dev *pdev)
  1788. {
  1789. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  1790. struct cafe_camera *cam = to_cam(v4l2_dev);
  1791. int ret = 0;
  1792. ret = pci_restore_state(pdev);
  1793. if (ret)
  1794. return ret;
  1795. ret = pci_enable_device(pdev);
  1796. if (ret) {
  1797. cam_warn(cam, "Unable to re-enable device on resume!\n");
  1798. return ret;
  1799. }
  1800. cafe_ctlr_init(cam);
  1801. cafe_ctlr_power_down(cam);
  1802. mutex_lock(&cam->s_mutex);
  1803. if (cam->users > 0) {
  1804. cafe_ctlr_power_up(cam);
  1805. __cafe_cam_reset(cam);
  1806. }
  1807. mutex_unlock(&cam->s_mutex);
  1808. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1809. if (cam->state == S_SPECREAD)
  1810. cam->state = S_IDLE; /* Don't bother restarting */
  1811. else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
  1812. ret = cafe_read_setup(cam, cam->state);
  1813. return ret;
  1814. }
  1815. #endif /* CONFIG_PM */
  1816. static struct pci_device_id cafe_ids[] = {
  1817. { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
  1818. PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
  1819. { 0, }
  1820. };
  1821. MODULE_DEVICE_TABLE(pci, cafe_ids);
  1822. static struct pci_driver cafe_pci_driver = {
  1823. .name = "cafe1000-ccic",
  1824. .id_table = cafe_ids,
  1825. .probe = cafe_pci_probe,
  1826. .remove = cafe_pci_remove,
  1827. #ifdef CONFIG_PM
  1828. .suspend = cafe_pci_suspend,
  1829. .resume = cafe_pci_resume,
  1830. #endif
  1831. };
  1832. static int __init cafe_init(void)
  1833. {
  1834. int ret;
  1835. printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
  1836. CAFE_VERSION);
  1837. ret = pci_register_driver(&cafe_pci_driver);
  1838. if (ret) {
  1839. printk(KERN_ERR "Unable to register cafe_ccic driver\n");
  1840. goto out;
  1841. }
  1842. ret = 0;
  1843. out:
  1844. return ret;
  1845. }
  1846. static void __exit cafe_exit(void)
  1847. {
  1848. pci_unregister_driver(&cafe_pci_driver);
  1849. }
  1850. module_init(cafe_init);
  1851. module_exit(cafe_exit);