cafe_ccic.c 56 KB

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