rj54n1cb0c.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. * Driver for RJ54N1CB0C CMOS Image Sensor from Sharp
  3. *
  4. * Copyright (C) 2009, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/slab.h>
  13. #include <linux/v4l2-mediabus.h>
  14. #include <linux/videodev2.h>
  15. #include <linux/module.h>
  16. #include <media/rj54n1cb0c.h>
  17. #include <media/soc_camera.h>
  18. #include <media/v4l2-clk.h>
  19. #include <media/v4l2-subdev.h>
  20. #include <media/v4l2-ctrls.h>
  21. #define RJ54N1_DEV_CODE 0x0400
  22. #define RJ54N1_DEV_CODE2 0x0401
  23. #define RJ54N1_OUT_SEL 0x0403
  24. #define RJ54N1_XY_OUTPUT_SIZE_S_H 0x0404
  25. #define RJ54N1_X_OUTPUT_SIZE_S_L 0x0405
  26. #define RJ54N1_Y_OUTPUT_SIZE_S_L 0x0406
  27. #define RJ54N1_XY_OUTPUT_SIZE_P_H 0x0407
  28. #define RJ54N1_X_OUTPUT_SIZE_P_L 0x0408
  29. #define RJ54N1_Y_OUTPUT_SIZE_P_L 0x0409
  30. #define RJ54N1_LINE_LENGTH_PCK_S_H 0x040a
  31. #define RJ54N1_LINE_LENGTH_PCK_S_L 0x040b
  32. #define RJ54N1_LINE_LENGTH_PCK_P_H 0x040c
  33. #define RJ54N1_LINE_LENGTH_PCK_P_L 0x040d
  34. #define RJ54N1_RESIZE_N 0x040e
  35. #define RJ54N1_RESIZE_N_STEP 0x040f
  36. #define RJ54N1_RESIZE_STEP 0x0410
  37. #define RJ54N1_RESIZE_HOLD_H 0x0411
  38. #define RJ54N1_RESIZE_HOLD_L 0x0412
  39. #define RJ54N1_H_OBEN_OFS 0x0413
  40. #define RJ54N1_V_OBEN_OFS 0x0414
  41. #define RJ54N1_RESIZE_CONTROL 0x0415
  42. #define RJ54N1_STILL_CONTROL 0x0417
  43. #define RJ54N1_INC_USE_SEL_H 0x0425
  44. #define RJ54N1_INC_USE_SEL_L 0x0426
  45. #define RJ54N1_MIRROR_STILL_MODE 0x0427
  46. #define RJ54N1_INIT_START 0x0428
  47. #define RJ54N1_SCALE_1_2_LEV 0x0429
  48. #define RJ54N1_SCALE_4_LEV 0x042a
  49. #define RJ54N1_Y_GAIN 0x04d8
  50. #define RJ54N1_APT_GAIN_UP 0x04fa
  51. #define RJ54N1_RA_SEL_UL 0x0530
  52. #define RJ54N1_BYTE_SWAP 0x0531
  53. #define RJ54N1_OUT_SIGPO 0x053b
  54. #define RJ54N1_WB_SEL_WEIGHT_I 0x054e
  55. #define RJ54N1_BIT8_WB 0x0569
  56. #define RJ54N1_HCAPS_WB 0x056a
  57. #define RJ54N1_VCAPS_WB 0x056b
  58. #define RJ54N1_HCAPE_WB 0x056c
  59. #define RJ54N1_VCAPE_WB 0x056d
  60. #define RJ54N1_EXPOSURE_CONTROL 0x058c
  61. #define RJ54N1_FRAME_LENGTH_S_H 0x0595
  62. #define RJ54N1_FRAME_LENGTH_S_L 0x0596
  63. #define RJ54N1_FRAME_LENGTH_P_H 0x0597
  64. #define RJ54N1_FRAME_LENGTH_P_L 0x0598
  65. #define RJ54N1_PEAK_H 0x05b7
  66. #define RJ54N1_PEAK_50 0x05b8
  67. #define RJ54N1_PEAK_60 0x05b9
  68. #define RJ54N1_PEAK_DIFF 0x05ba
  69. #define RJ54N1_IOC 0x05ef
  70. #define RJ54N1_TG_BYPASS 0x0700
  71. #define RJ54N1_PLL_L 0x0701
  72. #define RJ54N1_PLL_N 0x0702
  73. #define RJ54N1_PLL_EN 0x0704
  74. #define RJ54N1_RATIO_TG 0x0706
  75. #define RJ54N1_RATIO_T 0x0707
  76. #define RJ54N1_RATIO_R 0x0708
  77. #define RJ54N1_RAMP_TGCLK_EN 0x0709
  78. #define RJ54N1_OCLK_DSP 0x0710
  79. #define RJ54N1_RATIO_OP 0x0711
  80. #define RJ54N1_RATIO_O 0x0712
  81. #define RJ54N1_OCLK_SEL_EN 0x0713
  82. #define RJ54N1_CLK_RST 0x0717
  83. #define RJ54N1_RESET_STANDBY 0x0718
  84. #define RJ54N1_FWFLG 0x07fe
  85. #define E_EXCLK (1 << 7)
  86. #define SOFT_STDBY (1 << 4)
  87. #define SEN_RSTX (1 << 2)
  88. #define TG_RSTX (1 << 1)
  89. #define DSP_RSTX (1 << 0)
  90. #define RESIZE_HOLD_SEL (1 << 2)
  91. #define RESIZE_GO (1 << 1)
  92. /*
  93. * When cropping, the camera automatically centers the cropped region, there
  94. * doesn't seem to be a way to specify an explicit location of the rectangle.
  95. */
  96. #define RJ54N1_COLUMN_SKIP 0
  97. #define RJ54N1_ROW_SKIP 0
  98. #define RJ54N1_MAX_WIDTH 1600
  99. #define RJ54N1_MAX_HEIGHT 1200
  100. #define PLL_L 2
  101. #define PLL_N 0x31
  102. /* I2C addresses: 0x50, 0x51, 0x60, 0x61 */
  103. /* RJ54N1CB0C has only one fixed colorspace per pixelcode */
  104. struct rj54n1_datafmt {
  105. enum v4l2_mbus_pixelcode code;
  106. enum v4l2_colorspace colorspace;
  107. };
  108. /* Find a data format by a pixel code in an array */
  109. static const struct rj54n1_datafmt *rj54n1_find_datafmt(
  110. enum v4l2_mbus_pixelcode code, const struct rj54n1_datafmt *fmt,
  111. int n)
  112. {
  113. int i;
  114. for (i = 0; i < n; i++)
  115. if (fmt[i].code == code)
  116. return fmt + i;
  117. return NULL;
  118. }
  119. static const struct rj54n1_datafmt rj54n1_colour_fmts[] = {
  120. {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
  121. {V4L2_MBUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG},
  122. {V4L2_MBUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB},
  123. {V4L2_MBUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB},
  124. {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
  125. {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE, V4L2_COLORSPACE_SRGB},
  126. {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE, V4L2_COLORSPACE_SRGB},
  127. {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE, V4L2_COLORSPACE_SRGB},
  128. {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
  129. };
  130. struct rj54n1_clock_div {
  131. u8 ratio_tg; /* can be 0 or an odd number */
  132. u8 ratio_t;
  133. u8 ratio_r;
  134. u8 ratio_op;
  135. u8 ratio_o;
  136. };
  137. struct rj54n1 {
  138. struct v4l2_subdev subdev;
  139. struct v4l2_ctrl_handler hdl;
  140. struct v4l2_clk *clk;
  141. struct rj54n1_clock_div clk_div;
  142. const struct rj54n1_datafmt *fmt;
  143. struct v4l2_rect rect; /* Sensor window */
  144. unsigned int tgclk_mhz;
  145. bool auto_wb;
  146. unsigned short width; /* Output window */
  147. unsigned short height;
  148. unsigned short resize; /* Sensor * 1024 / resize = Output */
  149. unsigned short scale;
  150. u8 bank;
  151. };
  152. struct rj54n1_reg_val {
  153. u16 reg;
  154. u8 val;
  155. };
  156. static const struct rj54n1_reg_val bank_4[] = {
  157. {0x417, 0},
  158. {0x42c, 0},
  159. {0x42d, 0xf0},
  160. {0x42e, 0},
  161. {0x42f, 0x50},
  162. {0x430, 0xf5},
  163. {0x431, 0x16},
  164. {0x432, 0x20},
  165. {0x433, 0},
  166. {0x434, 0xc8},
  167. {0x43c, 8},
  168. {0x43e, 0x90},
  169. {0x445, 0x83},
  170. {0x4ba, 0x58},
  171. {0x4bb, 4},
  172. {0x4bc, 0x20},
  173. {0x4db, 4},
  174. {0x4fe, 2},
  175. };
  176. static const struct rj54n1_reg_val bank_5[] = {
  177. {0x514, 0},
  178. {0x516, 0},
  179. {0x518, 0},
  180. {0x51a, 0},
  181. {0x51d, 0xff},
  182. {0x56f, 0x28},
  183. {0x575, 0x40},
  184. {0x5bc, 0x48},
  185. {0x5c1, 6},
  186. {0x5e5, 0x11},
  187. {0x5e6, 0x43},
  188. {0x5e7, 0x33},
  189. {0x5e8, 0x21},
  190. {0x5e9, 0x30},
  191. {0x5ea, 0x0},
  192. {0x5eb, 0xa5},
  193. {0x5ec, 0xff},
  194. {0x5fe, 2},
  195. };
  196. static const struct rj54n1_reg_val bank_7[] = {
  197. {0x70a, 0},
  198. {0x714, 0xff},
  199. {0x715, 0xff},
  200. {0x716, 0x1f},
  201. {0x7FE, 2},
  202. };
  203. static const struct rj54n1_reg_val bank_8[] = {
  204. {0x800, 0x00},
  205. {0x801, 0x01},
  206. {0x802, 0x61},
  207. {0x805, 0x00},
  208. {0x806, 0x00},
  209. {0x807, 0x00},
  210. {0x808, 0x00},
  211. {0x809, 0x01},
  212. {0x80A, 0x61},
  213. {0x80B, 0x00},
  214. {0x80C, 0x01},
  215. {0x80D, 0x00},
  216. {0x80E, 0x00},
  217. {0x80F, 0x00},
  218. {0x810, 0x00},
  219. {0x811, 0x01},
  220. {0x812, 0x61},
  221. {0x813, 0x00},
  222. {0x814, 0x11},
  223. {0x815, 0x00},
  224. {0x816, 0x41},
  225. {0x817, 0x00},
  226. {0x818, 0x51},
  227. {0x819, 0x01},
  228. {0x81A, 0x1F},
  229. {0x81B, 0x00},
  230. {0x81C, 0x01},
  231. {0x81D, 0x00},
  232. {0x81E, 0x11},
  233. {0x81F, 0x00},
  234. {0x820, 0x41},
  235. {0x821, 0x00},
  236. {0x822, 0x51},
  237. {0x823, 0x00},
  238. {0x824, 0x00},
  239. {0x825, 0x00},
  240. {0x826, 0x47},
  241. {0x827, 0x01},
  242. {0x828, 0x4F},
  243. {0x829, 0x00},
  244. {0x82A, 0x00},
  245. {0x82B, 0x00},
  246. {0x82C, 0x30},
  247. {0x82D, 0x00},
  248. {0x82E, 0x40},
  249. {0x82F, 0x00},
  250. {0x830, 0xB3},
  251. {0x831, 0x00},
  252. {0x832, 0xE3},
  253. {0x833, 0x00},
  254. {0x834, 0x00},
  255. {0x835, 0x00},
  256. {0x836, 0x00},
  257. {0x837, 0x00},
  258. {0x838, 0x00},
  259. {0x839, 0x01},
  260. {0x83A, 0x61},
  261. {0x83B, 0x00},
  262. {0x83C, 0x01},
  263. {0x83D, 0x00},
  264. {0x83E, 0x00},
  265. {0x83F, 0x00},
  266. {0x840, 0x00},
  267. {0x841, 0x01},
  268. {0x842, 0x61},
  269. {0x843, 0x00},
  270. {0x844, 0x1D},
  271. {0x845, 0x00},
  272. {0x846, 0x00},
  273. {0x847, 0x00},
  274. {0x848, 0x00},
  275. {0x849, 0x01},
  276. {0x84A, 0x1F},
  277. {0x84B, 0x00},
  278. {0x84C, 0x05},
  279. {0x84D, 0x00},
  280. {0x84E, 0x19},
  281. {0x84F, 0x01},
  282. {0x850, 0x21},
  283. {0x851, 0x01},
  284. {0x852, 0x5D},
  285. {0x853, 0x00},
  286. {0x854, 0x00},
  287. {0x855, 0x00},
  288. {0x856, 0x19},
  289. {0x857, 0x01},
  290. {0x858, 0x21},
  291. {0x859, 0x00},
  292. {0x85A, 0x00},
  293. {0x85B, 0x00},
  294. {0x85C, 0x00},
  295. {0x85D, 0x00},
  296. {0x85E, 0x00},
  297. {0x85F, 0x00},
  298. {0x860, 0xB3},
  299. {0x861, 0x00},
  300. {0x862, 0xE3},
  301. {0x863, 0x00},
  302. {0x864, 0x00},
  303. {0x865, 0x00},
  304. {0x866, 0x00},
  305. {0x867, 0x00},
  306. {0x868, 0x00},
  307. {0x869, 0xE2},
  308. {0x86A, 0x00},
  309. {0x86B, 0x01},
  310. {0x86C, 0x06},
  311. {0x86D, 0x00},
  312. {0x86E, 0x00},
  313. {0x86F, 0x00},
  314. {0x870, 0x60},
  315. {0x871, 0x8C},
  316. {0x872, 0x10},
  317. {0x873, 0x00},
  318. {0x874, 0xE0},
  319. {0x875, 0x00},
  320. {0x876, 0x27},
  321. {0x877, 0x01},
  322. {0x878, 0x00},
  323. {0x879, 0x00},
  324. {0x87A, 0x00},
  325. {0x87B, 0x03},
  326. {0x87C, 0x00},
  327. {0x87D, 0x00},
  328. {0x87E, 0x00},
  329. {0x87F, 0x00},
  330. {0x880, 0x00},
  331. {0x881, 0x00},
  332. {0x882, 0x00},
  333. {0x883, 0x00},
  334. {0x884, 0x00},
  335. {0x885, 0x00},
  336. {0x886, 0xF8},
  337. {0x887, 0x00},
  338. {0x888, 0x03},
  339. {0x889, 0x00},
  340. {0x88A, 0x64},
  341. {0x88B, 0x00},
  342. {0x88C, 0x03},
  343. {0x88D, 0x00},
  344. {0x88E, 0xB1},
  345. {0x88F, 0x00},
  346. {0x890, 0x03},
  347. {0x891, 0x01},
  348. {0x892, 0x1D},
  349. {0x893, 0x00},
  350. {0x894, 0x03},
  351. {0x895, 0x01},
  352. {0x896, 0x4B},
  353. {0x897, 0x00},
  354. {0x898, 0xE5},
  355. {0x899, 0x00},
  356. {0x89A, 0x01},
  357. {0x89B, 0x00},
  358. {0x89C, 0x01},
  359. {0x89D, 0x04},
  360. {0x89E, 0xC8},
  361. {0x89F, 0x00},
  362. {0x8A0, 0x01},
  363. {0x8A1, 0x01},
  364. {0x8A2, 0x61},
  365. {0x8A3, 0x00},
  366. {0x8A4, 0x01},
  367. {0x8A5, 0x00},
  368. {0x8A6, 0x00},
  369. {0x8A7, 0x00},
  370. {0x8A8, 0x00},
  371. {0x8A9, 0x00},
  372. {0x8AA, 0x7F},
  373. {0x8AB, 0x03},
  374. {0x8AC, 0x00},
  375. {0x8AD, 0x00},
  376. {0x8AE, 0x00},
  377. {0x8AF, 0x00},
  378. {0x8B0, 0x00},
  379. {0x8B1, 0x00},
  380. {0x8B6, 0x00},
  381. {0x8B7, 0x01},
  382. {0x8B8, 0x00},
  383. {0x8B9, 0x00},
  384. {0x8BA, 0x02},
  385. {0x8BB, 0x00},
  386. {0x8BC, 0xFF},
  387. {0x8BD, 0x00},
  388. {0x8FE, 2},
  389. };
  390. static const struct rj54n1_reg_val bank_10[] = {
  391. {0x10bf, 0x69}
  392. };
  393. /* Clock dividers - these are default register values, divider = register + 1 */
  394. static const struct rj54n1_clock_div clk_div = {
  395. .ratio_tg = 3 /* default: 5 */,
  396. .ratio_t = 4 /* default: 1 */,
  397. .ratio_r = 4 /* default: 0 */,
  398. .ratio_op = 1 /* default: 5 */,
  399. .ratio_o = 9 /* default: 0 */,
  400. };
  401. static struct rj54n1 *to_rj54n1(const struct i2c_client *client)
  402. {
  403. return container_of(i2c_get_clientdata(client), struct rj54n1, subdev);
  404. }
  405. static int reg_read(struct i2c_client *client, const u16 reg)
  406. {
  407. struct rj54n1 *rj54n1 = to_rj54n1(client);
  408. int ret;
  409. /* set bank */
  410. if (rj54n1->bank != reg >> 8) {
  411. dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
  412. ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
  413. if (ret < 0)
  414. return ret;
  415. rj54n1->bank = reg >> 8;
  416. }
  417. return i2c_smbus_read_byte_data(client, reg & 0xff);
  418. }
  419. static int reg_write(struct i2c_client *client, const u16 reg,
  420. const u8 data)
  421. {
  422. struct rj54n1 *rj54n1 = to_rj54n1(client);
  423. int ret;
  424. /* set bank */
  425. if (rj54n1->bank != reg >> 8) {
  426. dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
  427. ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
  428. if (ret < 0)
  429. return ret;
  430. rj54n1->bank = reg >> 8;
  431. }
  432. dev_dbg(&client->dev, "[0x%x] = 0x%x\n", reg & 0xff, data);
  433. return i2c_smbus_write_byte_data(client, reg & 0xff, data);
  434. }
  435. static int reg_set(struct i2c_client *client, const u16 reg,
  436. const u8 data, const u8 mask)
  437. {
  438. int ret;
  439. ret = reg_read(client, reg);
  440. if (ret < 0)
  441. return ret;
  442. return reg_write(client, reg, (ret & ~mask) | (data & mask));
  443. }
  444. static int reg_write_multiple(struct i2c_client *client,
  445. const struct rj54n1_reg_val *rv, const int n)
  446. {
  447. int i, ret;
  448. for (i = 0; i < n; i++) {
  449. ret = reg_write(client, rv->reg, rv->val);
  450. if (ret < 0)
  451. return ret;
  452. rv++;
  453. }
  454. return 0;
  455. }
  456. static int rj54n1_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  457. enum v4l2_mbus_pixelcode *code)
  458. {
  459. if (index >= ARRAY_SIZE(rj54n1_colour_fmts))
  460. return -EINVAL;
  461. *code = rj54n1_colour_fmts[index].code;
  462. return 0;
  463. }
  464. static int rj54n1_s_stream(struct v4l2_subdev *sd, int enable)
  465. {
  466. struct i2c_client *client = v4l2_get_subdevdata(sd);
  467. /* Switch between preview and still shot modes */
  468. return reg_set(client, RJ54N1_STILL_CONTROL, (!enable) << 7, 0x80);
  469. }
  470. static int rj54n1_set_rect(struct i2c_client *client,
  471. u16 reg_x, u16 reg_y, u16 reg_xy,
  472. u32 width, u32 height)
  473. {
  474. int ret;
  475. ret = reg_write(client, reg_xy,
  476. ((width >> 4) & 0x70) |
  477. ((height >> 8) & 7));
  478. if (!ret)
  479. ret = reg_write(client, reg_x, width & 0xff);
  480. if (!ret)
  481. ret = reg_write(client, reg_y, height & 0xff);
  482. return ret;
  483. }
  484. /*
  485. * Some commands, specifically certain initialisation sequences, require
  486. * a commit operation.
  487. */
  488. static int rj54n1_commit(struct i2c_client *client)
  489. {
  490. int ret = reg_write(client, RJ54N1_INIT_START, 1);
  491. msleep(10);
  492. if (!ret)
  493. ret = reg_write(client, RJ54N1_INIT_START, 0);
  494. return ret;
  495. }
  496. static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
  497. s32 *out_w, s32 *out_h);
  498. static int rj54n1_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
  499. {
  500. struct i2c_client *client = v4l2_get_subdevdata(sd);
  501. struct rj54n1 *rj54n1 = to_rj54n1(client);
  502. const struct v4l2_rect *rect = &a->c;
  503. int dummy = 0, output_w, output_h,
  504. input_w = rect->width, input_h = rect->height;
  505. int ret;
  506. /* arbitrary minimum width and height, edges unimportant */
  507. soc_camera_limit_side(&dummy, &input_w,
  508. RJ54N1_COLUMN_SKIP, 8, RJ54N1_MAX_WIDTH);
  509. soc_camera_limit_side(&dummy, &input_h,
  510. RJ54N1_ROW_SKIP, 8, RJ54N1_MAX_HEIGHT);
  511. output_w = (input_w * 1024 + rj54n1->resize / 2) / rj54n1->resize;
  512. output_h = (input_h * 1024 + rj54n1->resize / 2) / rj54n1->resize;
  513. dev_dbg(&client->dev, "Scaling for %dx%d : %u = %dx%d\n",
  514. input_w, input_h, rj54n1->resize, output_w, output_h);
  515. ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
  516. if (ret < 0)
  517. return ret;
  518. rj54n1->width = output_w;
  519. rj54n1->height = output_h;
  520. rj54n1->resize = ret;
  521. rj54n1->rect.width = input_w;
  522. rj54n1->rect.height = input_h;
  523. return 0;
  524. }
  525. static int rj54n1_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  526. {
  527. struct i2c_client *client = v4l2_get_subdevdata(sd);
  528. struct rj54n1 *rj54n1 = to_rj54n1(client);
  529. a->c = rj54n1->rect;
  530. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  531. return 0;
  532. }
  533. static int rj54n1_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  534. {
  535. a->bounds.left = RJ54N1_COLUMN_SKIP;
  536. a->bounds.top = RJ54N1_ROW_SKIP;
  537. a->bounds.width = RJ54N1_MAX_WIDTH;
  538. a->bounds.height = RJ54N1_MAX_HEIGHT;
  539. a->defrect = a->bounds;
  540. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  541. a->pixelaspect.numerator = 1;
  542. a->pixelaspect.denominator = 1;
  543. return 0;
  544. }
  545. static int rj54n1_g_fmt(struct v4l2_subdev *sd,
  546. struct v4l2_mbus_framefmt *mf)
  547. {
  548. struct i2c_client *client = v4l2_get_subdevdata(sd);
  549. struct rj54n1 *rj54n1 = to_rj54n1(client);
  550. mf->code = rj54n1->fmt->code;
  551. mf->colorspace = rj54n1->fmt->colorspace;
  552. mf->field = V4L2_FIELD_NONE;
  553. mf->width = rj54n1->width;
  554. mf->height = rj54n1->height;
  555. return 0;
  556. }
  557. /*
  558. * The actual geometry configuration routine. It scales the input window into
  559. * the output one, updates the window sizes and returns an error or the resize
  560. * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
  561. */
  562. static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
  563. s32 *out_w, s32 *out_h)
  564. {
  565. struct i2c_client *client = v4l2_get_subdevdata(sd);
  566. struct rj54n1 *rj54n1 = to_rj54n1(client);
  567. unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
  568. output_w = *out_w, output_h = *out_h;
  569. u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
  570. unsigned int peak, peak_50, peak_60;
  571. int ret;
  572. /*
  573. * We have a problem with crops, where the window is larger than 512x384
  574. * and output window is larger than a half of the input one. In this
  575. * case we have to either reduce the input window to equal or below
  576. * 512x384 or the output window to equal or below 1/2 of the input.
  577. */
  578. if (output_w > max(512U, input_w / 2)) {
  579. if (2 * output_w > RJ54N1_MAX_WIDTH) {
  580. input_w = RJ54N1_MAX_WIDTH;
  581. output_w = RJ54N1_MAX_WIDTH / 2;
  582. } else {
  583. input_w = output_w * 2;
  584. }
  585. dev_dbg(&client->dev, "Adjusted output width: in %u, out %u\n",
  586. input_w, output_w);
  587. }
  588. if (output_h > max(384U, input_h / 2)) {
  589. if (2 * output_h > RJ54N1_MAX_HEIGHT) {
  590. input_h = RJ54N1_MAX_HEIGHT;
  591. output_h = RJ54N1_MAX_HEIGHT / 2;
  592. } else {
  593. input_h = output_h * 2;
  594. }
  595. dev_dbg(&client->dev, "Adjusted output height: in %u, out %u\n",
  596. input_h, output_h);
  597. }
  598. /* Idea: use the read mode for snapshots, handle separate geometries */
  599. ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
  600. RJ54N1_Y_OUTPUT_SIZE_S_L,
  601. RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
  602. if (!ret)
  603. ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
  604. RJ54N1_Y_OUTPUT_SIZE_P_L,
  605. RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
  606. if (ret < 0)
  607. return ret;
  608. if (output_w > input_w && output_h > input_h) {
  609. input_w = output_w;
  610. input_h = output_h;
  611. resize = 1024;
  612. } else {
  613. unsigned int resize_x, resize_y;
  614. resize_x = (input_w * 1024 + output_w / 2) / output_w;
  615. resize_y = (input_h * 1024 + output_h / 2) / output_h;
  616. /* We want max(resize_x, resize_y), check if it still fits */
  617. if (resize_x > resize_y &&
  618. (output_h * resize_x + 512) / 1024 > RJ54N1_MAX_HEIGHT)
  619. resize = (RJ54N1_MAX_HEIGHT * 1024 + output_h / 2) /
  620. output_h;
  621. else if (resize_y > resize_x &&
  622. (output_w * resize_y + 512) / 1024 > RJ54N1_MAX_WIDTH)
  623. resize = (RJ54N1_MAX_WIDTH * 1024 + output_w / 2) /
  624. output_w;
  625. else
  626. resize = max(resize_x, resize_y);
  627. /* Prohibited value ranges */
  628. switch (resize) {
  629. case 2040 ... 2047:
  630. resize = 2039;
  631. break;
  632. case 4080 ... 4095:
  633. resize = 4079;
  634. break;
  635. case 8160 ... 8191:
  636. resize = 8159;
  637. break;
  638. case 16320 ... 16384:
  639. resize = 16319;
  640. }
  641. }
  642. /* Set scaling */
  643. ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff);
  644. if (!ret)
  645. ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8);
  646. if (ret < 0)
  647. return ret;
  648. /*
  649. * Configure a skipping bitmask. The sensor will select a skipping value
  650. * among set bits automatically. This is very unclear in the datasheet
  651. * too. I was told, in this register one enables all skipping values,
  652. * that are required for a specific resize, and the camera selects
  653. * automatically, which ones to use. But it is unclear how to identify,
  654. * which cropping values are needed. Secondly, why don't we just set all
  655. * bits and let the camera choose? Would it increase processing time and
  656. * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
  657. * improve the image quality or stability for larger frames (see comment
  658. * above), but I didn't check the framerate.
  659. */
  660. skip = min(resize / 1024, 15U);
  661. inc_sel = 1 << skip;
  662. if (inc_sel <= 2)
  663. inc_sel = 0xc;
  664. else if (resize & 1023 && skip < 15)
  665. inc_sel |= 1 << (skip + 1);
  666. ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc);
  667. if (!ret)
  668. ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8);
  669. if (!rj54n1->auto_wb) {
  670. /* Auto white balance window */
  671. wb_left = output_w / 16;
  672. wb_right = (3 * output_w / 4 - 3) / 4;
  673. wb_top = output_h / 16;
  674. wb_bottom = (3 * output_h / 4 - 3) / 4;
  675. wb_bit8 = ((wb_left >> 2) & 0x40) | ((wb_top >> 4) & 0x10) |
  676. ((wb_right >> 6) & 4) | ((wb_bottom >> 8) & 1);
  677. if (!ret)
  678. ret = reg_write(client, RJ54N1_BIT8_WB, wb_bit8);
  679. if (!ret)
  680. ret = reg_write(client, RJ54N1_HCAPS_WB, wb_left);
  681. if (!ret)
  682. ret = reg_write(client, RJ54N1_VCAPS_WB, wb_top);
  683. if (!ret)
  684. ret = reg_write(client, RJ54N1_HCAPE_WB, wb_right);
  685. if (!ret)
  686. ret = reg_write(client, RJ54N1_VCAPE_WB, wb_bottom);
  687. }
  688. /* Antiflicker */
  689. peak = 12 * RJ54N1_MAX_WIDTH * (1 << 14) * resize / rj54n1->tgclk_mhz /
  690. 10000;
  691. peak_50 = peak / 6;
  692. peak_60 = peak / 5;
  693. if (!ret)
  694. ret = reg_write(client, RJ54N1_PEAK_H,
  695. ((peak_50 >> 4) & 0xf0) | (peak_60 >> 8));
  696. if (!ret)
  697. ret = reg_write(client, RJ54N1_PEAK_50, peak_50);
  698. if (!ret)
  699. ret = reg_write(client, RJ54N1_PEAK_60, peak_60);
  700. if (!ret)
  701. ret = reg_write(client, RJ54N1_PEAK_DIFF, peak / 150);
  702. /* Start resizing */
  703. if (!ret)
  704. ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
  705. RESIZE_HOLD_SEL | RESIZE_GO | 1);
  706. if (ret < 0)
  707. return ret;
  708. /* Constant taken from manufacturer's example */
  709. msleep(230);
  710. ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1);
  711. if (ret < 0)
  712. return ret;
  713. *in_w = (output_w * resize + 512) / 1024;
  714. *in_h = (output_h * resize + 512) / 1024;
  715. *out_w = output_w;
  716. *out_h = output_h;
  717. dev_dbg(&client->dev, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
  718. *in_w, *in_h, resize, output_w, output_h, skip);
  719. return resize;
  720. }
  721. static int rj54n1_set_clock(struct i2c_client *client)
  722. {
  723. struct rj54n1 *rj54n1 = to_rj54n1(client);
  724. int ret;
  725. /* Enable external clock */
  726. ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
  727. /* Leave stand-by. Note: use this when implementing suspend / resume */
  728. if (!ret)
  729. ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
  730. if (!ret)
  731. ret = reg_write(client, RJ54N1_PLL_L, PLL_L);
  732. if (!ret)
  733. ret = reg_write(client, RJ54N1_PLL_N, PLL_N);
  734. /* TGCLK dividers */
  735. if (!ret)
  736. ret = reg_write(client, RJ54N1_RATIO_TG,
  737. rj54n1->clk_div.ratio_tg);
  738. if (!ret)
  739. ret = reg_write(client, RJ54N1_RATIO_T,
  740. rj54n1->clk_div.ratio_t);
  741. if (!ret)
  742. ret = reg_write(client, RJ54N1_RATIO_R,
  743. rj54n1->clk_div.ratio_r);
  744. /* Enable TGCLK & RAMP */
  745. if (!ret)
  746. ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3);
  747. /* Disable clock output */
  748. if (!ret)
  749. ret = reg_write(client, RJ54N1_OCLK_DSP, 0);
  750. /* Set divisors */
  751. if (!ret)
  752. ret = reg_write(client, RJ54N1_RATIO_OP,
  753. rj54n1->clk_div.ratio_op);
  754. if (!ret)
  755. ret = reg_write(client, RJ54N1_RATIO_O,
  756. rj54n1->clk_div.ratio_o);
  757. /* Enable OCLK */
  758. if (!ret)
  759. ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
  760. /* Use PLL for Timing Generator, write 2 to reserved bits */
  761. if (!ret)
  762. ret = reg_write(client, RJ54N1_TG_BYPASS, 2);
  763. /* Take sensor out of reset */
  764. if (!ret)
  765. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  766. E_EXCLK | SEN_RSTX);
  767. /* Enable PLL */
  768. if (!ret)
  769. ret = reg_write(client, RJ54N1_PLL_EN, 1);
  770. /* Wait for PLL to stabilise */
  771. msleep(10);
  772. /* Enable clock to frequency divider */
  773. if (!ret)
  774. ret = reg_write(client, RJ54N1_CLK_RST, 1);
  775. if (!ret)
  776. ret = reg_read(client, RJ54N1_CLK_RST);
  777. if (ret != 1) {
  778. dev_err(&client->dev,
  779. "Resetting RJ54N1CB0C clock failed: %d!\n", ret);
  780. return -EIO;
  781. }
  782. /* Start the PLL */
  783. ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1);
  784. /* Enable OCLK */
  785. if (!ret)
  786. ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
  787. return ret;
  788. }
  789. static int rj54n1_reg_init(struct i2c_client *client)
  790. {
  791. struct rj54n1 *rj54n1 = to_rj54n1(client);
  792. int ret = rj54n1_set_clock(client);
  793. if (!ret)
  794. ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
  795. if (!ret)
  796. ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
  797. /* Set binning divisors */
  798. if (!ret)
  799. ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4));
  800. if (!ret)
  801. ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf);
  802. /* Switch to fixed resize mode */
  803. if (!ret)
  804. ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
  805. RESIZE_HOLD_SEL | 1);
  806. /* Set gain */
  807. if (!ret)
  808. ret = reg_write(client, RJ54N1_Y_GAIN, 0x84);
  809. /*
  810. * Mirror the image back: default is upside down and left-to-right...
  811. * Set manual preview / still shot switching
  812. */
  813. if (!ret)
  814. ret = reg_write(client, RJ54N1_MIRROR_STILL_MODE, 0x27);
  815. if (!ret)
  816. ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
  817. /* Auto exposure area */
  818. if (!ret)
  819. ret = reg_write(client, RJ54N1_EXPOSURE_CONTROL, 0x80);
  820. /* Check current auto WB config */
  821. if (!ret)
  822. ret = reg_read(client, RJ54N1_WB_SEL_WEIGHT_I);
  823. if (ret >= 0) {
  824. rj54n1->auto_wb = ret & 0x80;
  825. ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
  826. }
  827. if (!ret)
  828. ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
  829. if (!ret)
  830. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  831. E_EXCLK | DSP_RSTX | SEN_RSTX);
  832. /* Commit init */
  833. if (!ret)
  834. ret = rj54n1_commit(client);
  835. /* Take DSP, TG, sensor out of reset */
  836. if (!ret)
  837. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  838. E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
  839. /* Start register update? Same register as 0x?FE in many bank_* sets */
  840. if (!ret)
  841. ret = reg_write(client, RJ54N1_FWFLG, 2);
  842. /* Constant taken from manufacturer's example */
  843. msleep(700);
  844. return ret;
  845. }
  846. static int rj54n1_try_fmt(struct v4l2_subdev *sd,
  847. struct v4l2_mbus_framefmt *mf)
  848. {
  849. struct i2c_client *client = v4l2_get_subdevdata(sd);
  850. struct rj54n1 *rj54n1 = to_rj54n1(client);
  851. const struct rj54n1_datafmt *fmt;
  852. int align = mf->code == V4L2_MBUS_FMT_SBGGR10_1X10 ||
  853. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE ||
  854. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE ||
  855. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE ||
  856. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE;
  857. dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
  858. __func__, mf->code, mf->width, mf->height);
  859. fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
  860. ARRAY_SIZE(rj54n1_colour_fmts));
  861. if (!fmt) {
  862. fmt = rj54n1->fmt;
  863. mf->code = fmt->code;
  864. }
  865. mf->field = V4L2_FIELD_NONE;
  866. mf->colorspace = fmt->colorspace;
  867. v4l_bound_align_image(&mf->width, 112, RJ54N1_MAX_WIDTH, align,
  868. &mf->height, 84, RJ54N1_MAX_HEIGHT, align, 0);
  869. return 0;
  870. }
  871. static int rj54n1_s_fmt(struct v4l2_subdev *sd,
  872. struct v4l2_mbus_framefmt *mf)
  873. {
  874. struct i2c_client *client = v4l2_get_subdevdata(sd);
  875. struct rj54n1 *rj54n1 = to_rj54n1(client);
  876. const struct rj54n1_datafmt *fmt;
  877. int output_w, output_h, max_w, max_h,
  878. input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
  879. int ret;
  880. /*
  881. * The host driver can call us without .try_fmt(), so, we have to take
  882. * care ourseleves
  883. */
  884. rj54n1_try_fmt(sd, mf);
  885. /*
  886. * Verify if the sensor has just been powered on. TODO: replace this
  887. * with proper PM, when a suitable API is available.
  888. */
  889. ret = reg_read(client, RJ54N1_RESET_STANDBY);
  890. if (ret < 0)
  891. return ret;
  892. if (!(ret & E_EXCLK)) {
  893. ret = rj54n1_reg_init(client);
  894. if (ret < 0)
  895. return ret;
  896. }
  897. dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
  898. __func__, mf->code, mf->width, mf->height);
  899. /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
  900. switch (mf->code) {
  901. case V4L2_MBUS_FMT_YUYV8_2X8:
  902. ret = reg_write(client, RJ54N1_OUT_SEL, 0);
  903. if (!ret)
  904. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  905. break;
  906. case V4L2_MBUS_FMT_YVYU8_2X8:
  907. ret = reg_write(client, RJ54N1_OUT_SEL, 0);
  908. if (!ret)
  909. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  910. break;
  911. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  912. ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
  913. if (!ret)
  914. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  915. break;
  916. case V4L2_MBUS_FMT_RGB565_2X8_BE:
  917. ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
  918. if (!ret)
  919. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  920. break;
  921. case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE:
  922. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  923. if (!ret)
  924. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  925. if (!ret)
  926. ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
  927. break;
  928. case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
  929. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  930. if (!ret)
  931. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  932. if (!ret)
  933. ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
  934. break;
  935. case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE:
  936. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  937. if (!ret)
  938. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  939. if (!ret)
  940. ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
  941. break;
  942. case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE:
  943. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  944. if (!ret)
  945. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  946. if (!ret)
  947. ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
  948. break;
  949. case V4L2_MBUS_FMT_SBGGR10_1X10:
  950. ret = reg_write(client, RJ54N1_OUT_SEL, 5);
  951. break;
  952. default:
  953. ret = -EINVAL;
  954. }
  955. /* Special case: a raw mode with 10 bits of data per clock tick */
  956. if (!ret)
  957. ret = reg_set(client, RJ54N1_OCLK_SEL_EN,
  958. (mf->code == V4L2_MBUS_FMT_SBGGR10_1X10) << 1, 2);
  959. if (ret < 0)
  960. return ret;
  961. /* Supported scales 1:1 >= scale > 1:16 */
  962. max_w = mf->width * (16 * 1024 - 1) / 1024;
  963. if (input_w > max_w)
  964. input_w = max_w;
  965. max_h = mf->height * (16 * 1024 - 1) / 1024;
  966. if (input_h > max_h)
  967. input_h = max_h;
  968. output_w = mf->width;
  969. output_h = mf->height;
  970. ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
  971. if (ret < 0)
  972. return ret;
  973. fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
  974. ARRAY_SIZE(rj54n1_colour_fmts));
  975. rj54n1->fmt = fmt;
  976. rj54n1->resize = ret;
  977. rj54n1->rect.width = input_w;
  978. rj54n1->rect.height = input_h;
  979. rj54n1->width = output_w;
  980. rj54n1->height = output_h;
  981. mf->width = output_w;
  982. mf->height = output_h;
  983. mf->field = V4L2_FIELD_NONE;
  984. mf->colorspace = fmt->colorspace;
  985. return 0;
  986. }
  987. #ifdef CONFIG_VIDEO_ADV_DEBUG
  988. static int rj54n1_g_register(struct v4l2_subdev *sd,
  989. struct v4l2_dbg_register *reg)
  990. {
  991. struct i2c_client *client = v4l2_get_subdevdata(sd);
  992. if (reg->reg < 0x400 || reg->reg > 0x1fff)
  993. /* Registers > 0x0800 are only available from Sharp support */
  994. return -EINVAL;
  995. reg->size = 1;
  996. reg->val = reg_read(client, reg->reg);
  997. if (reg->val > 0xff)
  998. return -EIO;
  999. return 0;
  1000. }
  1001. static int rj54n1_s_register(struct v4l2_subdev *sd,
  1002. const struct v4l2_dbg_register *reg)
  1003. {
  1004. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1005. if (reg->reg < 0x400 || reg->reg > 0x1fff)
  1006. /* Registers >= 0x0800 are only available from Sharp support */
  1007. return -EINVAL;
  1008. if (reg_write(client, reg->reg, reg->val) < 0)
  1009. return -EIO;
  1010. return 0;
  1011. }
  1012. #endif
  1013. static int rj54n1_s_power(struct v4l2_subdev *sd, int on)
  1014. {
  1015. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1016. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  1017. struct rj54n1 *rj54n1 = to_rj54n1(client);
  1018. return soc_camera_set_power(&client->dev, ssdd, rj54n1->clk, on);
  1019. }
  1020. static int rj54n1_s_ctrl(struct v4l2_ctrl *ctrl)
  1021. {
  1022. struct rj54n1 *rj54n1 = container_of(ctrl->handler, struct rj54n1, hdl);
  1023. struct v4l2_subdev *sd = &rj54n1->subdev;
  1024. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1025. int data;
  1026. switch (ctrl->id) {
  1027. case V4L2_CID_VFLIP:
  1028. if (ctrl->val)
  1029. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1);
  1030. else
  1031. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1);
  1032. if (data < 0)
  1033. return -EIO;
  1034. return 0;
  1035. case V4L2_CID_HFLIP:
  1036. if (ctrl->val)
  1037. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2);
  1038. else
  1039. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2);
  1040. if (data < 0)
  1041. return -EIO;
  1042. return 0;
  1043. case V4L2_CID_GAIN:
  1044. if (reg_write(client, RJ54N1_Y_GAIN, ctrl->val * 2) < 0)
  1045. return -EIO;
  1046. return 0;
  1047. case V4L2_CID_AUTO_WHITE_BALANCE:
  1048. /* Auto WB area - whole image */
  1049. if (reg_set(client, RJ54N1_WB_SEL_WEIGHT_I, ctrl->val << 7,
  1050. 0x80) < 0)
  1051. return -EIO;
  1052. rj54n1->auto_wb = ctrl->val;
  1053. return 0;
  1054. }
  1055. return -EINVAL;
  1056. }
  1057. static const struct v4l2_ctrl_ops rj54n1_ctrl_ops = {
  1058. .s_ctrl = rj54n1_s_ctrl,
  1059. };
  1060. static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
  1061. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1062. .g_register = rj54n1_g_register,
  1063. .s_register = rj54n1_s_register,
  1064. #endif
  1065. .s_power = rj54n1_s_power,
  1066. };
  1067. static int rj54n1_g_mbus_config(struct v4l2_subdev *sd,
  1068. struct v4l2_mbus_config *cfg)
  1069. {
  1070. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1071. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  1072. cfg->flags =
  1073. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  1074. V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH |
  1075. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  1076. cfg->type = V4L2_MBUS_PARALLEL;
  1077. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  1078. return 0;
  1079. }
  1080. static int rj54n1_s_mbus_config(struct v4l2_subdev *sd,
  1081. const struct v4l2_mbus_config *cfg)
  1082. {
  1083. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1084. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  1085. /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
  1086. if (soc_camera_apply_board_flags(ssdd, cfg) &
  1087. V4L2_MBUS_PCLK_SAMPLE_RISING)
  1088. return reg_write(client, RJ54N1_OUT_SIGPO, 1 << 4);
  1089. else
  1090. return reg_write(client, RJ54N1_OUT_SIGPO, 0);
  1091. }
  1092. static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
  1093. .s_stream = rj54n1_s_stream,
  1094. .s_mbus_fmt = rj54n1_s_fmt,
  1095. .g_mbus_fmt = rj54n1_g_fmt,
  1096. .try_mbus_fmt = rj54n1_try_fmt,
  1097. .enum_mbus_fmt = rj54n1_enum_fmt,
  1098. .g_crop = rj54n1_g_crop,
  1099. .s_crop = rj54n1_s_crop,
  1100. .cropcap = rj54n1_cropcap,
  1101. .g_mbus_config = rj54n1_g_mbus_config,
  1102. .s_mbus_config = rj54n1_s_mbus_config,
  1103. };
  1104. static struct v4l2_subdev_ops rj54n1_subdev_ops = {
  1105. .core = &rj54n1_subdev_core_ops,
  1106. .video = &rj54n1_subdev_video_ops,
  1107. };
  1108. /*
  1109. * Interface active, can use i2c. If it fails, it can indeed mean, that
  1110. * this wasn't our capture interface, so, we wait for the right one
  1111. */
  1112. static int rj54n1_video_probe(struct i2c_client *client,
  1113. struct rj54n1_pdata *priv)
  1114. {
  1115. struct rj54n1 *rj54n1 = to_rj54n1(client);
  1116. int data1, data2;
  1117. int ret;
  1118. ret = rj54n1_s_power(&rj54n1->subdev, 1);
  1119. if (ret < 0)
  1120. return ret;
  1121. /* Read out the chip version register */
  1122. data1 = reg_read(client, RJ54N1_DEV_CODE);
  1123. data2 = reg_read(client, RJ54N1_DEV_CODE2);
  1124. if (data1 != 0x51 || data2 != 0x10) {
  1125. ret = -ENODEV;
  1126. dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
  1127. data1, data2);
  1128. goto done;
  1129. }
  1130. /* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
  1131. ret = reg_write(client, RJ54N1_IOC, priv->ioctl_high << 7);
  1132. if (ret < 0)
  1133. goto done;
  1134. dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
  1135. data1, data2);
  1136. ret = v4l2_ctrl_handler_setup(&rj54n1->hdl);
  1137. done:
  1138. rj54n1_s_power(&rj54n1->subdev, 0);
  1139. return ret;
  1140. }
  1141. static int rj54n1_probe(struct i2c_client *client,
  1142. const struct i2c_device_id *did)
  1143. {
  1144. struct rj54n1 *rj54n1;
  1145. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  1146. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1147. struct rj54n1_pdata *rj54n1_priv;
  1148. int ret;
  1149. if (!ssdd || !ssdd->drv_priv) {
  1150. dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n");
  1151. return -EINVAL;
  1152. }
  1153. rj54n1_priv = ssdd->drv_priv;
  1154. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1155. dev_warn(&adapter->dev,
  1156. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
  1157. return -EIO;
  1158. }
  1159. rj54n1 = devm_kzalloc(&client->dev, sizeof(struct rj54n1), GFP_KERNEL);
  1160. if (!rj54n1)
  1161. return -ENOMEM;
  1162. v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops);
  1163. v4l2_ctrl_handler_init(&rj54n1->hdl, 4);
  1164. v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
  1165. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1166. v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
  1167. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1168. v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
  1169. V4L2_CID_GAIN, 0, 127, 1, 66);
  1170. v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
  1171. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  1172. rj54n1->subdev.ctrl_handler = &rj54n1->hdl;
  1173. if (rj54n1->hdl.error)
  1174. return rj54n1->hdl.error;
  1175. rj54n1->clk_div = clk_div;
  1176. rj54n1->rect.left = RJ54N1_COLUMN_SKIP;
  1177. rj54n1->rect.top = RJ54N1_ROW_SKIP;
  1178. rj54n1->rect.width = RJ54N1_MAX_WIDTH;
  1179. rj54n1->rect.height = RJ54N1_MAX_HEIGHT;
  1180. rj54n1->width = RJ54N1_MAX_WIDTH;
  1181. rj54n1->height = RJ54N1_MAX_HEIGHT;
  1182. rj54n1->fmt = &rj54n1_colour_fmts[0];
  1183. rj54n1->resize = 1024;
  1184. rj54n1->tgclk_mhz = (rj54n1_priv->mclk_freq / PLL_L * PLL_N) /
  1185. (clk_div.ratio_tg + 1) / (clk_div.ratio_t + 1);
  1186. rj54n1->clk = v4l2_clk_get(&client->dev, "mclk");
  1187. if (IS_ERR(rj54n1->clk)) {
  1188. ret = PTR_ERR(rj54n1->clk);
  1189. goto eclkget;
  1190. }
  1191. ret = rj54n1_video_probe(client, rj54n1_priv);
  1192. if (ret < 0) {
  1193. v4l2_clk_put(rj54n1->clk);
  1194. eclkget:
  1195. v4l2_ctrl_handler_free(&rj54n1->hdl);
  1196. }
  1197. return ret;
  1198. }
  1199. static int rj54n1_remove(struct i2c_client *client)
  1200. {
  1201. struct rj54n1 *rj54n1 = to_rj54n1(client);
  1202. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  1203. v4l2_clk_put(rj54n1->clk);
  1204. v4l2_device_unregister_subdev(&rj54n1->subdev);
  1205. if (ssdd->free_bus)
  1206. ssdd->free_bus(ssdd);
  1207. v4l2_ctrl_handler_free(&rj54n1->hdl);
  1208. return 0;
  1209. }
  1210. static const struct i2c_device_id rj54n1_id[] = {
  1211. { "rj54n1cb0c", 0 },
  1212. { }
  1213. };
  1214. MODULE_DEVICE_TABLE(i2c, rj54n1_id);
  1215. static struct i2c_driver rj54n1_i2c_driver = {
  1216. .driver = {
  1217. .name = "rj54n1cb0c",
  1218. },
  1219. .probe = rj54n1_probe,
  1220. .remove = rj54n1_remove,
  1221. .id_table = rj54n1_id,
  1222. };
  1223. module_i2c_driver(rj54n1_i2c_driver);
  1224. MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
  1225. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  1226. MODULE_LICENSE("GPL v2");