atmel-isc.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  1. /*
  2. * Atmel Image Sensor Controller (ISC) driver
  3. *
  4. * Copyright (C) 2016 Atmel
  5. *
  6. * Author: Songjun Wu <songjun.wu@microchip.com>
  7. *
  8. * This program is free software; you may redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * Sensor-->PFE-->WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB-->RLP-->DMA
  13. *
  14. * ISC video pipeline integrates the following submodules:
  15. * PFE: Parallel Front End to sample the camera sensor input stream
  16. * WB: Programmable white balance in the Bayer domain
  17. * CFA: Color filter array interpolation module
  18. * CC: Programmable color correction
  19. * GAM: Gamma correction
  20. * CSC: Programmable color space conversion
  21. * CBC: Contrast and Brightness control
  22. * SUB: This module performs YCbCr444 to YCbCr420 chrominance subsampling
  23. * RLP: This module performs rounding, range limiting
  24. * and packing of the incoming data
  25. */
  26. #include <linux/clk.h>
  27. #include <linux/clkdev.h>
  28. #include <linux/clk-provider.h>
  29. #include <linux/delay.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/math64.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/of_graph.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/regmap.h>
  38. #include <linux/videodev2.h>
  39. #include <media/v4l2-ctrls.h>
  40. #include <media/v4l2-device.h>
  41. #include <media/v4l2-event.h>
  42. #include <media/v4l2-image-sizes.h>
  43. #include <media/v4l2-ioctl.h>
  44. #include <media/v4l2-fwnode.h>
  45. #include <media/v4l2-subdev.h>
  46. #include <media/videobuf2-dma-contig.h>
  47. #include "atmel-isc-regs.h"
  48. #define ATMEL_ISC_NAME "atmel_isc"
  49. #define ISC_MAX_SUPPORT_WIDTH 2592
  50. #define ISC_MAX_SUPPORT_HEIGHT 1944
  51. #define ISC_CLK_MAX_DIV 255
  52. enum isc_clk_id {
  53. ISC_ISPCK = 0,
  54. ISC_MCK = 1,
  55. };
  56. struct isc_clk {
  57. struct clk_hw hw;
  58. struct clk *clk;
  59. struct regmap *regmap;
  60. spinlock_t lock;
  61. u8 id;
  62. u8 parent_id;
  63. u32 div;
  64. struct device *dev;
  65. };
  66. #define to_isc_clk(hw) container_of(hw, struct isc_clk, hw)
  67. struct isc_buffer {
  68. struct vb2_v4l2_buffer vb;
  69. struct list_head list;
  70. };
  71. struct isc_subdev_entity {
  72. struct v4l2_subdev *sd;
  73. struct v4l2_async_subdev *asd;
  74. struct v4l2_async_notifier notifier;
  75. u32 pfe_cfg0;
  76. struct list_head list;
  77. };
  78. /* Indicate the format is generated by the sensor */
  79. #define FMT_FLAG_FROM_SENSOR BIT(0)
  80. /* Indicate the format is produced by ISC itself */
  81. #define FMT_FLAG_FROM_CONTROLLER BIT(1)
  82. /* Indicate a Raw Bayer format */
  83. #define FMT_FLAG_RAW_FORMAT BIT(2)
  84. #define FMT_FLAG_RAW_FROM_SENSOR (FMT_FLAG_FROM_SENSOR | \
  85. FMT_FLAG_RAW_FORMAT)
  86. /*
  87. * struct isc_format - ISC media bus format information
  88. * @fourcc: Fourcc code for this format
  89. * @mbus_code: V4L2 media bus format code.
  90. * flags: Indicate format from sensor or converted by controller
  91. * @bpp: Bits per pixel (when stored in memory)
  92. * (when transferred over a bus)
  93. * @sd_support: Subdev supports this format
  94. * @isc_support: ISC can convert raw format to this format
  95. */
  96. struct isc_format {
  97. u32 fourcc;
  98. u32 mbus_code;
  99. u32 flags;
  100. u8 bpp;
  101. bool sd_support;
  102. bool isc_support;
  103. };
  104. /* Pipeline bitmap */
  105. #define WB_ENABLE BIT(0)
  106. #define CFA_ENABLE BIT(1)
  107. #define CC_ENABLE BIT(2)
  108. #define GAM_ENABLE BIT(3)
  109. #define GAM_BENABLE BIT(4)
  110. #define GAM_GENABLE BIT(5)
  111. #define GAM_RENABLE BIT(6)
  112. #define CSC_ENABLE BIT(7)
  113. #define CBC_ENABLE BIT(8)
  114. #define SUB422_ENABLE BIT(9)
  115. #define SUB420_ENABLE BIT(10)
  116. #define GAM_ENABLES (GAM_RENABLE | GAM_GENABLE | GAM_BENABLE | GAM_ENABLE)
  117. struct fmt_config {
  118. u32 fourcc;
  119. u32 pfe_cfg0_bps;
  120. u32 cfa_baycfg;
  121. u32 rlp_cfg_mode;
  122. u32 dcfg_imode;
  123. u32 dctrl_dview;
  124. u32 bits_pipeline;
  125. };
  126. #define HIST_ENTRIES 512
  127. #define HIST_BAYER (ISC_HIS_CFG_MODE_B + 1)
  128. enum{
  129. HIST_INIT = 0,
  130. HIST_ENABLED,
  131. HIST_DISABLED,
  132. };
  133. struct isc_ctrls {
  134. struct v4l2_ctrl_handler handler;
  135. u32 brightness;
  136. u32 contrast;
  137. u8 gamma_index;
  138. u8 awb;
  139. u32 r_gain;
  140. u32 b_gain;
  141. u32 hist_entry[HIST_ENTRIES];
  142. u32 hist_count[HIST_BAYER];
  143. u8 hist_id;
  144. u8 hist_stat;
  145. };
  146. #define ISC_PIPE_LINE_NODE_NUM 11
  147. struct isc_device {
  148. struct regmap *regmap;
  149. struct clk *hclock;
  150. struct clk *ispck;
  151. struct isc_clk isc_clks[2];
  152. struct device *dev;
  153. struct v4l2_device v4l2_dev;
  154. struct video_device video_dev;
  155. struct vb2_queue vb2_vidq;
  156. spinlock_t dma_queue_lock;
  157. struct list_head dma_queue;
  158. struct isc_buffer *cur_frm;
  159. unsigned int sequence;
  160. bool stop;
  161. struct completion comp;
  162. struct v4l2_format fmt;
  163. struct isc_format **user_formats;
  164. unsigned int num_user_formats;
  165. const struct isc_format *current_fmt;
  166. const struct isc_format *raw_fmt;
  167. struct isc_ctrls ctrls;
  168. struct work_struct awb_work;
  169. struct mutex lock;
  170. struct regmap_field *pipeline[ISC_PIPE_LINE_NODE_NUM];
  171. struct isc_subdev_entity *current_subdev;
  172. struct list_head subdev_entities;
  173. };
  174. static struct isc_format formats_list[] = {
  175. {
  176. .fourcc = V4L2_PIX_FMT_SBGGR8,
  177. .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
  178. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  179. .bpp = 8,
  180. },
  181. {
  182. .fourcc = V4L2_PIX_FMT_SGBRG8,
  183. .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
  184. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  185. .bpp = 8,
  186. },
  187. {
  188. .fourcc = V4L2_PIX_FMT_SGRBG8,
  189. .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
  190. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  191. .bpp = 8,
  192. },
  193. {
  194. .fourcc = V4L2_PIX_FMT_SRGGB8,
  195. .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
  196. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  197. .bpp = 8,
  198. },
  199. {
  200. .fourcc = V4L2_PIX_FMT_SBGGR10,
  201. .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
  202. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  203. .bpp = 16,
  204. },
  205. {
  206. .fourcc = V4L2_PIX_FMT_SGBRG10,
  207. .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
  208. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  209. .bpp = 16,
  210. },
  211. {
  212. .fourcc = V4L2_PIX_FMT_SGRBG10,
  213. .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
  214. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  215. .bpp = 16,
  216. },
  217. {
  218. .fourcc = V4L2_PIX_FMT_SRGGB10,
  219. .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
  220. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  221. .bpp = 16,
  222. },
  223. {
  224. .fourcc = V4L2_PIX_FMT_SBGGR12,
  225. .mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
  226. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  227. .bpp = 16,
  228. },
  229. {
  230. .fourcc = V4L2_PIX_FMT_SGBRG12,
  231. .mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
  232. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  233. .bpp = 16,
  234. },
  235. {
  236. .fourcc = V4L2_PIX_FMT_SGRBG12,
  237. .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
  238. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  239. .bpp = 16,
  240. },
  241. {
  242. .fourcc = V4L2_PIX_FMT_SRGGB12,
  243. .mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
  244. .flags = FMT_FLAG_RAW_FROM_SENSOR,
  245. .bpp = 16,
  246. },
  247. {
  248. .fourcc = V4L2_PIX_FMT_YUV420,
  249. .mbus_code = 0x0,
  250. .flags = FMT_FLAG_FROM_CONTROLLER,
  251. .bpp = 12,
  252. },
  253. {
  254. .fourcc = V4L2_PIX_FMT_YUV422P,
  255. .mbus_code = 0x0,
  256. .flags = FMT_FLAG_FROM_CONTROLLER,
  257. .bpp = 16,
  258. },
  259. {
  260. .fourcc = V4L2_PIX_FMT_GREY,
  261. .mbus_code = MEDIA_BUS_FMT_Y8_1X8,
  262. .flags = FMT_FLAG_FROM_CONTROLLER |
  263. FMT_FLAG_FROM_SENSOR,
  264. .bpp = 8,
  265. },
  266. {
  267. .fourcc = V4L2_PIX_FMT_ARGB444,
  268. .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
  269. .flags = FMT_FLAG_FROM_CONTROLLER,
  270. .bpp = 16,
  271. },
  272. {
  273. .fourcc = V4L2_PIX_FMT_ARGB555,
  274. .mbus_code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
  275. .flags = FMT_FLAG_FROM_CONTROLLER,
  276. .bpp = 16,
  277. },
  278. {
  279. .fourcc = V4L2_PIX_FMT_RGB565,
  280. .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  281. .flags = FMT_FLAG_FROM_CONTROLLER,
  282. .bpp = 16,
  283. },
  284. {
  285. .fourcc = V4L2_PIX_FMT_ARGB32,
  286. .mbus_code = MEDIA_BUS_FMT_ARGB8888_1X32,
  287. .flags = FMT_FLAG_FROM_CONTROLLER,
  288. .bpp = 32,
  289. },
  290. {
  291. .fourcc = V4L2_PIX_FMT_YUYV,
  292. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  293. .flags = FMT_FLAG_FROM_CONTROLLER |
  294. FMT_FLAG_FROM_SENSOR,
  295. .bpp = 16,
  296. },
  297. };
  298. static struct fmt_config fmt_configs_list[] = {
  299. {
  300. .fourcc = V4L2_PIX_FMT_SBGGR8,
  301. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  302. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  303. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8,
  304. .dcfg_imode = ISC_DCFG_IMODE_PACKED8,
  305. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  306. .bits_pipeline = 0x0,
  307. },
  308. {
  309. .fourcc = V4L2_PIX_FMT_SGBRG8,
  310. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  311. .cfa_baycfg = ISC_BAY_CFG_GBGB,
  312. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8,
  313. .dcfg_imode = ISC_DCFG_IMODE_PACKED8,
  314. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  315. .bits_pipeline = 0x0,
  316. },
  317. {
  318. .fourcc = V4L2_PIX_FMT_SGRBG8,
  319. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  320. .cfa_baycfg = ISC_BAY_CFG_GRGR,
  321. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8,
  322. .dcfg_imode = ISC_DCFG_IMODE_PACKED8,
  323. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  324. .bits_pipeline = 0x0,
  325. },
  326. {
  327. .fourcc = V4L2_PIX_FMT_SRGGB8,
  328. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  329. .cfa_baycfg = ISC_BAY_CFG_RGRG,
  330. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8,
  331. .dcfg_imode = ISC_DCFG_IMODE_PACKED8,
  332. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  333. .bits_pipeline = 0x0,
  334. },
  335. {
  336. .fourcc = V4L2_PIX_FMT_SBGGR10,
  337. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
  338. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  339. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10,
  340. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  341. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  342. .bits_pipeline = 0x0,
  343. },
  344. {
  345. .fourcc = V4L2_PIX_FMT_SGBRG10,
  346. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
  347. .cfa_baycfg = ISC_BAY_CFG_GBGB,
  348. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10,
  349. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  350. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  351. .bits_pipeline = 0x0,
  352. },
  353. {
  354. .fourcc = V4L2_PIX_FMT_SGRBG10,
  355. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
  356. .cfa_baycfg = ISC_BAY_CFG_GRGR,
  357. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10,
  358. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  359. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  360. .bits_pipeline = 0x0,
  361. },
  362. {
  363. .fourcc = V4L2_PIX_FMT_SRGGB10,
  364. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
  365. .cfa_baycfg = ISC_BAY_CFG_RGRG,
  366. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10,
  367. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  368. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  369. .bits_pipeline = 0x0,
  370. },
  371. {
  372. .fourcc = V4L2_PIX_FMT_SBGGR12,
  373. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
  374. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  375. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12,
  376. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  377. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  378. .bits_pipeline = 0x0,
  379. },
  380. {
  381. .fourcc = V4L2_PIX_FMT_SGBRG12,
  382. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
  383. .cfa_baycfg = ISC_BAY_CFG_GBGB,
  384. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12,
  385. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  386. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  387. .bits_pipeline = 0x0
  388. },
  389. {
  390. .fourcc = V4L2_PIX_FMT_SGRBG12,
  391. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
  392. .cfa_baycfg = ISC_BAY_CFG_GRGR,
  393. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12,
  394. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  395. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  396. .bits_pipeline = 0x0,
  397. },
  398. {
  399. .fourcc = V4L2_PIX_FMT_SRGGB12,
  400. .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
  401. .cfa_baycfg = ISC_BAY_CFG_RGRG,
  402. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12,
  403. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  404. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  405. .bits_pipeline = 0x0,
  406. },
  407. {
  408. .fourcc = V4L2_PIX_FMT_YUV420,
  409. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  410. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  411. .rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC,
  412. .dcfg_imode = ISC_DCFG_IMODE_YC420P,
  413. .dctrl_dview = ISC_DCTRL_DVIEW_PLANAR,
  414. .bits_pipeline = SUB420_ENABLE | SUB422_ENABLE |
  415. CBC_ENABLE | CSC_ENABLE |
  416. GAM_ENABLES |
  417. CFA_ENABLE | WB_ENABLE,
  418. },
  419. {
  420. .fourcc = V4L2_PIX_FMT_YUV422P,
  421. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  422. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  423. .rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC,
  424. .dcfg_imode = ISC_DCFG_IMODE_YC422P,
  425. .dctrl_dview = ISC_DCTRL_DVIEW_PLANAR,
  426. .bits_pipeline = SUB422_ENABLE |
  427. CBC_ENABLE | CSC_ENABLE |
  428. GAM_ENABLES |
  429. CFA_ENABLE | WB_ENABLE,
  430. },
  431. {
  432. .fourcc = V4L2_PIX_FMT_GREY,
  433. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  434. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  435. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY8,
  436. .dcfg_imode = ISC_DCFG_IMODE_PACKED8,
  437. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  438. .bits_pipeline = CBC_ENABLE | CSC_ENABLE |
  439. GAM_ENABLES |
  440. CFA_ENABLE | WB_ENABLE,
  441. },
  442. {
  443. .fourcc = V4L2_PIX_FMT_ARGB444,
  444. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  445. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  446. .rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB444,
  447. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  448. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  449. .bits_pipeline = GAM_ENABLES | CFA_ENABLE | WB_ENABLE,
  450. },
  451. {
  452. .fourcc = V4L2_PIX_FMT_ARGB555,
  453. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  454. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  455. .rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB555,
  456. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  457. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  458. .bits_pipeline = GAM_ENABLES | CFA_ENABLE | WB_ENABLE,
  459. },
  460. {
  461. .fourcc = V4L2_PIX_FMT_RGB565,
  462. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  463. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  464. .rlp_cfg_mode = ISC_RLP_CFG_MODE_RGB565,
  465. .dcfg_imode = ISC_DCFG_IMODE_PACKED16,
  466. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  467. .bits_pipeline = GAM_ENABLES | CFA_ENABLE | WB_ENABLE,
  468. },
  469. {
  470. .fourcc = V4L2_PIX_FMT_ARGB32,
  471. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  472. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  473. .rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB32,
  474. .dcfg_imode = ISC_DCFG_IMODE_PACKED32,
  475. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  476. .bits_pipeline = GAM_ENABLES | CFA_ENABLE | WB_ENABLE,
  477. },
  478. {
  479. .fourcc = V4L2_PIX_FMT_YUYV,
  480. .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
  481. .cfa_baycfg = ISC_BAY_CFG_BGBG,
  482. .rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8,
  483. .dcfg_imode = ISC_DCFG_IMODE_PACKED8,
  484. .dctrl_dview = ISC_DCTRL_DVIEW_PACKED,
  485. .bits_pipeline = 0x0
  486. },
  487. };
  488. #define GAMMA_MAX 2
  489. #define GAMMA_ENTRIES 64
  490. /* Gamma table with gamma 1/2.2 */
  491. static const u32 isc_gamma_table[GAMMA_MAX + 1][GAMMA_ENTRIES] = {
  492. /* 0 --> gamma 1/1.8 */
  493. { 0x65, 0x66002F, 0x950025, 0xBB0020, 0xDB001D, 0xF8001A,
  494. 0x1130018, 0x12B0017, 0x1420016, 0x1580014, 0x16D0013, 0x1810012,
  495. 0x1940012, 0x1A60012, 0x1B80011, 0x1C90010, 0x1DA0010, 0x1EA000F,
  496. 0x1FA000F, 0x209000F, 0x218000F, 0x227000E, 0x235000E, 0x243000E,
  497. 0x251000E, 0x25F000D, 0x26C000D, 0x279000D, 0x286000D, 0x293000C,
  498. 0x2A0000C, 0x2AC000C, 0x2B8000C, 0x2C4000C, 0x2D0000B, 0x2DC000B,
  499. 0x2E7000B, 0x2F3000B, 0x2FE000B, 0x309000B, 0x314000B, 0x31F000A,
  500. 0x32A000A, 0x334000B, 0x33F000A, 0x349000A, 0x354000A, 0x35E000A,
  501. 0x368000A, 0x372000A, 0x37C000A, 0x386000A, 0x3900009, 0x399000A,
  502. 0x3A30009, 0x3AD0009, 0x3B60009, 0x3BF000A, 0x3C90009, 0x3D20009,
  503. 0x3DB0009, 0x3E40009, 0x3ED0009, 0x3F60009 },
  504. /* 1 --> gamma 1/2 */
  505. { 0x7F, 0x800034, 0xB50028, 0xDE0021, 0x100001E, 0x11E001B,
  506. 0x1390019, 0x1520017, 0x16A0015, 0x1800014, 0x1940014, 0x1A80013,
  507. 0x1BB0012, 0x1CD0011, 0x1DF0010, 0x1EF0010, 0x200000F, 0x20F000F,
  508. 0x21F000E, 0x22D000F, 0x23C000E, 0x24A000E, 0x258000D, 0x265000D,
  509. 0x273000C, 0x27F000D, 0x28C000C, 0x299000C, 0x2A5000C, 0x2B1000B,
  510. 0x2BC000C, 0x2C8000B, 0x2D3000C, 0x2DF000B, 0x2EA000A, 0x2F5000A,
  511. 0x2FF000B, 0x30A000A, 0x314000B, 0x31F000A, 0x329000A, 0x333000A,
  512. 0x33D0009, 0x3470009, 0x350000A, 0x35A0009, 0x363000A, 0x36D0009,
  513. 0x3760009, 0x37F0009, 0x3880009, 0x3910009, 0x39A0009, 0x3A30009,
  514. 0x3AC0008, 0x3B40009, 0x3BD0008, 0x3C60008, 0x3CE0008, 0x3D60009,
  515. 0x3DF0008, 0x3E70008, 0x3EF0008, 0x3F70008 },
  516. /* 2 --> gamma 1/2.2 */
  517. { 0x99, 0x9B0038, 0xD4002A, 0xFF0023, 0x122001F, 0x141001B,
  518. 0x15D0019, 0x1760017, 0x18E0015, 0x1A30015, 0x1B80013, 0x1CC0012,
  519. 0x1DE0011, 0x1F00010, 0x2010010, 0x2110010, 0x221000F, 0x230000F,
  520. 0x23F000E, 0x24D000E, 0x25B000D, 0x269000C, 0x276000C, 0x283000C,
  521. 0x28F000C, 0x29B000C, 0x2A7000C, 0x2B3000B, 0x2BF000B, 0x2CA000B,
  522. 0x2D5000B, 0x2E0000A, 0x2EB000A, 0x2F5000A, 0x2FF000A, 0x30A000A,
  523. 0x3140009, 0x31E0009, 0x327000A, 0x3310009, 0x33A0009, 0x3440009,
  524. 0x34D0009, 0x3560009, 0x35F0009, 0x3680008, 0x3710008, 0x3790009,
  525. 0x3820008, 0x38A0008, 0x3930008, 0x39B0008, 0x3A30008, 0x3AB0008,
  526. 0x3B30008, 0x3BB0008, 0x3C30008, 0x3CB0007, 0x3D20008, 0x3DA0007,
  527. 0x3E20007, 0x3E90007, 0x3F00008, 0x3F80007 },
  528. };
  529. static unsigned int sensor_preferred = 1;
  530. module_param(sensor_preferred, uint, 0644);
  531. MODULE_PARM_DESC(sensor_preferred,
  532. "Sensor is preferred to output the specified format (1-on 0-off), default 1");
  533. static int isc_wait_clk_stable(struct clk_hw *hw)
  534. {
  535. struct isc_clk *isc_clk = to_isc_clk(hw);
  536. struct regmap *regmap = isc_clk->regmap;
  537. unsigned long timeout = jiffies + usecs_to_jiffies(1000);
  538. unsigned int status;
  539. while (time_before(jiffies, timeout)) {
  540. regmap_read(regmap, ISC_CLKSR, &status);
  541. if (!(status & ISC_CLKSR_SIP))
  542. return 0;
  543. usleep_range(10, 250);
  544. }
  545. return -ETIMEDOUT;
  546. }
  547. static int isc_clk_prepare(struct clk_hw *hw)
  548. {
  549. struct isc_clk *isc_clk = to_isc_clk(hw);
  550. if (isc_clk->id == ISC_ISPCK)
  551. pm_runtime_get_sync(isc_clk->dev);
  552. return isc_wait_clk_stable(hw);
  553. }
  554. static void isc_clk_unprepare(struct clk_hw *hw)
  555. {
  556. struct isc_clk *isc_clk = to_isc_clk(hw);
  557. isc_wait_clk_stable(hw);
  558. if (isc_clk->id == ISC_ISPCK)
  559. pm_runtime_put_sync(isc_clk->dev);
  560. }
  561. static int isc_clk_enable(struct clk_hw *hw)
  562. {
  563. struct isc_clk *isc_clk = to_isc_clk(hw);
  564. u32 id = isc_clk->id;
  565. struct regmap *regmap = isc_clk->regmap;
  566. unsigned long flags;
  567. unsigned int status;
  568. dev_dbg(isc_clk->dev, "ISC CLK: %s, div = %d, parent id = %d\n",
  569. __func__, isc_clk->div, isc_clk->parent_id);
  570. spin_lock_irqsave(&isc_clk->lock, flags);
  571. regmap_update_bits(regmap, ISC_CLKCFG,
  572. ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
  573. (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
  574. (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
  575. regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
  576. spin_unlock_irqrestore(&isc_clk->lock, flags);
  577. regmap_read(regmap, ISC_CLKSR, &status);
  578. if (status & ISC_CLK(id))
  579. return 0;
  580. else
  581. return -EINVAL;
  582. }
  583. static void isc_clk_disable(struct clk_hw *hw)
  584. {
  585. struct isc_clk *isc_clk = to_isc_clk(hw);
  586. u32 id = isc_clk->id;
  587. unsigned long flags;
  588. spin_lock_irqsave(&isc_clk->lock, flags);
  589. regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
  590. spin_unlock_irqrestore(&isc_clk->lock, flags);
  591. }
  592. static int isc_clk_is_enabled(struct clk_hw *hw)
  593. {
  594. struct isc_clk *isc_clk = to_isc_clk(hw);
  595. u32 status;
  596. if (isc_clk->id == ISC_ISPCK)
  597. pm_runtime_get_sync(isc_clk->dev);
  598. regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
  599. if (isc_clk->id == ISC_ISPCK)
  600. pm_runtime_put_sync(isc_clk->dev);
  601. return status & ISC_CLK(isc_clk->id) ? 1 : 0;
  602. }
  603. static unsigned long
  604. isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  605. {
  606. struct isc_clk *isc_clk = to_isc_clk(hw);
  607. return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
  608. }
  609. static int isc_clk_determine_rate(struct clk_hw *hw,
  610. struct clk_rate_request *req)
  611. {
  612. struct isc_clk *isc_clk = to_isc_clk(hw);
  613. long best_rate = -EINVAL;
  614. int best_diff = -1;
  615. unsigned int i, div;
  616. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  617. struct clk_hw *parent;
  618. unsigned long parent_rate;
  619. parent = clk_hw_get_parent_by_index(hw, i);
  620. if (!parent)
  621. continue;
  622. parent_rate = clk_hw_get_rate(parent);
  623. if (!parent_rate)
  624. continue;
  625. for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
  626. unsigned long rate;
  627. int diff;
  628. rate = DIV_ROUND_CLOSEST(parent_rate, div);
  629. diff = abs(req->rate - rate);
  630. if (best_diff < 0 || best_diff > diff) {
  631. best_rate = rate;
  632. best_diff = diff;
  633. req->best_parent_rate = parent_rate;
  634. req->best_parent_hw = parent;
  635. }
  636. if (!best_diff || rate < req->rate)
  637. break;
  638. }
  639. if (!best_diff)
  640. break;
  641. }
  642. dev_dbg(isc_clk->dev,
  643. "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  644. __func__, best_rate,
  645. __clk_get_name((req->best_parent_hw)->clk),
  646. req->best_parent_rate);
  647. if (best_rate < 0)
  648. return best_rate;
  649. req->rate = best_rate;
  650. return 0;
  651. }
  652. static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
  653. {
  654. struct isc_clk *isc_clk = to_isc_clk(hw);
  655. if (index >= clk_hw_get_num_parents(hw))
  656. return -EINVAL;
  657. isc_clk->parent_id = index;
  658. return 0;
  659. }
  660. static u8 isc_clk_get_parent(struct clk_hw *hw)
  661. {
  662. struct isc_clk *isc_clk = to_isc_clk(hw);
  663. return isc_clk->parent_id;
  664. }
  665. static int isc_clk_set_rate(struct clk_hw *hw,
  666. unsigned long rate,
  667. unsigned long parent_rate)
  668. {
  669. struct isc_clk *isc_clk = to_isc_clk(hw);
  670. u32 div;
  671. if (!rate)
  672. return -EINVAL;
  673. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  674. if (div > (ISC_CLK_MAX_DIV + 1) || !div)
  675. return -EINVAL;
  676. isc_clk->div = div - 1;
  677. return 0;
  678. }
  679. static const struct clk_ops isc_clk_ops = {
  680. .prepare = isc_clk_prepare,
  681. .unprepare = isc_clk_unprepare,
  682. .enable = isc_clk_enable,
  683. .disable = isc_clk_disable,
  684. .is_enabled = isc_clk_is_enabled,
  685. .recalc_rate = isc_clk_recalc_rate,
  686. .determine_rate = isc_clk_determine_rate,
  687. .set_parent = isc_clk_set_parent,
  688. .get_parent = isc_clk_get_parent,
  689. .set_rate = isc_clk_set_rate,
  690. };
  691. static int isc_clk_register(struct isc_device *isc, unsigned int id)
  692. {
  693. struct regmap *regmap = isc->regmap;
  694. struct device_node *np = isc->dev->of_node;
  695. struct isc_clk *isc_clk;
  696. struct clk_init_data init;
  697. const char *clk_name = np->name;
  698. const char *parent_names[3];
  699. int num_parents;
  700. num_parents = of_clk_get_parent_count(np);
  701. if (num_parents < 1 || num_parents > 3)
  702. return -EINVAL;
  703. if (num_parents > 2 && id == ISC_ISPCK)
  704. num_parents = 2;
  705. of_clk_parent_fill(np, parent_names, num_parents);
  706. if (id == ISC_MCK)
  707. of_property_read_string(np, "clock-output-names", &clk_name);
  708. else
  709. clk_name = "isc-ispck";
  710. init.parent_names = parent_names;
  711. init.num_parents = num_parents;
  712. init.name = clk_name;
  713. init.ops = &isc_clk_ops;
  714. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  715. isc_clk = &isc->isc_clks[id];
  716. isc_clk->hw.init = &init;
  717. isc_clk->regmap = regmap;
  718. isc_clk->id = id;
  719. isc_clk->dev = isc->dev;
  720. spin_lock_init(&isc_clk->lock);
  721. isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
  722. if (IS_ERR(isc_clk->clk)) {
  723. dev_err(isc->dev, "%s: clock register fail\n", clk_name);
  724. return PTR_ERR(isc_clk->clk);
  725. } else if (id == ISC_MCK)
  726. of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
  727. return 0;
  728. }
  729. static int isc_clk_init(struct isc_device *isc)
  730. {
  731. unsigned int i;
  732. int ret;
  733. for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
  734. isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
  735. for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
  736. ret = isc_clk_register(isc, i);
  737. if (ret)
  738. return ret;
  739. }
  740. return 0;
  741. }
  742. static void isc_clk_cleanup(struct isc_device *isc)
  743. {
  744. unsigned int i;
  745. of_clk_del_provider(isc->dev->of_node);
  746. for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
  747. struct isc_clk *isc_clk = &isc->isc_clks[i];
  748. if (!IS_ERR(isc_clk->clk))
  749. clk_unregister(isc_clk->clk);
  750. }
  751. }
  752. static int isc_queue_setup(struct vb2_queue *vq,
  753. unsigned int *nbuffers, unsigned int *nplanes,
  754. unsigned int sizes[], struct device *alloc_devs[])
  755. {
  756. struct isc_device *isc = vb2_get_drv_priv(vq);
  757. unsigned int size = isc->fmt.fmt.pix.sizeimage;
  758. if (*nplanes)
  759. return sizes[0] < size ? -EINVAL : 0;
  760. *nplanes = 1;
  761. sizes[0] = size;
  762. return 0;
  763. }
  764. static int isc_buffer_prepare(struct vb2_buffer *vb)
  765. {
  766. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  767. struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
  768. unsigned long size = isc->fmt.fmt.pix.sizeimage;
  769. if (vb2_plane_size(vb, 0) < size) {
  770. v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
  771. vb2_plane_size(vb, 0), size);
  772. return -EINVAL;
  773. }
  774. vb2_set_plane_payload(vb, 0, size);
  775. vbuf->field = isc->fmt.fmt.pix.field;
  776. return 0;
  777. }
  778. static inline bool sensor_is_preferred(const struct isc_format *isc_fmt)
  779. {
  780. return (sensor_preferred && isc_fmt->sd_support) ||
  781. !isc_fmt->isc_support;
  782. }
  783. static struct fmt_config *get_fmt_config(u32 fourcc)
  784. {
  785. struct fmt_config *config;
  786. int i;
  787. config = &fmt_configs_list[0];
  788. for (i = 0; i < ARRAY_SIZE(fmt_configs_list); i++) {
  789. if (config->fourcc == fourcc)
  790. return config;
  791. config++;
  792. }
  793. return NULL;
  794. }
  795. static void isc_start_dma(struct isc_device *isc)
  796. {
  797. struct regmap *regmap = isc->regmap;
  798. struct v4l2_pix_format *pixfmt = &isc->fmt.fmt.pix;
  799. u32 sizeimage = pixfmt->sizeimage;
  800. struct fmt_config *config = get_fmt_config(isc->current_fmt->fourcc);
  801. u32 dctrl_dview;
  802. dma_addr_t addr0;
  803. addr0 = vb2_dma_contig_plane_dma_addr(&isc->cur_frm->vb.vb2_buf, 0);
  804. regmap_write(regmap, ISC_DAD0, addr0);
  805. switch (pixfmt->pixelformat) {
  806. case V4L2_PIX_FMT_YUV420:
  807. regmap_write(regmap, ISC_DAD1, addr0 + (sizeimage * 2) / 3);
  808. regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 5) / 6);
  809. break;
  810. case V4L2_PIX_FMT_YUV422P:
  811. regmap_write(regmap, ISC_DAD1, addr0 + sizeimage / 2);
  812. regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 3) / 4);
  813. break;
  814. default:
  815. break;
  816. }
  817. if (sensor_is_preferred(isc->current_fmt))
  818. dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
  819. else
  820. dctrl_dview = config->dctrl_dview;
  821. regmap_write(regmap, ISC_DCTRL, dctrl_dview | ISC_DCTRL_IE_IS);
  822. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
  823. }
  824. static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
  825. {
  826. struct regmap *regmap = isc->regmap;
  827. struct isc_ctrls *ctrls = &isc->ctrls;
  828. struct fmt_config *config = get_fmt_config(isc->raw_fmt->fourcc);
  829. u32 val, bay_cfg;
  830. const u32 *gamma;
  831. unsigned int i;
  832. /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
  833. for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
  834. val = pipeline & BIT(i) ? 1 : 0;
  835. regmap_field_write(isc->pipeline[i], val);
  836. }
  837. if (!pipeline)
  838. return;
  839. bay_cfg = config->cfa_baycfg;
  840. regmap_write(regmap, ISC_WB_CFG, bay_cfg);
  841. regmap_write(regmap, ISC_WB_O_RGR, 0x0);
  842. regmap_write(regmap, ISC_WB_O_BGR, 0x0);
  843. regmap_write(regmap, ISC_WB_G_RGR, ctrls->r_gain | (0x1 << 25));
  844. regmap_write(regmap, ISC_WB_G_BGR, ctrls->b_gain | (0x1 << 25));
  845. regmap_write(regmap, ISC_CFA_CFG, bay_cfg | ISC_CFA_CFG_EITPOL);
  846. gamma = &isc_gamma_table[ctrls->gamma_index][0];
  847. regmap_bulk_write(regmap, ISC_GAM_BENTRY, gamma, GAMMA_ENTRIES);
  848. regmap_bulk_write(regmap, ISC_GAM_GENTRY, gamma, GAMMA_ENTRIES);
  849. regmap_bulk_write(regmap, ISC_GAM_RENTRY, gamma, GAMMA_ENTRIES);
  850. /* Convert RGB to YUV */
  851. regmap_write(regmap, ISC_CSC_YR_YG, 0x42 | (0x81 << 16));
  852. regmap_write(regmap, ISC_CSC_YB_OY, 0x19 | (0x10 << 16));
  853. regmap_write(regmap, ISC_CSC_CBR_CBG, 0xFDA | (0xFB6 << 16));
  854. regmap_write(regmap, ISC_CSC_CBB_OCB, 0x70 | (0x80 << 16));
  855. regmap_write(regmap, ISC_CSC_CRR_CRG, 0x70 | (0xFA2 << 16));
  856. regmap_write(regmap, ISC_CSC_CRB_OCR, 0xFEE | (0x80 << 16));
  857. regmap_write(regmap, ISC_CBC_BRIGHT, ctrls->brightness);
  858. regmap_write(regmap, ISC_CBC_CONTRAST, ctrls->contrast);
  859. }
  860. static int isc_update_profile(struct isc_device *isc)
  861. {
  862. struct regmap *regmap = isc->regmap;
  863. u32 sr;
  864. int counter = 100;
  865. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
  866. regmap_read(regmap, ISC_CTRLSR, &sr);
  867. while ((sr & ISC_CTRL_UPPRO) && counter--) {
  868. usleep_range(1000, 2000);
  869. regmap_read(regmap, ISC_CTRLSR, &sr);
  870. }
  871. if (counter < 0) {
  872. v4l2_warn(&isc->v4l2_dev, "Time out to update profie\n");
  873. return -ETIMEDOUT;
  874. }
  875. return 0;
  876. }
  877. static void isc_set_histogram(struct isc_device *isc)
  878. {
  879. struct regmap *regmap = isc->regmap;
  880. struct isc_ctrls *ctrls = &isc->ctrls;
  881. struct fmt_config *config = get_fmt_config(isc->raw_fmt->fourcc);
  882. if (ctrls->awb && (ctrls->hist_stat != HIST_ENABLED)) {
  883. regmap_write(regmap, ISC_HIS_CFG,
  884. ISC_HIS_CFG_MODE_R |
  885. (config->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT) |
  886. ISC_HIS_CFG_RAR);
  887. regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_EN);
  888. regmap_write(regmap, ISC_INTEN, ISC_INT_HISDONE);
  889. ctrls->hist_id = ISC_HIS_CFG_MODE_R;
  890. isc_update_profile(isc);
  891. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
  892. ctrls->hist_stat = HIST_ENABLED;
  893. } else if (!ctrls->awb && (ctrls->hist_stat != HIST_DISABLED)) {
  894. regmap_write(regmap, ISC_INTDIS, ISC_INT_HISDONE);
  895. regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_DIS);
  896. ctrls->hist_stat = HIST_DISABLED;
  897. }
  898. }
  899. static inline void isc_get_param(const struct isc_format *fmt,
  900. u32 *rlp_mode, u32 *dcfg)
  901. {
  902. struct fmt_config *config = get_fmt_config(fmt->fourcc);
  903. *dcfg = ISC_DCFG_YMBSIZE_BEATS8;
  904. switch (fmt->fourcc) {
  905. case V4L2_PIX_FMT_SBGGR10:
  906. case V4L2_PIX_FMT_SGBRG10:
  907. case V4L2_PIX_FMT_SGRBG10:
  908. case V4L2_PIX_FMT_SRGGB10:
  909. case V4L2_PIX_FMT_SBGGR12:
  910. case V4L2_PIX_FMT_SGBRG12:
  911. case V4L2_PIX_FMT_SGRBG12:
  912. case V4L2_PIX_FMT_SRGGB12:
  913. *rlp_mode = config->rlp_cfg_mode;
  914. *dcfg |= config->dcfg_imode;
  915. break;
  916. default:
  917. *rlp_mode = ISC_RLP_CFG_MODE_DAT8;
  918. *dcfg |= ISC_DCFG_IMODE_PACKED8;
  919. break;
  920. }
  921. }
  922. static int isc_configure(struct isc_device *isc)
  923. {
  924. struct regmap *regmap = isc->regmap;
  925. const struct isc_format *current_fmt = isc->current_fmt;
  926. struct fmt_config *curfmt_config = get_fmt_config(current_fmt->fourcc);
  927. struct fmt_config *rawfmt_config = get_fmt_config(isc->raw_fmt->fourcc);
  928. struct isc_subdev_entity *subdev = isc->current_subdev;
  929. u32 pfe_cfg0, rlp_mode, dcfg, mask, pipeline;
  930. if (sensor_is_preferred(current_fmt)) {
  931. pfe_cfg0 = curfmt_config->pfe_cfg0_bps;
  932. pipeline = 0x0;
  933. isc_get_param(current_fmt, &rlp_mode, &dcfg);
  934. isc->ctrls.hist_stat = HIST_INIT;
  935. } else {
  936. pfe_cfg0 = rawfmt_config->pfe_cfg0_bps;
  937. pipeline = curfmt_config->bits_pipeline;
  938. rlp_mode = curfmt_config->rlp_cfg_mode;
  939. dcfg = curfmt_config->dcfg_imode |
  940. ISC_DCFG_YMBSIZE_BEATS8 | ISC_DCFG_CMBSIZE_BEATS8;
  941. }
  942. pfe_cfg0 |= subdev->pfe_cfg0 | ISC_PFE_CFG0_MODE_PROGRESSIVE;
  943. mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
  944. ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
  945. ISC_PFE_CFG0_MODE_MASK;
  946. regmap_update_bits(regmap, ISC_PFE_CFG0, mask, pfe_cfg0);
  947. regmap_update_bits(regmap, ISC_RLP_CFG, ISC_RLP_CFG_MODE_MASK,
  948. rlp_mode);
  949. regmap_write(regmap, ISC_DCFG, dcfg);
  950. /* Set the pipeline */
  951. isc_set_pipeline(isc, pipeline);
  952. if (pipeline)
  953. isc_set_histogram(isc);
  954. /* Update profile */
  955. return isc_update_profile(isc);
  956. }
  957. static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
  958. {
  959. struct isc_device *isc = vb2_get_drv_priv(vq);
  960. struct regmap *regmap = isc->regmap;
  961. struct isc_buffer *buf;
  962. unsigned long flags;
  963. int ret;
  964. /* Enable stream on the sub device */
  965. ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
  966. if (ret && ret != -ENOIOCTLCMD) {
  967. v4l2_err(&isc->v4l2_dev, "stream on failed in subdev\n");
  968. goto err_start_stream;
  969. }
  970. pm_runtime_get_sync(isc->dev);
  971. ret = isc_configure(isc);
  972. if (unlikely(ret))
  973. goto err_configure;
  974. /* Enable DMA interrupt */
  975. regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
  976. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  977. isc->sequence = 0;
  978. isc->stop = false;
  979. reinit_completion(&isc->comp);
  980. isc->cur_frm = list_first_entry(&isc->dma_queue,
  981. struct isc_buffer, list);
  982. list_del(&isc->cur_frm->list);
  983. isc_start_dma(isc);
  984. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  985. return 0;
  986. err_configure:
  987. pm_runtime_put_sync(isc->dev);
  988. v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
  989. err_start_stream:
  990. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  991. list_for_each_entry(buf, &isc->dma_queue, list)
  992. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  993. INIT_LIST_HEAD(&isc->dma_queue);
  994. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  995. return ret;
  996. }
  997. static void isc_stop_streaming(struct vb2_queue *vq)
  998. {
  999. struct isc_device *isc = vb2_get_drv_priv(vq);
  1000. unsigned long flags;
  1001. struct isc_buffer *buf;
  1002. int ret;
  1003. isc->stop = true;
  1004. /* Wait until the end of the current frame */
  1005. if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
  1006. v4l2_err(&isc->v4l2_dev,
  1007. "Timeout waiting for end of the capture\n");
  1008. /* Disable DMA interrupt */
  1009. regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
  1010. pm_runtime_put_sync(isc->dev);
  1011. /* Disable stream on the sub device */
  1012. ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
  1013. if (ret && ret != -ENOIOCTLCMD)
  1014. v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
  1015. /* Release all active buffers */
  1016. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  1017. if (unlikely(isc->cur_frm)) {
  1018. vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
  1019. VB2_BUF_STATE_ERROR);
  1020. isc->cur_frm = NULL;
  1021. }
  1022. list_for_each_entry(buf, &isc->dma_queue, list)
  1023. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1024. INIT_LIST_HEAD(&isc->dma_queue);
  1025. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  1026. }
  1027. static void isc_buffer_queue(struct vb2_buffer *vb)
  1028. {
  1029. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1030. struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
  1031. struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
  1032. unsigned long flags;
  1033. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  1034. if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
  1035. vb2_is_streaming(vb->vb2_queue)) {
  1036. isc->cur_frm = buf;
  1037. isc_start_dma(isc);
  1038. } else
  1039. list_add_tail(&buf->list, &isc->dma_queue);
  1040. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  1041. }
  1042. static const struct vb2_ops isc_vb2_ops = {
  1043. .queue_setup = isc_queue_setup,
  1044. .wait_prepare = vb2_ops_wait_prepare,
  1045. .wait_finish = vb2_ops_wait_finish,
  1046. .buf_prepare = isc_buffer_prepare,
  1047. .start_streaming = isc_start_streaming,
  1048. .stop_streaming = isc_stop_streaming,
  1049. .buf_queue = isc_buffer_queue,
  1050. };
  1051. static int isc_querycap(struct file *file, void *priv,
  1052. struct v4l2_capability *cap)
  1053. {
  1054. struct isc_device *isc = video_drvdata(file);
  1055. strcpy(cap->driver, ATMEL_ISC_NAME);
  1056. strcpy(cap->card, "Atmel Image Sensor Controller");
  1057. snprintf(cap->bus_info, sizeof(cap->bus_info),
  1058. "platform:%s", isc->v4l2_dev.name);
  1059. return 0;
  1060. }
  1061. static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
  1062. struct v4l2_fmtdesc *f)
  1063. {
  1064. struct isc_device *isc = video_drvdata(file);
  1065. u32 index = f->index;
  1066. if (index >= isc->num_user_formats)
  1067. return -EINVAL;
  1068. f->pixelformat = isc->user_formats[index]->fourcc;
  1069. return 0;
  1070. }
  1071. static int isc_g_fmt_vid_cap(struct file *file, void *priv,
  1072. struct v4l2_format *fmt)
  1073. {
  1074. struct isc_device *isc = video_drvdata(file);
  1075. *fmt = isc->fmt;
  1076. return 0;
  1077. }
  1078. static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
  1079. unsigned int fourcc)
  1080. {
  1081. unsigned int num_formats = isc->num_user_formats;
  1082. struct isc_format *fmt;
  1083. unsigned int i;
  1084. for (i = 0; i < num_formats; i++) {
  1085. fmt = isc->user_formats[i];
  1086. if (fmt->fourcc == fourcc)
  1087. return fmt;
  1088. }
  1089. return NULL;
  1090. }
  1091. static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
  1092. struct isc_format **current_fmt, u32 *code)
  1093. {
  1094. struct isc_format *isc_fmt;
  1095. struct v4l2_pix_format *pixfmt = &f->fmt.pix;
  1096. struct v4l2_subdev_pad_config pad_cfg;
  1097. struct v4l2_subdev_format format = {
  1098. .which = V4L2_SUBDEV_FORMAT_TRY,
  1099. };
  1100. u32 mbus_code;
  1101. int ret;
  1102. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1103. return -EINVAL;
  1104. isc_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
  1105. if (!isc_fmt) {
  1106. v4l2_warn(&isc->v4l2_dev, "Format 0x%x not found\n",
  1107. pixfmt->pixelformat);
  1108. isc_fmt = isc->user_formats[isc->num_user_formats - 1];
  1109. pixfmt->pixelformat = isc_fmt->fourcc;
  1110. }
  1111. /* Limit to Atmel ISC hardware capabilities */
  1112. if (pixfmt->width > ISC_MAX_SUPPORT_WIDTH)
  1113. pixfmt->width = ISC_MAX_SUPPORT_WIDTH;
  1114. if (pixfmt->height > ISC_MAX_SUPPORT_HEIGHT)
  1115. pixfmt->height = ISC_MAX_SUPPORT_HEIGHT;
  1116. if (sensor_is_preferred(isc_fmt))
  1117. mbus_code = isc_fmt->mbus_code;
  1118. else
  1119. mbus_code = isc->raw_fmt->mbus_code;
  1120. v4l2_fill_mbus_format(&format.format, pixfmt, mbus_code);
  1121. ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
  1122. &pad_cfg, &format);
  1123. if (ret < 0)
  1124. return ret;
  1125. v4l2_fill_pix_format(pixfmt, &format.format);
  1126. pixfmt->field = V4L2_FIELD_NONE;
  1127. pixfmt->bytesperline = (pixfmt->width * isc_fmt->bpp) >> 3;
  1128. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  1129. if (current_fmt)
  1130. *current_fmt = isc_fmt;
  1131. if (code)
  1132. *code = mbus_code;
  1133. return 0;
  1134. }
  1135. static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
  1136. {
  1137. struct v4l2_subdev_format format = {
  1138. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1139. };
  1140. struct isc_format *current_fmt;
  1141. u32 mbus_code;
  1142. int ret;
  1143. ret = isc_try_fmt(isc, f, &current_fmt, &mbus_code);
  1144. if (ret)
  1145. return ret;
  1146. v4l2_fill_mbus_format(&format.format, &f->fmt.pix, mbus_code);
  1147. ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
  1148. set_fmt, NULL, &format);
  1149. if (ret < 0)
  1150. return ret;
  1151. isc->fmt = *f;
  1152. isc->current_fmt = current_fmt;
  1153. return 0;
  1154. }
  1155. static int isc_s_fmt_vid_cap(struct file *file, void *priv,
  1156. struct v4l2_format *f)
  1157. {
  1158. struct isc_device *isc = video_drvdata(file);
  1159. if (vb2_is_streaming(&isc->vb2_vidq))
  1160. return -EBUSY;
  1161. return isc_set_fmt(isc, f);
  1162. }
  1163. static int isc_try_fmt_vid_cap(struct file *file, void *priv,
  1164. struct v4l2_format *f)
  1165. {
  1166. struct isc_device *isc = video_drvdata(file);
  1167. return isc_try_fmt(isc, f, NULL, NULL);
  1168. }
  1169. static int isc_enum_input(struct file *file, void *priv,
  1170. struct v4l2_input *inp)
  1171. {
  1172. if (inp->index != 0)
  1173. return -EINVAL;
  1174. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1175. inp->std = 0;
  1176. strcpy(inp->name, "Camera");
  1177. return 0;
  1178. }
  1179. static int isc_g_input(struct file *file, void *priv, unsigned int *i)
  1180. {
  1181. *i = 0;
  1182. return 0;
  1183. }
  1184. static int isc_s_input(struct file *file, void *priv, unsigned int i)
  1185. {
  1186. if (i > 0)
  1187. return -EINVAL;
  1188. return 0;
  1189. }
  1190. static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1191. {
  1192. struct isc_device *isc = video_drvdata(file);
  1193. return v4l2_g_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
  1194. }
  1195. static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1196. {
  1197. struct isc_device *isc = video_drvdata(file);
  1198. return v4l2_s_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
  1199. }
  1200. static int isc_enum_framesizes(struct file *file, void *fh,
  1201. struct v4l2_frmsizeenum *fsize)
  1202. {
  1203. struct isc_device *isc = video_drvdata(file);
  1204. const struct isc_format *isc_fmt;
  1205. struct v4l2_subdev_frame_size_enum fse = {
  1206. .index = fsize->index,
  1207. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1208. };
  1209. int ret;
  1210. isc_fmt = find_format_by_fourcc(isc, fsize->pixel_format);
  1211. if (!isc_fmt)
  1212. return -EINVAL;
  1213. if (sensor_is_preferred(isc_fmt))
  1214. fse.code = isc_fmt->mbus_code;
  1215. else
  1216. fse.code = isc->raw_fmt->mbus_code;
  1217. ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
  1218. NULL, &fse);
  1219. if (ret)
  1220. return ret;
  1221. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1222. fsize->discrete.width = fse.max_width;
  1223. fsize->discrete.height = fse.max_height;
  1224. return 0;
  1225. }
  1226. static int isc_enum_frameintervals(struct file *file, void *fh,
  1227. struct v4l2_frmivalenum *fival)
  1228. {
  1229. struct isc_device *isc = video_drvdata(file);
  1230. const struct isc_format *isc_fmt;
  1231. struct v4l2_subdev_frame_interval_enum fie = {
  1232. .index = fival->index,
  1233. .width = fival->width,
  1234. .height = fival->height,
  1235. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1236. };
  1237. int ret;
  1238. isc_fmt = find_format_by_fourcc(isc, fival->pixel_format);
  1239. if (!isc_fmt)
  1240. return -EINVAL;
  1241. if (sensor_is_preferred(isc_fmt))
  1242. fie.code = isc_fmt->mbus_code;
  1243. else
  1244. fie.code = isc->raw_fmt->mbus_code;
  1245. ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
  1246. enum_frame_interval, NULL, &fie);
  1247. if (ret)
  1248. return ret;
  1249. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1250. fival->discrete = fie.interval;
  1251. return 0;
  1252. }
  1253. static const struct v4l2_ioctl_ops isc_ioctl_ops = {
  1254. .vidioc_querycap = isc_querycap,
  1255. .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
  1256. .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
  1257. .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
  1258. .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
  1259. .vidioc_enum_input = isc_enum_input,
  1260. .vidioc_g_input = isc_g_input,
  1261. .vidioc_s_input = isc_s_input,
  1262. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1263. .vidioc_querybuf = vb2_ioctl_querybuf,
  1264. .vidioc_qbuf = vb2_ioctl_qbuf,
  1265. .vidioc_expbuf = vb2_ioctl_expbuf,
  1266. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1267. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1268. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1269. .vidioc_streamon = vb2_ioctl_streamon,
  1270. .vidioc_streamoff = vb2_ioctl_streamoff,
  1271. .vidioc_g_parm = isc_g_parm,
  1272. .vidioc_s_parm = isc_s_parm,
  1273. .vidioc_enum_framesizes = isc_enum_framesizes,
  1274. .vidioc_enum_frameintervals = isc_enum_frameintervals,
  1275. .vidioc_log_status = v4l2_ctrl_log_status,
  1276. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1277. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1278. };
  1279. static int isc_open(struct file *file)
  1280. {
  1281. struct isc_device *isc = video_drvdata(file);
  1282. struct v4l2_subdev *sd = isc->current_subdev->sd;
  1283. int ret;
  1284. if (mutex_lock_interruptible(&isc->lock))
  1285. return -ERESTARTSYS;
  1286. ret = v4l2_fh_open(file);
  1287. if (ret < 0)
  1288. goto unlock;
  1289. if (!v4l2_fh_is_singular_file(file))
  1290. goto unlock;
  1291. ret = v4l2_subdev_call(sd, core, s_power, 1);
  1292. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1293. v4l2_fh_release(file);
  1294. goto unlock;
  1295. }
  1296. ret = isc_set_fmt(isc, &isc->fmt);
  1297. if (ret) {
  1298. v4l2_subdev_call(sd, core, s_power, 0);
  1299. v4l2_fh_release(file);
  1300. }
  1301. unlock:
  1302. mutex_unlock(&isc->lock);
  1303. return ret;
  1304. }
  1305. static int isc_release(struct file *file)
  1306. {
  1307. struct isc_device *isc = video_drvdata(file);
  1308. struct v4l2_subdev *sd = isc->current_subdev->sd;
  1309. bool fh_singular;
  1310. int ret;
  1311. mutex_lock(&isc->lock);
  1312. fh_singular = v4l2_fh_is_singular_file(file);
  1313. ret = _vb2_fop_release(file, NULL);
  1314. if (fh_singular)
  1315. v4l2_subdev_call(sd, core, s_power, 0);
  1316. mutex_unlock(&isc->lock);
  1317. return ret;
  1318. }
  1319. static const struct v4l2_file_operations isc_fops = {
  1320. .owner = THIS_MODULE,
  1321. .open = isc_open,
  1322. .release = isc_release,
  1323. .unlocked_ioctl = video_ioctl2,
  1324. .read = vb2_fop_read,
  1325. .mmap = vb2_fop_mmap,
  1326. .poll = vb2_fop_poll,
  1327. };
  1328. static irqreturn_t isc_interrupt(int irq, void *dev_id)
  1329. {
  1330. struct isc_device *isc = (struct isc_device *)dev_id;
  1331. struct regmap *regmap = isc->regmap;
  1332. u32 isc_intsr, isc_intmask, pending;
  1333. irqreturn_t ret = IRQ_NONE;
  1334. regmap_read(regmap, ISC_INTSR, &isc_intsr);
  1335. regmap_read(regmap, ISC_INTMASK, &isc_intmask);
  1336. pending = isc_intsr & isc_intmask;
  1337. if (likely(pending & ISC_INT_DDONE)) {
  1338. spin_lock(&isc->dma_queue_lock);
  1339. if (isc->cur_frm) {
  1340. struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
  1341. struct vb2_buffer *vb = &vbuf->vb2_buf;
  1342. vb->timestamp = ktime_get_ns();
  1343. vbuf->sequence = isc->sequence++;
  1344. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  1345. isc->cur_frm = NULL;
  1346. }
  1347. if (!list_empty(&isc->dma_queue) && !isc->stop) {
  1348. isc->cur_frm = list_first_entry(&isc->dma_queue,
  1349. struct isc_buffer, list);
  1350. list_del(&isc->cur_frm->list);
  1351. isc_start_dma(isc);
  1352. }
  1353. if (isc->stop)
  1354. complete(&isc->comp);
  1355. ret = IRQ_HANDLED;
  1356. spin_unlock(&isc->dma_queue_lock);
  1357. }
  1358. if (pending & ISC_INT_HISDONE) {
  1359. schedule_work(&isc->awb_work);
  1360. ret = IRQ_HANDLED;
  1361. }
  1362. return ret;
  1363. }
  1364. static void isc_hist_count(struct isc_device *isc)
  1365. {
  1366. struct regmap *regmap = isc->regmap;
  1367. struct isc_ctrls *ctrls = &isc->ctrls;
  1368. u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
  1369. u32 *hist_entry = &ctrls->hist_entry[0];
  1370. u32 i;
  1371. regmap_bulk_read(regmap, ISC_HIS_ENTRY, hist_entry, HIST_ENTRIES);
  1372. *hist_count = 0;
  1373. for (i = 0; i < HIST_ENTRIES; i++)
  1374. *hist_count += i * (*hist_entry++);
  1375. }
  1376. static void isc_wb_update(struct isc_ctrls *ctrls)
  1377. {
  1378. u32 *hist_count = &ctrls->hist_count[0];
  1379. u64 g_count = (u64)hist_count[ISC_HIS_CFG_MODE_GB] << 9;
  1380. u32 hist_r = hist_count[ISC_HIS_CFG_MODE_R];
  1381. u32 hist_b = hist_count[ISC_HIS_CFG_MODE_B];
  1382. if (hist_r)
  1383. ctrls->r_gain = div_u64(g_count, hist_r);
  1384. if (hist_b)
  1385. ctrls->b_gain = div_u64(g_count, hist_b);
  1386. }
  1387. static void isc_awb_work(struct work_struct *w)
  1388. {
  1389. struct isc_device *isc =
  1390. container_of(w, struct isc_device, awb_work);
  1391. struct regmap *regmap = isc->regmap;
  1392. struct fmt_config *config = get_fmt_config(isc->raw_fmt->fourcc);
  1393. struct isc_ctrls *ctrls = &isc->ctrls;
  1394. u32 hist_id = ctrls->hist_id;
  1395. u32 baysel;
  1396. if (ctrls->hist_stat != HIST_ENABLED)
  1397. return;
  1398. isc_hist_count(isc);
  1399. if (hist_id != ISC_HIS_CFG_MODE_B) {
  1400. hist_id++;
  1401. } else {
  1402. isc_wb_update(ctrls);
  1403. hist_id = ISC_HIS_CFG_MODE_R;
  1404. }
  1405. ctrls->hist_id = hist_id;
  1406. baysel = config->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT;
  1407. pm_runtime_get_sync(isc->dev);
  1408. regmap_write(regmap, ISC_HIS_CFG, hist_id | baysel | ISC_HIS_CFG_RAR);
  1409. isc_update_profile(isc);
  1410. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
  1411. pm_runtime_put_sync(isc->dev);
  1412. }
  1413. static int isc_s_ctrl(struct v4l2_ctrl *ctrl)
  1414. {
  1415. struct isc_device *isc = container_of(ctrl->handler,
  1416. struct isc_device, ctrls.handler);
  1417. struct isc_ctrls *ctrls = &isc->ctrls;
  1418. switch (ctrl->id) {
  1419. case V4L2_CID_BRIGHTNESS:
  1420. ctrls->brightness = ctrl->val & ISC_CBC_BRIGHT_MASK;
  1421. break;
  1422. case V4L2_CID_CONTRAST:
  1423. ctrls->contrast = ctrl->val & ISC_CBC_CONTRAST_MASK;
  1424. break;
  1425. case V4L2_CID_GAMMA:
  1426. ctrls->gamma_index = ctrl->val;
  1427. break;
  1428. case V4L2_CID_AUTO_WHITE_BALANCE:
  1429. ctrls->awb = ctrl->val;
  1430. if (ctrls->hist_stat != HIST_ENABLED) {
  1431. ctrls->r_gain = 0x1 << 9;
  1432. ctrls->b_gain = 0x1 << 9;
  1433. }
  1434. break;
  1435. default:
  1436. return -EINVAL;
  1437. }
  1438. return 0;
  1439. }
  1440. static const struct v4l2_ctrl_ops isc_ctrl_ops = {
  1441. .s_ctrl = isc_s_ctrl,
  1442. };
  1443. static int isc_ctrl_init(struct isc_device *isc)
  1444. {
  1445. const struct v4l2_ctrl_ops *ops = &isc_ctrl_ops;
  1446. struct isc_ctrls *ctrls = &isc->ctrls;
  1447. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  1448. int ret;
  1449. ctrls->hist_stat = HIST_INIT;
  1450. ret = v4l2_ctrl_handler_init(hdl, 4);
  1451. if (ret < 0)
  1452. return ret;
  1453. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -1024, 1023, 1, 0);
  1454. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -2048, 2047, 1, 256);
  1455. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAMMA, 0, GAMMA_MAX, 1, 2);
  1456. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  1457. v4l2_ctrl_handler_setup(hdl);
  1458. return 0;
  1459. }
  1460. static int isc_async_bound(struct v4l2_async_notifier *notifier,
  1461. struct v4l2_subdev *subdev,
  1462. struct v4l2_async_subdev *asd)
  1463. {
  1464. struct isc_device *isc = container_of(notifier->v4l2_dev,
  1465. struct isc_device, v4l2_dev);
  1466. struct isc_subdev_entity *subdev_entity =
  1467. container_of(notifier, struct isc_subdev_entity, notifier);
  1468. if (video_is_registered(&isc->video_dev)) {
  1469. v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
  1470. return -EBUSY;
  1471. }
  1472. subdev_entity->sd = subdev;
  1473. return 0;
  1474. }
  1475. static void isc_async_unbind(struct v4l2_async_notifier *notifier,
  1476. struct v4l2_subdev *subdev,
  1477. struct v4l2_async_subdev *asd)
  1478. {
  1479. struct isc_device *isc = container_of(notifier->v4l2_dev,
  1480. struct isc_device, v4l2_dev);
  1481. cancel_work_sync(&isc->awb_work);
  1482. video_unregister_device(&isc->video_dev);
  1483. v4l2_ctrl_handler_free(&isc->ctrls.handler);
  1484. }
  1485. static struct isc_format *find_format_by_code(unsigned int code, int *index)
  1486. {
  1487. struct isc_format *fmt = &formats_list[0];
  1488. unsigned int i;
  1489. for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
  1490. if (fmt->mbus_code == code) {
  1491. *index = i;
  1492. return fmt;
  1493. }
  1494. fmt++;
  1495. }
  1496. return NULL;
  1497. }
  1498. static int isc_formats_init(struct isc_device *isc)
  1499. {
  1500. struct isc_format *fmt;
  1501. struct v4l2_subdev *subdev = isc->current_subdev->sd;
  1502. unsigned int num_fmts, i, j;
  1503. u32 list_size = ARRAY_SIZE(formats_list);
  1504. struct v4l2_subdev_mbus_code_enum mbus_code = {
  1505. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1506. };
  1507. while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
  1508. NULL, &mbus_code)) {
  1509. mbus_code.index++;
  1510. fmt = find_format_by_code(mbus_code.code, &i);
  1511. if ((!fmt) || (!(fmt->flags & FMT_FLAG_FROM_SENSOR)))
  1512. continue;
  1513. fmt->sd_support = true;
  1514. if (fmt->flags & FMT_FLAG_RAW_FORMAT)
  1515. isc->raw_fmt = fmt;
  1516. }
  1517. fmt = &formats_list[0];
  1518. for (i = 0; i < list_size; i++) {
  1519. if (fmt->flags & FMT_FLAG_FROM_CONTROLLER)
  1520. fmt->isc_support = true;
  1521. fmt++;
  1522. }
  1523. fmt = &formats_list[0];
  1524. num_fmts = 0;
  1525. for (i = 0; i < list_size; i++) {
  1526. if (fmt->isc_support || fmt->sd_support)
  1527. num_fmts++;
  1528. fmt++;
  1529. }
  1530. if (!num_fmts)
  1531. return -ENXIO;
  1532. isc->num_user_formats = num_fmts;
  1533. isc->user_formats = devm_kcalloc(isc->dev,
  1534. num_fmts, sizeof(*isc->user_formats),
  1535. GFP_KERNEL);
  1536. if (!isc->user_formats)
  1537. return -ENOMEM;
  1538. fmt = &formats_list[0];
  1539. for (i = 0, j = 0; i < list_size; i++) {
  1540. if (fmt->isc_support || fmt->sd_support)
  1541. isc->user_formats[j++] = fmt;
  1542. fmt++;
  1543. }
  1544. return 0;
  1545. }
  1546. static int isc_set_default_fmt(struct isc_device *isc)
  1547. {
  1548. struct v4l2_format f = {
  1549. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1550. .fmt.pix = {
  1551. .width = VGA_WIDTH,
  1552. .height = VGA_HEIGHT,
  1553. .field = V4L2_FIELD_NONE,
  1554. .pixelformat = isc->user_formats[0]->fourcc,
  1555. },
  1556. };
  1557. int ret;
  1558. ret = isc_try_fmt(isc, &f, NULL, NULL);
  1559. if (ret)
  1560. return ret;
  1561. isc->current_fmt = isc->user_formats[0];
  1562. isc->fmt = f;
  1563. return 0;
  1564. }
  1565. static int isc_async_complete(struct v4l2_async_notifier *notifier)
  1566. {
  1567. struct isc_device *isc = container_of(notifier->v4l2_dev,
  1568. struct isc_device, v4l2_dev);
  1569. struct video_device *vdev = &isc->video_dev;
  1570. struct vb2_queue *q = &isc->vb2_vidq;
  1571. int ret;
  1572. INIT_WORK(&isc->awb_work, isc_awb_work);
  1573. ret = v4l2_device_register_subdev_nodes(&isc->v4l2_dev);
  1574. if (ret < 0) {
  1575. v4l2_err(&isc->v4l2_dev, "Failed to register subdev nodes\n");
  1576. return ret;
  1577. }
  1578. isc->current_subdev = container_of(notifier,
  1579. struct isc_subdev_entity, notifier);
  1580. mutex_init(&isc->lock);
  1581. init_completion(&isc->comp);
  1582. /* Initialize videobuf2 queue */
  1583. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1584. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  1585. q->drv_priv = isc;
  1586. q->buf_struct_size = sizeof(struct isc_buffer);
  1587. q->ops = &isc_vb2_ops;
  1588. q->mem_ops = &vb2_dma_contig_memops;
  1589. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1590. q->lock = &isc->lock;
  1591. q->min_buffers_needed = 1;
  1592. q->dev = isc->dev;
  1593. ret = vb2_queue_init(q);
  1594. if (ret < 0) {
  1595. v4l2_err(&isc->v4l2_dev,
  1596. "vb2_queue_init() failed: %d\n", ret);
  1597. return ret;
  1598. }
  1599. /* Init video dma queues */
  1600. INIT_LIST_HEAD(&isc->dma_queue);
  1601. spin_lock_init(&isc->dma_queue_lock);
  1602. ret = isc_formats_init(isc);
  1603. if (ret < 0) {
  1604. v4l2_err(&isc->v4l2_dev,
  1605. "Init format failed: %d\n", ret);
  1606. return ret;
  1607. }
  1608. ret = isc_set_default_fmt(isc);
  1609. if (ret) {
  1610. v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
  1611. return ret;
  1612. }
  1613. ret = isc_ctrl_init(isc);
  1614. if (ret) {
  1615. v4l2_err(&isc->v4l2_dev, "Init isc ctrols failed: %d\n", ret);
  1616. return ret;
  1617. }
  1618. /* Register video device */
  1619. strlcpy(vdev->name, ATMEL_ISC_NAME, sizeof(vdev->name));
  1620. vdev->release = video_device_release_empty;
  1621. vdev->fops = &isc_fops;
  1622. vdev->ioctl_ops = &isc_ioctl_ops;
  1623. vdev->v4l2_dev = &isc->v4l2_dev;
  1624. vdev->vfl_dir = VFL_DIR_RX;
  1625. vdev->queue = q;
  1626. vdev->lock = &isc->lock;
  1627. vdev->ctrl_handler = &isc->ctrls.handler;
  1628. vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
  1629. video_set_drvdata(vdev, isc);
  1630. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1631. if (ret < 0) {
  1632. v4l2_err(&isc->v4l2_dev,
  1633. "video_register_device failed: %d\n", ret);
  1634. return ret;
  1635. }
  1636. return 0;
  1637. }
  1638. static const struct v4l2_async_notifier_operations isc_async_ops = {
  1639. .bound = isc_async_bound,
  1640. .unbind = isc_async_unbind,
  1641. .complete = isc_async_complete,
  1642. };
  1643. static void isc_subdev_cleanup(struct isc_device *isc)
  1644. {
  1645. struct isc_subdev_entity *subdev_entity;
  1646. list_for_each_entry(subdev_entity, &isc->subdev_entities, list)
  1647. v4l2_async_notifier_unregister(&subdev_entity->notifier);
  1648. INIT_LIST_HEAD(&isc->subdev_entities);
  1649. }
  1650. static int isc_pipeline_init(struct isc_device *isc)
  1651. {
  1652. struct device *dev = isc->dev;
  1653. struct regmap *regmap = isc->regmap;
  1654. struct regmap_field *regs;
  1655. unsigned int i;
  1656. /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
  1657. const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
  1658. REG_FIELD(ISC_WB_CTRL, 0, 0),
  1659. REG_FIELD(ISC_CFA_CTRL, 0, 0),
  1660. REG_FIELD(ISC_CC_CTRL, 0, 0),
  1661. REG_FIELD(ISC_GAM_CTRL, 0, 0),
  1662. REG_FIELD(ISC_GAM_CTRL, 1, 1),
  1663. REG_FIELD(ISC_GAM_CTRL, 2, 2),
  1664. REG_FIELD(ISC_GAM_CTRL, 3, 3),
  1665. REG_FIELD(ISC_CSC_CTRL, 0, 0),
  1666. REG_FIELD(ISC_CBC_CTRL, 0, 0),
  1667. REG_FIELD(ISC_SUB422_CTRL, 0, 0),
  1668. REG_FIELD(ISC_SUB420_CTRL, 0, 0),
  1669. };
  1670. for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
  1671. regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
  1672. if (IS_ERR(regs))
  1673. return PTR_ERR(regs);
  1674. isc->pipeline[i] = regs;
  1675. }
  1676. return 0;
  1677. }
  1678. static int isc_parse_dt(struct device *dev, struct isc_device *isc)
  1679. {
  1680. struct device_node *np = dev->of_node;
  1681. struct device_node *epn = NULL, *rem;
  1682. struct v4l2_fwnode_endpoint v4l2_epn;
  1683. struct isc_subdev_entity *subdev_entity;
  1684. unsigned int flags;
  1685. int ret;
  1686. INIT_LIST_HEAD(&isc->subdev_entities);
  1687. while (1) {
  1688. epn = of_graph_get_next_endpoint(np, epn);
  1689. if (!epn)
  1690. return 0;
  1691. rem = of_graph_get_remote_port_parent(epn);
  1692. if (!rem) {
  1693. dev_notice(dev, "Remote device at %pOF not found\n",
  1694. epn);
  1695. continue;
  1696. }
  1697. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(epn),
  1698. &v4l2_epn);
  1699. if (ret) {
  1700. of_node_put(rem);
  1701. ret = -EINVAL;
  1702. dev_err(dev, "Could not parse the endpoint\n");
  1703. break;
  1704. }
  1705. subdev_entity = devm_kzalloc(dev,
  1706. sizeof(*subdev_entity), GFP_KERNEL);
  1707. if (!subdev_entity) {
  1708. of_node_put(rem);
  1709. ret = -ENOMEM;
  1710. break;
  1711. }
  1712. /* asd will be freed by the subsystem once it's added to the
  1713. * notifier list
  1714. */
  1715. subdev_entity->asd = kzalloc(sizeof(*subdev_entity->asd),
  1716. GFP_KERNEL);
  1717. if (!subdev_entity->asd) {
  1718. of_node_put(rem);
  1719. ret = -ENOMEM;
  1720. break;
  1721. }
  1722. flags = v4l2_epn.bus.parallel.flags;
  1723. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  1724. subdev_entity->pfe_cfg0 = ISC_PFE_CFG0_HPOL_LOW;
  1725. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  1726. subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_VPOL_LOW;
  1727. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  1728. subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_PPOL_LOW;
  1729. subdev_entity->asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
  1730. subdev_entity->asd->match.fwnode =
  1731. of_fwnode_handle(rem);
  1732. list_add_tail(&subdev_entity->list, &isc->subdev_entities);
  1733. }
  1734. of_node_put(epn);
  1735. return ret;
  1736. }
  1737. /* regmap configuration */
  1738. #define ATMEL_ISC_REG_MAX 0xbfc
  1739. static const struct regmap_config isc_regmap_config = {
  1740. .reg_bits = 32,
  1741. .reg_stride = 4,
  1742. .val_bits = 32,
  1743. .max_register = ATMEL_ISC_REG_MAX,
  1744. };
  1745. static int atmel_isc_probe(struct platform_device *pdev)
  1746. {
  1747. struct device *dev = &pdev->dev;
  1748. struct isc_device *isc;
  1749. struct resource *res;
  1750. void __iomem *io_base;
  1751. struct isc_subdev_entity *subdev_entity;
  1752. int irq;
  1753. int ret;
  1754. isc = devm_kzalloc(dev, sizeof(*isc), GFP_KERNEL);
  1755. if (!isc)
  1756. return -ENOMEM;
  1757. platform_set_drvdata(pdev, isc);
  1758. isc->dev = dev;
  1759. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1760. io_base = devm_ioremap_resource(dev, res);
  1761. if (IS_ERR(io_base))
  1762. return PTR_ERR(io_base);
  1763. isc->regmap = devm_regmap_init_mmio(dev, io_base, &isc_regmap_config);
  1764. if (IS_ERR(isc->regmap)) {
  1765. ret = PTR_ERR(isc->regmap);
  1766. dev_err(dev, "failed to init register map: %d\n", ret);
  1767. return ret;
  1768. }
  1769. irq = platform_get_irq(pdev, 0);
  1770. if (irq < 0) {
  1771. ret = irq;
  1772. dev_err(dev, "failed to get irq: %d\n", ret);
  1773. return ret;
  1774. }
  1775. ret = devm_request_irq(dev, irq, isc_interrupt, 0,
  1776. ATMEL_ISC_NAME, isc);
  1777. if (ret < 0) {
  1778. dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
  1779. irq, ret);
  1780. return ret;
  1781. }
  1782. ret = isc_pipeline_init(isc);
  1783. if (ret)
  1784. return ret;
  1785. isc->hclock = devm_clk_get(dev, "hclock");
  1786. if (IS_ERR(isc->hclock)) {
  1787. ret = PTR_ERR(isc->hclock);
  1788. dev_err(dev, "failed to get hclock: %d\n", ret);
  1789. return ret;
  1790. }
  1791. ret = clk_prepare_enable(isc->hclock);
  1792. if (ret) {
  1793. dev_err(dev, "failed to enable hclock: %d\n", ret);
  1794. return ret;
  1795. }
  1796. ret = isc_clk_init(isc);
  1797. if (ret) {
  1798. dev_err(dev, "failed to init isc clock: %d\n", ret);
  1799. goto unprepare_hclk;
  1800. }
  1801. isc->ispck = isc->isc_clks[ISC_ISPCK].clk;
  1802. ret = clk_prepare_enable(isc->ispck);
  1803. if (ret) {
  1804. dev_err(dev, "failed to enable ispck: %d\n", ret);
  1805. goto unprepare_hclk;
  1806. }
  1807. /* ispck should be greater or equal to hclock */
  1808. ret = clk_set_rate(isc->ispck, clk_get_rate(isc->hclock));
  1809. if (ret) {
  1810. dev_err(dev, "failed to set ispck rate: %d\n", ret);
  1811. goto unprepare_clk;
  1812. }
  1813. ret = v4l2_device_register(dev, &isc->v4l2_dev);
  1814. if (ret) {
  1815. dev_err(dev, "unable to register v4l2 device.\n");
  1816. goto unprepare_clk;
  1817. }
  1818. ret = isc_parse_dt(dev, isc);
  1819. if (ret) {
  1820. dev_err(dev, "fail to parse device tree\n");
  1821. goto unregister_v4l2_device;
  1822. }
  1823. if (list_empty(&isc->subdev_entities)) {
  1824. dev_err(dev, "no subdev found\n");
  1825. ret = -ENODEV;
  1826. goto unregister_v4l2_device;
  1827. }
  1828. list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
  1829. subdev_entity->notifier.subdevs = &subdev_entity->asd;
  1830. subdev_entity->notifier.num_subdevs = 1;
  1831. subdev_entity->notifier.ops = &isc_async_ops;
  1832. ret = v4l2_async_notifier_register(&isc->v4l2_dev,
  1833. &subdev_entity->notifier);
  1834. if (ret) {
  1835. dev_err(dev, "fail to register async notifier\n");
  1836. kfree(subdev_entity->asd);
  1837. goto cleanup_subdev;
  1838. }
  1839. if (video_is_registered(&isc->video_dev))
  1840. break;
  1841. }
  1842. pm_runtime_set_active(dev);
  1843. pm_runtime_enable(dev);
  1844. pm_request_idle(dev);
  1845. return 0;
  1846. cleanup_subdev:
  1847. isc_subdev_cleanup(isc);
  1848. unregister_v4l2_device:
  1849. v4l2_device_unregister(&isc->v4l2_dev);
  1850. unprepare_clk:
  1851. clk_disable_unprepare(isc->ispck);
  1852. unprepare_hclk:
  1853. clk_disable_unprepare(isc->hclock);
  1854. isc_clk_cleanup(isc);
  1855. return ret;
  1856. }
  1857. static int atmel_isc_remove(struct platform_device *pdev)
  1858. {
  1859. struct isc_device *isc = platform_get_drvdata(pdev);
  1860. pm_runtime_disable(&pdev->dev);
  1861. clk_disable_unprepare(isc->ispck);
  1862. clk_disable_unprepare(isc->hclock);
  1863. isc_subdev_cleanup(isc);
  1864. v4l2_device_unregister(&isc->v4l2_dev);
  1865. isc_clk_cleanup(isc);
  1866. return 0;
  1867. }
  1868. static int __maybe_unused isc_runtime_suspend(struct device *dev)
  1869. {
  1870. struct isc_device *isc = dev_get_drvdata(dev);
  1871. clk_disable_unprepare(isc->ispck);
  1872. clk_disable_unprepare(isc->hclock);
  1873. return 0;
  1874. }
  1875. static int __maybe_unused isc_runtime_resume(struct device *dev)
  1876. {
  1877. struct isc_device *isc = dev_get_drvdata(dev);
  1878. int ret;
  1879. ret = clk_prepare_enable(isc->hclock);
  1880. if (ret)
  1881. return ret;
  1882. return clk_prepare_enable(isc->ispck);
  1883. }
  1884. static const struct dev_pm_ops atmel_isc_dev_pm_ops = {
  1885. SET_RUNTIME_PM_OPS(isc_runtime_suspend, isc_runtime_resume, NULL)
  1886. };
  1887. static const struct of_device_id atmel_isc_of_match[] = {
  1888. { .compatible = "atmel,sama5d2-isc" },
  1889. { }
  1890. };
  1891. MODULE_DEVICE_TABLE(of, atmel_isc_of_match);
  1892. static struct platform_driver atmel_isc_driver = {
  1893. .probe = atmel_isc_probe,
  1894. .remove = atmel_isc_remove,
  1895. .driver = {
  1896. .name = ATMEL_ISC_NAME,
  1897. .pm = &atmel_isc_dev_pm_ops,
  1898. .of_match_table = of_match_ptr(atmel_isc_of_match),
  1899. },
  1900. };
  1901. module_platform_driver(atmel_isc_driver);
  1902. MODULE_AUTHOR("Songjun Wu <songjun.wu@microchip.com>");
  1903. MODULE_DESCRIPTION("The V4L2 driver for Atmel-ISC");
  1904. MODULE_LICENSE("GPL v2");
  1905. MODULE_SUPPORTED_DEVICE("video");