imx258.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Intel Corporation
  3. #include <linux/acpi.h>
  4. #include <linux/clk.h>
  5. #include <linux/delay.h>
  6. #include <linux/i2c.h>
  7. #include <linux/module.h>
  8. #include <linux/pm_runtime.h>
  9. #include <media/v4l2-ctrls.h>
  10. #include <media/v4l2-device.h>
  11. #include <asm/unaligned.h>
  12. #define IMX258_REG_VALUE_08BIT 1
  13. #define IMX258_REG_VALUE_16BIT 2
  14. #define IMX258_REG_MODE_SELECT 0x0100
  15. #define IMX258_MODE_STANDBY 0x00
  16. #define IMX258_MODE_STREAMING 0x01
  17. /* Chip ID */
  18. #define IMX258_REG_CHIP_ID 0x0016
  19. #define IMX258_CHIP_ID 0x0258
  20. /* V_TIMING internal */
  21. #define IMX258_VTS_30FPS 0x0c50
  22. #define IMX258_VTS_30FPS_2K 0x0638
  23. #define IMX258_VTS_30FPS_VGA 0x034c
  24. #define IMX258_VTS_MAX 0xffff
  25. /*Frame Length Line*/
  26. #define IMX258_FLL_MIN 0x08a6
  27. #define IMX258_FLL_MAX 0xffff
  28. #define IMX258_FLL_STEP 1
  29. #define IMX258_FLL_DEFAULT 0x0c98
  30. /* HBLANK control - read only */
  31. #define IMX258_PPL_DEFAULT 5352
  32. /* Exposure control */
  33. #define IMX258_REG_EXPOSURE 0x0202
  34. #define IMX258_EXPOSURE_MIN 4
  35. #define IMX258_EXPOSURE_STEP 1
  36. #define IMX258_EXPOSURE_DEFAULT 0x640
  37. #define IMX258_EXPOSURE_MAX 65535
  38. /* Analog gain control */
  39. #define IMX258_REG_ANALOG_GAIN 0x0204
  40. #define IMX258_ANA_GAIN_MIN 0
  41. #define IMX258_ANA_GAIN_MAX 480
  42. #define IMX258_ANA_GAIN_STEP 1
  43. #define IMX258_ANA_GAIN_DEFAULT 0x0
  44. /* Digital gain control */
  45. #define IMX258_REG_GR_DIGITAL_GAIN 0x020e
  46. #define IMX258_REG_R_DIGITAL_GAIN 0x0210
  47. #define IMX258_REG_B_DIGITAL_GAIN 0x0212
  48. #define IMX258_REG_GB_DIGITAL_GAIN 0x0214
  49. #define IMX258_DGTL_GAIN_MIN 0
  50. #define IMX258_DGTL_GAIN_MAX 4096 /* Max = 0xFFF */
  51. #define IMX258_DGTL_GAIN_DEFAULT 1024
  52. #define IMX258_DGTL_GAIN_STEP 1
  53. /* HDR control */
  54. #define IMX258_REG_HDR 0x0220
  55. #define IMX258_HDR_ON BIT(0)
  56. #define IMX258_REG_HDR_RATIO 0x0222
  57. #define IMX258_HDR_RATIO_MIN 0
  58. #define IMX258_HDR_RATIO_MAX 5
  59. #define IMX258_HDR_RATIO_STEP 1
  60. #define IMX258_HDR_RATIO_DEFAULT 0x0
  61. /* Test Pattern Control */
  62. #define IMX258_REG_TEST_PATTERN 0x0600
  63. /* Orientation */
  64. #define REG_MIRROR_FLIP_CONTROL 0x0101
  65. #define REG_CONFIG_MIRROR_FLIP 0x03
  66. #define REG_CONFIG_FLIP_TEST_PATTERN 0x02
  67. /* Input clock frequency in Hz */
  68. #define IMX258_INPUT_CLOCK_FREQ 19200000
  69. struct imx258_reg {
  70. u16 address;
  71. u8 val;
  72. };
  73. struct imx258_reg_list {
  74. u32 num_of_regs;
  75. const struct imx258_reg *regs;
  76. };
  77. /* Link frequency config */
  78. struct imx258_link_freq_config {
  79. u32 pixels_per_line;
  80. /* PLL registers for this link frequency */
  81. struct imx258_reg_list reg_list;
  82. };
  83. /* Mode : resolution and related config&values */
  84. struct imx258_mode {
  85. /* Frame width */
  86. u32 width;
  87. /* Frame height */
  88. u32 height;
  89. /* V-timing */
  90. u32 vts_def;
  91. u32 vts_min;
  92. /* Index of Link frequency config to be used */
  93. u32 link_freq_index;
  94. /* Default register values */
  95. struct imx258_reg_list reg_list;
  96. };
  97. /* 4208x3118 needs 1267Mbps/lane, 4 lanes */
  98. static const struct imx258_reg mipi_data_rate_1267mbps[] = {
  99. { 0x0301, 0x05 },
  100. { 0x0303, 0x02 },
  101. { 0x0305, 0x03 },
  102. { 0x0306, 0x00 },
  103. { 0x0307, 0xC6 },
  104. { 0x0309, 0x0A },
  105. { 0x030B, 0x01 },
  106. { 0x030D, 0x02 },
  107. { 0x030E, 0x00 },
  108. { 0x030F, 0xD8 },
  109. { 0x0310, 0x00 },
  110. { 0x0820, 0x13 },
  111. { 0x0821, 0x4C },
  112. { 0x0822, 0xCC },
  113. { 0x0823, 0xCC },
  114. };
  115. static const struct imx258_reg mipi_data_rate_640mbps[] = {
  116. { 0x0301, 0x05 },
  117. { 0x0303, 0x02 },
  118. { 0x0305, 0x03 },
  119. { 0x0306, 0x00 },
  120. { 0x0307, 0x64 },
  121. { 0x0309, 0x0A },
  122. { 0x030B, 0x01 },
  123. { 0x030D, 0x02 },
  124. { 0x030E, 0x00 },
  125. { 0x030F, 0xD8 },
  126. { 0x0310, 0x00 },
  127. { 0x0820, 0x0A },
  128. { 0x0821, 0x00 },
  129. { 0x0822, 0x00 },
  130. { 0x0823, 0x00 },
  131. };
  132. static const struct imx258_reg mode_4208x3118_regs[] = {
  133. { 0x0136, 0x13 },
  134. { 0x0137, 0x33 },
  135. { 0x3051, 0x00 },
  136. { 0x3052, 0x00 },
  137. { 0x4E21, 0x14 },
  138. { 0x6B11, 0xCF },
  139. { 0x7FF0, 0x08 },
  140. { 0x7FF1, 0x0F },
  141. { 0x7FF2, 0x08 },
  142. { 0x7FF3, 0x1B },
  143. { 0x7FF4, 0x23 },
  144. { 0x7FF5, 0x60 },
  145. { 0x7FF6, 0x00 },
  146. { 0x7FF7, 0x01 },
  147. { 0x7FF8, 0x00 },
  148. { 0x7FF9, 0x78 },
  149. { 0x7FFA, 0x00 },
  150. { 0x7FFB, 0x00 },
  151. { 0x7FFC, 0x00 },
  152. { 0x7FFD, 0x00 },
  153. { 0x7FFE, 0x00 },
  154. { 0x7FFF, 0x03 },
  155. { 0x7F76, 0x03 },
  156. { 0x7F77, 0xFE },
  157. { 0x7FA8, 0x03 },
  158. { 0x7FA9, 0xFE },
  159. { 0x7B24, 0x81 },
  160. { 0x7B25, 0x00 },
  161. { 0x6564, 0x07 },
  162. { 0x6B0D, 0x41 },
  163. { 0x653D, 0x04 },
  164. { 0x6B05, 0x8C },
  165. { 0x6B06, 0xF9 },
  166. { 0x6B08, 0x65 },
  167. { 0x6B09, 0xFC },
  168. { 0x6B0A, 0xCF },
  169. { 0x6B0B, 0xD2 },
  170. { 0x6700, 0x0E },
  171. { 0x6707, 0x0E },
  172. { 0x9104, 0x00 },
  173. { 0x4648, 0x7F },
  174. { 0x7420, 0x00 },
  175. { 0x7421, 0x1C },
  176. { 0x7422, 0x00 },
  177. { 0x7423, 0xD7 },
  178. { 0x5F04, 0x00 },
  179. { 0x5F05, 0xED },
  180. { 0x0112, 0x0A },
  181. { 0x0113, 0x0A },
  182. { 0x0114, 0x03 },
  183. { 0x0342, 0x14 },
  184. { 0x0343, 0xE8 },
  185. { 0x0340, 0x0C },
  186. { 0x0341, 0x50 },
  187. { 0x0344, 0x00 },
  188. { 0x0345, 0x00 },
  189. { 0x0346, 0x00 },
  190. { 0x0347, 0x00 },
  191. { 0x0348, 0x10 },
  192. { 0x0349, 0x6F },
  193. { 0x034A, 0x0C },
  194. { 0x034B, 0x2E },
  195. { 0x0381, 0x01 },
  196. { 0x0383, 0x01 },
  197. { 0x0385, 0x01 },
  198. { 0x0387, 0x01 },
  199. { 0x0900, 0x00 },
  200. { 0x0901, 0x11 },
  201. { 0x0401, 0x00 },
  202. { 0x0404, 0x00 },
  203. { 0x0405, 0x10 },
  204. { 0x0408, 0x00 },
  205. { 0x0409, 0x00 },
  206. { 0x040A, 0x00 },
  207. { 0x040B, 0x00 },
  208. { 0x040C, 0x10 },
  209. { 0x040D, 0x70 },
  210. { 0x040E, 0x0C },
  211. { 0x040F, 0x30 },
  212. { 0x3038, 0x00 },
  213. { 0x303A, 0x00 },
  214. { 0x303B, 0x10 },
  215. { 0x300D, 0x00 },
  216. { 0x034C, 0x10 },
  217. { 0x034D, 0x70 },
  218. { 0x034E, 0x0C },
  219. { 0x034F, 0x30 },
  220. { 0x0350, 0x01 },
  221. { 0x0202, 0x0C },
  222. { 0x0203, 0x46 },
  223. { 0x0204, 0x00 },
  224. { 0x0205, 0x00 },
  225. { 0x020E, 0x01 },
  226. { 0x020F, 0x00 },
  227. { 0x0210, 0x01 },
  228. { 0x0211, 0x00 },
  229. { 0x0212, 0x01 },
  230. { 0x0213, 0x00 },
  231. { 0x0214, 0x01 },
  232. { 0x0215, 0x00 },
  233. { 0x7BCD, 0x00 },
  234. { 0x94DC, 0x20 },
  235. { 0x94DD, 0x20 },
  236. { 0x94DE, 0x20 },
  237. { 0x95DC, 0x20 },
  238. { 0x95DD, 0x20 },
  239. { 0x95DE, 0x20 },
  240. { 0x7FB0, 0x00 },
  241. { 0x9010, 0x3E },
  242. { 0x9419, 0x50 },
  243. { 0x941B, 0x50 },
  244. { 0x9519, 0x50 },
  245. { 0x951B, 0x50 },
  246. { 0x3030, 0x00 },
  247. { 0x3032, 0x00 },
  248. { 0x0220, 0x00 },
  249. };
  250. static const struct imx258_reg mode_2104_1560_regs[] = {
  251. { 0x0136, 0x13 },
  252. { 0x0137, 0x33 },
  253. { 0x3051, 0x00 },
  254. { 0x3052, 0x00 },
  255. { 0x4E21, 0x14 },
  256. { 0x6B11, 0xCF },
  257. { 0x7FF0, 0x08 },
  258. { 0x7FF1, 0x0F },
  259. { 0x7FF2, 0x08 },
  260. { 0x7FF3, 0x1B },
  261. { 0x7FF4, 0x23 },
  262. { 0x7FF5, 0x60 },
  263. { 0x7FF6, 0x00 },
  264. { 0x7FF7, 0x01 },
  265. { 0x7FF8, 0x00 },
  266. { 0x7FF9, 0x78 },
  267. { 0x7FFA, 0x00 },
  268. { 0x7FFB, 0x00 },
  269. { 0x7FFC, 0x00 },
  270. { 0x7FFD, 0x00 },
  271. { 0x7FFE, 0x00 },
  272. { 0x7FFF, 0x03 },
  273. { 0x7F76, 0x03 },
  274. { 0x7F77, 0xFE },
  275. { 0x7FA8, 0x03 },
  276. { 0x7FA9, 0xFE },
  277. { 0x7B24, 0x81 },
  278. { 0x7B25, 0x00 },
  279. { 0x6564, 0x07 },
  280. { 0x6B0D, 0x41 },
  281. { 0x653D, 0x04 },
  282. { 0x6B05, 0x8C },
  283. { 0x6B06, 0xF9 },
  284. { 0x6B08, 0x65 },
  285. { 0x6B09, 0xFC },
  286. { 0x6B0A, 0xCF },
  287. { 0x6B0B, 0xD2 },
  288. { 0x6700, 0x0E },
  289. { 0x6707, 0x0E },
  290. { 0x9104, 0x00 },
  291. { 0x4648, 0x7F },
  292. { 0x7420, 0x00 },
  293. { 0x7421, 0x1C },
  294. { 0x7422, 0x00 },
  295. { 0x7423, 0xD7 },
  296. { 0x5F04, 0x00 },
  297. { 0x5F05, 0xED },
  298. { 0x0112, 0x0A },
  299. { 0x0113, 0x0A },
  300. { 0x0114, 0x03 },
  301. { 0x0342, 0x14 },
  302. { 0x0343, 0xE8 },
  303. { 0x0340, 0x06 },
  304. { 0x0341, 0x38 },
  305. { 0x0344, 0x00 },
  306. { 0x0345, 0x00 },
  307. { 0x0346, 0x00 },
  308. { 0x0347, 0x00 },
  309. { 0x0348, 0x10 },
  310. { 0x0349, 0x6F },
  311. { 0x034A, 0x0C },
  312. { 0x034B, 0x2E },
  313. { 0x0381, 0x01 },
  314. { 0x0383, 0x01 },
  315. { 0x0385, 0x01 },
  316. { 0x0387, 0x01 },
  317. { 0x0900, 0x01 },
  318. { 0x0901, 0x12 },
  319. { 0x0401, 0x01 },
  320. { 0x0404, 0x00 },
  321. { 0x0405, 0x20 },
  322. { 0x0408, 0x00 },
  323. { 0x0409, 0x02 },
  324. { 0x040A, 0x00 },
  325. { 0x040B, 0x00 },
  326. { 0x040C, 0x10 },
  327. { 0x040D, 0x6A },
  328. { 0x040E, 0x06 },
  329. { 0x040F, 0x18 },
  330. { 0x3038, 0x00 },
  331. { 0x303A, 0x00 },
  332. { 0x303B, 0x10 },
  333. { 0x300D, 0x00 },
  334. { 0x034C, 0x08 },
  335. { 0x034D, 0x38 },
  336. { 0x034E, 0x06 },
  337. { 0x034F, 0x18 },
  338. { 0x0350, 0x01 },
  339. { 0x0202, 0x06 },
  340. { 0x0203, 0x2E },
  341. { 0x0204, 0x00 },
  342. { 0x0205, 0x00 },
  343. { 0x020E, 0x01 },
  344. { 0x020F, 0x00 },
  345. { 0x0210, 0x01 },
  346. { 0x0211, 0x00 },
  347. { 0x0212, 0x01 },
  348. { 0x0213, 0x00 },
  349. { 0x0214, 0x01 },
  350. { 0x0215, 0x00 },
  351. { 0x7BCD, 0x01 },
  352. { 0x94DC, 0x20 },
  353. { 0x94DD, 0x20 },
  354. { 0x94DE, 0x20 },
  355. { 0x95DC, 0x20 },
  356. { 0x95DD, 0x20 },
  357. { 0x95DE, 0x20 },
  358. { 0x7FB0, 0x00 },
  359. { 0x9010, 0x3E },
  360. { 0x9419, 0x50 },
  361. { 0x941B, 0x50 },
  362. { 0x9519, 0x50 },
  363. { 0x951B, 0x50 },
  364. { 0x3030, 0x00 },
  365. { 0x3032, 0x00 },
  366. { 0x0220, 0x00 },
  367. };
  368. static const struct imx258_reg mode_1048_780_regs[] = {
  369. { 0x0136, 0x13 },
  370. { 0x0137, 0x33 },
  371. { 0x3051, 0x00 },
  372. { 0x3052, 0x00 },
  373. { 0x4E21, 0x14 },
  374. { 0x6B11, 0xCF },
  375. { 0x7FF0, 0x08 },
  376. { 0x7FF1, 0x0F },
  377. { 0x7FF2, 0x08 },
  378. { 0x7FF3, 0x1B },
  379. { 0x7FF4, 0x23 },
  380. { 0x7FF5, 0x60 },
  381. { 0x7FF6, 0x00 },
  382. { 0x7FF7, 0x01 },
  383. { 0x7FF8, 0x00 },
  384. { 0x7FF9, 0x78 },
  385. { 0x7FFA, 0x00 },
  386. { 0x7FFB, 0x00 },
  387. { 0x7FFC, 0x00 },
  388. { 0x7FFD, 0x00 },
  389. { 0x7FFE, 0x00 },
  390. { 0x7FFF, 0x03 },
  391. { 0x7F76, 0x03 },
  392. { 0x7F77, 0xFE },
  393. { 0x7FA8, 0x03 },
  394. { 0x7FA9, 0xFE },
  395. { 0x7B24, 0x81 },
  396. { 0x7B25, 0x00 },
  397. { 0x6564, 0x07 },
  398. { 0x6B0D, 0x41 },
  399. { 0x653D, 0x04 },
  400. { 0x6B05, 0x8C },
  401. { 0x6B06, 0xF9 },
  402. { 0x6B08, 0x65 },
  403. { 0x6B09, 0xFC },
  404. { 0x6B0A, 0xCF },
  405. { 0x6B0B, 0xD2 },
  406. { 0x6700, 0x0E },
  407. { 0x6707, 0x0E },
  408. { 0x9104, 0x00 },
  409. { 0x4648, 0x7F },
  410. { 0x7420, 0x00 },
  411. { 0x7421, 0x1C },
  412. { 0x7422, 0x00 },
  413. { 0x7423, 0xD7 },
  414. { 0x5F04, 0x00 },
  415. { 0x5F05, 0xED },
  416. { 0x0112, 0x0A },
  417. { 0x0113, 0x0A },
  418. { 0x0114, 0x03 },
  419. { 0x0342, 0x14 },
  420. { 0x0343, 0xE8 },
  421. { 0x0340, 0x03 },
  422. { 0x0341, 0x4C },
  423. { 0x0344, 0x00 },
  424. { 0x0345, 0x00 },
  425. { 0x0346, 0x00 },
  426. { 0x0347, 0x00 },
  427. { 0x0348, 0x10 },
  428. { 0x0349, 0x6F },
  429. { 0x034A, 0x0C },
  430. { 0x034B, 0x2E },
  431. { 0x0381, 0x01 },
  432. { 0x0383, 0x01 },
  433. { 0x0385, 0x01 },
  434. { 0x0387, 0x01 },
  435. { 0x0900, 0x01 },
  436. { 0x0901, 0x14 },
  437. { 0x0401, 0x01 },
  438. { 0x0404, 0x00 },
  439. { 0x0405, 0x40 },
  440. { 0x0408, 0x00 },
  441. { 0x0409, 0x06 },
  442. { 0x040A, 0x00 },
  443. { 0x040B, 0x00 },
  444. { 0x040C, 0x10 },
  445. { 0x040D, 0x64 },
  446. { 0x040E, 0x03 },
  447. { 0x040F, 0x0C },
  448. { 0x3038, 0x00 },
  449. { 0x303A, 0x00 },
  450. { 0x303B, 0x10 },
  451. { 0x300D, 0x00 },
  452. { 0x034C, 0x04 },
  453. { 0x034D, 0x18 },
  454. { 0x034E, 0x03 },
  455. { 0x034F, 0x0C },
  456. { 0x0350, 0x01 },
  457. { 0x0202, 0x03 },
  458. { 0x0203, 0x42 },
  459. { 0x0204, 0x00 },
  460. { 0x0205, 0x00 },
  461. { 0x020E, 0x01 },
  462. { 0x020F, 0x00 },
  463. { 0x0210, 0x01 },
  464. { 0x0211, 0x00 },
  465. { 0x0212, 0x01 },
  466. { 0x0213, 0x00 },
  467. { 0x0214, 0x01 },
  468. { 0x0215, 0x00 },
  469. { 0x7BCD, 0x00 },
  470. { 0x94DC, 0x20 },
  471. { 0x94DD, 0x20 },
  472. { 0x94DE, 0x20 },
  473. { 0x95DC, 0x20 },
  474. { 0x95DD, 0x20 },
  475. { 0x95DE, 0x20 },
  476. { 0x7FB0, 0x00 },
  477. { 0x9010, 0x3E },
  478. { 0x9419, 0x50 },
  479. { 0x941B, 0x50 },
  480. { 0x9519, 0x50 },
  481. { 0x951B, 0x50 },
  482. { 0x3030, 0x00 },
  483. { 0x3032, 0x00 },
  484. { 0x0220, 0x00 },
  485. };
  486. static const char * const imx258_test_pattern_menu[] = {
  487. "Disabled",
  488. "Solid Colour",
  489. "Eight Vertical Colour Bars",
  490. "Colour Bars With Fade to Grey",
  491. "Pseudorandom Sequence (PN9)",
  492. };
  493. /* Configurations for supported link frequencies */
  494. #define IMX258_LINK_FREQ_634MHZ 633600000ULL
  495. #define IMX258_LINK_FREQ_320MHZ 320000000ULL
  496. enum {
  497. IMX258_LINK_FREQ_1267MBPS,
  498. IMX258_LINK_FREQ_640MBPS,
  499. };
  500. /*
  501. * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
  502. * data rate => double data rate; number of lanes => 4; bits per pixel => 10
  503. */
  504. static u64 link_freq_to_pixel_rate(u64 f)
  505. {
  506. f *= 2 * 4;
  507. do_div(f, 10);
  508. return f;
  509. }
  510. /* Menu items for LINK_FREQ V4L2 control */
  511. static const s64 link_freq_menu_items[] = {
  512. IMX258_LINK_FREQ_634MHZ,
  513. IMX258_LINK_FREQ_320MHZ,
  514. };
  515. /* Link frequency configs */
  516. static const struct imx258_link_freq_config link_freq_configs[] = {
  517. [IMX258_LINK_FREQ_1267MBPS] = {
  518. .pixels_per_line = IMX258_PPL_DEFAULT,
  519. .reg_list = {
  520. .num_of_regs = ARRAY_SIZE(mipi_data_rate_1267mbps),
  521. .regs = mipi_data_rate_1267mbps,
  522. }
  523. },
  524. [IMX258_LINK_FREQ_640MBPS] = {
  525. .pixels_per_line = IMX258_PPL_DEFAULT,
  526. .reg_list = {
  527. .num_of_regs = ARRAY_SIZE(mipi_data_rate_640mbps),
  528. .regs = mipi_data_rate_640mbps,
  529. }
  530. },
  531. };
  532. /* Mode configs */
  533. static const struct imx258_mode supported_modes[] = {
  534. {
  535. .width = 4208,
  536. .height = 3118,
  537. .vts_def = IMX258_VTS_30FPS,
  538. .vts_min = IMX258_VTS_30FPS,
  539. .reg_list = {
  540. .num_of_regs = ARRAY_SIZE(mode_4208x3118_regs),
  541. .regs = mode_4208x3118_regs,
  542. },
  543. .link_freq_index = IMX258_LINK_FREQ_1267MBPS,
  544. },
  545. {
  546. .width = 2104,
  547. .height = 1560,
  548. .vts_def = IMX258_VTS_30FPS_2K,
  549. .vts_min = IMX258_VTS_30FPS_2K,
  550. .reg_list = {
  551. .num_of_regs = ARRAY_SIZE(mode_2104_1560_regs),
  552. .regs = mode_2104_1560_regs,
  553. },
  554. .link_freq_index = IMX258_LINK_FREQ_640MBPS,
  555. },
  556. {
  557. .width = 1048,
  558. .height = 780,
  559. .vts_def = IMX258_VTS_30FPS_VGA,
  560. .vts_min = IMX258_VTS_30FPS_VGA,
  561. .reg_list = {
  562. .num_of_regs = ARRAY_SIZE(mode_1048_780_regs),
  563. .regs = mode_1048_780_regs,
  564. },
  565. .link_freq_index = IMX258_LINK_FREQ_640MBPS,
  566. },
  567. };
  568. struct imx258 {
  569. struct v4l2_subdev sd;
  570. struct media_pad pad;
  571. struct v4l2_ctrl_handler ctrl_handler;
  572. /* V4L2 Controls */
  573. struct v4l2_ctrl *link_freq;
  574. struct v4l2_ctrl *pixel_rate;
  575. struct v4l2_ctrl *vblank;
  576. struct v4l2_ctrl *hblank;
  577. struct v4l2_ctrl *exposure;
  578. /* Current mode */
  579. const struct imx258_mode *cur_mode;
  580. /*
  581. * Mutex for serialized access:
  582. * Protect sensor module set pad format and start/stop streaming safely.
  583. */
  584. struct mutex mutex;
  585. /* Streaming on/off */
  586. bool streaming;
  587. struct clk *clk;
  588. };
  589. static inline struct imx258 *to_imx258(struct v4l2_subdev *_sd)
  590. {
  591. return container_of(_sd, struct imx258, sd);
  592. }
  593. /* Read registers up to 2 at a time */
  594. static int imx258_read_reg(struct imx258 *imx258, u16 reg, u32 len, u32 *val)
  595. {
  596. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  597. struct i2c_msg msgs[2];
  598. u8 addr_buf[2] = { reg >> 8, reg & 0xff };
  599. u8 data_buf[4] = { 0, };
  600. int ret;
  601. if (len > 4)
  602. return -EINVAL;
  603. /* Write register address */
  604. msgs[0].addr = client->addr;
  605. msgs[0].flags = 0;
  606. msgs[0].len = ARRAY_SIZE(addr_buf);
  607. msgs[0].buf = addr_buf;
  608. /* Read data from register */
  609. msgs[1].addr = client->addr;
  610. msgs[1].flags = I2C_M_RD;
  611. msgs[1].len = len;
  612. msgs[1].buf = &data_buf[4 - len];
  613. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  614. if (ret != ARRAY_SIZE(msgs))
  615. return -EIO;
  616. *val = get_unaligned_be32(data_buf);
  617. return 0;
  618. }
  619. /* Write registers up to 2 at a time */
  620. static int imx258_write_reg(struct imx258 *imx258, u16 reg, u32 len, u32 val)
  621. {
  622. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  623. u8 buf[6];
  624. if (len > 4)
  625. return -EINVAL;
  626. put_unaligned_be16(reg, buf);
  627. put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
  628. if (i2c_master_send(client, buf, len + 2) != len + 2)
  629. return -EIO;
  630. return 0;
  631. }
  632. /* Write a list of registers */
  633. static int imx258_write_regs(struct imx258 *imx258,
  634. const struct imx258_reg *regs, u32 len)
  635. {
  636. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  637. unsigned int i;
  638. int ret;
  639. for (i = 0; i < len; i++) {
  640. ret = imx258_write_reg(imx258, regs[i].address, 1,
  641. regs[i].val);
  642. if (ret) {
  643. dev_err_ratelimited(
  644. &client->dev,
  645. "Failed to write reg 0x%4.4x. error = %d\n",
  646. regs[i].address, ret);
  647. return ret;
  648. }
  649. }
  650. return 0;
  651. }
  652. /* Open sub-device */
  653. static int imx258_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  654. {
  655. struct v4l2_mbus_framefmt *try_fmt =
  656. v4l2_subdev_get_try_format(sd, fh->state, 0);
  657. /* Initialize try_fmt */
  658. try_fmt->width = supported_modes[0].width;
  659. try_fmt->height = supported_modes[0].height;
  660. try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  661. try_fmt->field = V4L2_FIELD_NONE;
  662. return 0;
  663. }
  664. static int imx258_update_digital_gain(struct imx258 *imx258, u32 len, u32 val)
  665. {
  666. int ret;
  667. ret = imx258_write_reg(imx258, IMX258_REG_GR_DIGITAL_GAIN,
  668. IMX258_REG_VALUE_16BIT,
  669. val);
  670. if (ret)
  671. return ret;
  672. ret = imx258_write_reg(imx258, IMX258_REG_GB_DIGITAL_GAIN,
  673. IMX258_REG_VALUE_16BIT,
  674. val);
  675. if (ret)
  676. return ret;
  677. ret = imx258_write_reg(imx258, IMX258_REG_R_DIGITAL_GAIN,
  678. IMX258_REG_VALUE_16BIT,
  679. val);
  680. if (ret)
  681. return ret;
  682. ret = imx258_write_reg(imx258, IMX258_REG_B_DIGITAL_GAIN,
  683. IMX258_REG_VALUE_16BIT,
  684. val);
  685. if (ret)
  686. return ret;
  687. return 0;
  688. }
  689. static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
  690. {
  691. struct imx258 *imx258 =
  692. container_of(ctrl->handler, struct imx258, ctrl_handler);
  693. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  694. int ret = 0;
  695. /*
  696. * Applying V4L2 control value only happens
  697. * when power is up for streaming
  698. */
  699. if (pm_runtime_get_if_in_use(&client->dev) == 0)
  700. return 0;
  701. switch (ctrl->id) {
  702. case V4L2_CID_ANALOGUE_GAIN:
  703. ret = imx258_write_reg(imx258, IMX258_REG_ANALOG_GAIN,
  704. IMX258_REG_VALUE_16BIT,
  705. ctrl->val);
  706. break;
  707. case V4L2_CID_EXPOSURE:
  708. ret = imx258_write_reg(imx258, IMX258_REG_EXPOSURE,
  709. IMX258_REG_VALUE_16BIT,
  710. ctrl->val);
  711. break;
  712. case V4L2_CID_DIGITAL_GAIN:
  713. ret = imx258_update_digital_gain(imx258, IMX258_REG_VALUE_16BIT,
  714. ctrl->val);
  715. break;
  716. case V4L2_CID_TEST_PATTERN:
  717. ret = imx258_write_reg(imx258, IMX258_REG_TEST_PATTERN,
  718. IMX258_REG_VALUE_16BIT,
  719. ctrl->val);
  720. ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
  721. IMX258_REG_VALUE_08BIT,
  722. !ctrl->val ? REG_CONFIG_MIRROR_FLIP :
  723. REG_CONFIG_FLIP_TEST_PATTERN);
  724. break;
  725. case V4L2_CID_WIDE_DYNAMIC_RANGE:
  726. if (!ctrl->val) {
  727. ret = imx258_write_reg(imx258, IMX258_REG_HDR,
  728. IMX258_REG_VALUE_08BIT,
  729. IMX258_HDR_RATIO_MIN);
  730. } else {
  731. ret = imx258_write_reg(imx258, IMX258_REG_HDR,
  732. IMX258_REG_VALUE_08BIT,
  733. IMX258_HDR_ON);
  734. if (ret)
  735. break;
  736. ret = imx258_write_reg(imx258, IMX258_REG_HDR_RATIO,
  737. IMX258_REG_VALUE_08BIT,
  738. BIT(IMX258_HDR_RATIO_MAX));
  739. }
  740. break;
  741. default:
  742. dev_info(&client->dev,
  743. "ctrl(id:0x%x,val:0x%x) is not handled\n",
  744. ctrl->id, ctrl->val);
  745. ret = -EINVAL;
  746. break;
  747. }
  748. pm_runtime_put(&client->dev);
  749. return ret;
  750. }
  751. static const struct v4l2_ctrl_ops imx258_ctrl_ops = {
  752. .s_ctrl = imx258_set_ctrl,
  753. };
  754. static int imx258_enum_mbus_code(struct v4l2_subdev *sd,
  755. struct v4l2_subdev_state *sd_state,
  756. struct v4l2_subdev_mbus_code_enum *code)
  757. {
  758. /* Only one bayer order(GRBG) is supported */
  759. if (code->index > 0)
  760. return -EINVAL;
  761. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  762. return 0;
  763. }
  764. static int imx258_enum_frame_size(struct v4l2_subdev *sd,
  765. struct v4l2_subdev_state *sd_state,
  766. struct v4l2_subdev_frame_size_enum *fse)
  767. {
  768. if (fse->index >= ARRAY_SIZE(supported_modes))
  769. return -EINVAL;
  770. if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  771. return -EINVAL;
  772. fse->min_width = supported_modes[fse->index].width;
  773. fse->max_width = fse->min_width;
  774. fse->min_height = supported_modes[fse->index].height;
  775. fse->max_height = fse->min_height;
  776. return 0;
  777. }
  778. static void imx258_update_pad_format(const struct imx258_mode *mode,
  779. struct v4l2_subdev_format *fmt)
  780. {
  781. fmt->format.width = mode->width;
  782. fmt->format.height = mode->height;
  783. fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  784. fmt->format.field = V4L2_FIELD_NONE;
  785. }
  786. static int __imx258_get_pad_format(struct imx258 *imx258,
  787. struct v4l2_subdev_state *sd_state,
  788. struct v4l2_subdev_format *fmt)
  789. {
  790. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  791. fmt->format = *v4l2_subdev_get_try_format(&imx258->sd,
  792. sd_state,
  793. fmt->pad);
  794. else
  795. imx258_update_pad_format(imx258->cur_mode, fmt);
  796. return 0;
  797. }
  798. static int imx258_get_pad_format(struct v4l2_subdev *sd,
  799. struct v4l2_subdev_state *sd_state,
  800. struct v4l2_subdev_format *fmt)
  801. {
  802. struct imx258 *imx258 = to_imx258(sd);
  803. int ret;
  804. mutex_lock(&imx258->mutex);
  805. ret = __imx258_get_pad_format(imx258, sd_state, fmt);
  806. mutex_unlock(&imx258->mutex);
  807. return ret;
  808. }
  809. static int imx258_set_pad_format(struct v4l2_subdev *sd,
  810. struct v4l2_subdev_state *sd_state,
  811. struct v4l2_subdev_format *fmt)
  812. {
  813. struct imx258 *imx258 = to_imx258(sd);
  814. const struct imx258_mode *mode;
  815. struct v4l2_mbus_framefmt *framefmt;
  816. s32 vblank_def;
  817. s32 vblank_min;
  818. s64 h_blank;
  819. s64 pixel_rate;
  820. s64 link_freq;
  821. mutex_lock(&imx258->mutex);
  822. /* Only one raw bayer(GBRG) order is supported */
  823. fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  824. mode = v4l2_find_nearest_size(supported_modes,
  825. ARRAY_SIZE(supported_modes), width, height,
  826. fmt->format.width, fmt->format.height);
  827. imx258_update_pad_format(mode, fmt);
  828. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  829. framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  830. *framefmt = fmt->format;
  831. } else {
  832. imx258->cur_mode = mode;
  833. __v4l2_ctrl_s_ctrl(imx258->link_freq, mode->link_freq_index);
  834. link_freq = link_freq_menu_items[mode->link_freq_index];
  835. pixel_rate = link_freq_to_pixel_rate(link_freq);
  836. __v4l2_ctrl_s_ctrl_int64(imx258->pixel_rate, pixel_rate);
  837. /* Update limits and set FPS to default */
  838. vblank_def = imx258->cur_mode->vts_def -
  839. imx258->cur_mode->height;
  840. vblank_min = imx258->cur_mode->vts_min -
  841. imx258->cur_mode->height;
  842. __v4l2_ctrl_modify_range(
  843. imx258->vblank, vblank_min,
  844. IMX258_VTS_MAX - imx258->cur_mode->height, 1,
  845. vblank_def);
  846. __v4l2_ctrl_s_ctrl(imx258->vblank, vblank_def);
  847. h_blank =
  848. link_freq_configs[mode->link_freq_index].pixels_per_line
  849. - imx258->cur_mode->width;
  850. __v4l2_ctrl_modify_range(imx258->hblank, h_blank,
  851. h_blank, 1, h_blank);
  852. }
  853. mutex_unlock(&imx258->mutex);
  854. return 0;
  855. }
  856. /* Start streaming */
  857. static int imx258_start_streaming(struct imx258 *imx258)
  858. {
  859. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  860. const struct imx258_reg_list *reg_list;
  861. int ret, link_freq_index;
  862. /* Setup PLL */
  863. link_freq_index = imx258->cur_mode->link_freq_index;
  864. reg_list = &link_freq_configs[link_freq_index].reg_list;
  865. ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
  866. if (ret) {
  867. dev_err(&client->dev, "%s failed to set plls\n", __func__);
  868. return ret;
  869. }
  870. /* Apply default values of current mode */
  871. reg_list = &imx258->cur_mode->reg_list;
  872. ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
  873. if (ret) {
  874. dev_err(&client->dev, "%s failed to set mode\n", __func__);
  875. return ret;
  876. }
  877. /* Set Orientation be 180 degree */
  878. ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
  879. IMX258_REG_VALUE_08BIT, REG_CONFIG_MIRROR_FLIP);
  880. if (ret) {
  881. dev_err(&client->dev, "%s failed to set orientation\n",
  882. __func__);
  883. return ret;
  884. }
  885. /* Apply customized values from user */
  886. ret = __v4l2_ctrl_handler_setup(imx258->sd.ctrl_handler);
  887. if (ret)
  888. return ret;
  889. /* set stream on register */
  890. return imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
  891. IMX258_REG_VALUE_08BIT,
  892. IMX258_MODE_STREAMING);
  893. }
  894. /* Stop streaming */
  895. static int imx258_stop_streaming(struct imx258 *imx258)
  896. {
  897. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  898. int ret;
  899. /* set stream off register */
  900. ret = imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
  901. IMX258_REG_VALUE_08BIT, IMX258_MODE_STANDBY);
  902. if (ret)
  903. dev_err(&client->dev, "%s failed to set stream\n", __func__);
  904. /*
  905. * Return success even if it was an error, as there is nothing the
  906. * caller can do about it.
  907. */
  908. return 0;
  909. }
  910. static int imx258_power_on(struct device *dev)
  911. {
  912. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  913. struct imx258 *imx258 = to_imx258(sd);
  914. int ret;
  915. ret = clk_prepare_enable(imx258->clk);
  916. if (ret)
  917. dev_err(dev, "failed to enable clock\n");
  918. return ret;
  919. }
  920. static int imx258_power_off(struct device *dev)
  921. {
  922. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  923. struct imx258 *imx258 = to_imx258(sd);
  924. clk_disable_unprepare(imx258->clk);
  925. return 0;
  926. }
  927. static int imx258_set_stream(struct v4l2_subdev *sd, int enable)
  928. {
  929. struct imx258 *imx258 = to_imx258(sd);
  930. struct i2c_client *client = v4l2_get_subdevdata(sd);
  931. int ret = 0;
  932. mutex_lock(&imx258->mutex);
  933. if (imx258->streaming == enable) {
  934. mutex_unlock(&imx258->mutex);
  935. return 0;
  936. }
  937. if (enable) {
  938. ret = pm_runtime_resume_and_get(&client->dev);
  939. if (ret < 0)
  940. goto err_unlock;
  941. /*
  942. * Apply default & customized values
  943. * and then start streaming.
  944. */
  945. ret = imx258_start_streaming(imx258);
  946. if (ret)
  947. goto err_rpm_put;
  948. } else {
  949. imx258_stop_streaming(imx258);
  950. pm_runtime_put(&client->dev);
  951. }
  952. imx258->streaming = enable;
  953. mutex_unlock(&imx258->mutex);
  954. return ret;
  955. err_rpm_put:
  956. pm_runtime_put(&client->dev);
  957. err_unlock:
  958. mutex_unlock(&imx258->mutex);
  959. return ret;
  960. }
  961. static int __maybe_unused imx258_suspend(struct device *dev)
  962. {
  963. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  964. struct imx258 *imx258 = to_imx258(sd);
  965. if (imx258->streaming)
  966. imx258_stop_streaming(imx258);
  967. return 0;
  968. }
  969. static int __maybe_unused imx258_resume(struct device *dev)
  970. {
  971. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  972. struct imx258 *imx258 = to_imx258(sd);
  973. int ret;
  974. if (imx258->streaming) {
  975. ret = imx258_start_streaming(imx258);
  976. if (ret)
  977. goto error;
  978. }
  979. return 0;
  980. error:
  981. imx258_stop_streaming(imx258);
  982. imx258->streaming = 0;
  983. return ret;
  984. }
  985. /* Verify chip ID */
  986. static int imx258_identify_module(struct imx258 *imx258)
  987. {
  988. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  989. int ret;
  990. u32 val;
  991. ret = imx258_read_reg(imx258, IMX258_REG_CHIP_ID,
  992. IMX258_REG_VALUE_16BIT, &val);
  993. if (ret) {
  994. dev_err(&client->dev, "failed to read chip id %x\n",
  995. IMX258_CHIP_ID);
  996. return ret;
  997. }
  998. if (val != IMX258_CHIP_ID) {
  999. dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
  1000. IMX258_CHIP_ID, val);
  1001. return -EIO;
  1002. }
  1003. return 0;
  1004. }
  1005. static const struct v4l2_subdev_video_ops imx258_video_ops = {
  1006. .s_stream = imx258_set_stream,
  1007. };
  1008. static const struct v4l2_subdev_pad_ops imx258_pad_ops = {
  1009. .enum_mbus_code = imx258_enum_mbus_code,
  1010. .get_fmt = imx258_get_pad_format,
  1011. .set_fmt = imx258_set_pad_format,
  1012. .enum_frame_size = imx258_enum_frame_size,
  1013. };
  1014. static const struct v4l2_subdev_ops imx258_subdev_ops = {
  1015. .video = &imx258_video_ops,
  1016. .pad = &imx258_pad_ops,
  1017. };
  1018. static const struct v4l2_subdev_internal_ops imx258_internal_ops = {
  1019. .open = imx258_open,
  1020. };
  1021. /* Initialize control handlers */
  1022. static int imx258_init_controls(struct imx258 *imx258)
  1023. {
  1024. struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
  1025. struct v4l2_ctrl_handler *ctrl_hdlr;
  1026. s64 vblank_def;
  1027. s64 vblank_min;
  1028. s64 pixel_rate_min;
  1029. s64 pixel_rate_max;
  1030. int ret;
  1031. ctrl_hdlr = &imx258->ctrl_handler;
  1032. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
  1033. if (ret)
  1034. return ret;
  1035. mutex_init(&imx258->mutex);
  1036. ctrl_hdlr->lock = &imx258->mutex;
  1037. imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  1038. &imx258_ctrl_ops,
  1039. V4L2_CID_LINK_FREQ,
  1040. ARRAY_SIZE(link_freq_menu_items) - 1,
  1041. 0,
  1042. link_freq_menu_items);
  1043. if (imx258->link_freq)
  1044. imx258->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1045. pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]);
  1046. pixel_rate_min = link_freq_to_pixel_rate(link_freq_menu_items[1]);
  1047. /* By default, PIXEL_RATE is read only */
  1048. imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
  1049. V4L2_CID_PIXEL_RATE,
  1050. pixel_rate_min, pixel_rate_max,
  1051. 1, pixel_rate_max);
  1052. vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
  1053. vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
  1054. imx258->vblank = v4l2_ctrl_new_std(
  1055. ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
  1056. vblank_min,
  1057. IMX258_VTS_MAX - imx258->cur_mode->height, 1,
  1058. vblank_def);
  1059. if (imx258->vblank)
  1060. imx258->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1061. imx258->hblank = v4l2_ctrl_new_std(
  1062. ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
  1063. IMX258_PPL_DEFAULT - imx258->cur_mode->width,
  1064. IMX258_PPL_DEFAULT - imx258->cur_mode->width,
  1065. 1,
  1066. IMX258_PPL_DEFAULT - imx258->cur_mode->width);
  1067. if (imx258->hblank)
  1068. imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1069. imx258->exposure = v4l2_ctrl_new_std(
  1070. ctrl_hdlr, &imx258_ctrl_ops,
  1071. V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
  1072. IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
  1073. IMX258_EXPOSURE_DEFAULT);
  1074. v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  1075. IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
  1076. IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
  1077. v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  1078. IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
  1079. IMX258_DGTL_GAIN_STEP,
  1080. IMX258_DGTL_GAIN_DEFAULT);
  1081. v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_WIDE_DYNAMIC_RANGE,
  1082. 0, 1, 1, IMX258_HDR_RATIO_DEFAULT);
  1083. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
  1084. V4L2_CID_TEST_PATTERN,
  1085. ARRAY_SIZE(imx258_test_pattern_menu) - 1,
  1086. 0, 0, imx258_test_pattern_menu);
  1087. if (ctrl_hdlr->error) {
  1088. ret = ctrl_hdlr->error;
  1089. dev_err(&client->dev, "%s control init failed (%d)\n",
  1090. __func__, ret);
  1091. goto error;
  1092. }
  1093. imx258->sd.ctrl_handler = ctrl_hdlr;
  1094. return 0;
  1095. error:
  1096. v4l2_ctrl_handler_free(ctrl_hdlr);
  1097. mutex_destroy(&imx258->mutex);
  1098. return ret;
  1099. }
  1100. static void imx258_free_controls(struct imx258 *imx258)
  1101. {
  1102. v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
  1103. mutex_destroy(&imx258->mutex);
  1104. }
  1105. static int imx258_probe(struct i2c_client *client)
  1106. {
  1107. struct imx258 *imx258;
  1108. int ret;
  1109. u32 val = 0;
  1110. imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
  1111. if (!imx258)
  1112. return -ENOMEM;
  1113. imx258->clk = devm_clk_get_optional(&client->dev, NULL);
  1114. if (IS_ERR(imx258->clk))
  1115. return dev_err_probe(&client->dev, PTR_ERR(imx258->clk),
  1116. "error getting clock\n");
  1117. if (!imx258->clk) {
  1118. dev_dbg(&client->dev,
  1119. "no clock provided, using clock-frequency property\n");
  1120. device_property_read_u32(&client->dev, "clock-frequency", &val);
  1121. } else {
  1122. val = clk_get_rate(imx258->clk);
  1123. }
  1124. if (val != IMX258_INPUT_CLOCK_FREQ) {
  1125. dev_err(&client->dev, "input clock frequency not supported\n");
  1126. return -EINVAL;
  1127. }
  1128. /*
  1129. * Check that the device is mounted upside down. The driver only
  1130. * supports a single pixel order right now.
  1131. */
  1132. ret = device_property_read_u32(&client->dev, "rotation", &val);
  1133. if (ret || val != 180)
  1134. return -EINVAL;
  1135. /* Initialize subdev */
  1136. v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
  1137. /* Will be powered off via pm_runtime_idle */
  1138. ret = imx258_power_on(&client->dev);
  1139. if (ret)
  1140. return ret;
  1141. /* Check module identity */
  1142. ret = imx258_identify_module(imx258);
  1143. if (ret)
  1144. goto error_identify;
  1145. /* Set default mode to max resolution */
  1146. imx258->cur_mode = &supported_modes[0];
  1147. ret = imx258_init_controls(imx258);
  1148. if (ret)
  1149. goto error_identify;
  1150. /* Initialize subdev */
  1151. imx258->sd.internal_ops = &imx258_internal_ops;
  1152. imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1153. imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1154. /* Initialize source pad */
  1155. imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
  1156. ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
  1157. if (ret)
  1158. goto error_handler_free;
  1159. ret = v4l2_async_register_subdev_sensor(&imx258->sd);
  1160. if (ret < 0)
  1161. goto error_media_entity;
  1162. pm_runtime_set_active(&client->dev);
  1163. pm_runtime_enable(&client->dev);
  1164. pm_runtime_idle(&client->dev);
  1165. return 0;
  1166. error_media_entity:
  1167. media_entity_cleanup(&imx258->sd.entity);
  1168. error_handler_free:
  1169. imx258_free_controls(imx258);
  1170. error_identify:
  1171. imx258_power_off(&client->dev);
  1172. return ret;
  1173. }
  1174. static void imx258_remove(struct i2c_client *client)
  1175. {
  1176. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1177. struct imx258 *imx258 = to_imx258(sd);
  1178. v4l2_async_unregister_subdev(sd);
  1179. media_entity_cleanup(&sd->entity);
  1180. imx258_free_controls(imx258);
  1181. pm_runtime_disable(&client->dev);
  1182. if (!pm_runtime_status_suspended(&client->dev))
  1183. imx258_power_off(&client->dev);
  1184. pm_runtime_set_suspended(&client->dev);
  1185. }
  1186. static const struct dev_pm_ops imx258_pm_ops = {
  1187. SET_SYSTEM_SLEEP_PM_OPS(imx258_suspend, imx258_resume)
  1188. SET_RUNTIME_PM_OPS(imx258_power_off, imx258_power_on, NULL)
  1189. };
  1190. #ifdef CONFIG_ACPI
  1191. static const struct acpi_device_id imx258_acpi_ids[] = {
  1192. { "SONY258A" },
  1193. { /* sentinel */ }
  1194. };
  1195. MODULE_DEVICE_TABLE(acpi, imx258_acpi_ids);
  1196. #endif
  1197. static const struct of_device_id imx258_dt_ids[] = {
  1198. { .compatible = "sony,imx258" },
  1199. { /* sentinel */ }
  1200. };
  1201. MODULE_DEVICE_TABLE(of, imx258_dt_ids);
  1202. static struct i2c_driver imx258_i2c_driver = {
  1203. .driver = {
  1204. .name = "imx258",
  1205. .pm = &imx258_pm_ops,
  1206. .acpi_match_table = ACPI_PTR(imx258_acpi_ids),
  1207. .of_match_table = imx258_dt_ids,
  1208. },
  1209. .probe_new = imx258_probe,
  1210. .remove = imx258_remove,
  1211. };
  1212. module_i2c_driver(imx258_i2c_driver);
  1213. MODULE_AUTHOR("Yeh, Andy <[email protected]>");
  1214. MODULE_AUTHOR("Chiang, Alan");
  1215. MODULE_AUTHOR("Chen, Jason");
  1216. MODULE_DESCRIPTION("Sony IMX258 sensor driver");
  1217. MODULE_LICENSE("GPL v2");