cafe_ccic.c 54 KB

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