rj54n1cb0c.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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/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, V4L2_COLORSPACE_JPEG},
  119. {V4L2_MBUS_FMT_YVYU8_2X8, 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. static const 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. static const 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. static const struct rj54n1_reg_val bank_7[] = {
  193. {0x70a, 0},
  194. {0x714, 0xff},
  195. {0x715, 0xff},
  196. {0x716, 0x1f},
  197. {0x7FE, 2},
  198. };
  199. static const 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. static const struct rj54n1_reg_val bank_10[] = {
  387. {0x10bf, 0x69}
  388. };
  389. /* Clock dividers - these are default register values, divider = register + 1 */
  390. static const 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, unsigned int index,
  453. enum v4l2_mbus_pixelcode *code)
  454. {
  455. if (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 = v4l2_get_subdevdata(sd);
  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_rect(struct i2c_client *client,
  467. u16 reg_x, u16 reg_y, u16 reg_xy,
  468. u32 width, u32 height)
  469. {
  470. int ret;
  471. ret = reg_write(client, reg_xy,
  472. ((width >> 4) & 0x70) |
  473. ((height >> 8) & 7));
  474. if (!ret)
  475. ret = reg_write(client, reg_x, width & 0xff);
  476. if (!ret)
  477. ret = reg_write(client, reg_y, height & 0xff);
  478. return ret;
  479. }
  480. /*
  481. * Some commands, specifically certain initialisation sequences, require
  482. * a commit operation.
  483. */
  484. static int rj54n1_commit(struct i2c_client *client)
  485. {
  486. int ret = reg_write(client, RJ54N1_INIT_START, 1);
  487. msleep(10);
  488. if (!ret)
  489. ret = reg_write(client, RJ54N1_INIT_START, 0);
  490. return ret;
  491. }
  492. static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
  493. s32 *out_w, s32 *out_h);
  494. static int rj54n1_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  495. {
  496. struct i2c_client *client = v4l2_get_subdevdata(sd);
  497. struct rj54n1 *rj54n1 = to_rj54n1(client);
  498. struct v4l2_rect *rect = &a->c;
  499. int dummy = 0, output_w, output_h,
  500. input_w = rect->width, input_h = rect->height;
  501. int ret;
  502. /* arbitrary minimum width and height, edges unimportant */
  503. soc_camera_limit_side(&dummy, &input_w,
  504. RJ54N1_COLUMN_SKIP, 8, RJ54N1_MAX_WIDTH);
  505. soc_camera_limit_side(&dummy, &input_h,
  506. RJ54N1_ROW_SKIP, 8, RJ54N1_MAX_HEIGHT);
  507. output_w = (input_w * 1024 + rj54n1->resize / 2) / rj54n1->resize;
  508. output_h = (input_h * 1024 + rj54n1->resize / 2) / rj54n1->resize;
  509. dev_dbg(&client->dev, "Scaling for %dx%d : %u = %dx%d\n",
  510. input_w, input_h, rj54n1->resize, output_w, output_h);
  511. ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
  512. if (ret < 0)
  513. return ret;
  514. rj54n1->width = output_w;
  515. rj54n1->height = output_h;
  516. rj54n1->resize = ret;
  517. rj54n1->rect.width = input_w;
  518. rj54n1->rect.height = input_h;
  519. return 0;
  520. }
  521. static int rj54n1_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  522. {
  523. struct i2c_client *client = v4l2_get_subdevdata(sd);
  524. struct rj54n1 *rj54n1 = to_rj54n1(client);
  525. a->c = rj54n1->rect;
  526. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  527. return 0;
  528. }
  529. static int rj54n1_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  530. {
  531. a->bounds.left = RJ54N1_COLUMN_SKIP;
  532. a->bounds.top = RJ54N1_ROW_SKIP;
  533. a->bounds.width = RJ54N1_MAX_WIDTH;
  534. a->bounds.height = RJ54N1_MAX_HEIGHT;
  535. a->defrect = a->bounds;
  536. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  537. a->pixelaspect.numerator = 1;
  538. a->pixelaspect.denominator = 1;
  539. return 0;
  540. }
  541. static int rj54n1_g_fmt(struct v4l2_subdev *sd,
  542. struct v4l2_mbus_framefmt *mf)
  543. {
  544. struct i2c_client *client = v4l2_get_subdevdata(sd);
  545. struct rj54n1 *rj54n1 = to_rj54n1(client);
  546. mf->code = rj54n1->fmt->code;
  547. mf->colorspace = rj54n1->fmt->colorspace;
  548. mf->field = V4L2_FIELD_NONE;
  549. mf->width = rj54n1->width;
  550. mf->height = rj54n1->height;
  551. return 0;
  552. }
  553. /*
  554. * The actual geometry configuration routine. It scales the input window into
  555. * the output one, updates the window sizes and returns an error or the resize
  556. * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
  557. */
  558. static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
  559. s32 *out_w, s32 *out_h)
  560. {
  561. struct i2c_client *client = v4l2_get_subdevdata(sd);
  562. struct rj54n1 *rj54n1 = to_rj54n1(client);
  563. unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
  564. output_w = *out_w, output_h = *out_h;
  565. u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
  566. unsigned int peak, peak_50, peak_60;
  567. int ret;
  568. /*
  569. * We have a problem with crops, where the window is larger than 512x384
  570. * and output window is larger than a half of the input one. In this
  571. * case we have to either reduce the input window to equal or below
  572. * 512x384 or the output window to equal or below 1/2 of the input.
  573. */
  574. if (output_w > max(512U, input_w / 2)) {
  575. if (2 * output_w > RJ54N1_MAX_WIDTH) {
  576. input_w = RJ54N1_MAX_WIDTH;
  577. output_w = RJ54N1_MAX_WIDTH / 2;
  578. } else {
  579. input_w = output_w * 2;
  580. }
  581. dev_dbg(&client->dev, "Adjusted output width: in %u, out %u\n",
  582. input_w, output_w);
  583. }
  584. if (output_h > max(384U, input_h / 2)) {
  585. if (2 * output_h > RJ54N1_MAX_HEIGHT) {
  586. input_h = RJ54N1_MAX_HEIGHT;
  587. output_h = RJ54N1_MAX_HEIGHT / 2;
  588. } else {
  589. input_h = output_h * 2;
  590. }
  591. dev_dbg(&client->dev, "Adjusted output height: in %u, out %u\n",
  592. input_h, output_h);
  593. }
  594. /* Idea: use the read mode for snapshots, handle separate geometries */
  595. ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
  596. RJ54N1_Y_OUTPUT_SIZE_S_L,
  597. RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
  598. if (!ret)
  599. ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
  600. RJ54N1_Y_OUTPUT_SIZE_P_L,
  601. RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
  602. if (ret < 0)
  603. return ret;
  604. if (output_w > input_w && output_h > input_h) {
  605. input_w = output_w;
  606. input_h = output_h;
  607. resize = 1024;
  608. } else {
  609. unsigned int resize_x, resize_y;
  610. resize_x = (input_w * 1024 + output_w / 2) / output_w;
  611. resize_y = (input_h * 1024 + output_h / 2) / output_h;
  612. /* We want max(resize_x, resize_y), check if it still fits */
  613. if (resize_x > resize_y &&
  614. (output_h * resize_x + 512) / 1024 > RJ54N1_MAX_HEIGHT)
  615. resize = (RJ54N1_MAX_HEIGHT * 1024 + output_h / 2) /
  616. output_h;
  617. else if (resize_y > resize_x &&
  618. (output_w * resize_y + 512) / 1024 > RJ54N1_MAX_WIDTH)
  619. resize = (RJ54N1_MAX_WIDTH * 1024 + output_w / 2) /
  620. output_w;
  621. else
  622. resize = max(resize_x, resize_y);
  623. /* Prohibited value ranges */
  624. switch (resize) {
  625. case 2040 ... 2047:
  626. resize = 2039;
  627. break;
  628. case 4080 ... 4095:
  629. resize = 4079;
  630. break;
  631. case 8160 ... 8191:
  632. resize = 8159;
  633. break;
  634. case 16320 ... 16384:
  635. resize = 16319;
  636. }
  637. }
  638. /* Set scaling */
  639. ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff);
  640. if (!ret)
  641. ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8);
  642. if (ret < 0)
  643. return ret;
  644. /*
  645. * Configure a skipping bitmask. The sensor will select a skipping value
  646. * among set bits automatically. This is very unclear in the datasheet
  647. * too. I was told, in this register one enables all skipping values,
  648. * that are required for a specific resize, and the camera selects
  649. * automatically, which ones to use. But it is unclear how to identify,
  650. * which cropping values are needed. Secondly, why don't we just set all
  651. * bits and let the camera choose? Would it increase processing time and
  652. * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
  653. * improve the image quality or stability for larger frames (see comment
  654. * above), but I didn't check the framerate.
  655. */
  656. skip = min(resize / 1024, 15U);
  657. inc_sel = 1 << skip;
  658. if (inc_sel <= 2)
  659. inc_sel = 0xc;
  660. else if (resize & 1023 && skip < 15)
  661. inc_sel |= 1 << (skip + 1);
  662. ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc);
  663. if (!ret)
  664. ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8);
  665. if (!rj54n1->auto_wb) {
  666. /* Auto white balance window */
  667. wb_left = output_w / 16;
  668. wb_right = (3 * output_w / 4 - 3) / 4;
  669. wb_top = output_h / 16;
  670. wb_bottom = (3 * output_h / 4 - 3) / 4;
  671. wb_bit8 = ((wb_left >> 2) & 0x40) | ((wb_top >> 4) & 0x10) |
  672. ((wb_right >> 6) & 4) | ((wb_bottom >> 8) & 1);
  673. if (!ret)
  674. ret = reg_write(client, RJ54N1_BIT8_WB, wb_bit8);
  675. if (!ret)
  676. ret = reg_write(client, RJ54N1_HCAPS_WB, wb_left);
  677. if (!ret)
  678. ret = reg_write(client, RJ54N1_VCAPS_WB, wb_top);
  679. if (!ret)
  680. ret = reg_write(client, RJ54N1_HCAPE_WB, wb_right);
  681. if (!ret)
  682. ret = reg_write(client, RJ54N1_VCAPE_WB, wb_bottom);
  683. }
  684. /* Antiflicker */
  685. peak = 12 * RJ54N1_MAX_WIDTH * (1 << 14) * resize / rj54n1->tgclk_mhz /
  686. 10000;
  687. peak_50 = peak / 6;
  688. peak_60 = peak / 5;
  689. if (!ret)
  690. ret = reg_write(client, RJ54N1_PEAK_H,
  691. ((peak_50 >> 4) & 0xf0) | (peak_60 >> 8));
  692. if (!ret)
  693. ret = reg_write(client, RJ54N1_PEAK_50, peak_50);
  694. if (!ret)
  695. ret = reg_write(client, RJ54N1_PEAK_60, peak_60);
  696. if (!ret)
  697. ret = reg_write(client, RJ54N1_PEAK_DIFF, peak / 150);
  698. /* Start resizing */
  699. if (!ret)
  700. ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
  701. RESIZE_HOLD_SEL | RESIZE_GO | 1);
  702. if (ret < 0)
  703. return ret;
  704. /* Constant taken from manufacturer's example */
  705. msleep(230);
  706. ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1);
  707. if (ret < 0)
  708. return ret;
  709. *in_w = (output_w * resize + 512) / 1024;
  710. *in_h = (output_h * resize + 512) / 1024;
  711. *out_w = output_w;
  712. *out_h = output_h;
  713. dev_dbg(&client->dev, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
  714. *in_w, *in_h, resize, output_w, output_h, skip);
  715. return resize;
  716. }
  717. static int rj54n1_set_clock(struct i2c_client *client)
  718. {
  719. struct rj54n1 *rj54n1 = to_rj54n1(client);
  720. int ret;
  721. /* Enable external clock */
  722. ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
  723. /* Leave stand-by. Note: use this when implementing suspend / resume */
  724. if (!ret)
  725. ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
  726. if (!ret)
  727. ret = reg_write(client, RJ54N1_PLL_L, PLL_L);
  728. if (!ret)
  729. ret = reg_write(client, RJ54N1_PLL_N, PLL_N);
  730. /* TGCLK dividers */
  731. if (!ret)
  732. ret = reg_write(client, RJ54N1_RATIO_TG,
  733. rj54n1->clk_div.ratio_tg);
  734. if (!ret)
  735. ret = reg_write(client, RJ54N1_RATIO_T,
  736. rj54n1->clk_div.ratio_t);
  737. if (!ret)
  738. ret = reg_write(client, RJ54N1_RATIO_R,
  739. rj54n1->clk_div.ratio_r);
  740. /* Enable TGCLK & RAMP */
  741. if (!ret)
  742. ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3);
  743. /* Disable clock output */
  744. if (!ret)
  745. ret = reg_write(client, RJ54N1_OCLK_DSP, 0);
  746. /* Set divisors */
  747. if (!ret)
  748. ret = reg_write(client, RJ54N1_RATIO_OP,
  749. rj54n1->clk_div.ratio_op);
  750. if (!ret)
  751. ret = reg_write(client, RJ54N1_RATIO_O,
  752. rj54n1->clk_div.ratio_o);
  753. /* Enable OCLK */
  754. if (!ret)
  755. ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
  756. /* Use PLL for Timing Generator, write 2 to reserved bits */
  757. if (!ret)
  758. ret = reg_write(client, RJ54N1_TG_BYPASS, 2);
  759. /* Take sensor out of reset */
  760. if (!ret)
  761. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  762. E_EXCLK | SEN_RSTX);
  763. /* Enable PLL */
  764. if (!ret)
  765. ret = reg_write(client, RJ54N1_PLL_EN, 1);
  766. /* Wait for PLL to stabilise */
  767. msleep(10);
  768. /* Enable clock to frequency divider */
  769. if (!ret)
  770. ret = reg_write(client, RJ54N1_CLK_RST, 1);
  771. if (!ret)
  772. ret = reg_read(client, RJ54N1_CLK_RST);
  773. if (ret != 1) {
  774. dev_err(&client->dev,
  775. "Resetting RJ54N1CB0C clock failed: %d!\n", ret);
  776. return -EIO;
  777. }
  778. /* Start the PLL */
  779. ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1);
  780. /* Enable OCLK */
  781. if (!ret)
  782. ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
  783. return ret;
  784. }
  785. static int rj54n1_reg_init(struct i2c_client *client)
  786. {
  787. struct rj54n1 *rj54n1 = to_rj54n1(client);
  788. int ret = rj54n1_set_clock(client);
  789. if (!ret)
  790. ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
  791. if (!ret)
  792. ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
  793. /* Set binning divisors */
  794. if (!ret)
  795. ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4));
  796. if (!ret)
  797. ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf);
  798. /* Switch to fixed resize mode */
  799. if (!ret)
  800. ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
  801. RESIZE_HOLD_SEL | 1);
  802. /* Set gain */
  803. if (!ret)
  804. ret = reg_write(client, RJ54N1_Y_GAIN, 0x84);
  805. /*
  806. * Mirror the image back: default is upside down and left-to-right...
  807. * Set manual preview / still shot switching
  808. */
  809. if (!ret)
  810. ret = reg_write(client, RJ54N1_MIRROR_STILL_MODE, 0x27);
  811. if (!ret)
  812. ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
  813. /* Auto exposure area */
  814. if (!ret)
  815. ret = reg_write(client, RJ54N1_EXPOSURE_CONTROL, 0x80);
  816. /* Check current auto WB config */
  817. if (!ret)
  818. ret = reg_read(client, RJ54N1_WB_SEL_WEIGHT_I);
  819. if (ret >= 0) {
  820. rj54n1->auto_wb = ret & 0x80;
  821. ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
  822. }
  823. if (!ret)
  824. ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
  825. if (!ret)
  826. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  827. E_EXCLK | DSP_RSTX | SEN_RSTX);
  828. /* Commit init */
  829. if (!ret)
  830. ret = rj54n1_commit(client);
  831. /* Take DSP, TG, sensor out of reset */
  832. if (!ret)
  833. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  834. E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
  835. /* Start register update? Same register as 0x?FE in many bank_* sets */
  836. if (!ret)
  837. ret = reg_write(client, RJ54N1_FWFLG, 2);
  838. /* Constant taken from manufacturer's example */
  839. msleep(700);
  840. return ret;
  841. }
  842. static int rj54n1_try_fmt(struct v4l2_subdev *sd,
  843. struct v4l2_mbus_framefmt *mf)
  844. {
  845. struct i2c_client *client = v4l2_get_subdevdata(sd);
  846. struct rj54n1 *rj54n1 = to_rj54n1(client);
  847. const struct rj54n1_datafmt *fmt;
  848. int align = mf->code == V4L2_MBUS_FMT_SBGGR10_1X10 ||
  849. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE ||
  850. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE ||
  851. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE ||
  852. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE;
  853. dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
  854. __func__, mf->code, mf->width, mf->height);
  855. fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
  856. ARRAY_SIZE(rj54n1_colour_fmts));
  857. if (!fmt) {
  858. fmt = rj54n1->fmt;
  859. mf->code = fmt->code;
  860. }
  861. mf->field = V4L2_FIELD_NONE;
  862. mf->colorspace = fmt->colorspace;
  863. v4l_bound_align_image(&mf->width, 112, RJ54N1_MAX_WIDTH, align,
  864. &mf->height, 84, RJ54N1_MAX_HEIGHT, align, 0);
  865. return 0;
  866. }
  867. static int rj54n1_s_fmt(struct v4l2_subdev *sd,
  868. struct v4l2_mbus_framefmt *mf)
  869. {
  870. struct i2c_client *client = v4l2_get_subdevdata(sd);
  871. struct rj54n1 *rj54n1 = to_rj54n1(client);
  872. const struct rj54n1_datafmt *fmt;
  873. int output_w, output_h, max_w, max_h,
  874. input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
  875. int ret;
  876. /*
  877. * The host driver can call us without .try_fmt(), so, we have to take
  878. * care ourseleves
  879. */
  880. rj54n1_try_fmt(sd, mf);
  881. /*
  882. * Verify if the sensor has just been powered on. TODO: replace this
  883. * with proper PM, when a suitable API is available.
  884. */
  885. ret = reg_read(client, RJ54N1_RESET_STANDBY);
  886. if (ret < 0)
  887. return ret;
  888. if (!(ret & E_EXCLK)) {
  889. ret = rj54n1_reg_init(client);
  890. if (ret < 0)
  891. return ret;
  892. }
  893. dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
  894. __func__, mf->code, mf->width, mf->height);
  895. /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
  896. switch (mf->code) {
  897. case V4L2_MBUS_FMT_YUYV8_2X8:
  898. ret = reg_write(client, RJ54N1_OUT_SEL, 0);
  899. if (!ret)
  900. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  901. break;
  902. case V4L2_MBUS_FMT_YVYU8_2X8:
  903. ret = reg_write(client, RJ54N1_OUT_SEL, 0);
  904. if (!ret)
  905. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  906. break;
  907. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  908. ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
  909. if (!ret)
  910. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  911. break;
  912. case V4L2_MBUS_FMT_RGB565_2X8_BE:
  913. ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
  914. if (!ret)
  915. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  916. break;
  917. case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE:
  918. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  919. if (!ret)
  920. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  921. if (!ret)
  922. ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
  923. break;
  924. case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
  925. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  926. if (!ret)
  927. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  928. if (!ret)
  929. ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
  930. break;
  931. case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE:
  932. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  933. if (!ret)
  934. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  935. if (!ret)
  936. ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
  937. break;
  938. case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE:
  939. ret = reg_write(client, RJ54N1_OUT_SEL, 4);
  940. if (!ret)
  941. ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
  942. if (!ret)
  943. ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
  944. break;
  945. case V4L2_MBUS_FMT_SBGGR10_1X10:
  946. ret = reg_write(client, RJ54N1_OUT_SEL, 5);
  947. break;
  948. default:
  949. ret = -EINVAL;
  950. }
  951. /* Special case: a raw mode with 10 bits of data per clock tick */
  952. if (!ret)
  953. ret = reg_set(client, RJ54N1_OCLK_SEL_EN,
  954. (mf->code == V4L2_MBUS_FMT_SBGGR10_1X10) << 1, 2);
  955. if (ret < 0)
  956. return ret;
  957. /* Supported scales 1:1 >= scale > 1:16 */
  958. max_w = mf->width * (16 * 1024 - 1) / 1024;
  959. if (input_w > max_w)
  960. input_w = max_w;
  961. max_h = mf->height * (16 * 1024 - 1) / 1024;
  962. if (input_h > max_h)
  963. input_h = max_h;
  964. output_w = mf->width;
  965. output_h = mf->height;
  966. ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
  967. if (ret < 0)
  968. return ret;
  969. fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
  970. ARRAY_SIZE(rj54n1_colour_fmts));
  971. rj54n1->fmt = fmt;
  972. rj54n1->resize = ret;
  973. rj54n1->rect.width = input_w;
  974. rj54n1->rect.height = input_h;
  975. rj54n1->width = output_w;
  976. rj54n1->height = output_h;
  977. mf->width = output_w;
  978. mf->height = output_h;
  979. mf->field = V4L2_FIELD_NONE;
  980. mf->colorspace = fmt->colorspace;
  981. return 0;
  982. }
  983. static int rj54n1_g_chip_ident(struct v4l2_subdev *sd,
  984. struct v4l2_dbg_chip_ident *id)
  985. {
  986. struct i2c_client *client = v4l2_get_subdevdata(sd);
  987. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  988. return -EINVAL;
  989. if (id->match.addr != client->addr)
  990. return -ENODEV;
  991. id->ident = V4L2_IDENT_RJ54N1CB0C;
  992. id->revision = 0;
  993. return 0;
  994. }
  995. #ifdef CONFIG_VIDEO_ADV_DEBUG
  996. static int rj54n1_g_register(struct v4l2_subdev *sd,
  997. struct v4l2_dbg_register *reg)
  998. {
  999. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1000. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
  1001. reg->reg < 0x400 || reg->reg > 0x1fff)
  1002. /* Registers > 0x0800 are only available from Sharp support */
  1003. return -EINVAL;
  1004. if (reg->match.addr != client->addr)
  1005. return -ENODEV;
  1006. reg->size = 1;
  1007. reg->val = reg_read(client, reg->reg);
  1008. if (reg->val > 0xff)
  1009. return -EIO;
  1010. return 0;
  1011. }
  1012. static int rj54n1_s_register(struct v4l2_subdev *sd,
  1013. struct v4l2_dbg_register *reg)
  1014. {
  1015. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1016. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
  1017. reg->reg < 0x400 || reg->reg > 0x1fff)
  1018. /* Registers >= 0x0800 are only available from Sharp support */
  1019. return -EINVAL;
  1020. if (reg->match.addr != client->addr)
  1021. return -ENODEV;
  1022. if (reg_write(client, reg->reg, reg->val) < 0)
  1023. return -EIO;
  1024. return 0;
  1025. }
  1026. #endif
  1027. static const struct v4l2_queryctrl rj54n1_controls[] = {
  1028. {
  1029. .id = V4L2_CID_VFLIP,
  1030. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1031. .name = "Flip Vertically",
  1032. .minimum = 0,
  1033. .maximum = 1,
  1034. .step = 1,
  1035. .default_value = 0,
  1036. }, {
  1037. .id = V4L2_CID_HFLIP,
  1038. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1039. .name = "Flip Horizontally",
  1040. .minimum = 0,
  1041. .maximum = 1,
  1042. .step = 1,
  1043. .default_value = 0,
  1044. }, {
  1045. .id = V4L2_CID_GAIN,
  1046. .type = V4L2_CTRL_TYPE_INTEGER,
  1047. .name = "Gain",
  1048. .minimum = 0,
  1049. .maximum = 127,
  1050. .step = 1,
  1051. .default_value = 66,
  1052. .flags = V4L2_CTRL_FLAG_SLIDER,
  1053. }, {
  1054. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  1055. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1056. .name = "Auto white balance",
  1057. .minimum = 0,
  1058. .maximum = 1,
  1059. .step = 1,
  1060. .default_value = 1,
  1061. },
  1062. };
  1063. static struct soc_camera_ops rj54n1_ops = {
  1064. .controls = rj54n1_controls,
  1065. .num_controls = ARRAY_SIZE(rj54n1_controls),
  1066. };
  1067. static int rj54n1_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1068. {
  1069. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1070. struct rj54n1 *rj54n1 = to_rj54n1(client);
  1071. int data;
  1072. switch (ctrl->id) {
  1073. case V4L2_CID_VFLIP:
  1074. data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
  1075. if (data < 0)
  1076. return -EIO;
  1077. ctrl->value = !(data & 1);
  1078. break;
  1079. case V4L2_CID_HFLIP:
  1080. data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
  1081. if (data < 0)
  1082. return -EIO;
  1083. ctrl->value = !(data & 2);
  1084. break;
  1085. case V4L2_CID_GAIN:
  1086. data = reg_read(client, RJ54N1_Y_GAIN);
  1087. if (data < 0)
  1088. return -EIO;
  1089. ctrl->value = data / 2;
  1090. break;
  1091. case V4L2_CID_AUTO_WHITE_BALANCE:
  1092. ctrl->value = rj54n1->auto_wb;
  1093. break;
  1094. }
  1095. return 0;
  1096. }
  1097. static int rj54n1_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1098. {
  1099. int data;
  1100. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1101. struct rj54n1 *rj54n1 = to_rj54n1(client);
  1102. const struct v4l2_queryctrl *qctrl;
  1103. qctrl = soc_camera_find_qctrl(&rj54n1_ops, ctrl->id);
  1104. if (!qctrl)
  1105. return -EINVAL;
  1106. switch (ctrl->id) {
  1107. case V4L2_CID_VFLIP:
  1108. if (ctrl->value)
  1109. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1);
  1110. else
  1111. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1);
  1112. if (data < 0)
  1113. return -EIO;
  1114. break;
  1115. case V4L2_CID_HFLIP:
  1116. if (ctrl->value)
  1117. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2);
  1118. else
  1119. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2);
  1120. if (data < 0)
  1121. return -EIO;
  1122. break;
  1123. case V4L2_CID_GAIN:
  1124. if (ctrl->value > qctrl->maximum ||
  1125. ctrl->value < qctrl->minimum)
  1126. return -EINVAL;
  1127. else if (reg_write(client, RJ54N1_Y_GAIN, ctrl->value * 2) < 0)
  1128. return -EIO;
  1129. break;
  1130. case V4L2_CID_AUTO_WHITE_BALANCE:
  1131. /* Auto WB area - whole image */
  1132. if (reg_set(client, RJ54N1_WB_SEL_WEIGHT_I, ctrl->value << 7,
  1133. 0x80) < 0)
  1134. return -EIO;
  1135. rj54n1->auto_wb = ctrl->value;
  1136. break;
  1137. }
  1138. return 0;
  1139. }
  1140. static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
  1141. .g_ctrl = rj54n1_g_ctrl,
  1142. .s_ctrl = rj54n1_s_ctrl,
  1143. .g_chip_ident = rj54n1_g_chip_ident,
  1144. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1145. .g_register = rj54n1_g_register,
  1146. .s_register = rj54n1_s_register,
  1147. #endif
  1148. };
  1149. static int rj54n1_g_mbus_config(struct v4l2_subdev *sd,
  1150. struct v4l2_mbus_config *cfg)
  1151. {
  1152. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1153. struct soc_camera_device *icd = client->dev.platform_data;
  1154. struct soc_camera_link *icl = to_soc_camera_link(icd);
  1155. cfg->flags =
  1156. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  1157. V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH |
  1158. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  1159. cfg->type = V4L2_MBUS_PARALLEL;
  1160. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  1161. return 0;
  1162. }
  1163. static int rj54n1_s_mbus_config(struct v4l2_subdev *sd,
  1164. const struct v4l2_mbus_config *cfg)
  1165. {
  1166. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1167. struct soc_camera_device *icd = client->dev.platform_data;
  1168. struct soc_camera_link *icl = to_soc_camera_link(icd);
  1169. /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
  1170. if (soc_camera_apply_board_flags(icl, cfg) &
  1171. V4L2_MBUS_PCLK_SAMPLE_RISING)
  1172. return reg_write(client, RJ54N1_OUT_SIGPO, 1 << 4);
  1173. else
  1174. return reg_write(client, RJ54N1_OUT_SIGPO, 0);
  1175. }
  1176. static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
  1177. .s_stream = rj54n1_s_stream,
  1178. .s_mbus_fmt = rj54n1_s_fmt,
  1179. .g_mbus_fmt = rj54n1_g_fmt,
  1180. .try_mbus_fmt = rj54n1_try_fmt,
  1181. .enum_mbus_fmt = rj54n1_enum_fmt,
  1182. .g_crop = rj54n1_g_crop,
  1183. .s_crop = rj54n1_s_crop,
  1184. .cropcap = rj54n1_cropcap,
  1185. .g_mbus_config = rj54n1_g_mbus_config,
  1186. .s_mbus_config = rj54n1_s_mbus_config,
  1187. };
  1188. static struct v4l2_subdev_ops rj54n1_subdev_ops = {
  1189. .core = &rj54n1_subdev_core_ops,
  1190. .video = &rj54n1_subdev_video_ops,
  1191. };
  1192. /*
  1193. * Interface active, can use i2c. If it fails, it can indeed mean, that
  1194. * this wasn't our capture interface, so, we wait for the right one
  1195. */
  1196. static int rj54n1_video_probe(struct soc_camera_device *icd,
  1197. struct i2c_client *client,
  1198. struct rj54n1_pdata *priv)
  1199. {
  1200. int data1, data2;
  1201. int ret;
  1202. /* We must have a parent by now. And it cannot be a wrong one. */
  1203. BUG_ON(!icd->parent ||
  1204. to_soc_camera_host(icd->parent)->nr != icd->iface);
  1205. /* Read out the chip version register */
  1206. data1 = reg_read(client, RJ54N1_DEV_CODE);
  1207. data2 = reg_read(client, RJ54N1_DEV_CODE2);
  1208. if (data1 != 0x51 || data2 != 0x10) {
  1209. ret = -ENODEV;
  1210. dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
  1211. data1, data2);
  1212. goto ei2c;
  1213. }
  1214. /* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
  1215. ret = reg_write(client, RJ54N1_IOC, priv->ioctl_high << 7);
  1216. if (ret < 0)
  1217. goto ei2c;
  1218. dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
  1219. data1, data2);
  1220. ei2c:
  1221. return ret;
  1222. }
  1223. static int rj54n1_probe(struct i2c_client *client,
  1224. const struct i2c_device_id *did)
  1225. {
  1226. struct rj54n1 *rj54n1;
  1227. struct soc_camera_device *icd = client->dev.platform_data;
  1228. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1229. struct soc_camera_link *icl;
  1230. struct rj54n1_pdata *rj54n1_priv;
  1231. int ret;
  1232. if (!icd) {
  1233. dev_err(&client->dev, "RJ54N1CB0C: missing soc-camera data!\n");
  1234. return -EINVAL;
  1235. }
  1236. icl = to_soc_camera_link(icd);
  1237. if (!icl || !icl->priv) {
  1238. dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n");
  1239. return -EINVAL;
  1240. }
  1241. rj54n1_priv = icl->priv;
  1242. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1243. dev_warn(&adapter->dev,
  1244. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
  1245. return -EIO;
  1246. }
  1247. rj54n1 = kzalloc(sizeof(struct rj54n1), GFP_KERNEL);
  1248. if (!rj54n1)
  1249. return -ENOMEM;
  1250. v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops);
  1251. icd->ops = &rj54n1_ops;
  1252. rj54n1->clk_div = clk_div;
  1253. rj54n1->rect.left = RJ54N1_COLUMN_SKIP;
  1254. rj54n1->rect.top = RJ54N1_ROW_SKIP;
  1255. rj54n1->rect.width = RJ54N1_MAX_WIDTH;
  1256. rj54n1->rect.height = RJ54N1_MAX_HEIGHT;
  1257. rj54n1->width = RJ54N1_MAX_WIDTH;
  1258. rj54n1->height = RJ54N1_MAX_HEIGHT;
  1259. rj54n1->fmt = &rj54n1_colour_fmts[0];
  1260. rj54n1->resize = 1024;
  1261. rj54n1->tgclk_mhz = (rj54n1_priv->mclk_freq / PLL_L * PLL_N) /
  1262. (clk_div.ratio_tg + 1) / (clk_div.ratio_t + 1);
  1263. ret = rj54n1_video_probe(icd, client, rj54n1_priv);
  1264. if (ret < 0) {
  1265. icd->ops = NULL;
  1266. kfree(rj54n1);
  1267. return ret;
  1268. }
  1269. return ret;
  1270. }
  1271. static int rj54n1_remove(struct i2c_client *client)
  1272. {
  1273. struct rj54n1 *rj54n1 = to_rj54n1(client);
  1274. struct soc_camera_device *icd = client->dev.platform_data;
  1275. struct soc_camera_link *icl = to_soc_camera_link(icd);
  1276. icd->ops = NULL;
  1277. if (icl->free_bus)
  1278. icl->free_bus(icl);
  1279. kfree(rj54n1);
  1280. return 0;
  1281. }
  1282. static const struct i2c_device_id rj54n1_id[] = {
  1283. { "rj54n1cb0c", 0 },
  1284. { }
  1285. };
  1286. MODULE_DEVICE_TABLE(i2c, rj54n1_id);
  1287. static struct i2c_driver rj54n1_i2c_driver = {
  1288. .driver = {
  1289. .name = "rj54n1cb0c",
  1290. },
  1291. .probe = rj54n1_probe,
  1292. .remove = rj54n1_remove,
  1293. .id_table = rj54n1_id,
  1294. };
  1295. static int __init rj54n1_mod_init(void)
  1296. {
  1297. return i2c_add_driver(&rj54n1_i2c_driver);
  1298. }
  1299. static void __exit rj54n1_mod_exit(void)
  1300. {
  1301. i2c_del_driver(&rj54n1_i2c_driver);
  1302. }
  1303. module_init(rj54n1_mod_init);
  1304. module_exit(rj54n1_mod_exit);
  1305. MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
  1306. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  1307. MODULE_LICENSE("GPL v2");