cafe_ccic.c 52 KB

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