cafe_ccic.c 52 KB

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