cafe_ccic.c 56 KB

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