atmel-isc.c 49 KB

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