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