ov7251.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the OV7251 camera sensor.
  4. *
  5. * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2017-2018, Linaro Ltd.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-fwnode.h>
  23. #include <media/v4l2-subdev.h>
  24. #define OV7251_SC_MODE_SELECT 0x0100
  25. #define OV7251_SC_MODE_SELECT_SW_STANDBY 0x0
  26. #define OV7251_SC_MODE_SELECT_STREAMING 0x1
  27. #define OV7251_CHIP_ID_HIGH 0x300a
  28. #define OV7251_CHIP_ID_HIGH_BYTE 0x77
  29. #define OV7251_CHIP_ID_LOW 0x300b
  30. #define OV7251_CHIP_ID_LOW_BYTE 0x50
  31. #define OV7251_SC_GP_IO_IN1 0x3029
  32. #define OV7251_AEC_EXPO_0 0x3500
  33. #define OV7251_AEC_EXPO_1 0x3501
  34. #define OV7251_AEC_EXPO_2 0x3502
  35. #define OV7251_AEC_AGC_ADJ_0 0x350a
  36. #define OV7251_AEC_AGC_ADJ_1 0x350b
  37. #define OV7251_TIMING_FORMAT1 0x3820
  38. #define OV7251_TIMING_FORMAT1_VFLIP BIT(2)
  39. #define OV7251_TIMING_FORMAT2 0x3821
  40. #define OV7251_TIMING_FORMAT2_MIRROR BIT(2)
  41. #define OV7251_PRE_ISP_00 0x5e00
  42. #define OV7251_PRE_ISP_00_TEST_PATTERN BIT(7)
  43. #define OV7251_PLL1_PRE_DIV_REG 0x30b4
  44. #define OV7251_PLL1_MULT_REG 0x30b3
  45. #define OV7251_PLL1_DIVIDER_REG 0x30b1
  46. #define OV7251_PLL1_PIX_DIV_REG 0x30b0
  47. #define OV7251_PLL1_MIPI_DIV_REG 0x30b5
  48. #define OV7251_PLL2_PRE_DIV_REG 0x3098
  49. #define OV7251_PLL2_MULT_REG 0x3099
  50. #define OV7251_PLL2_DIVIDER_REG 0x309d
  51. #define OV7251_PLL2_SYS_DIV_REG 0x309a
  52. #define OV7251_PLL2_ADC_DIV_REG 0x309b
  53. #define OV7251_NATIVE_WIDTH 656
  54. #define OV7251_NATIVE_HEIGHT 496
  55. #define OV7251_ACTIVE_START_LEFT 4
  56. #define OV7251_ACTIVE_START_TOP 4
  57. #define OV7251_ACTIVE_WIDTH 648
  58. #define OV7251_ACTIVE_HEIGHT 488
  59. #define OV7251_FIXED_PPL 928
  60. #define OV7251_TIMING_VTS_REG 0x380e
  61. #define OV7251_TIMING_MIN_VTS 1
  62. #define OV7251_TIMING_MAX_VTS 0xffff
  63. #define OV7251_INTEGRATION_MARGIN 20
  64. struct reg_value {
  65. u16 reg;
  66. u8 val;
  67. };
  68. struct ov7251_mode_info {
  69. u32 width;
  70. u32 height;
  71. u32 vts;
  72. const struct reg_value *data;
  73. u32 data_size;
  74. u32 pixel_clock;
  75. u32 link_freq;
  76. u16 exposure_max;
  77. u16 exposure_def;
  78. struct v4l2_fract timeperframe;
  79. };
  80. struct ov7251_pll1_cfg {
  81. unsigned int pre_div;
  82. unsigned int mult;
  83. unsigned int div;
  84. unsigned int pix_div;
  85. unsigned int mipi_div;
  86. };
  87. struct ov7251_pll2_cfg {
  88. unsigned int pre_div;
  89. unsigned int mult;
  90. unsigned int div;
  91. unsigned int sys_div;
  92. unsigned int adc_div;
  93. };
  94. /*
  95. * Rubbish ordering, but only PLL1 needs to have a separate configuration per
  96. * link frequency and the array member needs to be last.
  97. */
  98. struct ov7251_pll_cfgs {
  99. const struct ov7251_pll2_cfg *pll2;
  100. const struct ov7251_pll1_cfg *pll1[];
  101. };
  102. enum xclk_rate {
  103. OV7251_19_2_MHZ,
  104. OV7251_24_MHZ,
  105. OV7251_NUM_SUPPORTED_RATES
  106. };
  107. enum supported_link_freqs {
  108. OV7251_LINK_FREQ_240_MHZ,
  109. OV7251_LINK_FREQ_319_2_MHZ,
  110. OV7251_NUM_SUPPORTED_LINK_FREQS
  111. };
  112. struct ov7251 {
  113. struct i2c_client *i2c_client;
  114. struct device *dev;
  115. struct v4l2_subdev sd;
  116. struct media_pad pad;
  117. struct v4l2_fwnode_endpoint ep;
  118. struct v4l2_mbus_framefmt fmt;
  119. struct v4l2_rect crop;
  120. struct clk *xclk;
  121. u32 xclk_freq;
  122. struct regulator *io_regulator;
  123. struct regulator *core_regulator;
  124. struct regulator *analog_regulator;
  125. const struct ov7251_pll_cfgs *pll_cfgs;
  126. enum supported_link_freqs link_freq_idx;
  127. const struct ov7251_mode_info *current_mode;
  128. struct v4l2_ctrl_handler ctrls;
  129. struct v4l2_ctrl *pixel_clock;
  130. struct v4l2_ctrl *link_freq;
  131. struct v4l2_ctrl *exposure;
  132. struct v4l2_ctrl *gain;
  133. struct v4l2_ctrl *hblank;
  134. struct v4l2_ctrl *vblank;
  135. /* Cached register values */
  136. u8 aec_pk_manual;
  137. u8 pre_isp_00;
  138. u8 timing_format1;
  139. u8 timing_format2;
  140. struct mutex lock; /* lock to protect power state, ctrls and mode */
  141. bool power_on;
  142. struct gpio_desc *enable_gpio;
  143. };
  144. static inline struct ov7251 *to_ov7251(struct v4l2_subdev *sd)
  145. {
  146. return container_of(sd, struct ov7251, sd);
  147. }
  148. static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_240_mhz = {
  149. .pre_div = 0x03,
  150. .mult = 0x4b,
  151. .div = 0x01,
  152. .pix_div = 0x0a,
  153. .mipi_div = 0x05,
  154. };
  155. static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_319_2_mhz = {
  156. .pre_div = 0x01,
  157. .mult = 0x85,
  158. .div = 0x04,
  159. .pix_div = 0x0a,
  160. .mipi_div = 0x05,
  161. };
  162. static const struct ov7251_pll1_cfg ov7251_pll1_cfg_24_mhz_240_mhz = {
  163. .pre_div = 0x03,
  164. .mult = 0x64,
  165. .div = 0x01,
  166. .pix_div = 0x0a,
  167. .mipi_div = 0x05,
  168. };
  169. static const struct ov7251_pll1_cfg ov7251_pll1_cfg_24_mhz_319_2_mhz = {
  170. .pre_div = 0x05,
  171. .mult = 0x85,
  172. .div = 0x02,
  173. .pix_div = 0x0a,
  174. .mipi_div = 0x05,
  175. };
  176. static const struct ov7251_pll2_cfg ov7251_pll2_cfg_19_2_mhz = {
  177. .pre_div = 0x04,
  178. .mult = 0x32,
  179. .div = 0x00,
  180. .sys_div = 0x05,
  181. .adc_div = 0x04,
  182. };
  183. static const struct ov7251_pll2_cfg ov7251_pll2_cfg_24_mhz = {
  184. .pre_div = 0x04,
  185. .mult = 0x28,
  186. .div = 0x00,
  187. .sys_div = 0x05,
  188. .adc_div = 0x04,
  189. };
  190. static const struct ov7251_pll_cfgs ov7251_pll_cfgs_19_2_mhz = {
  191. .pll2 = &ov7251_pll2_cfg_19_2_mhz,
  192. .pll1 = {
  193. [OV7251_LINK_FREQ_240_MHZ] = &ov7251_pll1_cfg_19_2_mhz_240_mhz,
  194. [OV7251_LINK_FREQ_319_2_MHZ] = &ov7251_pll1_cfg_19_2_mhz_319_2_mhz,
  195. },
  196. };
  197. static const struct ov7251_pll_cfgs ov7251_pll_cfgs_24_mhz = {
  198. .pll2 = &ov7251_pll2_cfg_24_mhz,
  199. .pll1 = {
  200. [OV7251_LINK_FREQ_240_MHZ] = &ov7251_pll1_cfg_24_mhz_240_mhz,
  201. [OV7251_LINK_FREQ_319_2_MHZ] = &ov7251_pll1_cfg_24_mhz_319_2_mhz,
  202. },
  203. };
  204. static const struct ov7251_pll_cfgs *ov7251_pll_cfgs[] = {
  205. [OV7251_19_2_MHZ] = &ov7251_pll_cfgs_19_2_mhz,
  206. [OV7251_24_MHZ] = &ov7251_pll_cfgs_24_mhz,
  207. };
  208. static const struct reg_value ov7251_global_init_setting[] = {
  209. { 0x0103, 0x01 },
  210. { 0x303b, 0x02 },
  211. };
  212. static const struct reg_value ov7251_setting_vga_30fps[] = {
  213. { 0x3005, 0x00 },
  214. { 0x3012, 0xc0 },
  215. { 0x3013, 0xd2 },
  216. { 0x3014, 0x04 },
  217. { 0x3016, 0xf0 },
  218. { 0x3017, 0xf0 },
  219. { 0x3018, 0xf0 },
  220. { 0x301a, 0xf0 },
  221. { 0x301b, 0xf0 },
  222. { 0x301c, 0xf0 },
  223. { 0x3023, 0x05 },
  224. { 0x3037, 0xf0 },
  225. { 0x3106, 0xda },
  226. { 0x3503, 0x07 },
  227. { 0x3509, 0x10 },
  228. { 0x3600, 0x1c },
  229. { 0x3602, 0x62 },
  230. { 0x3620, 0xb7 },
  231. { 0x3622, 0x04 },
  232. { 0x3626, 0x21 },
  233. { 0x3627, 0x30 },
  234. { 0x3630, 0x44 },
  235. { 0x3631, 0x35 },
  236. { 0x3634, 0x60 },
  237. { 0x3636, 0x00 },
  238. { 0x3662, 0x01 },
  239. { 0x3663, 0x70 },
  240. { 0x3664, 0x50 },
  241. { 0x3666, 0x0a },
  242. { 0x3669, 0x1a },
  243. { 0x366a, 0x00 },
  244. { 0x366b, 0x50 },
  245. { 0x3673, 0x01 },
  246. { 0x3674, 0xff },
  247. { 0x3675, 0x03 },
  248. { 0x3705, 0xc1 },
  249. { 0x3709, 0x40 },
  250. { 0x373c, 0x08 },
  251. { 0x3742, 0x00 },
  252. { 0x3757, 0xb3 },
  253. { 0x3788, 0x00 },
  254. { 0x37a8, 0x01 },
  255. { 0x37a9, 0xc0 },
  256. { 0x3800, 0x00 },
  257. { 0x3801, 0x04 },
  258. { 0x3802, 0x00 },
  259. { 0x3803, 0x04 },
  260. { 0x3804, 0x02 },
  261. { 0x3805, 0x8b },
  262. { 0x3806, 0x01 },
  263. { 0x3807, 0xeb },
  264. { 0x3808, 0x02 }, /* width high */
  265. { 0x3809, 0x80 }, /* width low */
  266. { 0x380a, 0x01 }, /* height high */
  267. { 0x380b, 0xe0 }, /* height low */
  268. { 0x380c, 0x03 }, /* total horiz timing high */
  269. { 0x380d, 0xa0 }, /* total horiz timing low */
  270. { 0x380e, 0x06 }, /* total vertical timing high */
  271. { 0x380f, 0xbc }, /* total vertical timing low */
  272. { 0x3810, 0x00 },
  273. { 0x3811, 0x04 },
  274. { 0x3812, 0x00 },
  275. { 0x3813, 0x05 },
  276. { 0x3814, 0x11 },
  277. { 0x3815, 0x11 },
  278. { 0x3820, 0x40 },
  279. { 0x3821, 0x00 },
  280. { 0x382f, 0x0e },
  281. { 0x3832, 0x00 },
  282. { 0x3833, 0x05 },
  283. { 0x3834, 0x00 },
  284. { 0x3835, 0x0c },
  285. { 0x3837, 0x00 },
  286. { 0x3b80, 0x00 },
  287. { 0x3b81, 0xa5 },
  288. { 0x3b82, 0x10 },
  289. { 0x3b83, 0x00 },
  290. { 0x3b84, 0x08 },
  291. { 0x3b85, 0x00 },
  292. { 0x3b86, 0x01 },
  293. { 0x3b87, 0x00 },
  294. { 0x3b88, 0x00 },
  295. { 0x3b89, 0x00 },
  296. { 0x3b8a, 0x00 },
  297. { 0x3b8b, 0x05 },
  298. { 0x3b8c, 0x00 },
  299. { 0x3b8d, 0x00 },
  300. { 0x3b8e, 0x00 },
  301. { 0x3b8f, 0x1a },
  302. { 0x3b94, 0x05 },
  303. { 0x3b95, 0xf2 },
  304. { 0x3b96, 0x40 },
  305. { 0x3c00, 0x89 },
  306. { 0x3c01, 0x63 },
  307. { 0x3c02, 0x01 },
  308. { 0x3c03, 0x00 },
  309. { 0x3c04, 0x00 },
  310. { 0x3c05, 0x03 },
  311. { 0x3c06, 0x00 },
  312. { 0x3c07, 0x06 },
  313. { 0x3c0c, 0x01 },
  314. { 0x3c0d, 0xd0 },
  315. { 0x3c0e, 0x02 },
  316. { 0x3c0f, 0x0a },
  317. { 0x4001, 0x42 },
  318. { 0x4004, 0x04 },
  319. { 0x4005, 0x00 },
  320. { 0x404e, 0x01 },
  321. { 0x4300, 0xff },
  322. { 0x4301, 0x00 },
  323. { 0x4315, 0x00 },
  324. { 0x4501, 0x48 },
  325. { 0x4600, 0x00 },
  326. { 0x4601, 0x4e },
  327. { 0x4801, 0x0f },
  328. { 0x4806, 0x0f },
  329. { 0x4819, 0xaa },
  330. { 0x4823, 0x3e },
  331. { 0x4837, 0x19 },
  332. { 0x4a0d, 0x00 },
  333. { 0x4a47, 0x7f },
  334. { 0x4a49, 0xf0 },
  335. { 0x4a4b, 0x30 },
  336. { 0x5000, 0x85 },
  337. { 0x5001, 0x80 },
  338. };
  339. static const struct reg_value ov7251_setting_vga_60fps[] = {
  340. { 0x3005, 0x00 },
  341. { 0x3012, 0xc0 },
  342. { 0x3013, 0xd2 },
  343. { 0x3014, 0x04 },
  344. { 0x3016, 0x10 },
  345. { 0x3017, 0x00 },
  346. { 0x3018, 0x00 },
  347. { 0x301a, 0x00 },
  348. { 0x301b, 0x00 },
  349. { 0x301c, 0x00 },
  350. { 0x3023, 0x05 },
  351. { 0x3037, 0xf0 },
  352. { 0x3106, 0xda },
  353. { 0x3503, 0x07 },
  354. { 0x3509, 0x10 },
  355. { 0x3600, 0x1c },
  356. { 0x3602, 0x62 },
  357. { 0x3620, 0xb7 },
  358. { 0x3622, 0x04 },
  359. { 0x3626, 0x21 },
  360. { 0x3627, 0x30 },
  361. { 0x3630, 0x44 },
  362. { 0x3631, 0x35 },
  363. { 0x3634, 0x60 },
  364. { 0x3636, 0x00 },
  365. { 0x3662, 0x01 },
  366. { 0x3663, 0x70 },
  367. { 0x3664, 0x50 },
  368. { 0x3666, 0x0a },
  369. { 0x3669, 0x1a },
  370. { 0x366a, 0x00 },
  371. { 0x366b, 0x50 },
  372. { 0x3673, 0x01 },
  373. { 0x3674, 0xff },
  374. { 0x3675, 0x03 },
  375. { 0x3705, 0xc1 },
  376. { 0x3709, 0x40 },
  377. { 0x373c, 0x08 },
  378. { 0x3742, 0x00 },
  379. { 0x3757, 0xb3 },
  380. { 0x3788, 0x00 },
  381. { 0x37a8, 0x01 },
  382. { 0x37a9, 0xc0 },
  383. { 0x3800, 0x00 },
  384. { 0x3801, 0x04 },
  385. { 0x3802, 0x00 },
  386. { 0x3803, 0x04 },
  387. { 0x3804, 0x02 },
  388. { 0x3805, 0x8b },
  389. { 0x3806, 0x01 },
  390. { 0x3807, 0xeb },
  391. { 0x3808, 0x02 }, /* width high */
  392. { 0x3809, 0x80 }, /* width low */
  393. { 0x380a, 0x01 }, /* height high */
  394. { 0x380b, 0xe0 }, /* height low */
  395. { 0x380c, 0x03 }, /* total horiz timing high */
  396. { 0x380d, 0xa0 }, /* total horiz timing low */
  397. { 0x380e, 0x03 }, /* total vertical timing high */
  398. { 0x380f, 0x5c }, /* total vertical timing low */
  399. { 0x3810, 0x00 },
  400. { 0x3811, 0x04 },
  401. { 0x3812, 0x00 },
  402. { 0x3813, 0x05 },
  403. { 0x3814, 0x11 },
  404. { 0x3815, 0x11 },
  405. { 0x3820, 0x40 },
  406. { 0x3821, 0x00 },
  407. { 0x382f, 0x0e },
  408. { 0x3832, 0x00 },
  409. { 0x3833, 0x05 },
  410. { 0x3834, 0x00 },
  411. { 0x3835, 0x0c },
  412. { 0x3837, 0x00 },
  413. { 0x3b80, 0x00 },
  414. { 0x3b81, 0xa5 },
  415. { 0x3b82, 0x10 },
  416. { 0x3b83, 0x00 },
  417. { 0x3b84, 0x08 },
  418. { 0x3b85, 0x00 },
  419. { 0x3b86, 0x01 },
  420. { 0x3b87, 0x00 },
  421. { 0x3b88, 0x00 },
  422. { 0x3b89, 0x00 },
  423. { 0x3b8a, 0x00 },
  424. { 0x3b8b, 0x05 },
  425. { 0x3b8c, 0x00 },
  426. { 0x3b8d, 0x00 },
  427. { 0x3b8e, 0x00 },
  428. { 0x3b8f, 0x1a },
  429. { 0x3b94, 0x05 },
  430. { 0x3b95, 0xf2 },
  431. { 0x3b96, 0x40 },
  432. { 0x3c00, 0x89 },
  433. { 0x3c01, 0x63 },
  434. { 0x3c02, 0x01 },
  435. { 0x3c03, 0x00 },
  436. { 0x3c04, 0x00 },
  437. { 0x3c05, 0x03 },
  438. { 0x3c06, 0x00 },
  439. { 0x3c07, 0x06 },
  440. { 0x3c0c, 0x01 },
  441. { 0x3c0d, 0xd0 },
  442. { 0x3c0e, 0x02 },
  443. { 0x3c0f, 0x0a },
  444. { 0x4001, 0x42 },
  445. { 0x4004, 0x04 },
  446. { 0x4005, 0x00 },
  447. { 0x404e, 0x01 },
  448. { 0x4300, 0xff },
  449. { 0x4301, 0x00 },
  450. { 0x4315, 0x00 },
  451. { 0x4501, 0x48 },
  452. { 0x4600, 0x00 },
  453. { 0x4601, 0x4e },
  454. { 0x4801, 0x0f },
  455. { 0x4806, 0x0f },
  456. { 0x4819, 0xaa },
  457. { 0x4823, 0x3e },
  458. { 0x4837, 0x19 },
  459. { 0x4a0d, 0x00 },
  460. { 0x4a47, 0x7f },
  461. { 0x4a49, 0xf0 },
  462. { 0x4a4b, 0x30 },
  463. { 0x5000, 0x85 },
  464. { 0x5001, 0x80 },
  465. };
  466. static const struct reg_value ov7251_setting_vga_90fps[] = {
  467. { 0x3005, 0x00 },
  468. { 0x3012, 0xc0 },
  469. { 0x3013, 0xd2 },
  470. { 0x3014, 0x04 },
  471. { 0x3016, 0x10 },
  472. { 0x3017, 0x00 },
  473. { 0x3018, 0x00 },
  474. { 0x301a, 0x00 },
  475. { 0x301b, 0x00 },
  476. { 0x301c, 0x00 },
  477. { 0x3023, 0x05 },
  478. { 0x3037, 0xf0 },
  479. { 0x3106, 0xda },
  480. { 0x3503, 0x07 },
  481. { 0x3509, 0x10 },
  482. { 0x3600, 0x1c },
  483. { 0x3602, 0x62 },
  484. { 0x3620, 0xb7 },
  485. { 0x3622, 0x04 },
  486. { 0x3626, 0x21 },
  487. { 0x3627, 0x30 },
  488. { 0x3630, 0x44 },
  489. { 0x3631, 0x35 },
  490. { 0x3634, 0x60 },
  491. { 0x3636, 0x00 },
  492. { 0x3662, 0x01 },
  493. { 0x3663, 0x70 },
  494. { 0x3664, 0x50 },
  495. { 0x3666, 0x0a },
  496. { 0x3669, 0x1a },
  497. { 0x366a, 0x00 },
  498. { 0x366b, 0x50 },
  499. { 0x3673, 0x01 },
  500. { 0x3674, 0xff },
  501. { 0x3675, 0x03 },
  502. { 0x3705, 0xc1 },
  503. { 0x3709, 0x40 },
  504. { 0x373c, 0x08 },
  505. { 0x3742, 0x00 },
  506. { 0x3757, 0xb3 },
  507. { 0x3788, 0x00 },
  508. { 0x37a8, 0x01 },
  509. { 0x37a9, 0xc0 },
  510. { 0x3800, 0x00 },
  511. { 0x3801, 0x04 },
  512. { 0x3802, 0x00 },
  513. { 0x3803, 0x04 },
  514. { 0x3804, 0x02 },
  515. { 0x3805, 0x8b },
  516. { 0x3806, 0x01 },
  517. { 0x3807, 0xeb },
  518. { 0x3808, 0x02 }, /* width high */
  519. { 0x3809, 0x80 }, /* width low */
  520. { 0x380a, 0x01 }, /* height high */
  521. { 0x380b, 0xe0 }, /* height low */
  522. { 0x380c, 0x03 }, /* total horiz timing high */
  523. { 0x380d, 0xa0 }, /* total horiz timing low */
  524. { 0x380e, 0x02 }, /* total vertical timing high */
  525. { 0x380f, 0x3c }, /* total vertical timing low */
  526. { 0x3810, 0x00 },
  527. { 0x3811, 0x04 },
  528. { 0x3812, 0x00 },
  529. { 0x3813, 0x05 },
  530. { 0x3814, 0x11 },
  531. { 0x3815, 0x11 },
  532. { 0x3820, 0x40 },
  533. { 0x3821, 0x00 },
  534. { 0x382f, 0x0e },
  535. { 0x3832, 0x00 },
  536. { 0x3833, 0x05 },
  537. { 0x3834, 0x00 },
  538. { 0x3835, 0x0c },
  539. { 0x3837, 0x00 },
  540. { 0x3b80, 0x00 },
  541. { 0x3b81, 0xa5 },
  542. { 0x3b82, 0x10 },
  543. { 0x3b83, 0x00 },
  544. { 0x3b84, 0x08 },
  545. { 0x3b85, 0x00 },
  546. { 0x3b86, 0x01 },
  547. { 0x3b87, 0x00 },
  548. { 0x3b88, 0x00 },
  549. { 0x3b89, 0x00 },
  550. { 0x3b8a, 0x00 },
  551. { 0x3b8b, 0x05 },
  552. { 0x3b8c, 0x00 },
  553. { 0x3b8d, 0x00 },
  554. { 0x3b8e, 0x00 },
  555. { 0x3b8f, 0x1a },
  556. { 0x3b94, 0x05 },
  557. { 0x3b95, 0xf2 },
  558. { 0x3b96, 0x40 },
  559. { 0x3c00, 0x89 },
  560. { 0x3c01, 0x63 },
  561. { 0x3c02, 0x01 },
  562. { 0x3c03, 0x00 },
  563. { 0x3c04, 0x00 },
  564. { 0x3c05, 0x03 },
  565. { 0x3c06, 0x00 },
  566. { 0x3c07, 0x06 },
  567. { 0x3c0c, 0x01 },
  568. { 0x3c0d, 0xd0 },
  569. { 0x3c0e, 0x02 },
  570. { 0x3c0f, 0x0a },
  571. { 0x4001, 0x42 },
  572. { 0x4004, 0x04 },
  573. { 0x4005, 0x00 },
  574. { 0x404e, 0x01 },
  575. { 0x4300, 0xff },
  576. { 0x4301, 0x00 },
  577. { 0x4315, 0x00 },
  578. { 0x4501, 0x48 },
  579. { 0x4600, 0x00 },
  580. { 0x4601, 0x4e },
  581. { 0x4801, 0x0f },
  582. { 0x4806, 0x0f },
  583. { 0x4819, 0xaa },
  584. { 0x4823, 0x3e },
  585. { 0x4837, 0x19 },
  586. { 0x4a0d, 0x00 },
  587. { 0x4a47, 0x7f },
  588. { 0x4a49, 0xf0 },
  589. { 0x4a4b, 0x30 },
  590. { 0x5000, 0x85 },
  591. { 0x5001, 0x80 },
  592. };
  593. static const unsigned long supported_xclk_rates[] = {
  594. [OV7251_19_2_MHZ] = 19200000,
  595. [OV7251_24_MHZ] = 24000000,
  596. };
  597. static const s64 link_freq[] = {
  598. [OV7251_LINK_FREQ_240_MHZ] = 240000000,
  599. [OV7251_LINK_FREQ_319_2_MHZ] = 319200000,
  600. };
  601. static const s64 pixel_rates[] = {
  602. [OV7251_LINK_FREQ_240_MHZ] = 48000000,
  603. [OV7251_LINK_FREQ_319_2_MHZ] = 63840000,
  604. };
  605. static const struct ov7251_mode_info ov7251_mode_info_data[] = {
  606. {
  607. .width = 640,
  608. .height = 480,
  609. .vts = 1724,
  610. .data = ov7251_setting_vga_30fps,
  611. .data_size = ARRAY_SIZE(ov7251_setting_vga_30fps),
  612. .exposure_max = 1704,
  613. .exposure_def = 504,
  614. .timeperframe = {
  615. .numerator = 100,
  616. .denominator = 3000
  617. }
  618. },
  619. {
  620. .width = 640,
  621. .height = 480,
  622. .vts = 860,
  623. .data = ov7251_setting_vga_60fps,
  624. .data_size = ARRAY_SIZE(ov7251_setting_vga_60fps),
  625. .exposure_max = 840,
  626. .exposure_def = 504,
  627. .timeperframe = {
  628. .numerator = 100,
  629. .denominator = 6014
  630. }
  631. },
  632. {
  633. .width = 640,
  634. .height = 480,
  635. .vts = 572,
  636. .data = ov7251_setting_vga_90fps,
  637. .data_size = ARRAY_SIZE(ov7251_setting_vga_90fps),
  638. .exposure_max = 552,
  639. .exposure_def = 504,
  640. .timeperframe = {
  641. .numerator = 100,
  642. .denominator = 9043
  643. }
  644. },
  645. };
  646. static int ov7251_regulators_enable(struct ov7251 *ov7251)
  647. {
  648. int ret;
  649. /* OV7251 power up sequence requires core regulator
  650. * to be enabled not earlier than io regulator
  651. */
  652. ret = regulator_enable(ov7251->io_regulator);
  653. if (ret < 0) {
  654. dev_err(ov7251->dev, "set io voltage failed\n");
  655. return ret;
  656. }
  657. ret = regulator_enable(ov7251->analog_regulator);
  658. if (ret) {
  659. dev_err(ov7251->dev, "set analog voltage failed\n");
  660. goto err_disable_io;
  661. }
  662. ret = regulator_enable(ov7251->core_regulator);
  663. if (ret) {
  664. dev_err(ov7251->dev, "set core voltage failed\n");
  665. goto err_disable_analog;
  666. }
  667. return 0;
  668. err_disable_analog:
  669. regulator_disable(ov7251->analog_regulator);
  670. err_disable_io:
  671. regulator_disable(ov7251->io_regulator);
  672. return ret;
  673. }
  674. static void ov7251_regulators_disable(struct ov7251 *ov7251)
  675. {
  676. int ret;
  677. ret = regulator_disable(ov7251->core_regulator);
  678. if (ret < 0)
  679. dev_err(ov7251->dev, "core regulator disable failed\n");
  680. ret = regulator_disable(ov7251->analog_regulator);
  681. if (ret < 0)
  682. dev_err(ov7251->dev, "analog regulator disable failed\n");
  683. ret = regulator_disable(ov7251->io_regulator);
  684. if (ret < 0)
  685. dev_err(ov7251->dev, "io regulator disable failed\n");
  686. }
  687. static int ov7251_write_reg(struct ov7251 *ov7251, u16 reg, u8 val)
  688. {
  689. u8 regbuf[3];
  690. int ret;
  691. regbuf[0] = reg >> 8;
  692. regbuf[1] = reg & 0xff;
  693. regbuf[2] = val;
  694. ret = i2c_master_send(ov7251->i2c_client, regbuf, 3);
  695. if (ret < 0) {
  696. dev_err(ov7251->dev, "%s: write reg error %d: reg=%x, val=%x\n",
  697. __func__, ret, reg, val);
  698. return ret;
  699. }
  700. return 0;
  701. }
  702. static int ov7251_write_seq_regs(struct ov7251 *ov7251, u16 reg, u8 *val,
  703. u8 num)
  704. {
  705. u8 regbuf[5];
  706. u8 nregbuf = sizeof(reg) + num * sizeof(*val);
  707. int ret = 0;
  708. if (nregbuf > sizeof(regbuf))
  709. return -EINVAL;
  710. regbuf[0] = reg >> 8;
  711. regbuf[1] = reg & 0xff;
  712. memcpy(regbuf + 2, val, num);
  713. ret = i2c_master_send(ov7251->i2c_client, regbuf, nregbuf);
  714. if (ret < 0) {
  715. dev_err(ov7251->dev,
  716. "%s: write seq regs error %d: first reg=%x\n",
  717. __func__, ret, reg);
  718. return ret;
  719. }
  720. return 0;
  721. }
  722. static int ov7251_read_reg(struct ov7251 *ov7251, u16 reg, u8 *val)
  723. {
  724. u8 regbuf[2];
  725. int ret;
  726. regbuf[0] = reg >> 8;
  727. regbuf[1] = reg & 0xff;
  728. ret = i2c_master_send(ov7251->i2c_client, regbuf, 2);
  729. if (ret < 0) {
  730. dev_err(ov7251->dev, "%s: write reg error %d: reg=%x\n",
  731. __func__, ret, reg);
  732. return ret;
  733. }
  734. ret = i2c_master_recv(ov7251->i2c_client, val, 1);
  735. if (ret < 0) {
  736. dev_err(ov7251->dev, "%s: read reg error %d: reg=%x\n",
  737. __func__, ret, reg);
  738. return ret;
  739. }
  740. return 0;
  741. }
  742. static int ov7251_pll_configure(struct ov7251 *ov7251)
  743. {
  744. const struct ov7251_pll_cfgs *configs;
  745. int ret;
  746. configs = ov7251->pll_cfgs;
  747. ret = ov7251_write_reg(ov7251, OV7251_PLL1_PRE_DIV_REG,
  748. configs->pll1[ov7251->link_freq_idx]->pre_div);
  749. if (ret < 0)
  750. return ret;
  751. ret = ov7251_write_reg(ov7251, OV7251_PLL1_MULT_REG,
  752. configs->pll1[ov7251->link_freq_idx]->mult);
  753. if (ret < 0)
  754. return ret;
  755. ret = ov7251_write_reg(ov7251, OV7251_PLL1_DIVIDER_REG,
  756. configs->pll1[ov7251->link_freq_idx]->div);
  757. if (ret < 0)
  758. return ret;
  759. ret = ov7251_write_reg(ov7251, OV7251_PLL1_PIX_DIV_REG,
  760. configs->pll1[ov7251->link_freq_idx]->pix_div);
  761. if (ret < 0)
  762. return ret;
  763. ret = ov7251_write_reg(ov7251, OV7251_PLL1_MIPI_DIV_REG,
  764. configs->pll1[ov7251->link_freq_idx]->mipi_div);
  765. if (ret < 0)
  766. return ret;
  767. ret = ov7251_write_reg(ov7251, OV7251_PLL2_PRE_DIV_REG,
  768. configs->pll2->pre_div);
  769. if (ret < 0)
  770. return ret;
  771. ret = ov7251_write_reg(ov7251, OV7251_PLL2_MULT_REG,
  772. configs->pll2->mult);
  773. if (ret < 0)
  774. return ret;
  775. ret = ov7251_write_reg(ov7251, OV7251_PLL2_DIVIDER_REG,
  776. configs->pll2->div);
  777. if (ret < 0)
  778. return ret;
  779. ret = ov7251_write_reg(ov7251, OV7251_PLL2_SYS_DIV_REG,
  780. configs->pll2->sys_div);
  781. if (ret < 0)
  782. return ret;
  783. ret = ov7251_write_reg(ov7251, OV7251_PLL2_ADC_DIV_REG,
  784. configs->pll2->adc_div);
  785. return ret;
  786. }
  787. static int ov7251_set_exposure(struct ov7251 *ov7251, s32 exposure)
  788. {
  789. u16 reg;
  790. u8 val[3];
  791. reg = OV7251_AEC_EXPO_0;
  792. val[0] = (exposure & 0xf000) >> 12; /* goes to OV7251_AEC_EXPO_0 */
  793. val[1] = (exposure & 0x0ff0) >> 4; /* goes to OV7251_AEC_EXPO_1 */
  794. val[2] = (exposure & 0x000f) << 4; /* goes to OV7251_AEC_EXPO_2 */
  795. return ov7251_write_seq_regs(ov7251, reg, val, 3);
  796. }
  797. static int ov7251_set_gain(struct ov7251 *ov7251, s32 gain)
  798. {
  799. u16 reg;
  800. u8 val[2];
  801. reg = OV7251_AEC_AGC_ADJ_0;
  802. val[0] = (gain & 0x0300) >> 8; /* goes to OV7251_AEC_AGC_ADJ_0 */
  803. val[1] = gain & 0xff; /* goes to OV7251_AEC_AGC_ADJ_1 */
  804. return ov7251_write_seq_regs(ov7251, reg, val, 2);
  805. }
  806. static int ov7251_set_register_array(struct ov7251 *ov7251,
  807. const struct reg_value *settings,
  808. unsigned int num_settings)
  809. {
  810. unsigned int i;
  811. int ret;
  812. for (i = 0; i < num_settings; ++i, ++settings) {
  813. ret = ov7251_write_reg(ov7251, settings->reg, settings->val);
  814. if (ret < 0)
  815. return ret;
  816. }
  817. return 0;
  818. }
  819. static int ov7251_set_power_on(struct device *dev)
  820. {
  821. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  822. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  823. struct ov7251 *ov7251 = to_ov7251(sd);
  824. int ret;
  825. u32 wait_us;
  826. ret = ov7251_regulators_enable(ov7251);
  827. if (ret < 0)
  828. return ret;
  829. ret = clk_prepare_enable(ov7251->xclk);
  830. if (ret < 0) {
  831. dev_err(ov7251->dev, "clk prepare enable failed\n");
  832. ov7251_regulators_disable(ov7251);
  833. return ret;
  834. }
  835. gpiod_set_value_cansleep(ov7251->enable_gpio, 1);
  836. /* wait at least 65536 external clock cycles */
  837. wait_us = DIV_ROUND_UP(65536 * 1000,
  838. DIV_ROUND_UP(ov7251->xclk_freq, 1000));
  839. usleep_range(wait_us, wait_us + 1000);
  840. ret = ov7251_set_register_array(ov7251,
  841. ov7251_global_init_setting,
  842. ARRAY_SIZE(ov7251_global_init_setting));
  843. if (ret < 0) {
  844. dev_err(ov7251->dev, "error during global init\n");
  845. gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
  846. clk_disable_unprepare(ov7251->xclk);
  847. ov7251_regulators_disable(ov7251);
  848. return ret;
  849. }
  850. return ret;
  851. }
  852. static int ov7251_set_power_off(struct device *dev)
  853. {
  854. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  855. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  856. struct ov7251 *ov7251 = to_ov7251(sd);
  857. clk_disable_unprepare(ov7251->xclk);
  858. gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
  859. ov7251_regulators_disable(ov7251);
  860. return 0;
  861. }
  862. static int ov7251_set_hflip(struct ov7251 *ov7251, s32 value)
  863. {
  864. u8 val = ov7251->timing_format2;
  865. int ret;
  866. if (value)
  867. val |= OV7251_TIMING_FORMAT2_MIRROR;
  868. else
  869. val &= ~OV7251_TIMING_FORMAT2_MIRROR;
  870. ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT2, val);
  871. if (!ret)
  872. ov7251->timing_format2 = val;
  873. return ret;
  874. }
  875. static int ov7251_set_vflip(struct ov7251 *ov7251, s32 value)
  876. {
  877. u8 val = ov7251->timing_format1;
  878. int ret;
  879. if (value)
  880. val |= OV7251_TIMING_FORMAT1_VFLIP;
  881. else
  882. val &= ~OV7251_TIMING_FORMAT1_VFLIP;
  883. ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT1, val);
  884. if (!ret)
  885. ov7251->timing_format1 = val;
  886. return ret;
  887. }
  888. static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value)
  889. {
  890. u8 val = ov7251->pre_isp_00;
  891. int ret;
  892. if (value)
  893. val |= OV7251_PRE_ISP_00_TEST_PATTERN;
  894. else
  895. val &= ~OV7251_PRE_ISP_00_TEST_PATTERN;
  896. ret = ov7251_write_reg(ov7251, OV7251_PRE_ISP_00, val);
  897. if (!ret)
  898. ov7251->pre_isp_00 = val;
  899. return ret;
  900. }
  901. static const char * const ov7251_test_pattern_menu[] = {
  902. "Disabled",
  903. "Vertical Pattern Bars",
  904. };
  905. static int ov7251_vts_configure(struct ov7251 *ov7251, s32 vblank)
  906. {
  907. u8 vts[2];
  908. vts[0] = ((ov7251->current_mode->height + vblank) & 0xff00) >> 8;
  909. vts[1] = ((ov7251->current_mode->height + vblank) & 0x00ff);
  910. return ov7251_write_seq_regs(ov7251, OV7251_TIMING_VTS_REG, vts, 2);
  911. }
  912. static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)
  913. {
  914. struct ov7251 *ov7251 = container_of(ctrl->handler,
  915. struct ov7251, ctrls);
  916. int ret;
  917. /* If VBLANK is altered we need to update exposure to compensate */
  918. if (ctrl->id == V4L2_CID_VBLANK) {
  919. int exposure_max;
  920. exposure_max = ov7251->current_mode->height + ctrl->val -
  921. OV7251_INTEGRATION_MARGIN;
  922. __v4l2_ctrl_modify_range(ov7251->exposure,
  923. ov7251->exposure->minimum,
  924. exposure_max,
  925. ov7251->exposure->step,
  926. min(ov7251->exposure->val,
  927. exposure_max));
  928. }
  929. /* v4l2_ctrl_lock() locks our mutex */
  930. if (!pm_runtime_get_if_in_use(ov7251->dev))
  931. return 0;
  932. switch (ctrl->id) {
  933. case V4L2_CID_EXPOSURE:
  934. ret = ov7251_set_exposure(ov7251, ctrl->val);
  935. break;
  936. case V4L2_CID_GAIN:
  937. ret = ov7251_set_gain(ov7251, ctrl->val);
  938. break;
  939. case V4L2_CID_TEST_PATTERN:
  940. ret = ov7251_set_test_pattern(ov7251, ctrl->val);
  941. break;
  942. case V4L2_CID_HFLIP:
  943. ret = ov7251_set_hflip(ov7251, ctrl->val);
  944. break;
  945. case V4L2_CID_VFLIP:
  946. ret = ov7251_set_vflip(ov7251, ctrl->val);
  947. break;
  948. case V4L2_CID_VBLANK:
  949. ret = ov7251_vts_configure(ov7251, ctrl->val);
  950. break;
  951. default:
  952. ret = -EINVAL;
  953. break;
  954. }
  955. pm_runtime_put(ov7251->dev);
  956. return ret;
  957. }
  958. static const struct v4l2_ctrl_ops ov7251_ctrl_ops = {
  959. .s_ctrl = ov7251_s_ctrl,
  960. };
  961. static int ov7251_enum_mbus_code(struct v4l2_subdev *sd,
  962. struct v4l2_subdev_state *sd_state,
  963. struct v4l2_subdev_mbus_code_enum *code)
  964. {
  965. if (code->index > 0)
  966. return -EINVAL;
  967. code->code = MEDIA_BUS_FMT_Y10_1X10;
  968. return 0;
  969. }
  970. static int ov7251_enum_frame_size(struct v4l2_subdev *subdev,
  971. struct v4l2_subdev_state *sd_state,
  972. struct v4l2_subdev_frame_size_enum *fse)
  973. {
  974. if (fse->code != MEDIA_BUS_FMT_Y10_1X10)
  975. return -EINVAL;
  976. if (fse->index >= ARRAY_SIZE(ov7251_mode_info_data))
  977. return -EINVAL;
  978. fse->min_width = ov7251_mode_info_data[fse->index].width;
  979. fse->max_width = ov7251_mode_info_data[fse->index].width;
  980. fse->min_height = ov7251_mode_info_data[fse->index].height;
  981. fse->max_height = ov7251_mode_info_data[fse->index].height;
  982. return 0;
  983. }
  984. static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev,
  985. struct v4l2_subdev_state *sd_state,
  986. struct v4l2_subdev_frame_interval_enum *fie)
  987. {
  988. unsigned int index = fie->index;
  989. unsigned int i;
  990. for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
  991. if (fie->width != ov7251_mode_info_data[i].width ||
  992. fie->height != ov7251_mode_info_data[i].height)
  993. continue;
  994. if (index-- == 0) {
  995. fie->interval = ov7251_mode_info_data[i].timeperframe;
  996. return 0;
  997. }
  998. }
  999. return -EINVAL;
  1000. }
  1001. static struct v4l2_mbus_framefmt *
  1002. __ov7251_get_pad_format(struct ov7251 *ov7251,
  1003. struct v4l2_subdev_state *sd_state,
  1004. unsigned int pad,
  1005. enum v4l2_subdev_format_whence which)
  1006. {
  1007. switch (which) {
  1008. case V4L2_SUBDEV_FORMAT_TRY:
  1009. return v4l2_subdev_get_try_format(&ov7251->sd, sd_state, pad);
  1010. case V4L2_SUBDEV_FORMAT_ACTIVE:
  1011. return &ov7251->fmt;
  1012. default:
  1013. return NULL;
  1014. }
  1015. }
  1016. static int ov7251_get_format(struct v4l2_subdev *sd,
  1017. struct v4l2_subdev_state *sd_state,
  1018. struct v4l2_subdev_format *format)
  1019. {
  1020. struct ov7251 *ov7251 = to_ov7251(sd);
  1021. mutex_lock(&ov7251->lock);
  1022. format->format = *__ov7251_get_pad_format(ov7251, sd_state,
  1023. format->pad,
  1024. format->which);
  1025. mutex_unlock(&ov7251->lock);
  1026. return 0;
  1027. }
  1028. static struct v4l2_rect *
  1029. __ov7251_get_pad_crop(struct ov7251 *ov7251,
  1030. struct v4l2_subdev_state *sd_state,
  1031. unsigned int pad, enum v4l2_subdev_format_whence which)
  1032. {
  1033. switch (which) {
  1034. case V4L2_SUBDEV_FORMAT_TRY:
  1035. return v4l2_subdev_get_try_crop(&ov7251->sd, sd_state, pad);
  1036. case V4L2_SUBDEV_FORMAT_ACTIVE:
  1037. return &ov7251->crop;
  1038. default:
  1039. return NULL;
  1040. }
  1041. }
  1042. static inline u32 avg_fps(const struct v4l2_fract *t)
  1043. {
  1044. return (t->denominator + (t->numerator >> 1)) / t->numerator;
  1045. }
  1046. static const struct ov7251_mode_info *
  1047. ov7251_find_mode_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
  1048. {
  1049. const struct ov7251_mode_info *mode = ov7251->current_mode;
  1050. unsigned int fps_req = avg_fps(timeperframe);
  1051. unsigned int max_dist_match = (unsigned int) -1;
  1052. unsigned int i, n = 0;
  1053. for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
  1054. unsigned int dist;
  1055. unsigned int fps_tmp;
  1056. if (mode->width != ov7251_mode_info_data[i].width ||
  1057. mode->height != ov7251_mode_info_data[i].height)
  1058. continue;
  1059. fps_tmp = avg_fps(&ov7251_mode_info_data[i].timeperframe);
  1060. dist = abs(fps_req - fps_tmp);
  1061. if (dist < max_dist_match) {
  1062. n = i;
  1063. max_dist_match = dist;
  1064. }
  1065. }
  1066. return &ov7251_mode_info_data[n];
  1067. }
  1068. static int ov7251_set_format(struct v4l2_subdev *sd,
  1069. struct v4l2_subdev_state *sd_state,
  1070. struct v4l2_subdev_format *format)
  1071. {
  1072. struct ov7251 *ov7251 = to_ov7251(sd);
  1073. struct v4l2_mbus_framefmt *__format;
  1074. int vblank_max, vblank_def;
  1075. struct v4l2_rect *__crop;
  1076. const struct ov7251_mode_info *new_mode;
  1077. int ret = 0;
  1078. mutex_lock(&ov7251->lock);
  1079. __crop = __ov7251_get_pad_crop(ov7251, sd_state, format->pad,
  1080. format->which);
  1081. new_mode = v4l2_find_nearest_size(ov7251_mode_info_data,
  1082. ARRAY_SIZE(ov7251_mode_info_data),
  1083. width, height,
  1084. format->format.width, format->format.height);
  1085. __crop->width = new_mode->width;
  1086. __crop->height = new_mode->height;
  1087. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1088. ret = __v4l2_ctrl_modify_range(ov7251->exposure,
  1089. 1, new_mode->exposure_max,
  1090. 1, new_mode->exposure_def);
  1091. if (ret < 0)
  1092. goto exit;
  1093. ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
  1094. new_mode->exposure_def);
  1095. if (ret < 0)
  1096. goto exit;
  1097. ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
  1098. if (ret < 0)
  1099. goto exit;
  1100. vblank_max = OV7251_TIMING_MAX_VTS - new_mode->height;
  1101. vblank_def = new_mode->vts - new_mode->height;
  1102. ret = __v4l2_ctrl_modify_range(ov7251->vblank,
  1103. OV7251_TIMING_MIN_VTS,
  1104. vblank_max, 1, vblank_def);
  1105. if (ret < 0)
  1106. goto exit;
  1107. ov7251->current_mode = new_mode;
  1108. }
  1109. __format = __ov7251_get_pad_format(ov7251, sd_state, format->pad,
  1110. format->which);
  1111. __format->width = __crop->width;
  1112. __format->height = __crop->height;
  1113. __format->code = MEDIA_BUS_FMT_Y10_1X10;
  1114. __format->field = V4L2_FIELD_NONE;
  1115. __format->colorspace = V4L2_COLORSPACE_SRGB;
  1116. __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
  1117. __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
  1118. __format->colorspace, __format->ycbcr_enc);
  1119. __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
  1120. format->format = *__format;
  1121. exit:
  1122. mutex_unlock(&ov7251->lock);
  1123. return ret;
  1124. }
  1125. static int ov7251_entity_init_cfg(struct v4l2_subdev *subdev,
  1126. struct v4l2_subdev_state *sd_state)
  1127. {
  1128. struct v4l2_subdev_format fmt = {
  1129. .which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
  1130. : V4L2_SUBDEV_FORMAT_ACTIVE,
  1131. .format = {
  1132. .width = 640,
  1133. .height = 480
  1134. }
  1135. };
  1136. ov7251_set_format(subdev, sd_state, &fmt);
  1137. return 0;
  1138. }
  1139. static int ov7251_get_selection(struct v4l2_subdev *sd,
  1140. struct v4l2_subdev_state *sd_state,
  1141. struct v4l2_subdev_selection *sel)
  1142. {
  1143. struct ov7251 *ov7251 = to_ov7251(sd);
  1144. switch (sel->target) {
  1145. case V4L2_SEL_TGT_CROP_DEFAULT:
  1146. case V4L2_SEL_TGT_CROP:
  1147. mutex_lock(&ov7251->lock);
  1148. sel->r = *__ov7251_get_pad_crop(ov7251, sd_state, sel->pad,
  1149. sel->which);
  1150. mutex_unlock(&ov7251->lock);
  1151. break;
  1152. case V4L2_SEL_TGT_NATIVE_SIZE:
  1153. sel->r.top = 0;
  1154. sel->r.left = 0;
  1155. sel->r.width = OV7251_NATIVE_WIDTH;
  1156. sel->r.height = OV7251_NATIVE_HEIGHT;
  1157. break;
  1158. case V4L2_SEL_TGT_CROP_BOUNDS:
  1159. sel->r.top = OV7251_ACTIVE_START_TOP;
  1160. sel->r.left = OV7251_ACTIVE_START_LEFT;
  1161. sel->r.width = OV7251_ACTIVE_WIDTH;
  1162. sel->r.height = OV7251_ACTIVE_HEIGHT;
  1163. break;
  1164. default:
  1165. return -EINVAL;
  1166. }
  1167. return 0;
  1168. }
  1169. static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable)
  1170. {
  1171. struct ov7251 *ov7251 = to_ov7251(subdev);
  1172. int ret;
  1173. mutex_lock(&ov7251->lock);
  1174. if (enable) {
  1175. ret = pm_runtime_get_sync(ov7251->dev);
  1176. if (ret < 0)
  1177. goto err_power_down;
  1178. ret = ov7251_pll_configure(ov7251);
  1179. if (ret) {
  1180. dev_err(ov7251->dev, "error configuring PLLs\n");
  1181. goto err_power_down;
  1182. }
  1183. ret = ov7251_set_register_array(ov7251,
  1184. ov7251->current_mode->data,
  1185. ov7251->current_mode->data_size);
  1186. if (ret < 0) {
  1187. dev_err(ov7251->dev, "could not set mode %dx%d\n",
  1188. ov7251->current_mode->width,
  1189. ov7251->current_mode->height);
  1190. goto err_power_down;
  1191. }
  1192. ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
  1193. if (ret < 0) {
  1194. dev_err(ov7251->dev, "could not sync v4l2 controls\n");
  1195. goto err_power_down;
  1196. }
  1197. ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
  1198. OV7251_SC_MODE_SELECT_STREAMING);
  1199. if (ret)
  1200. goto err_power_down;
  1201. } else {
  1202. ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
  1203. OV7251_SC_MODE_SELECT_SW_STANDBY);
  1204. pm_runtime_put(ov7251->dev);
  1205. }
  1206. mutex_unlock(&ov7251->lock);
  1207. return ret;
  1208. err_power_down:
  1209. pm_runtime_put(ov7251->dev);
  1210. mutex_unlock(&ov7251->lock);
  1211. return ret;
  1212. }
  1213. static int ov7251_get_frame_interval(struct v4l2_subdev *subdev,
  1214. struct v4l2_subdev_frame_interval *fi)
  1215. {
  1216. struct ov7251 *ov7251 = to_ov7251(subdev);
  1217. mutex_lock(&ov7251->lock);
  1218. fi->interval = ov7251->current_mode->timeperframe;
  1219. mutex_unlock(&ov7251->lock);
  1220. return 0;
  1221. }
  1222. static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,
  1223. struct v4l2_subdev_frame_interval *fi)
  1224. {
  1225. struct ov7251 *ov7251 = to_ov7251(subdev);
  1226. const struct ov7251_mode_info *new_mode;
  1227. int ret = 0;
  1228. mutex_lock(&ov7251->lock);
  1229. new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval);
  1230. if (new_mode != ov7251->current_mode) {
  1231. ret = __v4l2_ctrl_modify_range(ov7251->exposure,
  1232. 1, new_mode->exposure_max,
  1233. 1, new_mode->exposure_def);
  1234. if (ret < 0)
  1235. goto exit;
  1236. ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
  1237. new_mode->exposure_def);
  1238. if (ret < 0)
  1239. goto exit;
  1240. ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
  1241. if (ret < 0)
  1242. goto exit;
  1243. ov7251->current_mode = new_mode;
  1244. }
  1245. fi->interval = ov7251->current_mode->timeperframe;
  1246. exit:
  1247. mutex_unlock(&ov7251->lock);
  1248. return ret;
  1249. }
  1250. static const struct v4l2_subdev_video_ops ov7251_video_ops = {
  1251. .s_stream = ov7251_s_stream,
  1252. .g_frame_interval = ov7251_get_frame_interval,
  1253. .s_frame_interval = ov7251_set_frame_interval,
  1254. };
  1255. static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = {
  1256. .init_cfg = ov7251_entity_init_cfg,
  1257. .enum_mbus_code = ov7251_enum_mbus_code,
  1258. .enum_frame_size = ov7251_enum_frame_size,
  1259. .enum_frame_interval = ov7251_enum_frame_ival,
  1260. .get_fmt = ov7251_get_format,
  1261. .set_fmt = ov7251_set_format,
  1262. .get_selection = ov7251_get_selection,
  1263. };
  1264. static const struct v4l2_subdev_ops ov7251_subdev_ops = {
  1265. .video = &ov7251_video_ops,
  1266. .pad = &ov7251_subdev_pad_ops,
  1267. };
  1268. static int ov7251_check_hwcfg(struct ov7251 *ov7251)
  1269. {
  1270. struct fwnode_handle *fwnode = dev_fwnode(ov7251->dev);
  1271. struct v4l2_fwnode_endpoint bus_cfg = {
  1272. .bus_type = V4L2_MBUS_CSI2_DPHY,
  1273. };
  1274. struct fwnode_handle *endpoint;
  1275. unsigned int i, j;
  1276. int ret;
  1277. endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1278. if (!endpoint)
  1279. return -EPROBE_DEFER; /* could be provided by cio2-bridge */
  1280. ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
  1281. fwnode_handle_put(endpoint);
  1282. if (ret)
  1283. return dev_err_probe(ov7251->dev, ret,
  1284. "parsing endpoint node failed\n");
  1285. if (!bus_cfg.nr_of_link_frequencies) {
  1286. ret = dev_err_probe(ov7251->dev, -EINVAL,
  1287. "no link frequencies defined\n");
  1288. goto out_free_bus_cfg;
  1289. }
  1290. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
  1291. for (j = 0; j < ARRAY_SIZE(link_freq); j++)
  1292. if (bus_cfg.link_frequencies[i] == link_freq[j])
  1293. break;
  1294. if (j < ARRAY_SIZE(link_freq))
  1295. break;
  1296. }
  1297. if (i == bus_cfg.nr_of_link_frequencies) {
  1298. ret = dev_err_probe(ov7251->dev, -EINVAL,
  1299. "no supported link freq found\n");
  1300. goto out_free_bus_cfg;
  1301. }
  1302. ov7251->link_freq_idx = i;
  1303. out_free_bus_cfg:
  1304. v4l2_fwnode_endpoint_free(&bus_cfg);
  1305. return ret;
  1306. }
  1307. static int ov7251_detect_chip(struct ov7251 *ov7251)
  1308. {
  1309. u8 chip_id_high, chip_id_low, chip_rev;
  1310. int ret;
  1311. ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_HIGH, &chip_id_high);
  1312. if (ret < 0 || chip_id_high != OV7251_CHIP_ID_HIGH_BYTE)
  1313. return dev_err_probe(ov7251->dev, -ENODEV,
  1314. "could not read ID high\n");
  1315. ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_LOW, &chip_id_low);
  1316. if (ret < 0 || chip_id_low != OV7251_CHIP_ID_LOW_BYTE)
  1317. return dev_err_probe(ov7251->dev, -ENODEV,
  1318. "could not read ID low\n");
  1319. ret = ov7251_read_reg(ov7251, OV7251_SC_GP_IO_IN1, &chip_rev);
  1320. if (ret < 0)
  1321. return dev_err_probe(ov7251->dev, -ENODEV,
  1322. "could not read revision\n");
  1323. chip_rev >>= 4;
  1324. dev_info(ov7251->dev,
  1325. "OV7251 revision %x (%s) detected at address 0x%02x\n",
  1326. chip_rev,
  1327. chip_rev == 0x4 ? "1A / 1B" :
  1328. chip_rev == 0x5 ? "1C / 1D" :
  1329. chip_rev == 0x6 ? "1E" :
  1330. chip_rev == 0x7 ? "1F" : "unknown",
  1331. ov7251->i2c_client->addr);
  1332. return 0;
  1333. }
  1334. static int ov7251_init_ctrls(struct ov7251 *ov7251)
  1335. {
  1336. int vblank_max, vblank_def;
  1337. s64 pixel_rate;
  1338. int hblank;
  1339. v4l2_ctrl_handler_init(&ov7251->ctrls, 7);
  1340. ov7251->ctrls.lock = &ov7251->lock;
  1341. v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1342. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1343. v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1344. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1345. ov7251->exposure = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1346. V4L2_CID_EXPOSURE, 1, 32, 1, 32);
  1347. ov7251->gain = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1348. V4L2_CID_GAIN, 16, 1023, 1, 16);
  1349. v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops,
  1350. V4L2_CID_TEST_PATTERN,
  1351. ARRAY_SIZE(ov7251_test_pattern_menu) - 1,
  1352. 0, 0, ov7251_test_pattern_menu);
  1353. pixel_rate = pixel_rates[ov7251->link_freq_idx];
  1354. ov7251->pixel_clock = v4l2_ctrl_new_std(&ov7251->ctrls,
  1355. &ov7251_ctrl_ops,
  1356. V4L2_CID_PIXEL_RATE,
  1357. pixel_rate, INT_MAX,
  1358. pixel_rate, pixel_rate);
  1359. ov7251->link_freq = v4l2_ctrl_new_int_menu(&ov7251->ctrls,
  1360. &ov7251_ctrl_ops,
  1361. V4L2_CID_LINK_FREQ,
  1362. ARRAY_SIZE(link_freq) - 1,
  1363. ov7251->link_freq_idx,
  1364. link_freq);
  1365. if (ov7251->link_freq)
  1366. ov7251->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1367. if (ov7251->pixel_clock)
  1368. ov7251->pixel_clock->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1369. hblank = OV7251_FIXED_PPL - ov7251->current_mode->width;
  1370. ov7251->hblank = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1371. V4L2_CID_HBLANK, hblank, hblank, 1,
  1372. hblank);
  1373. if (ov7251->hblank)
  1374. ov7251->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1375. vblank_max = OV7251_TIMING_MAX_VTS - ov7251->current_mode->height;
  1376. vblank_def = ov7251->current_mode->vts - ov7251->current_mode->height;
  1377. ov7251->vblank = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1378. V4L2_CID_VBLANK,
  1379. OV7251_TIMING_MIN_VTS, vblank_max, 1,
  1380. vblank_def);
  1381. ov7251->sd.ctrl_handler = &ov7251->ctrls;
  1382. if (ov7251->ctrls.error) {
  1383. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1384. return ov7251->ctrls.error;
  1385. }
  1386. return 0;
  1387. }
  1388. static int ov7251_probe(struct i2c_client *client)
  1389. {
  1390. struct device *dev = &client->dev;
  1391. struct ov7251 *ov7251;
  1392. unsigned int rate = 0, clk_rate = 0;
  1393. int ret;
  1394. int i;
  1395. ov7251 = devm_kzalloc(dev, sizeof(struct ov7251), GFP_KERNEL);
  1396. if (!ov7251)
  1397. return -ENOMEM;
  1398. ov7251->i2c_client = client;
  1399. ov7251->dev = dev;
  1400. ret = ov7251_check_hwcfg(ov7251);
  1401. if (ret)
  1402. return ret;
  1403. /* get system clock (xclk) */
  1404. ov7251->xclk = devm_clk_get_optional(dev, NULL);
  1405. if (IS_ERR(ov7251->xclk))
  1406. return dev_err_probe(dev, PTR_ERR(ov7251->xclk),
  1407. "could not get xclk");
  1408. /*
  1409. * We could have either a 24MHz or 19.2MHz clock rate from either DT or
  1410. * ACPI. We also need to support the IPU3 case which will have both an
  1411. * external clock AND a clock-frequency property.
  1412. */
  1413. ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
  1414. &rate);
  1415. if (ret && !ov7251->xclk)
  1416. return dev_err_probe(dev, ret, "invalid clock config\n");
  1417. clk_rate = clk_get_rate(ov7251->xclk);
  1418. ov7251->xclk_freq = clk_rate ? clk_rate : rate;
  1419. if (ov7251->xclk_freq == 0)
  1420. return dev_err_probe(dev, -EINVAL, "invalid clock frequency\n");
  1421. if (!ret && ov7251->xclk) {
  1422. ret = clk_set_rate(ov7251->xclk, rate);
  1423. if (ret)
  1424. return dev_err_probe(dev, ret,
  1425. "failed to set clock rate\n");
  1426. }
  1427. for (i = 0; i < ARRAY_SIZE(supported_xclk_rates); i++)
  1428. if (ov7251->xclk_freq == supported_xclk_rates[i])
  1429. break;
  1430. if (i == ARRAY_SIZE(supported_xclk_rates))
  1431. return dev_err_probe(dev, -EINVAL,
  1432. "clock rate %u Hz is unsupported\n",
  1433. ov7251->xclk_freq);
  1434. ov7251->pll_cfgs = ov7251_pll_cfgs[i];
  1435. ov7251->io_regulator = devm_regulator_get(dev, "vdddo");
  1436. if (IS_ERR(ov7251->io_regulator)) {
  1437. dev_err(dev, "cannot get io regulator\n");
  1438. return PTR_ERR(ov7251->io_regulator);
  1439. }
  1440. ov7251->core_regulator = devm_regulator_get(dev, "vddd");
  1441. if (IS_ERR(ov7251->core_regulator)) {
  1442. dev_err(dev, "cannot get core regulator\n");
  1443. return PTR_ERR(ov7251->core_regulator);
  1444. }
  1445. ov7251->analog_regulator = devm_regulator_get(dev, "vdda");
  1446. if (IS_ERR(ov7251->analog_regulator)) {
  1447. dev_err(dev, "cannot get analog regulator\n");
  1448. return PTR_ERR(ov7251->analog_regulator);
  1449. }
  1450. ov7251->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
  1451. if (IS_ERR(ov7251->enable_gpio)) {
  1452. dev_err(dev, "cannot get enable gpio\n");
  1453. return PTR_ERR(ov7251->enable_gpio);
  1454. }
  1455. mutex_init(&ov7251->lock);
  1456. ov7251->current_mode = &ov7251_mode_info_data[0];
  1457. ret = ov7251_init_ctrls(ov7251);
  1458. if (ret) {
  1459. dev_err_probe(dev, ret, "error during v4l2 ctrl init\n");
  1460. goto destroy_mutex;
  1461. }
  1462. v4l2_i2c_subdev_init(&ov7251->sd, client, &ov7251_subdev_ops);
  1463. ov7251->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1464. ov7251->pad.flags = MEDIA_PAD_FL_SOURCE;
  1465. ov7251->sd.dev = &client->dev;
  1466. ov7251->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1467. ret = media_entity_pads_init(&ov7251->sd.entity, 1, &ov7251->pad);
  1468. if (ret < 0) {
  1469. dev_err(dev, "could not register media entity\n");
  1470. goto free_ctrl;
  1471. }
  1472. ret = ov7251_set_power_on(ov7251->dev);
  1473. if (ret)
  1474. goto free_entity;
  1475. ret = ov7251_detect_chip(ov7251);
  1476. if (ret)
  1477. goto power_down;
  1478. pm_runtime_set_active(&client->dev);
  1479. pm_runtime_get_noresume(&client->dev);
  1480. pm_runtime_enable(&client->dev);
  1481. ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
  1482. &ov7251->pre_isp_00);
  1483. if (ret < 0) {
  1484. dev_err(dev, "could not read test pattern value\n");
  1485. ret = -ENODEV;
  1486. goto err_pm_runtime;
  1487. }
  1488. ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
  1489. &ov7251->timing_format1);
  1490. if (ret < 0) {
  1491. dev_err(dev, "could not read vflip value\n");
  1492. ret = -ENODEV;
  1493. goto err_pm_runtime;
  1494. }
  1495. ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
  1496. &ov7251->timing_format2);
  1497. if (ret < 0) {
  1498. dev_err(dev, "could not read hflip value\n");
  1499. ret = -ENODEV;
  1500. goto err_pm_runtime;
  1501. }
  1502. pm_runtime_set_autosuspend_delay(&client->dev, 1000);
  1503. pm_runtime_use_autosuspend(&client->dev);
  1504. pm_runtime_put_autosuspend(&client->dev);
  1505. ret = v4l2_async_register_subdev(&ov7251->sd);
  1506. if (ret < 0) {
  1507. dev_err(dev, "could not register v4l2 device\n");
  1508. goto free_entity;
  1509. }
  1510. ov7251_entity_init_cfg(&ov7251->sd, NULL);
  1511. return 0;
  1512. err_pm_runtime:
  1513. pm_runtime_disable(ov7251->dev);
  1514. pm_runtime_put_noidle(ov7251->dev);
  1515. power_down:
  1516. ov7251_set_power_off(ov7251->dev);
  1517. free_entity:
  1518. media_entity_cleanup(&ov7251->sd.entity);
  1519. free_ctrl:
  1520. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1521. destroy_mutex:
  1522. mutex_destroy(&ov7251->lock);
  1523. return ret;
  1524. }
  1525. static void ov7251_remove(struct i2c_client *client)
  1526. {
  1527. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1528. struct ov7251 *ov7251 = to_ov7251(sd);
  1529. v4l2_async_unregister_subdev(&ov7251->sd);
  1530. media_entity_cleanup(&ov7251->sd.entity);
  1531. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1532. mutex_destroy(&ov7251->lock);
  1533. pm_runtime_disable(ov7251->dev);
  1534. if (!pm_runtime_status_suspended(ov7251->dev))
  1535. ov7251_set_power_off(ov7251->dev);
  1536. pm_runtime_set_suspended(ov7251->dev);
  1537. }
  1538. static const struct dev_pm_ops ov7251_pm_ops = {
  1539. SET_RUNTIME_PM_OPS(ov7251_set_power_off, ov7251_set_power_on, NULL)
  1540. };
  1541. static const struct of_device_id ov7251_of_match[] = {
  1542. { .compatible = "ovti,ov7251" },
  1543. { /* sentinel */ }
  1544. };
  1545. MODULE_DEVICE_TABLE(of, ov7251_of_match);
  1546. static const struct acpi_device_id ov7251_acpi_match[] = {
  1547. { "INT347E" },
  1548. { }
  1549. };
  1550. MODULE_DEVICE_TABLE(acpi, ov7251_acpi_match);
  1551. static struct i2c_driver ov7251_i2c_driver = {
  1552. .driver = {
  1553. .of_match_table = ov7251_of_match,
  1554. .acpi_match_table = ov7251_acpi_match,
  1555. .name = "ov7251",
  1556. .pm = &ov7251_pm_ops,
  1557. },
  1558. .probe_new = ov7251_probe,
  1559. .remove = ov7251_remove,
  1560. };
  1561. module_i2c_driver(ov7251_i2c_driver);
  1562. MODULE_DESCRIPTION("Omnivision OV7251 Camera Driver");
  1563. MODULE_AUTHOR("Todor Tomov <[email protected]>");
  1564. MODULE_LICENSE("GPL v2");