cafe_ccic.c 57 KB

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