ov9650.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Omnivision OV9650/OV9652 CMOS Image Sensor driver
  4. *
  5. * Copyright (C) 2013, Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
  6. *
  7. * Register definitions and initial settings based on a driver written
  8. * by Vladimir Fonov.
  9. * Copyright (c) 2010, Vladimir Fonov
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/kernel.h>
  16. #include <linux/media.h>
  17. #include <linux/module.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/videodev2.h>
  23. #include <media/media-entity.h>
  24. #include <media/v4l2-async.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-event.h>
  28. #include <media/v4l2-image-sizes.h>
  29. #include <media/v4l2-subdev.h>
  30. #include <media/v4l2-mediabus.h>
  31. static int debug;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  34. #define DRIVER_NAME "OV9650"
  35. /*
  36. * OV9650/OV9652 register definitions
  37. */
  38. #define REG_GAIN 0x00 /* Gain control, AGC[7:0] */
  39. #define REG_BLUE 0x01 /* AWB - Blue channel gain */
  40. #define REG_RED 0x02 /* AWB - Red channel gain */
  41. #define REG_VREF 0x03 /* [7:6] - AGC[9:8], [5:3]/[2:0] */
  42. #define VREF_GAIN_MASK 0xc0 /* - VREF end/start low 3 bits */
  43. #define REG_COM1 0x04
  44. #define COM1_CCIR656 0x40
  45. #define REG_B_AVE 0x05
  46. #define REG_GB_AVE 0x06
  47. #define REG_GR_AVE 0x07
  48. #define REG_R_AVE 0x08
  49. #define REG_COM2 0x09
  50. #define REG_PID 0x0a /* Product ID MSB */
  51. #define REG_VER 0x0b /* Product ID LSB */
  52. #define REG_COM3 0x0c
  53. #define COM3_SWAP 0x40
  54. #define COM3_VARIOPIXEL1 0x04
  55. #define REG_COM4 0x0d /* Vario Pixels */
  56. #define COM4_VARIOPIXEL2 0x80
  57. #define REG_COM5 0x0e /* System clock options */
  58. #define COM5_SLAVE_MODE 0x10
  59. #define COM5_SYSTEMCLOCK48MHZ 0x80
  60. #define REG_COM6 0x0f /* HREF & ADBLC options */
  61. #define REG_AECH 0x10 /* Exposure value, AEC[9:2] */
  62. #define REG_CLKRC 0x11 /* Clock control */
  63. #define CLK_EXT 0x40 /* Use external clock directly */
  64. #define CLK_SCALE 0x3f /* Mask for internal clock scale */
  65. #define REG_COM7 0x12 /* SCCB reset, output format */
  66. #define COM7_RESET 0x80
  67. #define COM7_FMT_MASK 0x38
  68. #define COM7_FMT_VGA 0x40
  69. #define COM7_FMT_CIF 0x20
  70. #define COM7_FMT_QVGA 0x10
  71. #define COM7_FMT_QCIF 0x08
  72. #define COM7_RGB 0x04
  73. #define COM7_YUV 0x00
  74. #define COM7_BAYER 0x01
  75. #define COM7_PBAYER 0x05
  76. #define REG_COM8 0x13 /* AGC/AEC options */
  77. #define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
  78. #define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
  79. #define COM8_BFILT 0x20 /* Band filter enable */
  80. #define COM8_AGC 0x04 /* Auto gain enable */
  81. #define COM8_AWB 0x02 /* White balance enable */
  82. #define COM8_AEC 0x01 /* Auto exposure enable */
  83. #define REG_COM9 0x14 /* Gain ceiling */
  84. #define COM9_GAIN_CEIL_MASK 0x70 /* */
  85. #define REG_COM10 0x15 /* PCLK, HREF, HSYNC signals polarity */
  86. #define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
  87. #define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
  88. #define COM10_HREF_REV 0x08 /* Reverse HREF */
  89. #define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
  90. #define COM10_VS_NEG 0x02 /* VSYNC negative */
  91. #define COM10_HS_NEG 0x01 /* HSYNC negative */
  92. #define REG_HSTART 0x17 /* Horiz start high bits */
  93. #define REG_HSTOP 0x18 /* Horiz stop high bits */
  94. #define REG_VSTART 0x19 /* Vert start high bits */
  95. #define REG_VSTOP 0x1a /* Vert stop high bits */
  96. #define REG_PSHFT 0x1b /* Pixel delay after HREF */
  97. #define REG_MIDH 0x1c /* Manufacturer ID MSB */
  98. #define REG_MIDL 0x1d /* Manufufacturer ID LSB */
  99. #define REG_MVFP 0x1e /* Image mirror/flip */
  100. #define MVFP_MIRROR 0x20 /* Mirror image */
  101. #define MVFP_FLIP 0x10 /* Vertical flip */
  102. #define REG_BOS 0x20 /* B channel Offset */
  103. #define REG_GBOS 0x21 /* Gb channel Offset */
  104. #define REG_GROS 0x22 /* Gr channel Offset */
  105. #define REG_ROS 0x23 /* R channel Offset */
  106. #define REG_AEW 0x24 /* AGC upper limit */
  107. #define REG_AEB 0x25 /* AGC lower limit */
  108. #define REG_VPT 0x26 /* AGC/AEC fast mode op region */
  109. #define REG_BBIAS 0x27 /* B channel output bias */
  110. #define REG_GBBIAS 0x28 /* Gb channel output bias */
  111. #define REG_GRCOM 0x29 /* Analog BLC & regulator */
  112. #define REG_EXHCH 0x2a /* Dummy pixel insert MSB */
  113. #define REG_EXHCL 0x2b /* Dummy pixel insert LSB */
  114. #define REG_RBIAS 0x2c /* R channel output bias */
  115. #define REG_ADVFL 0x2d /* LSB of dummy line insert */
  116. #define REG_ADVFH 0x2e /* MSB of dummy line insert */
  117. #define REG_YAVE 0x2f /* Y/G channel average value */
  118. #define REG_HSYST 0x30 /* HSYNC rising edge delay LSB*/
  119. #define REG_HSYEN 0x31 /* HSYNC falling edge delay LSB*/
  120. #define REG_HREF 0x32 /* HREF pieces */
  121. #define REG_CHLF 0x33 /* reserved */
  122. #define REG_ADC 0x37 /* reserved */
  123. #define REG_ACOM 0x38 /* reserved */
  124. #define REG_OFON 0x39 /* Power down register */
  125. #define OFON_PWRDN 0x08 /* Power down bit */
  126. #define REG_TSLB 0x3a /* YUVU format */
  127. #define TSLB_YUYV_MASK 0x0c /* UYVY or VYUY - see com13 */
  128. #define REG_COM11 0x3b /* Night mode, banding filter enable */
  129. #define COM11_NIGHT 0x80 /* Night mode enable */
  130. #define COM11_NMFR 0x60 /* Two bit NM frame rate */
  131. #define COM11_BANDING 0x01 /* Banding filter */
  132. #define COM11_AEC_REF_MASK 0x18 /* AEC reference area selection */
  133. #define REG_COM12 0x3c /* HREF option, UV average */
  134. #define COM12_HREF 0x80 /* HREF always */
  135. #define REG_COM13 0x3d /* Gamma selection, Color matrix en. */
  136. #define COM13_GAMMA 0x80 /* Gamma enable */
  137. #define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
  138. #define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
  139. #define REG_COM14 0x3e /* Edge enhancement options */
  140. #define COM14_EDGE_EN 0x02
  141. #define COM14_EEF_X2 0x01
  142. #define REG_EDGE 0x3f /* Edge enhancement factor */
  143. #define EDGE_FACTOR_MASK 0x0f
  144. #define REG_COM15 0x40 /* Output range, RGB 555/565 */
  145. #define COM15_R10F0 0x00 /* Data range 10 to F0 */
  146. #define COM15_R01FE 0x80 /* 01 to FE */
  147. #define COM15_R00FF 0xc0 /* 00 to FF */
  148. #define COM15_RGB565 0x10 /* RGB565 output */
  149. #define COM15_RGB555 0x30 /* RGB555 output */
  150. #define COM15_SWAPRB 0x04 /* Swap R&B */
  151. #define REG_COM16 0x41 /* Color matrix coeff options */
  152. #define REG_COM17 0x42 /* Single frame out, banding filter */
  153. /* n = 1...9, 0x4f..0x57 */
  154. #define REG_MTX(__n) (0x4f + (__n) - 1)
  155. #define REG_MTXS 0x58
  156. /* Lens Correction Option 1...5, __n = 0...5 */
  157. #define REG_LCC(__n) (0x62 + (__n) - 1)
  158. #define LCC5_LCC_ENABLE 0x01 /* LCC5, enable lens correction */
  159. #define LCC5_LCC_COLOR 0x04
  160. #define REG_MANU 0x67 /* Manual U value */
  161. #define REG_MANV 0x68 /* Manual V value */
  162. #define REG_HV 0x69 /* Manual banding filter MSB */
  163. #define REG_MBD 0x6a /* Manual banding filter value */
  164. #define REG_DBLV 0x6b /* reserved */
  165. #define REG_GSP 0x6c /* Gamma curve */
  166. #define GSP_LEN 15
  167. #define REG_GST 0x7c /* Gamma curve */
  168. #define GST_LEN 15
  169. #define REG_COM21 0x8b
  170. #define REG_COM22 0x8c /* Edge enhancement, denoising */
  171. #define COM22_WHTPCOR 0x02 /* White pixel correction enable */
  172. #define COM22_WHTPCOROPT 0x01 /* White pixel correction option */
  173. #define COM22_DENOISE 0x10 /* White pixel correction option */
  174. #define REG_COM23 0x8d /* Color bar test, color gain */
  175. #define COM23_TEST_MODE 0x10
  176. #define REG_DBLC1 0x8f /* Digital BLC */
  177. #define REG_DBLC_B 0x90 /* Digital BLC B channel offset */
  178. #define REG_DBLC_R 0x91 /* Digital BLC R channel offset */
  179. #define REG_DM_LNL 0x92 /* Dummy line low 8 bits */
  180. #define REG_DM_LNH 0x93 /* Dummy line high 8 bits */
  181. #define REG_LCCFB 0x9d /* Lens Correction B channel */
  182. #define REG_LCCFR 0x9e /* Lens Correction R channel */
  183. #define REG_DBLC_GB 0x9f /* Digital BLC GB chan offset */
  184. #define REG_DBLC_GR 0xa0 /* Digital BLC GR chan offset */
  185. #define REG_AECHM 0xa1 /* Exposure value - bits AEC[15:10] */
  186. #define REG_BD50ST 0xa2 /* Banding filter value for 50Hz */
  187. #define REG_BD60ST 0xa3 /* Banding filter value for 60Hz */
  188. #define REG_NULL 0xff /* Array end token */
  189. #define DEF_CLKRC 0x80
  190. #define OV965X_ID(_msb, _lsb) ((_msb) << 8 | (_lsb))
  191. #define OV9650_ID 0x9650
  192. #define OV9652_ID 0x9652
  193. struct ov965x_ctrls {
  194. struct v4l2_ctrl_handler handler;
  195. struct {
  196. struct v4l2_ctrl *auto_exp;
  197. struct v4l2_ctrl *exposure;
  198. };
  199. struct {
  200. struct v4l2_ctrl *auto_wb;
  201. struct v4l2_ctrl *blue_balance;
  202. struct v4l2_ctrl *red_balance;
  203. };
  204. struct {
  205. struct v4l2_ctrl *hflip;
  206. struct v4l2_ctrl *vflip;
  207. };
  208. struct {
  209. struct v4l2_ctrl *auto_gain;
  210. struct v4l2_ctrl *gain;
  211. };
  212. struct v4l2_ctrl *brightness;
  213. struct v4l2_ctrl *saturation;
  214. struct v4l2_ctrl *sharpness;
  215. struct v4l2_ctrl *light_freq;
  216. u8 update;
  217. };
  218. struct ov965x_framesize {
  219. u16 width;
  220. u16 height;
  221. u16 max_exp_lines;
  222. const u8 *regs;
  223. };
  224. struct ov965x_interval {
  225. struct v4l2_fract interval;
  226. /* Maximum resolution for this interval */
  227. struct v4l2_frmsize_discrete size;
  228. u8 clkrc_div;
  229. };
  230. enum gpio_id {
  231. GPIO_PWDN,
  232. GPIO_RST,
  233. NUM_GPIOS,
  234. };
  235. struct ov965x {
  236. struct v4l2_subdev sd;
  237. struct media_pad pad;
  238. enum v4l2_mbus_type bus_type;
  239. struct gpio_desc *gpios[NUM_GPIOS];
  240. /* External master clock frequency */
  241. unsigned long mclk_frequency;
  242. struct clk *clk;
  243. /* Protects the struct fields below */
  244. struct mutex lock;
  245. struct regmap *regmap;
  246. /* Exposure row interval in us */
  247. unsigned int exp_row_interval;
  248. unsigned short id;
  249. const struct ov965x_framesize *frame_size;
  250. /* YUYV sequence (pixel format) control register */
  251. u8 tslb_reg;
  252. struct v4l2_mbus_framefmt format;
  253. struct ov965x_ctrls ctrls;
  254. /* Pointer to frame rate control data structure */
  255. const struct ov965x_interval *fiv;
  256. int streaming;
  257. int power;
  258. u8 apply_frame_fmt;
  259. };
  260. struct i2c_rv {
  261. u8 addr;
  262. u8 value;
  263. };
  264. static const struct i2c_rv ov965x_init_regs[] = {
  265. { REG_COM2, 0x10 }, /* Set soft sleep mode */
  266. { REG_COM5, 0x00 }, /* System clock options */
  267. { REG_COM2, 0x01 }, /* Output drive, soft sleep mode */
  268. { REG_COM10, 0x00 }, /* Slave mode, HREF vs HSYNC, signals negate */
  269. { REG_EDGE, 0xa6 }, /* Edge enhancement threshold and factor */
  270. { REG_COM16, 0x02 }, /* Color matrix coeff double option */
  271. { REG_COM17, 0x08 }, /* Single frame out, banding filter */
  272. { 0x16, 0x06 },
  273. { REG_CHLF, 0xc0 }, /* Reserved */
  274. { 0x34, 0xbf },
  275. { 0xa8, 0x80 },
  276. { 0x96, 0x04 },
  277. { 0x8e, 0x00 },
  278. { REG_COM12, 0x77 }, /* HREF option, UV average */
  279. { 0x8b, 0x06 },
  280. { 0x35, 0x91 },
  281. { 0x94, 0x88 },
  282. { 0x95, 0x88 },
  283. { REG_COM15, 0xc1 }, /* Output range, RGB 555/565 */
  284. { REG_GRCOM, 0x2f }, /* Analog BLC & regulator */
  285. { REG_COM6, 0x43 }, /* HREF & ADBLC options */
  286. { REG_COM8, 0xe5 }, /* AGC/AEC options */
  287. { REG_COM13, 0x90 }, /* Gamma selection, colour matrix, UV delay */
  288. { REG_HV, 0x80 }, /* Manual banding filter MSB */
  289. { 0x5c, 0x96 }, /* Reserved up to 0xa5 */
  290. { 0x5d, 0x96 },
  291. { 0x5e, 0x10 },
  292. { 0x59, 0xeb },
  293. { 0x5a, 0x9c },
  294. { 0x5b, 0x55 },
  295. { 0x43, 0xf0 },
  296. { 0x44, 0x10 },
  297. { 0x45, 0x55 },
  298. { 0x46, 0x86 },
  299. { 0x47, 0x64 },
  300. { 0x48, 0x86 },
  301. { 0x5f, 0xe0 },
  302. { 0x60, 0x8c },
  303. { 0x61, 0x20 },
  304. { 0xa5, 0xd9 },
  305. { 0xa4, 0x74 }, /* reserved */
  306. { REG_COM23, 0x02 }, /* Color gain analog/_digital_ */
  307. { REG_COM8, 0xe7 }, /* Enable AEC, AWB, AEC */
  308. { REG_COM22, 0x23 }, /* Edge enhancement, denoising */
  309. { 0xa9, 0xb8 },
  310. { 0xaa, 0x92 },
  311. { 0xab, 0x0a },
  312. { REG_DBLC1, 0xdf }, /* Digital BLC */
  313. { REG_DBLC_B, 0x00 }, /* Digital BLC B chan offset */
  314. { REG_DBLC_R, 0x00 }, /* Digital BLC R chan offset */
  315. { REG_DBLC_GB, 0x00 }, /* Digital BLC GB chan offset */
  316. { REG_DBLC_GR, 0x00 },
  317. { REG_COM9, 0x3a }, /* Gain ceiling 16x */
  318. { REG_NULL, 0 }
  319. };
  320. #define NUM_FMT_REGS 14
  321. /*
  322. * COM7, COM3, COM4, HSTART, HSTOP, HREF, VSTART, VSTOP, VREF,
  323. * EXHCH, EXHCL, ADC, OCOM, OFON
  324. */
  325. static const u8 frame_size_reg_addr[NUM_FMT_REGS] = {
  326. 0x12, 0x0c, 0x0d, 0x17, 0x18, 0x32, 0x19, 0x1a, 0x03,
  327. 0x2a, 0x2b, 0x37, 0x38, 0x39,
  328. };
  329. static const u8 ov965x_sxga_regs[NUM_FMT_REGS] = {
  330. 0x00, 0x00, 0x00, 0x1e, 0xbe, 0xbf, 0x01, 0x81, 0x12,
  331. 0x10, 0x34, 0x81, 0x93, 0x51,
  332. };
  333. static const u8 ov965x_vga_regs[NUM_FMT_REGS] = {
  334. 0x40, 0x04, 0x80, 0x26, 0xc6, 0xed, 0x01, 0x3d, 0x00,
  335. 0x10, 0x40, 0x91, 0x12, 0x43,
  336. };
  337. /* Determined empirically. */
  338. static const u8 ov965x_qvga_regs[NUM_FMT_REGS] = {
  339. 0x10, 0x04, 0x80, 0x25, 0xc5, 0xbf, 0x00, 0x80, 0x12,
  340. 0x10, 0x40, 0x91, 0x12, 0x43,
  341. };
  342. static const struct ov965x_framesize ov965x_framesizes[] = {
  343. {
  344. .width = SXGA_WIDTH,
  345. .height = SXGA_HEIGHT,
  346. .regs = ov965x_sxga_regs,
  347. .max_exp_lines = 1048,
  348. }, {
  349. .width = VGA_WIDTH,
  350. .height = VGA_HEIGHT,
  351. .regs = ov965x_vga_regs,
  352. .max_exp_lines = 498,
  353. }, {
  354. .width = QVGA_WIDTH,
  355. .height = QVGA_HEIGHT,
  356. .regs = ov965x_qvga_regs,
  357. .max_exp_lines = 248,
  358. },
  359. };
  360. struct ov965x_pixfmt {
  361. u32 code;
  362. u32 colorspace;
  363. /* REG_TSLB value, only bits [3:2] may be set. */
  364. u8 tslb_reg;
  365. };
  366. static const struct ov965x_pixfmt ov965x_formats[] = {
  367. { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG, 0x00},
  368. { MEDIA_BUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG, 0x04},
  369. { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG, 0x0c},
  370. { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 0x08},
  371. };
  372. /*
  373. * This table specifies possible frame resolution and interval
  374. * combinations. Default CLKRC[5:0] divider values are valid
  375. * only for 24 MHz external clock frequency.
  376. */
  377. static struct ov965x_interval ov965x_intervals[] = {
  378. {{ 100, 625 }, { SXGA_WIDTH, SXGA_HEIGHT }, 0 }, /* 6.25 fps */
  379. {{ 10, 125 }, { VGA_WIDTH, VGA_HEIGHT }, 1 }, /* 12.5 fps */
  380. {{ 10, 125 }, { QVGA_WIDTH, QVGA_HEIGHT }, 3 }, /* 12.5 fps */
  381. {{ 1, 25 }, { VGA_WIDTH, VGA_HEIGHT }, 0 }, /* 25 fps */
  382. {{ 1, 25 }, { QVGA_WIDTH, QVGA_HEIGHT }, 1 }, /* 25 fps */
  383. };
  384. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  385. {
  386. return &container_of(ctrl->handler, struct ov965x, ctrls.handler)->sd;
  387. }
  388. static inline struct ov965x *to_ov965x(struct v4l2_subdev *sd)
  389. {
  390. return container_of(sd, struct ov965x, sd);
  391. }
  392. static int ov965x_read(struct ov965x *ov965x, u8 addr, u8 *val)
  393. {
  394. int ret;
  395. unsigned int buf;
  396. ret = regmap_read(ov965x->regmap, addr, &buf);
  397. if (!ret)
  398. *val = buf;
  399. else
  400. *val = -1;
  401. v4l2_dbg(2, debug, &ov965x->sd, "%s: 0x%02x @ 0x%02x. (%d)\n",
  402. __func__, *val, addr, ret);
  403. return ret;
  404. }
  405. static int ov965x_write(struct ov965x *ov965x, u8 addr, u8 val)
  406. {
  407. int ret;
  408. ret = regmap_write(ov965x->regmap, addr, val);
  409. v4l2_dbg(2, debug, &ov965x->sd, "%s: 0x%02x @ 0x%02X (%d)\n",
  410. __func__, val, addr, ret);
  411. return ret;
  412. }
  413. static int ov965x_write_array(struct ov965x *ov965x,
  414. const struct i2c_rv *regs)
  415. {
  416. int i, ret = 0;
  417. for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
  418. ret = ov965x_write(ov965x, regs[i].addr, regs[i].value);
  419. return ret;
  420. }
  421. static int ov965x_set_default_gamma_curve(struct ov965x *ov965x)
  422. {
  423. static const u8 gamma_curve[] = {
  424. /* Values taken from OV application note. */
  425. 0x40, 0x30, 0x4b, 0x60, 0x70, 0x70, 0x70, 0x70,
  426. 0x60, 0x60, 0x50, 0x48, 0x3a, 0x2e, 0x28, 0x22,
  427. 0x04, 0x07, 0x10, 0x28, 0x36, 0x44, 0x52, 0x60,
  428. 0x6c, 0x78, 0x8c, 0x9e, 0xbb, 0xd2, 0xe6
  429. };
  430. u8 addr = REG_GSP;
  431. unsigned int i;
  432. for (i = 0; i < ARRAY_SIZE(gamma_curve); i++) {
  433. int ret = ov965x_write(ov965x, addr, gamma_curve[i]);
  434. if (ret < 0)
  435. return ret;
  436. addr++;
  437. }
  438. return 0;
  439. };
  440. static int ov965x_set_color_matrix(struct ov965x *ov965x)
  441. {
  442. static const u8 mtx[] = {
  443. /* MTX1..MTX9, MTXS */
  444. 0x3a, 0x3d, 0x03, 0x12, 0x26, 0x38, 0x40, 0x40, 0x40, 0x0d
  445. };
  446. u8 addr = REG_MTX(1);
  447. unsigned int i;
  448. for (i = 0; i < ARRAY_SIZE(mtx); i++) {
  449. int ret = ov965x_write(ov965x, addr, mtx[i]);
  450. if (ret < 0)
  451. return ret;
  452. addr++;
  453. }
  454. return 0;
  455. }
  456. static int __ov965x_set_power(struct ov965x *ov965x, int on)
  457. {
  458. if (on) {
  459. int ret = clk_prepare_enable(ov965x->clk);
  460. if (ret)
  461. return ret;
  462. gpiod_set_value_cansleep(ov965x->gpios[GPIO_PWDN], 0);
  463. gpiod_set_value_cansleep(ov965x->gpios[GPIO_RST], 0);
  464. msleep(25);
  465. } else {
  466. gpiod_set_value_cansleep(ov965x->gpios[GPIO_RST], 1);
  467. gpiod_set_value_cansleep(ov965x->gpios[GPIO_PWDN], 1);
  468. clk_disable_unprepare(ov965x->clk);
  469. }
  470. ov965x->streaming = 0;
  471. return 0;
  472. }
  473. static int ov965x_s_power(struct v4l2_subdev *sd, int on)
  474. {
  475. struct ov965x *ov965x = to_ov965x(sd);
  476. int ret = 0;
  477. v4l2_dbg(1, debug, sd, "%s: on: %d\n", __func__, on);
  478. mutex_lock(&ov965x->lock);
  479. if (ov965x->power == !on) {
  480. ret = __ov965x_set_power(ov965x, on);
  481. if (!ret && on) {
  482. ret = ov965x_write_array(ov965x,
  483. ov965x_init_regs);
  484. ov965x->apply_frame_fmt = 1;
  485. ov965x->ctrls.update = 1;
  486. }
  487. }
  488. if (!ret)
  489. ov965x->power += on ? 1 : -1;
  490. WARN_ON(ov965x->power < 0);
  491. mutex_unlock(&ov965x->lock);
  492. return ret;
  493. }
  494. /*
  495. * V4L2 controls
  496. */
  497. static void ov965x_update_exposure_ctrl(struct ov965x *ov965x)
  498. {
  499. struct v4l2_ctrl *ctrl = ov965x->ctrls.exposure;
  500. unsigned long fint, trow;
  501. int min, max, def;
  502. u8 clkrc;
  503. mutex_lock(&ov965x->lock);
  504. if (WARN_ON(!ctrl || !ov965x->frame_size)) {
  505. mutex_unlock(&ov965x->lock);
  506. return;
  507. }
  508. clkrc = DEF_CLKRC + ov965x->fiv->clkrc_div;
  509. /* Calculate internal clock frequency */
  510. fint = ov965x->mclk_frequency * ((clkrc >> 7) + 1) /
  511. ((2 * ((clkrc & 0x3f) + 1)));
  512. /* and the row interval (in us). */
  513. trow = (2 * 1520 * 1000000UL) / fint;
  514. max = ov965x->frame_size->max_exp_lines * trow;
  515. ov965x->exp_row_interval = trow;
  516. mutex_unlock(&ov965x->lock);
  517. v4l2_dbg(1, debug, &ov965x->sd, "clkrc: %#x, fi: %lu, tr: %lu, %d\n",
  518. clkrc, fint, trow, max);
  519. /* Update exposure time range to match current frame format. */
  520. min = (trow + 100) / 100;
  521. max = (max - 100) / 100;
  522. def = min + (max - min) / 2;
  523. if (v4l2_ctrl_modify_range(ctrl, min, max, 1, def))
  524. v4l2_err(&ov965x->sd, "Exposure ctrl range update failed\n");
  525. }
  526. static int ov965x_set_banding_filter(struct ov965x *ov965x, int value)
  527. {
  528. unsigned long mbd, light_freq;
  529. int ret;
  530. u8 reg;
  531. ret = ov965x_read(ov965x, REG_COM8, &reg);
  532. if (!ret) {
  533. if (value == V4L2_CID_POWER_LINE_FREQUENCY_DISABLED)
  534. reg &= ~COM8_BFILT;
  535. else
  536. reg |= COM8_BFILT;
  537. ret = ov965x_write(ov965x, REG_COM8, reg);
  538. }
  539. if (value == V4L2_CID_POWER_LINE_FREQUENCY_DISABLED)
  540. return 0;
  541. if (WARN_ON(!ov965x->fiv))
  542. return -EINVAL;
  543. /* Set minimal exposure time for 50/60 HZ lighting */
  544. if (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ)
  545. light_freq = 50;
  546. else
  547. light_freq = 60;
  548. mbd = (1000UL * ov965x->fiv->interval.denominator *
  549. ov965x->frame_size->max_exp_lines) /
  550. ov965x->fiv->interval.numerator;
  551. mbd = ((mbd / (light_freq * 2)) + 500) / 1000UL;
  552. return ov965x_write(ov965x, REG_MBD, mbd);
  553. }
  554. static int ov965x_set_white_balance(struct ov965x *ov965x, int awb)
  555. {
  556. int ret;
  557. u8 reg;
  558. ret = ov965x_read(ov965x, REG_COM8, &reg);
  559. if (!ret) {
  560. reg = awb ? reg | REG_COM8 : reg & ~REG_COM8;
  561. ret = ov965x_write(ov965x, REG_COM8, reg);
  562. }
  563. if (!ret && !awb) {
  564. ret = ov965x_write(ov965x, REG_BLUE,
  565. ov965x->ctrls.blue_balance->val);
  566. if (ret < 0)
  567. return ret;
  568. ret = ov965x_write(ov965x, REG_RED,
  569. ov965x->ctrls.red_balance->val);
  570. }
  571. return ret;
  572. }
  573. #define NUM_BR_LEVELS 7
  574. #define NUM_BR_REGS 3
  575. static int ov965x_set_brightness(struct ov965x *ov965x, int val)
  576. {
  577. static const u8 regs[NUM_BR_LEVELS + 1][NUM_BR_REGS] = {
  578. { REG_AEW, REG_AEB, REG_VPT },
  579. { 0x1c, 0x12, 0x50 }, /* -3 */
  580. { 0x3d, 0x30, 0x71 }, /* -2 */
  581. { 0x50, 0x44, 0x92 }, /* -1 */
  582. { 0x70, 0x64, 0xc3 }, /* 0 */
  583. { 0x90, 0x84, 0xd4 }, /* +1 */
  584. { 0xc4, 0xbf, 0xf9 }, /* +2 */
  585. { 0xd8, 0xd0, 0xfa }, /* +3 */
  586. };
  587. int i, ret = 0;
  588. val += (NUM_BR_LEVELS / 2 + 1);
  589. if (val > NUM_BR_LEVELS)
  590. return -EINVAL;
  591. for (i = 0; i < NUM_BR_REGS && !ret; i++)
  592. ret = ov965x_write(ov965x, regs[0][i],
  593. regs[val][i]);
  594. return ret;
  595. }
  596. static int ov965x_set_gain(struct ov965x *ov965x, int auto_gain)
  597. {
  598. struct ov965x_ctrls *ctrls = &ov965x->ctrls;
  599. int ret = 0;
  600. u8 reg;
  601. /*
  602. * For manual mode we need to disable AGC first, so
  603. * gain value in REG_VREF, REG_GAIN is not overwritten.
  604. */
  605. if (ctrls->auto_gain->is_new) {
  606. ret = ov965x_read(ov965x, REG_COM8, &reg);
  607. if (ret < 0)
  608. return ret;
  609. if (ctrls->auto_gain->val)
  610. reg |= COM8_AGC;
  611. else
  612. reg &= ~COM8_AGC;
  613. ret = ov965x_write(ov965x, REG_COM8, reg);
  614. if (ret < 0)
  615. return ret;
  616. }
  617. if (ctrls->gain->is_new && !auto_gain) {
  618. unsigned int gain = ctrls->gain->val;
  619. unsigned int rgain;
  620. int m;
  621. /*
  622. * Convert gain control value to the sensor's gain
  623. * registers (VREF[7:6], GAIN[7:0]) format.
  624. */
  625. for (m = 6; m >= 0; m--)
  626. if (gain >= (1 << m) * 16)
  627. break;
  628. /* Sanity check: don't adjust the gain with a negative value */
  629. if (m < 0)
  630. return -EINVAL;
  631. rgain = (gain - ((1 << m) * 16)) / (1 << m);
  632. rgain |= (((1 << m) - 1) << 4);
  633. ret = ov965x_write(ov965x, REG_GAIN, rgain & 0xff);
  634. if (ret < 0)
  635. return ret;
  636. ret = ov965x_read(ov965x, REG_VREF, &reg);
  637. if (ret < 0)
  638. return ret;
  639. reg &= ~VREF_GAIN_MASK;
  640. reg |= (((rgain >> 8) & 0x3) << 6);
  641. ret = ov965x_write(ov965x, REG_VREF, reg);
  642. if (ret < 0)
  643. return ret;
  644. /* Return updated control's value to userspace */
  645. ctrls->gain->val = (1 << m) * (16 + (rgain & 0xf));
  646. }
  647. return ret;
  648. }
  649. static int ov965x_set_sharpness(struct ov965x *ov965x, unsigned int value)
  650. {
  651. u8 com14, edge;
  652. int ret;
  653. ret = ov965x_read(ov965x, REG_COM14, &com14);
  654. if (ret < 0)
  655. return ret;
  656. ret = ov965x_read(ov965x, REG_EDGE, &edge);
  657. if (ret < 0)
  658. return ret;
  659. com14 = value ? com14 | COM14_EDGE_EN : com14 & ~COM14_EDGE_EN;
  660. value--;
  661. if (value > 0x0f) {
  662. com14 |= COM14_EEF_X2;
  663. value >>= 1;
  664. } else {
  665. com14 &= ~COM14_EEF_X2;
  666. }
  667. ret = ov965x_write(ov965x, REG_COM14, com14);
  668. if (ret < 0)
  669. return ret;
  670. edge &= ~EDGE_FACTOR_MASK;
  671. edge |= ((u8)value & 0x0f);
  672. return ov965x_write(ov965x, REG_EDGE, edge);
  673. }
  674. static int ov965x_set_exposure(struct ov965x *ov965x, int exp)
  675. {
  676. struct ov965x_ctrls *ctrls = &ov965x->ctrls;
  677. bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
  678. int ret;
  679. u8 reg;
  680. if (ctrls->auto_exp->is_new) {
  681. ret = ov965x_read(ov965x, REG_COM8, &reg);
  682. if (ret < 0)
  683. return ret;
  684. if (auto_exposure)
  685. reg |= (COM8_AEC | COM8_AGC);
  686. else
  687. reg &= ~(COM8_AEC | COM8_AGC);
  688. ret = ov965x_write(ov965x, REG_COM8, reg);
  689. if (ret < 0)
  690. return ret;
  691. }
  692. if (!auto_exposure && ctrls->exposure->is_new) {
  693. unsigned int exposure = (ctrls->exposure->val * 100)
  694. / ov965x->exp_row_interval;
  695. /*
  696. * Manual exposure value
  697. * [b15:b0] - AECHM (b15:b10), AECH (b9:b2), COM1 (b1:b0)
  698. */
  699. ret = ov965x_write(ov965x, REG_COM1, exposure & 0x3);
  700. if (!ret)
  701. ret = ov965x_write(ov965x, REG_AECH,
  702. (exposure >> 2) & 0xff);
  703. if (!ret)
  704. ret = ov965x_write(ov965x, REG_AECHM,
  705. (exposure >> 10) & 0x3f);
  706. /* Update the value to minimize rounding errors */
  707. ctrls->exposure->val = ((exposure * ov965x->exp_row_interval)
  708. + 50) / 100;
  709. if (ret < 0)
  710. return ret;
  711. }
  712. v4l2_ctrl_activate(ov965x->ctrls.brightness, !exp);
  713. return 0;
  714. }
  715. static int ov965x_set_flip(struct ov965x *ov965x)
  716. {
  717. u8 mvfp = 0;
  718. if (ov965x->ctrls.hflip->val)
  719. mvfp |= MVFP_MIRROR;
  720. if (ov965x->ctrls.vflip->val)
  721. mvfp |= MVFP_FLIP;
  722. return ov965x_write(ov965x, REG_MVFP, mvfp);
  723. }
  724. #define NUM_SAT_LEVELS 5
  725. #define NUM_SAT_REGS 6
  726. static int ov965x_set_saturation(struct ov965x *ov965x, int val)
  727. {
  728. static const u8 regs[NUM_SAT_LEVELS][NUM_SAT_REGS] = {
  729. /* MTX(1)...MTX(6) */
  730. { 0x1d, 0x1f, 0x02, 0x09, 0x13, 0x1c }, /* -2 */
  731. { 0x2e, 0x31, 0x02, 0x0e, 0x1e, 0x2d }, /* -1 */
  732. { 0x3a, 0x3d, 0x03, 0x12, 0x26, 0x38 }, /* 0 */
  733. { 0x46, 0x49, 0x04, 0x16, 0x2e, 0x43 }, /* +1 */
  734. { 0x57, 0x5c, 0x05, 0x1b, 0x39, 0x54 }, /* +2 */
  735. };
  736. u8 addr = REG_MTX(1);
  737. int i, ret = 0;
  738. val += (NUM_SAT_LEVELS / 2);
  739. if (val >= NUM_SAT_LEVELS)
  740. return -EINVAL;
  741. for (i = 0; i < NUM_SAT_REGS && !ret; i++)
  742. ret = ov965x_write(ov965x, addr + i, regs[val][i]);
  743. return ret;
  744. }
  745. static int ov965x_set_test_pattern(struct ov965x *ov965x, int value)
  746. {
  747. int ret;
  748. u8 reg;
  749. ret = ov965x_read(ov965x, REG_COM23, &reg);
  750. if (ret < 0)
  751. return ret;
  752. reg = value ? reg | COM23_TEST_MODE : reg & ~COM23_TEST_MODE;
  753. return ov965x_write(ov965x, REG_COM23, reg);
  754. }
  755. static int __g_volatile_ctrl(struct ov965x *ov965x, struct v4l2_ctrl *ctrl)
  756. {
  757. unsigned int exposure, gain, m;
  758. u8 reg0, reg1, reg2;
  759. int ret;
  760. if (!ov965x->power)
  761. return 0;
  762. switch (ctrl->id) {
  763. case V4L2_CID_AUTOGAIN:
  764. if (!ctrl->val)
  765. return 0;
  766. ret = ov965x_read(ov965x, REG_GAIN, &reg0);
  767. if (ret < 0)
  768. return ret;
  769. ret = ov965x_read(ov965x, REG_VREF, &reg1);
  770. if (ret < 0)
  771. return ret;
  772. gain = ((reg1 >> 6) << 8) | reg0;
  773. m = 0x01 << fls(gain >> 4);
  774. ov965x->ctrls.gain->val = m * (16 + (gain & 0xf));
  775. break;
  776. case V4L2_CID_EXPOSURE_AUTO:
  777. if (ctrl->val == V4L2_EXPOSURE_MANUAL)
  778. return 0;
  779. ret = ov965x_read(ov965x, REG_COM1, &reg0);
  780. if (ret < 0)
  781. return ret;
  782. ret = ov965x_read(ov965x, REG_AECH, &reg1);
  783. if (ret < 0)
  784. return ret;
  785. ret = ov965x_read(ov965x, REG_AECHM, &reg2);
  786. if (ret < 0)
  787. return ret;
  788. exposure = ((reg2 & 0x3f) << 10) | (reg1 << 2) |
  789. (reg0 & 0x3);
  790. ov965x->ctrls.exposure->val = ((exposure *
  791. ov965x->exp_row_interval) + 50) / 100;
  792. break;
  793. }
  794. return 0;
  795. }
  796. static int ov965x_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  797. {
  798. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  799. struct ov965x *ov965x = to_ov965x(sd);
  800. int ret;
  801. v4l2_dbg(1, debug, sd, "g_ctrl: %s\n", ctrl->name);
  802. mutex_lock(&ov965x->lock);
  803. ret = __g_volatile_ctrl(ov965x, ctrl);
  804. mutex_unlock(&ov965x->lock);
  805. return ret;
  806. }
  807. static int ov965x_s_ctrl(struct v4l2_ctrl *ctrl)
  808. {
  809. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  810. struct ov965x *ov965x = to_ov965x(sd);
  811. int ret = -EINVAL;
  812. v4l2_dbg(1, debug, sd, "s_ctrl: %s, value: %d. power: %d\n",
  813. ctrl->name, ctrl->val, ov965x->power);
  814. mutex_lock(&ov965x->lock);
  815. /*
  816. * If the device is not powered up now postpone applying control's
  817. * value to the hardware, until it is ready to accept commands.
  818. */
  819. if (ov965x->power == 0) {
  820. mutex_unlock(&ov965x->lock);
  821. return 0;
  822. }
  823. switch (ctrl->id) {
  824. case V4L2_CID_AUTO_WHITE_BALANCE:
  825. ret = ov965x_set_white_balance(ov965x, ctrl->val);
  826. break;
  827. case V4L2_CID_BRIGHTNESS:
  828. ret = ov965x_set_brightness(ov965x, ctrl->val);
  829. break;
  830. case V4L2_CID_EXPOSURE_AUTO:
  831. ret = ov965x_set_exposure(ov965x, ctrl->val);
  832. break;
  833. case V4L2_CID_AUTOGAIN:
  834. ret = ov965x_set_gain(ov965x, ctrl->val);
  835. break;
  836. case V4L2_CID_HFLIP:
  837. ret = ov965x_set_flip(ov965x);
  838. break;
  839. case V4L2_CID_POWER_LINE_FREQUENCY:
  840. ret = ov965x_set_banding_filter(ov965x, ctrl->val);
  841. break;
  842. case V4L2_CID_SATURATION:
  843. ret = ov965x_set_saturation(ov965x, ctrl->val);
  844. break;
  845. case V4L2_CID_SHARPNESS:
  846. ret = ov965x_set_sharpness(ov965x, ctrl->val);
  847. break;
  848. case V4L2_CID_TEST_PATTERN:
  849. ret = ov965x_set_test_pattern(ov965x, ctrl->val);
  850. break;
  851. }
  852. mutex_unlock(&ov965x->lock);
  853. return ret;
  854. }
  855. static const struct v4l2_ctrl_ops ov965x_ctrl_ops = {
  856. .g_volatile_ctrl = ov965x_g_volatile_ctrl,
  857. .s_ctrl = ov965x_s_ctrl,
  858. };
  859. static const char * const test_pattern_menu[] = {
  860. "Disabled",
  861. "Color bars",
  862. };
  863. static int ov965x_initialize_controls(struct ov965x *ov965x)
  864. {
  865. const struct v4l2_ctrl_ops *ops = &ov965x_ctrl_ops;
  866. struct ov965x_ctrls *ctrls = &ov965x->ctrls;
  867. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  868. int ret;
  869. ret = v4l2_ctrl_handler_init(hdl, 16);
  870. if (ret < 0)
  871. return ret;
  872. /* Auto/manual white balance */
  873. ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
  874. V4L2_CID_AUTO_WHITE_BALANCE,
  875. 0, 1, 1, 1);
  876. ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
  877. 0, 0xff, 1, 0x80);
  878. ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
  879. 0, 0xff, 1, 0x80);
  880. /* Auto/manual exposure */
  881. ctrls->auto_exp =
  882. v4l2_ctrl_new_std_menu(hdl, ops,
  883. V4L2_CID_EXPOSURE_AUTO,
  884. V4L2_EXPOSURE_MANUAL, 0,
  885. V4L2_EXPOSURE_AUTO);
  886. /* Exposure time, in 100 us units. min/max is updated dynamically. */
  887. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops,
  888. V4L2_CID_EXPOSURE_ABSOLUTE,
  889. 2, 1500, 1, 500);
  890. /* Auto/manual gain */
  891. ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
  892. 0, 1, 1, 1);
  893. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
  894. 16, 64 * (16 + 15), 1, 64 * 16);
  895. ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
  896. -2, 2, 1, 0);
  897. ctrls->brightness = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS,
  898. -3, 3, 1, 0);
  899. ctrls->sharpness = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS,
  900. 0, 32, 1, 6);
  901. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  902. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  903. ctrls->light_freq =
  904. v4l2_ctrl_new_std_menu(hdl, ops,
  905. V4L2_CID_POWER_LINE_FREQUENCY,
  906. V4L2_CID_POWER_LINE_FREQUENCY_60HZ, ~0x7,
  907. V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
  908. v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
  909. ARRAY_SIZE(test_pattern_menu) - 1, 0, 0,
  910. test_pattern_menu);
  911. if (hdl->error) {
  912. ret = hdl->error;
  913. v4l2_ctrl_handler_free(hdl);
  914. return ret;
  915. }
  916. ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
  917. ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
  918. v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
  919. v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
  920. v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
  921. v4l2_ctrl_cluster(2, &ctrls->hflip);
  922. ov965x->sd.ctrl_handler = hdl;
  923. return 0;
  924. }
  925. /*
  926. * V4L2 subdev video and pad level operations
  927. */
  928. static void ov965x_get_default_format(struct v4l2_mbus_framefmt *mf)
  929. {
  930. mf->width = ov965x_framesizes[0].width;
  931. mf->height = ov965x_framesizes[0].height;
  932. mf->colorspace = ov965x_formats[0].colorspace;
  933. mf->code = ov965x_formats[0].code;
  934. mf->field = V4L2_FIELD_NONE;
  935. }
  936. static int ov965x_enum_mbus_code(struct v4l2_subdev *sd,
  937. struct v4l2_subdev_state *sd_state,
  938. struct v4l2_subdev_mbus_code_enum *code)
  939. {
  940. if (code->index >= ARRAY_SIZE(ov965x_formats))
  941. return -EINVAL;
  942. code->code = ov965x_formats[code->index].code;
  943. return 0;
  944. }
  945. static int ov965x_enum_frame_sizes(struct v4l2_subdev *sd,
  946. struct v4l2_subdev_state *sd_state,
  947. struct v4l2_subdev_frame_size_enum *fse)
  948. {
  949. int i = ARRAY_SIZE(ov965x_formats);
  950. if (fse->index >= ARRAY_SIZE(ov965x_framesizes))
  951. return -EINVAL;
  952. while (--i)
  953. if (fse->code == ov965x_formats[i].code)
  954. break;
  955. fse->code = ov965x_formats[i].code;
  956. fse->min_width = ov965x_framesizes[fse->index].width;
  957. fse->max_width = fse->min_width;
  958. fse->max_height = ov965x_framesizes[fse->index].height;
  959. fse->min_height = fse->max_height;
  960. return 0;
  961. }
  962. static int ov965x_get_frame_interval(struct v4l2_subdev *sd,
  963. struct v4l2_subdev_state *sd_state,
  964. struct v4l2_subdev_frame_interval *fi)
  965. {
  966. struct ov965x *ov965x = to_ov965x(sd);
  967. /*
  968. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  969. * subdev active state API.
  970. */
  971. if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  972. return -EINVAL;
  973. mutex_lock(&ov965x->lock);
  974. fi->interval = ov965x->fiv->interval;
  975. mutex_unlock(&ov965x->lock);
  976. return 0;
  977. }
  978. static int __ov965x_set_frame_interval(struct ov965x *ov965x,
  979. struct v4l2_subdev_frame_interval *fi)
  980. {
  981. struct v4l2_mbus_framefmt *mbus_fmt = &ov965x->format;
  982. const struct ov965x_interval *fiv = &ov965x_intervals[0];
  983. u64 req_int, err, min_err = ~0ULL;
  984. unsigned int i;
  985. if (fi->interval.denominator == 0)
  986. return -EINVAL;
  987. req_int = (u64)fi->interval.numerator * 10000;
  988. do_div(req_int, fi->interval.denominator);
  989. for (i = 0; i < ARRAY_SIZE(ov965x_intervals); i++) {
  990. const struct ov965x_interval *iv = &ov965x_intervals[i];
  991. if (mbus_fmt->width != iv->size.width ||
  992. mbus_fmt->height != iv->size.height)
  993. continue;
  994. err = abs((u64)(iv->interval.numerator * 10000) /
  995. iv->interval.denominator - req_int);
  996. if (err < min_err) {
  997. fiv = iv;
  998. min_err = err;
  999. }
  1000. }
  1001. ov965x->fiv = fiv;
  1002. v4l2_dbg(1, debug, &ov965x->sd, "Changed frame interval to %u us\n",
  1003. fiv->interval.numerator * 1000000 / fiv->interval.denominator);
  1004. return 0;
  1005. }
  1006. static int ov965x_set_frame_interval(struct v4l2_subdev *sd,
  1007. struct v4l2_subdev_state *sd_state,
  1008. struct v4l2_subdev_frame_interval *fi)
  1009. {
  1010. struct ov965x *ov965x = to_ov965x(sd);
  1011. int ret;
  1012. /*
  1013. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  1014. * subdev active state API.
  1015. */
  1016. if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  1017. return -EINVAL;
  1018. v4l2_dbg(1, debug, sd, "Setting %d/%d frame interval\n",
  1019. fi->interval.numerator, fi->interval.denominator);
  1020. mutex_lock(&ov965x->lock);
  1021. ret = __ov965x_set_frame_interval(ov965x, fi);
  1022. ov965x->apply_frame_fmt = 1;
  1023. mutex_unlock(&ov965x->lock);
  1024. return ret;
  1025. }
  1026. static int ov965x_get_fmt(struct v4l2_subdev *sd,
  1027. struct v4l2_subdev_state *sd_state,
  1028. struct v4l2_subdev_format *fmt)
  1029. {
  1030. struct ov965x *ov965x = to_ov965x(sd);
  1031. struct v4l2_mbus_framefmt *mf;
  1032. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1033. mf = v4l2_subdev_state_get_format(sd_state, 0);
  1034. fmt->format = *mf;
  1035. return 0;
  1036. }
  1037. mutex_lock(&ov965x->lock);
  1038. fmt->format = ov965x->format;
  1039. mutex_unlock(&ov965x->lock);
  1040. return 0;
  1041. }
  1042. static void __ov965x_try_frame_size(struct v4l2_mbus_framefmt *mf,
  1043. const struct ov965x_framesize **size)
  1044. {
  1045. const struct ov965x_framesize *fsize = &ov965x_framesizes[0],
  1046. *match = NULL;
  1047. int i = ARRAY_SIZE(ov965x_framesizes);
  1048. unsigned int min_err = UINT_MAX;
  1049. while (i--) {
  1050. int err = abs(fsize->width - mf->width)
  1051. + abs(fsize->height - mf->height);
  1052. if (err < min_err) {
  1053. min_err = err;
  1054. match = fsize;
  1055. }
  1056. fsize++;
  1057. }
  1058. if (!match)
  1059. match = &ov965x_framesizes[0];
  1060. mf->width = match->width;
  1061. mf->height = match->height;
  1062. if (size)
  1063. *size = match;
  1064. }
  1065. static int ov965x_set_fmt(struct v4l2_subdev *sd,
  1066. struct v4l2_subdev_state *sd_state,
  1067. struct v4l2_subdev_format *fmt)
  1068. {
  1069. unsigned int index = ARRAY_SIZE(ov965x_formats);
  1070. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1071. struct ov965x *ov965x = to_ov965x(sd);
  1072. const struct ov965x_framesize *size = NULL;
  1073. int ret = 0;
  1074. __ov965x_try_frame_size(mf, &size);
  1075. while (--index)
  1076. if (ov965x_formats[index].code == mf->code)
  1077. break;
  1078. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1079. mf->code = ov965x_formats[index].code;
  1080. mf->field = V4L2_FIELD_NONE;
  1081. mutex_lock(&ov965x->lock);
  1082. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1083. if (sd_state) {
  1084. mf = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  1085. *mf = fmt->format;
  1086. }
  1087. } else {
  1088. if (ov965x->streaming) {
  1089. ret = -EBUSY;
  1090. } else {
  1091. ov965x->frame_size = size;
  1092. ov965x->format = fmt->format;
  1093. ov965x->tslb_reg = ov965x_formats[index].tslb_reg;
  1094. ov965x->apply_frame_fmt = 1;
  1095. }
  1096. }
  1097. if (!ret && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1098. struct v4l2_subdev_frame_interval fiv = {
  1099. .interval = { 0, 1 }
  1100. };
  1101. /* Reset to minimum possible frame interval */
  1102. __ov965x_set_frame_interval(ov965x, &fiv);
  1103. }
  1104. mutex_unlock(&ov965x->lock);
  1105. if (!ret)
  1106. ov965x_update_exposure_ctrl(ov965x);
  1107. return ret;
  1108. }
  1109. static int ov965x_set_frame_size(struct ov965x *ov965x)
  1110. {
  1111. int i, ret = 0;
  1112. for (i = 0; ret == 0 && i < NUM_FMT_REGS; i++)
  1113. ret = ov965x_write(ov965x, frame_size_reg_addr[i],
  1114. ov965x->frame_size->regs[i]);
  1115. return ret;
  1116. }
  1117. static int __ov965x_set_params(struct ov965x *ov965x)
  1118. {
  1119. struct ov965x_ctrls *ctrls = &ov965x->ctrls;
  1120. int ret = 0;
  1121. u8 reg;
  1122. if (ov965x->apply_frame_fmt) {
  1123. reg = DEF_CLKRC + ov965x->fiv->clkrc_div;
  1124. ret = ov965x_write(ov965x, REG_CLKRC, reg);
  1125. if (ret < 0)
  1126. return ret;
  1127. ret = ov965x_set_frame_size(ov965x);
  1128. if (ret < 0)
  1129. return ret;
  1130. ret = ov965x_read(ov965x, REG_TSLB, &reg);
  1131. if (ret < 0)
  1132. return ret;
  1133. reg &= ~TSLB_YUYV_MASK;
  1134. reg |= ov965x->tslb_reg;
  1135. ret = ov965x_write(ov965x, REG_TSLB, reg);
  1136. if (ret < 0)
  1137. return ret;
  1138. }
  1139. ret = ov965x_set_default_gamma_curve(ov965x);
  1140. if (ret < 0)
  1141. return ret;
  1142. ret = ov965x_set_color_matrix(ov965x);
  1143. if (ret < 0)
  1144. return ret;
  1145. /*
  1146. * Select manual banding filter, the filter will
  1147. * be enabled further if required.
  1148. */
  1149. ret = ov965x_read(ov965x, REG_COM11, &reg);
  1150. if (!ret)
  1151. reg |= COM11_BANDING;
  1152. ret = ov965x_write(ov965x, REG_COM11, reg);
  1153. if (ret < 0)
  1154. return ret;
  1155. /*
  1156. * Banding filter (REG_MBD value) needs to match selected
  1157. * resolution and frame rate, so it's always updated here.
  1158. */
  1159. return ov965x_set_banding_filter(ov965x, ctrls->light_freq->val);
  1160. }
  1161. static int ov965x_s_stream(struct v4l2_subdev *sd, int on)
  1162. {
  1163. struct ov965x *ov965x = to_ov965x(sd);
  1164. struct ov965x_ctrls *ctrls = &ov965x->ctrls;
  1165. int ret = 0;
  1166. v4l2_dbg(1, debug, sd, "%s: on: %d\n", __func__, on);
  1167. mutex_lock(&ov965x->lock);
  1168. if (ov965x->streaming == !on) {
  1169. if (on)
  1170. ret = __ov965x_set_params(ov965x);
  1171. if (!ret && ctrls->update) {
  1172. /*
  1173. * ov965x_s_ctrl callback takes the mutex
  1174. * so it needs to be released here.
  1175. */
  1176. mutex_unlock(&ov965x->lock);
  1177. ret = v4l2_ctrl_handler_setup(&ctrls->handler);
  1178. mutex_lock(&ov965x->lock);
  1179. if (!ret)
  1180. ctrls->update = 0;
  1181. }
  1182. if (!ret)
  1183. ret = ov965x_write(ov965x, REG_COM2,
  1184. on ? 0x01 : 0x11);
  1185. }
  1186. if (!ret)
  1187. ov965x->streaming += on ? 1 : -1;
  1188. WARN_ON(ov965x->streaming < 0);
  1189. mutex_unlock(&ov965x->lock);
  1190. return ret;
  1191. }
  1192. /*
  1193. * V4L2 subdev internal operations
  1194. */
  1195. static int ov965x_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1196. {
  1197. struct v4l2_mbus_framefmt *mf =
  1198. v4l2_subdev_state_get_format(fh->state, 0);
  1199. ov965x_get_default_format(mf);
  1200. return 0;
  1201. }
  1202. static const struct v4l2_subdev_pad_ops ov965x_pad_ops = {
  1203. .enum_mbus_code = ov965x_enum_mbus_code,
  1204. .enum_frame_size = ov965x_enum_frame_sizes,
  1205. .get_fmt = ov965x_get_fmt,
  1206. .set_fmt = ov965x_set_fmt,
  1207. .get_frame_interval = ov965x_get_frame_interval,
  1208. .set_frame_interval = ov965x_set_frame_interval,
  1209. };
  1210. static const struct v4l2_subdev_video_ops ov965x_video_ops = {
  1211. .s_stream = ov965x_s_stream,
  1212. };
  1213. static const struct v4l2_subdev_internal_ops ov965x_sd_internal_ops = {
  1214. .open = ov965x_open,
  1215. };
  1216. static const struct v4l2_subdev_core_ops ov965x_core_ops = {
  1217. .s_power = ov965x_s_power,
  1218. .log_status = v4l2_ctrl_subdev_log_status,
  1219. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  1220. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1221. };
  1222. static const struct v4l2_subdev_ops ov965x_subdev_ops = {
  1223. .core = &ov965x_core_ops,
  1224. .pad = &ov965x_pad_ops,
  1225. .video = &ov965x_video_ops,
  1226. };
  1227. static int ov965x_configure_gpios(struct ov965x *ov965x)
  1228. {
  1229. struct device *dev = regmap_get_device(ov965x->regmap);
  1230. ov965x->gpios[GPIO_PWDN] = devm_gpiod_get_optional(dev, "powerdown",
  1231. GPIOD_OUT_HIGH);
  1232. if (IS_ERR(ov965x->gpios[GPIO_PWDN])) {
  1233. dev_info(dev, "can't get %s GPIO\n", "powerdown");
  1234. return PTR_ERR(ov965x->gpios[GPIO_PWDN]);
  1235. }
  1236. ov965x->gpios[GPIO_RST] = devm_gpiod_get_optional(dev, "reset",
  1237. GPIOD_OUT_HIGH);
  1238. if (IS_ERR(ov965x->gpios[GPIO_RST])) {
  1239. dev_info(dev, "can't get %s GPIO\n", "reset");
  1240. return PTR_ERR(ov965x->gpios[GPIO_RST]);
  1241. }
  1242. return 0;
  1243. }
  1244. static int ov965x_detect_sensor(struct v4l2_subdev *sd)
  1245. {
  1246. struct ov965x *ov965x = to_ov965x(sd);
  1247. u8 pid, ver;
  1248. int ret;
  1249. mutex_lock(&ov965x->lock);
  1250. ret = __ov965x_set_power(ov965x, 1);
  1251. if (ret)
  1252. goto out;
  1253. msleep(25);
  1254. /* Check sensor revision */
  1255. ret = ov965x_read(ov965x, REG_PID, &pid);
  1256. if (!ret)
  1257. ret = ov965x_read(ov965x, REG_VER, &ver);
  1258. __ov965x_set_power(ov965x, 0);
  1259. if (!ret) {
  1260. ov965x->id = OV965X_ID(pid, ver);
  1261. if (ov965x->id == OV9650_ID || ov965x->id == OV9652_ID) {
  1262. v4l2_info(sd, "Found OV%04X sensor\n", ov965x->id);
  1263. } else {
  1264. v4l2_err(sd, "Sensor detection failed (%04X)\n",
  1265. ov965x->id);
  1266. ret = -ENODEV;
  1267. }
  1268. }
  1269. out:
  1270. mutex_unlock(&ov965x->lock);
  1271. return ret;
  1272. }
  1273. static int ov965x_probe(struct i2c_client *client)
  1274. {
  1275. struct v4l2_subdev *sd;
  1276. struct ov965x *ov965x;
  1277. int ret;
  1278. static const struct regmap_config ov965x_regmap_config = {
  1279. .reg_bits = 8,
  1280. .val_bits = 8,
  1281. .max_register = 0xab,
  1282. };
  1283. ov965x = devm_kzalloc(&client->dev, sizeof(*ov965x), GFP_KERNEL);
  1284. if (!ov965x)
  1285. return -ENOMEM;
  1286. ov965x->regmap = devm_regmap_init_sccb(client, &ov965x_regmap_config);
  1287. if (IS_ERR(ov965x->regmap)) {
  1288. dev_err(&client->dev, "Failed to allocate register map\n");
  1289. return PTR_ERR(ov965x->regmap);
  1290. }
  1291. if (dev_fwnode(&client->dev)) {
  1292. ov965x->clk = devm_v4l2_sensor_clk_get(&client->dev, NULL);
  1293. if (IS_ERR(ov965x->clk))
  1294. return dev_err_probe(&client->dev, PTR_ERR(ov965x->clk),
  1295. "failed to get the clock\n");
  1296. ov965x->mclk_frequency = clk_get_rate(ov965x->clk);
  1297. ret = ov965x_configure_gpios(ov965x);
  1298. if (ret < 0)
  1299. return ret;
  1300. } else {
  1301. dev_err(&client->dev,
  1302. "No device properties specified\n");
  1303. return -EINVAL;
  1304. }
  1305. mutex_init(&ov965x->lock);
  1306. sd = &ov965x->sd;
  1307. v4l2_i2c_subdev_init(sd, client, &ov965x_subdev_ops);
  1308. strscpy(sd->name, DRIVER_NAME, sizeof(sd->name));
  1309. sd->internal_ops = &ov965x_sd_internal_ops;
  1310. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  1311. V4L2_SUBDEV_FL_HAS_EVENTS;
  1312. ov965x->pad.flags = MEDIA_PAD_FL_SOURCE;
  1313. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1314. ret = media_entity_pads_init(&sd->entity, 1, &ov965x->pad);
  1315. if (ret < 0)
  1316. goto err_mutex;
  1317. ret = ov965x_initialize_controls(ov965x);
  1318. if (ret < 0)
  1319. goto err_me;
  1320. ov965x_get_default_format(&ov965x->format);
  1321. ov965x->frame_size = &ov965x_framesizes[0];
  1322. ov965x->fiv = &ov965x_intervals[0];
  1323. ret = ov965x_detect_sensor(sd);
  1324. if (ret < 0)
  1325. goto err_ctrls;
  1326. /* Update exposure time min/max to match frame format */
  1327. ov965x_update_exposure_ctrl(ov965x);
  1328. ret = v4l2_async_register_subdev(sd);
  1329. if (ret < 0)
  1330. goto err_ctrls;
  1331. return 0;
  1332. err_ctrls:
  1333. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1334. err_me:
  1335. media_entity_cleanup(&sd->entity);
  1336. err_mutex:
  1337. mutex_destroy(&ov965x->lock);
  1338. return ret;
  1339. }
  1340. static void ov965x_remove(struct i2c_client *client)
  1341. {
  1342. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1343. struct ov965x *ov965x = to_ov965x(sd);
  1344. v4l2_async_unregister_subdev(sd);
  1345. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1346. media_entity_cleanup(&sd->entity);
  1347. mutex_destroy(&ov965x->lock);
  1348. }
  1349. static const struct i2c_device_id ov965x_id[] = {
  1350. { "OV9650" },
  1351. { "OV9652" },
  1352. { /* sentinel */ }
  1353. };
  1354. MODULE_DEVICE_TABLE(i2c, ov965x_id);
  1355. #if IS_ENABLED(CONFIG_OF)
  1356. static const struct of_device_id ov965x_of_match[] = {
  1357. { .compatible = "ovti,ov9650", },
  1358. { .compatible = "ovti,ov9652", },
  1359. { /* sentinel */ }
  1360. };
  1361. MODULE_DEVICE_TABLE(of, ov965x_of_match);
  1362. #endif
  1363. static struct i2c_driver ov965x_i2c_driver = {
  1364. .driver = {
  1365. .name = DRIVER_NAME,
  1366. .of_match_table = of_match_ptr(ov965x_of_match),
  1367. },
  1368. .probe = ov965x_probe,
  1369. .remove = ov965x_remove,
  1370. .id_table = ov965x_id,
  1371. };
  1372. module_i2c_driver(ov965x_i2c_driver);
  1373. MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
  1374. MODULE_DESCRIPTION("OV9650/OV9652 CMOS Image Sensor driver");
  1375. MODULE_LICENSE("GPL");