cafe_ccic.c 57 KB

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