rj54n1cb0c.c 35 KB

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