cafe_ccic.c 55 KB

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