ov5693.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
  4. *
  5. * Adapted from the atomisp-ov5693 driver, with contributions from:
  6. *
  7. * Daniel Scally
  8. * Jean-Michel Hautbois
  9. * Fabian Wuthrich
  10. * Tsuchiya Yuto
  11. * Jordan Hand
  12. * Jake Day
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/i2c.h>
  19. #include <linux/module.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <media/v4l2-cci.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-fwnode.h>
  28. /* System Control */
  29. #define OV5693_SW_RESET_REG CCI_REG8(0x0103)
  30. #define OV5693_SW_STREAM_REG CCI_REG8(0x0100)
  31. #define OV5693_START_STREAMING 0x01
  32. #define OV5693_STOP_STREAMING 0x00
  33. #define OV5693_SW_RESET 0x01
  34. #define OV5693_REG_CHIP_ID CCI_REG16(0x300a)
  35. /* Yes, this is right. The datasheet for the OV5693 gives its ID as 0x5690 */
  36. #define OV5693_CHIP_ID 0x5690
  37. /* Exposure */
  38. #define OV5693_EXPOSURE_CTRL_REG CCI_REG24(0x3500)
  39. #define OV5693_EXPOSURE_CTRL_MASK GENMASK(19, 4)
  40. #define OV5693_INTEGRATION_TIME_MARGIN 8
  41. #define OV5693_EXPOSURE_MIN 1
  42. #define OV5693_EXPOSURE_STEP 1
  43. /* Analogue Gain */
  44. #define OV5693_GAIN_CTRL_REG CCI_REG16(0x350a)
  45. #define OV5693_GAIN_CTRL_MASK GENMASK(10, 4)
  46. #define OV5693_GAIN_MIN 1
  47. #define OV5693_GAIN_MAX 127
  48. #define OV5693_GAIN_DEF 8
  49. #define OV5693_GAIN_STEP 1
  50. /* Digital Gain */
  51. #define OV5693_MWB_RED_GAIN_REG CCI_REG16(0x3400)
  52. #define OV5693_MWB_GREEN_GAIN_REG CCI_REG16(0x3402)
  53. #define OV5693_MWB_BLUE_GAIN_REG CCI_REG16(0x3404)
  54. #define OV5693_MWB_GAIN_MASK GENMASK(11, 0)
  55. #define OV5693_MWB_GAIN_MAX 0x0fff
  56. #define OV5693_DIGITAL_GAIN_MIN 1
  57. #define OV5693_DIGITAL_GAIN_MAX 4095
  58. #define OV5693_DIGITAL_GAIN_DEF 1024
  59. #define OV5693_DIGITAL_GAIN_STEP 1
  60. /* Timing and Format */
  61. #define OV5693_CROP_START_X_REG CCI_REG16(0x3800)
  62. #define OV5693_CROP_START_Y_REG CCI_REG16(0x3802)
  63. #define OV5693_CROP_END_X_REG CCI_REG16(0x3804)
  64. #define OV5693_CROP_END_Y_REG CCI_REG16(0x3806)
  65. #define OV5693_OUTPUT_SIZE_X_REG CCI_REG16(0x3808)
  66. #define OV5693_OUTPUT_SIZE_Y_REG CCI_REG16(0x380a)
  67. #define OV5693_TIMING_HTS_REG CCI_REG16(0x380c)
  68. #define OV5693_FIXED_PPL 2688U
  69. #define OV5693_TIMING_VTS_REG CCI_REG16(0x380e)
  70. #define OV5693_TIMING_MAX_VTS 0xffff
  71. #define OV5693_TIMING_MIN_VTS 0x04
  72. #define OV5693_OFFSET_START_X_REG CCI_REG16(0x3810)
  73. #define OV5693_OFFSET_START_Y_REG CCI_REG16(0x3812)
  74. #define OV5693_SUB_INC_X_REG CCI_REG8(0x3814)
  75. #define OV5693_SUB_INC_Y_REG CCI_REG8(0x3815)
  76. #define OV5693_FORMAT1_REG CCI_REG8(0x3820)
  77. #define OV5693_FORMAT1_FLIP_VERT_ISP_EN BIT(6)
  78. #define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN BIT(1)
  79. #define OV5693_FORMAT1_VBIN_EN BIT(0)
  80. #define OV5693_FORMAT2_REG CCI_REG8(0x3821)
  81. #define OV5693_FORMAT2_HDR_EN BIT(7)
  82. #define OV5693_FORMAT2_FLIP_HORZ_ISP_EN BIT(2)
  83. #define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN BIT(1)
  84. #define OV5693_FORMAT2_HBIN_EN BIT(0)
  85. #define OV5693_ISP_CTRL2_REG CCI_REG8(0x5002)
  86. #define OV5693_ISP_SCALE_ENABLE BIT(7)
  87. /* Pixel Array */
  88. #define OV5693_NATIVE_WIDTH 2624
  89. #define OV5693_NATIVE_HEIGHT 1956
  90. #define OV5693_NATIVE_START_LEFT 0
  91. #define OV5693_NATIVE_START_TOP 0
  92. #define OV5693_ACTIVE_WIDTH 2592
  93. #define OV5693_ACTIVE_HEIGHT 1944
  94. #define OV5693_ACTIVE_START_LEFT 16
  95. #define OV5693_ACTIVE_START_TOP 6
  96. #define OV5693_MIN_CROP_WIDTH 2
  97. #define OV5693_MIN_CROP_HEIGHT 2
  98. /* Test Pattern */
  99. #define OV5693_TEST_PATTERN_REG CCI_REG8(0x5e00)
  100. #define OV5693_TEST_PATTERN_ENABLE BIT(7)
  101. #define OV5693_TEST_PATTERN_ROLLING BIT(6)
  102. #define OV5693_TEST_PATTERN_RANDOM 0x01
  103. #define OV5693_TEST_PATTERN_BARS 0x00
  104. /* System Frequencies */
  105. #define OV5693_XVCLK_FREQ 19200000
  106. #define OV5693_LINK_FREQ_419_2MHZ 419200000
  107. #define OV5693_PIXEL_RATE 167680000
  108. #define to_ov5693_sensor(x) container_of(x, struct ov5693_device, sd)
  109. static const char * const ov5693_supply_names[] = {
  110. "avdd", /* Analog power */
  111. "dovdd", /* Digital I/O power */
  112. "dvdd", /* Digital circuit power */
  113. };
  114. #define OV5693_NUM_SUPPLIES ARRAY_SIZE(ov5693_supply_names)
  115. struct ov5693_device {
  116. struct device *dev;
  117. struct regmap *regmap;
  118. /* Protect against concurrent changes to controls */
  119. struct mutex lock;
  120. struct gpio_desc *reset;
  121. struct gpio_desc *powerdown;
  122. struct regulator_bulk_data supplies[OV5693_NUM_SUPPLIES];
  123. struct clk *xvclk;
  124. struct ov5693_mode {
  125. struct v4l2_rect crop;
  126. struct v4l2_mbus_framefmt format;
  127. bool binning_x;
  128. bool binning_y;
  129. unsigned int inc_x_odd;
  130. unsigned int inc_y_odd;
  131. unsigned int vts;
  132. } mode;
  133. struct v4l2_subdev sd;
  134. struct media_pad pad;
  135. struct ov5693_v4l2_ctrls {
  136. struct v4l2_ctrl_handler handler;
  137. struct v4l2_ctrl *link_freq;
  138. struct v4l2_ctrl *pixel_rate;
  139. struct v4l2_ctrl *exposure;
  140. struct v4l2_ctrl *analogue_gain;
  141. struct v4l2_ctrl *digital_gain;
  142. struct v4l2_ctrl *hflip;
  143. struct v4l2_ctrl *vflip;
  144. struct v4l2_ctrl *hblank;
  145. struct v4l2_ctrl *vblank;
  146. struct v4l2_ctrl *test_pattern;
  147. } ctrls;
  148. };
  149. static const struct cci_reg_sequence ov5693_global_regs[] = {
  150. {CCI_REG8(0x3016), 0xf0},
  151. {CCI_REG8(0x3017), 0xf0},
  152. {CCI_REG8(0x3018), 0xf0},
  153. {CCI_REG8(0x3022), 0x01},
  154. {CCI_REG8(0x3028), 0x44},
  155. {CCI_REG8(0x3098), 0x02},
  156. {CCI_REG8(0x3099), 0x19},
  157. {CCI_REG8(0x309a), 0x02},
  158. {CCI_REG8(0x309b), 0x01},
  159. {CCI_REG8(0x309c), 0x00},
  160. {CCI_REG8(0x30a0), 0xd2},
  161. {CCI_REG8(0x30a2), 0x01},
  162. {CCI_REG8(0x30b2), 0x00},
  163. {CCI_REG8(0x30b3), 0x83},
  164. {CCI_REG8(0x30b4), 0x03},
  165. {CCI_REG8(0x30b5), 0x04},
  166. {CCI_REG8(0x30b6), 0x01},
  167. {CCI_REG8(0x3080), 0x01},
  168. {CCI_REG8(0x3104), 0x21},
  169. {CCI_REG8(0x3106), 0x00},
  170. {CCI_REG8(0x3406), 0x01},
  171. {CCI_REG8(0x3503), 0x07},
  172. {CCI_REG8(0x350b), 0x40},
  173. {CCI_REG8(0x3601), 0x0a},
  174. {CCI_REG8(0x3602), 0x38},
  175. {CCI_REG8(0x3612), 0x80},
  176. {CCI_REG8(0x3620), 0x54},
  177. {CCI_REG8(0x3621), 0xc7},
  178. {CCI_REG8(0x3622), 0x0f},
  179. {CCI_REG8(0x3625), 0x10},
  180. {CCI_REG8(0x3630), 0x55},
  181. {CCI_REG8(0x3631), 0xf4},
  182. {CCI_REG8(0x3632), 0x00},
  183. {CCI_REG8(0x3633), 0x34},
  184. {CCI_REG8(0x3634), 0x02},
  185. {CCI_REG8(0x364d), 0x0d},
  186. {CCI_REG8(0x364f), 0xdd},
  187. {CCI_REG8(0x3660), 0x04},
  188. {CCI_REG8(0x3662), 0x10},
  189. {CCI_REG8(0x3663), 0xf1},
  190. {CCI_REG8(0x3665), 0x00},
  191. {CCI_REG8(0x3666), 0x20},
  192. {CCI_REG8(0x3667), 0x00},
  193. {CCI_REG8(0x366a), 0x80},
  194. {CCI_REG8(0x3680), 0xe0},
  195. {CCI_REG8(0x3681), 0x00},
  196. {CCI_REG8(0x3700), 0x42},
  197. {CCI_REG8(0x3701), 0x14},
  198. {CCI_REG8(0x3702), 0xa0},
  199. {CCI_REG8(0x3703), 0xd8},
  200. {CCI_REG8(0x3704), 0x78},
  201. {CCI_REG8(0x3705), 0x02},
  202. {CCI_REG8(0x370a), 0x00},
  203. {CCI_REG8(0x370b), 0x20},
  204. {CCI_REG8(0x370c), 0x0c},
  205. {CCI_REG8(0x370d), 0x11},
  206. {CCI_REG8(0x370e), 0x00},
  207. {CCI_REG8(0x370f), 0x40},
  208. {CCI_REG8(0x3710), 0x00},
  209. {CCI_REG8(0x371a), 0x1c},
  210. {CCI_REG8(0x371b), 0x05},
  211. {CCI_REG8(0x371c), 0x01},
  212. {CCI_REG8(0x371e), 0xa1},
  213. {CCI_REG8(0x371f), 0x0c},
  214. {CCI_REG8(0x3721), 0x00},
  215. {CCI_REG8(0x3724), 0x10},
  216. {CCI_REG8(0x3726), 0x00},
  217. {CCI_REG8(0x372a), 0x01},
  218. {CCI_REG8(0x3730), 0x10},
  219. {CCI_REG8(0x3738), 0x22},
  220. {CCI_REG8(0x3739), 0xe5},
  221. {CCI_REG8(0x373a), 0x50},
  222. {CCI_REG8(0x373b), 0x02},
  223. {CCI_REG8(0x373c), 0x41},
  224. {CCI_REG8(0x373f), 0x02},
  225. {CCI_REG8(0x3740), 0x42},
  226. {CCI_REG8(0x3741), 0x02},
  227. {CCI_REG8(0x3742), 0x18},
  228. {CCI_REG8(0x3743), 0x01},
  229. {CCI_REG8(0x3744), 0x02},
  230. {CCI_REG8(0x3747), 0x10},
  231. {CCI_REG8(0x374c), 0x04},
  232. {CCI_REG8(0x3751), 0xf0},
  233. {CCI_REG8(0x3752), 0x00},
  234. {CCI_REG8(0x3753), 0x00},
  235. {CCI_REG8(0x3754), 0xc0},
  236. {CCI_REG8(0x3755), 0x00},
  237. {CCI_REG8(0x3756), 0x1a},
  238. {CCI_REG8(0x3758), 0x00},
  239. {CCI_REG8(0x3759), 0x0f},
  240. {CCI_REG8(0x376b), 0x44},
  241. {CCI_REG8(0x375c), 0x04},
  242. {CCI_REG8(0x3774), 0x10},
  243. {CCI_REG8(0x3776), 0x00},
  244. {CCI_REG8(0x377f), 0x08},
  245. {CCI_REG8(0x3780), 0x22},
  246. {CCI_REG8(0x3781), 0x0c},
  247. {CCI_REG8(0x3784), 0x2c},
  248. {CCI_REG8(0x3785), 0x1e},
  249. {CCI_REG8(0x378f), 0xf5},
  250. {CCI_REG8(0x3791), 0xb0},
  251. {CCI_REG8(0x3795), 0x00},
  252. {CCI_REG8(0x3796), 0x64},
  253. {CCI_REG8(0x3797), 0x11},
  254. {CCI_REG8(0x3798), 0x30},
  255. {CCI_REG8(0x3799), 0x41},
  256. {CCI_REG8(0x379a), 0x07},
  257. {CCI_REG8(0x379b), 0xb0},
  258. {CCI_REG8(0x379c), 0x0c},
  259. {CCI_REG8(0x3a04), 0x06},
  260. {CCI_REG8(0x3a05), 0x14},
  261. {CCI_REG8(0x3e07), 0x20},
  262. {CCI_REG8(0x4000), 0x08},
  263. {CCI_REG8(0x4001), 0x04},
  264. {CCI_REG8(0x4004), 0x08},
  265. {CCI_REG8(0x4006), 0x20},
  266. {CCI_REG8(0x4008), 0x24},
  267. {CCI_REG8(0x4009), 0x10},
  268. {CCI_REG8(0x4058), 0x00},
  269. {CCI_REG8(0x4101), 0xb2},
  270. {CCI_REG8(0x4307), 0x31},
  271. {CCI_REG8(0x4511), 0x05},
  272. {CCI_REG8(0x4512), 0x01},
  273. {CCI_REG8(0x481f), 0x30},
  274. {CCI_REG8(0x4826), 0x2c},
  275. {CCI_REG8(0x4d02), 0xfd},
  276. {CCI_REG8(0x4d03), 0xf5},
  277. {CCI_REG8(0x4d04), 0x0c},
  278. {CCI_REG8(0x4d05), 0xcc},
  279. {CCI_REG8(0x4837), 0x0a},
  280. {CCI_REG8(0x5003), 0x20},
  281. {CCI_REG8(0x5013), 0x00},
  282. {CCI_REG8(0x5842), 0x01},
  283. {CCI_REG8(0x5843), 0x2b},
  284. {CCI_REG8(0x5844), 0x01},
  285. {CCI_REG8(0x5845), 0x92},
  286. {CCI_REG8(0x5846), 0x01},
  287. {CCI_REG8(0x5847), 0x8f},
  288. {CCI_REG8(0x5848), 0x01},
  289. {CCI_REG8(0x5849), 0x0c},
  290. {CCI_REG8(0x5e10), 0x0c},
  291. {CCI_REG8(0x3820), 0x00},
  292. {CCI_REG8(0x3821), 0x1e},
  293. {CCI_REG8(0x5041), 0x14}
  294. };
  295. static const struct v4l2_rect ov5693_default_crop = {
  296. .left = OV5693_ACTIVE_START_LEFT,
  297. .top = OV5693_ACTIVE_START_TOP,
  298. .width = OV5693_ACTIVE_WIDTH,
  299. .height = OV5693_ACTIVE_HEIGHT,
  300. };
  301. static const struct v4l2_mbus_framefmt ov5693_default_fmt = {
  302. .width = OV5693_ACTIVE_WIDTH,
  303. .height = OV5693_ACTIVE_HEIGHT,
  304. .code = MEDIA_BUS_FMT_SBGGR10_1X10,
  305. };
  306. static const s64 link_freq_menu_items[] = {
  307. OV5693_LINK_FREQ_419_2MHZ
  308. };
  309. static const char * const ov5693_test_pattern_menu[] = {
  310. "Disabled",
  311. "Random Data",
  312. "Colour Bars",
  313. "Colour Bars with Rolling Bar"
  314. };
  315. static const u8 ov5693_test_pattern_bits[] = {
  316. 0,
  317. OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_RANDOM,
  318. OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS,
  319. OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS |
  320. OV5693_TEST_PATTERN_ROLLING,
  321. };
  322. /* V4L2 Controls Functions */
  323. static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
  324. bool enable)
  325. {
  326. u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN |
  327. OV5693_FORMAT1_FLIP_VERT_SENSOR_EN;
  328. int ret;
  329. ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG, bits,
  330. enable ? bits : 0, NULL);
  331. if (ret)
  332. return ret;
  333. return 0;
  334. }
  335. static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
  336. bool enable)
  337. {
  338. u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN |
  339. OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN;
  340. int ret;
  341. ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG, bits,
  342. enable ? bits : 0, NULL);
  343. if (ret)
  344. return ret;
  345. return 0;
  346. }
  347. static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value)
  348. {
  349. u64 exposure;
  350. int ret;
  351. ret = cci_read(ov5693->regmap, OV5693_EXPOSURE_CTRL_REG, &exposure,
  352. NULL);
  353. if (ret)
  354. return ret;
  355. /* The lowest 4 bits are unsupported fractional bits */
  356. *value = exposure >> 4;
  357. return 0;
  358. }
  359. static int ov5693_exposure_configure(struct ov5693_device *ov5693,
  360. u32 exposure)
  361. {
  362. int ret = 0;
  363. exposure = (exposure << 4) & OV5693_EXPOSURE_CTRL_MASK;
  364. cci_write(ov5693->regmap, OV5693_EXPOSURE_CTRL_REG, exposure, &ret);
  365. return ret;
  366. }
  367. static int ov5693_get_gain(struct ov5693_device *ov5693, u32 *gain)
  368. {
  369. u64 value;
  370. int ret;
  371. ret = cci_read(ov5693->regmap, OV5693_GAIN_CTRL_REG, &value, NULL);
  372. if (ret)
  373. return ret;
  374. /* As with exposure, the lowest 4 bits are fractional bits. */
  375. *gain = value >> 4;
  376. return ret;
  377. }
  378. static int ov5693_digital_gain_configure(struct ov5693_device *ov5693,
  379. u32 gain)
  380. {
  381. int ret = 0;
  382. gain &= OV5693_MWB_GAIN_MASK;
  383. cci_write(ov5693->regmap, OV5693_MWB_RED_GAIN_REG, gain, &ret);
  384. cci_write(ov5693->regmap, OV5693_MWB_GREEN_GAIN_REG, gain, &ret);
  385. cci_write(ov5693->regmap, OV5693_MWB_BLUE_GAIN_REG, gain, &ret);
  386. return ret;
  387. }
  388. static int ov5693_analog_gain_configure(struct ov5693_device *ov5693, u32 gain)
  389. {
  390. int ret = 0;
  391. gain = (gain << 4) & OV5693_GAIN_CTRL_MASK;
  392. cci_write(ov5693->regmap, OV5693_GAIN_CTRL_REG, gain, &ret);
  393. return ret;
  394. }
  395. static int ov5693_vts_configure(struct ov5693_device *ov5693, u32 vblank)
  396. {
  397. u16 vts = ov5693->mode.format.height + vblank;
  398. int ret = 0;
  399. cci_write(ov5693->regmap, OV5693_TIMING_VTS_REG, vts, &ret);
  400. return ret;
  401. }
  402. static int ov5693_test_pattern_configure(struct ov5693_device *ov5693, u32 idx)
  403. {
  404. int ret = 0;
  405. cci_write(ov5693->regmap, OV5693_TEST_PATTERN_REG,
  406. ov5693_test_pattern_bits[idx], &ret);
  407. return ret;
  408. }
  409. static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
  410. {
  411. struct ov5693_device *ov5693 =
  412. container_of(ctrl->handler, struct ov5693_device, ctrls.handler);
  413. int ret = 0;
  414. /* If VBLANK is altered we need to update exposure to compensate */
  415. if (ctrl->id == V4L2_CID_VBLANK) {
  416. int exposure_max;
  417. exposure_max = ov5693->mode.format.height + ctrl->val -
  418. OV5693_INTEGRATION_TIME_MARGIN;
  419. __v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
  420. ov5693->ctrls.exposure->minimum,
  421. exposure_max,
  422. ov5693->ctrls.exposure->step,
  423. min(ov5693->ctrls.exposure->val,
  424. exposure_max));
  425. }
  426. /* Only apply changes to the controls if the device is powered up */
  427. if (!pm_runtime_get_if_in_use(ov5693->dev))
  428. return 0;
  429. switch (ctrl->id) {
  430. case V4L2_CID_EXPOSURE:
  431. ret = ov5693_exposure_configure(ov5693, ctrl->val);
  432. break;
  433. case V4L2_CID_ANALOGUE_GAIN:
  434. ret = ov5693_analog_gain_configure(ov5693, ctrl->val);
  435. break;
  436. case V4L2_CID_DIGITAL_GAIN:
  437. ret = ov5693_digital_gain_configure(ov5693, ctrl->val);
  438. break;
  439. case V4L2_CID_HFLIP:
  440. ret = ov5693_flip_horz_configure(ov5693, !!ctrl->val);
  441. break;
  442. case V4L2_CID_VFLIP:
  443. ret = ov5693_flip_vert_configure(ov5693, !!ctrl->val);
  444. break;
  445. case V4L2_CID_VBLANK:
  446. ret = ov5693_vts_configure(ov5693, ctrl->val);
  447. break;
  448. case V4L2_CID_TEST_PATTERN:
  449. ret = ov5693_test_pattern_configure(ov5693, ctrl->val);
  450. break;
  451. default:
  452. ret = -EINVAL;
  453. }
  454. pm_runtime_put(ov5693->dev);
  455. return ret;
  456. }
  457. static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  458. {
  459. struct ov5693_device *ov5693 = container_of(ctrl->handler,
  460. struct ov5693_device,
  461. ctrls.handler);
  462. switch (ctrl->id) {
  463. case V4L2_CID_EXPOSURE_ABSOLUTE:
  464. return ov5693_get_exposure(ov5693, &ctrl->val);
  465. case V4L2_CID_AUTOGAIN:
  466. return ov5693_get_gain(ov5693, &ctrl->val);
  467. default:
  468. return -EINVAL;
  469. }
  470. }
  471. static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
  472. .s_ctrl = ov5693_s_ctrl,
  473. .g_volatile_ctrl = ov5693_g_volatile_ctrl
  474. };
  475. /* System Control Functions */
  476. static int ov5693_mode_configure(struct ov5693_device *ov5693)
  477. {
  478. const struct ov5693_mode *mode = &ov5693->mode;
  479. int ret = 0;
  480. /* Crop Start X */
  481. cci_write(ov5693->regmap, OV5693_CROP_START_X_REG, mode->crop.left,
  482. &ret);
  483. /* Offset X */
  484. cci_write(ov5693->regmap, OV5693_OFFSET_START_X_REG, 0, &ret);
  485. /* Output Size X */
  486. cci_write(ov5693->regmap, OV5693_OUTPUT_SIZE_X_REG, mode->format.width,
  487. &ret);
  488. /* Crop End X */
  489. cci_write(ov5693->regmap, OV5693_CROP_END_X_REG,
  490. mode->crop.left + mode->crop.width, &ret);
  491. /* Horizontal Total Size */
  492. cci_write(ov5693->regmap, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL,
  493. &ret);
  494. /* Crop Start Y */
  495. cci_write(ov5693->regmap, OV5693_CROP_START_Y_REG, mode->crop.top,
  496. &ret);
  497. /* Offset Y */
  498. cci_write(ov5693->regmap, OV5693_OFFSET_START_Y_REG, 0, &ret);
  499. /* Output Size Y */
  500. cci_write(ov5693->regmap, OV5693_OUTPUT_SIZE_Y_REG, mode->format.height,
  501. &ret);
  502. /* Crop End Y */
  503. cci_write(ov5693->regmap, OV5693_CROP_END_Y_REG,
  504. mode->crop.top + mode->crop.height, &ret);
  505. /* Subsample X increase */
  506. cci_write(ov5693->regmap, OV5693_SUB_INC_X_REG,
  507. ((mode->inc_x_odd << 4) & 0xf0) | 0x01, &ret);
  508. /* Subsample Y increase */
  509. cci_write(ov5693->regmap, OV5693_SUB_INC_Y_REG,
  510. ((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret);
  511. /* Binning */
  512. cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG,
  513. OV5693_FORMAT1_VBIN_EN,
  514. mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0, &ret);
  515. cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG,
  516. OV5693_FORMAT2_HBIN_EN,
  517. mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0, &ret);
  518. return ret;
  519. }
  520. static int ov5693_enable_streaming(struct ov5693_device *ov5693, bool enable)
  521. {
  522. int ret = 0;
  523. cci_write(ov5693->regmap, OV5693_SW_STREAM_REG,
  524. enable ? OV5693_START_STREAMING : OV5693_STOP_STREAMING,
  525. &ret);
  526. return ret;
  527. }
  528. static int ov5693_sw_reset(struct ov5693_device *ov5693)
  529. {
  530. int ret = 0;
  531. cci_write(ov5693->regmap, OV5693_SW_RESET_REG, OV5693_SW_RESET, &ret);
  532. return ret;
  533. }
  534. static int ov5693_sensor_init(struct ov5693_device *ov5693)
  535. {
  536. int ret;
  537. ret = ov5693_sw_reset(ov5693);
  538. if (ret)
  539. return dev_err_probe(ov5693->dev, ret,
  540. "software reset error\n");
  541. ret = cci_multi_reg_write(ov5693->regmap, ov5693_global_regs,
  542. ARRAY_SIZE(ov5693_global_regs), NULL);
  543. if (ret)
  544. return dev_err_probe(ov5693->dev, ret,
  545. "global settings error\n");
  546. ret = ov5693_mode_configure(ov5693);
  547. if (ret)
  548. return dev_err_probe(ov5693->dev, ret,
  549. "mode configure error\n");
  550. ret = ov5693_enable_streaming(ov5693, false);
  551. if (ret)
  552. dev_err(ov5693->dev, "stop streaming error\n");
  553. return ret;
  554. }
  555. static void ov5693_sensor_powerdown(struct ov5693_device *ov5693)
  556. {
  557. gpiod_set_value_cansleep(ov5693->reset, 1);
  558. gpiod_set_value_cansleep(ov5693->powerdown, 1);
  559. regulator_bulk_disable(OV5693_NUM_SUPPLIES, ov5693->supplies);
  560. clk_disable_unprepare(ov5693->xvclk);
  561. }
  562. static int ov5693_sensor_powerup(struct ov5693_device *ov5693)
  563. {
  564. int ret;
  565. gpiod_set_value_cansleep(ov5693->reset, 1);
  566. gpiod_set_value_cansleep(ov5693->powerdown, 1);
  567. ret = clk_prepare_enable(ov5693->xvclk);
  568. if (ret) {
  569. dev_err(ov5693->dev, "Failed to enable clk\n");
  570. goto fail_power;
  571. }
  572. ret = regulator_bulk_enable(OV5693_NUM_SUPPLIES, ov5693->supplies);
  573. if (ret) {
  574. dev_err(ov5693->dev, "Failed to enable regulators\n");
  575. goto fail_power;
  576. }
  577. gpiod_set_value_cansleep(ov5693->powerdown, 0);
  578. gpiod_set_value_cansleep(ov5693->reset, 0);
  579. usleep_range(5000, 7500);
  580. return 0;
  581. fail_power:
  582. ov5693_sensor_powerdown(ov5693);
  583. return ret;
  584. }
  585. static int __maybe_unused ov5693_sensor_suspend(struct device *dev)
  586. {
  587. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  588. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  589. ov5693_sensor_powerdown(ov5693);
  590. return 0;
  591. }
  592. static int __maybe_unused ov5693_sensor_resume(struct device *dev)
  593. {
  594. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  595. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  596. int ret;
  597. mutex_lock(&ov5693->lock);
  598. ret = ov5693_sensor_powerup(ov5693);
  599. if (ret)
  600. goto out_unlock;
  601. ret = ov5693_sensor_init(ov5693);
  602. if (ret) {
  603. dev_err(dev, "ov5693 sensor init failure\n");
  604. goto err_power;
  605. }
  606. goto out_unlock;
  607. err_power:
  608. ov5693_sensor_powerdown(ov5693);
  609. out_unlock:
  610. mutex_unlock(&ov5693->lock);
  611. return ret;
  612. }
  613. static int ov5693_detect(struct ov5693_device *ov5693)
  614. {
  615. int ret;
  616. u64 id;
  617. ret = cci_read(ov5693->regmap, OV5693_REG_CHIP_ID, &id, NULL);
  618. if (ret)
  619. return ret;
  620. if (id != OV5693_CHIP_ID)
  621. return dev_err_probe(ov5693->dev, -ENODEV,
  622. "sensor ID mismatch. Got 0x%04llx\n", id);
  623. return 0;
  624. }
  625. /* V4L2 Framework callbacks */
  626. static unsigned int __ov5693_calc_vts(u32 height)
  627. {
  628. /*
  629. * We need to set a sensible default VTS for whatever format height we
  630. * happen to be given from set_fmt(). This function just targets
  631. * an even multiple of 30fps.
  632. */
  633. unsigned int tgt_fps;
  634. tgt_fps = rounddown(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / height, 30);
  635. return ALIGN_DOWN(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / tgt_fps, 2);
  636. }
  637. static struct v4l2_mbus_framefmt *
  638. __ov5693_get_pad_format(struct ov5693_device *ov5693,
  639. struct v4l2_subdev_state *state,
  640. unsigned int pad, enum v4l2_subdev_format_whence which)
  641. {
  642. switch (which) {
  643. case V4L2_SUBDEV_FORMAT_TRY:
  644. return v4l2_subdev_state_get_format(state, pad);
  645. case V4L2_SUBDEV_FORMAT_ACTIVE:
  646. return &ov5693->mode.format;
  647. default:
  648. return NULL;
  649. }
  650. }
  651. static struct v4l2_rect *
  652. __ov5693_get_pad_crop(struct ov5693_device *ov5693,
  653. struct v4l2_subdev_state *state,
  654. unsigned int pad, enum v4l2_subdev_format_whence which)
  655. {
  656. switch (which) {
  657. case V4L2_SUBDEV_FORMAT_TRY:
  658. return v4l2_subdev_state_get_crop(state, pad);
  659. case V4L2_SUBDEV_FORMAT_ACTIVE:
  660. return &ov5693->mode.crop;
  661. }
  662. return NULL;
  663. }
  664. static int ov5693_get_fmt(struct v4l2_subdev *sd,
  665. struct v4l2_subdev_state *state,
  666. struct v4l2_subdev_format *format)
  667. {
  668. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  669. format->format = ov5693->mode.format;
  670. return 0;
  671. }
  672. static int ov5693_set_fmt(struct v4l2_subdev *sd,
  673. struct v4l2_subdev_state *state,
  674. struct v4l2_subdev_format *format)
  675. {
  676. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  677. const struct v4l2_rect *crop;
  678. struct v4l2_mbus_framefmt *fmt;
  679. unsigned int hratio, vratio;
  680. unsigned int width, height;
  681. unsigned int hblank;
  682. int exposure_max;
  683. crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which);
  684. /*
  685. * Align to two to simplify the binning calculations below, and clamp
  686. * the requested format at the crop rectangle
  687. */
  688. width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
  689. OV5693_MIN_CROP_WIDTH, crop->width);
  690. height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
  691. OV5693_MIN_CROP_HEIGHT, crop->height);
  692. /*
  693. * We can only support setting either the dimensions of the crop rect
  694. * or those dimensions binned (separately) by a factor of two.
  695. */
  696. hratio = clamp_t(unsigned int,
  697. DIV_ROUND_CLOSEST(crop->width, width), 1, 2);
  698. vratio = clamp_t(unsigned int,
  699. DIV_ROUND_CLOSEST(crop->height, height), 1, 2);
  700. fmt = __ov5693_get_pad_format(ov5693, state, format->pad,
  701. format->which);
  702. fmt->width = crop->width / hratio;
  703. fmt->height = crop->height / vratio;
  704. fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  705. format->format = *fmt;
  706. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  707. return 0;
  708. mutex_lock(&ov5693->lock);
  709. ov5693->mode.binning_x = hratio > 1;
  710. ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1;
  711. ov5693->mode.binning_y = vratio > 1;
  712. ov5693->mode.inc_y_odd = vratio > 1 ? 3 : 1;
  713. ov5693->mode.vts = __ov5693_calc_vts(fmt->height);
  714. __v4l2_ctrl_modify_range(ov5693->ctrls.vblank,
  715. OV5693_TIMING_MIN_VTS,
  716. OV5693_TIMING_MAX_VTS - fmt->height,
  717. 1, ov5693->mode.vts - fmt->height);
  718. __v4l2_ctrl_s_ctrl(ov5693->ctrls.vblank,
  719. ov5693->mode.vts - fmt->height);
  720. hblank = OV5693_FIXED_PPL - fmt->width;
  721. __v4l2_ctrl_modify_range(ov5693->ctrls.hblank, hblank, hblank, 1,
  722. hblank);
  723. exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
  724. __v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
  725. ov5693->ctrls.exposure->minimum, exposure_max,
  726. ov5693->ctrls.exposure->step,
  727. min(ov5693->ctrls.exposure->val,
  728. exposure_max));
  729. mutex_unlock(&ov5693->lock);
  730. return 0;
  731. }
  732. static int ov5693_get_selection(struct v4l2_subdev *sd,
  733. struct v4l2_subdev_state *state,
  734. struct v4l2_subdev_selection *sel)
  735. {
  736. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  737. switch (sel->target) {
  738. case V4L2_SEL_TGT_CROP:
  739. mutex_lock(&ov5693->lock);
  740. sel->r = *__ov5693_get_pad_crop(ov5693, state, sel->pad,
  741. sel->which);
  742. mutex_unlock(&ov5693->lock);
  743. break;
  744. case V4L2_SEL_TGT_NATIVE_SIZE:
  745. sel->r.top = 0;
  746. sel->r.left = 0;
  747. sel->r.width = OV5693_NATIVE_WIDTH;
  748. sel->r.height = OV5693_NATIVE_HEIGHT;
  749. break;
  750. case V4L2_SEL_TGT_CROP_BOUNDS:
  751. case V4L2_SEL_TGT_CROP_DEFAULT:
  752. sel->r.top = OV5693_ACTIVE_START_TOP;
  753. sel->r.left = OV5693_ACTIVE_START_LEFT;
  754. sel->r.width = OV5693_ACTIVE_WIDTH;
  755. sel->r.height = OV5693_ACTIVE_HEIGHT;
  756. break;
  757. default:
  758. return -EINVAL;
  759. }
  760. return 0;
  761. }
  762. static int ov5693_set_selection(struct v4l2_subdev *sd,
  763. struct v4l2_subdev_state *state,
  764. struct v4l2_subdev_selection *sel)
  765. {
  766. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  767. struct v4l2_mbus_framefmt *format;
  768. struct v4l2_rect *__crop;
  769. struct v4l2_rect rect;
  770. if (sel->target != V4L2_SEL_TGT_CROP)
  771. return -EINVAL;
  772. /*
  773. * Clamp the boundaries of the crop rectangle to the size of the sensor
  774. * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
  775. * disrupted.
  776. */
  777. rect.left = clamp(ALIGN(sel->r.left, 2), OV5693_NATIVE_START_LEFT,
  778. OV5693_NATIVE_WIDTH);
  779. rect.top = clamp(ALIGN(sel->r.top, 2), OV5693_NATIVE_START_TOP,
  780. OV5693_NATIVE_HEIGHT);
  781. rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
  782. OV5693_MIN_CROP_WIDTH, OV5693_NATIVE_WIDTH);
  783. rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
  784. OV5693_MIN_CROP_HEIGHT, OV5693_NATIVE_HEIGHT);
  785. /* Make sure the crop rectangle isn't outside the bounds of the array */
  786. rect.width = min_t(unsigned int, rect.width,
  787. OV5693_NATIVE_WIDTH - rect.left);
  788. rect.height = min_t(unsigned int, rect.height,
  789. OV5693_NATIVE_HEIGHT - rect.top);
  790. __crop = __ov5693_get_pad_crop(ov5693, state, sel->pad, sel->which);
  791. if (rect.width != __crop->width || rect.height != __crop->height) {
  792. /*
  793. * Reset the output image size if the crop rectangle size has
  794. * been modified.
  795. */
  796. format = __ov5693_get_pad_format(ov5693, state, sel->pad,
  797. sel->which);
  798. format->width = rect.width;
  799. format->height = rect.height;
  800. }
  801. *__crop = rect;
  802. sel->r = rect;
  803. return 0;
  804. }
  805. static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
  806. {
  807. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  808. int ret;
  809. if (enable) {
  810. ret = pm_runtime_resume_and_get(ov5693->dev);
  811. if (ret)
  812. return ret;
  813. mutex_lock(&ov5693->lock);
  814. ret = __v4l2_ctrl_handler_setup(&ov5693->ctrls.handler);
  815. if (ret) {
  816. mutex_unlock(&ov5693->lock);
  817. goto err_power_down;
  818. }
  819. ret = ov5693_enable_streaming(ov5693, true);
  820. mutex_unlock(&ov5693->lock);
  821. } else {
  822. mutex_lock(&ov5693->lock);
  823. ret = ov5693_enable_streaming(ov5693, false);
  824. mutex_unlock(&ov5693->lock);
  825. }
  826. if (ret)
  827. goto err_power_down;
  828. if (!enable)
  829. pm_runtime_put(ov5693->dev);
  830. return 0;
  831. err_power_down:
  832. pm_runtime_put_noidle(ov5693->dev);
  833. return ret;
  834. }
  835. static int ov5693_get_frame_interval(struct v4l2_subdev *sd,
  836. struct v4l2_subdev_state *sd_state,
  837. struct v4l2_subdev_frame_interval *interval)
  838. {
  839. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  840. unsigned int framesize = OV5693_FIXED_PPL * (ov5693->mode.format.height +
  841. ov5693->ctrls.vblank->val);
  842. unsigned int fps = DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE, framesize);
  843. /*
  844. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  845. * subdev active state API.
  846. */
  847. if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  848. return -EINVAL;
  849. interval->interval.numerator = 1;
  850. interval->interval.denominator = fps;
  851. return 0;
  852. }
  853. static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
  854. struct v4l2_subdev_state *state,
  855. struct v4l2_subdev_mbus_code_enum *code)
  856. {
  857. /* Only a single mbus format is supported */
  858. if (code->index > 0)
  859. return -EINVAL;
  860. code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  861. return 0;
  862. }
  863. static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
  864. struct v4l2_subdev_state *state,
  865. struct v4l2_subdev_frame_size_enum *fse)
  866. {
  867. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  868. struct v4l2_rect *__crop;
  869. if (fse->index > 1 || fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
  870. return -EINVAL;
  871. __crop = __ov5693_get_pad_crop(ov5693, state, fse->pad, fse->which);
  872. if (!__crop)
  873. return -EINVAL;
  874. fse->min_width = __crop->width / (fse->index + 1);
  875. fse->min_height = __crop->height / (fse->index + 1);
  876. fse->max_width = fse->min_width;
  877. fse->max_height = fse->min_height;
  878. return 0;
  879. }
  880. static const struct v4l2_subdev_video_ops ov5693_video_ops = {
  881. .s_stream = ov5693_s_stream,
  882. };
  883. static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
  884. .enum_mbus_code = ov5693_enum_mbus_code,
  885. .enum_frame_size = ov5693_enum_frame_size,
  886. .get_fmt = ov5693_get_fmt,
  887. .set_fmt = ov5693_set_fmt,
  888. .get_selection = ov5693_get_selection,
  889. .set_selection = ov5693_set_selection,
  890. .get_frame_interval = ov5693_get_frame_interval,
  891. };
  892. static const struct v4l2_subdev_ops ov5693_ops = {
  893. .video = &ov5693_video_ops,
  894. .pad = &ov5693_pad_ops,
  895. };
  896. /* Sensor and Driver Configuration Functions */
  897. static int ov5693_init_controls(struct ov5693_device *ov5693)
  898. {
  899. const struct v4l2_ctrl_ops *ops = &ov5693_ctrl_ops;
  900. struct ov5693_v4l2_ctrls *ctrls = &ov5693->ctrls;
  901. struct v4l2_fwnode_device_properties props;
  902. int vblank_max, vblank_def;
  903. int exposure_max;
  904. int hblank;
  905. int ret;
  906. ret = v4l2_ctrl_handler_init(&ctrls->handler, 12);
  907. if (ret)
  908. return ret;
  909. /* link freq */
  910. ctrls->link_freq = v4l2_ctrl_new_int_menu(&ctrls->handler,
  911. NULL, V4L2_CID_LINK_FREQ,
  912. 0, 0, link_freq_menu_items);
  913. if (ctrls->link_freq)
  914. ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  915. /* pixel rate */
  916. ctrls->pixel_rate = v4l2_ctrl_new_std(&ctrls->handler, NULL,
  917. V4L2_CID_PIXEL_RATE, 0,
  918. OV5693_PIXEL_RATE, 1,
  919. OV5693_PIXEL_RATE);
  920. /* Exposure */
  921. exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
  922. ctrls->exposure = v4l2_ctrl_new_std(&ctrls->handler, ops,
  923. V4L2_CID_EXPOSURE,
  924. OV5693_EXPOSURE_MIN, exposure_max,
  925. OV5693_EXPOSURE_STEP, exposure_max);
  926. /* Gain */
  927. ctrls->analogue_gain = v4l2_ctrl_new_std(&ctrls->handler,
  928. ops, V4L2_CID_ANALOGUE_GAIN,
  929. OV5693_GAIN_MIN,
  930. OV5693_GAIN_MAX,
  931. OV5693_GAIN_STEP,
  932. OV5693_GAIN_DEF);
  933. ctrls->digital_gain = v4l2_ctrl_new_std(&ctrls->handler, ops,
  934. V4L2_CID_DIGITAL_GAIN,
  935. OV5693_DIGITAL_GAIN_MIN,
  936. OV5693_DIGITAL_GAIN_MAX,
  937. OV5693_DIGITAL_GAIN_STEP,
  938. OV5693_DIGITAL_GAIN_DEF);
  939. /* Flip */
  940. ctrls->hflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
  941. V4L2_CID_HFLIP, 0, 1, 1, 0);
  942. ctrls->vflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
  943. V4L2_CID_VFLIP, 0, 1, 1, 0);
  944. hblank = OV5693_FIXED_PPL - ov5693->mode.format.width;
  945. ctrls->hblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
  946. V4L2_CID_HBLANK, hblank,
  947. hblank, 1, hblank);
  948. if (ctrls->hblank)
  949. ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  950. vblank_max = OV5693_TIMING_MAX_VTS - ov5693->mode.format.height;
  951. vblank_def = ov5693->mode.vts - ov5693->mode.format.height;
  952. ctrls->vblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
  953. V4L2_CID_VBLANK,
  954. OV5693_TIMING_MIN_VTS,
  955. vblank_max, 1, vblank_def);
  956. ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(
  957. &ctrls->handler, ops,
  958. V4L2_CID_TEST_PATTERN,
  959. ARRAY_SIZE(ov5693_test_pattern_menu) - 1,
  960. 0, 0, ov5693_test_pattern_menu);
  961. if (ctrls->handler.error) {
  962. dev_err(ov5693->dev, "Error initialising v4l2 ctrls\n");
  963. ret = ctrls->handler.error;
  964. goto err_free_handler;
  965. }
  966. /* set properties from fwnode (e.g. rotation, orientation) */
  967. ret = v4l2_fwnode_device_parse(ov5693->dev, &props);
  968. if (ret)
  969. goto err_free_handler;
  970. ret = v4l2_ctrl_new_fwnode_properties(&ctrls->handler, ops,
  971. &props);
  972. if (ret)
  973. goto err_free_handler;
  974. /* Use same lock for controls as for everything else. */
  975. ctrls->handler.lock = &ov5693->lock;
  976. ov5693->sd.ctrl_handler = &ctrls->handler;
  977. return 0;
  978. err_free_handler:
  979. v4l2_ctrl_handler_free(&ctrls->handler);
  980. return ret;
  981. }
  982. static int ov5693_configure_gpios(struct ov5693_device *ov5693)
  983. {
  984. ov5693->reset = devm_gpiod_get_optional(ov5693->dev, "reset",
  985. GPIOD_OUT_HIGH);
  986. if (IS_ERR(ov5693->reset)) {
  987. dev_err(ov5693->dev, "Error fetching reset GPIO\n");
  988. return PTR_ERR(ov5693->reset);
  989. }
  990. ov5693->powerdown = devm_gpiod_get_optional(ov5693->dev, "powerdown",
  991. GPIOD_OUT_HIGH);
  992. if (IS_ERR(ov5693->powerdown)) {
  993. dev_err(ov5693->dev, "Error fetching powerdown GPIO\n");
  994. return PTR_ERR(ov5693->powerdown);
  995. }
  996. return 0;
  997. }
  998. static int ov5693_get_regulators(struct ov5693_device *ov5693)
  999. {
  1000. unsigned int i;
  1001. for (i = 0; i < OV5693_NUM_SUPPLIES; i++)
  1002. ov5693->supplies[i].supply = ov5693_supply_names[i];
  1003. return devm_regulator_bulk_get(ov5693->dev, OV5693_NUM_SUPPLIES,
  1004. ov5693->supplies);
  1005. }
  1006. static int ov5693_check_hwcfg(struct ov5693_device *ov5693)
  1007. {
  1008. struct fwnode_handle *fwnode = dev_fwnode(ov5693->dev);
  1009. struct v4l2_fwnode_endpoint bus_cfg = {
  1010. .bus_type = V4L2_MBUS_CSI2_DPHY,
  1011. };
  1012. struct fwnode_handle *endpoint;
  1013. unsigned int i;
  1014. int ret;
  1015. endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1016. if (!endpoint)
  1017. return -EPROBE_DEFER; /* Could be provided by cio2-bridge */
  1018. ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
  1019. fwnode_handle_put(endpoint);
  1020. if (ret)
  1021. return ret;
  1022. if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
  1023. dev_err(ov5693->dev, "only a 2-lane CSI2 config is supported");
  1024. ret = -EINVAL;
  1025. goto out_free_bus_cfg;
  1026. }
  1027. if (!bus_cfg.nr_of_link_frequencies) {
  1028. dev_err(ov5693->dev, "no link frequencies defined\n");
  1029. ret = -EINVAL;
  1030. goto out_free_bus_cfg;
  1031. }
  1032. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  1033. if (bus_cfg.link_frequencies[i] == OV5693_LINK_FREQ_419_2MHZ)
  1034. break;
  1035. if (i == bus_cfg.nr_of_link_frequencies) {
  1036. dev_err(ov5693->dev, "supported link freq %ull not found\n",
  1037. OV5693_LINK_FREQ_419_2MHZ);
  1038. ret = -EINVAL;
  1039. goto out_free_bus_cfg;
  1040. }
  1041. out_free_bus_cfg:
  1042. v4l2_fwnode_endpoint_free(&bus_cfg);
  1043. return ret;
  1044. }
  1045. static int ov5693_probe(struct i2c_client *client)
  1046. {
  1047. struct ov5693_device *ov5693;
  1048. u32 xvclk_rate;
  1049. int ret = 0;
  1050. ov5693 = devm_kzalloc(&client->dev, sizeof(*ov5693), GFP_KERNEL);
  1051. if (!ov5693)
  1052. return -ENOMEM;
  1053. ov5693->dev = &client->dev;
  1054. ov5693->regmap = devm_cci_regmap_init_i2c(client, 16);
  1055. if (IS_ERR(ov5693->regmap))
  1056. return PTR_ERR(ov5693->regmap);
  1057. ret = ov5693_check_hwcfg(ov5693);
  1058. if (ret)
  1059. return ret;
  1060. mutex_init(&ov5693->lock);
  1061. v4l2_i2c_subdev_init(&ov5693->sd, client, &ov5693_ops);
  1062. ov5693->xvclk = devm_clk_get_optional(&client->dev, "xvclk");
  1063. if (IS_ERR(ov5693->xvclk))
  1064. return dev_err_probe(&client->dev, PTR_ERR(ov5693->xvclk),
  1065. "failed to get xvclk: %ld\n",
  1066. PTR_ERR(ov5693->xvclk));
  1067. if (ov5693->xvclk) {
  1068. xvclk_rate = clk_get_rate(ov5693->xvclk);
  1069. } else {
  1070. ret = fwnode_property_read_u32(dev_fwnode(&client->dev),
  1071. "clock-frequency",
  1072. &xvclk_rate);
  1073. if (ret) {
  1074. dev_err(&client->dev, "can't get clock frequency");
  1075. return ret;
  1076. }
  1077. }
  1078. if (xvclk_rate != OV5693_XVCLK_FREQ)
  1079. dev_warn(&client->dev, "Found clk freq %u, expected %u\n",
  1080. xvclk_rate, OV5693_XVCLK_FREQ);
  1081. ret = ov5693_configure_gpios(ov5693);
  1082. if (ret)
  1083. return ret;
  1084. ret = ov5693_get_regulators(ov5693);
  1085. if (ret)
  1086. return dev_err_probe(&client->dev, ret,
  1087. "Error fetching regulators\n");
  1088. ov5693->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1089. ov5693->pad.flags = MEDIA_PAD_FL_SOURCE;
  1090. ov5693->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1091. ov5693->mode.crop = ov5693_default_crop;
  1092. ov5693->mode.format = ov5693_default_fmt;
  1093. ov5693->mode.vts = __ov5693_calc_vts(ov5693->mode.format.height);
  1094. ret = ov5693_init_controls(ov5693);
  1095. if (ret)
  1096. return ret;
  1097. ret = media_entity_pads_init(&ov5693->sd.entity, 1, &ov5693->pad);
  1098. if (ret)
  1099. goto err_ctrl_handler_free;
  1100. /*
  1101. * We need the driver to work in the event that pm runtime is disable in
  1102. * the kernel, so power up and verify the chip now. In the event that
  1103. * runtime pm is disabled this will leave the chip on, so that streaming
  1104. * will work.
  1105. */
  1106. ret = ov5693_sensor_powerup(ov5693);
  1107. if (ret)
  1108. goto err_media_entity_cleanup;
  1109. ret = ov5693_detect(ov5693);
  1110. if (ret)
  1111. goto err_powerdown;
  1112. pm_runtime_set_active(&client->dev);
  1113. pm_runtime_get_noresume(&client->dev);
  1114. pm_runtime_enable(&client->dev);
  1115. ret = v4l2_async_register_subdev_sensor(&ov5693->sd);
  1116. if (ret) {
  1117. dev_err(&client->dev, "failed to register V4L2 subdev: %d",
  1118. ret);
  1119. goto err_pm_runtime;
  1120. }
  1121. pm_runtime_set_autosuspend_delay(&client->dev, 1000);
  1122. pm_runtime_use_autosuspend(&client->dev);
  1123. pm_runtime_put_autosuspend(&client->dev);
  1124. return ret;
  1125. err_pm_runtime:
  1126. pm_runtime_disable(&client->dev);
  1127. pm_runtime_put_noidle(&client->dev);
  1128. err_powerdown:
  1129. ov5693_sensor_powerdown(ov5693);
  1130. err_media_entity_cleanup:
  1131. media_entity_cleanup(&ov5693->sd.entity);
  1132. err_ctrl_handler_free:
  1133. v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
  1134. return ret;
  1135. }
  1136. static void ov5693_remove(struct i2c_client *client)
  1137. {
  1138. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1139. struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
  1140. v4l2_async_unregister_subdev(sd);
  1141. media_entity_cleanup(&ov5693->sd.entity);
  1142. v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
  1143. mutex_destroy(&ov5693->lock);
  1144. /*
  1145. * Disable runtime PM. In case runtime PM is disabled in the kernel,
  1146. * make sure to turn power off manually.
  1147. */
  1148. pm_runtime_disable(&client->dev);
  1149. if (!pm_runtime_status_suspended(&client->dev))
  1150. ov5693_sensor_powerdown(ov5693);
  1151. pm_runtime_set_suspended(&client->dev);
  1152. }
  1153. static const struct dev_pm_ops ov5693_pm_ops = {
  1154. SET_RUNTIME_PM_OPS(ov5693_sensor_suspend, ov5693_sensor_resume, NULL)
  1155. };
  1156. static const struct acpi_device_id ov5693_acpi_match[] = {
  1157. {"INT33BE"},
  1158. {},
  1159. };
  1160. MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
  1161. static const struct of_device_id ov5693_of_match[] = {
  1162. { .compatible = "ovti,ov5693", },
  1163. { /* sentinel */ },
  1164. };
  1165. MODULE_DEVICE_TABLE(of, ov5693_of_match);
  1166. static struct i2c_driver ov5693_driver = {
  1167. .driver = {
  1168. .name = "ov5693",
  1169. .acpi_match_table = ov5693_acpi_match,
  1170. .of_match_table = ov5693_of_match,
  1171. .pm = &ov5693_pm_ops,
  1172. },
  1173. .probe = ov5693_probe,
  1174. .remove = ov5693_remove,
  1175. };
  1176. module_i2c_driver(ov5693_driver);
  1177. MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
  1178. MODULE_LICENSE("GPL");