ov772x.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ov772x Camera Driver
  4. *
  5. * Copyright (C) 2017 Jacopo Mondi <jacopo+renesas@jmondi.org>
  6. *
  7. * Copyright (C) 2008 Renesas Solutions Corp.
  8. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  9. *
  10. * Based on ov7670 and soc_camera_platform driver,
  11. *
  12. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  13. * Copyright (C) 2008 Magnus Damm
  14. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/i2c.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/regmap.h>
  24. #include <linux/slab.h>
  25. #include <linux/v4l2-mediabus.h>
  26. #include <linux/videodev2.h>
  27. #include <media/i2c/ov772x.h>
  28. #include <media/v4l2-ctrls.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-event.h>
  31. #include <media/v4l2-fwnode.h>
  32. #include <media/v4l2-image-sizes.h>
  33. #include <media/v4l2-subdev.h>
  34. /*
  35. * register offset
  36. */
  37. #define GAIN 0x00 /* AGC - Gain control gain setting */
  38. #define BLUE 0x01 /* AWB - Blue channel gain setting */
  39. #define RED 0x02 /* AWB - Red channel gain setting */
  40. #define GREEN 0x03 /* AWB - Green channel gain setting */
  41. #define COM1 0x04 /* Common control 1 */
  42. #define BAVG 0x05 /* U/B Average Level */
  43. #define GAVG 0x06 /* Y/Gb Average Level */
  44. #define RAVG 0x07 /* V/R Average Level */
  45. #define AECH 0x08 /* Exposure Value - AEC MSBs */
  46. #define COM2 0x09 /* Common control 2 */
  47. #define PID 0x0A /* Product ID Number MSB */
  48. #define VER 0x0B /* Product ID Number LSB */
  49. #define COM3 0x0C /* Common control 3 */
  50. #define COM4 0x0D /* Common control 4 */
  51. #define COM5 0x0E /* Common control 5 */
  52. #define COM6 0x0F /* Common control 6 */
  53. #define AEC 0x10 /* Exposure Value */
  54. #define CLKRC 0x11 /* Internal clock */
  55. #define COM7 0x12 /* Common control 7 */
  56. #define COM8 0x13 /* Common control 8 */
  57. #define COM9 0x14 /* Common control 9 */
  58. #define COM10 0x15 /* Common control 10 */
  59. #define REG16 0x16 /* Register 16 */
  60. #define HSTART 0x17 /* Horizontal sensor size */
  61. #define HSIZE 0x18 /* Horizontal frame (HREF column) end high 8-bit */
  62. #define VSTART 0x19 /* Vertical frame (row) start high 8-bit */
  63. #define VSIZE 0x1A /* Vertical sensor size */
  64. #define PSHFT 0x1B /* Data format - pixel delay select */
  65. #define MIDH 0x1C /* Manufacturer ID byte - high */
  66. #define MIDL 0x1D /* Manufacturer ID byte - low */
  67. #define LAEC 0x1F /* Fine AEC value */
  68. #define COM11 0x20 /* Common control 11 */
  69. #define BDBASE 0x22 /* Banding filter Minimum AEC value */
  70. #define DBSTEP 0x23 /* Banding filter Maximum Setp */
  71. #define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
  72. #define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
  73. #define VPT 0x26 /* AGC/AEC Fast mode operating region */
  74. #define REG28 0x28 /* Register 28 */
  75. #define HOUTSIZE 0x29 /* Horizontal data output size MSBs */
  76. #define EXHCH 0x2A /* Dummy pixel insert MSB */
  77. #define EXHCL 0x2B /* Dummy pixel insert LSB */
  78. #define VOUTSIZE 0x2C /* Vertical data output size MSBs */
  79. #define ADVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
  80. #define ADVFH 0x2E /* MSG of insert dummy lines in Vertical direction */
  81. #define YAVE 0x2F /* Y/G Channel Average value */
  82. #define LUMHTH 0x30 /* Histogram AEC/AGC Luminance high level threshold */
  83. #define LUMLTH 0x31 /* Histogram AEC/AGC Luminance low level threshold */
  84. #define HREF 0x32 /* Image start and size control */
  85. #define DM_LNL 0x33 /* Dummy line low 8 bits */
  86. #define DM_LNH 0x34 /* Dummy line high 8 bits */
  87. #define ADOFF_B 0x35 /* AD offset compensation value for B channel */
  88. #define ADOFF_R 0x36 /* AD offset compensation value for R channel */
  89. #define ADOFF_GB 0x37 /* AD offset compensation value for Gb channel */
  90. #define ADOFF_GR 0x38 /* AD offset compensation value for Gr channel */
  91. #define OFF_B 0x39 /* Analog process B channel offset value */
  92. #define OFF_R 0x3A /* Analog process R channel offset value */
  93. #define OFF_GB 0x3B /* Analog process Gb channel offset value */
  94. #define OFF_GR 0x3C /* Analog process Gr channel offset value */
  95. #define COM12 0x3D /* Common control 12 */
  96. #define COM13 0x3E /* Common control 13 */
  97. #define COM14 0x3F /* Common control 14 */
  98. #define COM15 0x40 /* Common control 15*/
  99. #define COM16 0x41 /* Common control 16 */
  100. #define TGT_B 0x42 /* BLC blue channel target value */
  101. #define TGT_R 0x43 /* BLC red channel target value */
  102. #define TGT_GB 0x44 /* BLC Gb channel target value */
  103. #define TGT_GR 0x45 /* BLC Gr channel target value */
  104. /* for ov7720 */
  105. #define LCC0 0x46 /* Lens correction control 0 */
  106. #define LCC1 0x47 /* Lens correction option 1 - X coordinate */
  107. #define LCC2 0x48 /* Lens correction option 2 - Y coordinate */
  108. #define LCC3 0x49 /* Lens correction option 3 */
  109. #define LCC4 0x4A /* Lens correction option 4 - radius of the circular */
  110. #define LCC5 0x4B /* Lens correction option 5 */
  111. #define LCC6 0x4C /* Lens correction option 6 */
  112. /* for ov7725 */
  113. #define LC_CTR 0x46 /* Lens correction control */
  114. #define LC_XC 0x47 /* X coordinate of lens correction center relative */
  115. #define LC_YC 0x48 /* Y coordinate of lens correction center relative */
  116. #define LC_COEF 0x49 /* Lens correction coefficient */
  117. #define LC_RADI 0x4A /* Lens correction radius */
  118. #define LC_COEFB 0x4B /* Lens B channel compensation coefficient */
  119. #define LC_COEFR 0x4C /* Lens R channel compensation coefficient */
  120. #define FIXGAIN 0x4D /* Analog fix gain amplifer */
  121. #define AREF0 0x4E /* Sensor reference control */
  122. #define AREF1 0x4F /* Sensor reference current control */
  123. #define AREF2 0x50 /* Analog reference control */
  124. #define AREF3 0x51 /* ADC reference control */
  125. #define AREF4 0x52 /* ADC reference control */
  126. #define AREF5 0x53 /* ADC reference control */
  127. #define AREF6 0x54 /* Analog reference control */
  128. #define AREF7 0x55 /* Analog reference control */
  129. #define UFIX 0x60 /* U channel fixed value output */
  130. #define VFIX 0x61 /* V channel fixed value output */
  131. #define AWBB_BLK 0x62 /* AWB option for advanced AWB */
  132. #define AWB_CTRL0 0x63 /* AWB control byte 0 */
  133. #define DSP_CTRL1 0x64 /* DSP control byte 1 */
  134. #define DSP_CTRL2 0x65 /* DSP control byte 2 */
  135. #define DSP_CTRL3 0x66 /* DSP control byte 3 */
  136. #define DSP_CTRL4 0x67 /* DSP control byte 4 */
  137. #define AWB_BIAS 0x68 /* AWB BLC level clip */
  138. #define AWB_CTRL1 0x69 /* AWB control 1 */
  139. #define AWB_CTRL2 0x6A /* AWB control 2 */
  140. #define AWB_CTRL3 0x6B /* AWB control 3 */
  141. #define AWB_CTRL4 0x6C /* AWB control 4 */
  142. #define AWB_CTRL5 0x6D /* AWB control 5 */
  143. #define AWB_CTRL6 0x6E /* AWB control 6 */
  144. #define AWB_CTRL7 0x6F /* AWB control 7 */
  145. #define AWB_CTRL8 0x70 /* AWB control 8 */
  146. #define AWB_CTRL9 0x71 /* AWB control 9 */
  147. #define AWB_CTRL10 0x72 /* AWB control 10 */
  148. #define AWB_CTRL11 0x73 /* AWB control 11 */
  149. #define AWB_CTRL12 0x74 /* AWB control 12 */
  150. #define AWB_CTRL13 0x75 /* AWB control 13 */
  151. #define AWB_CTRL14 0x76 /* AWB control 14 */
  152. #define AWB_CTRL15 0x77 /* AWB control 15 */
  153. #define AWB_CTRL16 0x78 /* AWB control 16 */
  154. #define AWB_CTRL17 0x79 /* AWB control 17 */
  155. #define AWB_CTRL18 0x7A /* AWB control 18 */
  156. #define AWB_CTRL19 0x7B /* AWB control 19 */
  157. #define AWB_CTRL20 0x7C /* AWB control 20 */
  158. #define AWB_CTRL21 0x7D /* AWB control 21 */
  159. #define GAM1 0x7E /* Gamma Curve 1st segment input end point */
  160. #define GAM2 0x7F /* Gamma Curve 2nd segment input end point */
  161. #define GAM3 0x80 /* Gamma Curve 3rd segment input end point */
  162. #define GAM4 0x81 /* Gamma Curve 4th segment input end point */
  163. #define GAM5 0x82 /* Gamma Curve 5th segment input end point */
  164. #define GAM6 0x83 /* Gamma Curve 6th segment input end point */
  165. #define GAM7 0x84 /* Gamma Curve 7th segment input end point */
  166. #define GAM8 0x85 /* Gamma Curve 8th segment input end point */
  167. #define GAM9 0x86 /* Gamma Curve 9th segment input end point */
  168. #define GAM10 0x87 /* Gamma Curve 10th segment input end point */
  169. #define GAM11 0x88 /* Gamma Curve 11th segment input end point */
  170. #define GAM12 0x89 /* Gamma Curve 12th segment input end point */
  171. #define GAM13 0x8A /* Gamma Curve 13th segment input end point */
  172. #define GAM14 0x8B /* Gamma Curve 14th segment input end point */
  173. #define GAM15 0x8C /* Gamma Curve 15th segment input end point */
  174. #define SLOP 0x8D /* Gamma curve highest segment slope */
  175. #define DNSTH 0x8E /* De-noise threshold */
  176. #define EDGE_STRNGT 0x8F /* Edge strength control when manual mode */
  177. #define EDGE_TRSHLD 0x90 /* Edge threshold control when manual mode */
  178. #define DNSOFF 0x91 /* Auto De-noise threshold control */
  179. #define EDGE_UPPER 0x92 /* Edge strength upper limit when Auto mode */
  180. #define EDGE_LOWER 0x93 /* Edge strength lower limit when Auto mode */
  181. #define MTX1 0x94 /* Matrix coefficient 1 */
  182. #define MTX2 0x95 /* Matrix coefficient 2 */
  183. #define MTX3 0x96 /* Matrix coefficient 3 */
  184. #define MTX4 0x97 /* Matrix coefficient 4 */
  185. #define MTX5 0x98 /* Matrix coefficient 5 */
  186. #define MTX6 0x99 /* Matrix coefficient 6 */
  187. #define MTX_CTRL 0x9A /* Matrix control */
  188. #define BRIGHT 0x9B /* Brightness control */
  189. #define CNTRST 0x9C /* Contrast contrast */
  190. #define CNTRST_CTRL 0x9D /* Contrast contrast center */
  191. #define UVAD_J0 0x9E /* Auto UV adjust contrast 0 */
  192. #define UVAD_J1 0x9F /* Auto UV adjust contrast 1 */
  193. #define SCAL0 0xA0 /* Scaling control 0 */
  194. #define SCAL1 0xA1 /* Scaling control 1 */
  195. #define SCAL2 0xA2 /* Scaling control 2 */
  196. #define FIFODLYM 0xA3 /* FIFO manual mode delay control */
  197. #define FIFODLYA 0xA4 /* FIFO auto mode delay control */
  198. #define SDE 0xA6 /* Special digital effect control */
  199. #define USAT 0xA7 /* U component saturation control */
  200. #define VSAT 0xA8 /* V component saturation control */
  201. /* for ov7720 */
  202. #define HUE0 0xA9 /* Hue control 0 */
  203. #define HUE1 0xAA /* Hue control 1 */
  204. /* for ov7725 */
  205. #define HUECOS 0xA9 /* Cosine value */
  206. #define HUESIN 0xAA /* Sine value */
  207. #define SIGN 0xAB /* Sign bit for Hue and contrast */
  208. #define DSPAUTO 0xAC /* DSP auto function ON/OFF control */
  209. /*
  210. * register detail
  211. */
  212. /* COM2 */
  213. #define SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
  214. /* Output drive capability */
  215. #define OCAP_1x 0x00 /* 1x */
  216. #define OCAP_2x 0x01 /* 2x */
  217. #define OCAP_3x 0x02 /* 3x */
  218. #define OCAP_4x 0x03 /* 4x */
  219. /* COM3 */
  220. #define SWAP_MASK (SWAP_RGB | SWAP_YUV | SWAP_ML)
  221. #define IMG_MASK (VFLIP_IMG | HFLIP_IMG | SCOLOR_TEST)
  222. #define VFLIP_IMG 0x80 /* Vertical flip image ON/OFF selection */
  223. #define HFLIP_IMG 0x40 /* Horizontal mirror image ON/OFF selection */
  224. #define SWAP_RGB 0x20 /* Swap B/R output sequence in RGB mode */
  225. #define SWAP_YUV 0x10 /* Swap Y/UV output sequence in YUV mode */
  226. #define SWAP_ML 0x08 /* Swap output MSB/LSB */
  227. /* Tri-state option for output clock */
  228. #define NOTRI_CLOCK 0x04 /* 0: Tri-state at this period */
  229. /* 1: No tri-state at this period */
  230. /* Tri-state option for output data */
  231. #define NOTRI_DATA 0x02 /* 0: Tri-state at this period */
  232. /* 1: No tri-state at this period */
  233. #define SCOLOR_TEST 0x01 /* Sensor color bar test pattern */
  234. /* COM4 */
  235. /* PLL frequency control */
  236. #define PLL_BYPASS 0x00 /* 00: Bypass PLL */
  237. #define PLL_4x 0x40 /* 01: PLL 4x */
  238. #define PLL_6x 0x80 /* 10: PLL 6x */
  239. #define PLL_8x 0xc0 /* 11: PLL 8x */
  240. /* AEC evaluate window */
  241. #define AEC_FULL 0x00 /* 00: Full window */
  242. #define AEC_1p2 0x10 /* 01: 1/2 window */
  243. #define AEC_1p4 0x20 /* 10: 1/4 window */
  244. #define AEC_2p3 0x30 /* 11: Low 2/3 window */
  245. #define COM4_RESERVED 0x01 /* Reserved bit */
  246. /* COM5 */
  247. #define AFR_ON_OFF 0x80 /* Auto frame rate control ON/OFF selection */
  248. #define AFR_SPPED 0x40 /* Auto frame rate control speed selection */
  249. /* Auto frame rate max rate control */
  250. #define AFR_NO_RATE 0x00 /* No reduction of frame rate */
  251. #define AFR_1p2 0x10 /* Max reduction to 1/2 frame rate */
  252. #define AFR_1p4 0x20 /* Max reduction to 1/4 frame rate */
  253. #define AFR_1p8 0x30 /* Max reduction to 1/8 frame rate */
  254. /* Auto frame rate active point control */
  255. #define AF_2x 0x00 /* Add frame when AGC reaches 2x gain */
  256. #define AF_4x 0x04 /* Add frame when AGC reaches 4x gain */
  257. #define AF_8x 0x08 /* Add frame when AGC reaches 8x gain */
  258. #define AF_16x 0x0c /* Add frame when AGC reaches 16x gain */
  259. /* AEC max step control */
  260. #define AEC_NO_LIMIT 0x01 /* 0 : AEC increase step has limit */
  261. /* 1 : No limit to AEC increase step */
  262. /* CLKRC */
  263. /* Input clock divider register */
  264. #define CLKRC_RESERVED 0x80 /* Reserved bit */
  265. #define CLKRC_DIV(n) ((n) - 1)
  266. /* COM7 */
  267. /* SCCB Register Reset */
  268. #define SCCB_RESET 0x80 /* 0 : No change */
  269. /* 1 : Resets all registers to default */
  270. /* Resolution selection */
  271. #define SLCT_MASK 0x40 /* Mask of VGA or QVGA */
  272. #define SLCT_VGA 0x00 /* 0 : VGA */
  273. #define SLCT_QVGA 0x40 /* 1 : QVGA */
  274. #define ITU656_ON_OFF 0x20 /* ITU656 protocol ON/OFF selection */
  275. #define SENSOR_RAW 0x10 /* Sensor RAW */
  276. /* RGB output format control */
  277. #define FMT_MASK 0x0c /* Mask of color format */
  278. #define FMT_GBR422 0x00 /* 00 : GBR 4:2:2 */
  279. #define FMT_RGB565 0x04 /* 01 : RGB 565 */
  280. #define FMT_RGB555 0x08 /* 10 : RGB 555 */
  281. #define FMT_RGB444 0x0c /* 11 : RGB 444 */
  282. /* Output format control */
  283. #define OFMT_MASK 0x03 /* Mask of output format */
  284. #define OFMT_YUV 0x00 /* 00 : YUV */
  285. #define OFMT_P_BRAW 0x01 /* 01 : Processed Bayer RAW */
  286. #define OFMT_RGB 0x02 /* 10 : RGB */
  287. #define OFMT_BRAW 0x03 /* 11 : Bayer RAW */
  288. /* COM8 */
  289. #define FAST_ALGO 0x80 /* Enable fast AGC/AEC algorithm */
  290. /* AEC Setp size limit */
  291. #define UNLMT_STEP 0x40 /* 0 : Step size is limited */
  292. /* 1 : Unlimited step size */
  293. #define BNDF_ON_OFF 0x20 /* Banding filter ON/OFF */
  294. #define AEC_BND 0x10 /* Enable AEC below banding value */
  295. #define AEC_ON_OFF 0x08 /* Fine AEC ON/OFF control */
  296. #define AGC_ON 0x04 /* AGC Enable */
  297. #define AWB_ON 0x02 /* AWB Enable */
  298. #define AEC_ON 0x01 /* AEC Enable */
  299. /* COM9 */
  300. #define BASE_AECAGC 0x80 /* Histogram or average based AEC/AGC */
  301. /* Automatic gain ceiling - maximum AGC value */
  302. #define GAIN_2x 0x00 /* 000 : 2x */
  303. #define GAIN_4x 0x10 /* 001 : 4x */
  304. #define GAIN_8x 0x20 /* 010 : 8x */
  305. #define GAIN_16x 0x30 /* 011 : 16x */
  306. #define GAIN_32x 0x40 /* 100 : 32x */
  307. #define GAIN_64x 0x50 /* 101 : 64x */
  308. #define GAIN_128x 0x60 /* 110 : 128x */
  309. #define DROP_VSYNC 0x04 /* Drop VSYNC output of corrupt frame */
  310. #define DROP_HREF 0x02 /* Drop HREF output of corrupt frame */
  311. /* COM11 */
  312. #define SGLF_ON_OFF 0x02 /* Single frame ON/OFF selection */
  313. #define SGLF_TRIG 0x01 /* Single frame transfer trigger */
  314. /* HREF */
  315. #define HREF_VSTART_SHIFT 6 /* VSTART LSB */
  316. #define HREF_HSTART_SHIFT 4 /* HSTART 2 LSBs */
  317. #define HREF_VSIZE_SHIFT 2 /* VSIZE LSB */
  318. #define HREF_HSIZE_SHIFT 0 /* HSIZE 2 LSBs */
  319. /* EXHCH */
  320. #define EXHCH_VSIZE_SHIFT 2 /* VOUTSIZE LSB */
  321. #define EXHCH_HSIZE_SHIFT 0 /* HOUTSIZE 2 LSBs */
  322. /* DSP_CTRL1 */
  323. #define FIFO_ON 0x80 /* FIFO enable/disable selection */
  324. #define UV_ON_OFF 0x40 /* UV adjust function ON/OFF selection */
  325. #define YUV444_2_422 0x20 /* YUV444 to 422 UV channel option selection */
  326. #define CLR_MTRX_ON_OFF 0x10 /* Color matrix ON/OFF selection */
  327. #define INTPLT_ON_OFF 0x08 /* Interpolation ON/OFF selection */
  328. #define GMM_ON_OFF 0x04 /* Gamma function ON/OFF selection */
  329. #define AUTO_BLK_ON_OFF 0x02 /* Black defect auto correction ON/OFF */
  330. #define AUTO_WHT_ON_OFF 0x01 /* White define auto correction ON/OFF */
  331. /* DSP_CTRL3 */
  332. #define UV_MASK 0x80 /* UV output sequence option */
  333. #define UV_ON 0x80 /* ON */
  334. #define UV_OFF 0x00 /* OFF */
  335. #define CBAR_MASK 0x20 /* DSP Color bar mask */
  336. #define CBAR_ON 0x20 /* ON */
  337. #define CBAR_OFF 0x00 /* OFF */
  338. /* DSP_CTRL4 */
  339. #define DSP_OFMT_YUV 0x00
  340. #define DSP_OFMT_RGB 0x00
  341. #define DSP_OFMT_RAW8 0x02
  342. #define DSP_OFMT_RAW10 0x03
  343. /* DSPAUTO (DSP Auto Function ON/OFF Control) */
  344. #define AWB_ACTRL 0x80 /* AWB auto threshold control */
  345. #define DENOISE_ACTRL 0x40 /* De-noise auto threshold control */
  346. #define EDGE_ACTRL 0x20 /* Edge enhancement auto strength control */
  347. #define UV_ACTRL 0x10 /* UV adjust auto slope control */
  348. #define SCAL0_ACTRL 0x08 /* Auto scaling factor control */
  349. #define SCAL1_2_ACTRL 0x04 /* Auto scaling factor control */
  350. #define OV772X_MAX_WIDTH VGA_WIDTH
  351. #define OV772X_MAX_HEIGHT VGA_HEIGHT
  352. /*
  353. * ID
  354. */
  355. #define OV7720 0x7720
  356. #define OV7725 0x7721
  357. #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
  358. /*
  359. * PLL multipliers
  360. */
  361. static struct {
  362. unsigned int mult;
  363. u8 com4;
  364. } ov772x_pll[] = {
  365. { 1, PLL_BYPASS, },
  366. { 4, PLL_4x, },
  367. { 6, PLL_6x, },
  368. { 8, PLL_8x, },
  369. };
  370. /*
  371. * struct
  372. */
  373. struct ov772x_color_format {
  374. u32 code;
  375. enum v4l2_colorspace colorspace;
  376. u8 dsp3;
  377. u8 dsp4;
  378. u8 com3;
  379. u8 com7;
  380. };
  381. struct ov772x_win_size {
  382. char *name;
  383. unsigned char com7_bit;
  384. unsigned int sizeimage;
  385. struct v4l2_rect rect;
  386. };
  387. struct ov772x_priv {
  388. struct v4l2_subdev subdev;
  389. struct v4l2_ctrl_handler hdl;
  390. struct clk *clk;
  391. struct regmap *regmap;
  392. struct ov772x_camera_info *info;
  393. struct gpio_desc *pwdn_gpio;
  394. struct gpio_desc *rstb_gpio;
  395. const struct ov772x_color_format *cfmt;
  396. const struct ov772x_win_size *win;
  397. struct v4l2_ctrl *vflip_ctrl;
  398. struct v4l2_ctrl *hflip_ctrl;
  399. unsigned int test_pattern;
  400. /* band_filter = COM8[5] ? 256 - BDBASE : 0 */
  401. struct v4l2_ctrl *band_filter_ctrl;
  402. unsigned int fps;
  403. /* lock to protect power_count and streaming */
  404. struct mutex lock;
  405. int power_count;
  406. int streaming;
  407. struct media_pad pad;
  408. enum v4l2_mbus_type bus_type;
  409. };
  410. /*
  411. * supported color format list
  412. */
  413. static const struct ov772x_color_format ov772x_cfmts[] = {
  414. {
  415. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  416. .colorspace = V4L2_COLORSPACE_SRGB,
  417. .dsp3 = 0x0,
  418. .dsp4 = DSP_OFMT_YUV,
  419. .com3 = SWAP_YUV,
  420. .com7 = OFMT_YUV,
  421. },
  422. {
  423. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  424. .colorspace = V4L2_COLORSPACE_SRGB,
  425. .dsp3 = UV_ON,
  426. .dsp4 = DSP_OFMT_YUV,
  427. .com3 = SWAP_YUV,
  428. .com7 = OFMT_YUV,
  429. },
  430. {
  431. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  432. .colorspace = V4L2_COLORSPACE_SRGB,
  433. .dsp3 = 0x0,
  434. .dsp4 = DSP_OFMT_YUV,
  435. .com3 = 0x0,
  436. .com7 = OFMT_YUV,
  437. },
  438. {
  439. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
  440. .colorspace = V4L2_COLORSPACE_SRGB,
  441. .dsp3 = 0x0,
  442. .dsp4 = DSP_OFMT_YUV,
  443. .com3 = SWAP_RGB,
  444. .com7 = FMT_RGB555 | OFMT_RGB,
  445. },
  446. {
  447. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
  448. .colorspace = V4L2_COLORSPACE_SRGB,
  449. .dsp3 = 0x0,
  450. .dsp4 = DSP_OFMT_YUV,
  451. .com3 = 0x0,
  452. .com7 = FMT_RGB555 | OFMT_RGB,
  453. },
  454. {
  455. .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  456. .colorspace = V4L2_COLORSPACE_SRGB,
  457. .dsp3 = 0x0,
  458. .dsp4 = DSP_OFMT_YUV,
  459. .com3 = SWAP_RGB,
  460. .com7 = FMT_RGB565 | OFMT_RGB,
  461. },
  462. {
  463. .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
  464. .colorspace = V4L2_COLORSPACE_SRGB,
  465. .dsp3 = 0x0,
  466. .dsp4 = DSP_OFMT_YUV,
  467. .com3 = 0x0,
  468. .com7 = FMT_RGB565 | OFMT_RGB,
  469. },
  470. {
  471. /* Setting DSP4 to DSP_OFMT_RAW8 still gives 10-bit output,
  472. * regardless of the COM7 value. We can thus only support 10-bit
  473. * Bayer until someone figures it out.
  474. */
  475. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  476. .colorspace = V4L2_COLORSPACE_SRGB,
  477. .dsp3 = 0x0,
  478. .dsp4 = DSP_OFMT_RAW10,
  479. .com3 = 0x0,
  480. .com7 = SENSOR_RAW | OFMT_BRAW,
  481. },
  482. };
  483. /*
  484. * window size list
  485. */
  486. static const struct ov772x_win_size ov772x_win_sizes[] = {
  487. {
  488. .name = "VGA",
  489. .com7_bit = SLCT_VGA,
  490. .sizeimage = 510 * 748,
  491. .rect = {
  492. .left = 140,
  493. .top = 14,
  494. .width = VGA_WIDTH,
  495. .height = VGA_HEIGHT,
  496. },
  497. }, {
  498. .name = "QVGA",
  499. .com7_bit = SLCT_QVGA,
  500. .sizeimage = 278 * 576,
  501. .rect = {
  502. .left = 252,
  503. .top = 6,
  504. .width = QVGA_WIDTH,
  505. .height = QVGA_HEIGHT,
  506. },
  507. },
  508. };
  509. static const char * const ov772x_test_pattern_menu[] = {
  510. "Disabled",
  511. "Vertical Color Bar Type 1",
  512. };
  513. /*
  514. * frame rate settings lists
  515. */
  516. static const unsigned int ov772x_frame_intervals[] = { 5, 10, 15, 20, 30, 60 };
  517. /*
  518. * general function
  519. */
  520. static struct ov772x_priv *to_ov772x(struct v4l2_subdev *sd)
  521. {
  522. return container_of(sd, struct ov772x_priv, subdev);
  523. }
  524. static int ov772x_reset(struct ov772x_priv *priv)
  525. {
  526. int ret;
  527. ret = regmap_write(priv->regmap, COM7, SCCB_RESET);
  528. if (ret < 0)
  529. return ret;
  530. usleep_range(1000, 5000);
  531. return regmap_update_bits(priv->regmap, COM2, SOFT_SLEEP_MODE,
  532. SOFT_SLEEP_MODE);
  533. }
  534. /*
  535. * subdev ops
  536. */
  537. static int ov772x_s_stream(struct v4l2_subdev *sd, int enable)
  538. {
  539. struct i2c_client *client = v4l2_get_subdevdata(sd);
  540. struct ov772x_priv *priv = to_ov772x(sd);
  541. int ret = 0;
  542. mutex_lock(&priv->lock);
  543. if (priv->streaming == enable)
  544. goto done;
  545. if (priv->bus_type == V4L2_MBUS_BT656) {
  546. ret = regmap_update_bits(priv->regmap, COM7, ITU656_ON_OFF,
  547. enable ?
  548. ITU656_ON_OFF : ~ITU656_ON_OFF);
  549. if (ret)
  550. goto done;
  551. }
  552. ret = regmap_update_bits(priv->regmap, COM2, SOFT_SLEEP_MODE,
  553. enable ? 0 : SOFT_SLEEP_MODE);
  554. if (ret)
  555. goto done;
  556. if (enable) {
  557. dev_dbg(&client->dev, "format %d, win %s\n",
  558. priv->cfmt->code, priv->win->name);
  559. }
  560. priv->streaming = enable;
  561. done:
  562. mutex_unlock(&priv->lock);
  563. return ret;
  564. }
  565. static unsigned int ov772x_select_fps(struct ov772x_priv *priv,
  566. struct v4l2_fract *tpf)
  567. {
  568. unsigned int fps = tpf->numerator ?
  569. tpf->denominator / tpf->numerator :
  570. tpf->denominator;
  571. unsigned int best_diff;
  572. unsigned int diff;
  573. unsigned int idx;
  574. unsigned int i;
  575. /* Approximate to the closest supported frame interval. */
  576. best_diff = ~0L;
  577. for (i = 0, idx = 0; i < ARRAY_SIZE(ov772x_frame_intervals); i++) {
  578. diff = abs(fps - ov772x_frame_intervals[i]);
  579. if (diff < best_diff) {
  580. idx = i;
  581. best_diff = diff;
  582. }
  583. }
  584. return ov772x_frame_intervals[idx];
  585. }
  586. static int ov772x_set_frame_rate(struct ov772x_priv *priv,
  587. unsigned int fps,
  588. const struct ov772x_color_format *cfmt,
  589. const struct ov772x_win_size *win)
  590. {
  591. unsigned long fin = clk_get_rate(priv->clk);
  592. unsigned int best_diff;
  593. unsigned int fsize;
  594. unsigned int pclk;
  595. unsigned int diff;
  596. unsigned int i;
  597. u8 clkrc = 0;
  598. u8 com4 = 0;
  599. int ret;
  600. /* Use image size (with blankings) to calculate desired pixel clock. */
  601. switch (cfmt->com7 & OFMT_MASK) {
  602. case OFMT_BRAW:
  603. fsize = win->sizeimage;
  604. break;
  605. case OFMT_RGB:
  606. case OFMT_YUV:
  607. default:
  608. fsize = win->sizeimage * 2;
  609. break;
  610. }
  611. pclk = fps * fsize;
  612. /*
  613. * Pixel clock generation circuit is pretty simple:
  614. *
  615. * Fin -> [ / CLKRC_div] -> [ * PLL_mult] -> pclk
  616. *
  617. * Try to approximate the desired pixel clock testing all available
  618. * PLL multipliers (1x, 4x, 6x, 8x) and calculate corresponding
  619. * divisor with:
  620. *
  621. * div = PLL_mult * Fin / pclk
  622. *
  623. * and re-calculate the pixel clock using it:
  624. *
  625. * pclk = Fin * PLL_mult / CLKRC_div
  626. *
  627. * Choose the PLL_mult and CLKRC_div pair that gives a pixel clock
  628. * closer to the desired one.
  629. *
  630. * The desired pixel clock is calculated using a known frame size
  631. * (blanking included) and FPS.
  632. */
  633. best_diff = ~0L;
  634. for (i = 0; i < ARRAY_SIZE(ov772x_pll); i++) {
  635. unsigned int pll_mult = ov772x_pll[i].mult;
  636. unsigned int pll_out = pll_mult * fin;
  637. unsigned int t_pclk;
  638. unsigned int div;
  639. if (pll_out < pclk)
  640. continue;
  641. div = DIV_ROUND_CLOSEST(pll_out, pclk);
  642. t_pclk = DIV_ROUND_CLOSEST(fin * pll_mult, div);
  643. diff = abs(pclk - t_pclk);
  644. if (diff < best_diff) {
  645. best_diff = diff;
  646. clkrc = CLKRC_DIV(div);
  647. com4 = ov772x_pll[i].com4;
  648. }
  649. }
  650. ret = regmap_write(priv->regmap, COM4, com4 | COM4_RESERVED);
  651. if (ret < 0)
  652. return ret;
  653. ret = regmap_write(priv->regmap, CLKRC, clkrc | CLKRC_RESERVED);
  654. if (ret < 0)
  655. return ret;
  656. return 0;
  657. }
  658. static int ov772x_get_frame_interval(struct v4l2_subdev *sd,
  659. struct v4l2_subdev_state *sd_state,
  660. struct v4l2_subdev_frame_interval *ival)
  661. {
  662. struct ov772x_priv *priv = to_ov772x(sd);
  663. struct v4l2_fract *tpf = &ival->interval;
  664. /*
  665. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  666. * subdev active state API.
  667. */
  668. if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  669. return -EINVAL;
  670. tpf->numerator = 1;
  671. tpf->denominator = priv->fps;
  672. return 0;
  673. }
  674. static int ov772x_set_frame_interval(struct v4l2_subdev *sd,
  675. struct v4l2_subdev_state *sd_state,
  676. struct v4l2_subdev_frame_interval *ival)
  677. {
  678. struct ov772x_priv *priv = to_ov772x(sd);
  679. struct v4l2_fract *tpf = &ival->interval;
  680. unsigned int fps;
  681. int ret = 0;
  682. /*
  683. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  684. * subdev active state API.
  685. */
  686. if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  687. return -EINVAL;
  688. mutex_lock(&priv->lock);
  689. if (priv->streaming) {
  690. ret = -EBUSY;
  691. goto error;
  692. }
  693. fps = ov772x_select_fps(priv, tpf);
  694. /*
  695. * If the device is not powered up by the host driver do
  696. * not apply any changes to H/W at this time. Instead
  697. * the frame rate will be restored right after power-up.
  698. */
  699. if (priv->power_count > 0) {
  700. ret = ov772x_set_frame_rate(priv, fps, priv->cfmt, priv->win);
  701. if (ret)
  702. goto error;
  703. }
  704. tpf->numerator = 1;
  705. tpf->denominator = fps;
  706. priv->fps = fps;
  707. error:
  708. mutex_unlock(&priv->lock);
  709. return ret;
  710. }
  711. static int ov772x_s_ctrl(struct v4l2_ctrl *ctrl)
  712. {
  713. struct ov772x_priv *priv = container_of(ctrl->handler,
  714. struct ov772x_priv, hdl);
  715. struct regmap *regmap = priv->regmap;
  716. int ret = 0;
  717. u8 val;
  718. /* v4l2_ctrl_lock() locks our own mutex */
  719. /*
  720. * If the device is not powered up by the host driver do
  721. * not apply any controls to H/W at this time. Instead
  722. * the controls will be restored right after power-up.
  723. */
  724. if (priv->power_count == 0)
  725. return 0;
  726. switch (ctrl->id) {
  727. case V4L2_CID_VFLIP:
  728. val = ctrl->val ? VFLIP_IMG : 0x00;
  729. if (priv->info && (priv->info->flags & OV772X_FLAG_VFLIP))
  730. val ^= VFLIP_IMG;
  731. return regmap_update_bits(regmap, COM3, VFLIP_IMG, val);
  732. case V4L2_CID_HFLIP:
  733. val = ctrl->val ? HFLIP_IMG : 0x00;
  734. if (priv->info && (priv->info->flags & OV772X_FLAG_HFLIP))
  735. val ^= HFLIP_IMG;
  736. return regmap_update_bits(regmap, COM3, HFLIP_IMG, val);
  737. case V4L2_CID_BAND_STOP_FILTER:
  738. if (!ctrl->val) {
  739. /* Switch the filter off, it is on now */
  740. ret = regmap_update_bits(regmap, BDBASE, 0xff, 0xff);
  741. if (!ret)
  742. ret = regmap_update_bits(regmap, COM8,
  743. BNDF_ON_OFF, 0);
  744. } else {
  745. /* Switch the filter on, set AEC low limit */
  746. val = 256 - ctrl->val;
  747. ret = regmap_update_bits(regmap, COM8,
  748. BNDF_ON_OFF, BNDF_ON_OFF);
  749. if (!ret)
  750. ret = regmap_update_bits(regmap, BDBASE,
  751. 0xff, val);
  752. }
  753. return ret;
  754. case V4L2_CID_TEST_PATTERN:
  755. priv->test_pattern = ctrl->val;
  756. return 0;
  757. }
  758. return -EINVAL;
  759. }
  760. #ifdef CONFIG_VIDEO_ADV_DEBUG
  761. static int ov772x_g_register(struct v4l2_subdev *sd,
  762. struct v4l2_dbg_register *reg)
  763. {
  764. struct ov772x_priv *priv = to_ov772x(sd);
  765. int ret;
  766. unsigned int val;
  767. reg->size = 1;
  768. if (reg->reg > 0xff)
  769. return -EINVAL;
  770. ret = regmap_read(priv->regmap, reg->reg, &val);
  771. if (ret < 0)
  772. return ret;
  773. reg->val = (__u64)val;
  774. return 0;
  775. }
  776. static int ov772x_s_register(struct v4l2_subdev *sd,
  777. const struct v4l2_dbg_register *reg)
  778. {
  779. struct ov772x_priv *priv = to_ov772x(sd);
  780. if (reg->reg > 0xff ||
  781. reg->val > 0xff)
  782. return -EINVAL;
  783. return regmap_write(priv->regmap, reg->reg, reg->val);
  784. }
  785. #endif
  786. static int ov772x_power_on(struct ov772x_priv *priv)
  787. {
  788. struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
  789. int ret;
  790. if (priv->clk) {
  791. ret = clk_prepare_enable(priv->clk);
  792. if (ret)
  793. return ret;
  794. }
  795. if (priv->pwdn_gpio) {
  796. gpiod_set_value(priv->pwdn_gpio, 1);
  797. usleep_range(500, 1000);
  798. }
  799. /*
  800. * FIXME: The reset signal is connected to a shared GPIO on some
  801. * platforms (namely the SuperH Migo-R). Until a framework becomes
  802. * available to handle this cleanly, request the GPIO temporarily
  803. * to avoid conflicts.
  804. */
  805. priv->rstb_gpio = gpiod_get_optional(&client->dev, "reset",
  806. GPIOD_OUT_LOW);
  807. if (IS_ERR(priv->rstb_gpio)) {
  808. dev_info(&client->dev, "Unable to get GPIO \"reset\"");
  809. clk_disable_unprepare(priv->clk);
  810. return PTR_ERR(priv->rstb_gpio);
  811. }
  812. if (priv->rstb_gpio) {
  813. gpiod_set_value(priv->rstb_gpio, 1);
  814. usleep_range(500, 1000);
  815. gpiod_set_value(priv->rstb_gpio, 0);
  816. usleep_range(500, 1000);
  817. gpiod_put(priv->rstb_gpio);
  818. }
  819. return 0;
  820. }
  821. static int ov772x_power_off(struct ov772x_priv *priv)
  822. {
  823. clk_disable_unprepare(priv->clk);
  824. if (priv->pwdn_gpio) {
  825. gpiod_set_value(priv->pwdn_gpio, 0);
  826. usleep_range(500, 1000);
  827. }
  828. return 0;
  829. }
  830. static int ov772x_set_params(struct ov772x_priv *priv,
  831. const struct ov772x_color_format *cfmt,
  832. const struct ov772x_win_size *win);
  833. static int ov772x_s_power(struct v4l2_subdev *sd, int on)
  834. {
  835. struct ov772x_priv *priv = to_ov772x(sd);
  836. int ret = 0;
  837. mutex_lock(&priv->lock);
  838. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  839. * update the power state.
  840. */
  841. if (priv->power_count == !on) {
  842. if (on) {
  843. ret = ov772x_power_on(priv);
  844. /*
  845. * Restore the format, the frame rate, and
  846. * the controls
  847. */
  848. if (!ret)
  849. ret = ov772x_set_params(priv, priv->cfmt,
  850. priv->win);
  851. } else {
  852. ret = ov772x_power_off(priv);
  853. }
  854. }
  855. if (!ret) {
  856. /* Update the power count. */
  857. priv->power_count += on ? 1 : -1;
  858. WARN(priv->power_count < 0, "Unbalanced power count\n");
  859. WARN(priv->power_count > 1, "Duplicated s_power call\n");
  860. }
  861. mutex_unlock(&priv->lock);
  862. return ret;
  863. }
  864. static const struct ov772x_win_size *ov772x_select_win(u32 width, u32 height)
  865. {
  866. const struct ov772x_win_size *win = &ov772x_win_sizes[0];
  867. u32 best_diff = UINT_MAX;
  868. unsigned int i;
  869. for (i = 0; i < ARRAY_SIZE(ov772x_win_sizes); ++i) {
  870. u32 diff = abs(width - ov772x_win_sizes[i].rect.width)
  871. + abs(height - ov772x_win_sizes[i].rect.height);
  872. if (diff < best_diff) {
  873. best_diff = diff;
  874. win = &ov772x_win_sizes[i];
  875. }
  876. }
  877. return win;
  878. }
  879. static void ov772x_select_params(const struct v4l2_mbus_framefmt *mf,
  880. const struct ov772x_color_format **cfmt,
  881. const struct ov772x_win_size **win)
  882. {
  883. unsigned int i;
  884. /* Select a format. */
  885. *cfmt = &ov772x_cfmts[0];
  886. for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++) {
  887. if (mf->code == ov772x_cfmts[i].code) {
  888. *cfmt = &ov772x_cfmts[i];
  889. break;
  890. }
  891. }
  892. /* Select a window size. */
  893. *win = ov772x_select_win(mf->width, mf->height);
  894. }
  895. static int ov772x_edgectrl(struct ov772x_priv *priv)
  896. {
  897. struct regmap *regmap = priv->regmap;
  898. int ret;
  899. if (!priv->info)
  900. return 0;
  901. if (priv->info->edgectrl.strength & OV772X_MANUAL_EDGE_CTRL) {
  902. /*
  903. * Manual Edge Control Mode.
  904. *
  905. * Edge auto strength bit is set by default.
  906. * Remove it when manual mode.
  907. */
  908. ret = regmap_update_bits(regmap, DSPAUTO, EDGE_ACTRL, 0x00);
  909. if (ret < 0)
  910. return ret;
  911. ret = regmap_update_bits(regmap, EDGE_TRSHLD,
  912. OV772X_EDGE_THRESHOLD_MASK,
  913. priv->info->edgectrl.threshold);
  914. if (ret < 0)
  915. return ret;
  916. ret = regmap_update_bits(regmap, EDGE_STRNGT,
  917. OV772X_EDGE_STRENGTH_MASK,
  918. priv->info->edgectrl.strength);
  919. if (ret < 0)
  920. return ret;
  921. } else if (priv->info->edgectrl.upper > priv->info->edgectrl.lower) {
  922. /*
  923. * Auto Edge Control Mode.
  924. *
  925. * Set upper and lower limit.
  926. */
  927. ret = regmap_update_bits(regmap, EDGE_UPPER,
  928. OV772X_EDGE_UPPER_MASK,
  929. priv->info->edgectrl.upper);
  930. if (ret < 0)
  931. return ret;
  932. ret = regmap_update_bits(regmap, EDGE_LOWER,
  933. OV772X_EDGE_LOWER_MASK,
  934. priv->info->edgectrl.lower);
  935. if (ret < 0)
  936. return ret;
  937. }
  938. return 0;
  939. }
  940. static int ov772x_set_params(struct ov772x_priv *priv,
  941. const struct ov772x_color_format *cfmt,
  942. const struct ov772x_win_size *win)
  943. {
  944. int ret;
  945. u8 val;
  946. /* Reset hardware. */
  947. ov772x_reset(priv);
  948. /* Edge Ctrl. */
  949. ret = ov772x_edgectrl(priv);
  950. if (ret < 0)
  951. return ret;
  952. /* Format and window size. */
  953. ret = regmap_write(priv->regmap, HSTART, win->rect.left >> 2);
  954. if (ret < 0)
  955. goto ov772x_set_fmt_error;
  956. ret = regmap_write(priv->regmap, HSIZE, win->rect.width >> 2);
  957. if (ret < 0)
  958. goto ov772x_set_fmt_error;
  959. ret = regmap_write(priv->regmap, VSTART, win->rect.top >> 1);
  960. if (ret < 0)
  961. goto ov772x_set_fmt_error;
  962. ret = regmap_write(priv->regmap, VSIZE, win->rect.height >> 1);
  963. if (ret < 0)
  964. goto ov772x_set_fmt_error;
  965. ret = regmap_write(priv->regmap, HOUTSIZE, win->rect.width >> 2);
  966. if (ret < 0)
  967. goto ov772x_set_fmt_error;
  968. ret = regmap_write(priv->regmap, VOUTSIZE, win->rect.height >> 1);
  969. if (ret < 0)
  970. goto ov772x_set_fmt_error;
  971. ret = regmap_write(priv->regmap, HREF,
  972. ((win->rect.top & 1) << HREF_VSTART_SHIFT) |
  973. ((win->rect.left & 3) << HREF_HSTART_SHIFT) |
  974. ((win->rect.height & 1) << HREF_VSIZE_SHIFT) |
  975. ((win->rect.width & 3) << HREF_HSIZE_SHIFT));
  976. if (ret < 0)
  977. goto ov772x_set_fmt_error;
  978. ret = regmap_write(priv->regmap, EXHCH,
  979. ((win->rect.height & 1) << EXHCH_VSIZE_SHIFT) |
  980. ((win->rect.width & 3) << EXHCH_HSIZE_SHIFT));
  981. if (ret < 0)
  982. goto ov772x_set_fmt_error;
  983. /* Set DSP_CTRL3. */
  984. val = cfmt->dsp3;
  985. if (val) {
  986. ret = regmap_update_bits(priv->regmap, DSP_CTRL3, UV_MASK, val);
  987. if (ret < 0)
  988. goto ov772x_set_fmt_error;
  989. }
  990. /* DSP_CTRL4: AEC reference point and DSP output format. */
  991. if (cfmt->dsp4) {
  992. ret = regmap_write(priv->regmap, DSP_CTRL4, cfmt->dsp4);
  993. if (ret < 0)
  994. goto ov772x_set_fmt_error;
  995. }
  996. /* Set COM3. */
  997. val = cfmt->com3;
  998. if (priv->info && (priv->info->flags & OV772X_FLAG_VFLIP))
  999. val |= VFLIP_IMG;
  1000. if (priv->info && (priv->info->flags & OV772X_FLAG_HFLIP))
  1001. val |= HFLIP_IMG;
  1002. if (priv->vflip_ctrl->val)
  1003. val ^= VFLIP_IMG;
  1004. if (priv->hflip_ctrl->val)
  1005. val ^= HFLIP_IMG;
  1006. if (priv->test_pattern)
  1007. val |= SCOLOR_TEST;
  1008. ret = regmap_update_bits(priv->regmap, COM3, SWAP_MASK | IMG_MASK, val);
  1009. if (ret < 0)
  1010. goto ov772x_set_fmt_error;
  1011. /* COM7: Sensor resolution and output format control. */
  1012. ret = regmap_write(priv->regmap, COM7, win->com7_bit | cfmt->com7);
  1013. if (ret < 0)
  1014. goto ov772x_set_fmt_error;
  1015. /* COM4, CLKRC: Set pixel clock and framerate. */
  1016. ret = ov772x_set_frame_rate(priv, priv->fps, cfmt, win);
  1017. if (ret < 0)
  1018. goto ov772x_set_fmt_error;
  1019. /* Set COM8. */
  1020. if (priv->band_filter_ctrl->val) {
  1021. unsigned short band_filter = priv->band_filter_ctrl->val;
  1022. ret = regmap_update_bits(priv->regmap, COM8,
  1023. BNDF_ON_OFF, BNDF_ON_OFF);
  1024. if (!ret)
  1025. ret = regmap_update_bits(priv->regmap, BDBASE,
  1026. 0xff, 256 - band_filter);
  1027. if (ret < 0)
  1028. goto ov772x_set_fmt_error;
  1029. }
  1030. return ret;
  1031. ov772x_set_fmt_error:
  1032. ov772x_reset(priv);
  1033. return ret;
  1034. }
  1035. static int ov772x_get_selection(struct v4l2_subdev *sd,
  1036. struct v4l2_subdev_state *sd_state,
  1037. struct v4l2_subdev_selection *sel)
  1038. {
  1039. struct ov772x_priv *priv = to_ov772x(sd);
  1040. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  1041. return -EINVAL;
  1042. sel->r.left = 0;
  1043. sel->r.top = 0;
  1044. switch (sel->target) {
  1045. case V4L2_SEL_TGT_CROP_BOUNDS:
  1046. case V4L2_SEL_TGT_CROP:
  1047. sel->r.width = priv->win->rect.width;
  1048. sel->r.height = priv->win->rect.height;
  1049. return 0;
  1050. default:
  1051. return -EINVAL;
  1052. }
  1053. }
  1054. static int ov772x_get_fmt(struct v4l2_subdev *sd,
  1055. struct v4l2_subdev_state *sd_state,
  1056. struct v4l2_subdev_format *format)
  1057. {
  1058. struct v4l2_mbus_framefmt *mf = &format->format;
  1059. struct ov772x_priv *priv = to_ov772x(sd);
  1060. if (format->pad)
  1061. return -EINVAL;
  1062. mf->width = priv->win->rect.width;
  1063. mf->height = priv->win->rect.height;
  1064. mf->code = priv->cfmt->code;
  1065. mf->colorspace = priv->cfmt->colorspace;
  1066. mf->field = V4L2_FIELD_NONE;
  1067. return 0;
  1068. }
  1069. static int ov772x_set_fmt(struct v4l2_subdev *sd,
  1070. struct v4l2_subdev_state *sd_state,
  1071. struct v4l2_subdev_format *format)
  1072. {
  1073. struct ov772x_priv *priv = to_ov772x(sd);
  1074. struct v4l2_mbus_framefmt *mf = &format->format;
  1075. const struct ov772x_color_format *cfmt;
  1076. const struct ov772x_win_size *win;
  1077. int ret = 0;
  1078. if (format->pad)
  1079. return -EINVAL;
  1080. ov772x_select_params(mf, &cfmt, &win);
  1081. mf->code = cfmt->code;
  1082. mf->width = win->rect.width;
  1083. mf->height = win->rect.height;
  1084. mf->field = V4L2_FIELD_NONE;
  1085. mf->colorspace = cfmt->colorspace;
  1086. mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1087. mf->quantization = V4L2_QUANTIZATION_DEFAULT;
  1088. mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  1089. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1090. *v4l2_subdev_state_get_format(sd_state, 0) = *mf;
  1091. return 0;
  1092. }
  1093. mutex_lock(&priv->lock);
  1094. if (priv->streaming) {
  1095. ret = -EBUSY;
  1096. goto error;
  1097. }
  1098. /*
  1099. * If the device is not powered up by the host driver do
  1100. * not apply any changes to H/W at this time. Instead
  1101. * the format will be restored right after power-up.
  1102. */
  1103. if (priv->power_count > 0) {
  1104. ret = ov772x_set_params(priv, cfmt, win);
  1105. if (ret < 0)
  1106. goto error;
  1107. }
  1108. priv->win = win;
  1109. priv->cfmt = cfmt;
  1110. error:
  1111. mutex_unlock(&priv->lock);
  1112. return ret;
  1113. }
  1114. static int ov772x_video_probe(struct ov772x_priv *priv)
  1115. {
  1116. struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
  1117. int pid, ver, midh, midl;
  1118. const char *devname;
  1119. int ret;
  1120. ret = ov772x_power_on(priv);
  1121. if (ret < 0)
  1122. return ret;
  1123. /* Check and show product ID and manufacturer ID. */
  1124. ret = regmap_read(priv->regmap, PID, &pid);
  1125. if (ret < 0)
  1126. return ret;
  1127. ret = regmap_read(priv->regmap, VER, &ver);
  1128. if (ret < 0)
  1129. return ret;
  1130. switch (VERSION(pid, ver)) {
  1131. case OV7720:
  1132. devname = "ov7720";
  1133. break;
  1134. case OV7725:
  1135. devname = "ov7725";
  1136. break;
  1137. default:
  1138. dev_err(&client->dev,
  1139. "Product ID error %x:%x\n", pid, ver);
  1140. ret = -ENODEV;
  1141. goto done;
  1142. }
  1143. ret = regmap_read(priv->regmap, MIDH, &midh);
  1144. if (ret < 0)
  1145. return ret;
  1146. ret = regmap_read(priv->regmap, MIDL, &midl);
  1147. if (ret < 0)
  1148. return ret;
  1149. dev_info(&client->dev,
  1150. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  1151. devname, pid, ver, midh, midl);
  1152. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  1153. done:
  1154. ov772x_power_off(priv);
  1155. return ret;
  1156. }
  1157. static const struct v4l2_ctrl_ops ov772x_ctrl_ops = {
  1158. .s_ctrl = ov772x_s_ctrl,
  1159. };
  1160. static const struct v4l2_subdev_core_ops ov772x_subdev_core_ops = {
  1161. .log_status = v4l2_ctrl_subdev_log_status,
  1162. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  1163. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1164. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1165. .g_register = ov772x_g_register,
  1166. .s_register = ov772x_s_register,
  1167. #endif
  1168. .s_power = ov772x_s_power,
  1169. };
  1170. static int ov772x_enum_frame_interval(struct v4l2_subdev *sd,
  1171. struct v4l2_subdev_state *sd_state,
  1172. struct v4l2_subdev_frame_interval_enum *fie)
  1173. {
  1174. if (fie->pad || fie->index >= ARRAY_SIZE(ov772x_frame_intervals))
  1175. return -EINVAL;
  1176. if (fie->width != VGA_WIDTH && fie->width != QVGA_WIDTH)
  1177. return -EINVAL;
  1178. if (fie->height != VGA_HEIGHT && fie->height != QVGA_HEIGHT)
  1179. return -EINVAL;
  1180. fie->interval.numerator = 1;
  1181. fie->interval.denominator = ov772x_frame_intervals[fie->index];
  1182. return 0;
  1183. }
  1184. static int ov772x_enum_mbus_code(struct v4l2_subdev *sd,
  1185. struct v4l2_subdev_state *sd_state,
  1186. struct v4l2_subdev_mbus_code_enum *code)
  1187. {
  1188. if (code->pad || code->index >= ARRAY_SIZE(ov772x_cfmts))
  1189. return -EINVAL;
  1190. code->code = ov772x_cfmts[code->index].code;
  1191. return 0;
  1192. }
  1193. static const struct v4l2_subdev_video_ops ov772x_subdev_video_ops = {
  1194. .s_stream = ov772x_s_stream,
  1195. };
  1196. static const struct v4l2_subdev_pad_ops ov772x_subdev_pad_ops = {
  1197. .enum_frame_interval = ov772x_enum_frame_interval,
  1198. .enum_mbus_code = ov772x_enum_mbus_code,
  1199. .get_selection = ov772x_get_selection,
  1200. .get_fmt = ov772x_get_fmt,
  1201. .set_fmt = ov772x_set_fmt,
  1202. .get_frame_interval = ov772x_get_frame_interval,
  1203. .set_frame_interval = ov772x_set_frame_interval,
  1204. };
  1205. static const struct v4l2_subdev_ops ov772x_subdev_ops = {
  1206. .core = &ov772x_subdev_core_ops,
  1207. .video = &ov772x_subdev_video_ops,
  1208. .pad = &ov772x_subdev_pad_ops,
  1209. };
  1210. static int ov772x_parse_dt(struct i2c_client *client,
  1211. struct ov772x_priv *priv)
  1212. {
  1213. struct v4l2_fwnode_endpoint bus_cfg = {
  1214. .bus_type = V4L2_MBUS_PARALLEL
  1215. };
  1216. struct fwnode_handle *ep;
  1217. int ret;
  1218. ep = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), NULL);
  1219. if (!ep) {
  1220. dev_err(&client->dev, "Endpoint node not found\n");
  1221. return -EINVAL;
  1222. }
  1223. /*
  1224. * For backward compatibility with older DTS where the
  1225. * bus-type property was not mandatory, assume
  1226. * V4L2_MBUS_PARALLEL as it was the only supported bus at the
  1227. * time. v4l2_fwnode_endpoint_alloc_parse() will not fail if
  1228. * 'bus-type' is not specified.
  1229. */
  1230. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1231. if (ret) {
  1232. bus_cfg = (struct v4l2_fwnode_endpoint)
  1233. { .bus_type = V4L2_MBUS_BT656 };
  1234. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1235. if (ret)
  1236. goto error_fwnode_put;
  1237. }
  1238. priv->bus_type = bus_cfg.bus_type;
  1239. v4l2_fwnode_endpoint_free(&bus_cfg);
  1240. error_fwnode_put:
  1241. fwnode_handle_put(ep);
  1242. return ret;
  1243. }
  1244. /*
  1245. * i2c_driver function
  1246. */
  1247. static int ov772x_probe(struct i2c_client *client)
  1248. {
  1249. struct ov772x_priv *priv;
  1250. int ret;
  1251. static const struct regmap_config ov772x_regmap_config = {
  1252. .reg_bits = 8,
  1253. .val_bits = 8,
  1254. .max_register = DSPAUTO,
  1255. };
  1256. if (!client->dev.of_node && !client->dev.platform_data) {
  1257. dev_err(&client->dev,
  1258. "Missing ov772x platform data for non-DT device\n");
  1259. return -EINVAL;
  1260. }
  1261. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  1262. if (!priv)
  1263. return -ENOMEM;
  1264. priv->regmap = devm_regmap_init_sccb(client, &ov772x_regmap_config);
  1265. if (IS_ERR(priv->regmap)) {
  1266. dev_err(&client->dev, "Failed to allocate register map\n");
  1267. return PTR_ERR(priv->regmap);
  1268. }
  1269. priv->info = client->dev.platform_data;
  1270. mutex_init(&priv->lock);
  1271. v4l2_i2c_subdev_init(&priv->subdev, client, &ov772x_subdev_ops);
  1272. priv->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  1273. V4L2_SUBDEV_FL_HAS_EVENTS;
  1274. v4l2_ctrl_handler_init(&priv->hdl, 3);
  1275. /* Use our mutex for the controls */
  1276. priv->hdl.lock = &priv->lock;
  1277. priv->vflip_ctrl = v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
  1278. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1279. priv->hflip_ctrl = v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
  1280. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1281. priv->band_filter_ctrl = v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
  1282. V4L2_CID_BAND_STOP_FILTER,
  1283. 0, 256, 1, 0);
  1284. v4l2_ctrl_new_std_menu_items(&priv->hdl, &ov772x_ctrl_ops,
  1285. V4L2_CID_TEST_PATTERN,
  1286. ARRAY_SIZE(ov772x_test_pattern_menu) - 1,
  1287. 0, 0, ov772x_test_pattern_menu);
  1288. priv->subdev.ctrl_handler = &priv->hdl;
  1289. if (priv->hdl.error) {
  1290. ret = priv->hdl.error;
  1291. goto error_ctrl_free;
  1292. }
  1293. priv->clk = clk_get(&client->dev, NULL);
  1294. if (IS_ERR(priv->clk)) {
  1295. dev_err(&client->dev, "Unable to get xclk clock\n");
  1296. ret = PTR_ERR(priv->clk);
  1297. goto error_ctrl_free;
  1298. }
  1299. priv->pwdn_gpio = gpiod_get_optional(&client->dev, "powerdown",
  1300. GPIOD_OUT_LOW);
  1301. if (IS_ERR(priv->pwdn_gpio)) {
  1302. dev_info(&client->dev, "Unable to get GPIO \"powerdown\"");
  1303. ret = PTR_ERR(priv->pwdn_gpio);
  1304. goto error_clk_put;
  1305. }
  1306. ret = ov772x_parse_dt(client, priv);
  1307. if (ret)
  1308. goto error_clk_put;
  1309. ret = ov772x_video_probe(priv);
  1310. if (ret < 0)
  1311. goto error_gpio_put;
  1312. priv->pad.flags = MEDIA_PAD_FL_SOURCE;
  1313. priv->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1314. ret = media_entity_pads_init(&priv->subdev.entity, 1, &priv->pad);
  1315. if (ret < 0)
  1316. goto error_gpio_put;
  1317. priv->cfmt = &ov772x_cfmts[0];
  1318. priv->win = &ov772x_win_sizes[0];
  1319. priv->fps = 15;
  1320. ret = v4l2_async_register_subdev(&priv->subdev);
  1321. if (ret)
  1322. goto error_entity_cleanup;
  1323. return 0;
  1324. error_entity_cleanup:
  1325. media_entity_cleanup(&priv->subdev.entity);
  1326. error_gpio_put:
  1327. if (priv->pwdn_gpio)
  1328. gpiod_put(priv->pwdn_gpio);
  1329. error_clk_put:
  1330. clk_put(priv->clk);
  1331. error_ctrl_free:
  1332. v4l2_ctrl_handler_free(&priv->hdl);
  1333. mutex_destroy(&priv->lock);
  1334. return ret;
  1335. }
  1336. static void ov772x_remove(struct i2c_client *client)
  1337. {
  1338. struct ov772x_priv *priv = to_ov772x(i2c_get_clientdata(client));
  1339. media_entity_cleanup(&priv->subdev.entity);
  1340. clk_put(priv->clk);
  1341. if (priv->pwdn_gpio)
  1342. gpiod_put(priv->pwdn_gpio);
  1343. v4l2_async_unregister_subdev(&priv->subdev);
  1344. v4l2_ctrl_handler_free(&priv->hdl);
  1345. mutex_destroy(&priv->lock);
  1346. }
  1347. static const struct i2c_device_id ov772x_id[] = {
  1348. { "ov772x" },
  1349. { }
  1350. };
  1351. MODULE_DEVICE_TABLE(i2c, ov772x_id);
  1352. static const struct of_device_id ov772x_of_match[] = {
  1353. { .compatible = "ovti,ov7725", },
  1354. { .compatible = "ovti,ov7720", },
  1355. { /* sentinel */ },
  1356. };
  1357. MODULE_DEVICE_TABLE(of, ov772x_of_match);
  1358. static struct i2c_driver ov772x_i2c_driver = {
  1359. .driver = {
  1360. .name = "ov772x",
  1361. .of_match_table = ov772x_of_match,
  1362. },
  1363. .probe = ov772x_probe,
  1364. .remove = ov772x_remove,
  1365. .id_table = ov772x_id,
  1366. };
  1367. module_i2c_driver(ov772x_i2c_driver);
  1368. MODULE_DESCRIPTION("V4L2 driver for OV772x image sensor");
  1369. MODULE_AUTHOR("Kuninori Morimoto");
  1370. MODULE_LICENSE("GPL v2");