imx274.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /*
  2. * imx274.c - IMX274 CMOS Image Sensor driver
  3. *
  4. * Copyright (C) 2017, Leopard Imaging, Inc.
  5. *
  6. * Leon Luo <leonl@leopardimaging.com>
  7. * Edwin Zou <edwinz@leopardimaging.com>
  8. * Luca Ceresoli <luca@lucaceresoli.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/gpio.h>
  25. #include <linux/gpio/consumer.h>
  26. #include <linux/i2c.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/regmap.h>
  32. #include <linux/slab.h>
  33. #include <linux/v4l2-mediabus.h>
  34. #include <linux/videodev2.h>
  35. #include <media/v4l2-ctrls.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-subdev.h>
  38. /*
  39. * See "SHR, SVR Setting" in datasheet
  40. */
  41. #define IMX274_DEFAULT_FRAME_LENGTH (4550)
  42. #define IMX274_MAX_FRAME_LENGTH (0x000fffff)
  43. /*
  44. * See "Frame Rate Adjustment" in datasheet
  45. */
  46. #define IMX274_PIXCLK_CONST1 (72000000)
  47. #define IMX274_PIXCLK_CONST2 (1000000)
  48. /*
  49. * The input gain is shifted by IMX274_GAIN_SHIFT to get
  50. * decimal number. The real gain is
  51. * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
  52. */
  53. #define IMX274_GAIN_SHIFT (8)
  54. #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
  55. /*
  56. * See "Analog Gain" and "Digital Gain" in datasheet
  57. * min gain is 1X
  58. * max gain is calculated based on IMX274_GAIN_REG_MAX
  59. */
  60. #define IMX274_GAIN_REG_MAX (1957)
  61. #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
  62. #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
  63. / (2048 - IMX274_GAIN_REG_MAX))
  64. #define IMX274_MAX_DIGITAL_GAIN (8)
  65. #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
  66. #define IMX274_GAIN_CONST (2048) /* for gain formula */
  67. /*
  68. * 1 line time in us = (HMAX / 72), minimal is 4 lines
  69. */
  70. #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
  71. #define IMX274_DEFAULT_MODE IMX274_BINNING_OFF
  72. #define IMX274_MAX_WIDTH (3840)
  73. #define IMX274_MAX_HEIGHT (2160)
  74. #define IMX274_MAX_FRAME_RATE (120)
  75. #define IMX274_MIN_FRAME_RATE (5)
  76. #define IMX274_DEF_FRAME_RATE (60)
  77. /*
  78. * register SHR is limited to (SVR value + 1) x VMAX value - 4
  79. */
  80. #define IMX274_SHR_LIMIT_CONST (4)
  81. /*
  82. * Min and max sensor reset delay (microseconds)
  83. */
  84. #define IMX274_RESET_DELAY1 (2000)
  85. #define IMX274_RESET_DELAY2 (2200)
  86. /*
  87. * shift and mask constants
  88. */
  89. #define IMX274_SHIFT_8_BITS (8)
  90. #define IMX274_SHIFT_16_BITS (16)
  91. #define IMX274_MASK_LSB_2_BITS (0x03)
  92. #define IMX274_MASK_LSB_3_BITS (0x07)
  93. #define IMX274_MASK_LSB_4_BITS (0x0f)
  94. #define IMX274_MASK_LSB_8_BITS (0x00ff)
  95. #define DRIVER_NAME "IMX274"
  96. /*
  97. * IMX274 register definitions
  98. */
  99. #define IMX274_SHR_REG_MSB 0x300D /* SHR */
  100. #define IMX274_SHR_REG_LSB 0x300C /* SHR */
  101. #define IMX274_SVR_REG_MSB 0x300F /* SVR */
  102. #define IMX274_SVR_REG_LSB 0x300E /* SVR */
  103. #define IMX274_HTRIM_EN_REG 0x3037
  104. #define IMX274_HTRIM_START_REG_LSB 0x3038
  105. #define IMX274_HTRIM_START_REG_MSB 0x3039
  106. #define IMX274_HTRIM_END_REG_LSB 0x303A
  107. #define IMX274_HTRIM_END_REG_MSB 0x303B
  108. #define IMX274_VWIDCUTEN_REG 0x30DD
  109. #define IMX274_VWIDCUT_REG_LSB 0x30DE
  110. #define IMX274_VWIDCUT_REG_MSB 0x30DF
  111. #define IMX274_VWINPOS_REG_LSB 0x30E0
  112. #define IMX274_VWINPOS_REG_MSB 0x30E1
  113. #define IMX274_WRITE_VSIZE_REG_LSB 0x3130
  114. #define IMX274_WRITE_VSIZE_REG_MSB 0x3131
  115. #define IMX274_Y_OUT_SIZE_REG_LSB 0x3132
  116. #define IMX274_Y_OUT_SIZE_REG_MSB 0x3133
  117. #define IMX274_VMAX_REG_1 0x30FA /* VMAX, MSB */
  118. #define IMX274_VMAX_REG_2 0x30F9 /* VMAX */
  119. #define IMX274_VMAX_REG_3 0x30F8 /* VMAX, LSB */
  120. #define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */
  121. #define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */
  122. #define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */
  123. #define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */
  124. #define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */
  125. #define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */
  126. #define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */
  127. #define IMX274_STANDBY_REG 0x3000 /* STANDBY */
  128. #define IMX274_TABLE_WAIT_MS 0
  129. #define IMX274_TABLE_END 1
  130. /*
  131. * imx274 I2C operation related structure
  132. */
  133. struct reg_8 {
  134. u16 addr;
  135. u8 val;
  136. };
  137. static const struct regmap_config imx274_regmap_config = {
  138. .reg_bits = 16,
  139. .val_bits = 8,
  140. .cache_type = REGCACHE_RBTREE,
  141. };
  142. enum imx274_binning {
  143. IMX274_BINNING_OFF,
  144. IMX274_BINNING_2_1,
  145. IMX274_BINNING_3_1,
  146. };
  147. /*
  148. * Parameters for each imx274 readout mode.
  149. *
  150. * These are the values to configure the sensor in one of the
  151. * implemented modes.
  152. *
  153. * @init_regs: registers to initialize the mode
  154. * @bin_ratio: downscale factor (e.g. 3 for 3:1 binning)
  155. * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
  156. * Adjustment (CSI-2)" in the datasheet)
  157. * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
  158. * datasheet)
  159. * @max_fps: Maximum frames per second
  160. * @nocpiop: Number of clocks per internal offset period (see "Integration Time
  161. * in Each Readout Drive Mode (CSI-2)" in the datasheet)
  162. */
  163. struct imx274_frmfmt {
  164. const struct reg_8 *init_regs;
  165. unsigned int bin_ratio;
  166. int min_frame_len;
  167. int min_SHR;
  168. int max_fps;
  169. int nocpiop;
  170. };
  171. /*
  172. * imx274 test pattern related structure
  173. */
  174. enum {
  175. TEST_PATTERN_DISABLED = 0,
  176. TEST_PATTERN_ALL_000H,
  177. TEST_PATTERN_ALL_FFFH,
  178. TEST_PATTERN_ALL_555H,
  179. TEST_PATTERN_ALL_AAAH,
  180. TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */
  181. TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */
  182. TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */
  183. TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */
  184. TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */
  185. TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */
  186. TEST_PATTERN_H_COLOR_BARS,
  187. TEST_PATTERN_V_COLOR_BARS,
  188. };
  189. static const char * const tp_qmenu[] = {
  190. "Disabled",
  191. "All 000h Pattern",
  192. "All FFFh Pattern",
  193. "All 555h Pattern",
  194. "All AAAh Pattern",
  195. "Vertical Stripe (555h / AAAh)",
  196. "Vertical Stripe (AAAh / 555h)",
  197. "Vertical Stripe (000h / 555h)",
  198. "Vertical Stripe (555h / 000h)",
  199. "Vertical Stripe (000h / FFFh)",
  200. "Vertical Stripe (FFFh / 000h)",
  201. "Horizontal Color Bars",
  202. "Vertical Color Bars",
  203. };
  204. /*
  205. * All-pixel scan mode (10-bit)
  206. * imx274 mode1(refer to datasheet) register configuration with
  207. * 3840x2160 resolution, raw10 data and mipi four lane output
  208. */
  209. static const struct reg_8 imx274_mode1_3840x2160_raw10[] = {
  210. {0x3004, 0x01},
  211. {0x3005, 0x01},
  212. {0x3006, 0x00},
  213. {0x3007, 0xa2},
  214. {0x3018, 0xA2}, /* output XVS, HVS */
  215. {0x306B, 0x05},
  216. {0x30E2, 0x01},
  217. {0x30EE, 0x01},
  218. {0x3342, 0x0A},
  219. {0x3343, 0x00},
  220. {0x3344, 0x16},
  221. {0x3345, 0x00},
  222. {0x33A6, 0x01},
  223. {0x3528, 0x0E},
  224. {0x3554, 0x1F},
  225. {0x3555, 0x01},
  226. {0x3556, 0x01},
  227. {0x3557, 0x01},
  228. {0x3558, 0x01},
  229. {0x3559, 0x00},
  230. {0x355A, 0x00},
  231. {0x35BA, 0x0E},
  232. {0x366A, 0x1B},
  233. {0x366B, 0x1A},
  234. {0x366C, 0x19},
  235. {0x366D, 0x17},
  236. {0x3A41, 0x08},
  237. {IMX274_TABLE_END, 0x00}
  238. };
  239. /*
  240. * Horizontal/vertical 2/2-line binning
  241. * (Horizontal and vertical weightedbinning, 10-bit)
  242. * imx274 mode3(refer to datasheet) register configuration with
  243. * 1920x1080 resolution, raw10 data and mipi four lane output
  244. */
  245. static const struct reg_8 imx274_mode3_1920x1080_raw10[] = {
  246. {0x3004, 0x02},
  247. {0x3005, 0x21},
  248. {0x3006, 0x00},
  249. {0x3007, 0xb1},
  250. {0x3018, 0xA2}, /* output XVS, HVS */
  251. {0x306B, 0x05},
  252. {0x30E2, 0x02},
  253. {0x30EE, 0x01},
  254. {0x3342, 0x0A},
  255. {0x3343, 0x00},
  256. {0x3344, 0x1A},
  257. {0x3345, 0x00},
  258. {0x33A6, 0x01},
  259. {0x3528, 0x0E},
  260. {0x3554, 0x00},
  261. {0x3555, 0x01},
  262. {0x3556, 0x01},
  263. {0x3557, 0x01},
  264. {0x3558, 0x01},
  265. {0x3559, 0x00},
  266. {0x355A, 0x00},
  267. {0x35BA, 0x0E},
  268. {0x366A, 0x1B},
  269. {0x366B, 0x1A},
  270. {0x366C, 0x19},
  271. {0x366D, 0x17},
  272. {0x3A41, 0x08},
  273. {IMX274_TABLE_END, 0x00}
  274. };
  275. /*
  276. * Vertical 2/3 subsampling binning horizontal 3 binning
  277. * imx274 mode5(refer to datasheet) register configuration with
  278. * 1280x720 resolution, raw10 data and mipi four lane output
  279. */
  280. static const struct reg_8 imx274_mode5_1280x720_raw10[] = {
  281. {0x3004, 0x03},
  282. {0x3005, 0x31},
  283. {0x3006, 0x00},
  284. {0x3007, 0xa9},
  285. {0x3018, 0xA2}, /* output XVS, HVS */
  286. {0x306B, 0x05},
  287. {0x30E2, 0x03},
  288. {0x30EE, 0x01},
  289. {0x3342, 0x0A},
  290. {0x3343, 0x00},
  291. {0x3344, 0x1B},
  292. {0x3345, 0x00},
  293. {0x33A6, 0x01},
  294. {0x3528, 0x0E},
  295. {0x3554, 0x00},
  296. {0x3555, 0x01},
  297. {0x3556, 0x01},
  298. {0x3557, 0x01},
  299. {0x3558, 0x01},
  300. {0x3559, 0x00},
  301. {0x355A, 0x00},
  302. {0x35BA, 0x0E},
  303. {0x366A, 0x1B},
  304. {0x366B, 0x19},
  305. {0x366C, 0x17},
  306. {0x366D, 0x17},
  307. {0x3A41, 0x04},
  308. {IMX274_TABLE_END, 0x00}
  309. };
  310. /*
  311. * imx274 first step register configuration for
  312. * starting stream
  313. */
  314. static const struct reg_8 imx274_start_1[] = {
  315. {IMX274_STANDBY_REG, 0x12},
  316. {IMX274_TABLE_END, 0x00}
  317. };
  318. /*
  319. * imx274 second step register configuration for
  320. * starting stream
  321. */
  322. static const struct reg_8 imx274_start_2[] = {
  323. {0x3120, 0xF0}, /* clock settings */
  324. {0x3121, 0x00}, /* clock settings */
  325. {0x3122, 0x02}, /* clock settings */
  326. {0x3129, 0x9C}, /* clock settings */
  327. {0x312A, 0x02}, /* clock settings */
  328. {0x312D, 0x02}, /* clock settings */
  329. {0x310B, 0x00},
  330. /* PLSTMG */
  331. {0x304C, 0x00}, /* PLSTMG01 */
  332. {0x304D, 0x03},
  333. {0x331C, 0x1A},
  334. {0x331D, 0x00},
  335. {0x3502, 0x02},
  336. {0x3529, 0x0E},
  337. {0x352A, 0x0E},
  338. {0x352B, 0x0E},
  339. {0x3538, 0x0E},
  340. {0x3539, 0x0E},
  341. {0x3553, 0x00},
  342. {0x357D, 0x05},
  343. {0x357F, 0x05},
  344. {0x3581, 0x04},
  345. {0x3583, 0x76},
  346. {0x3587, 0x01},
  347. {0x35BB, 0x0E},
  348. {0x35BC, 0x0E},
  349. {0x35BD, 0x0E},
  350. {0x35BE, 0x0E},
  351. {0x35BF, 0x0E},
  352. {0x366E, 0x00},
  353. {0x366F, 0x00},
  354. {0x3670, 0x00},
  355. {0x3671, 0x00},
  356. /* PSMIPI */
  357. {0x3304, 0x32}, /* PSMIPI1 */
  358. {0x3305, 0x00},
  359. {0x3306, 0x32},
  360. {0x3307, 0x00},
  361. {0x3590, 0x32},
  362. {0x3591, 0x00},
  363. {0x3686, 0x32},
  364. {0x3687, 0x00},
  365. {IMX274_TABLE_END, 0x00}
  366. };
  367. /*
  368. * imx274 third step register configuration for
  369. * starting stream
  370. */
  371. static const struct reg_8 imx274_start_3[] = {
  372. {IMX274_STANDBY_REG, 0x00},
  373. {0x303E, 0x02}, /* SYS_MODE = 2 */
  374. {IMX274_TABLE_END, 0x00}
  375. };
  376. /*
  377. * imx274 forth step register configuration for
  378. * starting stream
  379. */
  380. static const struct reg_8 imx274_start_4[] = {
  381. {0x30F4, 0x00},
  382. {0x3018, 0xA2}, /* XHS VHS OUTUPT */
  383. {IMX274_TABLE_END, 0x00}
  384. };
  385. /*
  386. * imx274 register configuration for stoping stream
  387. */
  388. static const struct reg_8 imx274_stop[] = {
  389. {IMX274_STANDBY_REG, 0x01},
  390. {IMX274_TABLE_END, 0x00}
  391. };
  392. /*
  393. * imx274 disable test pattern register configuration
  394. */
  395. static const struct reg_8 imx274_tp_disabled[] = {
  396. {0x303C, 0x00},
  397. {0x377F, 0x00},
  398. {0x3781, 0x00},
  399. {0x370B, 0x00},
  400. {IMX274_TABLE_END, 0x00}
  401. };
  402. /*
  403. * imx274 test pattern register configuration
  404. * reg 0x303D defines the test pattern modes
  405. */
  406. static const struct reg_8 imx274_tp_regs[] = {
  407. {0x303C, 0x11},
  408. {0x370E, 0x01},
  409. {0x377F, 0x01},
  410. {0x3781, 0x01},
  411. {0x370B, 0x11},
  412. {IMX274_TABLE_END, 0x00}
  413. };
  414. /* nocpiop happens to be the same number for the implemented modes */
  415. static const struct imx274_frmfmt imx274_formats[] = {
  416. {
  417. /* mode 1, 4K */
  418. .bin_ratio = 1,
  419. .init_regs = imx274_mode1_3840x2160_raw10,
  420. .min_frame_len = 4550,
  421. .min_SHR = 12,
  422. .max_fps = 60,
  423. .nocpiop = 112,
  424. },
  425. {
  426. /* mode 3, 1080p */
  427. .bin_ratio = 2,
  428. .init_regs = imx274_mode3_1920x1080_raw10,
  429. .min_frame_len = 2310,
  430. .min_SHR = 8,
  431. .max_fps = 120,
  432. .nocpiop = 112,
  433. },
  434. {
  435. /* mode 5, 720p */
  436. .bin_ratio = 3,
  437. .init_regs = imx274_mode5_1280x720_raw10,
  438. .min_frame_len = 2310,
  439. .min_SHR = 8,
  440. .max_fps = 120,
  441. .nocpiop = 112,
  442. },
  443. };
  444. /*
  445. * struct imx274_ctrls - imx274 ctrl structure
  446. * @handler: V4L2 ctrl handler structure
  447. * @exposure: Pointer to expsure ctrl structure
  448. * @gain: Pointer to gain ctrl structure
  449. * @vflip: Pointer to vflip ctrl structure
  450. * @test_pattern: Pointer to test pattern ctrl structure
  451. */
  452. struct imx274_ctrls {
  453. struct v4l2_ctrl_handler handler;
  454. struct v4l2_ctrl *exposure;
  455. struct v4l2_ctrl *gain;
  456. struct v4l2_ctrl *vflip;
  457. struct v4l2_ctrl *test_pattern;
  458. };
  459. /*
  460. * struct stim274 - imx274 device structure
  461. * @sd: V4L2 subdevice structure
  462. * @pad: Media pad structure
  463. * @client: Pointer to I2C client
  464. * @ctrls: imx274 control structure
  465. * @crop: rect to be captured
  466. * @compose: compose rect, i.e. output resolution
  467. * @format: V4L2 media bus frame format structure
  468. * (width and height are in sync with the compose rect)
  469. * @frame_rate: V4L2 frame rate structure
  470. * @regmap: Pointer to regmap structure
  471. * @reset_gpio: Pointer to reset gpio
  472. * @lock: Mutex structure
  473. * @mode: Parameters for the selected readout mode
  474. */
  475. struct stimx274 {
  476. struct v4l2_subdev sd;
  477. struct media_pad pad;
  478. struct i2c_client *client;
  479. struct imx274_ctrls ctrls;
  480. struct v4l2_rect crop;
  481. struct v4l2_mbus_framefmt format;
  482. struct v4l2_fract frame_interval;
  483. struct regmap *regmap;
  484. struct gpio_desc *reset_gpio;
  485. struct mutex lock; /* mutex lock for operations */
  486. const struct imx274_frmfmt *mode;
  487. };
  488. #define IMX274_ROUND(dim, step, flags) \
  489. ((flags) & V4L2_SEL_FLAG_GE \
  490. ? roundup((dim), (step)) \
  491. : ((flags) & V4L2_SEL_FLAG_LE \
  492. ? rounddown((dim), (step)) \
  493. : rounddown((dim) + (step) / 2, (step))))
  494. /*
  495. * Function declaration
  496. */
  497. static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl);
  498. static int imx274_set_exposure(struct stimx274 *priv, int val);
  499. static int imx274_set_vflip(struct stimx274 *priv, int val);
  500. static int imx274_set_test_pattern(struct stimx274 *priv, int val);
  501. static int imx274_set_frame_interval(struct stimx274 *priv,
  502. struct v4l2_fract frame_interval);
  503. static inline void msleep_range(unsigned int delay_base)
  504. {
  505. usleep_range(delay_base * 1000, delay_base * 1000 + 500);
  506. }
  507. /*
  508. * v4l2_ctrl and v4l2_subdev related operations
  509. */
  510. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  511. {
  512. return &container_of(ctrl->handler,
  513. struct stimx274, ctrls.handler)->sd;
  514. }
  515. static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd)
  516. {
  517. return container_of(sd, struct stimx274, sd);
  518. }
  519. /*
  520. * Writing a register table
  521. *
  522. * @priv: Pointer to device
  523. * @table: Table containing register values (with optional delays)
  524. *
  525. * This is used to write register table into sensor's reg map.
  526. *
  527. * Return: 0 on success, errors otherwise
  528. */
  529. static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[])
  530. {
  531. struct regmap *regmap = priv->regmap;
  532. int err = 0;
  533. const struct reg_8 *next;
  534. u8 val;
  535. int range_start = -1;
  536. int range_count = 0;
  537. u8 range_vals[16];
  538. int max_range_vals = ARRAY_SIZE(range_vals);
  539. for (next = table;; next++) {
  540. if ((next->addr != range_start + range_count) ||
  541. (next->addr == IMX274_TABLE_END) ||
  542. (next->addr == IMX274_TABLE_WAIT_MS) ||
  543. (range_count == max_range_vals)) {
  544. if (range_count == 1)
  545. err = regmap_write(regmap,
  546. range_start, range_vals[0]);
  547. else if (range_count > 1)
  548. err = regmap_bulk_write(regmap, range_start,
  549. &range_vals[0],
  550. range_count);
  551. else
  552. err = 0;
  553. if (err)
  554. return err;
  555. range_start = -1;
  556. range_count = 0;
  557. /* Handle special address values */
  558. if (next->addr == IMX274_TABLE_END)
  559. break;
  560. if (next->addr == IMX274_TABLE_WAIT_MS) {
  561. msleep_range(next->val);
  562. continue;
  563. }
  564. }
  565. val = next->val;
  566. if (range_start == -1)
  567. range_start = next->addr;
  568. range_vals[range_count++] = val;
  569. }
  570. return 0;
  571. }
  572. static inline int imx274_read_reg(struct stimx274 *priv, u16 addr, u8 *val)
  573. {
  574. unsigned int uint_val;
  575. int err;
  576. err = regmap_read(priv->regmap, addr, &uint_val);
  577. if (err)
  578. dev_err(&priv->client->dev,
  579. "%s : i2c read failed, addr = %x\n", __func__, addr);
  580. else
  581. dev_dbg(&priv->client->dev,
  582. "%s : addr 0x%x, val=0x%x\n", __func__,
  583. addr, uint_val);
  584. *val = uint_val;
  585. return err;
  586. }
  587. static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val)
  588. {
  589. int err;
  590. err = regmap_write(priv->regmap, addr, val);
  591. if (err)
  592. dev_err(&priv->client->dev,
  593. "%s : i2c write failed, %x = %x\n", __func__,
  594. addr, val);
  595. else
  596. dev_dbg(&priv->client->dev,
  597. "%s : addr 0x%x, val=0x%x\n", __func__,
  598. addr, val);
  599. return err;
  600. }
  601. /**
  602. * Write a multibyte register.
  603. *
  604. * Uses a bulk write where possible.
  605. *
  606. * @priv: Pointer to device structure
  607. * @addr: Address of the LSB register. Other registers must be
  608. * consecutive, least-to-most significant.
  609. * @val: Value to be written to the register (cpu endianness)
  610. * @nbytes: Number of bits to write (range: [1..3])
  611. */
  612. static int imx274_write_mbreg(struct stimx274 *priv, u16 addr, u32 val,
  613. size_t nbytes)
  614. {
  615. __le32 val_le = cpu_to_le32(val);
  616. int err;
  617. err = regmap_bulk_write(priv->regmap, addr, &val_le, nbytes);
  618. if (err)
  619. dev_err(&priv->client->dev,
  620. "%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
  621. __func__, addr, val, nbytes);
  622. else
  623. dev_dbg(&priv->client->dev,
  624. "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
  625. __func__, addr, val, nbytes);
  626. return err;
  627. }
  628. /*
  629. * Set mode registers to start stream.
  630. * @priv: Pointer to device structure
  631. *
  632. * Return: 0 on success, errors otherwise
  633. */
  634. static int imx274_mode_regs(struct stimx274 *priv)
  635. {
  636. int err = 0;
  637. err = imx274_write_table(priv, imx274_start_1);
  638. if (err)
  639. return err;
  640. err = imx274_write_table(priv, imx274_start_2);
  641. if (err)
  642. return err;
  643. err = imx274_write_table(priv, priv->mode->init_regs);
  644. return err;
  645. }
  646. /*
  647. * imx274_start_stream - Function for starting stream per mode index
  648. * @priv: Pointer to device structure
  649. *
  650. * Return: 0 on success, errors otherwise
  651. */
  652. static int imx274_start_stream(struct stimx274 *priv)
  653. {
  654. int err = 0;
  655. /*
  656. * Refer to "Standby Cancel Sequence when using CSI-2" in
  657. * imx274 datasheet, it should wait 10ms or more here.
  658. * give it 1 extra ms for margin
  659. */
  660. msleep_range(11);
  661. err = imx274_write_table(priv, imx274_start_3);
  662. if (err)
  663. return err;
  664. /*
  665. * Refer to "Standby Cancel Sequence when using CSI-2" in
  666. * imx274 datasheet, it should wait 7ms or more here.
  667. * give it 1 extra ms for margin
  668. */
  669. msleep_range(8);
  670. err = imx274_write_table(priv, imx274_start_4);
  671. if (err)
  672. return err;
  673. return 0;
  674. }
  675. /*
  676. * imx274_reset - Function called to reset the sensor
  677. * @priv: Pointer to device structure
  678. * @rst: Input value for determining the sensor's end state after reset
  679. *
  680. * Set the senor in reset and then
  681. * if rst = 0, keep it in reset;
  682. * if rst = 1, bring it out of reset.
  683. *
  684. */
  685. static void imx274_reset(struct stimx274 *priv, int rst)
  686. {
  687. gpiod_set_value_cansleep(priv->reset_gpio, 0);
  688. usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
  689. gpiod_set_value_cansleep(priv->reset_gpio, !!rst);
  690. usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
  691. }
  692. /**
  693. * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
  694. * @ctrl: V4L2 control to be set
  695. *
  696. * This function is used to set the V4L2 controls for the imx274 sensor.
  697. *
  698. * Return: 0 on success, errors otherwise
  699. */
  700. static int imx274_s_ctrl(struct v4l2_ctrl *ctrl)
  701. {
  702. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  703. struct stimx274 *imx274 = to_imx274(sd);
  704. int ret = -EINVAL;
  705. dev_dbg(&imx274->client->dev,
  706. "%s : s_ctrl: %s, value: %d\n", __func__,
  707. ctrl->name, ctrl->val);
  708. switch (ctrl->id) {
  709. case V4L2_CID_EXPOSURE:
  710. dev_dbg(&imx274->client->dev,
  711. "%s : set V4L2_CID_EXPOSURE\n", __func__);
  712. ret = imx274_set_exposure(imx274, ctrl->val);
  713. break;
  714. case V4L2_CID_GAIN:
  715. dev_dbg(&imx274->client->dev,
  716. "%s : set V4L2_CID_GAIN\n", __func__);
  717. ret = imx274_set_gain(imx274, ctrl);
  718. break;
  719. case V4L2_CID_VFLIP:
  720. dev_dbg(&imx274->client->dev,
  721. "%s : set V4L2_CID_VFLIP\n", __func__);
  722. ret = imx274_set_vflip(imx274, ctrl->val);
  723. break;
  724. case V4L2_CID_TEST_PATTERN:
  725. dev_dbg(&imx274->client->dev,
  726. "%s : set V4L2_CID_TEST_PATTERN\n", __func__);
  727. ret = imx274_set_test_pattern(imx274, ctrl->val);
  728. break;
  729. }
  730. return ret;
  731. }
  732. static int imx274_binning_goodness(struct stimx274 *imx274,
  733. int w, int ask_w,
  734. int h, int ask_h, u32 flags)
  735. {
  736. struct device *dev = &imx274->client->dev;
  737. const int goodness = 100000;
  738. int val = 0;
  739. if (flags & V4L2_SEL_FLAG_GE) {
  740. if (w < ask_w)
  741. val -= goodness;
  742. if (h < ask_h)
  743. val -= goodness;
  744. }
  745. if (flags & V4L2_SEL_FLAG_LE) {
  746. if (w > ask_w)
  747. val -= goodness;
  748. if (h > ask_h)
  749. val -= goodness;
  750. }
  751. val -= abs(w - ask_w);
  752. val -= abs(h - ask_h);
  753. dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n",
  754. __func__, ask_w, ask_h, w, h, val);
  755. return val;
  756. }
  757. /**
  758. * Helper function to change binning and set both compose and format.
  759. *
  760. * We have two entry points to change binning: set_fmt and
  761. * set_selection(COMPOSE). Both have to compute the new output size
  762. * and set it in both the compose rect and the frame format size. We
  763. * also need to do the same things after setting cropping to restore
  764. * 1:1 binning.
  765. *
  766. * This function contains the common code for these three cases, it
  767. * has many arguments in order to accommodate the needs of all of
  768. * them.
  769. *
  770. * Must be called with imx274->lock locked.
  771. *
  772. * @imx274: The device object
  773. * @cfg: The pad config we are editing for TRY requests
  774. * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
  775. * @width: Input-output parameter: set to the desired width before
  776. * the call, contains the chosen value after returning successfully
  777. * @height: Input-output parameter for height (see @width)
  778. * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not
  779. * available (when called from set_fmt)
  780. */
  781. static int __imx274_change_compose(struct stimx274 *imx274,
  782. struct v4l2_subdev_pad_config *cfg,
  783. u32 which,
  784. u32 *width,
  785. u32 *height,
  786. u32 flags)
  787. {
  788. struct device *dev = &imx274->client->dev;
  789. const struct v4l2_rect *cur_crop;
  790. struct v4l2_mbus_framefmt *tgt_fmt;
  791. unsigned int i;
  792. const struct imx274_frmfmt *best_mode = &imx274_formats[0];
  793. int best_goodness = INT_MIN;
  794. if (which == V4L2_SUBDEV_FORMAT_TRY) {
  795. cur_crop = &cfg->try_crop;
  796. tgt_fmt = &cfg->try_fmt;
  797. } else {
  798. cur_crop = &imx274->crop;
  799. tgt_fmt = &imx274->format;
  800. }
  801. for (i = 0; i < ARRAY_SIZE(imx274_formats); i++) {
  802. unsigned int ratio = imx274_formats[i].bin_ratio;
  803. int goodness = imx274_binning_goodness(
  804. imx274,
  805. cur_crop->width / ratio, *width,
  806. cur_crop->height / ratio, *height,
  807. flags);
  808. if (goodness >= best_goodness) {
  809. best_goodness = goodness;
  810. best_mode = &imx274_formats[i];
  811. }
  812. }
  813. *width = cur_crop->width / best_mode->bin_ratio;
  814. *height = cur_crop->height / best_mode->bin_ratio;
  815. if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
  816. imx274->mode = best_mode;
  817. dev_dbg(dev, "%s: selected %u:1 binning\n",
  818. __func__, best_mode->bin_ratio);
  819. tgt_fmt->width = *width;
  820. tgt_fmt->height = *height;
  821. tgt_fmt->field = V4L2_FIELD_NONE;
  822. return 0;
  823. }
  824. /**
  825. * imx274_get_fmt - Get the pad format
  826. * @sd: Pointer to V4L2 Sub device structure
  827. * @cfg: Pointer to sub device pad information structure
  828. * @fmt: Pointer to pad level media bus format
  829. *
  830. * This function is used to get the pad format information.
  831. *
  832. * Return: 0 on success
  833. */
  834. static int imx274_get_fmt(struct v4l2_subdev *sd,
  835. struct v4l2_subdev_pad_config *cfg,
  836. struct v4l2_subdev_format *fmt)
  837. {
  838. struct stimx274 *imx274 = to_imx274(sd);
  839. mutex_lock(&imx274->lock);
  840. fmt->format = imx274->format;
  841. mutex_unlock(&imx274->lock);
  842. return 0;
  843. }
  844. /**
  845. * imx274_set_fmt - This is used to set the pad format
  846. * @sd: Pointer to V4L2 Sub device structure
  847. * @cfg: Pointer to sub device pad information structure
  848. * @format: Pointer to pad level media bus format
  849. *
  850. * This function is used to set the pad format.
  851. *
  852. * Return: 0 on success
  853. */
  854. static int imx274_set_fmt(struct v4l2_subdev *sd,
  855. struct v4l2_subdev_pad_config *cfg,
  856. struct v4l2_subdev_format *format)
  857. {
  858. struct v4l2_mbus_framefmt *fmt = &format->format;
  859. struct stimx274 *imx274 = to_imx274(sd);
  860. int err = 0;
  861. mutex_lock(&imx274->lock);
  862. err = __imx274_change_compose(imx274, cfg, format->which,
  863. &fmt->width, &fmt->height, 0);
  864. if (err)
  865. goto out;
  866. /*
  867. * __imx274_change_compose already set width and height in the
  868. * applicable format, but we need to keep all other format
  869. * values, so do a full copy here
  870. */
  871. fmt->field = V4L2_FIELD_NONE;
  872. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  873. cfg->try_fmt = *fmt;
  874. else
  875. imx274->format = *fmt;
  876. out:
  877. mutex_unlock(&imx274->lock);
  878. return err;
  879. }
  880. static int imx274_get_selection(struct v4l2_subdev *sd,
  881. struct v4l2_subdev_pad_config *cfg,
  882. struct v4l2_subdev_selection *sel)
  883. {
  884. struct stimx274 *imx274 = to_imx274(sd);
  885. const struct v4l2_rect *src_crop;
  886. const struct v4l2_mbus_framefmt *src_fmt;
  887. int ret = 0;
  888. if (sel->pad != 0)
  889. return -EINVAL;
  890. if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
  891. sel->r.left = 0;
  892. sel->r.top = 0;
  893. sel->r.width = IMX274_MAX_WIDTH;
  894. sel->r.height = IMX274_MAX_HEIGHT;
  895. return 0;
  896. }
  897. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  898. src_crop = &cfg->try_crop;
  899. src_fmt = &cfg->try_fmt;
  900. } else {
  901. src_crop = &imx274->crop;
  902. src_fmt = &imx274->format;
  903. }
  904. mutex_lock(&imx274->lock);
  905. switch (sel->target) {
  906. case V4L2_SEL_TGT_CROP:
  907. sel->r = *src_crop;
  908. break;
  909. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  910. sel->r.top = 0;
  911. sel->r.left = 0;
  912. sel->r.width = src_crop->width;
  913. sel->r.height = src_crop->height;
  914. break;
  915. case V4L2_SEL_TGT_COMPOSE:
  916. sel->r.top = 0;
  917. sel->r.left = 0;
  918. sel->r.width = src_fmt->width;
  919. sel->r.height = src_fmt->height;
  920. break;
  921. default:
  922. ret = -EINVAL;
  923. }
  924. mutex_unlock(&imx274->lock);
  925. return ret;
  926. }
  927. static int imx274_set_selection_crop(struct stimx274 *imx274,
  928. struct v4l2_subdev_pad_config *cfg,
  929. struct v4l2_subdev_selection *sel)
  930. {
  931. struct v4l2_rect *tgt_crop;
  932. struct v4l2_rect new_crop;
  933. bool size_changed;
  934. /*
  935. * h_step could be 12 or 24 depending on the binning. But we
  936. * won't know the binning until we choose the mode later in
  937. * __imx274_change_compose(). Thus let's be safe and use the
  938. * most conservative value in all cases.
  939. */
  940. const u32 h_step = 24;
  941. new_crop.width = min_t(u32,
  942. IMX274_ROUND(sel->r.width, h_step, sel->flags),
  943. IMX274_MAX_WIDTH);
  944. /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
  945. if (new_crop.width < 144)
  946. new_crop.width = 144;
  947. new_crop.left = min_t(u32,
  948. IMX274_ROUND(sel->r.left, h_step, 0),
  949. IMX274_MAX_WIDTH - new_crop.width);
  950. new_crop.height = min_t(u32,
  951. IMX274_ROUND(sel->r.height, 2, sel->flags),
  952. IMX274_MAX_HEIGHT);
  953. new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0),
  954. IMX274_MAX_HEIGHT - new_crop.height);
  955. sel->r = new_crop;
  956. if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
  957. tgt_crop = &cfg->try_crop;
  958. else
  959. tgt_crop = &imx274->crop;
  960. mutex_lock(&imx274->lock);
  961. size_changed = (new_crop.width != tgt_crop->width ||
  962. new_crop.height != tgt_crop->height);
  963. /* __imx274_change_compose needs the new size in *tgt_crop */
  964. *tgt_crop = new_crop;
  965. /* if crop size changed then reset the output image size */
  966. if (size_changed)
  967. __imx274_change_compose(imx274, cfg, sel->which,
  968. &new_crop.width, &new_crop.height,
  969. sel->flags);
  970. mutex_unlock(&imx274->lock);
  971. return 0;
  972. }
  973. static int imx274_set_selection(struct v4l2_subdev *sd,
  974. struct v4l2_subdev_pad_config *cfg,
  975. struct v4l2_subdev_selection *sel)
  976. {
  977. struct stimx274 *imx274 = to_imx274(sd);
  978. if (sel->pad != 0)
  979. return -EINVAL;
  980. if (sel->target == V4L2_SEL_TGT_CROP)
  981. return imx274_set_selection_crop(imx274, cfg, sel);
  982. if (sel->target == V4L2_SEL_TGT_COMPOSE) {
  983. int err;
  984. mutex_lock(&imx274->lock);
  985. err = __imx274_change_compose(imx274, cfg, sel->which,
  986. &sel->r.width, &sel->r.height,
  987. sel->flags);
  988. mutex_unlock(&imx274->lock);
  989. /*
  990. * __imx274_change_compose already set width and
  991. * height in set->r, we still need to set top-left
  992. */
  993. if (!err) {
  994. sel->r.top = 0;
  995. sel->r.left = 0;
  996. }
  997. return err;
  998. }
  999. return -EINVAL;
  1000. }
  1001. static int imx274_apply_trimming(struct stimx274 *imx274)
  1002. {
  1003. u32 h_start;
  1004. u32 h_end;
  1005. u32 hmax;
  1006. u32 v_cut;
  1007. s32 v_pos;
  1008. u32 write_v_size;
  1009. u32 y_out_size;
  1010. int err;
  1011. h_start = imx274->crop.left + 12;
  1012. h_end = h_start + imx274->crop.width;
  1013. /* Use the minimum allowed value of HMAX */
  1014. /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
  1015. /* Note: 260 is the minimum HMAX in all implemented modes */
  1016. hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23);
  1017. /* invert v_pos if VFLIP */
  1018. v_pos = imx274->ctrls.vflip->cur.val ?
  1019. (-imx274->crop.top / 2) : (imx274->crop.top / 2);
  1020. v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2;
  1021. write_v_size = imx274->crop.height + 22;
  1022. y_out_size = imx274->crop.height + 14;
  1023. err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2);
  1024. if (!err)
  1025. err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1);
  1026. if (!err)
  1027. err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB,
  1028. h_start, 2);
  1029. if (!err)
  1030. err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB,
  1031. h_end, 2);
  1032. if (!err)
  1033. err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1);
  1034. if (!err)
  1035. err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB,
  1036. v_cut, 2);
  1037. if (!err)
  1038. err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB,
  1039. v_pos, 2);
  1040. if (!err)
  1041. err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB,
  1042. write_v_size, 2);
  1043. if (!err)
  1044. err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB,
  1045. y_out_size, 2);
  1046. return err;
  1047. }
  1048. /**
  1049. * imx274_g_frame_interval - Get the frame interval
  1050. * @sd: Pointer to V4L2 Sub device structure
  1051. * @fi: Pointer to V4l2 Sub device frame interval structure
  1052. *
  1053. * This function is used to get the frame interval.
  1054. *
  1055. * Return: 0 on success
  1056. */
  1057. static int imx274_g_frame_interval(struct v4l2_subdev *sd,
  1058. struct v4l2_subdev_frame_interval *fi)
  1059. {
  1060. struct stimx274 *imx274 = to_imx274(sd);
  1061. fi->interval = imx274->frame_interval;
  1062. dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n",
  1063. __func__, imx274->frame_interval.numerator,
  1064. imx274->frame_interval.denominator);
  1065. return 0;
  1066. }
  1067. /**
  1068. * imx274_s_frame_interval - Set the frame interval
  1069. * @sd: Pointer to V4L2 Sub device structure
  1070. * @fi: Pointer to V4l2 Sub device frame interval structure
  1071. *
  1072. * This function is used to set the frame intervavl.
  1073. *
  1074. * Return: 0 on success
  1075. */
  1076. static int imx274_s_frame_interval(struct v4l2_subdev *sd,
  1077. struct v4l2_subdev_frame_interval *fi)
  1078. {
  1079. struct stimx274 *imx274 = to_imx274(sd);
  1080. struct v4l2_ctrl *ctrl = imx274->ctrls.exposure;
  1081. int min, max, def;
  1082. int ret;
  1083. mutex_lock(&imx274->lock);
  1084. ret = imx274_set_frame_interval(imx274, fi->interval);
  1085. if (!ret) {
  1086. fi->interval = imx274->frame_interval;
  1087. /*
  1088. * exposure time range is decided by frame interval
  1089. * need to update it after frame interval changes
  1090. */
  1091. min = IMX274_MIN_EXPOSURE_TIME;
  1092. max = fi->interval.numerator * 1000000
  1093. / fi->interval.denominator;
  1094. def = max;
  1095. if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def)) {
  1096. dev_err(&imx274->client->dev,
  1097. "Exposure ctrl range update failed\n");
  1098. goto unlock;
  1099. }
  1100. /* update exposure time accordingly */
  1101. imx274_set_exposure(imx274, ctrl->val);
  1102. dev_dbg(&imx274->client->dev, "set frame interval to %uus\n",
  1103. fi->interval.numerator * 1000000
  1104. / fi->interval.denominator);
  1105. }
  1106. unlock:
  1107. mutex_unlock(&imx274->lock);
  1108. return ret;
  1109. }
  1110. /**
  1111. * imx274_load_default - load default control values
  1112. * @priv: Pointer to device structure
  1113. *
  1114. * Return: 0 on success, errors otherwise
  1115. */
  1116. static int imx274_load_default(struct stimx274 *priv)
  1117. {
  1118. int ret;
  1119. /* load default control values */
  1120. priv->frame_interval.numerator = 1;
  1121. priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
  1122. priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE;
  1123. priv->ctrls.gain->val = IMX274_DEF_GAIN;
  1124. priv->ctrls.vflip->val = 0;
  1125. priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED;
  1126. /* update frame rate */
  1127. ret = imx274_set_frame_interval(priv,
  1128. priv->frame_interval);
  1129. if (ret)
  1130. return ret;
  1131. /* update exposure time */
  1132. ret = v4l2_ctrl_s_ctrl(priv->ctrls.exposure, priv->ctrls.exposure->val);
  1133. if (ret)
  1134. return ret;
  1135. /* update gain */
  1136. ret = v4l2_ctrl_s_ctrl(priv->ctrls.gain, priv->ctrls.gain->val);
  1137. if (ret)
  1138. return ret;
  1139. /* update vflip */
  1140. ret = v4l2_ctrl_s_ctrl(priv->ctrls.vflip, priv->ctrls.vflip->val);
  1141. if (ret)
  1142. return ret;
  1143. return 0;
  1144. }
  1145. /**
  1146. * imx274_s_stream - It is used to start/stop the streaming.
  1147. * @sd: V4L2 Sub device
  1148. * @on: Flag (True / False)
  1149. *
  1150. * This function controls the start or stop of streaming for the
  1151. * imx274 sensor.
  1152. *
  1153. * Return: 0 on success, errors otherwise
  1154. */
  1155. static int imx274_s_stream(struct v4l2_subdev *sd, int on)
  1156. {
  1157. struct stimx274 *imx274 = to_imx274(sd);
  1158. int ret = 0;
  1159. dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__,
  1160. on ? "Stream Start" : "Stream Stop",
  1161. imx274->mode - &imx274_formats[0]);
  1162. mutex_lock(&imx274->lock);
  1163. if (on) {
  1164. /* load mode registers */
  1165. ret = imx274_mode_regs(imx274);
  1166. if (ret)
  1167. goto fail;
  1168. ret = imx274_apply_trimming(imx274);
  1169. if (ret)
  1170. goto fail;
  1171. /*
  1172. * update frame rate & expsoure. if the last mode is different,
  1173. * HMAX could be changed. As the result, frame rate & exposure
  1174. * are changed.
  1175. * gain is not affected.
  1176. */
  1177. ret = imx274_set_frame_interval(imx274,
  1178. imx274->frame_interval);
  1179. if (ret)
  1180. goto fail;
  1181. /* update exposure time */
  1182. ret = __v4l2_ctrl_s_ctrl(imx274->ctrls.exposure,
  1183. imx274->ctrls.exposure->val);
  1184. if (ret)
  1185. goto fail;
  1186. /* start stream */
  1187. ret = imx274_start_stream(imx274);
  1188. if (ret)
  1189. goto fail;
  1190. } else {
  1191. /* stop stream */
  1192. ret = imx274_write_table(imx274, imx274_stop);
  1193. if (ret)
  1194. goto fail;
  1195. }
  1196. mutex_unlock(&imx274->lock);
  1197. dev_dbg(&imx274->client->dev, "%s : Done\n", __func__);
  1198. return 0;
  1199. fail:
  1200. mutex_unlock(&imx274->lock);
  1201. dev_err(&imx274->client->dev, "s_stream failed\n");
  1202. return ret;
  1203. }
  1204. /*
  1205. * imx274_get_frame_length - Function for obtaining current frame length
  1206. * @priv: Pointer to device structure
  1207. * @val: Pointer to obainted value
  1208. *
  1209. * frame_length = vmax x (svr + 1), in unit of hmax.
  1210. *
  1211. * Return: 0 on success
  1212. */
  1213. static int imx274_get_frame_length(struct stimx274 *priv, u32 *val)
  1214. {
  1215. int err;
  1216. u16 svr;
  1217. u32 vmax;
  1218. u8 reg_val[3];
  1219. /* svr */
  1220. err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, &reg_val[0]);
  1221. if (err)
  1222. goto fail;
  1223. err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, &reg_val[1]);
  1224. if (err)
  1225. goto fail;
  1226. svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
  1227. /* vmax */
  1228. err = imx274_read_reg(priv, IMX274_VMAX_REG_3, &reg_val[0]);
  1229. if (err)
  1230. goto fail;
  1231. err = imx274_read_reg(priv, IMX274_VMAX_REG_2, &reg_val[1]);
  1232. if (err)
  1233. goto fail;
  1234. err = imx274_read_reg(priv, IMX274_VMAX_REG_1, &reg_val[2]);
  1235. if (err)
  1236. goto fail;
  1237. vmax = ((reg_val[2] & IMX274_MASK_LSB_3_BITS) << IMX274_SHIFT_16_BITS)
  1238. + (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
  1239. *val = vmax * (svr + 1);
  1240. return 0;
  1241. fail:
  1242. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1243. return err;
  1244. }
  1245. static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val,
  1246. u32 *frame_length)
  1247. {
  1248. int err;
  1249. err = imx274_get_frame_length(priv, frame_length);
  1250. if (err)
  1251. return err;
  1252. if (*frame_length < priv->mode->min_frame_len)
  1253. *frame_length = priv->mode->min_frame_len;
  1254. *val = *frame_length - *val; /* convert to raw shr */
  1255. if (*val > *frame_length - IMX274_SHR_LIMIT_CONST)
  1256. *val = *frame_length - IMX274_SHR_LIMIT_CONST;
  1257. else if (*val < priv->mode->min_SHR)
  1258. *val = priv->mode->min_SHR;
  1259. return 0;
  1260. }
  1261. /*
  1262. * imx274_set_digital gain - Function called when setting digital gain
  1263. * @priv: Pointer to device structure
  1264. * @dgain: Value of digital gain.
  1265. *
  1266. * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
  1267. *
  1268. * Return: 0 on success
  1269. */
  1270. static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain)
  1271. {
  1272. u8 reg_val;
  1273. reg_val = ffs(dgain);
  1274. if (reg_val)
  1275. reg_val--;
  1276. reg_val = clamp(reg_val, (u8)0, (u8)3);
  1277. return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG,
  1278. reg_val & IMX274_MASK_LSB_4_BITS);
  1279. }
  1280. /*
  1281. * imx274_set_gain - Function called when setting gain
  1282. * @priv: Pointer to device structure
  1283. * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
  1284. * @ctrl: v4l2 control pointer
  1285. *
  1286. * Set the gain based on input value.
  1287. * The caller should hold the mutex lock imx274->lock if necessary
  1288. *
  1289. * Return: 0 on success
  1290. */
  1291. static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl)
  1292. {
  1293. int err;
  1294. u32 gain, analog_gain, digital_gain, gain_reg;
  1295. gain = (u32)(ctrl->val);
  1296. dev_dbg(&priv->client->dev,
  1297. "%s : input gain = %d.%d\n", __func__,
  1298. gain >> IMX274_GAIN_SHIFT,
  1299. ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT);
  1300. if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN)
  1301. gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN;
  1302. else if (gain < IMX274_MIN_GAIN)
  1303. gain = IMX274_MIN_GAIN;
  1304. if (gain <= IMX274_MAX_ANALOG_GAIN)
  1305. digital_gain = 1;
  1306. else if (gain <= IMX274_MAX_ANALOG_GAIN * 2)
  1307. digital_gain = 2;
  1308. else if (gain <= IMX274_MAX_ANALOG_GAIN * 4)
  1309. digital_gain = 4;
  1310. else
  1311. digital_gain = IMX274_MAX_DIGITAL_GAIN;
  1312. analog_gain = gain / digital_gain;
  1313. dev_dbg(&priv->client->dev,
  1314. "%s : digital gain = %d, analog gain = %d.%d\n",
  1315. __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT,
  1316. ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100)
  1317. >> IMX274_GAIN_SHIFT);
  1318. err = imx274_set_digital_gain(priv, digital_gain);
  1319. if (err)
  1320. goto fail;
  1321. /* convert to register value, refer to imx274 datasheet */
  1322. gain_reg = (u32)IMX274_GAIN_CONST -
  1323. (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain;
  1324. if (gain_reg > IMX274_GAIN_REG_MAX)
  1325. gain_reg = IMX274_GAIN_REG_MAX;
  1326. err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg,
  1327. 2);
  1328. if (err)
  1329. goto fail;
  1330. if (IMX274_GAIN_CONST - gain_reg == 0) {
  1331. err = -EINVAL;
  1332. goto fail;
  1333. }
  1334. /* convert register value back to gain value */
  1335. ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT)
  1336. / (IMX274_GAIN_CONST - gain_reg) * digital_gain;
  1337. dev_dbg(&priv->client->dev,
  1338. "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
  1339. __func__, gain_reg, ctrl->val);
  1340. return 0;
  1341. fail:
  1342. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1343. return err;
  1344. }
  1345. /*
  1346. * imx274_set_coarse_time - Function called when setting SHR value
  1347. * @priv: Pointer to device structure
  1348. * @val: Value for exposure time in number of line_length, or [HMAX]
  1349. *
  1350. * Set SHR value based on input value.
  1351. *
  1352. * Return: 0 on success
  1353. */
  1354. static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val)
  1355. {
  1356. int err;
  1357. u32 coarse_time, frame_length;
  1358. coarse_time = *val;
  1359. /* convert exposure_time to appropriate SHR value */
  1360. err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length);
  1361. if (err)
  1362. goto fail;
  1363. err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2);
  1364. if (err)
  1365. goto fail;
  1366. *val = frame_length - coarse_time;
  1367. return 0;
  1368. fail:
  1369. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1370. return err;
  1371. }
  1372. /*
  1373. * imx274_set_exposure - Function called when setting exposure time
  1374. * @priv: Pointer to device structure
  1375. * @val: Variable for exposure time, in the unit of micro-second
  1376. *
  1377. * Set exposure time based on input value.
  1378. * The caller should hold the mutex lock imx274->lock if necessary
  1379. *
  1380. * Return: 0 on success
  1381. */
  1382. static int imx274_set_exposure(struct stimx274 *priv, int val)
  1383. {
  1384. int err;
  1385. u16 hmax;
  1386. u8 reg_val[2];
  1387. u32 coarse_time; /* exposure time in unit of line (HMAX)*/
  1388. dev_dbg(&priv->client->dev,
  1389. "%s : EXPOSURE control input = %d\n", __func__, val);
  1390. /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
  1391. /* obtain HMAX value */
  1392. err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, &reg_val[0]);
  1393. if (err)
  1394. goto fail;
  1395. err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, &reg_val[1]);
  1396. if (err)
  1397. goto fail;
  1398. hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
  1399. if (hmax == 0) {
  1400. err = -EINVAL;
  1401. goto fail;
  1402. }
  1403. coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val
  1404. - priv->mode->nocpiop) / hmax;
  1405. /* step 2: convert exposure_time into SHR value */
  1406. /* set SHR */
  1407. err = imx274_set_coarse_time(priv, &coarse_time);
  1408. if (err)
  1409. goto fail;
  1410. priv->ctrls.exposure->val =
  1411. (coarse_time * hmax + priv->mode->nocpiop)
  1412. / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2);
  1413. dev_dbg(&priv->client->dev,
  1414. "%s : EXPOSURE control success\n", __func__);
  1415. return 0;
  1416. fail:
  1417. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1418. return err;
  1419. }
  1420. /*
  1421. * imx274_set_vflip - Function called when setting vertical flip
  1422. * @priv: Pointer to device structure
  1423. * @val: Value for vflip setting
  1424. *
  1425. * Set vertical flip based on input value.
  1426. * val = 0: normal, no vertical flip
  1427. * val = 1: vertical flip enabled
  1428. * The caller should hold the mutex lock imx274->lock if necessary
  1429. *
  1430. * Return: 0 on success
  1431. */
  1432. static int imx274_set_vflip(struct stimx274 *priv, int val)
  1433. {
  1434. int err;
  1435. err = imx274_write_reg(priv, IMX274_VFLIP_REG, val);
  1436. if (err) {
  1437. dev_err(&priv->client->dev, "VFLIP control error\n");
  1438. return err;
  1439. }
  1440. dev_dbg(&priv->client->dev,
  1441. "%s : VFLIP control success\n", __func__);
  1442. return 0;
  1443. }
  1444. /*
  1445. * imx274_set_test_pattern - Function called when setting test pattern
  1446. * @priv: Pointer to device structure
  1447. * @val: Variable for test pattern
  1448. *
  1449. * Set to different test patterns based on input value.
  1450. *
  1451. * Return: 0 on success
  1452. */
  1453. static int imx274_set_test_pattern(struct stimx274 *priv, int val)
  1454. {
  1455. int err = 0;
  1456. if (val == TEST_PATTERN_DISABLED) {
  1457. err = imx274_write_table(priv, imx274_tp_disabled);
  1458. } else if (val <= TEST_PATTERN_V_COLOR_BARS) {
  1459. err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1);
  1460. if (!err)
  1461. err = imx274_write_table(priv, imx274_tp_regs);
  1462. } else {
  1463. err = -EINVAL;
  1464. }
  1465. if (!err)
  1466. dev_dbg(&priv->client->dev,
  1467. "%s : TEST PATTERN control success\n", __func__);
  1468. else
  1469. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1470. return err;
  1471. }
  1472. /*
  1473. * imx274_set_frame_length - Function called when setting frame length
  1474. * @priv: Pointer to device structure
  1475. * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
  1476. *
  1477. * Set frame length based on input value.
  1478. *
  1479. * Return: 0 on success
  1480. */
  1481. static int imx274_set_frame_length(struct stimx274 *priv, u32 val)
  1482. {
  1483. int err;
  1484. u32 frame_length;
  1485. dev_dbg(&priv->client->dev, "%s : input length = %d\n",
  1486. __func__, val);
  1487. frame_length = (u32)val;
  1488. err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3);
  1489. if (err)
  1490. goto fail;
  1491. return 0;
  1492. fail:
  1493. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1494. return err;
  1495. }
  1496. /*
  1497. * imx274_set_frame_interval - Function called when setting frame interval
  1498. * @priv: Pointer to device structure
  1499. * @frame_interval: Variable for frame interval
  1500. *
  1501. * Change frame interval by updating VMAX value
  1502. * The caller should hold the mutex lock imx274->lock if necessary
  1503. *
  1504. * Return: 0 on success
  1505. */
  1506. static int imx274_set_frame_interval(struct stimx274 *priv,
  1507. struct v4l2_fract frame_interval)
  1508. {
  1509. int err;
  1510. u32 frame_length, req_frame_rate;
  1511. u16 svr;
  1512. u16 hmax;
  1513. u8 reg_val[2];
  1514. dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d",
  1515. __func__, frame_interval.numerator,
  1516. frame_interval.denominator);
  1517. if (frame_interval.numerator == 0 || frame_interval.denominator == 0) {
  1518. frame_interval.denominator = IMX274_DEF_FRAME_RATE;
  1519. frame_interval.numerator = 1;
  1520. }
  1521. req_frame_rate = (u32)(frame_interval.denominator
  1522. / frame_interval.numerator);
  1523. /* boundary check */
  1524. if (req_frame_rate > priv->mode->max_fps) {
  1525. frame_interval.numerator = 1;
  1526. frame_interval.denominator = priv->mode->max_fps;
  1527. } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) {
  1528. frame_interval.numerator = 1;
  1529. frame_interval.denominator = IMX274_MIN_FRAME_RATE;
  1530. }
  1531. /*
  1532. * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
  1533. * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
  1534. */
  1535. /* SVR */
  1536. err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, &reg_val[0]);
  1537. if (err)
  1538. goto fail;
  1539. err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, &reg_val[1]);
  1540. if (err)
  1541. goto fail;
  1542. svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
  1543. dev_dbg(&priv->client->dev,
  1544. "%s : register SVR = %d\n", __func__, svr);
  1545. /* HMAX */
  1546. err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, &reg_val[0]);
  1547. if (err)
  1548. goto fail;
  1549. err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, &reg_val[1]);
  1550. if (err)
  1551. goto fail;
  1552. hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
  1553. dev_dbg(&priv->client->dev,
  1554. "%s : register HMAX = %d\n", __func__, hmax);
  1555. if (hmax == 0 || frame_interval.denominator == 0) {
  1556. err = -EINVAL;
  1557. goto fail;
  1558. }
  1559. frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax
  1560. * frame_interval.numerator
  1561. / frame_interval.denominator;
  1562. err = imx274_set_frame_length(priv, frame_length);
  1563. if (err)
  1564. goto fail;
  1565. priv->frame_interval = frame_interval;
  1566. return 0;
  1567. fail:
  1568. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1569. return err;
  1570. }
  1571. static const struct v4l2_subdev_pad_ops imx274_pad_ops = {
  1572. .get_fmt = imx274_get_fmt,
  1573. .set_fmt = imx274_set_fmt,
  1574. .get_selection = imx274_get_selection,
  1575. .set_selection = imx274_set_selection,
  1576. };
  1577. static const struct v4l2_subdev_video_ops imx274_video_ops = {
  1578. .g_frame_interval = imx274_g_frame_interval,
  1579. .s_frame_interval = imx274_s_frame_interval,
  1580. .s_stream = imx274_s_stream,
  1581. };
  1582. static const struct v4l2_subdev_ops imx274_subdev_ops = {
  1583. .pad = &imx274_pad_ops,
  1584. .video = &imx274_video_ops,
  1585. };
  1586. static const struct v4l2_ctrl_ops imx274_ctrl_ops = {
  1587. .s_ctrl = imx274_s_ctrl,
  1588. };
  1589. static const struct of_device_id imx274_of_id_table[] = {
  1590. { .compatible = "sony,imx274" },
  1591. { }
  1592. };
  1593. MODULE_DEVICE_TABLE(of, imx274_of_id_table);
  1594. static const struct i2c_device_id imx274_id[] = {
  1595. { "IMX274", 0 },
  1596. { }
  1597. };
  1598. MODULE_DEVICE_TABLE(i2c, imx274_id);
  1599. static int imx274_probe(struct i2c_client *client,
  1600. const struct i2c_device_id *id)
  1601. {
  1602. struct v4l2_subdev *sd;
  1603. struct stimx274 *imx274;
  1604. int ret;
  1605. /* initialize imx274 */
  1606. imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL);
  1607. if (!imx274)
  1608. return -ENOMEM;
  1609. mutex_init(&imx274->lock);
  1610. /* initialize format */
  1611. imx274->mode = &imx274_formats[IMX274_DEFAULT_MODE];
  1612. imx274->crop.width = IMX274_MAX_WIDTH;
  1613. imx274->crop.height = IMX274_MAX_HEIGHT;
  1614. imx274->format.width = imx274->crop.width / imx274->mode->bin_ratio;
  1615. imx274->format.height = imx274->crop.height / imx274->mode->bin_ratio;
  1616. imx274->format.field = V4L2_FIELD_NONE;
  1617. imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
  1618. imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
  1619. imx274->frame_interval.numerator = 1;
  1620. imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
  1621. /* initialize regmap */
  1622. imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
  1623. if (IS_ERR(imx274->regmap)) {
  1624. dev_err(&client->dev,
  1625. "regmap init failed: %ld\n", PTR_ERR(imx274->regmap));
  1626. ret = -ENODEV;
  1627. goto err_regmap;
  1628. }
  1629. /* initialize subdevice */
  1630. imx274->client = client;
  1631. sd = &imx274->sd;
  1632. v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops);
  1633. strlcpy(sd->name, DRIVER_NAME, sizeof(sd->name));
  1634. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1635. /* initialize subdev media pad */
  1636. imx274->pad.flags = MEDIA_PAD_FL_SOURCE;
  1637. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1638. ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad);
  1639. if (ret < 0) {
  1640. dev_err(&client->dev,
  1641. "%s : media entity init Failed %d\n", __func__, ret);
  1642. goto err_regmap;
  1643. }
  1644. /* initialize sensor reset gpio */
  1645. imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  1646. GPIOD_OUT_HIGH);
  1647. if (IS_ERR(imx274->reset_gpio)) {
  1648. if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER)
  1649. dev_err(&client->dev, "Reset GPIO not setup in DT");
  1650. ret = PTR_ERR(imx274->reset_gpio);
  1651. goto err_me;
  1652. }
  1653. /* pull sensor out of reset */
  1654. imx274_reset(imx274, 1);
  1655. /* initialize controls */
  1656. ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 2);
  1657. if (ret < 0) {
  1658. dev_err(&client->dev,
  1659. "%s : ctrl handler init Failed\n", __func__);
  1660. goto err_me;
  1661. }
  1662. imx274->ctrls.handler.lock = &imx274->lock;
  1663. /* add new controls */
  1664. imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items(
  1665. &imx274->ctrls.handler, &imx274_ctrl_ops,
  1666. V4L2_CID_TEST_PATTERN,
  1667. ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu);
  1668. imx274->ctrls.gain = v4l2_ctrl_new_std(
  1669. &imx274->ctrls.handler,
  1670. &imx274_ctrl_ops,
  1671. V4L2_CID_GAIN, IMX274_MIN_GAIN,
  1672. IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1,
  1673. IMX274_DEF_GAIN);
  1674. imx274->ctrls.exposure = v4l2_ctrl_new_std(
  1675. &imx274->ctrls.handler,
  1676. &imx274_ctrl_ops,
  1677. V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME,
  1678. 1000000 / IMX274_DEF_FRAME_RATE, 1,
  1679. IMX274_MIN_EXPOSURE_TIME);
  1680. imx274->ctrls.vflip = v4l2_ctrl_new_std(
  1681. &imx274->ctrls.handler,
  1682. &imx274_ctrl_ops,
  1683. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1684. imx274->sd.ctrl_handler = &imx274->ctrls.handler;
  1685. if (imx274->ctrls.handler.error) {
  1686. ret = imx274->ctrls.handler.error;
  1687. goto err_ctrls;
  1688. }
  1689. /* setup default controls */
  1690. ret = v4l2_ctrl_handler_setup(&imx274->ctrls.handler);
  1691. if (ret) {
  1692. dev_err(&client->dev,
  1693. "Error %d setup default controls\n", ret);
  1694. goto err_ctrls;
  1695. }
  1696. /* load default control values */
  1697. ret = imx274_load_default(imx274);
  1698. if (ret) {
  1699. dev_err(&client->dev,
  1700. "%s : imx274_load_default failed %d\n",
  1701. __func__, ret);
  1702. goto err_ctrls;
  1703. }
  1704. /* register subdevice */
  1705. ret = v4l2_async_register_subdev(sd);
  1706. if (ret < 0) {
  1707. dev_err(&client->dev,
  1708. "%s : v4l2_async_register_subdev failed %d\n",
  1709. __func__, ret);
  1710. goto err_ctrls;
  1711. }
  1712. dev_info(&client->dev, "imx274 : imx274 probe success !\n");
  1713. return 0;
  1714. err_ctrls:
  1715. v4l2_ctrl_handler_free(&imx274->ctrls.handler);
  1716. err_me:
  1717. media_entity_cleanup(&sd->entity);
  1718. err_regmap:
  1719. mutex_destroy(&imx274->lock);
  1720. return ret;
  1721. }
  1722. static int imx274_remove(struct i2c_client *client)
  1723. {
  1724. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1725. struct stimx274 *imx274 = to_imx274(sd);
  1726. /* stop stream */
  1727. imx274_write_table(imx274, imx274_stop);
  1728. v4l2_async_unregister_subdev(sd);
  1729. v4l2_ctrl_handler_free(&imx274->ctrls.handler);
  1730. media_entity_cleanup(&sd->entity);
  1731. mutex_destroy(&imx274->lock);
  1732. return 0;
  1733. }
  1734. static struct i2c_driver imx274_i2c_driver = {
  1735. .driver = {
  1736. .name = DRIVER_NAME,
  1737. .of_match_table = imx274_of_id_table,
  1738. },
  1739. .probe = imx274_probe,
  1740. .remove = imx274_remove,
  1741. .id_table = imx274_id,
  1742. };
  1743. module_i2c_driver(imx274_i2c_driver);
  1744. MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
  1745. MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
  1746. MODULE_LICENSE("GPL v2");