ov9650.c 41 KB

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