ov5647.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * A V4L2 driver for OmniVision OV5647 cameras.
  4. *
  5. * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
  6. * Copyright (C) 2011 Sylwester Nawrocki <[email protected]>
  7. *
  8. * Based on Omnivision OV7670 Camera Driver
  9. * Copyright (C) 2006-7 Jonathan Corbet <[email protected]>
  10. *
  11. * Copyright (C) 2016, Synopsys, Inc.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/of_graph.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-ctrls.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-event.h>
  27. #include <media/v4l2-fwnode.h>
  28. #include <media/v4l2-image-sizes.h>
  29. #include <media/v4l2-mediabus.h>
  30. /*
  31. * From the datasheet, "20ms after PWDN goes low or 20ms after RESETB goes
  32. * high if reset is inserted after PWDN goes high, host can access sensor's
  33. * SCCB to initialize sensor."
  34. */
  35. #define PWDN_ACTIVE_DELAY_MS 20
  36. #define MIPI_CTRL00_CLOCK_LANE_GATE BIT(5)
  37. #define MIPI_CTRL00_LINE_SYNC_ENABLE BIT(4)
  38. #define MIPI_CTRL00_BUS_IDLE BIT(2)
  39. #define MIPI_CTRL00_CLOCK_LANE_DISABLE BIT(0)
  40. #define OV5647_SW_STANDBY 0x0100
  41. #define OV5647_SW_RESET 0x0103
  42. #define OV5647_REG_CHIPID_H 0x300a
  43. #define OV5647_REG_CHIPID_L 0x300b
  44. #define OV5640_REG_PAD_OUT 0x300d
  45. #define OV5647_REG_EXP_HI 0x3500
  46. #define OV5647_REG_EXP_MID 0x3501
  47. #define OV5647_REG_EXP_LO 0x3502
  48. #define OV5647_REG_AEC_AGC 0x3503
  49. #define OV5647_REG_GAIN_HI 0x350a
  50. #define OV5647_REG_GAIN_LO 0x350b
  51. #define OV5647_REG_VTS_HI 0x380e
  52. #define OV5647_REG_VTS_LO 0x380f
  53. #define OV5647_REG_FRAME_OFF_NUMBER 0x4202
  54. #define OV5647_REG_MIPI_CTRL00 0x4800
  55. #define OV5647_REG_MIPI_CTRL14 0x4814
  56. #define OV5647_REG_AWB 0x5001
  57. #define REG_TERM 0xfffe
  58. #define VAL_TERM 0xfe
  59. #define REG_DLY 0xffff
  60. /* OV5647 native and active pixel array size */
  61. #define OV5647_NATIVE_WIDTH 2624U
  62. #define OV5647_NATIVE_HEIGHT 1956U
  63. #define OV5647_PIXEL_ARRAY_LEFT 16U
  64. #define OV5647_PIXEL_ARRAY_TOP 16U
  65. #define OV5647_PIXEL_ARRAY_WIDTH 2592U
  66. #define OV5647_PIXEL_ARRAY_HEIGHT 1944U
  67. #define OV5647_VBLANK_MIN 4
  68. #define OV5647_VTS_MAX 32767
  69. #define OV5647_EXPOSURE_MIN 4
  70. #define OV5647_EXPOSURE_STEP 1
  71. #define OV5647_EXPOSURE_DEFAULT 1000
  72. #define OV5647_EXPOSURE_MAX 65535
  73. struct regval_list {
  74. u16 addr;
  75. u8 data;
  76. };
  77. struct ov5647_mode {
  78. struct v4l2_mbus_framefmt format;
  79. struct v4l2_rect crop;
  80. u64 pixel_rate;
  81. int hts;
  82. int vts;
  83. const struct regval_list *reg_list;
  84. unsigned int num_regs;
  85. };
  86. struct ov5647 {
  87. struct v4l2_subdev sd;
  88. struct media_pad pad;
  89. struct mutex lock;
  90. struct clk *xclk;
  91. struct gpio_desc *pwdn;
  92. bool clock_ncont;
  93. struct v4l2_ctrl_handler ctrls;
  94. const struct ov5647_mode *mode;
  95. struct v4l2_ctrl *pixel_rate;
  96. struct v4l2_ctrl *hblank;
  97. struct v4l2_ctrl *vblank;
  98. struct v4l2_ctrl *exposure;
  99. bool streaming;
  100. };
  101. static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd)
  102. {
  103. return container_of(sd, struct ov5647, sd);
  104. }
  105. static const struct regval_list sensor_oe_disable_regs[] = {
  106. {0x3000, 0x00},
  107. {0x3001, 0x00},
  108. {0x3002, 0x00},
  109. };
  110. static const struct regval_list sensor_oe_enable_regs[] = {
  111. {0x3000, 0x0f},
  112. {0x3001, 0xff},
  113. {0x3002, 0xe4},
  114. };
  115. static struct regval_list ov5647_2592x1944_10bpp[] = {
  116. {0x0100, 0x00},
  117. {0x0103, 0x01},
  118. {0x3034, 0x1a},
  119. {0x3035, 0x21},
  120. {0x3036, 0x69},
  121. {0x303c, 0x11},
  122. {0x3106, 0xf5},
  123. {0x3821, 0x06},
  124. {0x3820, 0x00},
  125. {0x3827, 0xec},
  126. {0x370c, 0x03},
  127. {0x3612, 0x5b},
  128. {0x3618, 0x04},
  129. {0x5000, 0x06},
  130. {0x5002, 0x41},
  131. {0x5003, 0x08},
  132. {0x5a00, 0x08},
  133. {0x3000, 0x00},
  134. {0x3001, 0x00},
  135. {0x3002, 0x00},
  136. {0x3016, 0x08},
  137. {0x3017, 0xe0},
  138. {0x3018, 0x44},
  139. {0x301c, 0xf8},
  140. {0x301d, 0xf0},
  141. {0x3a18, 0x00},
  142. {0x3a19, 0xf8},
  143. {0x3c01, 0x80},
  144. {0x3b07, 0x0c},
  145. {0x380c, 0x0b},
  146. {0x380d, 0x1c},
  147. {0x3814, 0x11},
  148. {0x3815, 0x11},
  149. {0x3708, 0x64},
  150. {0x3709, 0x12},
  151. {0x3808, 0x0a},
  152. {0x3809, 0x20},
  153. {0x380a, 0x07},
  154. {0x380b, 0x98},
  155. {0x3800, 0x00},
  156. {0x3801, 0x00},
  157. {0x3802, 0x00},
  158. {0x3803, 0x00},
  159. {0x3804, 0x0a},
  160. {0x3805, 0x3f},
  161. {0x3806, 0x07},
  162. {0x3807, 0xa3},
  163. {0x3811, 0x10},
  164. {0x3813, 0x06},
  165. {0x3630, 0x2e},
  166. {0x3632, 0xe2},
  167. {0x3633, 0x23},
  168. {0x3634, 0x44},
  169. {0x3636, 0x06},
  170. {0x3620, 0x64},
  171. {0x3621, 0xe0},
  172. {0x3600, 0x37},
  173. {0x3704, 0xa0},
  174. {0x3703, 0x5a},
  175. {0x3715, 0x78},
  176. {0x3717, 0x01},
  177. {0x3731, 0x02},
  178. {0x370b, 0x60},
  179. {0x3705, 0x1a},
  180. {0x3f05, 0x02},
  181. {0x3f06, 0x10},
  182. {0x3f01, 0x0a},
  183. {0x3a08, 0x01},
  184. {0x3a09, 0x28},
  185. {0x3a0a, 0x00},
  186. {0x3a0b, 0xf6},
  187. {0x3a0d, 0x08},
  188. {0x3a0e, 0x06},
  189. {0x3a0f, 0x58},
  190. {0x3a10, 0x50},
  191. {0x3a1b, 0x58},
  192. {0x3a1e, 0x50},
  193. {0x3a11, 0x60},
  194. {0x3a1f, 0x28},
  195. {0x4001, 0x02},
  196. {0x4004, 0x04},
  197. {0x4000, 0x09},
  198. {0x4837, 0x19},
  199. {0x4800, 0x24},
  200. {0x3503, 0x03},
  201. {0x0100, 0x01},
  202. };
  203. static struct regval_list ov5647_1080p30_10bpp[] = {
  204. {0x0100, 0x00},
  205. {0x0103, 0x01},
  206. {0x3034, 0x1a},
  207. {0x3035, 0x21},
  208. {0x3036, 0x62},
  209. {0x303c, 0x11},
  210. {0x3106, 0xf5},
  211. {0x3821, 0x06},
  212. {0x3820, 0x00},
  213. {0x3827, 0xec},
  214. {0x370c, 0x03},
  215. {0x3612, 0x5b},
  216. {0x3618, 0x04},
  217. {0x5000, 0x06},
  218. {0x5002, 0x41},
  219. {0x5003, 0x08},
  220. {0x5a00, 0x08},
  221. {0x3000, 0x00},
  222. {0x3001, 0x00},
  223. {0x3002, 0x00},
  224. {0x3016, 0x08},
  225. {0x3017, 0xe0},
  226. {0x3018, 0x44},
  227. {0x301c, 0xf8},
  228. {0x301d, 0xf0},
  229. {0x3a18, 0x00},
  230. {0x3a19, 0xf8},
  231. {0x3c01, 0x80},
  232. {0x3b07, 0x0c},
  233. {0x380c, 0x09},
  234. {0x380d, 0x70},
  235. {0x3814, 0x11},
  236. {0x3815, 0x11},
  237. {0x3708, 0x64},
  238. {0x3709, 0x12},
  239. {0x3808, 0x07},
  240. {0x3809, 0x80},
  241. {0x380a, 0x04},
  242. {0x380b, 0x38},
  243. {0x3800, 0x01},
  244. {0x3801, 0x5c},
  245. {0x3802, 0x01},
  246. {0x3803, 0xb2},
  247. {0x3804, 0x08},
  248. {0x3805, 0xe3},
  249. {0x3806, 0x05},
  250. {0x3807, 0xf1},
  251. {0x3811, 0x04},
  252. {0x3813, 0x02},
  253. {0x3630, 0x2e},
  254. {0x3632, 0xe2},
  255. {0x3633, 0x23},
  256. {0x3634, 0x44},
  257. {0x3636, 0x06},
  258. {0x3620, 0x64},
  259. {0x3621, 0xe0},
  260. {0x3600, 0x37},
  261. {0x3704, 0xa0},
  262. {0x3703, 0x5a},
  263. {0x3715, 0x78},
  264. {0x3717, 0x01},
  265. {0x3731, 0x02},
  266. {0x370b, 0x60},
  267. {0x3705, 0x1a},
  268. {0x3f05, 0x02},
  269. {0x3f06, 0x10},
  270. {0x3f01, 0x0a},
  271. {0x3a08, 0x01},
  272. {0x3a09, 0x4b},
  273. {0x3a0a, 0x01},
  274. {0x3a0b, 0x13},
  275. {0x3a0d, 0x04},
  276. {0x3a0e, 0x03},
  277. {0x3a0f, 0x58},
  278. {0x3a10, 0x50},
  279. {0x3a1b, 0x58},
  280. {0x3a1e, 0x50},
  281. {0x3a11, 0x60},
  282. {0x3a1f, 0x28},
  283. {0x4001, 0x02},
  284. {0x4004, 0x04},
  285. {0x4000, 0x09},
  286. {0x4837, 0x19},
  287. {0x4800, 0x34},
  288. {0x3503, 0x03},
  289. {0x0100, 0x01},
  290. };
  291. static struct regval_list ov5647_2x2binned_10bpp[] = {
  292. {0x0100, 0x00},
  293. {0x0103, 0x01},
  294. {0x3034, 0x1a},
  295. {0x3035, 0x21},
  296. {0x3036, 0x62},
  297. {0x303c, 0x11},
  298. {0x3106, 0xf5},
  299. {0x3827, 0xec},
  300. {0x370c, 0x03},
  301. {0x3612, 0x59},
  302. {0x3618, 0x00},
  303. {0x5000, 0x06},
  304. {0x5002, 0x41},
  305. {0x5003, 0x08},
  306. {0x5a00, 0x08},
  307. {0x3000, 0x00},
  308. {0x3001, 0x00},
  309. {0x3002, 0x00},
  310. {0x3016, 0x08},
  311. {0x3017, 0xe0},
  312. {0x3018, 0x44},
  313. {0x301c, 0xf8},
  314. {0x301d, 0xf0},
  315. {0x3a18, 0x00},
  316. {0x3a19, 0xf8},
  317. {0x3c01, 0x80},
  318. {0x3b07, 0x0c},
  319. {0x3800, 0x00},
  320. {0x3801, 0x00},
  321. {0x3802, 0x00},
  322. {0x3803, 0x00},
  323. {0x3804, 0x0a},
  324. {0x3805, 0x3f},
  325. {0x3806, 0x07},
  326. {0x3807, 0xa3},
  327. {0x3808, 0x05},
  328. {0x3809, 0x10},
  329. {0x380a, 0x03},
  330. {0x380b, 0xcc},
  331. {0x380c, 0x07},
  332. {0x380d, 0x68},
  333. {0x3811, 0x0c},
  334. {0x3813, 0x06},
  335. {0x3814, 0x31},
  336. {0x3815, 0x31},
  337. {0x3630, 0x2e},
  338. {0x3632, 0xe2},
  339. {0x3633, 0x23},
  340. {0x3634, 0x44},
  341. {0x3636, 0x06},
  342. {0x3620, 0x64},
  343. {0x3621, 0xe0},
  344. {0x3600, 0x37},
  345. {0x3704, 0xa0},
  346. {0x3703, 0x5a},
  347. {0x3715, 0x78},
  348. {0x3717, 0x01},
  349. {0x3731, 0x02},
  350. {0x370b, 0x60},
  351. {0x3705, 0x1a},
  352. {0x3f05, 0x02},
  353. {0x3f06, 0x10},
  354. {0x3f01, 0x0a},
  355. {0x3a08, 0x01},
  356. {0x3a09, 0x28},
  357. {0x3a0a, 0x00},
  358. {0x3a0b, 0xf6},
  359. {0x3a0d, 0x08},
  360. {0x3a0e, 0x06},
  361. {0x3a0f, 0x58},
  362. {0x3a10, 0x50},
  363. {0x3a1b, 0x58},
  364. {0x3a1e, 0x50},
  365. {0x3a11, 0x60},
  366. {0x3a1f, 0x28},
  367. {0x4001, 0x02},
  368. {0x4004, 0x04},
  369. {0x4000, 0x09},
  370. {0x4837, 0x16},
  371. {0x4800, 0x24},
  372. {0x3503, 0x03},
  373. {0x3820, 0x41},
  374. {0x3821, 0x07},
  375. {0x350a, 0x00},
  376. {0x350b, 0x10},
  377. {0x3500, 0x00},
  378. {0x3501, 0x1a},
  379. {0x3502, 0xf0},
  380. {0x3212, 0xa0},
  381. {0x0100, 0x01},
  382. };
  383. static struct regval_list ov5647_640x480_10bpp[] = {
  384. {0x0100, 0x00},
  385. {0x0103, 0x01},
  386. {0x3035, 0x11},
  387. {0x3036, 0x46},
  388. {0x303c, 0x11},
  389. {0x3821, 0x07},
  390. {0x3820, 0x41},
  391. {0x370c, 0x03},
  392. {0x3612, 0x59},
  393. {0x3618, 0x00},
  394. {0x5000, 0x06},
  395. {0x5003, 0x08},
  396. {0x5a00, 0x08},
  397. {0x3000, 0xff},
  398. {0x3001, 0xff},
  399. {0x3002, 0xff},
  400. {0x301d, 0xf0},
  401. {0x3a18, 0x00},
  402. {0x3a19, 0xf8},
  403. {0x3c01, 0x80},
  404. {0x3b07, 0x0c},
  405. {0x380c, 0x07},
  406. {0x380d, 0x3c},
  407. {0x3814, 0x35},
  408. {0x3815, 0x35},
  409. {0x3708, 0x64},
  410. {0x3709, 0x52},
  411. {0x3808, 0x02},
  412. {0x3809, 0x80},
  413. {0x380a, 0x01},
  414. {0x380b, 0xe0},
  415. {0x3800, 0x00},
  416. {0x3801, 0x10},
  417. {0x3802, 0x00},
  418. {0x3803, 0x00},
  419. {0x3804, 0x0a},
  420. {0x3805, 0x2f},
  421. {0x3806, 0x07},
  422. {0x3807, 0x9f},
  423. {0x3630, 0x2e},
  424. {0x3632, 0xe2},
  425. {0x3633, 0x23},
  426. {0x3634, 0x44},
  427. {0x3620, 0x64},
  428. {0x3621, 0xe0},
  429. {0x3600, 0x37},
  430. {0x3704, 0xa0},
  431. {0x3703, 0x5a},
  432. {0x3715, 0x78},
  433. {0x3717, 0x01},
  434. {0x3731, 0x02},
  435. {0x370b, 0x60},
  436. {0x3705, 0x1a},
  437. {0x3f05, 0x02},
  438. {0x3f06, 0x10},
  439. {0x3f01, 0x0a},
  440. {0x3a08, 0x01},
  441. {0x3a09, 0x2e},
  442. {0x3a0a, 0x00},
  443. {0x3a0b, 0xfb},
  444. {0x3a0d, 0x02},
  445. {0x3a0e, 0x01},
  446. {0x3a0f, 0x58},
  447. {0x3a10, 0x50},
  448. {0x3a1b, 0x58},
  449. {0x3a1e, 0x50},
  450. {0x3a11, 0x60},
  451. {0x3a1f, 0x28},
  452. {0x4001, 0x02},
  453. {0x4004, 0x02},
  454. {0x4000, 0x09},
  455. {0x3000, 0x00},
  456. {0x3001, 0x00},
  457. {0x3002, 0x00},
  458. {0x3017, 0xe0},
  459. {0x301c, 0xfc},
  460. {0x3636, 0x06},
  461. {0x3016, 0x08},
  462. {0x3827, 0xec},
  463. {0x3018, 0x44},
  464. {0x3035, 0x21},
  465. {0x3106, 0xf5},
  466. {0x3034, 0x1a},
  467. {0x301c, 0xf8},
  468. {0x4800, 0x34},
  469. {0x3503, 0x03},
  470. {0x0100, 0x01},
  471. };
  472. static const struct ov5647_mode ov5647_modes[] = {
  473. /* 2592x1944 full resolution full FOV 10-bit mode. */
  474. {
  475. .format = {
  476. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  477. .colorspace = V4L2_COLORSPACE_SRGB,
  478. .field = V4L2_FIELD_NONE,
  479. .width = 2592,
  480. .height = 1944
  481. },
  482. .crop = {
  483. .left = OV5647_PIXEL_ARRAY_LEFT,
  484. .top = OV5647_PIXEL_ARRAY_TOP,
  485. .width = 2592,
  486. .height = 1944
  487. },
  488. .pixel_rate = 87500000,
  489. .hts = 2844,
  490. .vts = 0x7b0,
  491. .reg_list = ov5647_2592x1944_10bpp,
  492. .num_regs = ARRAY_SIZE(ov5647_2592x1944_10bpp)
  493. },
  494. /* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */
  495. {
  496. .format = {
  497. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  498. .colorspace = V4L2_COLORSPACE_SRGB,
  499. .field = V4L2_FIELD_NONE,
  500. .width = 1920,
  501. .height = 1080
  502. },
  503. .crop = {
  504. .left = 348 + OV5647_PIXEL_ARRAY_LEFT,
  505. .top = 434 + OV5647_PIXEL_ARRAY_TOP,
  506. .width = 1928,
  507. .height = 1080,
  508. },
  509. .pixel_rate = 81666700,
  510. .hts = 2416,
  511. .vts = 0x450,
  512. .reg_list = ov5647_1080p30_10bpp,
  513. .num_regs = ARRAY_SIZE(ov5647_1080p30_10bpp)
  514. },
  515. /* 2x2 binned full FOV 10-bit mode. */
  516. {
  517. .format = {
  518. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  519. .colorspace = V4L2_COLORSPACE_SRGB,
  520. .field = V4L2_FIELD_NONE,
  521. .width = 1296,
  522. .height = 972
  523. },
  524. .crop = {
  525. .left = OV5647_PIXEL_ARRAY_LEFT,
  526. .top = OV5647_PIXEL_ARRAY_TOP,
  527. .width = 2592,
  528. .height = 1944,
  529. },
  530. .pixel_rate = 81666700,
  531. .hts = 1896,
  532. .vts = 0x59b,
  533. .reg_list = ov5647_2x2binned_10bpp,
  534. .num_regs = ARRAY_SIZE(ov5647_2x2binned_10bpp)
  535. },
  536. /* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */
  537. {
  538. .format = {
  539. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  540. .colorspace = V4L2_COLORSPACE_SRGB,
  541. .field = V4L2_FIELD_NONE,
  542. .width = 640,
  543. .height = 480
  544. },
  545. .crop = {
  546. .left = 16 + OV5647_PIXEL_ARRAY_LEFT,
  547. .top = OV5647_PIXEL_ARRAY_TOP,
  548. .width = 2560,
  549. .height = 1920,
  550. },
  551. .pixel_rate = 55000000,
  552. .hts = 1852,
  553. .vts = 0x1f8,
  554. .reg_list = ov5647_640x480_10bpp,
  555. .num_regs = ARRAY_SIZE(ov5647_640x480_10bpp)
  556. },
  557. };
  558. /* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */
  559. #define OV5647_DEFAULT_MODE (&ov5647_modes[3])
  560. #define OV5647_DEFAULT_FORMAT (ov5647_modes[3].format)
  561. static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
  562. {
  563. unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
  564. struct i2c_client *client = v4l2_get_subdevdata(sd);
  565. int ret;
  566. ret = i2c_master_send(client, data, 4);
  567. if (ret < 0) {
  568. dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
  569. __func__, reg);
  570. return ret;
  571. }
  572. return 0;
  573. }
  574. static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
  575. {
  576. unsigned char data[3] = { reg >> 8, reg & 0xff, val};
  577. struct i2c_client *client = v4l2_get_subdevdata(sd);
  578. int ret;
  579. ret = i2c_master_send(client, data, 3);
  580. if (ret < 0) {
  581. dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
  582. __func__, reg);
  583. return ret;
  584. }
  585. return 0;
  586. }
  587. static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
  588. {
  589. unsigned char data_w[2] = { reg >> 8, reg & 0xff };
  590. struct i2c_client *client = v4l2_get_subdevdata(sd);
  591. int ret;
  592. ret = i2c_master_send(client, data_w, 2);
  593. if (ret < 0) {
  594. dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
  595. __func__, reg);
  596. return ret;
  597. }
  598. ret = i2c_master_recv(client, val, 1);
  599. if (ret < 0) {
  600. dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
  601. __func__, reg);
  602. return ret;
  603. }
  604. return 0;
  605. }
  606. static int ov5647_write_array(struct v4l2_subdev *sd,
  607. const struct regval_list *regs, int array_size)
  608. {
  609. int i, ret;
  610. for (i = 0; i < array_size; i++) {
  611. ret = ov5647_write(sd, regs[i].addr, regs[i].data);
  612. if (ret < 0)
  613. return ret;
  614. }
  615. return 0;
  616. }
  617. static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
  618. {
  619. u8 channel_id;
  620. int ret;
  621. ret = ov5647_read(sd, OV5647_REG_MIPI_CTRL14, &channel_id);
  622. if (ret < 0)
  623. return ret;
  624. channel_id &= ~(3 << 6);
  625. return ov5647_write(sd, OV5647_REG_MIPI_CTRL14,
  626. channel_id | (channel << 6));
  627. }
  628. static int ov5647_set_mode(struct v4l2_subdev *sd)
  629. {
  630. struct i2c_client *client = v4l2_get_subdevdata(sd);
  631. struct ov5647 *sensor = to_sensor(sd);
  632. u8 resetval, rdval;
  633. int ret;
  634. ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
  635. if (ret < 0)
  636. return ret;
  637. ret = ov5647_write_array(sd, sensor->mode->reg_list,
  638. sensor->mode->num_regs);
  639. if (ret < 0) {
  640. dev_err(&client->dev, "write sensor default regs error\n");
  641. return ret;
  642. }
  643. ret = ov5647_set_virtual_channel(sd, 0);
  644. if (ret < 0)
  645. return ret;
  646. ret = ov5647_read(sd, OV5647_SW_STANDBY, &resetval);
  647. if (ret < 0)
  648. return ret;
  649. if (!(resetval & 0x01)) {
  650. dev_err(&client->dev, "Device was in SW standby");
  651. ret = ov5647_write(sd, OV5647_SW_STANDBY, 0x01);
  652. if (ret < 0)
  653. return ret;
  654. }
  655. return 0;
  656. }
  657. static int ov5647_stream_on(struct v4l2_subdev *sd)
  658. {
  659. struct i2c_client *client = v4l2_get_subdevdata(sd);
  660. struct ov5647 *sensor = to_sensor(sd);
  661. u8 val = MIPI_CTRL00_BUS_IDLE;
  662. int ret;
  663. ret = ov5647_set_mode(sd);
  664. if (ret) {
  665. dev_err(&client->dev, "Failed to program sensor mode: %d\n", ret);
  666. return ret;
  667. }
  668. /* Apply customized values from user when stream starts. */
  669. ret = __v4l2_ctrl_handler_setup(sd->ctrl_handler);
  670. if (ret)
  671. return ret;
  672. if (sensor->clock_ncont)
  673. val |= MIPI_CTRL00_CLOCK_LANE_GATE |
  674. MIPI_CTRL00_LINE_SYNC_ENABLE;
  675. ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, val);
  676. if (ret < 0)
  677. return ret;
  678. ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x00);
  679. if (ret < 0)
  680. return ret;
  681. return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x00);
  682. }
  683. static int ov5647_stream_off(struct v4l2_subdev *sd)
  684. {
  685. int ret;
  686. ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00,
  687. MIPI_CTRL00_CLOCK_LANE_GATE | MIPI_CTRL00_BUS_IDLE |
  688. MIPI_CTRL00_CLOCK_LANE_DISABLE);
  689. if (ret < 0)
  690. return ret;
  691. ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x0f);
  692. if (ret < 0)
  693. return ret;
  694. return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x01);
  695. }
  696. static int ov5647_power_on(struct device *dev)
  697. {
  698. struct ov5647 *sensor = dev_get_drvdata(dev);
  699. int ret;
  700. dev_dbg(dev, "OV5647 power on\n");
  701. if (sensor->pwdn) {
  702. gpiod_set_value_cansleep(sensor->pwdn, 0);
  703. msleep(PWDN_ACTIVE_DELAY_MS);
  704. }
  705. ret = clk_prepare_enable(sensor->xclk);
  706. if (ret < 0) {
  707. dev_err(dev, "clk prepare enable failed\n");
  708. goto error_pwdn;
  709. }
  710. ret = ov5647_write_array(&sensor->sd, sensor_oe_enable_regs,
  711. ARRAY_SIZE(sensor_oe_enable_regs));
  712. if (ret < 0) {
  713. dev_err(dev, "write sensor_oe_enable_regs error\n");
  714. goto error_clk_disable;
  715. }
  716. /* Stream off to coax lanes into LP-11 state. */
  717. ret = ov5647_stream_off(&sensor->sd);
  718. if (ret < 0) {
  719. dev_err(dev, "camera not available, check power\n");
  720. goto error_clk_disable;
  721. }
  722. return 0;
  723. error_clk_disable:
  724. clk_disable_unprepare(sensor->xclk);
  725. error_pwdn:
  726. gpiod_set_value_cansleep(sensor->pwdn, 1);
  727. return ret;
  728. }
  729. static int ov5647_power_off(struct device *dev)
  730. {
  731. struct ov5647 *sensor = dev_get_drvdata(dev);
  732. u8 rdval;
  733. int ret;
  734. dev_dbg(dev, "OV5647 power off\n");
  735. ret = ov5647_write_array(&sensor->sd, sensor_oe_disable_regs,
  736. ARRAY_SIZE(sensor_oe_disable_regs));
  737. if (ret < 0)
  738. dev_dbg(dev, "disable oe failed\n");
  739. /* Enter software standby */
  740. ret = ov5647_read(&sensor->sd, OV5647_SW_STANDBY, &rdval);
  741. if (ret < 0)
  742. dev_dbg(dev, "software standby failed\n");
  743. rdval &= ~0x01;
  744. ret = ov5647_write(&sensor->sd, OV5647_SW_STANDBY, rdval);
  745. if (ret < 0)
  746. dev_dbg(dev, "software standby failed\n");
  747. clk_disable_unprepare(sensor->xclk);
  748. gpiod_set_value_cansleep(sensor->pwdn, 1);
  749. return 0;
  750. }
  751. #ifdef CONFIG_VIDEO_ADV_DEBUG
  752. static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
  753. struct v4l2_dbg_register *reg)
  754. {
  755. int ret;
  756. u8 val;
  757. ret = ov5647_read(sd, reg->reg & 0xff, &val);
  758. if (ret < 0)
  759. return ret;
  760. reg->val = val;
  761. reg->size = 1;
  762. return 0;
  763. }
  764. static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
  765. const struct v4l2_dbg_register *reg)
  766. {
  767. return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
  768. }
  769. #endif
  770. /* Subdev core operations registration */
  771. static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
  772. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  773. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  774. #ifdef CONFIG_VIDEO_ADV_DEBUG
  775. .g_register = ov5647_sensor_get_register,
  776. .s_register = ov5647_sensor_set_register,
  777. #endif
  778. };
  779. static const struct v4l2_rect *
  780. __ov5647_get_pad_crop(struct ov5647 *ov5647,
  781. struct v4l2_subdev_state *sd_state,
  782. unsigned int pad, enum v4l2_subdev_format_whence which)
  783. {
  784. switch (which) {
  785. case V4L2_SUBDEV_FORMAT_TRY:
  786. return v4l2_subdev_get_try_crop(&ov5647->sd, sd_state, pad);
  787. case V4L2_SUBDEV_FORMAT_ACTIVE:
  788. return &ov5647->mode->crop;
  789. }
  790. return NULL;
  791. }
  792. static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
  793. {
  794. struct i2c_client *client = v4l2_get_subdevdata(sd);
  795. struct ov5647 *sensor = to_sensor(sd);
  796. int ret;
  797. mutex_lock(&sensor->lock);
  798. if (sensor->streaming == enable) {
  799. mutex_unlock(&sensor->lock);
  800. return 0;
  801. }
  802. if (enable) {
  803. ret = pm_runtime_resume_and_get(&client->dev);
  804. if (ret < 0)
  805. goto error_unlock;
  806. ret = ov5647_stream_on(sd);
  807. if (ret < 0) {
  808. dev_err(&client->dev, "stream start failed: %d\n", ret);
  809. goto error_pm;
  810. }
  811. } else {
  812. ret = ov5647_stream_off(sd);
  813. if (ret < 0) {
  814. dev_err(&client->dev, "stream stop failed: %d\n", ret);
  815. goto error_pm;
  816. }
  817. pm_runtime_put(&client->dev);
  818. }
  819. sensor->streaming = enable;
  820. mutex_unlock(&sensor->lock);
  821. return 0;
  822. error_pm:
  823. pm_runtime_put(&client->dev);
  824. error_unlock:
  825. mutex_unlock(&sensor->lock);
  826. return ret;
  827. }
  828. static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
  829. .s_stream = ov5647_s_stream,
  830. };
  831. static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
  832. struct v4l2_subdev_state *sd_state,
  833. struct v4l2_subdev_mbus_code_enum *code)
  834. {
  835. if (code->index > 0)
  836. return -EINVAL;
  837. code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  838. return 0;
  839. }
  840. static int ov5647_enum_frame_size(struct v4l2_subdev *sd,
  841. struct v4l2_subdev_state *sd_state,
  842. struct v4l2_subdev_frame_size_enum *fse)
  843. {
  844. const struct v4l2_mbus_framefmt *fmt;
  845. if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
  846. fse->index >= ARRAY_SIZE(ov5647_modes))
  847. return -EINVAL;
  848. fmt = &ov5647_modes[fse->index].format;
  849. fse->min_width = fmt->width;
  850. fse->max_width = fmt->width;
  851. fse->min_height = fmt->height;
  852. fse->max_height = fmt->height;
  853. return 0;
  854. }
  855. static int ov5647_get_pad_fmt(struct v4l2_subdev *sd,
  856. struct v4l2_subdev_state *sd_state,
  857. struct v4l2_subdev_format *format)
  858. {
  859. struct v4l2_mbus_framefmt *fmt = &format->format;
  860. const struct v4l2_mbus_framefmt *sensor_format;
  861. struct ov5647 *sensor = to_sensor(sd);
  862. mutex_lock(&sensor->lock);
  863. switch (format->which) {
  864. case V4L2_SUBDEV_FORMAT_TRY:
  865. sensor_format = v4l2_subdev_get_try_format(sd, sd_state,
  866. format->pad);
  867. break;
  868. default:
  869. sensor_format = &sensor->mode->format;
  870. break;
  871. }
  872. *fmt = *sensor_format;
  873. mutex_unlock(&sensor->lock);
  874. return 0;
  875. }
  876. static int ov5647_set_pad_fmt(struct v4l2_subdev *sd,
  877. struct v4l2_subdev_state *sd_state,
  878. struct v4l2_subdev_format *format)
  879. {
  880. struct v4l2_mbus_framefmt *fmt = &format->format;
  881. struct ov5647 *sensor = to_sensor(sd);
  882. const struct ov5647_mode *mode;
  883. mode = v4l2_find_nearest_size(ov5647_modes, ARRAY_SIZE(ov5647_modes),
  884. format.width, format.height,
  885. fmt->width, fmt->height);
  886. /* Update the sensor mode and apply at it at streamon time. */
  887. mutex_lock(&sensor->lock);
  888. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  889. *v4l2_subdev_get_try_format(sd, sd_state, format->pad) = mode->format;
  890. } else {
  891. int exposure_max, exposure_def;
  892. int hblank, vblank;
  893. sensor->mode = mode;
  894. __v4l2_ctrl_modify_range(sensor->pixel_rate, mode->pixel_rate,
  895. mode->pixel_rate, 1, mode->pixel_rate);
  896. hblank = mode->hts - mode->format.width;
  897. __v4l2_ctrl_modify_range(sensor->hblank, hblank, hblank, 1,
  898. hblank);
  899. vblank = mode->vts - mode->format.height;
  900. __v4l2_ctrl_modify_range(sensor->vblank, OV5647_VBLANK_MIN,
  901. OV5647_VTS_MAX - mode->format.height,
  902. 1, vblank);
  903. __v4l2_ctrl_s_ctrl(sensor->vblank, vblank);
  904. exposure_max = mode->vts - 4;
  905. exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
  906. __v4l2_ctrl_modify_range(sensor->exposure,
  907. sensor->exposure->minimum,
  908. exposure_max, sensor->exposure->step,
  909. exposure_def);
  910. }
  911. *fmt = mode->format;
  912. mutex_unlock(&sensor->lock);
  913. return 0;
  914. }
  915. static int ov5647_get_selection(struct v4l2_subdev *sd,
  916. struct v4l2_subdev_state *sd_state,
  917. struct v4l2_subdev_selection *sel)
  918. {
  919. switch (sel->target) {
  920. case V4L2_SEL_TGT_CROP: {
  921. struct ov5647 *sensor = to_sensor(sd);
  922. mutex_lock(&sensor->lock);
  923. sel->r = *__ov5647_get_pad_crop(sensor, sd_state, sel->pad,
  924. sel->which);
  925. mutex_unlock(&sensor->lock);
  926. return 0;
  927. }
  928. case V4L2_SEL_TGT_NATIVE_SIZE:
  929. sel->r.top = 0;
  930. sel->r.left = 0;
  931. sel->r.width = OV5647_NATIVE_WIDTH;
  932. sel->r.height = OV5647_NATIVE_HEIGHT;
  933. return 0;
  934. case V4L2_SEL_TGT_CROP_DEFAULT:
  935. case V4L2_SEL_TGT_CROP_BOUNDS:
  936. sel->r.top = OV5647_PIXEL_ARRAY_TOP;
  937. sel->r.left = OV5647_PIXEL_ARRAY_LEFT;
  938. sel->r.width = OV5647_PIXEL_ARRAY_WIDTH;
  939. sel->r.height = OV5647_PIXEL_ARRAY_HEIGHT;
  940. return 0;
  941. }
  942. return -EINVAL;
  943. }
  944. static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
  945. .enum_mbus_code = ov5647_enum_mbus_code,
  946. .enum_frame_size = ov5647_enum_frame_size,
  947. .set_fmt = ov5647_set_pad_fmt,
  948. .get_fmt = ov5647_get_pad_fmt,
  949. .get_selection = ov5647_get_selection,
  950. };
  951. static const struct v4l2_subdev_ops ov5647_subdev_ops = {
  952. .core = &ov5647_subdev_core_ops,
  953. .video = &ov5647_subdev_video_ops,
  954. .pad = &ov5647_subdev_pad_ops,
  955. };
  956. static int ov5647_detect(struct v4l2_subdev *sd)
  957. {
  958. struct i2c_client *client = v4l2_get_subdevdata(sd);
  959. u8 read;
  960. int ret;
  961. ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
  962. if (ret < 0)
  963. return ret;
  964. ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
  965. if (ret < 0)
  966. return ret;
  967. if (read != 0x56) {
  968. dev_err(&client->dev, "ID High expected 0x56 got %x", read);
  969. return -ENODEV;
  970. }
  971. ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
  972. if (ret < 0)
  973. return ret;
  974. if (read != 0x47) {
  975. dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
  976. return -ENODEV;
  977. }
  978. return ov5647_write(sd, OV5647_SW_RESET, 0x00);
  979. }
  980. static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  981. {
  982. struct v4l2_mbus_framefmt *format =
  983. v4l2_subdev_get_try_format(sd, fh->state, 0);
  984. struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
  985. crop->left = OV5647_PIXEL_ARRAY_LEFT;
  986. crop->top = OV5647_PIXEL_ARRAY_TOP;
  987. crop->width = OV5647_PIXEL_ARRAY_WIDTH;
  988. crop->height = OV5647_PIXEL_ARRAY_HEIGHT;
  989. *format = OV5647_DEFAULT_FORMAT;
  990. return 0;
  991. }
  992. static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
  993. .open = ov5647_open,
  994. };
  995. static int ov5647_s_auto_white_balance(struct v4l2_subdev *sd, u32 val)
  996. {
  997. return ov5647_write(sd, OV5647_REG_AWB, val ? 1 : 0);
  998. }
  999. static int ov5647_s_autogain(struct v4l2_subdev *sd, u32 val)
  1000. {
  1001. int ret;
  1002. u8 reg;
  1003. /* Non-zero turns on AGC by clearing bit 1.*/
  1004. ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
  1005. if (ret)
  1006. return ret;
  1007. return ov5647_write(sd, OV5647_REG_AEC_AGC, val ? reg & ~BIT(1)
  1008. : reg | BIT(1));
  1009. }
  1010. static int ov5647_s_exposure_auto(struct v4l2_subdev *sd, u32 val)
  1011. {
  1012. int ret;
  1013. u8 reg;
  1014. /*
  1015. * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by
  1016. * clearing bit 0.
  1017. */
  1018. ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
  1019. if (ret)
  1020. return ret;
  1021. return ov5647_write(sd, OV5647_REG_AEC_AGC,
  1022. val == V4L2_EXPOSURE_MANUAL ? reg | BIT(0)
  1023. : reg & ~BIT(0));
  1024. }
  1025. static int ov5647_s_analogue_gain(struct v4l2_subdev *sd, u32 val)
  1026. {
  1027. int ret;
  1028. /* 10 bits of gain, 2 in the high register. */
  1029. ret = ov5647_write(sd, OV5647_REG_GAIN_HI, (val >> 8) & 3);
  1030. if (ret)
  1031. return ret;
  1032. return ov5647_write(sd, OV5647_REG_GAIN_LO, val & 0xff);
  1033. }
  1034. static int ov5647_s_exposure(struct v4l2_subdev *sd, u32 val)
  1035. {
  1036. int ret;
  1037. /*
  1038. * Sensor has 20 bits, but the bottom 4 bits are fractions of a line
  1039. * which we leave as zero (and don't receive in "val").
  1040. */
  1041. ret = ov5647_write(sd, OV5647_REG_EXP_HI, (val >> 12) & 0xf);
  1042. if (ret)
  1043. return ret;
  1044. ret = ov5647_write(sd, OV5647_REG_EXP_MID, (val >> 4) & 0xff);
  1045. if (ret)
  1046. return ret;
  1047. return ov5647_write(sd, OV5647_REG_EXP_LO, (val & 0xf) << 4);
  1048. }
  1049. static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl)
  1050. {
  1051. struct ov5647 *sensor = container_of(ctrl->handler,
  1052. struct ov5647, ctrls);
  1053. struct v4l2_subdev *sd = &sensor->sd;
  1054. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1055. int ret = 0;
  1056. /* v4l2_ctrl_lock() locks our own mutex */
  1057. if (ctrl->id == V4L2_CID_VBLANK) {
  1058. int exposure_max, exposure_def;
  1059. /* Update max exposure while meeting expected vblanking */
  1060. exposure_max = sensor->mode->format.height + ctrl->val - 4;
  1061. exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
  1062. __v4l2_ctrl_modify_range(sensor->exposure,
  1063. sensor->exposure->minimum,
  1064. exposure_max, sensor->exposure->step,
  1065. exposure_def);
  1066. }
  1067. /*
  1068. * If the device is not powered up do not apply any controls
  1069. * to H/W at this time. Instead the controls will be restored
  1070. * at s_stream(1) time.
  1071. */
  1072. if (pm_runtime_get_if_in_use(&client->dev) == 0)
  1073. return 0;
  1074. switch (ctrl->id) {
  1075. case V4L2_CID_AUTO_WHITE_BALANCE:
  1076. ret = ov5647_s_auto_white_balance(sd, ctrl->val);
  1077. break;
  1078. case V4L2_CID_AUTOGAIN:
  1079. ret = ov5647_s_autogain(sd, ctrl->val);
  1080. break;
  1081. case V4L2_CID_EXPOSURE_AUTO:
  1082. ret = ov5647_s_exposure_auto(sd, ctrl->val);
  1083. break;
  1084. case V4L2_CID_ANALOGUE_GAIN:
  1085. ret = ov5647_s_analogue_gain(sd, ctrl->val);
  1086. break;
  1087. case V4L2_CID_EXPOSURE:
  1088. ret = ov5647_s_exposure(sd, ctrl->val);
  1089. break;
  1090. case V4L2_CID_VBLANK:
  1091. ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
  1092. sensor->mode->format.height + ctrl->val);
  1093. break;
  1094. /* Read-only, but we adjust it based on mode. */
  1095. case V4L2_CID_PIXEL_RATE:
  1096. case V4L2_CID_HBLANK:
  1097. /* Read-only, but we adjust it based on mode. */
  1098. break;
  1099. default:
  1100. dev_info(&client->dev,
  1101. "Control (id:0x%x, val:0x%x) not supported\n",
  1102. ctrl->id, ctrl->val);
  1103. return -EINVAL;
  1104. }
  1105. pm_runtime_put(&client->dev);
  1106. return ret;
  1107. }
  1108. static const struct v4l2_ctrl_ops ov5647_ctrl_ops = {
  1109. .s_ctrl = ov5647_s_ctrl,
  1110. };
  1111. static int ov5647_init_controls(struct ov5647 *sensor)
  1112. {
  1113. struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd);
  1114. int hblank, exposure_max, exposure_def;
  1115. v4l2_ctrl_handler_init(&sensor->ctrls, 8);
  1116. v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
  1117. V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
  1118. v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
  1119. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 0);
  1120. v4l2_ctrl_new_std_menu(&sensor->ctrls, &ov5647_ctrl_ops,
  1121. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
  1122. 0, V4L2_EXPOSURE_MANUAL);
  1123. exposure_max = sensor->mode->vts - 4;
  1124. exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
  1125. sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
  1126. V4L2_CID_EXPOSURE,
  1127. OV5647_EXPOSURE_MIN,
  1128. exposure_max, OV5647_EXPOSURE_STEP,
  1129. exposure_def);
  1130. /* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */
  1131. v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
  1132. V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 32);
  1133. /* By default, PIXEL_RATE is read only, but it does change per mode */
  1134. sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
  1135. V4L2_CID_PIXEL_RATE,
  1136. sensor->mode->pixel_rate,
  1137. sensor->mode->pixel_rate, 1,
  1138. sensor->mode->pixel_rate);
  1139. /* By default, HBLANK is read only, but it does change per mode. */
  1140. hblank = sensor->mode->hts - sensor->mode->format.width;
  1141. sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
  1142. V4L2_CID_HBLANK, hblank, hblank, 1,
  1143. hblank);
  1144. sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
  1145. V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
  1146. OV5647_VTS_MAX -
  1147. sensor->mode->format.height, 1,
  1148. sensor->mode->vts -
  1149. sensor->mode->format.height);
  1150. if (sensor->ctrls.error)
  1151. goto handler_free;
  1152. sensor->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1153. sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1154. sensor->sd.ctrl_handler = &sensor->ctrls;
  1155. return 0;
  1156. handler_free:
  1157. dev_err(&client->dev, "%s Controls initialization failed (%d)\n",
  1158. __func__, sensor->ctrls.error);
  1159. v4l2_ctrl_handler_free(&sensor->ctrls);
  1160. return sensor->ctrls.error;
  1161. }
  1162. static int ov5647_parse_dt(struct ov5647 *sensor, struct device_node *np)
  1163. {
  1164. struct v4l2_fwnode_endpoint bus_cfg = {
  1165. .bus_type = V4L2_MBUS_CSI2_DPHY,
  1166. };
  1167. struct device_node *ep;
  1168. int ret;
  1169. ep = of_graph_get_next_endpoint(np, NULL);
  1170. if (!ep)
  1171. return -EINVAL;
  1172. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
  1173. if (ret)
  1174. goto out;
  1175. sensor->clock_ncont = bus_cfg.bus.mipi_csi2.flags &
  1176. V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  1177. out:
  1178. of_node_put(ep);
  1179. return ret;
  1180. }
  1181. static int ov5647_probe(struct i2c_client *client)
  1182. {
  1183. struct device_node *np = client->dev.of_node;
  1184. struct device *dev = &client->dev;
  1185. struct ov5647 *sensor;
  1186. struct v4l2_subdev *sd;
  1187. u32 xclk_freq;
  1188. int ret;
  1189. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  1190. if (!sensor)
  1191. return -ENOMEM;
  1192. if (IS_ENABLED(CONFIG_OF) && np) {
  1193. ret = ov5647_parse_dt(sensor, np);
  1194. if (ret) {
  1195. dev_err(dev, "DT parsing error: %d\n", ret);
  1196. return ret;
  1197. }
  1198. }
  1199. sensor->xclk = devm_clk_get(dev, NULL);
  1200. if (IS_ERR(sensor->xclk)) {
  1201. dev_err(dev, "could not get xclk");
  1202. return PTR_ERR(sensor->xclk);
  1203. }
  1204. xclk_freq = clk_get_rate(sensor->xclk);
  1205. if (xclk_freq != 25000000) {
  1206. dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
  1207. return -EINVAL;
  1208. }
  1209. /* Request the power down GPIO asserted. */
  1210. sensor->pwdn = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH);
  1211. if (IS_ERR(sensor->pwdn)) {
  1212. dev_err(dev, "Failed to get 'pwdn' gpio\n");
  1213. return -EINVAL;
  1214. }
  1215. mutex_init(&sensor->lock);
  1216. sensor->mode = OV5647_DEFAULT_MODE;
  1217. ret = ov5647_init_controls(sensor);
  1218. if (ret)
  1219. goto mutex_destroy;
  1220. sd = &sensor->sd;
  1221. v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
  1222. sd->internal_ops = &ov5647_subdev_internal_ops;
  1223. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1224. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  1225. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1226. ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
  1227. if (ret < 0)
  1228. goto ctrl_handler_free;
  1229. ret = ov5647_power_on(dev);
  1230. if (ret)
  1231. goto entity_cleanup;
  1232. ret = ov5647_detect(sd);
  1233. if (ret < 0)
  1234. goto power_off;
  1235. ret = v4l2_async_register_subdev(sd);
  1236. if (ret < 0)
  1237. goto power_off;
  1238. /* Enable runtime PM and turn off the device */
  1239. pm_runtime_set_active(dev);
  1240. pm_runtime_enable(dev);
  1241. pm_runtime_idle(dev);
  1242. dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
  1243. return 0;
  1244. power_off:
  1245. ov5647_power_off(dev);
  1246. entity_cleanup:
  1247. media_entity_cleanup(&sd->entity);
  1248. ctrl_handler_free:
  1249. v4l2_ctrl_handler_free(&sensor->ctrls);
  1250. mutex_destroy:
  1251. mutex_destroy(&sensor->lock);
  1252. return ret;
  1253. }
  1254. static void ov5647_remove(struct i2c_client *client)
  1255. {
  1256. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1257. struct ov5647 *sensor = to_sensor(sd);
  1258. v4l2_async_unregister_subdev(&sensor->sd);
  1259. media_entity_cleanup(&sensor->sd.entity);
  1260. v4l2_ctrl_handler_free(&sensor->ctrls);
  1261. v4l2_device_unregister_subdev(sd);
  1262. pm_runtime_disable(&client->dev);
  1263. mutex_destroy(&sensor->lock);
  1264. }
  1265. static const struct dev_pm_ops ov5647_pm_ops = {
  1266. SET_RUNTIME_PM_OPS(ov5647_power_off, ov5647_power_on, NULL)
  1267. };
  1268. static const struct i2c_device_id ov5647_id[] = {
  1269. { "ov5647", 0 },
  1270. { /* sentinel */ }
  1271. };
  1272. MODULE_DEVICE_TABLE(i2c, ov5647_id);
  1273. #if IS_ENABLED(CONFIG_OF)
  1274. static const struct of_device_id ov5647_of_match[] = {
  1275. { .compatible = "ovti,ov5647" },
  1276. { /* sentinel */ },
  1277. };
  1278. MODULE_DEVICE_TABLE(of, ov5647_of_match);
  1279. #endif
  1280. static struct i2c_driver ov5647_driver = {
  1281. .driver = {
  1282. .of_match_table = of_match_ptr(ov5647_of_match),
  1283. .name = "ov5647",
  1284. .pm = &ov5647_pm_ops,
  1285. },
  1286. .probe_new = ov5647_probe,
  1287. .remove = ov5647_remove,
  1288. .id_table = ov5647_id,
  1289. };
  1290. module_i2c_driver(ov5647_driver);
  1291. MODULE_AUTHOR("Ramiro Oliveira <[email protected]>");
  1292. MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
  1293. MODULE_LICENSE("GPL v2");