atmel-isc.c 49 KB

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