cafe_ccic.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  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. .tvnorms = V4L2_STD_NTSC_M,
  1478. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1479. .fops = &cafe_v4l_fops,
  1480. .ioctl_ops = &cafe_v4l_ioctl_ops,
  1481. .release = video_device_release_empty,
  1482. };
  1483. /* ---------------------------------------------------------------------- */
  1484. /*
  1485. * Interrupt handler stuff
  1486. */
  1487. static void cafe_frame_tasklet(unsigned long data)
  1488. {
  1489. struct cafe_camera *cam = (struct cafe_camera *) data;
  1490. int i;
  1491. unsigned long flags;
  1492. struct cafe_sio_buffer *sbuf;
  1493. spin_lock_irqsave(&cam->dev_lock, flags);
  1494. for (i = 0; i < cam->nbufs; i++) {
  1495. int bufno = cam->next_buf;
  1496. if (bufno < 0) { /* "will never happen" */
  1497. cam_err(cam, "No valid bufs in tasklet!\n");
  1498. break;
  1499. }
  1500. if (++(cam->next_buf) >= cam->nbufs)
  1501. cam->next_buf = 0;
  1502. if (! test_bit(bufno, &cam->flags))
  1503. continue;
  1504. if (list_empty(&cam->sb_avail))
  1505. break; /* Leave it valid, hope for better later */
  1506. clear_bit(bufno, &cam->flags);
  1507. sbuf = list_entry(cam->sb_avail.next,
  1508. struct cafe_sio_buffer, list);
  1509. /*
  1510. * Drop the lock during the big copy. This *should* be safe...
  1511. */
  1512. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1513. memcpy(sbuf->buffer, cam->dma_bufs[bufno],
  1514. cam->pix_format.sizeimage);
  1515. sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
  1516. sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
  1517. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1518. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1519. spin_lock_irqsave(&cam->dev_lock, flags);
  1520. list_move_tail(&sbuf->list, &cam->sb_full);
  1521. }
  1522. if (! list_empty(&cam->sb_full))
  1523. wake_up(&cam->iowait);
  1524. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1525. }
  1526. static void cafe_frame_complete(struct cafe_camera *cam, int frame)
  1527. {
  1528. /*
  1529. * Basic frame housekeeping.
  1530. */
  1531. if (test_bit(frame, &cam->flags) && printk_ratelimit())
  1532. cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
  1533. set_bit(frame, &cam->flags);
  1534. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1535. if (cam->next_buf < 0)
  1536. cam->next_buf = frame;
  1537. cam->buf_seq[frame] = ++(cam->sequence);
  1538. switch (cam->state) {
  1539. /*
  1540. * If in single read mode, try going speculative.
  1541. */
  1542. case S_SINGLEREAD:
  1543. cam->state = S_SPECREAD;
  1544. cam->specframes = 0;
  1545. wake_up(&cam->iowait);
  1546. break;
  1547. /*
  1548. * If we are already doing speculative reads, and nobody is
  1549. * reading them, just stop.
  1550. */
  1551. case S_SPECREAD:
  1552. if (++(cam->specframes) >= cam->nbufs) {
  1553. cafe_ctlr_stop(cam);
  1554. cafe_ctlr_irq_disable(cam);
  1555. cam->state = S_IDLE;
  1556. }
  1557. wake_up(&cam->iowait);
  1558. break;
  1559. /*
  1560. * For the streaming case, we defer the real work to the
  1561. * camera tasklet.
  1562. *
  1563. * FIXME: if the application is not consuming the buffers,
  1564. * we should eventually put things on hold and restart in
  1565. * vidioc_dqbuf().
  1566. */
  1567. case S_STREAMING:
  1568. tasklet_schedule(&cam->s_tasklet);
  1569. break;
  1570. default:
  1571. cam_err(cam, "Frame interrupt in non-operational state\n");
  1572. break;
  1573. }
  1574. }
  1575. static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
  1576. {
  1577. unsigned int frame;
  1578. cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1579. /*
  1580. * Handle any frame completions. There really should
  1581. * not be more than one of these, or we have fallen
  1582. * far behind.
  1583. */
  1584. for (frame = 0; frame < cam->nbufs; frame++)
  1585. if (irqs & (IRQ_EOF0 << frame))
  1586. cafe_frame_complete(cam, frame);
  1587. /*
  1588. * If a frame starts, note that we have DMA active. This
  1589. * code assumes that we won't get multiple frame interrupts
  1590. * at once; may want to rethink that.
  1591. */
  1592. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
  1593. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1594. }
  1595. static irqreturn_t cafe_irq(int irq, void *data)
  1596. {
  1597. struct cafe_camera *cam = data;
  1598. unsigned int irqs;
  1599. spin_lock(&cam->dev_lock);
  1600. irqs = cafe_reg_read(cam, REG_IRQSTAT);
  1601. if ((irqs & ALLIRQS) == 0) {
  1602. spin_unlock(&cam->dev_lock);
  1603. return IRQ_NONE;
  1604. }
  1605. if (irqs & FRAMEIRQS)
  1606. cafe_frame_irq(cam, irqs);
  1607. if (irqs & TWSIIRQS) {
  1608. cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
  1609. wake_up(&cam->smbus_wait);
  1610. }
  1611. spin_unlock(&cam->dev_lock);
  1612. return IRQ_HANDLED;
  1613. }
  1614. /* -------------------------------------------------------------------------- */
  1615. /*
  1616. * PCI interface stuff.
  1617. */
  1618. static int cafe_pci_probe(struct pci_dev *pdev,
  1619. const struct pci_device_id *id)
  1620. {
  1621. int ret;
  1622. struct cafe_camera *cam;
  1623. /*
  1624. * Start putting together one of our big camera structures.
  1625. */
  1626. ret = -ENOMEM;
  1627. cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
  1628. if (cam == NULL)
  1629. goto out;
  1630. ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
  1631. if (ret)
  1632. goto out_free;
  1633. mutex_init(&cam->s_mutex);
  1634. mutex_lock(&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. mutex_unlock(&cam->s_mutex); /* attach can deadlock */
  1674. ret = cafe_smbus_setup(cam);
  1675. if (ret)
  1676. goto out_freeirq;
  1677. cam->sensor_addr = 0x42;
  1678. cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, &cam->i2c_adapter,
  1679. "ov7670", "ov7670", cam->sensor_addr, NULL);
  1680. if (cam->sensor == NULL) {
  1681. ret = -ENODEV;
  1682. goto out_smbus;
  1683. }
  1684. ret = cafe_cam_init(cam);
  1685. if (ret)
  1686. goto out_smbus;
  1687. /*
  1688. * Get the v4l2 setup done.
  1689. */
  1690. mutex_lock(&cam->s_mutex);
  1691. cam->vdev = cafe_v4l_template;
  1692. cam->vdev.debug = 0;
  1693. /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
  1694. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1695. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1696. if (ret)
  1697. goto out_smbus;
  1698. video_set_drvdata(&cam->vdev, cam);
  1699. /*
  1700. * If so requested, try to get our DMA buffers now.
  1701. */
  1702. if (!alloc_bufs_at_read) {
  1703. if (cafe_alloc_dma_bufs(cam, 1))
  1704. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1705. " will try again later.");
  1706. }
  1707. mutex_unlock(&cam->s_mutex);
  1708. return 0;
  1709. out_smbus:
  1710. cafe_smbus_shutdown(cam);
  1711. out_freeirq:
  1712. cafe_ctlr_power_down(cam);
  1713. free_irq(pdev->irq, cam);
  1714. out_iounmap:
  1715. pci_iounmap(pdev, cam->regs);
  1716. out_free:
  1717. v4l2_device_unregister(&cam->v4l2_dev);
  1718. out_unreg:
  1719. kfree(cam);
  1720. out:
  1721. return ret;
  1722. }
  1723. /*
  1724. * Shut down an initialized device
  1725. */
  1726. static void cafe_shutdown(struct cafe_camera *cam)
  1727. {
  1728. /* FIXME: Make sure we take care of everything here */
  1729. if (cam->n_sbufs > 0)
  1730. /* What if they are still mapped? Shouldn't be, but... */
  1731. cafe_free_sio_buffers(cam);
  1732. cafe_ctlr_stop_dma(cam);
  1733. cafe_ctlr_power_down(cam);
  1734. cafe_smbus_shutdown(cam);
  1735. cafe_free_dma_bufs(cam);
  1736. free_irq(cam->pdev->irq, cam);
  1737. pci_iounmap(cam->pdev, cam->regs);
  1738. video_unregister_device(&cam->vdev);
  1739. }
  1740. static void cafe_pci_remove(struct pci_dev *pdev)
  1741. {
  1742. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  1743. struct cafe_camera *cam = to_cam(v4l2_dev);
  1744. if (cam == NULL) {
  1745. printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
  1746. return;
  1747. }
  1748. mutex_lock(&cam->s_mutex);
  1749. if (cam->users > 0)
  1750. cam_warn(cam, "Removing a device with users!\n");
  1751. cafe_shutdown(cam);
  1752. v4l2_device_unregister(&cam->v4l2_dev);
  1753. kfree(cam);
  1754. /* No unlock - it no longer exists */
  1755. }
  1756. #ifdef CONFIG_PM
  1757. /*
  1758. * Basic power management.
  1759. */
  1760. static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1761. {
  1762. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  1763. struct cafe_camera *cam = to_cam(v4l2_dev);
  1764. int ret;
  1765. enum cafe_state cstate;
  1766. ret = pci_save_state(pdev);
  1767. if (ret)
  1768. return ret;
  1769. cstate = cam->state; /* HACK - stop_dma sets to idle */
  1770. cafe_ctlr_stop_dma(cam);
  1771. cafe_ctlr_power_down(cam);
  1772. pci_disable_device(pdev);
  1773. cam->state = cstate;
  1774. return 0;
  1775. }
  1776. static int cafe_pci_resume(struct pci_dev *pdev)
  1777. {
  1778. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  1779. struct cafe_camera *cam = to_cam(v4l2_dev);
  1780. int ret = 0;
  1781. ret = pci_restore_state(pdev);
  1782. if (ret)
  1783. return ret;
  1784. ret = pci_enable_device(pdev);
  1785. if (ret) {
  1786. cam_warn(cam, "Unable to re-enable device on resume!\n");
  1787. return ret;
  1788. }
  1789. cafe_ctlr_init(cam);
  1790. cafe_ctlr_power_down(cam);
  1791. mutex_lock(&cam->s_mutex);
  1792. if (cam->users > 0) {
  1793. cafe_ctlr_power_up(cam);
  1794. __cafe_cam_reset(cam);
  1795. }
  1796. mutex_unlock(&cam->s_mutex);
  1797. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1798. if (cam->state == S_SPECREAD)
  1799. cam->state = S_IDLE; /* Don't bother restarting */
  1800. else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
  1801. ret = cafe_read_setup(cam, cam->state);
  1802. return ret;
  1803. }
  1804. #endif /* CONFIG_PM */
  1805. static struct pci_device_id cafe_ids[] = {
  1806. { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
  1807. PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
  1808. { 0, }
  1809. };
  1810. MODULE_DEVICE_TABLE(pci, cafe_ids);
  1811. static struct pci_driver cafe_pci_driver = {
  1812. .name = "cafe1000-ccic",
  1813. .id_table = cafe_ids,
  1814. .probe = cafe_pci_probe,
  1815. .remove = cafe_pci_remove,
  1816. #ifdef CONFIG_PM
  1817. .suspend = cafe_pci_suspend,
  1818. .resume = cafe_pci_resume,
  1819. #endif
  1820. };
  1821. static int __init cafe_init(void)
  1822. {
  1823. int ret;
  1824. printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
  1825. CAFE_VERSION);
  1826. ret = pci_register_driver(&cafe_pci_driver);
  1827. if (ret) {
  1828. printk(KERN_ERR "Unable to register cafe_ccic driver\n");
  1829. goto out;
  1830. }
  1831. ret = 0;
  1832. out:
  1833. return ret;
  1834. }
  1835. static void __exit cafe_exit(void)
  1836. {
  1837. pci_unregister_driver(&cafe_pci_driver);
  1838. }
  1839. module_init(cafe_init);
  1840. module_exit(cafe_exit);