cafe_ccic.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286
  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. * Put the sensor into operational mode (assumes OLPC-style
  669. * wiring). Control 0 is reset - set to 1 to operate.
  670. * Control 1 is power down, set to 0 to operate.
  671. */
  672. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
  673. // mdelay(1); /* Marvell says 1ms will do it */
  674. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
  675. // mdelay(1); /* Enough? */
  676. spin_unlock_irqrestore(&cam->dev_lock, flags);
  677. }
  678. static void cafe_ctlr_power_down(struct cafe_camera *cam)
  679. {
  680. unsigned long flags;
  681. spin_lock_irqsave(&cam->dev_lock, flags);
  682. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
  683. cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
  684. spin_unlock_irqrestore(&cam->dev_lock, flags);
  685. }
  686. /* -------------------------------------------------------------------- */
  687. /*
  688. * Communications with the sensor.
  689. */
  690. static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
  691. {
  692. struct i2c_client *sc = cam->sensor;
  693. int ret;
  694. if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
  695. return -EINVAL;
  696. ret = sc->driver->command(sc, cmd, arg);
  697. if (ret == -EPERM) /* Unsupported command */
  698. return 0;
  699. return ret;
  700. }
  701. static int __cafe_cam_reset(struct cafe_camera *cam)
  702. {
  703. int zero = 0;
  704. return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
  705. }
  706. /*
  707. * We have found the sensor on the i2c. Let's try to have a
  708. * conversation.
  709. */
  710. static int cafe_cam_init(struct cafe_camera *cam)
  711. {
  712. struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 };
  713. int ret;
  714. mutex_lock(&cam->s_mutex);
  715. if (cam->state != S_NOTREADY)
  716. cam_warn(cam, "Cam init with device in funky state %d",
  717. cam->state);
  718. ret = __cafe_cam_reset(cam);
  719. if (ret)
  720. goto out;
  721. chip.match_chip = cam->sensor->addr;
  722. ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip);
  723. if (ret)
  724. goto out;
  725. cam->sensor_type = chip.ident;
  726. // if (cam->sensor->addr != OV7xx0_SID) {
  727. if (cam->sensor_type != V4L2_IDENT_OV7670) {
  728. cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
  729. ret = -EINVAL;
  730. goto out;
  731. }
  732. /* Get/set parameters? */
  733. ret = 0;
  734. cam->state = S_IDLE;
  735. out:
  736. mutex_unlock(&cam->s_mutex);
  737. return ret;
  738. }
  739. /*
  740. * Configure the sensor to match the parameters we have. Caller should
  741. * hold s_mutex
  742. */
  743. static int cafe_cam_set_flip(struct cafe_camera *cam)
  744. {
  745. struct v4l2_control ctrl;
  746. memset(&ctrl, 0, sizeof(ctrl));
  747. ctrl.id = V4L2_CID_VFLIP;
  748. ctrl.value = flip;
  749. return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
  750. }
  751. static int cafe_cam_configure(struct cafe_camera *cam)
  752. {
  753. struct v4l2_format fmt;
  754. int ret, zero = 0;
  755. if (cam->state != S_IDLE)
  756. return -EINVAL;
  757. fmt.fmt.pix = cam->pix_format;
  758. ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
  759. if (ret == 0)
  760. ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
  761. /*
  762. * OV7670 does weird things if flip is set *before* format...
  763. */
  764. ret += cafe_cam_set_flip(cam);
  765. return ret;
  766. }
  767. /* -------------------------------------------------------------------- */
  768. /*
  769. * DMA buffer management. These functions need s_mutex held.
  770. */
  771. /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
  772. * does a get_free_pages() call, and we waste a good chunk of an orderN
  773. * allocation. Should try to allocate the whole set in one chunk.
  774. */
  775. static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
  776. {
  777. int i;
  778. cafe_set_config_needed(cam, 1);
  779. if (loadtime)
  780. cam->dma_buf_size = dma_buf_size;
  781. else
  782. cam->dma_buf_size = cam->pix_format.sizeimage;
  783. if (n_dma_bufs > 3)
  784. n_dma_bufs = 3;
  785. cam->nbufs = 0;
  786. for (i = 0; i < n_dma_bufs; i++) {
  787. cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
  788. cam->dma_buf_size, cam->dma_handles + i,
  789. GFP_KERNEL);
  790. if (cam->dma_bufs[i] == NULL) {
  791. cam_warn(cam, "Failed to allocate DMA buffer\n");
  792. break;
  793. }
  794. /* For debug, remove eventually */
  795. memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
  796. (cam->nbufs)++;
  797. }
  798. switch (cam->nbufs) {
  799. case 1:
  800. dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
  801. cam->dma_bufs[0], cam->dma_handles[0]);
  802. cam->nbufs = 0;
  803. case 0:
  804. cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
  805. return -ENOMEM;
  806. case 2:
  807. if (n_dma_bufs > 2)
  808. cam_warn(cam, "Will limp along with only 2 buffers\n");
  809. break;
  810. }
  811. return 0;
  812. }
  813. static void cafe_free_dma_bufs(struct cafe_camera *cam)
  814. {
  815. int i;
  816. for (i = 0; i < cam->nbufs; i++) {
  817. dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
  818. cam->dma_bufs[i], cam->dma_handles[i]);
  819. cam->dma_bufs[i] = NULL;
  820. }
  821. cam->nbufs = 0;
  822. }
  823. /* ----------------------------------------------------------------------- */
  824. /*
  825. * Here starts the V4L2 interface code.
  826. */
  827. /*
  828. * Read an image from the device.
  829. */
  830. static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
  831. char __user *buffer, size_t len, loff_t *pos)
  832. {
  833. int bufno;
  834. unsigned long flags;
  835. spin_lock_irqsave(&cam->dev_lock, flags);
  836. if (cam->next_buf < 0) {
  837. cam_err(cam, "deliver_buffer: No next buffer\n");
  838. spin_unlock_irqrestore(&cam->dev_lock, flags);
  839. return -EIO;
  840. }
  841. bufno = cam->next_buf;
  842. clear_bit(bufno, &cam->flags);
  843. if (++(cam->next_buf) >= cam->nbufs)
  844. cam->next_buf = 0;
  845. if (! test_bit(cam->next_buf, &cam->flags))
  846. cam->next_buf = -1;
  847. cam->specframes = 0;
  848. spin_unlock_irqrestore(&cam->dev_lock, flags);
  849. if (len > cam->pix_format.sizeimage)
  850. len = cam->pix_format.sizeimage;
  851. if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
  852. return -EFAULT;
  853. (*pos) += len;
  854. return len;
  855. }
  856. /*
  857. * Get everything ready, and start grabbing frames.
  858. */
  859. static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
  860. {
  861. int ret;
  862. unsigned long flags;
  863. /*
  864. * Configuration. If we still don't have DMA buffers,
  865. * make one last, desperate attempt.
  866. */
  867. if (cam->nbufs == 0)
  868. if (cafe_alloc_dma_bufs(cam, 0))
  869. return -ENOMEM;
  870. if (cafe_needs_config(cam)) {
  871. cafe_cam_configure(cam);
  872. ret = cafe_ctlr_configure(cam);
  873. if (ret)
  874. return ret;
  875. }
  876. /*
  877. * Turn it loose.
  878. */
  879. spin_lock_irqsave(&cam->dev_lock, flags);
  880. cafe_reset_buffers(cam);
  881. cafe_ctlr_irq_enable(cam);
  882. cam->state = state;
  883. cafe_ctlr_start(cam);
  884. spin_unlock_irqrestore(&cam->dev_lock, flags);
  885. return 0;
  886. }
  887. static ssize_t cafe_v4l_read(struct file *filp,
  888. char __user *buffer, size_t len, loff_t *pos)
  889. {
  890. struct cafe_camera *cam = filp->private_data;
  891. int ret = 0;
  892. /*
  893. * Perhaps we're in speculative read mode and already
  894. * have data?
  895. */
  896. mutex_lock(&cam->s_mutex);
  897. if (cam->state == S_SPECREAD) {
  898. if (cam->next_buf >= 0) {
  899. ret = cafe_deliver_buffer(cam, buffer, len, pos);
  900. if (ret != 0)
  901. goto out_unlock;
  902. }
  903. } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
  904. ret = -EIO;
  905. goto out_unlock;
  906. } else if (cam->state != S_IDLE) {
  907. ret = -EBUSY;
  908. goto out_unlock;
  909. }
  910. /*
  911. * v4l2: multiple processes can open the device, but only
  912. * one gets to grab data from it.
  913. */
  914. if (cam->owner && cam->owner != filp) {
  915. ret = -EBUSY;
  916. goto out_unlock;
  917. }
  918. cam->owner = filp;
  919. /*
  920. * Do setup if need be.
  921. */
  922. if (cam->state != S_SPECREAD) {
  923. ret = cafe_read_setup(cam, S_SINGLEREAD);
  924. if (ret)
  925. goto out_unlock;
  926. }
  927. /*
  928. * Wait for something to happen. This should probably
  929. * be interruptible (FIXME).
  930. */
  931. wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
  932. if (cam->next_buf < 0) {
  933. cam_err(cam, "read() operation timed out\n");
  934. cafe_ctlr_stop_dma(cam);
  935. ret = -EIO;
  936. goto out_unlock;
  937. }
  938. /*
  939. * Give them their data and we should be done.
  940. */
  941. ret = cafe_deliver_buffer(cam, buffer, len, pos);
  942. out_unlock:
  943. mutex_unlock(&cam->s_mutex);
  944. return ret;
  945. }
  946. /*
  947. * Streaming I/O support.
  948. */
  949. static int cafe_vidioc_streamon(struct file *filp, void *priv,
  950. enum v4l2_buf_type type)
  951. {
  952. struct cafe_camera *cam = filp->private_data;
  953. int ret = -EINVAL;
  954. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  955. goto out;
  956. mutex_lock(&cam->s_mutex);
  957. if (cam->state != S_IDLE || cam->n_sbufs == 0)
  958. goto out_unlock;
  959. cam->sequence = 0;
  960. ret = cafe_read_setup(cam, S_STREAMING);
  961. out_unlock:
  962. mutex_unlock(&cam->s_mutex);
  963. out:
  964. return ret;
  965. }
  966. static int cafe_vidioc_streamoff(struct file *filp, void *priv,
  967. enum v4l2_buf_type type)
  968. {
  969. struct cafe_camera *cam = filp->private_data;
  970. int ret = -EINVAL;
  971. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  972. goto out;
  973. mutex_lock(&cam->s_mutex);
  974. if (cam->state != S_STREAMING)
  975. goto out_unlock;
  976. cafe_ctlr_stop_dma(cam);
  977. ret = 0;
  978. out_unlock:
  979. mutex_unlock(&cam->s_mutex);
  980. out:
  981. return ret;
  982. }
  983. static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
  984. {
  985. struct cafe_sio_buffer *buf = cam->sb_bufs + index;
  986. INIT_LIST_HEAD(&buf->list);
  987. buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
  988. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  989. if (buf->buffer == NULL)
  990. return -ENOMEM;
  991. buf->mapcount = 0;
  992. buf->cam = cam;
  993. buf->v4lbuf.index = index;
  994. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  995. buf->v4lbuf.field = V4L2_FIELD_NONE;
  996. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  997. /*
  998. * Offset: must be 32-bit even on a 64-bit system. video-buf
  999. * just uses the length times the index, but the spec warns
  1000. * against doing just that - vma merging problems. So we
  1001. * leave a gap between each pair of buffers.
  1002. */
  1003. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  1004. return 0;
  1005. }
  1006. static int cafe_free_sio_buffers(struct cafe_camera *cam)
  1007. {
  1008. int i;
  1009. /*
  1010. * If any buffers are mapped, we cannot free them at all.
  1011. */
  1012. for (i = 0; i < cam->n_sbufs; i++)
  1013. if (cam->sb_bufs[i].mapcount > 0)
  1014. return -EBUSY;
  1015. /*
  1016. * OK, let's do it.
  1017. */
  1018. for (i = 0; i < cam->n_sbufs; i++)
  1019. vfree(cam->sb_bufs[i].buffer);
  1020. cam->n_sbufs = 0;
  1021. kfree(cam->sb_bufs);
  1022. cam->sb_bufs = NULL;
  1023. INIT_LIST_HEAD(&cam->sb_avail);
  1024. INIT_LIST_HEAD(&cam->sb_full);
  1025. return 0;
  1026. }
  1027. static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
  1028. struct v4l2_requestbuffers *req)
  1029. {
  1030. struct cafe_camera *cam = filp->private_data;
  1031. int ret = 0; /* Silence warning */
  1032. /*
  1033. * Make sure it's something we can do. User pointers could be
  1034. * implemented without great pain, but that's not been done yet.
  1035. */
  1036. if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1037. return -EINVAL;
  1038. if (req->memory != V4L2_MEMORY_MMAP)
  1039. return -EINVAL;
  1040. /*
  1041. * If they ask for zero buffers, they really want us to stop streaming
  1042. * (if it's happening) and free everything. Should we check owner?
  1043. */
  1044. mutex_lock(&cam->s_mutex);
  1045. if (req->count == 0) {
  1046. if (cam->state == S_STREAMING)
  1047. cafe_ctlr_stop_dma(cam);
  1048. ret = cafe_free_sio_buffers (cam);
  1049. goto out;
  1050. }
  1051. /*
  1052. * Device needs to be idle and working. We *could* try to do the
  1053. * right thing in S_SPECREAD by shutting things down, but it
  1054. * probably doesn't matter.
  1055. */
  1056. if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
  1057. ret = -EBUSY;
  1058. goto out;
  1059. }
  1060. cam->owner = filp;
  1061. if (req->count < min_buffers)
  1062. req->count = min_buffers;
  1063. else if (req->count > max_buffers)
  1064. req->count = max_buffers;
  1065. if (cam->n_sbufs > 0) {
  1066. ret = cafe_free_sio_buffers(cam);
  1067. if (ret)
  1068. goto out;
  1069. }
  1070. cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
  1071. GFP_KERNEL);
  1072. if (cam->sb_bufs == NULL) {
  1073. ret = -ENOMEM;
  1074. goto out;
  1075. }
  1076. for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
  1077. ret = cafe_setup_siobuf(cam, cam->n_sbufs);
  1078. if (ret)
  1079. break;
  1080. }
  1081. if (cam->n_sbufs == 0) /* no luck at all - ret already set */
  1082. kfree(cam->sb_bufs);
  1083. req->count = cam->n_sbufs; /* In case of partial success */
  1084. out:
  1085. mutex_unlock(&cam->s_mutex);
  1086. return ret;
  1087. }
  1088. static int cafe_vidioc_querybuf(struct file *filp, void *priv,
  1089. struct v4l2_buffer *buf)
  1090. {
  1091. struct cafe_camera *cam = filp->private_data;
  1092. int ret = -EINVAL;
  1093. mutex_lock(&cam->s_mutex);
  1094. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1095. goto out;
  1096. if (buf->index < 0 || buf->index >= cam->n_sbufs)
  1097. goto out;
  1098. *buf = cam->sb_bufs[buf->index].v4lbuf;
  1099. ret = 0;
  1100. out:
  1101. mutex_unlock(&cam->s_mutex);
  1102. return ret;
  1103. }
  1104. static int cafe_vidioc_qbuf(struct file *filp, void *priv,
  1105. struct v4l2_buffer *buf)
  1106. {
  1107. struct cafe_camera *cam = filp->private_data;
  1108. struct cafe_sio_buffer *sbuf;
  1109. int ret = -EINVAL;
  1110. unsigned long flags;
  1111. mutex_lock(&cam->s_mutex);
  1112. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1113. goto out;
  1114. if (buf->index < 0 || buf->index >= cam->n_sbufs)
  1115. goto out;
  1116. sbuf = cam->sb_bufs + buf->index;
  1117. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
  1118. ret = 0; /* Already queued?? */
  1119. goto out;
  1120. }
  1121. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
  1122. /* Spec doesn't say anything, seems appropriate tho */
  1123. ret = -EBUSY;
  1124. goto out;
  1125. }
  1126. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  1127. spin_lock_irqsave(&cam->dev_lock, flags);
  1128. list_add(&sbuf->list, &cam->sb_avail);
  1129. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1130. ret = 0;
  1131. out:
  1132. mutex_unlock(&cam->s_mutex);
  1133. return ret;
  1134. }
  1135. static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
  1136. struct v4l2_buffer *buf)
  1137. {
  1138. struct cafe_camera *cam = filp->private_data;
  1139. struct cafe_sio_buffer *sbuf;
  1140. int ret = -EINVAL;
  1141. unsigned long flags;
  1142. mutex_lock(&cam->s_mutex);
  1143. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1144. goto out_unlock;
  1145. if (cam->state != S_STREAMING)
  1146. goto out_unlock;
  1147. if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
  1148. ret = -EAGAIN;
  1149. goto out_unlock;
  1150. }
  1151. while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
  1152. mutex_unlock(&cam->s_mutex);
  1153. if (wait_event_interruptible(cam->iowait,
  1154. !list_empty(&cam->sb_full))) {
  1155. ret = -ERESTARTSYS;
  1156. goto out;
  1157. }
  1158. mutex_lock(&cam->s_mutex);
  1159. }
  1160. if (cam->state != S_STREAMING)
  1161. ret = -EINTR;
  1162. else {
  1163. spin_lock_irqsave(&cam->dev_lock, flags);
  1164. /* Should probably recheck !list_empty() here */
  1165. sbuf = list_entry(cam->sb_full.next,
  1166. struct cafe_sio_buffer, list);
  1167. list_del_init(&sbuf->list);
  1168. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1169. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  1170. *buf = sbuf->v4lbuf;
  1171. ret = 0;
  1172. }
  1173. out_unlock:
  1174. mutex_unlock(&cam->s_mutex);
  1175. out:
  1176. return ret;
  1177. }
  1178. static void cafe_v4l_vm_open(struct vm_area_struct *vma)
  1179. {
  1180. struct cafe_sio_buffer *sbuf = vma->vm_private_data;
  1181. /*
  1182. * Locking: done under mmap_sem, so we don't need to
  1183. * go back to the camera lock here.
  1184. */
  1185. sbuf->mapcount++;
  1186. }
  1187. static void cafe_v4l_vm_close(struct vm_area_struct *vma)
  1188. {
  1189. struct cafe_sio_buffer *sbuf = vma->vm_private_data;
  1190. mutex_lock(&sbuf->cam->s_mutex);
  1191. sbuf->mapcount--;
  1192. /* Docs say we should stop I/O too... */
  1193. if (sbuf->mapcount == 0)
  1194. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  1195. mutex_unlock(&sbuf->cam->s_mutex);
  1196. }
  1197. static struct vm_operations_struct cafe_v4l_vm_ops = {
  1198. .open = cafe_v4l_vm_open,
  1199. .close = cafe_v4l_vm_close
  1200. };
  1201. static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  1202. {
  1203. struct cafe_camera *cam = filp->private_data;
  1204. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1205. int ret = -EINVAL;
  1206. int i;
  1207. struct cafe_sio_buffer *sbuf = NULL;
  1208. if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
  1209. return -EINVAL;
  1210. /*
  1211. * Find the buffer they are looking for.
  1212. */
  1213. mutex_lock(&cam->s_mutex);
  1214. for (i = 0; i < cam->n_sbufs; i++)
  1215. if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
  1216. sbuf = cam->sb_bufs + i;
  1217. break;
  1218. }
  1219. if (sbuf == NULL)
  1220. goto out;
  1221. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  1222. if (ret)
  1223. goto out;
  1224. vma->vm_flags |= VM_DONTEXPAND;
  1225. vma->vm_private_data = sbuf;
  1226. vma->vm_ops = &cafe_v4l_vm_ops;
  1227. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  1228. cafe_v4l_vm_open(vma);
  1229. ret = 0;
  1230. out:
  1231. mutex_unlock(&cam->s_mutex);
  1232. return ret;
  1233. }
  1234. static int cafe_v4l_open(struct inode *inode, struct file *filp)
  1235. {
  1236. struct cafe_camera *cam;
  1237. cam = cafe_find_dev(iminor(inode));
  1238. if (cam == NULL)
  1239. return -ENODEV;
  1240. filp->private_data = cam;
  1241. mutex_lock(&cam->s_mutex);
  1242. if (cam->users == 0) {
  1243. cafe_ctlr_power_up(cam);
  1244. __cafe_cam_reset(cam);
  1245. cafe_set_config_needed(cam, 1);
  1246. /* FIXME make sure this is complete */
  1247. }
  1248. (cam->users)++;
  1249. mutex_unlock(&cam->s_mutex);
  1250. return 0;
  1251. }
  1252. static int cafe_v4l_release(struct inode *inode, struct file *filp)
  1253. {
  1254. struct cafe_camera *cam = filp->private_data;
  1255. mutex_lock(&cam->s_mutex);
  1256. (cam->users)--;
  1257. if (filp == cam->owner) {
  1258. cafe_ctlr_stop_dma(cam);
  1259. cafe_free_sio_buffers(cam);
  1260. cam->owner = NULL;
  1261. }
  1262. if (cam->users == 0) {
  1263. cafe_ctlr_power_down(cam);
  1264. if (! alloc_bufs_at_load)
  1265. cafe_free_dma_bufs(cam);
  1266. }
  1267. mutex_unlock(&cam->s_mutex);
  1268. return 0;
  1269. }
  1270. static unsigned int cafe_v4l_poll(struct file *filp,
  1271. struct poll_table_struct *pt)
  1272. {
  1273. struct cafe_camera *cam = filp->private_data;
  1274. poll_wait(filp, &cam->iowait, pt);
  1275. if (cam->next_buf >= 0)
  1276. return POLLIN | POLLRDNORM;
  1277. return 0;
  1278. }
  1279. static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
  1280. struct v4l2_queryctrl *qc)
  1281. {
  1282. struct cafe_camera *cam = filp->private_data;
  1283. int ret;
  1284. mutex_lock(&cam->s_mutex);
  1285. ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
  1286. mutex_unlock(&cam->s_mutex);
  1287. return ret;
  1288. }
  1289. static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
  1290. struct v4l2_control *ctrl)
  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_G_CTRL, ctrl);
  1296. mutex_unlock(&cam->s_mutex);
  1297. return ret;
  1298. }
  1299. static int cafe_vidioc_s_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_S_CTRL, ctrl);
  1306. mutex_unlock(&cam->s_mutex);
  1307. return ret;
  1308. }
  1309. static int cafe_vidioc_querycap(struct file *file, void *priv,
  1310. struct v4l2_capability *cap)
  1311. {
  1312. strcpy(cap->driver, "cafe_ccic");
  1313. strcpy(cap->card, "cafe_ccic");
  1314. cap->version = CAFE_VERSION;
  1315. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1316. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1317. return 0;
  1318. }
  1319. /*
  1320. * The default format we use until somebody says otherwise.
  1321. */
  1322. static struct v4l2_pix_format cafe_def_pix_format = {
  1323. .width = VGA_WIDTH,
  1324. .height = VGA_HEIGHT,
  1325. .pixelformat = V4L2_PIX_FMT_YUYV,
  1326. .field = V4L2_FIELD_NONE,
  1327. .bytesperline = VGA_WIDTH*2,
  1328. .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
  1329. };
  1330. static int cafe_vidioc_enum_fmt_cap(struct file *filp,
  1331. void *priv, struct v4l2_fmtdesc *fmt)
  1332. {
  1333. struct cafe_camera *cam = priv;
  1334. int ret;
  1335. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1336. return -EINVAL;
  1337. mutex_lock(&cam->s_mutex);
  1338. ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
  1339. mutex_unlock(&cam->s_mutex);
  1340. return ret;
  1341. }
  1342. static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
  1343. struct v4l2_format *fmt)
  1344. {
  1345. struct cafe_camera *cam = priv;
  1346. int ret;
  1347. mutex_lock(&cam->s_mutex);
  1348. ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
  1349. mutex_unlock(&cam->s_mutex);
  1350. return ret;
  1351. }
  1352. static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
  1353. struct v4l2_format *fmt)
  1354. {
  1355. struct cafe_camera *cam = priv;
  1356. int ret;
  1357. /*
  1358. * Can't do anything if the device is not idle
  1359. * Also can't if there are streaming buffers in place.
  1360. */
  1361. if (cam->state != S_IDLE || cam->n_sbufs > 0)
  1362. return -EBUSY;
  1363. /*
  1364. * See if the formatting works in principle.
  1365. */
  1366. ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
  1367. if (ret)
  1368. return ret;
  1369. /*
  1370. * Now we start to change things for real, so let's do it
  1371. * under lock.
  1372. */
  1373. mutex_lock(&cam->s_mutex);
  1374. cam->pix_format = fmt->fmt.pix;
  1375. /*
  1376. * Make sure we have appropriate DMA buffers.
  1377. */
  1378. ret = -ENOMEM;
  1379. if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
  1380. cafe_free_dma_bufs(cam);
  1381. if (cam->nbufs == 0) {
  1382. if (cafe_alloc_dma_bufs(cam, 0))
  1383. goto out;
  1384. }
  1385. /*
  1386. * It looks like this might work, so let's program the sensor.
  1387. */
  1388. ret = cafe_cam_configure(cam);
  1389. if (! ret)
  1390. ret = cafe_ctlr_configure(cam);
  1391. out:
  1392. mutex_unlock(&cam->s_mutex);
  1393. return ret;
  1394. }
  1395. /*
  1396. * Return our stored notion of how the camera is/should be configured.
  1397. * The V4l2 spec wants us to be smarter, and actually get this from
  1398. * the camera (and not mess with it at open time). Someday.
  1399. */
  1400. static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
  1401. struct v4l2_format *f)
  1402. {
  1403. struct cafe_camera *cam = priv;
  1404. f->fmt.pix = cam->pix_format;
  1405. return 0;
  1406. }
  1407. /*
  1408. * We only have one input - the sensor - so minimize the nonsense here.
  1409. */
  1410. static int cafe_vidioc_enum_input(struct file *filp, void *priv,
  1411. struct v4l2_input *input)
  1412. {
  1413. if (input->index != 0)
  1414. return -EINVAL;
  1415. input->type = V4L2_INPUT_TYPE_CAMERA;
  1416. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  1417. strcpy(input->name, "Camera");
  1418. return 0;
  1419. }
  1420. static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  1421. {
  1422. *i = 0;
  1423. return 0;
  1424. }
  1425. static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  1426. {
  1427. if (i != 0)
  1428. return -EINVAL;
  1429. return 0;
  1430. }
  1431. /* from vivi.c */
  1432. static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  1433. {
  1434. return 0;
  1435. }
  1436. /*
  1437. * G/S_PARM. Most of this is done by the sensor, but we are
  1438. * the level which controls the number of read buffers.
  1439. */
  1440. static int cafe_vidioc_g_parm(struct file *filp, void *priv,
  1441. struct v4l2_streamparm *parms)
  1442. {
  1443. struct cafe_camera *cam = priv;
  1444. int ret;
  1445. mutex_lock(&cam->s_mutex);
  1446. ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
  1447. mutex_unlock(&cam->s_mutex);
  1448. parms->parm.capture.readbuffers = n_dma_bufs;
  1449. return ret;
  1450. }
  1451. static int cafe_vidioc_s_parm(struct file *filp, void *priv,
  1452. struct v4l2_streamparm *parms)
  1453. {
  1454. struct cafe_camera *cam = priv;
  1455. int ret;
  1456. mutex_lock(&cam->s_mutex);
  1457. ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
  1458. mutex_unlock(&cam->s_mutex);
  1459. parms->parm.capture.readbuffers = n_dma_bufs;
  1460. return ret;
  1461. }
  1462. static void cafe_v4l_dev_release(struct video_device *vd)
  1463. {
  1464. struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
  1465. kfree(cam);
  1466. }
  1467. /*
  1468. * This template device holds all of those v4l2 methods; we
  1469. * clone it for specific real devices.
  1470. */
  1471. static const struct file_operations cafe_v4l_fops = {
  1472. .owner = THIS_MODULE,
  1473. .open = cafe_v4l_open,
  1474. .release = cafe_v4l_release,
  1475. .read = cafe_v4l_read,
  1476. .poll = cafe_v4l_poll,
  1477. .mmap = cafe_v4l_mmap,
  1478. .ioctl = video_ioctl2,
  1479. .llseek = no_llseek,
  1480. };
  1481. static struct video_device cafe_v4l_template = {
  1482. .name = "cafe",
  1483. .type = VFL_TYPE_GRABBER,
  1484. .type2 = VID_TYPE_CAPTURE,
  1485. .minor = -1, /* Get one dynamically */
  1486. .tvnorms = V4L2_STD_NTSC_M,
  1487. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1488. .fops = &cafe_v4l_fops,
  1489. .release = cafe_v4l_dev_release,
  1490. .vidioc_querycap = cafe_vidioc_querycap,
  1491. .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
  1492. .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
  1493. .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
  1494. .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
  1495. .vidioc_enum_input = cafe_vidioc_enum_input,
  1496. .vidioc_g_input = cafe_vidioc_g_input,
  1497. .vidioc_s_input = cafe_vidioc_s_input,
  1498. .vidioc_s_std = cafe_vidioc_s_std,
  1499. .vidioc_reqbufs = cafe_vidioc_reqbufs,
  1500. .vidioc_querybuf = cafe_vidioc_querybuf,
  1501. .vidioc_qbuf = cafe_vidioc_qbuf,
  1502. .vidioc_dqbuf = cafe_vidioc_dqbuf,
  1503. .vidioc_streamon = cafe_vidioc_streamon,
  1504. .vidioc_streamoff = cafe_vidioc_streamoff,
  1505. .vidioc_queryctrl = cafe_vidioc_queryctrl,
  1506. .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
  1507. .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
  1508. .vidioc_g_parm = cafe_vidioc_g_parm,
  1509. .vidioc_s_parm = cafe_vidioc_s_parm,
  1510. };
  1511. /* ---------------------------------------------------------------------- */
  1512. /*
  1513. * Interrupt handler stuff
  1514. */
  1515. static void cafe_frame_tasklet(unsigned long data)
  1516. {
  1517. struct cafe_camera *cam = (struct cafe_camera *) data;
  1518. int i;
  1519. unsigned long flags;
  1520. struct cafe_sio_buffer *sbuf;
  1521. spin_lock_irqsave(&cam->dev_lock, flags);
  1522. for (i = 0; i < cam->nbufs; i++) {
  1523. int bufno = cam->next_buf;
  1524. if (bufno < 0) { /* "will never happen" */
  1525. cam_err(cam, "No valid bufs in tasklet!\n");
  1526. break;
  1527. }
  1528. if (++(cam->next_buf) >= cam->nbufs)
  1529. cam->next_buf = 0;
  1530. if (! test_bit(bufno, &cam->flags))
  1531. continue;
  1532. if (list_empty(&cam->sb_avail))
  1533. break; /* Leave it valid, hope for better later */
  1534. clear_bit(bufno, &cam->flags);
  1535. sbuf = list_entry(cam->sb_avail.next,
  1536. struct cafe_sio_buffer, list);
  1537. /*
  1538. * Drop the lock during the big copy. This *should* be safe...
  1539. */
  1540. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1541. memcpy(sbuf->buffer, cam->dma_bufs[bufno],
  1542. cam->pix_format.sizeimage);
  1543. sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
  1544. sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
  1545. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1546. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1547. spin_lock_irqsave(&cam->dev_lock, flags);
  1548. list_move_tail(&sbuf->list, &cam->sb_full);
  1549. }
  1550. if (! list_empty(&cam->sb_full))
  1551. wake_up(&cam->iowait);
  1552. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1553. }
  1554. static void cafe_frame_complete(struct cafe_camera *cam, int frame)
  1555. {
  1556. /*
  1557. * Basic frame housekeeping.
  1558. */
  1559. if (test_bit(frame, &cam->flags) && printk_ratelimit())
  1560. cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
  1561. set_bit(frame, &cam->flags);
  1562. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1563. if (cam->next_buf < 0)
  1564. cam->next_buf = frame;
  1565. cam->buf_seq[frame] = ++(cam->sequence);
  1566. switch (cam->state) {
  1567. /*
  1568. * If in single read mode, try going speculative.
  1569. */
  1570. case S_SINGLEREAD:
  1571. cam->state = S_SPECREAD;
  1572. cam->specframes = 0;
  1573. wake_up(&cam->iowait);
  1574. break;
  1575. /*
  1576. * If we are already doing speculative reads, and nobody is
  1577. * reading them, just stop.
  1578. */
  1579. case S_SPECREAD:
  1580. if (++(cam->specframes) >= cam->nbufs) {
  1581. cafe_ctlr_stop(cam);
  1582. cafe_ctlr_irq_disable(cam);
  1583. cam->state = S_IDLE;
  1584. }
  1585. wake_up(&cam->iowait);
  1586. break;
  1587. /*
  1588. * For the streaming case, we defer the real work to the
  1589. * camera tasklet.
  1590. *
  1591. * FIXME: if the application is not consuming the buffers,
  1592. * we should eventually put things on hold and restart in
  1593. * vidioc_dqbuf().
  1594. */
  1595. case S_STREAMING:
  1596. tasklet_schedule(&cam->s_tasklet);
  1597. break;
  1598. default:
  1599. cam_err(cam, "Frame interrupt in non-operational state\n");
  1600. break;
  1601. }
  1602. }
  1603. static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
  1604. {
  1605. unsigned int frame;
  1606. cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1607. /*
  1608. * Handle any frame completions. There really should
  1609. * not be more than one of these, or we have fallen
  1610. * far behind.
  1611. */
  1612. for (frame = 0; frame < cam->nbufs; frame++)
  1613. if (irqs & (IRQ_EOF0 << frame))
  1614. cafe_frame_complete(cam, frame);
  1615. /*
  1616. * If a frame starts, note that we have DMA active. This
  1617. * code assumes that we won't get multiple frame interrupts
  1618. * at once; may want to rethink that.
  1619. */
  1620. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
  1621. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1622. }
  1623. static irqreturn_t cafe_irq(int irq, void *data)
  1624. {
  1625. struct cafe_camera *cam = data;
  1626. unsigned int irqs;
  1627. spin_lock(&cam->dev_lock);
  1628. irqs = cafe_reg_read(cam, REG_IRQSTAT);
  1629. if ((irqs & ALLIRQS) == 0) {
  1630. spin_unlock(&cam->dev_lock);
  1631. return IRQ_NONE;
  1632. }
  1633. if (irqs & FRAMEIRQS)
  1634. cafe_frame_irq(cam, irqs);
  1635. if (irqs & TWSIIRQS) {
  1636. cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
  1637. wake_up(&cam->smbus_wait);
  1638. }
  1639. spin_unlock(&cam->dev_lock);
  1640. return IRQ_HANDLED;
  1641. }
  1642. /* -------------------------------------------------------------------------- */
  1643. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1644. /*
  1645. * Debugfs stuff.
  1646. */
  1647. static char cafe_debug_buf[1024];
  1648. static struct dentry *cafe_dfs_root;
  1649. static void cafe_dfs_setup(void)
  1650. {
  1651. cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
  1652. if (IS_ERR(cafe_dfs_root)) {
  1653. cafe_dfs_root = NULL; /* Never mind */
  1654. printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
  1655. }
  1656. }
  1657. static void cafe_dfs_shutdown(void)
  1658. {
  1659. if (cafe_dfs_root)
  1660. debugfs_remove(cafe_dfs_root);
  1661. }
  1662. static int cafe_dfs_open(struct inode *inode, struct file *file)
  1663. {
  1664. file->private_data = inode->i_private;
  1665. return 0;
  1666. }
  1667. static ssize_t cafe_dfs_read_regs(struct file *file,
  1668. char __user *buf, size_t count, loff_t *ppos)
  1669. {
  1670. struct cafe_camera *cam = file->private_data;
  1671. char *s = cafe_debug_buf;
  1672. int offset;
  1673. for (offset = 0; offset < 0x44; offset += 4)
  1674. s += sprintf(s, "%02x: %08x\n", offset,
  1675. cafe_reg_read(cam, offset));
  1676. for (offset = 0x88; offset <= 0x90; offset += 4)
  1677. s += sprintf(s, "%02x: %08x\n", offset,
  1678. cafe_reg_read(cam, offset));
  1679. for (offset = 0xb4; offset <= 0xbc; offset += 4)
  1680. s += sprintf(s, "%02x: %08x\n", offset,
  1681. cafe_reg_read(cam, offset));
  1682. for (offset = 0x3000; offset <= 0x300c; offset += 4)
  1683. s += sprintf(s, "%04x: %08x\n", offset,
  1684. cafe_reg_read(cam, offset));
  1685. return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
  1686. s - cafe_debug_buf);
  1687. }
  1688. static const struct file_operations cafe_dfs_reg_ops = {
  1689. .owner = THIS_MODULE,
  1690. .read = cafe_dfs_read_regs,
  1691. .open = cafe_dfs_open
  1692. };
  1693. static ssize_t cafe_dfs_read_cam(struct file *file,
  1694. char __user *buf, size_t count, loff_t *ppos)
  1695. {
  1696. struct cafe_camera *cam = file->private_data;
  1697. char *s = cafe_debug_buf;
  1698. int offset;
  1699. if (! cam->sensor)
  1700. return -EINVAL;
  1701. for (offset = 0x0; offset < 0x8a; offset++)
  1702. {
  1703. u8 v;
  1704. cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
  1705. s += sprintf(s, "%02x: %02x\n", offset, v);
  1706. }
  1707. return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
  1708. s - cafe_debug_buf);
  1709. }
  1710. static const struct file_operations cafe_dfs_cam_ops = {
  1711. .owner = THIS_MODULE,
  1712. .read = cafe_dfs_read_cam,
  1713. .open = cafe_dfs_open
  1714. };
  1715. static void cafe_dfs_cam_setup(struct cafe_camera *cam)
  1716. {
  1717. char fname[40];
  1718. if (!cafe_dfs_root)
  1719. return;
  1720. sprintf(fname, "regs-%d", cam->v4ldev.minor);
  1721. cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
  1722. cam, &cafe_dfs_reg_ops);
  1723. sprintf(fname, "cam-%d", cam->v4ldev.minor);
  1724. cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
  1725. cam, &cafe_dfs_cam_ops);
  1726. }
  1727. static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
  1728. {
  1729. if (! IS_ERR(cam->dfs_regs))
  1730. debugfs_remove(cam->dfs_regs);
  1731. if (! IS_ERR(cam->dfs_cam_regs))
  1732. debugfs_remove(cam->dfs_cam_regs);
  1733. }
  1734. #else
  1735. #define cafe_dfs_setup()
  1736. #define cafe_dfs_shutdown()
  1737. #define cafe_dfs_cam_setup(cam)
  1738. #define cafe_dfs_cam_shutdown(cam)
  1739. #endif /* CONFIG_VIDEO_ADV_DEBUG */
  1740. /* ------------------------------------------------------------------------*/
  1741. /*
  1742. * PCI interface stuff.
  1743. */
  1744. static int cafe_pci_probe(struct pci_dev *pdev,
  1745. const struct pci_device_id *id)
  1746. {
  1747. int ret;
  1748. u16 classword;
  1749. struct cafe_camera *cam;
  1750. /*
  1751. * Make sure we have a camera here - we'll get calls for
  1752. * the other cafe devices as well.
  1753. */
  1754. pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
  1755. if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
  1756. return -ENODEV;
  1757. /*
  1758. * Start putting together one of our big camera structures.
  1759. */
  1760. ret = -ENOMEM;
  1761. cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
  1762. if (cam == NULL)
  1763. goto out;
  1764. mutex_init(&cam->s_mutex);
  1765. mutex_lock(&cam->s_mutex);
  1766. spin_lock_init(&cam->dev_lock);
  1767. cam->state = S_NOTREADY;
  1768. cafe_set_config_needed(cam, 1);
  1769. init_waitqueue_head(&cam->smbus_wait);
  1770. init_waitqueue_head(&cam->iowait);
  1771. cam->pdev = pdev;
  1772. cam->pix_format = cafe_def_pix_format;
  1773. INIT_LIST_HEAD(&cam->dev_list);
  1774. INIT_LIST_HEAD(&cam->sb_avail);
  1775. INIT_LIST_HEAD(&cam->sb_full);
  1776. tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
  1777. /*
  1778. * Get set up on the PCI bus.
  1779. */
  1780. ret = pci_enable_device(pdev);
  1781. if (ret)
  1782. goto out_free;
  1783. pci_set_master(pdev);
  1784. ret = -EIO;
  1785. cam->regs = pci_iomap(pdev, 0, 0);
  1786. if (! cam->regs) {
  1787. printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
  1788. goto out_free;
  1789. }
  1790. ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
  1791. if (ret)
  1792. goto out_iounmap;
  1793. cafe_ctlr_init(cam);
  1794. cafe_ctlr_power_up(cam);
  1795. /*
  1796. * Set up I2C/SMBUS communications
  1797. */
  1798. mutex_unlock(&cam->s_mutex); /* attach can deadlock */
  1799. ret = cafe_smbus_setup(cam);
  1800. if (ret)
  1801. goto out_freeirq;
  1802. /*
  1803. * Get the v4l2 setup done.
  1804. */
  1805. mutex_lock(&cam->s_mutex);
  1806. cam->v4ldev = cafe_v4l_template;
  1807. cam->v4ldev.debug = 0;
  1808. // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
  1809. cam->v4ldev.dev = &pdev->dev;
  1810. ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
  1811. if (ret)
  1812. goto out_smbus;
  1813. /*
  1814. * If so requested, try to get our DMA buffers now.
  1815. */
  1816. if (alloc_bufs_at_load) {
  1817. if (cafe_alloc_dma_bufs(cam, 1))
  1818. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1819. " will try again later.");
  1820. }
  1821. cafe_dfs_cam_setup(cam);
  1822. mutex_unlock(&cam->s_mutex);
  1823. cafe_add_dev(cam);
  1824. return 0;
  1825. out_smbus:
  1826. cafe_smbus_shutdown(cam);
  1827. out_freeirq:
  1828. cafe_ctlr_power_down(cam);
  1829. free_irq(pdev->irq, cam);
  1830. out_iounmap:
  1831. pci_iounmap(pdev, cam->regs);
  1832. out_free:
  1833. kfree(cam);
  1834. out:
  1835. return ret;
  1836. }
  1837. /*
  1838. * Shut down an initialized device
  1839. */
  1840. static void cafe_shutdown(struct cafe_camera *cam)
  1841. {
  1842. /* FIXME: Make sure we take care of everything here */
  1843. cafe_dfs_cam_shutdown(cam);
  1844. if (cam->n_sbufs > 0)
  1845. /* What if they are still mapped? Shouldn't be, but... */
  1846. cafe_free_sio_buffers(cam);
  1847. cafe_remove_dev(cam);
  1848. cafe_ctlr_stop_dma(cam);
  1849. cafe_ctlr_power_down(cam);
  1850. cafe_smbus_shutdown(cam);
  1851. cafe_free_dma_bufs(cam);
  1852. free_irq(cam->pdev->irq, cam);
  1853. pci_iounmap(cam->pdev, cam->regs);
  1854. video_unregister_device(&cam->v4ldev);
  1855. /* kfree(cam); done in v4l_release () */
  1856. }
  1857. static void cafe_pci_remove(struct pci_dev *pdev)
  1858. {
  1859. struct cafe_camera *cam = cafe_find_by_pdev(pdev);
  1860. if (cam == NULL) {
  1861. printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
  1862. return;
  1863. }
  1864. mutex_lock(&cam->s_mutex);
  1865. if (cam->users > 0)
  1866. cam_warn(cam, "Removing a device with users!\n");
  1867. cafe_shutdown(cam);
  1868. /* No unlock - it no longer exists */
  1869. }
  1870. #ifdef CONFIG_PM
  1871. /*
  1872. * Basic power management.
  1873. */
  1874. static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1875. {
  1876. struct cafe_camera *cam = cafe_find_by_pdev(pdev);
  1877. int ret;
  1878. ret = pci_save_state(pdev);
  1879. if (ret)
  1880. return ret;
  1881. cafe_ctlr_stop_dma(cam);
  1882. cafe_ctlr_power_down(cam);
  1883. pci_disable_device(pdev);
  1884. return 0;
  1885. }
  1886. static int cafe_pci_resume(struct pci_dev *pdev)
  1887. {
  1888. struct cafe_camera *cam = cafe_find_by_pdev(pdev);
  1889. int ret = 0;
  1890. ret = pci_restore_state(pdev);
  1891. if (ret)
  1892. return ret;
  1893. ret = pci_enable_device(pdev);
  1894. if (ret) {
  1895. cam_warn(cam, "Unable to re-enable device on resume!\n");
  1896. return ret;
  1897. }
  1898. cafe_ctlr_init(cam);
  1899. cafe_ctlr_power_up(cam);
  1900. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1901. if (cam->state == S_SPECREAD)
  1902. cam->state = S_IDLE; /* Don't bother restarting */
  1903. else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
  1904. ret = cafe_read_setup(cam, cam->state);
  1905. return ret;
  1906. }
  1907. #endif /* CONFIG_PM */
  1908. static struct pci_device_id cafe_ids[] = {
  1909. { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
  1910. { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
  1911. { 0, }
  1912. };
  1913. MODULE_DEVICE_TABLE(pci, cafe_ids);
  1914. static struct pci_driver cafe_pci_driver = {
  1915. .name = "cafe1000-ccic",
  1916. .id_table = cafe_ids,
  1917. .probe = cafe_pci_probe,
  1918. .remove = cafe_pci_remove,
  1919. #ifdef CONFIG_PM
  1920. .suspend = cafe_pci_suspend,
  1921. .resume = cafe_pci_resume,
  1922. #endif
  1923. };
  1924. static int __init cafe_init(void)
  1925. {
  1926. int ret;
  1927. printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
  1928. CAFE_VERSION);
  1929. cafe_dfs_setup();
  1930. ret = pci_register_driver(&cafe_pci_driver);
  1931. if (ret) {
  1932. printk(KERN_ERR "Unable to register cafe_ccic driver\n");
  1933. goto out;
  1934. }
  1935. request_module("ov7670"); /* FIXME want something more general */
  1936. ret = 0;
  1937. out:
  1938. return ret;
  1939. }
  1940. static void __exit cafe_exit(void)
  1941. {
  1942. pci_unregister_driver(&cafe_pci_driver);
  1943. cafe_dfs_shutdown();
  1944. }
  1945. module_init(cafe_init);
  1946. module_exit(cafe_exit);