cafe_ccic.c 53 KB

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