cafe_ccic.c 55 KB

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