rj54n1cb0c.c 36 KB

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