cafe_ccic.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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 void cafe_ctlr_stop_dma(struct cafe_camera *cam);
  429. static void cafe_ctlr_power_down(struct cafe_camera *cam);
  430. static int cafe_smbus_attach(struct i2c_client *client)
  431. {
  432. struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
  433. /*
  434. * Don't talk to chips we don't recognize.
  435. */
  436. if (client->driver->id == I2C_DRIVERID_OV7670) {
  437. cam->sensor = client;
  438. return cafe_cam_init(cam);
  439. }
  440. return -EINVAL;
  441. }
  442. static int cafe_smbus_detach(struct i2c_client *client)
  443. {
  444. struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
  445. if (cam->sensor == client) {
  446. cafe_ctlr_stop_dma(cam);
  447. cafe_ctlr_power_down(cam);
  448. cam_err(cam, "lost the sensor!\n");
  449. cam->sensor = NULL; /* Bummer, no camera */
  450. cam->state = S_NOTREADY;
  451. }
  452. return 0;
  453. }
  454. static int cafe_smbus_setup(struct cafe_camera *cam)
  455. {
  456. struct i2c_adapter *adap = &cam->i2c_adapter;
  457. int ret;
  458. cafe_smbus_enable_irq(cam);
  459. adap->id = I2C_HW_SMBUS_CAFE;
  460. adap->class = I2C_CLASS_CAM_DIGITAL;
  461. adap->owner = THIS_MODULE;
  462. adap->client_register = cafe_smbus_attach;
  463. adap->client_unregister = cafe_smbus_detach;
  464. adap->algo = &cafe_smbus_algo;
  465. strcpy(adap->name, "cafe_ccic");
  466. i2c_set_adapdata(adap, cam);
  467. ret = i2c_add_adapter(adap);
  468. if (ret)
  469. printk(KERN_ERR "Unable to register cafe i2c adapter\n");
  470. return ret;
  471. }
  472. static void cafe_smbus_shutdown(struct cafe_camera *cam)
  473. {
  474. i2c_del_adapter(&cam->i2c_adapter);
  475. }
  476. /* ------------------------------------------------------------------- */
  477. /*
  478. * Deal with the controller.
  479. */
  480. /*
  481. * Do everything we think we need to have the interface operating
  482. * according to the desired format.
  483. */
  484. static void cafe_ctlr_dma(struct cafe_camera *cam)
  485. {
  486. /*
  487. * Store the first two Y buffers (we aren't supporting
  488. * planar formats for now, so no UV bufs). Then either
  489. * set the third if it exists, or tell the controller
  490. * to just use two.
  491. */
  492. cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
  493. cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
  494. if (cam->nbufs > 2) {
  495. cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
  496. cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
  497. }
  498. else
  499. cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
  500. cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
  501. }
  502. static void cafe_ctlr_image(struct cafe_camera *cam)
  503. {
  504. int imgsz;
  505. struct v4l2_pix_format *fmt = &cam->pix_format;
  506. imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
  507. (fmt->bytesperline & IMGSZ_H_MASK);
  508. cafe_reg_write(cam, REG_IMGSIZE, imgsz);
  509. cafe_reg_write(cam, REG_IMGOFFSET, 0);
  510. /* YPITCH just drops the last two bits */
  511. cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
  512. IMGP_YP_MASK);
  513. /*
  514. * Tell the controller about the image format we are using.
  515. */
  516. switch (cam->pix_format.pixelformat) {
  517. case V4L2_PIX_FMT_YUYV:
  518. cafe_reg_write_mask(cam, REG_CTRL0,
  519. C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
  520. C0_DF_MASK);
  521. break;
  522. /*
  523. * For "fake rgb32" get the image pitch right.
  524. */
  525. case V4L2_PIX_FMT_RGB32:
  526. cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline/2,
  527. IMGP_YP_MASK);
  528. imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
  529. ((fmt->bytesperline/2) & IMGSZ_H_MASK);
  530. cafe_reg_write(cam, REG_IMGSIZE, imgsz);
  531. /* fall into ... */
  532. case V4L2_PIX_FMT_RGB444:
  533. cafe_reg_write_mask(cam, REG_CTRL0,
  534. C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
  535. C0_DF_MASK);
  536. /* Alpha value? */
  537. break;
  538. case V4L2_PIX_FMT_RGB565:
  539. cafe_reg_write_mask(cam, REG_CTRL0,
  540. C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
  541. C0_DF_MASK);
  542. break;
  543. default:
  544. cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
  545. break;
  546. }
  547. /*
  548. * Make sure it knows we want to use hsync/vsync.
  549. */
  550. cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
  551. C0_SIFM_MASK);
  552. }
  553. /*
  554. * Configure the controller for operation; caller holds the
  555. * device mutex.
  556. */
  557. static int cafe_ctlr_configure(struct cafe_camera *cam)
  558. {
  559. unsigned long flags;
  560. spin_lock_irqsave(&cam->dev_lock, flags);
  561. cafe_ctlr_dma(cam);
  562. cafe_ctlr_image(cam);
  563. cafe_set_config_needed(cam, 0);
  564. spin_unlock_irqrestore(&cam->dev_lock, flags);
  565. return 0;
  566. }
  567. static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
  568. {
  569. /*
  570. * Clear any pending interrupts, since we do not
  571. * expect to have I/O active prior to enabling.
  572. */
  573. cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
  574. cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
  575. }
  576. static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
  577. {
  578. cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
  579. }
  580. /*
  581. * Make the controller start grabbing images. Everything must
  582. * be set up before doing this.
  583. */
  584. static void cafe_ctlr_start(struct cafe_camera *cam)
  585. {
  586. /* set_bit performs a read, so no other barrier should be
  587. needed here */
  588. cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
  589. }
  590. static void cafe_ctlr_stop(struct cafe_camera *cam)
  591. {
  592. cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  593. }
  594. static void cafe_ctlr_init(struct cafe_camera *cam)
  595. {
  596. unsigned long flags;
  597. spin_lock_irqsave(&cam->dev_lock, flags);
  598. /*
  599. * Added magic to bring up the hardware on the B-Test board
  600. */
  601. cafe_reg_write(cam, 0x3038, 0x8);
  602. cafe_reg_write(cam, 0x315c, 0x80008);
  603. /*
  604. * Go through the dance needed to wake the device up.
  605. * Note that these registers are global and shared
  606. * with the NAND and SD devices. Interaction between the
  607. * three still needs to be examined.
  608. */
  609. cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
  610. cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
  611. cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
  612. mdelay(5); /* FIXME revisit this */
  613. cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
  614. cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
  615. /*
  616. * Make sure it's not powered down.
  617. */
  618. cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  619. /*
  620. * Turn off the enable bit. It sure should be off anyway,
  621. * but it's good to be sure.
  622. */
  623. cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  624. /*
  625. * Mask all interrupts.
  626. */
  627. cafe_reg_write(cam, REG_IRQMASK, 0);
  628. /*
  629. * Clock the sensor appropriately. Controller clock should
  630. * be 48MHz, sensor "typical" value is half that.
  631. */
  632. cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
  633. spin_unlock_irqrestore(&cam->dev_lock, flags);
  634. }
  635. /*
  636. * Stop the controller, and don't return until we're really sure that no
  637. * further DMA is going on.
  638. */
  639. static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
  640. {
  641. unsigned long flags;
  642. /*
  643. * Theory: stop the camera controller (whether it is operating
  644. * or not). Delay briefly just in case we race with the SOF
  645. * interrupt, then wait until no DMA is active.
  646. */
  647. spin_lock_irqsave(&cam->dev_lock, flags);
  648. cafe_ctlr_stop(cam);
  649. spin_unlock_irqrestore(&cam->dev_lock, flags);
  650. mdelay(1);
  651. wait_event_timeout(cam->iowait,
  652. !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
  653. if (test_bit(CF_DMA_ACTIVE, &cam->flags))
  654. cam_err(cam, "Timeout waiting for DMA to end\n");
  655. /* This would be bad news - what now? */
  656. spin_lock_irqsave(&cam->dev_lock, flags);
  657. cam->state = S_IDLE;
  658. cafe_ctlr_irq_disable(cam);
  659. spin_unlock_irqrestore(&cam->dev_lock, flags);
  660. }
  661. /*
  662. * Power up and down.
  663. */
  664. static void cafe_ctlr_power_up(struct cafe_camera *cam)
  665. {
  666. unsigned long flags;
  667. spin_lock_irqsave(&cam->dev_lock, flags);
  668. cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  669. /*
  670. * Put the sensor into operational mode (assumes OLPC-style
  671. * wiring). Control 0 is reset - set to 1 to operate.
  672. * Control 1 is power down, set to 0 to operate.
  673. */
  674. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
  675. mdelay(1); /* Marvell says 1ms will do it */
  676. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
  677. mdelay(1); /* Enough? */
  678. spin_unlock_irqrestore(&cam->dev_lock, flags);
  679. }
  680. static void cafe_ctlr_power_down(struct cafe_camera *cam)
  681. {
  682. unsigned long flags;
  683. spin_lock_irqsave(&cam->dev_lock, flags);
  684. cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
  685. cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
  686. spin_unlock_irqrestore(&cam->dev_lock, flags);
  687. }
  688. /* -------------------------------------------------------------------- */
  689. /*
  690. * Communications with the sensor.
  691. */
  692. static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
  693. {
  694. struct i2c_client *sc = cam->sensor;
  695. int ret;
  696. if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
  697. return -EINVAL;
  698. ret = sc->driver->command(sc, cmd, arg);
  699. if (ret == -EPERM) /* Unsupported command */
  700. return 0;
  701. return ret;
  702. }
  703. static int __cafe_cam_reset(struct cafe_camera *cam)
  704. {
  705. int zero = 0;
  706. return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
  707. }
  708. /*
  709. * We have found the sensor on the i2c. Let's try to have a
  710. * conversation.
  711. */
  712. static int cafe_cam_init(struct cafe_camera *cam)
  713. {
  714. int ret;
  715. mutex_lock(&cam->s_mutex);
  716. if (cam->state != S_NOTREADY)
  717. cam_warn(cam, "Cam init with device in funky state %d",
  718. cam->state);
  719. ret = __cafe_cam_reset(cam);
  720. if (ret)
  721. goto out;
  722. ret = __cafe_cam_cmd(cam, VIDIOC_INT_G_CHIP_IDENT, &cam->sensor_type);
  723. if (ret)
  724. goto out;
  725. // if (cam->sensor->addr != OV7xx0_SID) {
  726. if (cam->sensor_type != V4L2_IDENT_OV7670) {
  727. cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
  728. ret = -EINVAL;
  729. goto out;
  730. }
  731. /* Get/set parameters? */
  732. ret = 0;
  733. cam->state = S_IDLE;
  734. out:
  735. mutex_unlock(&cam->s_mutex);
  736. return ret;
  737. }
  738. /*
  739. * Configure the sensor to match the parameters we have. Caller should
  740. * hold s_mutex
  741. */
  742. static int cafe_cam_set_flip(struct cafe_camera *cam)
  743. {
  744. struct v4l2_control ctrl;
  745. memset(&ctrl, 0, sizeof(ctrl));
  746. ctrl.id = V4L2_CID_VFLIP;
  747. ctrl.value = flip;
  748. return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
  749. }
  750. static int cafe_cam_configure(struct cafe_camera *cam)
  751. {
  752. struct v4l2_format fmt;
  753. int ret, zero = 0;
  754. if (cam->state != S_IDLE)
  755. return -EINVAL;
  756. fmt.fmt.pix = cam->pix_format;
  757. ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
  758. if (ret == 0)
  759. ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
  760. /*
  761. * OV7670 does weird things if flip is set *before* format...
  762. */
  763. ret += cafe_cam_set_flip(cam);
  764. return ret;
  765. }
  766. /* -------------------------------------------------------------------- */
  767. /*
  768. * DMA buffer management. These functions need s_mutex held.
  769. */
  770. /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
  771. * does a get_free_pages() call, and we waste a good chunk of an orderN
  772. * allocation. Should try to allocate the whole set in one chunk.
  773. */
  774. static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
  775. {
  776. int i;
  777. cafe_set_config_needed(cam, 1);
  778. if (loadtime)
  779. cam->dma_buf_size = dma_buf_size;
  780. else {
  781. cam->dma_buf_size = cam->pix_format.sizeimage;
  782. if (cam->pix_format.pixelformat == V4L2_PIX_FMT_RGB32)
  783. cam->dma_buf_size /= 2;
  784. }
  785. if (n_dma_bufs > 3)
  786. n_dma_bufs = 3;
  787. cam->nbufs = 0;
  788. for (i = 0; i < n_dma_bufs; i++) {
  789. cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
  790. cam->dma_buf_size, cam->dma_handles + i,
  791. GFP_KERNEL);
  792. if (cam->dma_bufs[i] == NULL) {
  793. cam_warn(cam, "Failed to allocate DMA buffer\n");
  794. break;
  795. }
  796. /* For debug, remove eventually */
  797. memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
  798. (cam->nbufs)++;
  799. }
  800. switch (cam->nbufs) {
  801. case 1:
  802. dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
  803. cam->dma_bufs[0], cam->dma_handles[0]);
  804. cam->nbufs = 0;
  805. case 0:
  806. cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
  807. return -ENOMEM;
  808. case 2:
  809. if (n_dma_bufs > 2)
  810. cam_warn(cam, "Will limp along with only 2 buffers\n");
  811. break;
  812. }
  813. return 0;
  814. }
  815. static void cafe_free_dma_bufs(struct cafe_camera *cam)
  816. {
  817. int i;
  818. for (i = 0; i < cam->nbufs; i++) {
  819. dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
  820. cam->dma_bufs[i], cam->dma_handles[i]);
  821. cam->dma_bufs[i] = NULL;
  822. }
  823. cam->nbufs = 0;
  824. }
  825. /* ----------------------------------------------------------------------- */
  826. /*
  827. * Here starts the V4L2 interface code.
  828. */
  829. /*
  830. * Read an image from the device.
  831. */
  832. static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
  833. char __user *buffer, size_t len, loff_t *pos)
  834. {
  835. int bufno;
  836. unsigned long flags;
  837. spin_lock_irqsave(&cam->dev_lock, flags);
  838. if (cam->next_buf < 0) {
  839. cam_err(cam, "deliver_buffer: No next buffer\n");
  840. spin_unlock_irqrestore(&cam->dev_lock, flags);
  841. return -EIO;
  842. }
  843. bufno = cam->next_buf;
  844. clear_bit(bufno, &cam->flags);
  845. if (++(cam->next_buf) >= cam->nbufs)
  846. cam->next_buf = 0;
  847. if (! test_bit(cam->next_buf, &cam->flags))
  848. cam->next_buf = -1;
  849. cam->specframes = 0;
  850. spin_unlock_irqrestore(&cam->dev_lock, flags);
  851. if (len > cam->pix_format.sizeimage)
  852. len = cam->pix_format.sizeimage;
  853. if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
  854. return -EFAULT;
  855. (*pos) += len;
  856. return len;
  857. }
  858. /*
  859. * Get everything ready, and start grabbing frames.
  860. */
  861. static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
  862. {
  863. int ret;
  864. unsigned long flags;
  865. /*
  866. * Configuration. If we still don't have DMA buffers,
  867. * make one last, desperate attempt.
  868. */
  869. if (cam->nbufs == 0)
  870. if (cafe_alloc_dma_bufs(cam, 0))
  871. return -ENOMEM;
  872. if (cafe_needs_config(cam)) {
  873. cafe_cam_configure(cam);
  874. ret = cafe_ctlr_configure(cam);
  875. if (ret)
  876. return ret;
  877. }
  878. /*
  879. * Turn it loose.
  880. */
  881. spin_lock_irqsave(&cam->dev_lock, flags);
  882. cafe_reset_buffers(cam);
  883. cafe_ctlr_irq_enable(cam);
  884. cam->state = state;
  885. cafe_ctlr_start(cam);
  886. spin_unlock_irqrestore(&cam->dev_lock, flags);
  887. return 0;
  888. }
  889. static ssize_t cafe_v4l_read(struct file *filp,
  890. char __user *buffer, size_t len, loff_t *pos)
  891. {
  892. struct cafe_camera *cam = filp->private_data;
  893. int ret;
  894. /*
  895. * Perhaps we're in speculative read mode and already
  896. * have data?
  897. */
  898. mutex_lock(&cam->s_mutex);
  899. if (cam->state == S_SPECREAD) {
  900. if (cam->next_buf >= 0) {
  901. ret = cafe_deliver_buffer(cam, buffer, len, pos);
  902. if (ret != 0)
  903. goto out_unlock;
  904. }
  905. } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
  906. ret = -EIO;
  907. goto out_unlock;
  908. } else if (cam->state != S_IDLE) {
  909. ret = -EBUSY;
  910. goto out_unlock;
  911. }
  912. /*
  913. * v4l2: multiple processes can open the device, but only
  914. * one gets to grab data from it.
  915. */
  916. if (cam->owner && cam->owner != filp) {
  917. ret = -EBUSY;
  918. goto out_unlock;
  919. }
  920. cam->owner = filp;
  921. /*
  922. * Do setup if need be.
  923. */
  924. if (cam->state != S_SPECREAD) {
  925. ret = cafe_read_setup(cam, S_SINGLEREAD);
  926. if (ret)
  927. goto out_unlock;
  928. }
  929. /*
  930. * Wait for something to happen. This should probably
  931. * be interruptible (FIXME).
  932. */
  933. wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
  934. if (cam->next_buf < 0) {
  935. cam_err(cam, "read() operation timed out\n");
  936. cafe_ctlr_stop_dma(cam);
  937. ret = -EIO;
  938. goto out_unlock;
  939. }
  940. /*
  941. * Give them their data and we should be done.
  942. */
  943. ret = cafe_deliver_buffer(cam, buffer, len, pos);
  944. out_unlock:
  945. mutex_unlock(&cam->s_mutex);
  946. return ret;
  947. }
  948. /*
  949. * Streaming I/O support.
  950. */
  951. static int cafe_vidioc_streamon(struct file *filp, void *priv,
  952. enum v4l2_buf_type type)
  953. {
  954. struct cafe_camera *cam = filp->private_data;
  955. int ret = -EINVAL;
  956. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  957. goto out;
  958. mutex_lock(&cam->s_mutex);
  959. if (cam->state != S_IDLE || cam->n_sbufs == 0)
  960. goto out_unlock;
  961. cam->sequence = 0;
  962. ret = cafe_read_setup(cam, S_STREAMING);
  963. out_unlock:
  964. mutex_unlock(&cam->s_mutex);
  965. out:
  966. return ret;
  967. }
  968. static int cafe_vidioc_streamoff(struct file *filp, void *priv,
  969. enum v4l2_buf_type type)
  970. {
  971. struct cafe_camera *cam = filp->private_data;
  972. int ret = -EINVAL;
  973. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  974. goto out;
  975. mutex_lock(&cam->s_mutex);
  976. if (cam->state != S_STREAMING)
  977. goto out_unlock;
  978. cafe_ctlr_stop_dma(cam);
  979. ret = 0;
  980. out_unlock:
  981. mutex_unlock(&cam->s_mutex);
  982. out:
  983. return ret;
  984. }
  985. static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
  986. {
  987. struct cafe_sio_buffer *buf = cam->sb_bufs + index;
  988. INIT_LIST_HEAD(&buf->list);
  989. buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
  990. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  991. if (buf->buffer == NULL)
  992. return -ENOMEM;
  993. buf->mapcount = 0;
  994. buf->cam = cam;
  995. buf->v4lbuf.index = index;
  996. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  997. buf->v4lbuf.field = V4L2_FIELD_NONE;
  998. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  999. /*
  1000. * Offset: must be 32-bit even on a 64-bit system. video-buf
  1001. * just uses the length times the index, but the spec warns
  1002. * against doing just that - vma merging problems. So we
  1003. * leave a gap between each pair of buffers.
  1004. */
  1005. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  1006. return 0;
  1007. }
  1008. static int cafe_free_sio_buffers(struct cafe_camera *cam)
  1009. {
  1010. int i;
  1011. /*
  1012. * If any buffers are mapped, we cannot free them at all.
  1013. */
  1014. for (i = 0; i < cam->n_sbufs; i++)
  1015. if (cam->sb_bufs[i].mapcount > 0)
  1016. return -EBUSY;
  1017. /*
  1018. * OK, let's do it.
  1019. */
  1020. for (i = 0; i < cam->n_sbufs; i++)
  1021. vfree(cam->sb_bufs[i].buffer);
  1022. cam->n_sbufs = 0;
  1023. kfree(cam->sb_bufs);
  1024. cam->sb_bufs = NULL;
  1025. INIT_LIST_HEAD(&cam->sb_avail);
  1026. INIT_LIST_HEAD(&cam->sb_full);
  1027. return 0;
  1028. }
  1029. static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
  1030. struct v4l2_requestbuffers *req)
  1031. {
  1032. struct cafe_camera *cam = filp->private_data;
  1033. int ret;
  1034. /*
  1035. * Make sure it's something we can do. User pointers could be
  1036. * implemented without great pain, but that's not been done yet.
  1037. */
  1038. if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1039. return -EINVAL;
  1040. if (req->memory != V4L2_MEMORY_MMAP)
  1041. return -EINVAL;
  1042. /*
  1043. * If they ask for zero buffers, they really want us to stop streaming
  1044. * (if it's happening) and free everything. Should we check owner?
  1045. */
  1046. mutex_lock(&cam->s_mutex);
  1047. if (req->count == 0) {
  1048. if (cam->state == S_STREAMING)
  1049. cafe_ctlr_stop_dma(cam);
  1050. ret = cafe_free_sio_buffers (cam);
  1051. goto out;
  1052. }
  1053. /*
  1054. * Device needs to be idle and working. We *could* try to do the
  1055. * right thing in S_SPECREAD by shutting things down, but it
  1056. * probably doesn't matter.
  1057. */
  1058. if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
  1059. ret = -EBUSY;
  1060. goto out;
  1061. }
  1062. cam->owner = filp;
  1063. if (req->count < min_buffers)
  1064. req->count = min_buffers;
  1065. else if (req->count > max_buffers)
  1066. req->count = max_buffers;
  1067. if (cam->n_sbufs > 0) {
  1068. ret = cafe_free_sio_buffers(cam);
  1069. if (ret)
  1070. goto out;
  1071. }
  1072. cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
  1073. GFP_KERNEL);
  1074. if (cam->sb_bufs == NULL) {
  1075. ret = -ENOMEM;
  1076. goto out;
  1077. }
  1078. for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
  1079. ret = cafe_setup_siobuf(cam, cam->n_sbufs);
  1080. if (ret)
  1081. break;
  1082. }
  1083. if (cam->n_sbufs == 0) /* no luck at all - ret already set */
  1084. kfree(cam->sb_bufs);
  1085. else
  1086. ret = 0;
  1087. req->count = cam->n_sbufs; /* In case of partial success */
  1088. out:
  1089. mutex_unlock(&cam->s_mutex);
  1090. return ret;
  1091. }
  1092. static int cafe_vidioc_querybuf(struct file *filp, void *priv,
  1093. struct v4l2_buffer *buf)
  1094. {
  1095. struct cafe_camera *cam = filp->private_data;
  1096. int ret = -EINVAL;
  1097. mutex_lock(&cam->s_mutex);
  1098. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1099. goto out;
  1100. if (buf->index < 0 || buf->index >= cam->n_sbufs)
  1101. goto out;
  1102. *buf = cam->sb_bufs[buf->index].v4lbuf;
  1103. ret = 0;
  1104. out:
  1105. mutex_unlock(&cam->s_mutex);
  1106. return ret;
  1107. }
  1108. static int cafe_vidioc_qbuf(struct file *filp, void *priv,
  1109. struct v4l2_buffer *buf)
  1110. {
  1111. struct cafe_camera *cam = filp->private_data;
  1112. struct cafe_sio_buffer *sbuf;
  1113. int ret = -EINVAL;
  1114. unsigned long flags;
  1115. mutex_lock(&cam->s_mutex);
  1116. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1117. goto out;
  1118. if (buf->index < 0 || buf->index >= cam->n_sbufs)
  1119. goto out;
  1120. sbuf = cam->sb_bufs + buf->index;
  1121. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
  1122. ret = 0; /* Already queued?? */
  1123. goto out;
  1124. }
  1125. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
  1126. /* Spec doesn't say anything, seems appropriate tho */
  1127. ret = -EBUSY;
  1128. goto out;
  1129. }
  1130. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  1131. spin_lock_irqsave(&cam->dev_lock, flags);
  1132. list_add(&sbuf->list, &cam->sb_avail);
  1133. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1134. ret = 0;
  1135. out:
  1136. mutex_unlock(&cam->s_mutex);
  1137. return ret;
  1138. }
  1139. static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
  1140. struct v4l2_buffer *buf)
  1141. {
  1142. struct cafe_camera *cam = filp->private_data;
  1143. struct cafe_sio_buffer *sbuf;
  1144. int ret = -EINVAL;
  1145. unsigned long flags;
  1146. mutex_lock(&cam->s_mutex);
  1147. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1148. goto out_unlock;
  1149. if (cam->state != S_STREAMING)
  1150. goto out_unlock;
  1151. if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
  1152. ret = -EAGAIN;
  1153. goto out_unlock;
  1154. }
  1155. while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
  1156. mutex_unlock(&cam->s_mutex);
  1157. if (wait_event_interruptible(cam->iowait,
  1158. !list_empty(&cam->sb_full))) {
  1159. ret = -ERESTARTSYS;
  1160. goto out;
  1161. }
  1162. mutex_lock(&cam->s_mutex);
  1163. }
  1164. if (cam->state != S_STREAMING)
  1165. ret = -EINTR;
  1166. else {
  1167. spin_lock_irqsave(&cam->dev_lock, flags);
  1168. /* Should probably recheck !list_empty() here */
  1169. sbuf = list_entry(cam->sb_full.next,
  1170. struct cafe_sio_buffer, list);
  1171. list_del_init(&sbuf->list);
  1172. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1173. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  1174. *buf = sbuf->v4lbuf;
  1175. ret = 0;
  1176. }
  1177. out_unlock:
  1178. mutex_unlock(&cam->s_mutex);
  1179. out:
  1180. return ret;
  1181. }
  1182. static void cafe_v4l_vm_open(struct vm_area_struct *vma)
  1183. {
  1184. struct cafe_sio_buffer *sbuf = vma->vm_private_data;
  1185. /*
  1186. * Locking: done under mmap_sem, so we don't need to
  1187. * go back to the camera lock here.
  1188. */
  1189. sbuf->mapcount++;
  1190. }
  1191. static void cafe_v4l_vm_close(struct vm_area_struct *vma)
  1192. {
  1193. struct cafe_sio_buffer *sbuf = vma->vm_private_data;
  1194. mutex_lock(&sbuf->cam->s_mutex);
  1195. sbuf->mapcount--;
  1196. /* Docs say we should stop I/O too... */
  1197. if (sbuf->mapcount == 0)
  1198. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  1199. mutex_unlock(&sbuf->cam->s_mutex);
  1200. }
  1201. static struct vm_operations_struct cafe_v4l_vm_ops = {
  1202. .open = cafe_v4l_vm_open,
  1203. .close = cafe_v4l_vm_close
  1204. };
  1205. static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  1206. {
  1207. struct cafe_camera *cam = filp->private_data;
  1208. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1209. int ret = -EINVAL;
  1210. int i;
  1211. struct cafe_sio_buffer *sbuf = NULL;
  1212. if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
  1213. return -EINVAL;
  1214. /*
  1215. * Find the buffer they are looking for.
  1216. */
  1217. mutex_lock(&cam->s_mutex);
  1218. for (i = 0; i < cam->n_sbufs; i++)
  1219. if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
  1220. sbuf = cam->sb_bufs + i;
  1221. break;
  1222. }
  1223. if (sbuf == NULL)
  1224. goto out;
  1225. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  1226. if (ret)
  1227. goto out;
  1228. vma->vm_flags |= VM_DONTEXPAND;
  1229. vma->vm_private_data = sbuf;
  1230. vma->vm_ops = &cafe_v4l_vm_ops;
  1231. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  1232. cafe_v4l_vm_open(vma);
  1233. ret = 0;
  1234. out:
  1235. mutex_unlock(&cam->s_mutex);
  1236. return ret;
  1237. }
  1238. static int cafe_v4l_open(struct inode *inode, struct file *filp)
  1239. {
  1240. struct cafe_camera *cam;
  1241. cam = cafe_find_dev(iminor(inode));
  1242. if (cam == NULL)
  1243. return -ENODEV;
  1244. filp->private_data = cam;
  1245. mutex_lock(&cam->s_mutex);
  1246. if (cam->users == 0) {
  1247. cafe_ctlr_power_up(cam);
  1248. __cafe_cam_reset(cam);
  1249. cafe_set_config_needed(cam, 1);
  1250. /* FIXME make sure this is complete */
  1251. }
  1252. (cam->users)++;
  1253. mutex_unlock(&cam->s_mutex);
  1254. return 0;
  1255. }
  1256. static int cafe_v4l_release(struct inode *inode, struct file *filp)
  1257. {
  1258. struct cafe_camera *cam = filp->private_data;
  1259. mutex_lock(&cam->s_mutex);
  1260. (cam->users)--;
  1261. if (filp == cam->owner) {
  1262. cafe_ctlr_stop_dma(cam);
  1263. cafe_free_sio_buffers(cam);
  1264. cam->owner = NULL;
  1265. }
  1266. if (cam->users == 0) {
  1267. cafe_ctlr_power_down(cam);
  1268. if (! alloc_bufs_at_load)
  1269. cafe_free_dma_bufs(cam);
  1270. }
  1271. mutex_unlock(&cam->s_mutex);
  1272. return 0;
  1273. }
  1274. static unsigned int cafe_v4l_poll(struct file *filp,
  1275. struct poll_table_struct *pt)
  1276. {
  1277. struct cafe_camera *cam = filp->private_data;
  1278. poll_wait(filp, &cam->iowait, pt);
  1279. if (cam->next_buf >= 0)
  1280. return POLLIN | POLLRDNORM;
  1281. return 0;
  1282. }
  1283. static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
  1284. struct v4l2_queryctrl *qc)
  1285. {
  1286. struct cafe_camera *cam = filp->private_data;
  1287. int ret;
  1288. mutex_lock(&cam->s_mutex);
  1289. ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
  1290. mutex_unlock(&cam->s_mutex);
  1291. return ret;
  1292. }
  1293. static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
  1294. struct v4l2_control *ctrl)
  1295. {
  1296. struct cafe_camera *cam = filp->private_data;
  1297. int ret;
  1298. mutex_lock(&cam->s_mutex);
  1299. ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
  1300. mutex_unlock(&cam->s_mutex);
  1301. return ret;
  1302. }
  1303. static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
  1304. struct v4l2_control *ctrl)
  1305. {
  1306. struct cafe_camera *cam = filp->private_data;
  1307. int ret;
  1308. mutex_lock(&cam->s_mutex);
  1309. ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
  1310. mutex_unlock(&cam->s_mutex);
  1311. return ret;
  1312. }
  1313. static int cafe_vidioc_querycap(struct file *file, void *priv,
  1314. struct v4l2_capability *cap)
  1315. {
  1316. strcpy(cap->driver, "cafe_ccic");
  1317. strcpy(cap->card, "cafe_ccic");
  1318. cap->version = CAFE_VERSION;
  1319. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1320. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1321. return 0;
  1322. }
  1323. /*
  1324. * The default format we use until somebody says otherwise.
  1325. */
  1326. static struct v4l2_pix_format cafe_def_pix_format = {
  1327. .width = VGA_WIDTH,
  1328. .height = VGA_HEIGHT,
  1329. .pixelformat = V4L2_PIX_FMT_YUYV,
  1330. .field = V4L2_FIELD_NONE,
  1331. .bytesperline = VGA_WIDTH*2,
  1332. .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
  1333. };
  1334. static int cafe_vidioc_enum_fmt_cap(struct file *filp,
  1335. void *priv, struct v4l2_fmtdesc *fmt)
  1336. {
  1337. struct cafe_camera *cam = priv;
  1338. int ret;
  1339. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1340. return -EINVAL;
  1341. mutex_lock(&cam->s_mutex);
  1342. ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
  1343. mutex_unlock(&cam->s_mutex);
  1344. return ret;
  1345. }
  1346. static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
  1347. struct v4l2_format *fmt)
  1348. {
  1349. struct cafe_camera *cam = priv;
  1350. int ret;
  1351. mutex_lock(&cam->s_mutex);
  1352. ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
  1353. mutex_unlock(&cam->s_mutex);
  1354. return ret;
  1355. }
  1356. static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
  1357. struct v4l2_format *fmt)
  1358. {
  1359. struct cafe_camera *cam = priv;
  1360. int ret;
  1361. /*
  1362. * Can't do anything if the device is not idle
  1363. * Also can't if there are streaming buffers in place.
  1364. */
  1365. if (cam->state != S_IDLE || cam->n_sbufs > 0)
  1366. return -EBUSY;
  1367. /*
  1368. * See if the formatting works in principle.
  1369. */
  1370. ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
  1371. if (ret)
  1372. return ret;
  1373. /*
  1374. * Now we start to change things for real, so let's do it
  1375. * under lock.
  1376. */
  1377. mutex_lock(&cam->s_mutex);
  1378. cam->pix_format = fmt->fmt.pix;
  1379. /*
  1380. * Make sure we have appropriate DMA buffers.
  1381. */
  1382. ret = -ENOMEM;
  1383. if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
  1384. cafe_free_dma_bufs(cam);
  1385. if (cam->nbufs == 0) {
  1386. if (cafe_alloc_dma_bufs(cam, 0))
  1387. goto out;
  1388. }
  1389. /*
  1390. * It looks like this might work, so let's program the sensor.
  1391. */
  1392. ret = cafe_cam_configure(cam);
  1393. if (! ret)
  1394. ret = cafe_ctlr_configure(cam);
  1395. out:
  1396. mutex_unlock(&cam->s_mutex);
  1397. return ret;
  1398. }
  1399. /*
  1400. * Return our stored notion of how the camera is/should be configured.
  1401. * The V4l2 spec wants us to be smarter, and actually get this from
  1402. * the camera (and not mess with it at open time). Someday.
  1403. */
  1404. static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
  1405. struct v4l2_format *f)
  1406. {
  1407. struct cafe_camera *cam = priv;
  1408. f->fmt.pix = cam->pix_format;
  1409. return 0;
  1410. }
  1411. /*
  1412. * We only have one input - the sensor - so minimize the nonsense here.
  1413. */
  1414. static int cafe_vidioc_enum_input(struct file *filp, void *priv,
  1415. struct v4l2_input *input)
  1416. {
  1417. if (input->index != 0)
  1418. return -EINVAL;
  1419. input->type = V4L2_INPUT_TYPE_CAMERA;
  1420. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  1421. strcpy(input->name, "Camera");
  1422. return 0;
  1423. }
  1424. static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  1425. {
  1426. *i = 0;
  1427. return 0;
  1428. }
  1429. static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  1430. {
  1431. if (i != 0)
  1432. return -EINVAL;
  1433. return 0;
  1434. }
  1435. /* from vivi.c */
  1436. static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id a)
  1437. {
  1438. return 0;
  1439. }
  1440. /*
  1441. * The TV Norm stuff is weird - we're a camera with little to do with TV,
  1442. * really. The following is what vivi does.
  1443. */
  1444. static struct v4l2_tvnorm cafe_tvnorm[] = {
  1445. {
  1446. .name = "NTSC-M",
  1447. .id = V4L2_STD_NTSC_M,
  1448. }
  1449. };
  1450. static void cafe_v4l_dev_release(struct video_device *vd)
  1451. {
  1452. struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
  1453. kfree(cam);
  1454. }
  1455. /*
  1456. * This template device holds all of those v4l2 methods; we
  1457. * clone it for specific real devices.
  1458. */
  1459. static struct file_operations cafe_v4l_fops = {
  1460. .owner = THIS_MODULE,
  1461. .open = cafe_v4l_open,
  1462. .release = cafe_v4l_release,
  1463. .read = cafe_v4l_read,
  1464. .poll = cafe_v4l_poll,
  1465. .mmap = cafe_v4l_mmap,
  1466. .ioctl = video_ioctl2,
  1467. .llseek = no_llseek,
  1468. };
  1469. static struct video_device cafe_v4l_template = {
  1470. .name = "cafe",
  1471. .type = VFL_TYPE_GRABBER,
  1472. .type2 = VID_TYPE_CAPTURE,
  1473. .minor = -1, /* Get one dynamically */
  1474. .tvnorms = cafe_tvnorm,
  1475. .tvnormsize = 1,
  1476. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1477. .fops = &cafe_v4l_fops,
  1478. .release = cafe_v4l_dev_release,
  1479. .vidioc_querycap = cafe_vidioc_querycap,
  1480. .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
  1481. .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
  1482. .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
  1483. .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
  1484. .vidioc_enum_input = cafe_vidioc_enum_input,
  1485. .vidioc_g_input = cafe_vidioc_g_input,
  1486. .vidioc_s_input = cafe_vidioc_s_input,
  1487. .vidioc_s_std = cafe_vidioc_s_std,
  1488. .vidioc_reqbufs = cafe_vidioc_reqbufs,
  1489. .vidioc_querybuf = cafe_vidioc_querybuf,
  1490. .vidioc_qbuf = cafe_vidioc_qbuf,
  1491. .vidioc_dqbuf = cafe_vidioc_dqbuf,
  1492. .vidioc_streamon = cafe_vidioc_streamon,
  1493. .vidioc_streamoff = cafe_vidioc_streamoff,
  1494. .vidioc_queryctrl = cafe_vidioc_queryctrl,
  1495. .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
  1496. .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
  1497. /* Do cropping someday */
  1498. };
  1499. /* ---------------------------------------------------------------------- */
  1500. /*
  1501. * Interrupt handler stuff
  1502. */
  1503. /*
  1504. * Create RGB32 from RGB444 so it can be displayed before the applications
  1505. * know about the latter format.
  1506. */
  1507. static void cafe_fake_rgb32(struct cafe_camera *cam, char *dest, char *src)
  1508. {
  1509. int i;
  1510. u16 *ssrc = (u16 *) src;
  1511. /* RGB444 version */
  1512. for (i = 0; i < cam->pix_format.sizeimage; i += 4) {
  1513. // dest[0] = (*ssrc & 0xf000) >> 8;
  1514. dest[0] = (*ssrc & 0x000f) << 4;
  1515. dest[1] = (*ssrc & 0x00f0);
  1516. dest[2] = (*ssrc & 0x0f00) >> 4;
  1517. dest[3] = (*ssrc & 0xf000); /* Alpha */
  1518. dest += 4;
  1519. ssrc++;
  1520. }
  1521. }
  1522. static void cafe_frame_tasklet(unsigned long data)
  1523. {
  1524. struct cafe_camera *cam = (struct cafe_camera *) data;
  1525. int i;
  1526. unsigned long flags;
  1527. struct cafe_sio_buffer *sbuf;
  1528. spin_lock_irqsave(&cam->dev_lock, flags);
  1529. for (i = 0; i < cam->nbufs; i++) {
  1530. int bufno = cam->next_buf;
  1531. if (bufno < 0) { /* "will never happen" */
  1532. cam_err(cam, "No valid bufs in tasklet!\n");
  1533. break;
  1534. }
  1535. if (++(cam->next_buf) >= cam->nbufs)
  1536. cam->next_buf = 0;
  1537. if (! test_bit(bufno, &cam->flags))
  1538. continue;
  1539. if (list_empty(&cam->sb_avail))
  1540. break; /* Leave it valid, hope for better later */
  1541. clear_bit(bufno, &cam->flags);
  1542. /*
  1543. * We could perhaps drop the spinlock during this
  1544. * big copy. Something to consider.
  1545. */
  1546. sbuf = list_entry(cam->sb_avail.next,
  1547. struct cafe_sio_buffer, list);
  1548. if (cam->pix_format.pixelformat == V4L2_PIX_FMT_RGB32)
  1549. cafe_fake_rgb32(cam, sbuf->buffer, cam->dma_bufs[bufno]);
  1550. else
  1551. memcpy(sbuf->buffer, cam->dma_bufs[bufno],
  1552. cam->pix_format.sizeimage);
  1553. sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
  1554. sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
  1555. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1556. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1557. list_move_tail(&sbuf->list, &cam->sb_full);
  1558. }
  1559. if (! list_empty(&cam->sb_full))
  1560. wake_up(&cam->iowait);
  1561. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1562. }
  1563. static void cafe_frame_complete(struct cafe_camera *cam, int frame)
  1564. {
  1565. /*
  1566. * Basic frame housekeeping.
  1567. */
  1568. if (test_bit(frame, &cam->flags) && printk_ratelimit())
  1569. cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
  1570. set_bit(frame, &cam->flags);
  1571. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1572. if (cam->next_buf < 0)
  1573. cam->next_buf = frame;
  1574. cam->buf_seq[frame] = ++(cam->sequence);
  1575. switch (cam->state) {
  1576. /*
  1577. * If in single read mode, try going speculative.
  1578. */
  1579. case S_SINGLEREAD:
  1580. cam->state = S_SPECREAD;
  1581. cam->specframes = 0;
  1582. wake_up(&cam->iowait);
  1583. break;
  1584. /*
  1585. * If we are already doing speculative reads, and nobody is
  1586. * reading them, just stop.
  1587. */
  1588. case S_SPECREAD:
  1589. if (++(cam->specframes) >= cam->nbufs) {
  1590. cafe_ctlr_stop(cam);
  1591. cafe_ctlr_irq_disable(cam);
  1592. cam->state = S_IDLE;
  1593. }
  1594. wake_up(&cam->iowait);
  1595. break;
  1596. /*
  1597. * For the streaming case, we defer the real work to the
  1598. * camera tasklet.
  1599. *
  1600. * FIXME: if the application is not consuming the buffers,
  1601. * we should eventually put things on hold and restart in
  1602. * vidioc_dqbuf().
  1603. */
  1604. case S_STREAMING:
  1605. tasklet_schedule(&cam->s_tasklet);
  1606. break;
  1607. default:
  1608. cam_err(cam, "Frame interrupt in non-operational state\n");
  1609. break;
  1610. }
  1611. }
  1612. static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
  1613. {
  1614. unsigned int frame;
  1615. cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1616. /*
  1617. * Handle any frame completions. There really should
  1618. * not be more than one of these, or we have fallen
  1619. * far behind.
  1620. */
  1621. for (frame = 0; frame < cam->nbufs; frame++)
  1622. if (irqs & (IRQ_EOF0 << frame))
  1623. cafe_frame_complete(cam, frame);
  1624. /*
  1625. * If a frame starts, note that we have DMA active. This
  1626. * code assumes that we won't get multiple frame interrupts
  1627. * at once; may want to rethink that.
  1628. */
  1629. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
  1630. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1631. }
  1632. static irqreturn_t cafe_irq(int irq, void *data)
  1633. {
  1634. struct cafe_camera *cam = data;
  1635. unsigned int irqs;
  1636. spin_lock(&cam->dev_lock);
  1637. irqs = cafe_reg_read(cam, REG_IRQSTAT);
  1638. if ((irqs & ALLIRQS) == 0) {
  1639. spin_unlock(&cam->dev_lock);
  1640. return IRQ_NONE;
  1641. }
  1642. if (irqs & FRAMEIRQS)
  1643. cafe_frame_irq(cam, irqs);
  1644. if (irqs & TWSIIRQS) {
  1645. cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
  1646. wake_up(&cam->smbus_wait);
  1647. }
  1648. spin_unlock(&cam->dev_lock);
  1649. return IRQ_HANDLED;
  1650. }
  1651. /* -------------------------------------------------------------------------- */
  1652. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1653. /*
  1654. * Debugfs stuff.
  1655. */
  1656. static char cafe_debug_buf[1024];
  1657. static struct dentry *cafe_dfs_root;
  1658. static void cafe_dfs_setup(void)
  1659. {
  1660. cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
  1661. if (IS_ERR(cafe_dfs_root)) {
  1662. cafe_dfs_root = NULL; /* Never mind */
  1663. printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
  1664. }
  1665. }
  1666. static void cafe_dfs_shutdown(void)
  1667. {
  1668. if (cafe_dfs_root)
  1669. debugfs_remove(cafe_dfs_root);
  1670. }
  1671. static int cafe_dfs_open(struct inode *inode, struct file *file)
  1672. {
  1673. file->private_data = inode->i_private;
  1674. return 0;
  1675. }
  1676. static ssize_t cafe_dfs_read_regs(struct file *file,
  1677. char __user *buf, size_t count, loff_t *ppos)
  1678. {
  1679. struct cafe_camera *cam = file->private_data;
  1680. char *s = cafe_debug_buf;
  1681. int offset;
  1682. for (offset = 0; offset < 0x44; offset += 4)
  1683. s += sprintf(s, "%02x: %08x\n", offset,
  1684. cafe_reg_read(cam, offset));
  1685. for (offset = 0x88; offset <= 0x90; offset += 4)
  1686. s += sprintf(s, "%02x: %08x\n", offset,
  1687. cafe_reg_read(cam, offset));
  1688. for (offset = 0xb4; offset <= 0xbc; offset += 4)
  1689. s += sprintf(s, "%02x: %08x\n", offset,
  1690. cafe_reg_read(cam, offset));
  1691. for (offset = 0x3000; offset <= 0x300c; offset += 4)
  1692. s += sprintf(s, "%04x: %08x\n", offset,
  1693. cafe_reg_read(cam, offset));
  1694. return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
  1695. s - cafe_debug_buf);
  1696. }
  1697. static struct file_operations cafe_dfs_reg_ops = {
  1698. .owner = THIS_MODULE,
  1699. .read = cafe_dfs_read_regs,
  1700. .open = cafe_dfs_open
  1701. };
  1702. static ssize_t cafe_dfs_read_cam(struct file *file,
  1703. char __user *buf, size_t count, loff_t *ppos)
  1704. {
  1705. struct cafe_camera *cam = file->private_data;
  1706. char *s = cafe_debug_buf;
  1707. int offset;
  1708. if (! cam->sensor)
  1709. return -EINVAL;
  1710. for (offset = 0x0; offset < 0x8a; offset++)
  1711. {
  1712. u8 v;
  1713. cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
  1714. s += sprintf(s, "%02x: %02x\n", offset, v);
  1715. }
  1716. return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
  1717. s - cafe_debug_buf);
  1718. }
  1719. static struct file_operations cafe_dfs_cam_ops = {
  1720. .owner = THIS_MODULE,
  1721. .read = cafe_dfs_read_cam,
  1722. .open = cafe_dfs_open
  1723. };
  1724. static void cafe_dfs_cam_setup(struct cafe_camera *cam)
  1725. {
  1726. char fname[40];
  1727. if (!cafe_dfs_root)
  1728. return;
  1729. sprintf(fname, "regs-%d", cam->v4ldev.minor);
  1730. cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
  1731. cam, &cafe_dfs_reg_ops);
  1732. sprintf(fname, "cam-%d", cam->v4ldev.minor);
  1733. cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
  1734. cam, &cafe_dfs_cam_ops);
  1735. }
  1736. static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
  1737. {
  1738. if (! IS_ERR(cam->dfs_regs))
  1739. debugfs_remove(cam->dfs_regs);
  1740. if (! IS_ERR(cam->dfs_cam_regs))
  1741. debugfs_remove(cam->dfs_cam_regs);
  1742. }
  1743. #else
  1744. #define cafe_dfs_setup()
  1745. #define cafe_dfs_shutdown()
  1746. #define cafe_dfs_cam_setup(cam)
  1747. #define cafe_dfs_cam_shutdown(cam)
  1748. #endif /* CONFIG_VIDEO_ADV_DEBUG */
  1749. /* ------------------------------------------------------------------------*/
  1750. /*
  1751. * PCI interface stuff.
  1752. */
  1753. static int cafe_pci_probe(struct pci_dev *pdev,
  1754. const struct pci_device_id *id)
  1755. {
  1756. int ret;
  1757. u16 classword;
  1758. struct cafe_camera *cam;
  1759. /*
  1760. * Make sure we have a camera here - we'll get calls for
  1761. * the other cafe devices as well.
  1762. */
  1763. pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
  1764. if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
  1765. return -ENODEV;
  1766. /*
  1767. * Start putting together one of our big camera structures.
  1768. */
  1769. ret = -ENOMEM;
  1770. cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
  1771. if (cam == NULL)
  1772. goto out;
  1773. mutex_init(&cam->s_mutex);
  1774. mutex_lock(&cam->s_mutex);
  1775. spin_lock_init(&cam->dev_lock);
  1776. cam->state = S_NOTREADY;
  1777. cafe_set_config_needed(cam, 1);
  1778. init_waitqueue_head(&cam->smbus_wait);
  1779. init_waitqueue_head(&cam->iowait);
  1780. cam->pdev = pdev;
  1781. cam->pix_format = cafe_def_pix_format;
  1782. INIT_LIST_HEAD(&cam->dev_list);
  1783. INIT_LIST_HEAD(&cam->sb_avail);
  1784. INIT_LIST_HEAD(&cam->sb_full);
  1785. tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
  1786. /*
  1787. * Get set up on the PCI bus.
  1788. */
  1789. ret = pci_enable_device(pdev);
  1790. if (ret)
  1791. goto out_free;
  1792. pci_set_master(pdev);
  1793. ret = -EIO;
  1794. cam->regs = pci_iomap(pdev, 0, 0);
  1795. if (! cam->regs) {
  1796. printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
  1797. goto out_free;
  1798. }
  1799. ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
  1800. if (ret)
  1801. goto out_iounmap;
  1802. cafe_ctlr_init(cam);
  1803. cafe_ctlr_power_up(cam);
  1804. /*
  1805. * Set up I2C/SMBUS communications
  1806. */
  1807. mutex_unlock(&cam->s_mutex); /* attach can deadlock */
  1808. ret = cafe_smbus_setup(cam);
  1809. if (ret)
  1810. goto out_freeirq;
  1811. /*
  1812. * Get the v4l2 setup done.
  1813. */
  1814. mutex_lock(&cam->s_mutex);
  1815. cam->v4ldev = cafe_v4l_template;
  1816. cam->v4ldev.debug = 0;
  1817. // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
  1818. ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
  1819. if (ret)
  1820. goto out_smbus;
  1821. /*
  1822. * If so requested, try to get our DMA buffers now.
  1823. */
  1824. if (alloc_bufs_at_load) {
  1825. if (cafe_alloc_dma_bufs(cam, 1))
  1826. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1827. " will try again later.");
  1828. }
  1829. cafe_dfs_cam_setup(cam);
  1830. mutex_unlock(&cam->s_mutex);
  1831. cafe_add_dev(cam);
  1832. return 0;
  1833. out_smbus:
  1834. cafe_smbus_shutdown(cam);
  1835. out_freeirq:
  1836. cafe_ctlr_power_down(cam);
  1837. free_irq(pdev->irq, cam);
  1838. out_iounmap:
  1839. pci_iounmap(pdev, cam->regs);
  1840. out_free:
  1841. kfree(cam);
  1842. out:
  1843. return ret;
  1844. }
  1845. /*
  1846. * Shut down an initialized device
  1847. */
  1848. static void cafe_shutdown(struct cafe_camera *cam)
  1849. {
  1850. /* FIXME: Make sure we take care of everything here */
  1851. cafe_dfs_cam_shutdown(cam);
  1852. if (cam->n_sbufs > 0)
  1853. /* What if they are still mapped? Shouldn't be, but... */
  1854. cafe_free_sio_buffers(cam);
  1855. cafe_remove_dev(cam);
  1856. cafe_ctlr_stop_dma(cam);
  1857. cafe_ctlr_power_down(cam);
  1858. cafe_smbus_shutdown(cam);
  1859. cafe_free_dma_bufs(cam);
  1860. free_irq(cam->pdev->irq, cam);
  1861. pci_iounmap(cam->pdev, cam->regs);
  1862. video_unregister_device(&cam->v4ldev);
  1863. /* kfree(cam); done in v4l_release () */
  1864. }
  1865. static void cafe_pci_remove(struct pci_dev *pdev)
  1866. {
  1867. struct cafe_camera *cam = cafe_find_by_pdev(pdev);
  1868. if (cam == NULL) {
  1869. cam_warn(cam, "pci_remove on unknown pdev %p\n", pdev);
  1870. return;
  1871. }
  1872. mutex_lock(&cam->s_mutex);
  1873. if (cam->users > 0)
  1874. cam_warn(cam, "Removing a device with users!\n");
  1875. cafe_shutdown(cam);
  1876. /* No unlock - it no longer exists */
  1877. }
  1878. static struct pci_device_id cafe_ids[] = {
  1879. { PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
  1880. { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
  1881. { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
  1882. { 0, }
  1883. };
  1884. MODULE_DEVICE_TABLE(pci, cafe_ids);
  1885. static struct pci_driver cafe_pci_driver = {
  1886. .name = "cafe1000-ccic",
  1887. .id_table = cafe_ids,
  1888. .probe = cafe_pci_probe,
  1889. .remove = cafe_pci_remove,
  1890. };
  1891. static int __init cafe_init(void)
  1892. {
  1893. int ret;
  1894. printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
  1895. CAFE_VERSION);
  1896. cafe_dfs_setup();
  1897. ret = pci_register_driver(&cafe_pci_driver);
  1898. if (ret) {
  1899. printk(KERN_ERR "Unable to register cafe_ccic driver\n");
  1900. goto out;
  1901. }
  1902. request_module("ov7670"); /* FIXME want something more general */
  1903. ret = 0;
  1904. out:
  1905. return ret;
  1906. }
  1907. static void __exit cafe_exit(void)
  1908. {
  1909. pci_unregister_driver(&cafe_pci_driver);
  1910. cafe_dfs_shutdown();
  1911. }
  1912. module_init(cafe_init);
  1913. module_exit(cafe_exit);