cafe_ccic.c 54 KB

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