cafe_ccic.c 55 KB

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