cafe_ccic.c 54 KB

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