ov5647.c 32 KB

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