ov9650.c 41 KB

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