ov9650.c 43 KB

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