imx258.c 30 KB

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