ov5675.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2019 Intel Corporation.
  3. #include <linux/acpi.h>
  4. #include <linux/clk.h>
  5. #include <linux/delay.h>
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/i2c.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/unaligned.h>
  13. #include <media/v4l2-ctrls.h>
  14. #include <media/v4l2-device.h>
  15. #include <media/v4l2-fwnode.h>
  16. #define OV5675_REG_VALUE_08BIT 1
  17. #define OV5675_REG_VALUE_16BIT 2
  18. #define OV5675_REG_VALUE_24BIT 3
  19. #define OV5675_LINK_FREQ_450MHZ 450000000ULL
  20. #define OV5675_SCLK 90000000LL
  21. #define OV5675_XVCLK_19_2 19200000
  22. #define OV5675_DATA_LANES 2
  23. #define OV5675_RGB_DEPTH 10
  24. #define OV5675_REG_CHIP_ID 0x300a
  25. #define OV5675_CHIP_ID 0x5675
  26. #define OV5675_REG_MODE_SELECT 0x0100
  27. #define OV5675_MODE_STANDBY 0x00
  28. #define OV5675_MODE_STREAMING 0x01
  29. /* vertical-timings from sensor */
  30. #define OV5675_REG_VTS 0x380e
  31. #define OV5675_VTS_30FPS 0x07e4
  32. #define OV5675_VTS_30FPS_MIN 0x07e4
  33. #define OV5675_VTS_MAX 0x7fff
  34. /* horizontal-timings from sensor */
  35. #define OV5675_REG_HTS 0x380c
  36. /* Exposure controls from sensor */
  37. #define OV5675_REG_EXPOSURE 0x3500
  38. #define OV5675_EXPOSURE_MIN 4
  39. #define OV5675_EXPOSURE_MAX_MARGIN 4
  40. #define OV5675_EXPOSURE_STEP 1
  41. /* Analog gain controls from sensor */
  42. #define OV5675_REG_ANALOG_GAIN 0x3508
  43. #define OV5675_ANAL_GAIN_MIN 128
  44. #define OV5675_ANAL_GAIN_MAX 2047
  45. #define OV5675_ANAL_GAIN_STEP 1
  46. /* Digital gain controls from sensor */
  47. #define OV5675_REG_DIGITAL_GAIN 0x350a
  48. #define OV5675_REG_MWB_R_GAIN 0x5019
  49. #define OV5675_REG_MWB_G_GAIN 0x501b
  50. #define OV5675_REG_MWB_B_GAIN 0x501d
  51. #define OV5675_DGTL_GAIN_MIN 1024
  52. #define OV5675_DGTL_GAIN_MAX 4095
  53. #define OV5675_DGTL_GAIN_STEP 1
  54. #define OV5675_DGTL_GAIN_DEFAULT 1024
  55. /* Group Access */
  56. #define OV5675_REG_GROUP_ACCESS 0x3208
  57. #define OV5675_GROUP_HOLD_START 0x0
  58. #define OV5675_GROUP_HOLD_END 0x10
  59. #define OV5675_GROUP_HOLD_LAUNCH 0xa0
  60. /* Test Pattern Control */
  61. #define OV5675_REG_TEST_PATTERN 0x4503
  62. #define OV5675_TEST_PATTERN_ENABLE BIT(7)
  63. #define OV5675_TEST_PATTERN_BAR_SHIFT 2
  64. /* Flip Mirror Controls from sensor */
  65. #define OV5675_REG_FORMAT1 0x3820
  66. #define OV5675_REG_FORMAT2 0x373d
  67. #define to_ov5675(_sd) container_of(_sd, struct ov5675, sd)
  68. static const char * const ov5675_supply_names[] = {
  69. "avdd", /* Analog power */
  70. "dovdd", /* Digital I/O power */
  71. "dvdd", /* Digital core power */
  72. };
  73. #define OV5675_NUM_SUPPLIES ARRAY_SIZE(ov5675_supply_names)
  74. enum {
  75. OV5675_LINK_FREQ_900MBPS,
  76. };
  77. struct ov5675_reg {
  78. u16 address;
  79. u8 val;
  80. };
  81. struct ov5675_reg_list {
  82. u32 num_of_regs;
  83. const struct ov5675_reg *regs;
  84. };
  85. struct ov5675_link_freq_config {
  86. const struct ov5675_reg_list reg_list;
  87. };
  88. struct ov5675_mode {
  89. /* Frame width in pixels */
  90. u32 width;
  91. /* Frame height in pixels */
  92. u32 height;
  93. /* Horizontal timining size */
  94. u32 hts;
  95. /* Default vertical timining size */
  96. u32 vts_def;
  97. /* Min vertical timining size */
  98. u32 vts_min;
  99. /* Link frequency needed for this resolution */
  100. u32 link_freq_index;
  101. /* Sensor register settings for this resolution */
  102. const struct ov5675_reg_list reg_list;
  103. };
  104. static const struct ov5675_reg mipi_data_rate_900mbps[] = {
  105. {0x0103, 0x01},
  106. {0x0100, 0x00},
  107. {0x0300, 0x04},
  108. {0x0302, 0x8d},
  109. {0x0303, 0x00},
  110. {0x030d, 0x26},
  111. };
  112. static const struct ov5675_reg mode_2592x1944_regs[] = {
  113. {0x3002, 0x21},
  114. {0x3107, 0x23},
  115. {0x3501, 0x20},
  116. {0x3503, 0x0c},
  117. {0x3508, 0x03},
  118. {0x3509, 0x00},
  119. {0x3600, 0x66},
  120. {0x3602, 0x30},
  121. {0x3610, 0xa5},
  122. {0x3612, 0x93},
  123. {0x3620, 0x80},
  124. {0x3642, 0x0e},
  125. {0x3661, 0x00},
  126. {0x3662, 0x10},
  127. {0x3664, 0xf3},
  128. {0x3665, 0x9e},
  129. {0x3667, 0xa5},
  130. {0x366e, 0x55},
  131. {0x366f, 0x55},
  132. {0x3670, 0x11},
  133. {0x3671, 0x11},
  134. {0x3672, 0x11},
  135. {0x3673, 0x11},
  136. {0x3714, 0x24},
  137. {0x371a, 0x3e},
  138. {0x3733, 0x10},
  139. {0x3734, 0x00},
  140. {0x373d, 0x24},
  141. {0x3764, 0x20},
  142. {0x3765, 0x20},
  143. {0x3766, 0x12},
  144. {0x37a1, 0x14},
  145. {0x37a8, 0x1c},
  146. {0x37ab, 0x0f},
  147. {0x37c2, 0x04},
  148. {0x37cb, 0x00},
  149. {0x37cc, 0x00},
  150. {0x37cd, 0x00},
  151. {0x37ce, 0x00},
  152. {0x37d8, 0x02},
  153. {0x37d9, 0x08},
  154. {0x37dc, 0x04},
  155. {0x3800, 0x00},
  156. {0x3801, 0x00},
  157. {0x3802, 0x00},
  158. {0x3803, 0x04},
  159. {0x3804, 0x0a},
  160. {0x3805, 0x3f},
  161. {0x3806, 0x07},
  162. {0x3807, 0xb3},
  163. {0x3808, 0x0a},
  164. {0x3809, 0x20},
  165. {0x380a, 0x07},
  166. {0x380b, 0x98},
  167. {0x380c, 0x02},
  168. {0x380d, 0xee},
  169. {0x380e, 0x07},
  170. {0x380f, 0xe4},
  171. {0x3811, 0x10},
  172. {0x3813, 0x0d},
  173. {0x3814, 0x01},
  174. {0x3815, 0x01},
  175. {0x3816, 0x01},
  176. {0x3817, 0x01},
  177. {0x381e, 0x02},
  178. {0x3820, 0x88},
  179. {0x3821, 0x01},
  180. {0x3832, 0x04},
  181. {0x3c80, 0x01},
  182. {0x3c82, 0x00},
  183. {0x3c83, 0xc8},
  184. {0x3c8c, 0x0f},
  185. {0x3c8d, 0xa0},
  186. {0x3c90, 0x07},
  187. {0x3c91, 0x00},
  188. {0x3c92, 0x00},
  189. {0x3c93, 0x00},
  190. {0x3c94, 0xd0},
  191. {0x3c95, 0x50},
  192. {0x3c96, 0x35},
  193. {0x3c97, 0x00},
  194. {0x4001, 0xe0},
  195. {0x4008, 0x02},
  196. {0x4009, 0x0d},
  197. {0x400f, 0x80},
  198. {0x4013, 0x02},
  199. {0x4040, 0x00},
  200. {0x4041, 0x07},
  201. {0x404c, 0x50},
  202. {0x404e, 0x20},
  203. {0x4500, 0x06},
  204. {0x4503, 0x00},
  205. {0x450a, 0x04},
  206. {0x4809, 0x04},
  207. {0x480c, 0x12},
  208. {0x4819, 0x70},
  209. {0x4825, 0x32},
  210. {0x4826, 0x32},
  211. {0x482a, 0x06},
  212. {0x4833, 0x08},
  213. {0x4837, 0x0d},
  214. {0x5000, 0x77},
  215. {0x5b00, 0x01},
  216. {0x5b01, 0x10},
  217. {0x5b02, 0x01},
  218. {0x5b03, 0xdb},
  219. {0x5b05, 0x6c},
  220. {0x5e10, 0xfc},
  221. {0x3500, 0x00},
  222. {0x3501, 0x3E},
  223. {0x3502, 0x60},
  224. {0x3503, 0x08},
  225. {0x3508, 0x04},
  226. {0x3509, 0x00},
  227. {0x3832, 0x48},
  228. {0x5780, 0x3e},
  229. {0x5781, 0x0f},
  230. {0x5782, 0x44},
  231. {0x5783, 0x02},
  232. {0x5784, 0x01},
  233. {0x5785, 0x01},
  234. {0x5786, 0x00},
  235. {0x5787, 0x04},
  236. {0x5788, 0x02},
  237. {0x5789, 0x0f},
  238. {0x578a, 0xfd},
  239. {0x578b, 0xf5},
  240. {0x578c, 0xf5},
  241. {0x578d, 0x03},
  242. {0x578e, 0x08},
  243. {0x578f, 0x0c},
  244. {0x5790, 0x08},
  245. {0x5791, 0x06},
  246. {0x5792, 0x00},
  247. {0x5793, 0x52},
  248. {0x5794, 0xa3},
  249. {0x4003, 0x40},
  250. {0x3107, 0x01},
  251. {0x3c80, 0x08},
  252. {0x3c83, 0xb1},
  253. {0x3c8c, 0x10},
  254. {0x3c8d, 0x00},
  255. {0x3c90, 0x00},
  256. {0x3c94, 0x00},
  257. {0x3c95, 0x00},
  258. {0x3c96, 0x00},
  259. {0x37cb, 0x09},
  260. {0x37cc, 0x15},
  261. {0x37cd, 0x1f},
  262. {0x37ce, 0x1f},
  263. };
  264. static const struct ov5675_reg mode_1296x972_regs[] = {
  265. {0x3002, 0x21},
  266. {0x3107, 0x23},
  267. {0x3501, 0x20},
  268. {0x3503, 0x0c},
  269. {0x3508, 0x03},
  270. {0x3509, 0x00},
  271. {0x3600, 0x66},
  272. {0x3602, 0x30},
  273. {0x3610, 0xa5},
  274. {0x3612, 0x93},
  275. {0x3620, 0x80},
  276. {0x3642, 0x0e},
  277. {0x3661, 0x00},
  278. {0x3662, 0x08},
  279. {0x3664, 0xf3},
  280. {0x3665, 0x9e},
  281. {0x3667, 0xa5},
  282. {0x366e, 0x55},
  283. {0x366f, 0x55},
  284. {0x3670, 0x11},
  285. {0x3671, 0x11},
  286. {0x3672, 0x11},
  287. {0x3673, 0x11},
  288. {0x3714, 0x28},
  289. {0x371a, 0x3e},
  290. {0x3733, 0x10},
  291. {0x3734, 0x00},
  292. {0x373d, 0x24},
  293. {0x3764, 0x20},
  294. {0x3765, 0x20},
  295. {0x3766, 0x12},
  296. {0x37a1, 0x14},
  297. {0x37a8, 0x1c},
  298. {0x37ab, 0x0f},
  299. {0x37c2, 0x14},
  300. {0x37cb, 0x00},
  301. {0x37cc, 0x00},
  302. {0x37cd, 0x00},
  303. {0x37ce, 0x00},
  304. {0x37d8, 0x02},
  305. {0x37d9, 0x04},
  306. {0x37dc, 0x04},
  307. {0x3800, 0x00},
  308. {0x3801, 0x00},
  309. {0x3802, 0x00},
  310. {0x3803, 0x00},
  311. {0x3804, 0x0a},
  312. {0x3805, 0x3f},
  313. {0x3806, 0x07},
  314. {0x3807, 0xb7},
  315. {0x3808, 0x05},
  316. {0x3809, 0x10},
  317. {0x380a, 0x03},
  318. {0x380b, 0xcc},
  319. {0x380c, 0x02},
  320. {0x380d, 0xee},
  321. {0x380e, 0x07},
  322. {0x380f, 0xd0},
  323. {0x3811, 0x08},
  324. {0x3813, 0x0d},
  325. {0x3814, 0x03},
  326. {0x3815, 0x01},
  327. {0x3816, 0x03},
  328. {0x3817, 0x01},
  329. {0x381e, 0x02},
  330. {0x3820, 0x8b},
  331. {0x3821, 0x01},
  332. {0x3832, 0x04},
  333. {0x3c80, 0x01},
  334. {0x3c82, 0x00},
  335. {0x3c83, 0xc8},
  336. {0x3c8c, 0x0f},
  337. {0x3c8d, 0xa0},
  338. {0x3c90, 0x07},
  339. {0x3c91, 0x00},
  340. {0x3c92, 0x00},
  341. {0x3c93, 0x00},
  342. {0x3c94, 0xd0},
  343. {0x3c95, 0x50},
  344. {0x3c96, 0x35},
  345. {0x3c97, 0x00},
  346. {0x4001, 0xe0},
  347. {0x4008, 0x00},
  348. {0x4009, 0x07},
  349. {0x400f, 0x80},
  350. {0x4013, 0x02},
  351. {0x4040, 0x00},
  352. {0x4041, 0x03},
  353. {0x404c, 0x50},
  354. {0x404e, 0x20},
  355. {0x4500, 0x06},
  356. {0x4503, 0x00},
  357. {0x450a, 0x04},
  358. {0x4809, 0x04},
  359. {0x480c, 0x12},
  360. {0x4819, 0x70},
  361. {0x4825, 0x32},
  362. {0x4826, 0x32},
  363. {0x482a, 0x06},
  364. {0x4833, 0x08},
  365. {0x4837, 0x0d},
  366. {0x5000, 0x77},
  367. {0x5b00, 0x01},
  368. {0x5b01, 0x10},
  369. {0x5b02, 0x01},
  370. {0x5b03, 0xdb},
  371. {0x5b05, 0x6c},
  372. {0x5e10, 0xfc},
  373. {0x3500, 0x00},
  374. {0x3501, 0x1F},
  375. {0x3502, 0x20},
  376. {0x3503, 0x08},
  377. {0x3508, 0x04},
  378. {0x3509, 0x00},
  379. {0x3832, 0x48},
  380. {0x5780, 0x3e},
  381. {0x5781, 0x0f},
  382. {0x5782, 0x44},
  383. {0x5783, 0x02},
  384. {0x5784, 0x01},
  385. {0x5785, 0x01},
  386. {0x5786, 0x00},
  387. {0x5787, 0x04},
  388. {0x5788, 0x02},
  389. {0x5789, 0x0f},
  390. {0x578a, 0xfd},
  391. {0x578b, 0xf5},
  392. {0x578c, 0xf5},
  393. {0x578d, 0x03},
  394. {0x578e, 0x08},
  395. {0x578f, 0x0c},
  396. {0x5790, 0x08},
  397. {0x5791, 0x06},
  398. {0x5792, 0x00},
  399. {0x5793, 0x52},
  400. {0x5794, 0xa3},
  401. {0x4003, 0x40},
  402. {0x3107, 0x01},
  403. {0x3c80, 0x08},
  404. {0x3c83, 0xb1},
  405. {0x3c8c, 0x10},
  406. {0x3c8d, 0x00},
  407. {0x3c90, 0x00},
  408. {0x3c94, 0x00},
  409. {0x3c95, 0x00},
  410. {0x3c96, 0x00},
  411. {0x37cb, 0x09},
  412. {0x37cc, 0x15},
  413. {0x37cd, 0x1f},
  414. {0x37ce, 0x1f},
  415. };
  416. static const char * const ov5675_test_pattern_menu[] = {
  417. "Disabled",
  418. "Standard Color Bar",
  419. "Top-Bottom Darker Color Bar",
  420. "Right-Left Darker Color Bar",
  421. "Bottom-Top Darker Color Bar"
  422. };
  423. static const s64 link_freq_menu_items[] = {
  424. OV5675_LINK_FREQ_450MHZ,
  425. };
  426. static const struct ov5675_link_freq_config link_freq_configs[] = {
  427. [OV5675_LINK_FREQ_900MBPS] = {
  428. .reg_list = {
  429. .num_of_regs = ARRAY_SIZE(mipi_data_rate_900mbps),
  430. .regs = mipi_data_rate_900mbps,
  431. }
  432. }
  433. };
  434. static const struct ov5675_mode supported_modes[] = {
  435. {
  436. .width = 2592,
  437. .height = 1944,
  438. .hts = 1500,
  439. .vts_def = OV5675_VTS_30FPS,
  440. .vts_min = OV5675_VTS_30FPS_MIN,
  441. .reg_list = {
  442. .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
  443. .regs = mode_2592x1944_regs,
  444. },
  445. .link_freq_index = OV5675_LINK_FREQ_900MBPS,
  446. },
  447. {
  448. .width = 1296,
  449. .height = 972,
  450. .hts = 1500,
  451. .vts_def = OV5675_VTS_30FPS,
  452. .vts_min = OV5675_VTS_30FPS_MIN,
  453. .reg_list = {
  454. .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
  455. .regs = mode_1296x972_regs,
  456. },
  457. .link_freq_index = OV5675_LINK_FREQ_900MBPS,
  458. }
  459. };
  460. struct ov5675 {
  461. struct device *dev;
  462. struct v4l2_subdev sd;
  463. struct media_pad pad;
  464. struct v4l2_ctrl_handler ctrl_handler;
  465. struct clk *xvclk;
  466. struct gpio_desc *reset_gpio;
  467. struct regulator_bulk_data supplies[OV5675_NUM_SUPPLIES];
  468. /* V4L2 Controls */
  469. struct v4l2_ctrl *link_freq;
  470. struct v4l2_ctrl *pixel_rate;
  471. struct v4l2_ctrl *vblank;
  472. struct v4l2_ctrl *hblank;
  473. struct v4l2_ctrl *exposure;
  474. /* Current mode */
  475. const struct ov5675_mode *cur_mode;
  476. /* To serialize asynchronous callbacks */
  477. struct mutex mutex;
  478. /* True if the device has been identified */
  479. bool identified;
  480. };
  481. static u64 to_pixel_rate(u32 f_index)
  482. {
  483. u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV5675_DATA_LANES;
  484. do_div(pixel_rate, OV5675_RGB_DEPTH);
  485. return pixel_rate;
  486. }
  487. static u64 to_pixels_per_line(u32 hts, u32 f_index)
  488. {
  489. u64 ppl = hts * to_pixel_rate(f_index);
  490. do_div(ppl, OV5675_SCLK);
  491. return ppl;
  492. }
  493. static int ov5675_read_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 *val)
  494. {
  495. struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
  496. struct i2c_msg msgs[2];
  497. u8 addr_buf[2];
  498. u8 data_buf[4] = {0};
  499. int ret;
  500. if (len > 4)
  501. return -EINVAL;
  502. put_unaligned_be16(reg, addr_buf);
  503. msgs[0].addr = client->addr;
  504. msgs[0].flags = 0;
  505. msgs[0].len = sizeof(addr_buf);
  506. msgs[0].buf = addr_buf;
  507. msgs[1].addr = client->addr;
  508. msgs[1].flags = I2C_M_RD;
  509. msgs[1].len = len;
  510. msgs[1].buf = &data_buf[4 - len];
  511. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  512. if (ret != ARRAY_SIZE(msgs))
  513. return -EIO;
  514. *val = get_unaligned_be32(data_buf);
  515. return 0;
  516. }
  517. static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val)
  518. {
  519. struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
  520. u8 buf[6];
  521. if (len > 4)
  522. return -EINVAL;
  523. put_unaligned_be16(reg, buf);
  524. put_unaligned_be32(val << 8 * (4 - len), buf + 2);
  525. if (i2c_master_send(client, buf, len + 2) != len + 2)
  526. return -EIO;
  527. return 0;
  528. }
  529. static int ov5675_write_reg_list(struct ov5675 *ov5675,
  530. const struct ov5675_reg_list *r_list)
  531. {
  532. unsigned int i;
  533. int ret;
  534. for (i = 0; i < r_list->num_of_regs; i++) {
  535. ret = ov5675_write_reg(ov5675, r_list->regs[i].address, 1,
  536. r_list->regs[i].val);
  537. if (ret) {
  538. dev_err_ratelimited(ov5675->dev,
  539. "failed to write reg 0x%4.4x. error = %d",
  540. r_list->regs[i].address, ret);
  541. return ret;
  542. }
  543. }
  544. return 0;
  545. }
  546. static int ov5675_update_digital_gain(struct ov5675 *ov5675, u32 d_gain)
  547. {
  548. int ret;
  549. ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
  550. OV5675_REG_VALUE_08BIT,
  551. OV5675_GROUP_HOLD_START);
  552. if (ret)
  553. return ret;
  554. ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_R_GAIN,
  555. OV5675_REG_VALUE_16BIT, d_gain);
  556. if (ret)
  557. return ret;
  558. ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_G_GAIN,
  559. OV5675_REG_VALUE_16BIT, d_gain);
  560. if (ret)
  561. return ret;
  562. ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_B_GAIN,
  563. OV5675_REG_VALUE_16BIT, d_gain);
  564. if (ret)
  565. return ret;
  566. ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
  567. OV5675_REG_VALUE_08BIT,
  568. OV5675_GROUP_HOLD_END);
  569. if (ret)
  570. return ret;
  571. ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
  572. OV5675_REG_VALUE_08BIT,
  573. OV5675_GROUP_HOLD_LAUNCH);
  574. return ret;
  575. }
  576. static int ov5675_test_pattern(struct ov5675 *ov5675, u32 pattern)
  577. {
  578. if (pattern)
  579. pattern = (pattern - 1) << OV5675_TEST_PATTERN_BAR_SHIFT |
  580. OV5675_TEST_PATTERN_ENABLE;
  581. return ov5675_write_reg(ov5675, OV5675_REG_TEST_PATTERN,
  582. OV5675_REG_VALUE_08BIT, pattern);
  583. }
  584. /*
  585. * OV5675 supports keeping the pixel order by mirror and flip function
  586. * The Bayer order isn't affected by the flip controls
  587. */
  588. static int ov5675_set_ctrl_hflip(struct ov5675 *ov5675, u32 ctrl_val)
  589. {
  590. int ret;
  591. u32 val;
  592. ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
  593. OV5675_REG_VALUE_08BIT, &val);
  594. if (ret)
  595. return ret;
  596. return ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
  597. OV5675_REG_VALUE_08BIT,
  598. ctrl_val ? val & ~BIT(3) : val | BIT(3));
  599. }
  600. static int ov5675_set_ctrl_vflip(struct ov5675 *ov5675, u8 ctrl_val)
  601. {
  602. int ret;
  603. u32 val;
  604. ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
  605. OV5675_REG_VALUE_08BIT, &val);
  606. if (ret)
  607. return ret;
  608. ret = ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
  609. OV5675_REG_VALUE_08BIT,
  610. ctrl_val ? val | BIT(4) | BIT(5) : val & ~BIT(4) & ~BIT(5));
  611. if (ret)
  612. return ret;
  613. ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT2,
  614. OV5675_REG_VALUE_08BIT, &val);
  615. if (ret)
  616. return ret;
  617. return ov5675_write_reg(ov5675, OV5675_REG_FORMAT2,
  618. OV5675_REG_VALUE_08BIT,
  619. ctrl_val ? val | BIT(1) : val & ~BIT(1));
  620. }
  621. static int ov5675_set_ctrl(struct v4l2_ctrl *ctrl)
  622. {
  623. struct ov5675 *ov5675 = container_of(ctrl->handler,
  624. struct ov5675, ctrl_handler);
  625. s64 exposure_max;
  626. int ret = 0;
  627. /* Propagate change of current control to all related controls */
  628. if (ctrl->id == V4L2_CID_VBLANK) {
  629. /* Update max exposure while meeting expected vblanking */
  630. exposure_max = ov5675->cur_mode->height + ctrl->val -
  631. OV5675_EXPOSURE_MAX_MARGIN;
  632. __v4l2_ctrl_modify_range(ov5675->exposure,
  633. ov5675->exposure->minimum,
  634. exposure_max, ov5675->exposure->step,
  635. exposure_max);
  636. }
  637. /* V4L2 controls values will be applied only when power is already up */
  638. if (!pm_runtime_get_if_in_use(ov5675->dev))
  639. return 0;
  640. switch (ctrl->id) {
  641. case V4L2_CID_ANALOGUE_GAIN:
  642. ret = ov5675_write_reg(ov5675, OV5675_REG_ANALOG_GAIN,
  643. OV5675_REG_VALUE_16BIT, ctrl->val);
  644. break;
  645. case V4L2_CID_DIGITAL_GAIN:
  646. ret = ov5675_update_digital_gain(ov5675, ctrl->val);
  647. break;
  648. case V4L2_CID_EXPOSURE:
  649. /* 4 least significant bits of expsoure are fractional part
  650. * val = val << 4
  651. * for ov5675, the unit of exposure is different from other
  652. * OmniVision sensors, its exposure value is twice of the
  653. * register value, the exposure should be divided by 2 before
  654. * set register, e.g. val << 3.
  655. */
  656. ret = ov5675_write_reg(ov5675, OV5675_REG_EXPOSURE,
  657. OV5675_REG_VALUE_24BIT, ctrl->val << 3);
  658. break;
  659. case V4L2_CID_VBLANK:
  660. ret = ov5675_write_reg(ov5675, OV5675_REG_VTS,
  661. OV5675_REG_VALUE_16BIT,
  662. ov5675->cur_mode->height + ctrl->val +
  663. 10);
  664. break;
  665. case V4L2_CID_TEST_PATTERN:
  666. ret = ov5675_test_pattern(ov5675, ctrl->val);
  667. break;
  668. case V4L2_CID_HFLIP:
  669. ov5675_set_ctrl_hflip(ov5675, ctrl->val);
  670. break;
  671. case V4L2_CID_VFLIP:
  672. ov5675_set_ctrl_vflip(ov5675, ctrl->val);
  673. break;
  674. default:
  675. ret = -EINVAL;
  676. break;
  677. }
  678. pm_runtime_put(ov5675->dev);
  679. return ret;
  680. }
  681. static const struct v4l2_ctrl_ops ov5675_ctrl_ops = {
  682. .s_ctrl = ov5675_set_ctrl,
  683. };
  684. static int ov5675_init_controls(struct ov5675 *ov5675)
  685. {
  686. struct v4l2_fwnode_device_properties props;
  687. struct v4l2_ctrl_handler *ctrl_hdlr;
  688. s64 exposure_max, h_blank;
  689. int ret;
  690. ctrl_hdlr = &ov5675->ctrl_handler;
  691. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
  692. if (ret)
  693. return ret;
  694. ctrl_hdlr->lock = &ov5675->mutex;
  695. ov5675->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov5675_ctrl_ops,
  696. V4L2_CID_LINK_FREQ,
  697. ARRAY_SIZE(link_freq_menu_items) - 1,
  698. 0, link_freq_menu_items);
  699. if (ov5675->link_freq)
  700. ov5675->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  701. ov5675->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
  702. V4L2_CID_PIXEL_RATE, 0,
  703. to_pixel_rate(OV5675_LINK_FREQ_900MBPS),
  704. 1,
  705. to_pixel_rate(OV5675_LINK_FREQ_900MBPS));
  706. ov5675->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
  707. V4L2_CID_VBLANK,
  708. ov5675->cur_mode->vts_min - ov5675->cur_mode->height,
  709. OV5675_VTS_MAX - ov5675->cur_mode->height, 1,
  710. ov5675->cur_mode->vts_def - ov5675->cur_mode->height);
  711. h_blank = to_pixels_per_line(ov5675->cur_mode->hts,
  712. ov5675->cur_mode->link_freq_index) - ov5675->cur_mode->width;
  713. ov5675->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
  714. V4L2_CID_HBLANK, h_blank, h_blank, 1,
  715. h_blank);
  716. if (ov5675->hblank)
  717. ov5675->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  718. v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  719. OV5675_ANAL_GAIN_MIN, OV5675_ANAL_GAIN_MAX,
  720. OV5675_ANAL_GAIN_STEP, OV5675_ANAL_GAIN_MIN);
  721. v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  722. OV5675_DGTL_GAIN_MIN, OV5675_DGTL_GAIN_MAX,
  723. OV5675_DGTL_GAIN_STEP, OV5675_DGTL_GAIN_DEFAULT);
  724. exposure_max = (ov5675->cur_mode->vts_def - OV5675_EXPOSURE_MAX_MARGIN);
  725. ov5675->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
  726. V4L2_CID_EXPOSURE,
  727. OV5675_EXPOSURE_MIN, exposure_max,
  728. OV5675_EXPOSURE_STEP,
  729. exposure_max);
  730. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5675_ctrl_ops,
  731. V4L2_CID_TEST_PATTERN,
  732. ARRAY_SIZE(ov5675_test_pattern_menu) - 1,
  733. 0, 0, ov5675_test_pattern_menu);
  734. v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
  735. V4L2_CID_HFLIP, 0, 1, 1, 0);
  736. v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
  737. V4L2_CID_VFLIP, 0, 1, 1, 0);
  738. if (ctrl_hdlr->error) {
  739. v4l2_ctrl_handler_free(ctrl_hdlr);
  740. return ctrl_hdlr->error;
  741. }
  742. ret = v4l2_fwnode_device_parse(ov5675->dev, &props);
  743. if (ret)
  744. goto error;
  745. ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov5675_ctrl_ops,
  746. &props);
  747. if (ret)
  748. goto error;
  749. ov5675->sd.ctrl_handler = ctrl_hdlr;
  750. return 0;
  751. error:
  752. v4l2_ctrl_handler_free(ctrl_hdlr);
  753. return ret;
  754. }
  755. static void ov5675_update_pad_format(const struct ov5675_mode *mode,
  756. struct v4l2_mbus_framefmt *fmt)
  757. {
  758. fmt->width = mode->width;
  759. fmt->height = mode->height;
  760. fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  761. fmt->field = V4L2_FIELD_NONE;
  762. }
  763. static int ov5675_identify_module(struct ov5675 *ov5675)
  764. {
  765. int ret;
  766. u32 val;
  767. if (ov5675->identified)
  768. return 0;
  769. ret = ov5675_read_reg(ov5675, OV5675_REG_CHIP_ID,
  770. OV5675_REG_VALUE_24BIT, &val);
  771. if (ret)
  772. return ret;
  773. if (val != OV5675_CHIP_ID) {
  774. dev_err(ov5675->dev, "chip id mismatch: %x!=%x",
  775. OV5675_CHIP_ID, val);
  776. return -ENXIO;
  777. }
  778. ov5675->identified = true;
  779. return 0;
  780. }
  781. static int ov5675_start_streaming(struct ov5675 *ov5675)
  782. {
  783. const struct ov5675_reg_list *reg_list;
  784. int link_freq_index, ret;
  785. ret = ov5675_identify_module(ov5675);
  786. if (ret)
  787. return ret;
  788. link_freq_index = ov5675->cur_mode->link_freq_index;
  789. reg_list = &link_freq_configs[link_freq_index].reg_list;
  790. ret = ov5675_write_reg_list(ov5675, reg_list);
  791. if (ret) {
  792. dev_err(ov5675->dev, "failed to set plls");
  793. return ret;
  794. }
  795. reg_list = &ov5675->cur_mode->reg_list;
  796. ret = ov5675_write_reg_list(ov5675, reg_list);
  797. if (ret) {
  798. dev_err(ov5675->dev, "failed to set mode");
  799. return ret;
  800. }
  801. ret = __v4l2_ctrl_handler_setup(ov5675->sd.ctrl_handler);
  802. if (ret)
  803. return ret;
  804. ret = ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
  805. OV5675_REG_VALUE_08BIT, OV5675_MODE_STREAMING);
  806. if (ret) {
  807. dev_err(ov5675->dev, "failed to set stream");
  808. return ret;
  809. }
  810. return 0;
  811. }
  812. static void ov5675_stop_streaming(struct ov5675 *ov5675)
  813. {
  814. if (ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
  815. OV5675_REG_VALUE_08BIT, OV5675_MODE_STANDBY))
  816. dev_err(ov5675->dev, "failed to set stream");
  817. }
  818. static int ov5675_set_stream(struct v4l2_subdev *sd, int enable)
  819. {
  820. struct ov5675 *ov5675 = to_ov5675(sd);
  821. int ret = 0;
  822. mutex_lock(&ov5675->mutex);
  823. if (enable) {
  824. ret = pm_runtime_resume_and_get(ov5675->dev);
  825. if (ret < 0) {
  826. mutex_unlock(&ov5675->mutex);
  827. return ret;
  828. }
  829. ret = ov5675_start_streaming(ov5675);
  830. if (ret) {
  831. enable = 0;
  832. ov5675_stop_streaming(ov5675);
  833. pm_runtime_put(ov5675->dev);
  834. }
  835. } else {
  836. ov5675_stop_streaming(ov5675);
  837. pm_runtime_put(ov5675->dev);
  838. }
  839. mutex_unlock(&ov5675->mutex);
  840. return ret;
  841. }
  842. static int ov5675_power_off(struct device *dev)
  843. {
  844. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  845. struct ov5675 *ov5675 = to_ov5675(sd);
  846. usleep_range(90, 100);
  847. clk_disable_unprepare(ov5675->xvclk);
  848. gpiod_set_value_cansleep(ov5675->reset_gpio, 1);
  849. regulator_bulk_disable(OV5675_NUM_SUPPLIES, ov5675->supplies);
  850. return 0;
  851. }
  852. static int ov5675_power_on(struct device *dev)
  853. {
  854. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  855. struct ov5675 *ov5675 = to_ov5675(sd);
  856. int ret;
  857. ret = clk_prepare_enable(ov5675->xvclk);
  858. if (ret < 0) {
  859. dev_err(dev, "failed to enable xvclk: %d\n", ret);
  860. return ret;
  861. }
  862. gpiod_set_value_cansleep(ov5675->reset_gpio, 1);
  863. ret = regulator_bulk_enable(OV5675_NUM_SUPPLIES, ov5675->supplies);
  864. if (ret) {
  865. clk_disable_unprepare(ov5675->xvclk);
  866. return ret;
  867. }
  868. /* Reset pulse should be at least 2ms and reset gpio released only once
  869. * regulators are stable.
  870. */
  871. usleep_range(2000, 2200);
  872. gpiod_set_value_cansleep(ov5675->reset_gpio, 0);
  873. /* Worst case quiesence gap is 1.365 milliseconds @ 6MHz XVCLK
  874. * Add an additional threshold grace period to ensure reset
  875. * completion before initiating our first I2C transaction.
  876. */
  877. usleep_range(1500, 1600);
  878. return 0;
  879. }
  880. static int ov5675_set_format(struct v4l2_subdev *sd,
  881. struct v4l2_subdev_state *sd_state,
  882. struct v4l2_subdev_format *fmt)
  883. {
  884. struct ov5675 *ov5675 = to_ov5675(sd);
  885. const struct ov5675_mode *mode;
  886. s32 vblank_def, h_blank;
  887. mode = v4l2_find_nearest_size(supported_modes,
  888. ARRAY_SIZE(supported_modes), width,
  889. height, fmt->format.width,
  890. fmt->format.height);
  891. mutex_lock(&ov5675->mutex);
  892. ov5675_update_pad_format(mode, &fmt->format);
  893. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  894. *v4l2_subdev_state_get_format(sd_state, fmt->pad) = fmt->format;
  895. } else {
  896. ov5675->cur_mode = mode;
  897. __v4l2_ctrl_s_ctrl(ov5675->link_freq, mode->link_freq_index);
  898. __v4l2_ctrl_s_ctrl_int64(ov5675->pixel_rate,
  899. to_pixel_rate(mode->link_freq_index));
  900. /* Update limits and set FPS to default */
  901. vblank_def = mode->vts_def - mode->height;
  902. __v4l2_ctrl_modify_range(ov5675->vblank,
  903. mode->vts_min - mode->height,
  904. OV5675_VTS_MAX - mode->height, 1,
  905. vblank_def);
  906. __v4l2_ctrl_s_ctrl(ov5675->vblank, vblank_def);
  907. h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
  908. mode->width;
  909. __v4l2_ctrl_modify_range(ov5675->hblank, h_blank, h_blank, 1,
  910. h_blank);
  911. }
  912. mutex_unlock(&ov5675->mutex);
  913. return 0;
  914. }
  915. static int ov5675_get_format(struct v4l2_subdev *sd,
  916. struct v4l2_subdev_state *sd_state,
  917. struct v4l2_subdev_format *fmt)
  918. {
  919. struct ov5675 *ov5675 = to_ov5675(sd);
  920. mutex_lock(&ov5675->mutex);
  921. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  922. fmt->format = *v4l2_subdev_state_get_format(sd_state,
  923. fmt->pad);
  924. else
  925. ov5675_update_pad_format(ov5675->cur_mode, &fmt->format);
  926. mutex_unlock(&ov5675->mutex);
  927. return 0;
  928. }
  929. static int ov5675_get_selection(struct v4l2_subdev *sd,
  930. struct v4l2_subdev_state *state,
  931. struct v4l2_subdev_selection *sel)
  932. {
  933. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  934. return -EINVAL;
  935. switch (sel->target) {
  936. case V4L2_SEL_TGT_CROP_BOUNDS:
  937. sel->r.top = 0;
  938. sel->r.left = 0;
  939. sel->r.width = 2624;
  940. sel->r.height = 2000;
  941. return 0;
  942. case V4L2_SEL_TGT_CROP:
  943. case V4L2_SEL_TGT_CROP_DEFAULT:
  944. sel->r.top = 16;
  945. sel->r.left = 16;
  946. sel->r.width = 2592;
  947. sel->r.height = 1944;
  948. return 0;
  949. }
  950. return -EINVAL;
  951. }
  952. static int ov5675_enum_mbus_code(struct v4l2_subdev *sd,
  953. struct v4l2_subdev_state *sd_state,
  954. struct v4l2_subdev_mbus_code_enum *code)
  955. {
  956. if (code->index > 0)
  957. return -EINVAL;
  958. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  959. return 0;
  960. }
  961. static int ov5675_enum_frame_size(struct v4l2_subdev *sd,
  962. struct v4l2_subdev_state *sd_state,
  963. struct v4l2_subdev_frame_size_enum *fse)
  964. {
  965. if (fse->index >= ARRAY_SIZE(supported_modes))
  966. return -EINVAL;
  967. if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  968. return -EINVAL;
  969. fse->min_width = supported_modes[fse->index].width;
  970. fse->max_width = fse->min_width;
  971. fse->min_height = supported_modes[fse->index].height;
  972. fse->max_height = fse->min_height;
  973. return 0;
  974. }
  975. static int ov5675_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  976. {
  977. struct ov5675 *ov5675 = to_ov5675(sd);
  978. mutex_lock(&ov5675->mutex);
  979. ov5675_update_pad_format(&supported_modes[0],
  980. v4l2_subdev_state_get_format(fh->state, 0));
  981. mutex_unlock(&ov5675->mutex);
  982. return 0;
  983. }
  984. static const struct v4l2_subdev_video_ops ov5675_video_ops = {
  985. .s_stream = ov5675_set_stream,
  986. };
  987. static const struct v4l2_subdev_pad_ops ov5675_pad_ops = {
  988. .set_fmt = ov5675_set_format,
  989. .get_fmt = ov5675_get_format,
  990. .get_selection = ov5675_get_selection,
  991. .enum_mbus_code = ov5675_enum_mbus_code,
  992. .enum_frame_size = ov5675_enum_frame_size,
  993. };
  994. static const struct v4l2_subdev_ops ov5675_subdev_ops = {
  995. .video = &ov5675_video_ops,
  996. .pad = &ov5675_pad_ops,
  997. };
  998. static const struct media_entity_operations ov5675_subdev_entity_ops = {
  999. .link_validate = v4l2_subdev_link_validate,
  1000. };
  1001. static const struct v4l2_subdev_internal_ops ov5675_internal_ops = {
  1002. .open = ov5675_open,
  1003. };
  1004. static int ov5675_get_hwcfg(struct ov5675 *ov5675)
  1005. {
  1006. struct device *dev = ov5675->dev;
  1007. struct fwnode_handle *ep;
  1008. struct fwnode_handle *fwnode = dev_fwnode(dev);
  1009. struct v4l2_fwnode_endpoint bus_cfg = {
  1010. .bus_type = V4L2_MBUS_CSI2_DPHY
  1011. };
  1012. u32 xvclk_rate;
  1013. int ret;
  1014. unsigned int i, j;
  1015. if (!fwnode)
  1016. return -ENXIO;
  1017. ov5675->xvclk = devm_v4l2_sensor_clk_get(dev, NULL);
  1018. if (IS_ERR(ov5675->xvclk))
  1019. return dev_err_probe(dev, PTR_ERR(ov5675->xvclk),
  1020. "failed to get xvclk: %pe\n",
  1021. ov5675->xvclk);
  1022. xvclk_rate = clk_get_rate(ov5675->xvclk);
  1023. if (xvclk_rate != OV5675_XVCLK_19_2) {
  1024. dev_err(dev, "external clock rate %u is unsupported",
  1025. xvclk_rate);
  1026. return -EINVAL;
  1027. }
  1028. ov5675->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1029. GPIOD_OUT_HIGH);
  1030. if (IS_ERR(ov5675->reset_gpio)) {
  1031. ret = PTR_ERR(ov5675->reset_gpio);
  1032. dev_err(dev, "failed to get reset-gpios: %d\n", ret);
  1033. return ret;
  1034. }
  1035. for (i = 0; i < OV5675_NUM_SUPPLIES; i++)
  1036. ov5675->supplies[i].supply = ov5675_supply_names[i];
  1037. ret = devm_regulator_bulk_get(dev, OV5675_NUM_SUPPLIES,
  1038. ov5675->supplies);
  1039. if (ret)
  1040. return ret;
  1041. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1042. if (!ep)
  1043. return -ENXIO;
  1044. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1045. fwnode_handle_put(ep);
  1046. if (ret)
  1047. return ret;
  1048. if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV5675_DATA_LANES) {
  1049. dev_err(dev, "number of CSI2 data lanes %d is not supported",
  1050. bus_cfg.bus.mipi_csi2.num_data_lanes);
  1051. ret = -EINVAL;
  1052. goto check_hwcfg_error;
  1053. }
  1054. if (!bus_cfg.nr_of_link_frequencies) {
  1055. dev_err(dev, "no link frequencies defined");
  1056. ret = -EINVAL;
  1057. goto check_hwcfg_error;
  1058. }
  1059. for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
  1060. for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
  1061. if (link_freq_menu_items[i] ==
  1062. bus_cfg.link_frequencies[j])
  1063. break;
  1064. }
  1065. if (j == bus_cfg.nr_of_link_frequencies) {
  1066. dev_err(dev, "no link frequency %lld supported",
  1067. link_freq_menu_items[i]);
  1068. ret = -EINVAL;
  1069. goto check_hwcfg_error;
  1070. }
  1071. }
  1072. check_hwcfg_error:
  1073. v4l2_fwnode_endpoint_free(&bus_cfg);
  1074. return ret;
  1075. }
  1076. static void ov5675_remove(struct i2c_client *client)
  1077. {
  1078. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1079. struct ov5675 *ov5675 = to_ov5675(sd);
  1080. v4l2_async_unregister_subdev(sd);
  1081. media_entity_cleanup(&sd->entity);
  1082. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1083. pm_runtime_disable(ov5675->dev);
  1084. mutex_destroy(&ov5675->mutex);
  1085. if (!pm_runtime_status_suspended(ov5675->dev))
  1086. ov5675_power_off(ov5675->dev);
  1087. pm_runtime_set_suspended(ov5675->dev);
  1088. }
  1089. static int ov5675_probe(struct i2c_client *client)
  1090. {
  1091. struct ov5675 *ov5675;
  1092. bool full_power;
  1093. int ret;
  1094. ov5675 = devm_kzalloc(&client->dev, sizeof(*ov5675), GFP_KERNEL);
  1095. if (!ov5675)
  1096. return -ENOMEM;
  1097. ov5675->dev = &client->dev;
  1098. ret = ov5675_get_hwcfg(ov5675);
  1099. if (ret)
  1100. return ret;
  1101. v4l2_i2c_subdev_init(&ov5675->sd, client, &ov5675_subdev_ops);
  1102. ret = ov5675_power_on(ov5675->dev);
  1103. if (ret) {
  1104. dev_err(ov5675->dev, "failed to power on: %d\n", ret);
  1105. return ret;
  1106. }
  1107. full_power = acpi_dev_state_d0(ov5675->dev);
  1108. if (full_power) {
  1109. ret = ov5675_identify_module(ov5675);
  1110. if (ret) {
  1111. dev_err(ov5675->dev, "failed to find sensor: %d", ret);
  1112. goto probe_power_off;
  1113. }
  1114. }
  1115. mutex_init(&ov5675->mutex);
  1116. ov5675->cur_mode = &supported_modes[0];
  1117. ret = ov5675_init_controls(ov5675);
  1118. if (ret) {
  1119. dev_err(ov5675->dev, "failed to init controls: %d", ret);
  1120. goto probe_error_v4l2_ctrl_handler_free;
  1121. }
  1122. ov5675->sd.internal_ops = &ov5675_internal_ops;
  1123. ov5675->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1124. ov5675->sd.entity.ops = &ov5675_subdev_entity_ops;
  1125. ov5675->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1126. ov5675->pad.flags = MEDIA_PAD_FL_SOURCE;
  1127. ret = media_entity_pads_init(&ov5675->sd.entity, 1, &ov5675->pad);
  1128. if (ret) {
  1129. dev_err(ov5675->dev, "failed to init entity pads: %d", ret);
  1130. goto probe_error_v4l2_ctrl_handler_free;
  1131. }
  1132. ret = v4l2_async_register_subdev_sensor(&ov5675->sd);
  1133. if (ret < 0) {
  1134. dev_err(ov5675->dev, "failed to register V4L2 subdev: %d",
  1135. ret);
  1136. goto probe_error_media_entity_cleanup;
  1137. }
  1138. /* Set the device's state to active if it's in D0 state. */
  1139. if (full_power)
  1140. pm_runtime_set_active(ov5675->dev);
  1141. pm_runtime_enable(ov5675->dev);
  1142. pm_runtime_idle(ov5675->dev);
  1143. return 0;
  1144. probe_error_media_entity_cleanup:
  1145. media_entity_cleanup(&ov5675->sd.entity);
  1146. probe_error_v4l2_ctrl_handler_free:
  1147. v4l2_ctrl_handler_free(ov5675->sd.ctrl_handler);
  1148. mutex_destroy(&ov5675->mutex);
  1149. probe_power_off:
  1150. ov5675_power_off(ov5675->dev);
  1151. return ret;
  1152. }
  1153. static const struct dev_pm_ops ov5675_pm_ops = {
  1154. SET_RUNTIME_PM_OPS(ov5675_power_off, ov5675_power_on, NULL)
  1155. };
  1156. #ifdef CONFIG_ACPI
  1157. static const struct acpi_device_id ov5675_acpi_ids[] = {
  1158. {"OVTI5675"},
  1159. {}
  1160. };
  1161. MODULE_DEVICE_TABLE(acpi, ov5675_acpi_ids);
  1162. #endif
  1163. static const struct of_device_id ov5675_of_match[] = {
  1164. { .compatible = "ovti,ov5675", },
  1165. { /* sentinel */ },
  1166. };
  1167. MODULE_DEVICE_TABLE(of, ov5675_of_match);
  1168. static struct i2c_driver ov5675_i2c_driver = {
  1169. .driver = {
  1170. .name = "ov5675",
  1171. .pm = &ov5675_pm_ops,
  1172. .acpi_match_table = ACPI_PTR(ov5675_acpi_ids),
  1173. .of_match_table = ov5675_of_match,
  1174. },
  1175. .probe = ov5675_probe,
  1176. .remove = ov5675_remove,
  1177. .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
  1178. };
  1179. module_i2c_driver(ov5675_i2c_driver);
  1180. MODULE_AUTHOR("Shawn Tu");
  1181. MODULE_DESCRIPTION("OmniVision OV5675 sensor driver");
  1182. MODULE_LICENSE("GPL v2");