ov7251.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  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. usleep_range(1000, 1100);
  836. gpiod_set_value_cansleep(ov7251->enable_gpio, 1);
  837. /* wait at least 65536 external clock cycles */
  838. wait_us = DIV_ROUND_UP(65536 * 1000,
  839. DIV_ROUND_UP(ov7251->xclk_freq, 1000));
  840. usleep_range(wait_us, wait_us + 1000);
  841. ret = ov7251_set_register_array(ov7251,
  842. ov7251_global_init_setting,
  843. ARRAY_SIZE(ov7251_global_init_setting));
  844. if (ret < 0) {
  845. dev_err(ov7251->dev, "error during global init\n");
  846. gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
  847. clk_disable_unprepare(ov7251->xclk);
  848. ov7251_regulators_disable(ov7251);
  849. return ret;
  850. }
  851. return ret;
  852. }
  853. static int ov7251_set_power_off(struct device *dev)
  854. {
  855. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  856. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  857. struct ov7251 *ov7251 = to_ov7251(sd);
  858. clk_disable_unprepare(ov7251->xclk);
  859. gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
  860. ov7251_regulators_disable(ov7251);
  861. return 0;
  862. }
  863. static int ov7251_set_hflip(struct ov7251 *ov7251, s32 value)
  864. {
  865. u8 val = ov7251->timing_format2;
  866. int ret;
  867. if (value)
  868. val |= OV7251_TIMING_FORMAT2_MIRROR;
  869. else
  870. val &= ~OV7251_TIMING_FORMAT2_MIRROR;
  871. ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT2, val);
  872. if (!ret)
  873. ov7251->timing_format2 = val;
  874. return ret;
  875. }
  876. static int ov7251_set_vflip(struct ov7251 *ov7251, s32 value)
  877. {
  878. u8 val = ov7251->timing_format1;
  879. int ret;
  880. if (value)
  881. val |= OV7251_TIMING_FORMAT1_VFLIP;
  882. else
  883. val &= ~OV7251_TIMING_FORMAT1_VFLIP;
  884. ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT1, val);
  885. if (!ret)
  886. ov7251->timing_format1 = val;
  887. return ret;
  888. }
  889. static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value)
  890. {
  891. u8 val = ov7251->pre_isp_00;
  892. int ret;
  893. if (value)
  894. val |= OV7251_PRE_ISP_00_TEST_PATTERN;
  895. else
  896. val &= ~OV7251_PRE_ISP_00_TEST_PATTERN;
  897. ret = ov7251_write_reg(ov7251, OV7251_PRE_ISP_00, val);
  898. if (!ret)
  899. ov7251->pre_isp_00 = val;
  900. return ret;
  901. }
  902. static const char * const ov7251_test_pattern_menu[] = {
  903. "Disabled",
  904. "Vertical Pattern Bars",
  905. };
  906. static int ov7251_vts_configure(struct ov7251 *ov7251, s32 vblank)
  907. {
  908. u8 vts[2];
  909. vts[0] = ((ov7251->current_mode->height + vblank) & 0xff00) >> 8;
  910. vts[1] = ((ov7251->current_mode->height + vblank) & 0x00ff);
  911. return ov7251_write_seq_regs(ov7251, OV7251_TIMING_VTS_REG, vts, 2);
  912. }
  913. static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)
  914. {
  915. struct ov7251 *ov7251 = container_of(ctrl->handler,
  916. struct ov7251, ctrls);
  917. int ret;
  918. /* If VBLANK is altered we need to update exposure to compensate */
  919. if (ctrl->id == V4L2_CID_VBLANK) {
  920. int exposure_max;
  921. exposure_max = ov7251->current_mode->height + ctrl->val -
  922. OV7251_INTEGRATION_MARGIN;
  923. __v4l2_ctrl_modify_range(ov7251->exposure,
  924. ov7251->exposure->minimum,
  925. exposure_max,
  926. ov7251->exposure->step,
  927. min(ov7251->exposure->val,
  928. exposure_max));
  929. }
  930. /* v4l2_ctrl_lock() locks our mutex */
  931. if (!pm_runtime_get_if_in_use(ov7251->dev))
  932. return 0;
  933. switch (ctrl->id) {
  934. case V4L2_CID_EXPOSURE:
  935. ret = ov7251_set_exposure(ov7251, ctrl->val);
  936. break;
  937. case V4L2_CID_GAIN:
  938. ret = ov7251_set_gain(ov7251, ctrl->val);
  939. break;
  940. case V4L2_CID_TEST_PATTERN:
  941. ret = ov7251_set_test_pattern(ov7251, ctrl->val);
  942. break;
  943. case V4L2_CID_HFLIP:
  944. ret = ov7251_set_hflip(ov7251, ctrl->val);
  945. break;
  946. case V4L2_CID_VFLIP:
  947. ret = ov7251_set_vflip(ov7251, ctrl->val);
  948. break;
  949. case V4L2_CID_VBLANK:
  950. ret = ov7251_vts_configure(ov7251, ctrl->val);
  951. break;
  952. default:
  953. ret = -EINVAL;
  954. break;
  955. }
  956. pm_runtime_put(ov7251->dev);
  957. return ret;
  958. }
  959. static const struct v4l2_ctrl_ops ov7251_ctrl_ops = {
  960. .s_ctrl = ov7251_s_ctrl,
  961. };
  962. static int ov7251_enum_mbus_code(struct v4l2_subdev *sd,
  963. struct v4l2_subdev_state *sd_state,
  964. struct v4l2_subdev_mbus_code_enum *code)
  965. {
  966. if (code->index > 0)
  967. return -EINVAL;
  968. code->code = MEDIA_BUS_FMT_Y10_1X10;
  969. return 0;
  970. }
  971. static int ov7251_enum_frame_size(struct v4l2_subdev *subdev,
  972. struct v4l2_subdev_state *sd_state,
  973. struct v4l2_subdev_frame_size_enum *fse)
  974. {
  975. if (fse->code != MEDIA_BUS_FMT_Y10_1X10)
  976. return -EINVAL;
  977. if (fse->index >= ARRAY_SIZE(ov7251_mode_info_data))
  978. return -EINVAL;
  979. fse->min_width = ov7251_mode_info_data[fse->index].width;
  980. fse->max_width = ov7251_mode_info_data[fse->index].width;
  981. fse->min_height = ov7251_mode_info_data[fse->index].height;
  982. fse->max_height = ov7251_mode_info_data[fse->index].height;
  983. return 0;
  984. }
  985. static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev,
  986. struct v4l2_subdev_state *sd_state,
  987. struct v4l2_subdev_frame_interval_enum *fie)
  988. {
  989. unsigned int index = fie->index;
  990. unsigned int i;
  991. for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
  992. if (fie->width != ov7251_mode_info_data[i].width ||
  993. fie->height != ov7251_mode_info_data[i].height)
  994. continue;
  995. if (index-- == 0) {
  996. fie->interval = ov7251_mode_info_data[i].timeperframe;
  997. return 0;
  998. }
  999. }
  1000. return -EINVAL;
  1001. }
  1002. static struct v4l2_mbus_framefmt *
  1003. __ov7251_get_pad_format(struct ov7251 *ov7251,
  1004. struct v4l2_subdev_state *sd_state,
  1005. unsigned int pad,
  1006. enum v4l2_subdev_format_whence which)
  1007. {
  1008. switch (which) {
  1009. case V4L2_SUBDEV_FORMAT_TRY:
  1010. return v4l2_subdev_state_get_format(sd_state, pad);
  1011. case V4L2_SUBDEV_FORMAT_ACTIVE:
  1012. return &ov7251->fmt;
  1013. default:
  1014. return NULL;
  1015. }
  1016. }
  1017. static int ov7251_get_format(struct v4l2_subdev *sd,
  1018. struct v4l2_subdev_state *sd_state,
  1019. struct v4l2_subdev_format *format)
  1020. {
  1021. struct ov7251 *ov7251 = to_ov7251(sd);
  1022. mutex_lock(&ov7251->lock);
  1023. format->format = *__ov7251_get_pad_format(ov7251, sd_state,
  1024. format->pad,
  1025. format->which);
  1026. mutex_unlock(&ov7251->lock);
  1027. return 0;
  1028. }
  1029. static struct v4l2_rect *
  1030. __ov7251_get_pad_crop(struct ov7251 *ov7251,
  1031. struct v4l2_subdev_state *sd_state,
  1032. unsigned int pad, enum v4l2_subdev_format_whence which)
  1033. {
  1034. switch (which) {
  1035. case V4L2_SUBDEV_FORMAT_TRY:
  1036. return v4l2_subdev_state_get_crop(sd_state, pad);
  1037. case V4L2_SUBDEV_FORMAT_ACTIVE:
  1038. return &ov7251->crop;
  1039. default:
  1040. return NULL;
  1041. }
  1042. }
  1043. static inline u32 avg_fps(const struct v4l2_fract *t)
  1044. {
  1045. return (t->denominator + (t->numerator >> 1)) / t->numerator;
  1046. }
  1047. static const struct ov7251_mode_info *
  1048. ov7251_find_mode_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
  1049. {
  1050. const struct ov7251_mode_info *mode = ov7251->current_mode;
  1051. unsigned int fps_req = avg_fps(timeperframe);
  1052. unsigned int max_dist_match = (unsigned int) -1;
  1053. unsigned int i, n = 0;
  1054. for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
  1055. unsigned int dist;
  1056. unsigned int fps_tmp;
  1057. if (mode->width != ov7251_mode_info_data[i].width ||
  1058. mode->height != ov7251_mode_info_data[i].height)
  1059. continue;
  1060. fps_tmp = avg_fps(&ov7251_mode_info_data[i].timeperframe);
  1061. dist = abs(fps_req - fps_tmp);
  1062. if (dist < max_dist_match) {
  1063. n = i;
  1064. max_dist_match = dist;
  1065. }
  1066. }
  1067. return &ov7251_mode_info_data[n];
  1068. }
  1069. static int ov7251_set_format(struct v4l2_subdev *sd,
  1070. struct v4l2_subdev_state *sd_state,
  1071. struct v4l2_subdev_format *format)
  1072. {
  1073. struct ov7251 *ov7251 = to_ov7251(sd);
  1074. struct v4l2_mbus_framefmt *__format;
  1075. int vblank_max, vblank_def;
  1076. struct v4l2_rect *__crop;
  1077. const struct ov7251_mode_info *new_mode;
  1078. int ret = 0;
  1079. mutex_lock(&ov7251->lock);
  1080. __crop = __ov7251_get_pad_crop(ov7251, sd_state, format->pad,
  1081. format->which);
  1082. new_mode = v4l2_find_nearest_size(ov7251_mode_info_data,
  1083. ARRAY_SIZE(ov7251_mode_info_data),
  1084. width, height,
  1085. format->format.width, format->format.height);
  1086. __crop->width = new_mode->width;
  1087. __crop->height = new_mode->height;
  1088. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1089. ret = __v4l2_ctrl_modify_range(ov7251->exposure,
  1090. 1, new_mode->exposure_max,
  1091. 1, new_mode->exposure_def);
  1092. if (ret < 0)
  1093. goto exit;
  1094. ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
  1095. new_mode->exposure_def);
  1096. if (ret < 0)
  1097. goto exit;
  1098. ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
  1099. if (ret < 0)
  1100. goto exit;
  1101. vblank_max = OV7251_TIMING_MAX_VTS - new_mode->height;
  1102. vblank_def = new_mode->vts - new_mode->height;
  1103. ret = __v4l2_ctrl_modify_range(ov7251->vblank,
  1104. OV7251_TIMING_MIN_VTS,
  1105. vblank_max, 1, vblank_def);
  1106. if (ret < 0)
  1107. goto exit;
  1108. ov7251->current_mode = new_mode;
  1109. }
  1110. __format = __ov7251_get_pad_format(ov7251, sd_state, format->pad,
  1111. format->which);
  1112. __format->width = __crop->width;
  1113. __format->height = __crop->height;
  1114. __format->code = MEDIA_BUS_FMT_Y10_1X10;
  1115. __format->field = V4L2_FIELD_NONE;
  1116. __format->colorspace = V4L2_COLORSPACE_SRGB;
  1117. __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
  1118. __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
  1119. __format->colorspace, __format->ycbcr_enc);
  1120. __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
  1121. format->format = *__format;
  1122. exit:
  1123. mutex_unlock(&ov7251->lock);
  1124. return ret;
  1125. }
  1126. static int ov7251_init_state(struct v4l2_subdev *subdev,
  1127. struct v4l2_subdev_state *sd_state)
  1128. {
  1129. struct v4l2_subdev_format fmt = {
  1130. .which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
  1131. : V4L2_SUBDEV_FORMAT_ACTIVE,
  1132. .format = {
  1133. .width = 640,
  1134. .height = 480
  1135. }
  1136. };
  1137. ov7251_set_format(subdev, sd_state, &fmt);
  1138. return 0;
  1139. }
  1140. static int ov7251_get_selection(struct v4l2_subdev *sd,
  1141. struct v4l2_subdev_state *sd_state,
  1142. struct v4l2_subdev_selection *sel)
  1143. {
  1144. struct ov7251 *ov7251 = to_ov7251(sd);
  1145. switch (sel->target) {
  1146. case V4L2_SEL_TGT_CROP_DEFAULT:
  1147. case V4L2_SEL_TGT_CROP:
  1148. mutex_lock(&ov7251->lock);
  1149. sel->r = *__ov7251_get_pad_crop(ov7251, sd_state, sel->pad,
  1150. sel->which);
  1151. mutex_unlock(&ov7251->lock);
  1152. break;
  1153. case V4L2_SEL_TGT_NATIVE_SIZE:
  1154. sel->r.top = 0;
  1155. sel->r.left = 0;
  1156. sel->r.width = OV7251_NATIVE_WIDTH;
  1157. sel->r.height = OV7251_NATIVE_HEIGHT;
  1158. break;
  1159. case V4L2_SEL_TGT_CROP_BOUNDS:
  1160. sel->r.top = OV7251_ACTIVE_START_TOP;
  1161. sel->r.left = OV7251_ACTIVE_START_LEFT;
  1162. sel->r.width = OV7251_ACTIVE_WIDTH;
  1163. sel->r.height = OV7251_ACTIVE_HEIGHT;
  1164. break;
  1165. default:
  1166. return -EINVAL;
  1167. }
  1168. return 0;
  1169. }
  1170. static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable)
  1171. {
  1172. struct ov7251 *ov7251 = to_ov7251(subdev);
  1173. int ret;
  1174. mutex_lock(&ov7251->lock);
  1175. if (enable) {
  1176. ret = pm_runtime_resume_and_get(ov7251->dev);
  1177. if (ret) {
  1178. mutex_unlock(&ov7251->lock);
  1179. return ret;
  1180. }
  1181. ret = ov7251_pll_configure(ov7251);
  1182. if (ret) {
  1183. dev_err(ov7251->dev, "error configuring PLLs\n");
  1184. goto err_power_down;
  1185. }
  1186. ret = ov7251_set_register_array(ov7251,
  1187. ov7251->current_mode->data,
  1188. ov7251->current_mode->data_size);
  1189. if (ret < 0) {
  1190. dev_err(ov7251->dev, "could not set mode %dx%d\n",
  1191. ov7251->current_mode->width,
  1192. ov7251->current_mode->height);
  1193. goto err_power_down;
  1194. }
  1195. ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
  1196. if (ret < 0) {
  1197. dev_err(ov7251->dev, "could not sync v4l2 controls\n");
  1198. goto err_power_down;
  1199. }
  1200. ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
  1201. OV7251_SC_MODE_SELECT_STREAMING);
  1202. if (ret)
  1203. goto err_power_down;
  1204. } else {
  1205. ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
  1206. OV7251_SC_MODE_SELECT_SW_STANDBY);
  1207. pm_runtime_put(ov7251->dev);
  1208. }
  1209. mutex_unlock(&ov7251->lock);
  1210. return ret;
  1211. err_power_down:
  1212. pm_runtime_put(ov7251->dev);
  1213. mutex_unlock(&ov7251->lock);
  1214. return ret;
  1215. }
  1216. static int ov7251_get_frame_interval(struct v4l2_subdev *subdev,
  1217. struct v4l2_subdev_state *sd_state,
  1218. struct v4l2_subdev_frame_interval *fi)
  1219. {
  1220. struct ov7251 *ov7251 = to_ov7251(subdev);
  1221. /*
  1222. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  1223. * subdev active state API.
  1224. */
  1225. if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  1226. return -EINVAL;
  1227. mutex_lock(&ov7251->lock);
  1228. fi->interval = ov7251->current_mode->timeperframe;
  1229. mutex_unlock(&ov7251->lock);
  1230. return 0;
  1231. }
  1232. static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,
  1233. struct v4l2_subdev_state *sd_state,
  1234. struct v4l2_subdev_frame_interval *fi)
  1235. {
  1236. struct ov7251 *ov7251 = to_ov7251(subdev);
  1237. const struct ov7251_mode_info *new_mode;
  1238. int ret = 0;
  1239. /*
  1240. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  1241. * subdev active state API.
  1242. */
  1243. if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  1244. return -EINVAL;
  1245. mutex_lock(&ov7251->lock);
  1246. new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval);
  1247. if (new_mode != ov7251->current_mode) {
  1248. ret = __v4l2_ctrl_modify_range(ov7251->exposure,
  1249. 1, new_mode->exposure_max,
  1250. 1, new_mode->exposure_def);
  1251. if (ret < 0)
  1252. goto exit;
  1253. ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
  1254. new_mode->exposure_def);
  1255. if (ret < 0)
  1256. goto exit;
  1257. ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
  1258. if (ret < 0)
  1259. goto exit;
  1260. ov7251->current_mode = new_mode;
  1261. }
  1262. fi->interval = ov7251->current_mode->timeperframe;
  1263. exit:
  1264. mutex_unlock(&ov7251->lock);
  1265. return ret;
  1266. }
  1267. static const struct v4l2_subdev_video_ops ov7251_video_ops = {
  1268. .s_stream = ov7251_s_stream,
  1269. };
  1270. static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = {
  1271. .enum_mbus_code = ov7251_enum_mbus_code,
  1272. .enum_frame_size = ov7251_enum_frame_size,
  1273. .enum_frame_interval = ov7251_enum_frame_ival,
  1274. .get_fmt = ov7251_get_format,
  1275. .set_fmt = ov7251_set_format,
  1276. .get_selection = ov7251_get_selection,
  1277. .get_frame_interval = ov7251_get_frame_interval,
  1278. .set_frame_interval = ov7251_set_frame_interval,
  1279. };
  1280. static const struct v4l2_subdev_ops ov7251_subdev_ops = {
  1281. .video = &ov7251_video_ops,
  1282. .pad = &ov7251_subdev_pad_ops,
  1283. };
  1284. static const struct v4l2_subdev_internal_ops ov7251_internal_ops = {
  1285. .init_state = ov7251_init_state,
  1286. };
  1287. static int ov7251_check_hwcfg(struct ov7251 *ov7251)
  1288. {
  1289. struct fwnode_handle *fwnode = dev_fwnode(ov7251->dev);
  1290. struct v4l2_fwnode_endpoint bus_cfg = {
  1291. .bus_type = V4L2_MBUS_CSI2_DPHY,
  1292. };
  1293. struct fwnode_handle *endpoint;
  1294. unsigned int i, j;
  1295. int ret;
  1296. /*
  1297. * Sometimes the fwnode graph is initialized by the bridge driver
  1298. * Bridge drivers doing this may also add GPIO mappings, wait for this.
  1299. */
  1300. endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1301. if (!endpoint)
  1302. return dev_err_probe(ov7251->dev, -EPROBE_DEFER,
  1303. "waiting for fwnode graph endpoint\n");
  1304. ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
  1305. fwnode_handle_put(endpoint);
  1306. if (ret)
  1307. return dev_err_probe(ov7251->dev, ret,
  1308. "parsing endpoint node failed\n");
  1309. if (!bus_cfg.nr_of_link_frequencies) {
  1310. ret = dev_err_probe(ov7251->dev, -EINVAL,
  1311. "no link frequencies defined\n");
  1312. goto out_free_bus_cfg;
  1313. }
  1314. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
  1315. for (j = 0; j < ARRAY_SIZE(link_freq); j++)
  1316. if (bus_cfg.link_frequencies[i] == link_freq[j])
  1317. break;
  1318. if (j < ARRAY_SIZE(link_freq))
  1319. break;
  1320. }
  1321. if (i == bus_cfg.nr_of_link_frequencies) {
  1322. ret = dev_err_probe(ov7251->dev, -EINVAL,
  1323. "no supported link freq found\n");
  1324. goto out_free_bus_cfg;
  1325. }
  1326. ov7251->link_freq_idx = i;
  1327. out_free_bus_cfg:
  1328. v4l2_fwnode_endpoint_free(&bus_cfg);
  1329. return ret;
  1330. }
  1331. static int ov7251_detect_chip(struct ov7251 *ov7251)
  1332. {
  1333. u8 chip_id_high, chip_id_low, chip_rev;
  1334. int ret;
  1335. ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_HIGH, &chip_id_high);
  1336. if (ret < 0 || chip_id_high != OV7251_CHIP_ID_HIGH_BYTE)
  1337. return dev_err_probe(ov7251->dev, -ENODEV,
  1338. "could not read ID high\n");
  1339. ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_LOW, &chip_id_low);
  1340. if (ret < 0 || chip_id_low != OV7251_CHIP_ID_LOW_BYTE)
  1341. return dev_err_probe(ov7251->dev, -ENODEV,
  1342. "could not read ID low\n");
  1343. ret = ov7251_read_reg(ov7251, OV7251_SC_GP_IO_IN1, &chip_rev);
  1344. if (ret < 0)
  1345. return dev_err_probe(ov7251->dev, -ENODEV,
  1346. "could not read revision\n");
  1347. chip_rev >>= 4;
  1348. dev_info(ov7251->dev,
  1349. "OV7251 revision %x (%s) detected at address 0x%02x\n",
  1350. chip_rev,
  1351. chip_rev == 0x4 ? "1A / 1B" :
  1352. chip_rev == 0x5 ? "1C / 1D" :
  1353. chip_rev == 0x6 ? "1E" :
  1354. chip_rev == 0x7 ? "1F" : "unknown",
  1355. ov7251->i2c_client->addr);
  1356. return 0;
  1357. }
  1358. static int ov7251_init_ctrls(struct ov7251 *ov7251)
  1359. {
  1360. int vblank_max, vblank_def;
  1361. s64 pixel_rate;
  1362. int hblank;
  1363. v4l2_ctrl_handler_init(&ov7251->ctrls, 7);
  1364. ov7251->ctrls.lock = &ov7251->lock;
  1365. v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1366. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1367. v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1368. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1369. ov7251->exposure = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1370. V4L2_CID_EXPOSURE, 1, 32, 1, 32);
  1371. ov7251->gain = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1372. V4L2_CID_GAIN, 16, 1023, 1, 16);
  1373. v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops,
  1374. V4L2_CID_TEST_PATTERN,
  1375. ARRAY_SIZE(ov7251_test_pattern_menu) - 1,
  1376. 0, 0, ov7251_test_pattern_menu);
  1377. pixel_rate = pixel_rates[ov7251->link_freq_idx];
  1378. ov7251->pixel_clock = v4l2_ctrl_new_std(&ov7251->ctrls,
  1379. &ov7251_ctrl_ops,
  1380. V4L2_CID_PIXEL_RATE,
  1381. pixel_rate, INT_MAX,
  1382. pixel_rate, pixel_rate);
  1383. ov7251->link_freq = v4l2_ctrl_new_int_menu(&ov7251->ctrls,
  1384. &ov7251_ctrl_ops,
  1385. V4L2_CID_LINK_FREQ,
  1386. ARRAY_SIZE(link_freq) - 1,
  1387. ov7251->link_freq_idx,
  1388. link_freq);
  1389. if (ov7251->link_freq)
  1390. ov7251->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1391. if (ov7251->pixel_clock)
  1392. ov7251->pixel_clock->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1393. hblank = OV7251_FIXED_PPL - ov7251->current_mode->width;
  1394. ov7251->hblank = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1395. V4L2_CID_HBLANK, hblank, hblank, 1,
  1396. hblank);
  1397. if (ov7251->hblank)
  1398. ov7251->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1399. vblank_max = OV7251_TIMING_MAX_VTS - ov7251->current_mode->height;
  1400. vblank_def = ov7251->current_mode->vts - ov7251->current_mode->height;
  1401. ov7251->vblank = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1402. V4L2_CID_VBLANK,
  1403. OV7251_TIMING_MIN_VTS, vblank_max, 1,
  1404. vblank_def);
  1405. ov7251->sd.ctrl_handler = &ov7251->ctrls;
  1406. if (ov7251->ctrls.error) {
  1407. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1408. return ov7251->ctrls.error;
  1409. }
  1410. return 0;
  1411. }
  1412. static int ov7251_probe(struct i2c_client *client)
  1413. {
  1414. struct device *dev = &client->dev;
  1415. struct ov7251 *ov7251;
  1416. int ret;
  1417. int i;
  1418. ov7251 = devm_kzalloc(dev, sizeof(struct ov7251), GFP_KERNEL);
  1419. if (!ov7251)
  1420. return -ENOMEM;
  1421. ov7251->i2c_client = client;
  1422. ov7251->dev = dev;
  1423. ret = ov7251_check_hwcfg(ov7251);
  1424. if (ret)
  1425. return ret;
  1426. /* get system clock (xclk) */
  1427. ov7251->xclk = devm_v4l2_sensor_clk_get(dev, NULL);
  1428. if (IS_ERR(ov7251->xclk))
  1429. return dev_err_probe(dev, PTR_ERR(ov7251->xclk),
  1430. "could not get xclk");
  1431. ov7251->xclk_freq = clk_get_rate(ov7251->xclk);
  1432. for (i = 0; i < ARRAY_SIZE(supported_xclk_rates); i++)
  1433. if (ov7251->xclk_freq == supported_xclk_rates[i])
  1434. break;
  1435. if (i == ARRAY_SIZE(supported_xclk_rates))
  1436. return dev_err_probe(dev, -EINVAL,
  1437. "clock rate %u Hz is unsupported\n",
  1438. ov7251->xclk_freq);
  1439. ov7251->pll_cfgs = ov7251_pll_cfgs[i];
  1440. ov7251->io_regulator = devm_regulator_get(dev, "vdddo");
  1441. if (IS_ERR(ov7251->io_regulator)) {
  1442. dev_err(dev, "cannot get io regulator\n");
  1443. return PTR_ERR(ov7251->io_regulator);
  1444. }
  1445. ov7251->core_regulator = devm_regulator_get(dev, "vddd");
  1446. if (IS_ERR(ov7251->core_regulator)) {
  1447. dev_err(dev, "cannot get core regulator\n");
  1448. return PTR_ERR(ov7251->core_regulator);
  1449. }
  1450. ov7251->analog_regulator = devm_regulator_get(dev, "vdda");
  1451. if (IS_ERR(ov7251->analog_regulator)) {
  1452. dev_err(dev, "cannot get analog regulator\n");
  1453. return PTR_ERR(ov7251->analog_regulator);
  1454. }
  1455. ov7251->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
  1456. if (IS_ERR(ov7251->enable_gpio)) {
  1457. dev_err(dev, "cannot get enable gpio\n");
  1458. return PTR_ERR(ov7251->enable_gpio);
  1459. }
  1460. mutex_init(&ov7251->lock);
  1461. ov7251->current_mode = &ov7251_mode_info_data[0];
  1462. ret = ov7251_init_ctrls(ov7251);
  1463. if (ret) {
  1464. dev_err_probe(dev, ret, "error during v4l2 ctrl init\n");
  1465. goto destroy_mutex;
  1466. }
  1467. v4l2_i2c_subdev_init(&ov7251->sd, client, &ov7251_subdev_ops);
  1468. ov7251->sd.internal_ops = &ov7251_internal_ops;
  1469. ov7251->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1470. ov7251->pad.flags = MEDIA_PAD_FL_SOURCE;
  1471. ov7251->sd.dev = &client->dev;
  1472. ov7251->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1473. ret = media_entity_pads_init(&ov7251->sd.entity, 1, &ov7251->pad);
  1474. if (ret < 0) {
  1475. dev_err(dev, "could not register media entity\n");
  1476. goto free_ctrl;
  1477. }
  1478. ret = ov7251_set_power_on(ov7251->dev);
  1479. if (ret)
  1480. goto free_entity;
  1481. ret = ov7251_detect_chip(ov7251);
  1482. if (ret)
  1483. goto power_down;
  1484. pm_runtime_set_active(&client->dev);
  1485. pm_runtime_get_noresume(&client->dev);
  1486. pm_runtime_enable(&client->dev);
  1487. ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
  1488. &ov7251->pre_isp_00);
  1489. if (ret < 0) {
  1490. dev_err(dev, "could not read test pattern value\n");
  1491. ret = -ENODEV;
  1492. goto err_pm_runtime;
  1493. }
  1494. ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
  1495. &ov7251->timing_format1);
  1496. if (ret < 0) {
  1497. dev_err(dev, "could not read vflip value\n");
  1498. ret = -ENODEV;
  1499. goto err_pm_runtime;
  1500. }
  1501. ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
  1502. &ov7251->timing_format2);
  1503. if (ret < 0) {
  1504. dev_err(dev, "could not read hflip value\n");
  1505. ret = -ENODEV;
  1506. goto err_pm_runtime;
  1507. }
  1508. pm_runtime_set_autosuspend_delay(&client->dev, 1000);
  1509. pm_runtime_use_autosuspend(&client->dev);
  1510. pm_runtime_put_autosuspend(&client->dev);
  1511. ret = v4l2_async_register_subdev(&ov7251->sd);
  1512. if (ret < 0) {
  1513. dev_err(dev, "could not register v4l2 device\n");
  1514. goto free_entity;
  1515. }
  1516. ov7251_init_state(&ov7251->sd, NULL);
  1517. return 0;
  1518. err_pm_runtime:
  1519. pm_runtime_disable(ov7251->dev);
  1520. pm_runtime_put_noidle(ov7251->dev);
  1521. power_down:
  1522. ov7251_set_power_off(ov7251->dev);
  1523. free_entity:
  1524. media_entity_cleanup(&ov7251->sd.entity);
  1525. free_ctrl:
  1526. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1527. destroy_mutex:
  1528. mutex_destroy(&ov7251->lock);
  1529. return ret;
  1530. }
  1531. static void ov7251_remove(struct i2c_client *client)
  1532. {
  1533. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1534. struct ov7251 *ov7251 = to_ov7251(sd);
  1535. v4l2_async_unregister_subdev(&ov7251->sd);
  1536. media_entity_cleanup(&ov7251->sd.entity);
  1537. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1538. mutex_destroy(&ov7251->lock);
  1539. pm_runtime_disable(ov7251->dev);
  1540. if (!pm_runtime_status_suspended(ov7251->dev))
  1541. ov7251_set_power_off(ov7251->dev);
  1542. pm_runtime_set_suspended(ov7251->dev);
  1543. }
  1544. static const struct dev_pm_ops ov7251_pm_ops = {
  1545. SET_RUNTIME_PM_OPS(ov7251_set_power_off, ov7251_set_power_on, NULL)
  1546. };
  1547. static const struct of_device_id ov7251_of_match[] = {
  1548. { .compatible = "ovti,ov7251" },
  1549. { /* sentinel */ }
  1550. };
  1551. MODULE_DEVICE_TABLE(of, ov7251_of_match);
  1552. static const struct acpi_device_id ov7251_acpi_match[] = {
  1553. { "INT347E" },
  1554. { }
  1555. };
  1556. MODULE_DEVICE_TABLE(acpi, ov7251_acpi_match);
  1557. static struct i2c_driver ov7251_i2c_driver = {
  1558. .driver = {
  1559. .of_match_table = ov7251_of_match,
  1560. .acpi_match_table = ov7251_acpi_match,
  1561. .name = "ov7251",
  1562. .pm = &ov7251_pm_ops,
  1563. },
  1564. .probe = ov7251_probe,
  1565. .remove = ov7251_remove,
  1566. };
  1567. module_i2c_driver(ov7251_i2c_driver);
  1568. MODULE_DESCRIPTION("Omnivision OV7251 Camera Driver");
  1569. MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
  1570. MODULE_LICENSE("GPL v2");