rj54n1cb0c.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /*
  2. * Driver for RJ54N1CB0C CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2009, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/slab.h>
  13. #include <linux/videodev2.h>
  14. #include <media/v4l2-subdev.h>
  15. #include <media/v4l2-chip-ident.h>
  16. #include <media/soc_camera.h>
  17. #define RJ54N1_DEV_CODE 0x0400
  18. #define RJ54N1_DEV_CODE2 0x0401
  19. #define RJ54N1_OUT_SEL 0x0403
  20. #define RJ54N1_XY_OUTPUT_SIZE_S_H 0x0404
  21. #define RJ54N1_X_OUTPUT_SIZE_S_L 0x0405
  22. #define RJ54N1_Y_OUTPUT_SIZE_S_L 0x0406
  23. #define RJ54N1_XY_OUTPUT_SIZE_P_H 0x0407
  24. #define RJ54N1_X_OUTPUT_SIZE_P_L 0x0408
  25. #define RJ54N1_Y_OUTPUT_SIZE_P_L 0x0409
  26. #define RJ54N1_LINE_LENGTH_PCK_S_H 0x040a
  27. #define RJ54N1_LINE_LENGTH_PCK_S_L 0x040b
  28. #define RJ54N1_LINE_LENGTH_PCK_P_H 0x040c
  29. #define RJ54N1_LINE_LENGTH_PCK_P_L 0x040d
  30. #define RJ54N1_RESIZE_N 0x040e
  31. #define RJ54N1_RESIZE_N_STEP 0x040f
  32. #define RJ54N1_RESIZE_STEP 0x0410
  33. #define RJ54N1_RESIZE_HOLD_H 0x0411
  34. #define RJ54N1_RESIZE_HOLD_L 0x0412
  35. #define RJ54N1_H_OBEN_OFS 0x0413
  36. #define RJ54N1_V_OBEN_OFS 0x0414
  37. #define RJ54N1_RESIZE_CONTROL 0x0415
  38. #define RJ54N1_INC_USE_SEL_H 0x0425
  39. #define RJ54N1_INC_USE_SEL_L 0x0426
  40. #define RJ54N1_MIRROR_STILL_MODE 0x0427
  41. #define RJ54N1_INIT_START 0x0428
  42. #define RJ54N1_SCALE_1_2_LEV 0x0429
  43. #define RJ54N1_SCALE_4_LEV 0x042a
  44. #define RJ54N1_Y_GAIN 0x04d8
  45. #define RJ54N1_APT_GAIN_UP 0x04fa
  46. #define RJ54N1_RA_SEL_UL 0x0530
  47. #define RJ54N1_BYTE_SWAP 0x0531
  48. #define RJ54N1_OUT_SIGPO 0x053b
  49. #define RJ54N1_FRAME_LENGTH_S_H 0x0595
  50. #define RJ54N1_FRAME_LENGTH_S_L 0x0596
  51. #define RJ54N1_FRAME_LENGTH_P_H 0x0597
  52. #define RJ54N1_FRAME_LENGTH_P_L 0x0598
  53. #define RJ54N1_IOC 0x05ef
  54. #define RJ54N1_TG_BYPASS 0x0700
  55. #define RJ54N1_PLL_L 0x0701
  56. #define RJ54N1_PLL_N 0x0702
  57. #define RJ54N1_PLL_EN 0x0704
  58. #define RJ54N1_RATIO_TG 0x0706
  59. #define RJ54N1_RATIO_T 0x0707
  60. #define RJ54N1_RATIO_R 0x0708
  61. #define RJ54N1_RAMP_TGCLK_EN 0x0709
  62. #define RJ54N1_OCLK_DSP 0x0710
  63. #define RJ54N1_RATIO_OP 0x0711
  64. #define RJ54N1_RATIO_O 0x0712
  65. #define RJ54N1_OCLK_SEL_EN 0x0713
  66. #define RJ54N1_CLK_RST 0x0717
  67. #define RJ54N1_RESET_STANDBY 0x0718
  68. #define E_EXCLK (1 << 7)
  69. #define SOFT_STDBY (1 << 4)
  70. #define SEN_RSTX (1 << 2)
  71. #define TG_RSTX (1 << 1)
  72. #define DSP_RSTX (1 << 0)
  73. #define RESIZE_HOLD_SEL (1 << 2)
  74. #define RESIZE_GO (1 << 1)
  75. #define RJ54N1_COLUMN_SKIP 0
  76. #define RJ54N1_ROW_SKIP 0
  77. #define RJ54N1_MAX_WIDTH 1600
  78. #define RJ54N1_MAX_HEIGHT 1200
  79. /* I2C addresses: 0x50, 0x51, 0x60, 0x61 */
  80. static const struct soc_camera_data_format rj54n1_colour_formats[] = {
  81. {
  82. .name = "YUYV",
  83. .depth = 16,
  84. .fourcc = V4L2_PIX_FMT_YUYV,
  85. .colorspace = V4L2_COLORSPACE_JPEG,
  86. }, {
  87. .name = "RGB565",
  88. .depth = 16,
  89. .fourcc = V4L2_PIX_FMT_RGB565,
  90. .colorspace = V4L2_COLORSPACE_SRGB,
  91. }
  92. };
  93. struct rj54n1_clock_div {
  94. u8 ratio_tg;
  95. u8 ratio_t;
  96. u8 ratio_r;
  97. u8 ratio_op;
  98. u8 ratio_o;
  99. };
  100. struct rj54n1 {
  101. struct v4l2_subdev subdev;
  102. struct v4l2_rect rect; /* Sensor window */
  103. unsigned short width; /* Output window */
  104. unsigned short height;
  105. unsigned short resize; /* Sensor * 1024 / resize = Output */
  106. struct rj54n1_clock_div clk_div;
  107. u32 fourcc;
  108. unsigned short scale;
  109. u8 bank;
  110. };
  111. struct rj54n1_reg_val {
  112. u16 reg;
  113. u8 val;
  114. };
  115. const static struct rj54n1_reg_val bank_4[] = {
  116. {0x417, 0},
  117. {0x42c, 0},
  118. {0x42d, 0xf0},
  119. {0x42e, 0},
  120. {0x42f, 0x50},
  121. {0x430, 0xf5},
  122. {0x431, 0x16},
  123. {0x432, 0x20},
  124. {0x433, 0},
  125. {0x434, 0xc8},
  126. {0x43c, 8},
  127. {0x43e, 0x90},
  128. {0x445, 0x83},
  129. {0x4ba, 0x58},
  130. {0x4bb, 4},
  131. {0x4bc, 0x20},
  132. {0x4db, 4},
  133. {0x4fe, 2},
  134. };
  135. const static struct rj54n1_reg_val bank_5[] = {
  136. {0x514, 0},
  137. {0x516, 0},
  138. {0x518, 0},
  139. {0x51a, 0},
  140. {0x51d, 0xff},
  141. {0x56f, 0x28},
  142. {0x575, 0x40},
  143. {0x5bc, 0x48},
  144. {0x5c1, 6},
  145. {0x5e5, 0x11},
  146. {0x5e6, 0x43},
  147. {0x5e7, 0x33},
  148. {0x5e8, 0x21},
  149. {0x5e9, 0x30},
  150. {0x5ea, 0x0},
  151. {0x5eb, 0xa5},
  152. {0x5ec, 0xff},
  153. {0x5fe, 2},
  154. };
  155. const static struct rj54n1_reg_val bank_7[] = {
  156. {0x70a, 0},
  157. {0x714, 0xff},
  158. {0x715, 0xff},
  159. {0x716, 0x1f},
  160. {0x7FE, 0x02},
  161. };
  162. const static struct rj54n1_reg_val bank_8[] = {
  163. {0x800, 0x00},
  164. {0x801, 0x01},
  165. {0x802, 0x61},
  166. {0x805, 0x00},
  167. {0x806, 0x00},
  168. {0x807, 0x00},
  169. {0x808, 0x00},
  170. {0x809, 0x01},
  171. {0x80A, 0x61},
  172. {0x80B, 0x00},
  173. {0x80C, 0x01},
  174. {0x80D, 0x00},
  175. {0x80E, 0x00},
  176. {0x80F, 0x00},
  177. {0x810, 0x00},
  178. {0x811, 0x01},
  179. {0x812, 0x61},
  180. {0x813, 0x00},
  181. {0x814, 0x11},
  182. {0x815, 0x00},
  183. {0x816, 0x41},
  184. {0x817, 0x00},
  185. {0x818, 0x51},
  186. {0x819, 0x01},
  187. {0x81A, 0x1F},
  188. {0x81B, 0x00},
  189. {0x81C, 0x01},
  190. {0x81D, 0x00},
  191. {0x81E, 0x11},
  192. {0x81F, 0x00},
  193. {0x820, 0x41},
  194. {0x821, 0x00},
  195. {0x822, 0x51},
  196. {0x823, 0x00},
  197. {0x824, 0x00},
  198. {0x825, 0x00},
  199. {0x826, 0x47},
  200. {0x827, 0x01},
  201. {0x828, 0x4F},
  202. {0x829, 0x00},
  203. {0x82A, 0x00},
  204. {0x82B, 0x00},
  205. {0x82C, 0x30},
  206. {0x82D, 0x00},
  207. {0x82E, 0x40},
  208. {0x82F, 0x00},
  209. {0x830, 0xB3},
  210. {0x831, 0x00},
  211. {0x832, 0xE3},
  212. {0x833, 0x00},
  213. {0x834, 0x00},
  214. {0x835, 0x00},
  215. {0x836, 0x00},
  216. {0x837, 0x00},
  217. {0x838, 0x00},
  218. {0x839, 0x01},
  219. {0x83A, 0x61},
  220. {0x83B, 0x00},
  221. {0x83C, 0x01},
  222. {0x83D, 0x00},
  223. {0x83E, 0x00},
  224. {0x83F, 0x00},
  225. {0x840, 0x00},
  226. {0x841, 0x01},
  227. {0x842, 0x61},
  228. {0x843, 0x00},
  229. {0x844, 0x1D},
  230. {0x845, 0x00},
  231. {0x846, 0x00},
  232. {0x847, 0x00},
  233. {0x848, 0x00},
  234. {0x849, 0x01},
  235. {0x84A, 0x1F},
  236. {0x84B, 0x00},
  237. {0x84C, 0x05},
  238. {0x84D, 0x00},
  239. {0x84E, 0x19},
  240. {0x84F, 0x01},
  241. {0x850, 0x21},
  242. {0x851, 0x01},
  243. {0x852, 0x5D},
  244. {0x853, 0x00},
  245. {0x854, 0x00},
  246. {0x855, 0x00},
  247. {0x856, 0x19},
  248. {0x857, 0x01},
  249. {0x858, 0x21},
  250. {0x859, 0x00},
  251. {0x85A, 0x00},
  252. {0x85B, 0x00},
  253. {0x85C, 0x00},
  254. {0x85D, 0x00},
  255. {0x85E, 0x00},
  256. {0x85F, 0x00},
  257. {0x860, 0xB3},
  258. {0x861, 0x00},
  259. {0x862, 0xE3},
  260. {0x863, 0x00},
  261. {0x864, 0x00},
  262. {0x865, 0x00},
  263. {0x866, 0x00},
  264. {0x867, 0x00},
  265. {0x868, 0x00},
  266. {0x869, 0xE2},
  267. {0x86A, 0x00},
  268. {0x86B, 0x01},
  269. {0x86C, 0x06},
  270. {0x86D, 0x00},
  271. {0x86E, 0x00},
  272. {0x86F, 0x00},
  273. {0x870, 0x60},
  274. {0x871, 0x8C},
  275. {0x872, 0x10},
  276. {0x873, 0x00},
  277. {0x874, 0xE0},
  278. {0x875, 0x00},
  279. {0x876, 0x27},
  280. {0x877, 0x01},
  281. {0x878, 0x00},
  282. {0x879, 0x00},
  283. {0x87A, 0x00},
  284. {0x87B, 0x03},
  285. {0x87C, 0x00},
  286. {0x87D, 0x00},
  287. {0x87E, 0x00},
  288. {0x87F, 0x00},
  289. {0x880, 0x00},
  290. {0x881, 0x00},
  291. {0x882, 0x00},
  292. {0x883, 0x00},
  293. {0x884, 0x00},
  294. {0x885, 0x00},
  295. {0x886, 0xF8},
  296. {0x887, 0x00},
  297. {0x888, 0x03},
  298. {0x889, 0x00},
  299. {0x88A, 0x64},
  300. {0x88B, 0x00},
  301. {0x88C, 0x03},
  302. {0x88D, 0x00},
  303. {0x88E, 0xB1},
  304. {0x88F, 0x00},
  305. {0x890, 0x03},
  306. {0x891, 0x01},
  307. {0x892, 0x1D},
  308. {0x893, 0x00},
  309. {0x894, 0x03},
  310. {0x895, 0x01},
  311. {0x896, 0x4B},
  312. {0x897, 0x00},
  313. {0x898, 0xE5},
  314. {0x899, 0x00},
  315. {0x89A, 0x01},
  316. {0x89B, 0x00},
  317. {0x89C, 0x01},
  318. {0x89D, 0x04},
  319. {0x89E, 0xC8},
  320. {0x89F, 0x00},
  321. {0x8A0, 0x01},
  322. {0x8A1, 0x01},
  323. {0x8A2, 0x61},
  324. {0x8A3, 0x00},
  325. {0x8A4, 0x01},
  326. {0x8A5, 0x00},
  327. {0x8A6, 0x00},
  328. {0x8A7, 0x00},
  329. {0x8A8, 0x00},
  330. {0x8A9, 0x00},
  331. {0x8AA, 0x7F},
  332. {0x8AB, 0x03},
  333. {0x8AC, 0x00},
  334. {0x8AD, 0x00},
  335. {0x8AE, 0x00},
  336. {0x8AF, 0x00},
  337. {0x8B0, 0x00},
  338. {0x8B1, 0x00},
  339. {0x8B6, 0x00},
  340. {0x8B7, 0x01},
  341. {0x8B8, 0x00},
  342. {0x8B9, 0x00},
  343. {0x8BA, 0x02},
  344. {0x8BB, 0x00},
  345. {0x8BC, 0xFF},
  346. {0x8BD, 0x00},
  347. {0x8FE, 0x02},
  348. };
  349. const static struct rj54n1_reg_val bank_10[] = {
  350. {0x10bf, 0x69}
  351. };
  352. /* Clock dividers - these are default register values, divider = register + 1 */
  353. const static struct rj54n1_clock_div clk_div = {
  354. .ratio_tg = 3 /* default: 5 */,
  355. .ratio_t = 4 /* default: 1 */,
  356. .ratio_r = 4 /* default: 0 */,
  357. .ratio_op = 1 /* default: 5 */,
  358. .ratio_o = 9 /* default: 0 */,
  359. };
  360. static struct rj54n1 *to_rj54n1(const struct i2c_client *client)
  361. {
  362. return container_of(i2c_get_clientdata(client), struct rj54n1, subdev);
  363. }
  364. static int reg_read(struct i2c_client *client, const u16 reg)
  365. {
  366. struct rj54n1 *rj54n1 = to_rj54n1(client);
  367. int ret;
  368. /* set bank */
  369. if (rj54n1->bank != reg >> 8) {
  370. dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
  371. ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
  372. if (ret < 0)
  373. return ret;
  374. rj54n1->bank = reg >> 8;
  375. }
  376. return i2c_smbus_read_byte_data(client, reg & 0xff);
  377. }
  378. static int reg_write(struct i2c_client *client, const u16 reg,
  379. const u8 data)
  380. {
  381. struct rj54n1 *rj54n1 = to_rj54n1(client);
  382. int ret;
  383. /* set bank */
  384. if (rj54n1->bank != reg >> 8) {
  385. dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
  386. ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
  387. if (ret < 0)
  388. return ret;
  389. rj54n1->bank = reg >> 8;
  390. }
  391. dev_dbg(&client->dev, "[0x%x] = 0x%x\n", reg & 0xff, data);
  392. return i2c_smbus_write_byte_data(client, reg & 0xff, data);
  393. }
  394. static int reg_set(struct i2c_client *client, const u16 reg,
  395. const u8 data, const u8 mask)
  396. {
  397. int ret;
  398. ret = reg_read(client, reg);
  399. if (ret < 0)
  400. return ret;
  401. return reg_write(client, reg, (ret & ~mask) | (data & mask));
  402. }
  403. static int reg_write_multiple(struct i2c_client *client,
  404. const struct rj54n1_reg_val *rv, const int n)
  405. {
  406. int i, ret;
  407. for (i = 0; i < n; i++) {
  408. ret = reg_write(client, rv->reg, rv->val);
  409. if (ret < 0)
  410. return ret;
  411. rv++;
  412. }
  413. return 0;
  414. }
  415. static int rj54n1_s_stream(struct v4l2_subdev *sd, int enable)
  416. {
  417. /* TODO: start / stop streaming */
  418. return 0;
  419. }
  420. static int rj54n1_set_bus_param(struct soc_camera_device *icd,
  421. unsigned long flags)
  422. {
  423. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  424. struct i2c_client *client = sd->priv;
  425. /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
  426. if (flags & SOCAM_PCLK_SAMPLE_RISING)
  427. return reg_write(client, RJ54N1_OUT_SIGPO, 1 << 4);
  428. else
  429. return reg_write(client, RJ54N1_OUT_SIGPO, 0);
  430. }
  431. static unsigned long rj54n1_query_bus_param(struct soc_camera_device *icd)
  432. {
  433. struct soc_camera_link *icl = to_soc_camera_link(icd);
  434. const unsigned long flags =
  435. SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
  436. SOCAM_MASTER | SOCAM_DATAWIDTH_8 |
  437. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
  438. SOCAM_DATA_ACTIVE_HIGH;
  439. return soc_camera_apply_sensor_flags(icl, flags);
  440. }
  441. static int rj54n1_set_rect(struct i2c_client *client,
  442. u16 reg_x, u16 reg_y, u16 reg_xy,
  443. u32 width, u32 height)
  444. {
  445. int ret;
  446. ret = reg_write(client, reg_xy,
  447. ((width >> 4) & 0x70) |
  448. ((height >> 8) & 7));
  449. if (!ret)
  450. ret = reg_write(client, reg_x, width & 0xff);
  451. if (!ret)
  452. ret = reg_write(client, reg_y, height & 0xff);
  453. return ret;
  454. }
  455. /*
  456. * Some commands, specifically certain initialisation sequences, require
  457. * a commit operation.
  458. */
  459. static int rj54n1_commit(struct i2c_client *client)
  460. {
  461. int ret = reg_write(client, RJ54N1_INIT_START, 1);
  462. msleep(10);
  463. if (!ret)
  464. ret = reg_write(client, RJ54N1_INIT_START, 0);
  465. return ret;
  466. }
  467. static int rj54n1_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  468. {
  469. struct i2c_client *client = sd->priv;
  470. struct rj54n1 *rj54n1 = to_rj54n1(client);
  471. a->c = rj54n1->rect;
  472. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  473. return 0;
  474. }
  475. static int rj54n1_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  476. {
  477. a->bounds.left = RJ54N1_COLUMN_SKIP;
  478. a->bounds.top = RJ54N1_ROW_SKIP;
  479. a->bounds.width = RJ54N1_MAX_WIDTH;
  480. a->bounds.height = RJ54N1_MAX_HEIGHT;
  481. a->defrect = a->bounds;
  482. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  483. a->pixelaspect.numerator = 1;
  484. a->pixelaspect.denominator = 1;
  485. return 0;
  486. }
  487. static int rj54n1_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
  488. {
  489. struct i2c_client *client = sd->priv;
  490. struct rj54n1 *rj54n1 = to_rj54n1(client);
  491. struct v4l2_pix_format *pix = &f->fmt.pix;
  492. pix->pixelformat = rj54n1->fourcc;
  493. pix->field = V4L2_FIELD_NONE;
  494. pix->width = rj54n1->width;
  495. pix->height = rj54n1->height;
  496. return 0;
  497. }
  498. /*
  499. * The actual geometry configuration routine. It scales the input window into
  500. * the output one, updates the window sizes and returns an error or the resize
  501. * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
  502. */
  503. static int rj54n1_sensor_scale(struct v4l2_subdev *sd, u32 *in_w, u32 *in_h,
  504. u32 *out_w, u32 *out_h)
  505. {
  506. struct i2c_client *client = sd->priv;
  507. unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
  508. output_w = *out_w, output_h = *out_h;
  509. u16 inc_sel;
  510. int ret;
  511. ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
  512. RJ54N1_Y_OUTPUT_SIZE_S_L,
  513. RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
  514. if (!ret)
  515. ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
  516. RJ54N1_Y_OUTPUT_SIZE_P_L,
  517. RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
  518. if (ret < 0)
  519. return ret;
  520. if (output_w > input_w || output_h > input_h) {
  521. input_w = output_w;
  522. input_h = output_h;
  523. resize = 1024;
  524. } else {
  525. unsigned int resize_x, resize_y;
  526. resize_x = input_w * 1024 / output_w;
  527. resize_y = input_h * 1024 / output_h;
  528. resize = min(resize_x, resize_y);
  529. /* Prohibited value ranges */
  530. switch (resize) {
  531. case 2040 ... 2047:
  532. resize = 2039;
  533. break;
  534. case 4080 ... 4095:
  535. resize = 4079;
  536. break;
  537. case 8160 ... 8191:
  538. resize = 8159;
  539. break;
  540. case 16320 ... 16383:
  541. resize = 16319;
  542. }
  543. input_w = output_w * resize / 1024;
  544. input_h = output_h * resize / 1024;
  545. }
  546. /* Set scaling */
  547. ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff);
  548. if (!ret)
  549. ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8);
  550. if (ret < 0)
  551. return ret;
  552. /*
  553. * Configure a skipping bitmask. The sensor will select a skipping value
  554. * among set bits automatically.
  555. */
  556. skip = min(resize / 1024, (unsigned)15);
  557. inc_sel = 1 << skip;
  558. if (inc_sel <= 2)
  559. inc_sel = 0xc;
  560. else if (resize & 1023 && skip < 15)
  561. inc_sel |= 1 << (skip + 1);
  562. ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc);
  563. if (!ret)
  564. ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8);
  565. /* Start resizing */
  566. if (!ret)
  567. ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
  568. RESIZE_HOLD_SEL | RESIZE_GO | 1);
  569. if (ret < 0)
  570. return ret;
  571. dev_dbg(&client->dev, "resize %u, skip %u\n", resize, skip);
  572. /* Constant taken from manufacturer's example */
  573. msleep(230);
  574. ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1);
  575. if (ret < 0)
  576. return ret;
  577. *in_w = input_w;
  578. *in_h = input_h;
  579. *out_w = output_w;
  580. *out_h = output_h;
  581. return resize;
  582. }
  583. static int rj54n1_set_clock(struct i2c_client *client)
  584. {
  585. struct rj54n1 *rj54n1 = to_rj54n1(client);
  586. int ret;
  587. /* Enable external clock */
  588. ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
  589. /* Leave stand-by */
  590. if (!ret)
  591. ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
  592. if (!ret)
  593. ret = reg_write(client, RJ54N1_PLL_L, 2);
  594. if (!ret)
  595. ret = reg_write(client, RJ54N1_PLL_N, 0x31);
  596. /* TGCLK dividers */
  597. if (!ret)
  598. ret = reg_write(client, RJ54N1_RATIO_TG,
  599. rj54n1->clk_div.ratio_tg);
  600. if (!ret)
  601. ret = reg_write(client, RJ54N1_RATIO_T,
  602. rj54n1->clk_div.ratio_t);
  603. if (!ret)
  604. ret = reg_write(client, RJ54N1_RATIO_R,
  605. rj54n1->clk_div.ratio_r);
  606. /* Enable TGCLK & RAMP */
  607. if (!ret)
  608. ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3);
  609. /* Disable clock output */
  610. if (!ret)
  611. ret = reg_write(client, RJ54N1_OCLK_DSP, 0);
  612. /* Set divisors */
  613. if (!ret)
  614. ret = reg_write(client, RJ54N1_RATIO_OP,
  615. rj54n1->clk_div.ratio_op);
  616. if (!ret)
  617. ret = reg_write(client, RJ54N1_RATIO_O,
  618. rj54n1->clk_div.ratio_o);
  619. /* Enable OCLK */
  620. if (!ret)
  621. ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
  622. /* Use PLL for Timing Generator, write 2 to reserved bits */
  623. if (!ret)
  624. ret = reg_write(client, RJ54N1_TG_BYPASS, 2);
  625. /* Take sensor out of reset */
  626. if (!ret)
  627. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  628. E_EXCLK | SEN_RSTX);
  629. /* Enable PLL */
  630. if (!ret)
  631. ret = reg_write(client, RJ54N1_PLL_EN, 1);
  632. /* Wait for PLL to stabilise */
  633. msleep(10);
  634. /* Enable clock to frequency divider */
  635. if (!ret)
  636. ret = reg_write(client, RJ54N1_CLK_RST, 1);
  637. if (!ret)
  638. ret = reg_read(client, RJ54N1_CLK_RST);
  639. if (ret != 1) {
  640. dev_err(&client->dev,
  641. "Resetting RJ54N1CB0C clock failed: %d!\n", ret);
  642. return -EIO;
  643. }
  644. /* Start the PLL */
  645. ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1);
  646. /* Enable OCLK */
  647. if (!ret)
  648. ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
  649. return ret;
  650. }
  651. static int rj54n1_reg_init(struct i2c_client *client)
  652. {
  653. int ret = rj54n1_set_clock(client);
  654. if (!ret)
  655. ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
  656. if (!ret)
  657. ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
  658. /* Set binning divisors */
  659. if (!ret)
  660. ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4));
  661. if (!ret)
  662. ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf);
  663. /* Switch to fixed resize mode */
  664. if (!ret)
  665. ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
  666. RESIZE_HOLD_SEL | 1);
  667. /* Set gain */
  668. if (!ret)
  669. ret = reg_write(client, RJ54N1_Y_GAIN, 0x84);
  670. /* Mirror the image back: default is upside down and left-to-right... */
  671. if (!ret)
  672. ret = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 3, 3);
  673. if (!ret)
  674. ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
  675. if (!ret)
  676. ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
  677. if (!ret)
  678. ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
  679. if (!ret)
  680. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  681. E_EXCLK | DSP_RSTX | SEN_RSTX);
  682. /* Commit init */
  683. if (!ret)
  684. ret = rj54n1_commit(client);
  685. /* Take DSP, TG, sensor out of reset */
  686. if (!ret)
  687. ret = reg_write(client, RJ54N1_RESET_STANDBY,
  688. E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
  689. if (!ret)
  690. ret = reg_write(client, 0x7fe, 2);
  691. /* Constant taken from manufacturer's example */
  692. msleep(700);
  693. return ret;
  694. }
  695. /* FIXME: streaming output only up to 800x600 is functional */
  696. static int rj54n1_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
  697. {
  698. struct v4l2_pix_format *pix = &f->fmt.pix;
  699. pix->field = V4L2_FIELD_NONE;
  700. if (pix->width > 800)
  701. pix->width = 800;
  702. if (pix->height > 600)
  703. pix->height = 600;
  704. return 0;
  705. }
  706. static int rj54n1_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
  707. {
  708. struct i2c_client *client = sd->priv;
  709. struct rj54n1 *rj54n1 = to_rj54n1(client);
  710. struct v4l2_pix_format *pix = &f->fmt.pix;
  711. unsigned int output_w, output_h,
  712. input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
  713. int ret;
  714. /*
  715. * The host driver can call us without .try_fmt(), so, we have to take
  716. * care ourseleves
  717. */
  718. ret = rj54n1_try_fmt(sd, f);
  719. /*
  720. * Verify if the sensor has just been powered on. TODO: replace this
  721. * with proper PM, when a suitable API is available.
  722. */
  723. if (!ret)
  724. ret = reg_read(client, RJ54N1_RESET_STANDBY);
  725. if (ret < 0)
  726. return ret;
  727. if (!(ret & E_EXCLK)) {
  728. ret = rj54n1_reg_init(client);
  729. if (ret < 0)
  730. return ret;
  731. }
  732. /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
  733. switch (pix->pixelformat) {
  734. case V4L2_PIX_FMT_YUYV:
  735. ret = reg_write(client, RJ54N1_OUT_SEL, 0);
  736. if (!ret)
  737. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  738. break;
  739. case V4L2_PIX_FMT_RGB565:
  740. ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
  741. if (!ret)
  742. ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
  743. break;
  744. default:
  745. ret = -EINVAL;
  746. }
  747. if (ret < 0)
  748. return ret;
  749. /* Supported scales 1:1 - 1:16 */
  750. if (pix->width < input_w / 16)
  751. pix->width = input_w / 16;
  752. if (pix->height < input_h / 16)
  753. pix->height = input_h / 16;
  754. output_w = pix->width;
  755. output_h = pix->height;
  756. ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
  757. if (ret < 0)
  758. return ret;
  759. rj54n1->fourcc = pix->pixelformat;
  760. rj54n1->resize = ret;
  761. rj54n1->rect.width = input_w;
  762. rj54n1->rect.height = input_h;
  763. rj54n1->width = output_w;
  764. rj54n1->height = output_h;
  765. pix->width = output_w;
  766. pix->height = output_h;
  767. pix->field = V4L2_FIELD_NONE;
  768. return ret;
  769. }
  770. static int rj54n1_g_chip_ident(struct v4l2_subdev *sd,
  771. struct v4l2_dbg_chip_ident *id)
  772. {
  773. struct i2c_client *client = sd->priv;
  774. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  775. return -EINVAL;
  776. if (id->match.addr != client->addr)
  777. return -ENODEV;
  778. id->ident = V4L2_IDENT_RJ54N1CB0C;
  779. id->revision = 0;
  780. return 0;
  781. }
  782. #ifdef CONFIG_VIDEO_ADV_DEBUG
  783. static int rj54n1_g_register(struct v4l2_subdev *sd,
  784. struct v4l2_dbg_register *reg)
  785. {
  786. struct i2c_client *client = sd->priv;
  787. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
  788. reg->reg < 0x400 || reg->reg > 0x1fff)
  789. /* Registers > 0x0800 are only available from Sharp support */
  790. return -EINVAL;
  791. if (reg->match.addr != client->addr)
  792. return -ENODEV;
  793. reg->size = 1;
  794. reg->val = reg_read(client, reg->reg);
  795. if (reg->val > 0xff)
  796. return -EIO;
  797. return 0;
  798. }
  799. static int rj54n1_s_register(struct v4l2_subdev *sd,
  800. struct v4l2_dbg_register *reg)
  801. {
  802. struct i2c_client *client = sd->priv;
  803. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
  804. reg->reg < 0x400 || reg->reg > 0x1fff)
  805. /* Registers >= 0x0800 are only available from Sharp support */
  806. return -EINVAL;
  807. if (reg->match.addr != client->addr)
  808. return -ENODEV;
  809. if (reg_write(client, reg->reg, reg->val) < 0)
  810. return -EIO;
  811. return 0;
  812. }
  813. #endif
  814. static const struct v4l2_queryctrl rj54n1_controls[] = {
  815. {
  816. .id = V4L2_CID_VFLIP,
  817. .type = V4L2_CTRL_TYPE_BOOLEAN,
  818. .name = "Flip Vertically",
  819. .minimum = 0,
  820. .maximum = 1,
  821. .step = 1,
  822. .default_value = 0,
  823. }, {
  824. .id = V4L2_CID_HFLIP,
  825. .type = V4L2_CTRL_TYPE_BOOLEAN,
  826. .name = "Flip Horizontally",
  827. .minimum = 0,
  828. .maximum = 1,
  829. .step = 1,
  830. .default_value = 0,
  831. }, {
  832. .id = V4L2_CID_GAIN,
  833. .type = V4L2_CTRL_TYPE_INTEGER,
  834. .name = "Gain",
  835. .minimum = 0,
  836. .maximum = 127,
  837. .step = 1,
  838. .default_value = 66,
  839. .flags = V4L2_CTRL_FLAG_SLIDER,
  840. },
  841. };
  842. static struct soc_camera_ops rj54n1_ops = {
  843. .set_bus_param = rj54n1_set_bus_param,
  844. .query_bus_param = rj54n1_query_bus_param,
  845. .controls = rj54n1_controls,
  846. .num_controls = ARRAY_SIZE(rj54n1_controls),
  847. };
  848. static int rj54n1_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  849. {
  850. struct i2c_client *client = sd->priv;
  851. int data;
  852. switch (ctrl->id) {
  853. case V4L2_CID_VFLIP:
  854. data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
  855. if (data < 0)
  856. return -EIO;
  857. ctrl->value = !(data & 1);
  858. break;
  859. case V4L2_CID_HFLIP:
  860. data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
  861. if (data < 0)
  862. return -EIO;
  863. ctrl->value = !(data & 2);
  864. break;
  865. case V4L2_CID_GAIN:
  866. data = reg_read(client, RJ54N1_Y_GAIN);
  867. if (data < 0)
  868. return -EIO;
  869. ctrl->value = data / 2;
  870. break;
  871. }
  872. return 0;
  873. }
  874. static int rj54n1_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  875. {
  876. int data;
  877. struct i2c_client *client = sd->priv;
  878. const struct v4l2_queryctrl *qctrl;
  879. qctrl = soc_camera_find_qctrl(&rj54n1_ops, ctrl->id);
  880. if (!qctrl)
  881. return -EINVAL;
  882. switch (ctrl->id) {
  883. case V4L2_CID_VFLIP:
  884. if (ctrl->value)
  885. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1);
  886. else
  887. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1);
  888. if (data < 0)
  889. return -EIO;
  890. break;
  891. case V4L2_CID_HFLIP:
  892. if (ctrl->value)
  893. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2);
  894. else
  895. data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2);
  896. if (data < 0)
  897. return -EIO;
  898. break;
  899. case V4L2_CID_GAIN:
  900. if (ctrl->value > qctrl->maximum ||
  901. ctrl->value < qctrl->minimum)
  902. return -EINVAL;
  903. else if (reg_write(client, RJ54N1_Y_GAIN, ctrl->value * 2) < 0)
  904. return -EIO;
  905. break;
  906. }
  907. return 0;
  908. }
  909. static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
  910. .g_ctrl = rj54n1_g_ctrl,
  911. .s_ctrl = rj54n1_s_ctrl,
  912. .g_chip_ident = rj54n1_g_chip_ident,
  913. #ifdef CONFIG_VIDEO_ADV_DEBUG
  914. .g_register = rj54n1_g_register,
  915. .s_register = rj54n1_s_register,
  916. #endif
  917. };
  918. static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
  919. .s_stream = rj54n1_s_stream,
  920. .s_fmt = rj54n1_s_fmt,
  921. .g_fmt = rj54n1_g_fmt,
  922. .try_fmt = rj54n1_try_fmt,
  923. .g_crop = rj54n1_g_crop,
  924. .cropcap = rj54n1_cropcap,
  925. };
  926. static struct v4l2_subdev_ops rj54n1_subdev_ops = {
  927. .core = &rj54n1_subdev_core_ops,
  928. .video = &rj54n1_subdev_video_ops,
  929. };
  930. static int rj54n1_pin_config(struct i2c_client *client)
  931. {
  932. /*
  933. * Experimentally found out IOCTRL wired to 0. TODO: add to platform
  934. * data: 0 or 1 << 7.
  935. */
  936. return reg_write(client, RJ54N1_IOC, 0);
  937. }
  938. /*
  939. * Interface active, can use i2c. If it fails, it can indeed mean, that
  940. * this wasn't our capture interface, so, we wait for the right one
  941. */
  942. static int rj54n1_video_probe(struct soc_camera_device *icd,
  943. struct i2c_client *client)
  944. {
  945. int data1, data2;
  946. int ret;
  947. /* This could be a BUG_ON() or a WARN_ON(), or remove it completely */
  948. if (!icd->dev.parent ||
  949. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  950. return -ENODEV;
  951. /* Read out the chip version register */
  952. data1 = reg_read(client, RJ54N1_DEV_CODE);
  953. data2 = reg_read(client, RJ54N1_DEV_CODE2);
  954. if (data1 != 0x51 || data2 != 0x10) {
  955. ret = -ENODEV;
  956. dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
  957. data1, data2);
  958. goto ei2c;
  959. }
  960. ret = rj54n1_pin_config(client);
  961. if (ret < 0)
  962. goto ei2c;
  963. dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
  964. data1, data2);
  965. ei2c:
  966. return ret;
  967. }
  968. static int rj54n1_probe(struct i2c_client *client,
  969. const struct i2c_device_id *did)
  970. {
  971. struct rj54n1 *rj54n1;
  972. struct soc_camera_device *icd = client->dev.platform_data;
  973. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  974. struct soc_camera_link *icl;
  975. int ret;
  976. if (!icd) {
  977. dev_err(&client->dev, "RJ54N1CB0C: missing soc-camera data!\n");
  978. return -EINVAL;
  979. }
  980. icl = to_soc_camera_link(icd);
  981. if (!icl) {
  982. dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n");
  983. return -EINVAL;
  984. }
  985. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  986. dev_warn(&adapter->dev,
  987. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
  988. return -EIO;
  989. }
  990. rj54n1 = kzalloc(sizeof(struct rj54n1), GFP_KERNEL);
  991. if (!rj54n1)
  992. return -ENOMEM;
  993. v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops);
  994. icd->ops = &rj54n1_ops;
  995. rj54n1->clk_div = clk_div;
  996. rj54n1->rect.left = RJ54N1_COLUMN_SKIP;
  997. rj54n1->rect.top = RJ54N1_ROW_SKIP;
  998. rj54n1->rect.width = RJ54N1_MAX_WIDTH;
  999. rj54n1->rect.height = RJ54N1_MAX_HEIGHT;
  1000. rj54n1->width = RJ54N1_MAX_WIDTH;
  1001. rj54n1->height = RJ54N1_MAX_HEIGHT;
  1002. rj54n1->fourcc = V4L2_PIX_FMT_YUYV;
  1003. rj54n1->resize = 1024;
  1004. ret = rj54n1_video_probe(icd, client);
  1005. if (ret < 0) {
  1006. icd->ops = NULL;
  1007. i2c_set_clientdata(client, NULL);
  1008. kfree(rj54n1);
  1009. return ret;
  1010. }
  1011. icd->formats = rj54n1_colour_formats;
  1012. icd->num_formats = ARRAY_SIZE(rj54n1_colour_formats);
  1013. return ret;
  1014. }
  1015. static int rj54n1_remove(struct i2c_client *client)
  1016. {
  1017. struct rj54n1 *rj54n1 = to_rj54n1(client);
  1018. struct soc_camera_device *icd = client->dev.platform_data;
  1019. struct soc_camera_link *icl = to_soc_camera_link(icd);
  1020. icd->ops = NULL;
  1021. if (icl->free_bus)
  1022. icl->free_bus(icl);
  1023. i2c_set_clientdata(client, NULL);
  1024. client->driver = NULL;
  1025. kfree(rj54n1);
  1026. return 0;
  1027. }
  1028. static const struct i2c_device_id rj54n1_id[] = {
  1029. { "rj54n1cb0c", 0 },
  1030. { }
  1031. };
  1032. MODULE_DEVICE_TABLE(i2c, rj54n1_id);
  1033. static struct i2c_driver rj54n1_i2c_driver = {
  1034. .driver = {
  1035. .name = "rj54n1cb0c",
  1036. },
  1037. .probe = rj54n1_probe,
  1038. .remove = rj54n1_remove,
  1039. .id_table = rj54n1_id,
  1040. };
  1041. static int __init rj54n1_mod_init(void)
  1042. {
  1043. return i2c_add_driver(&rj54n1_i2c_driver);
  1044. }
  1045. static void __exit rj54n1_mod_exit(void)
  1046. {
  1047. i2c_del_driver(&rj54n1_i2c_driver);
  1048. }
  1049. module_init(rj54n1_mod_init);
  1050. module_exit(rj54n1_mod_exit);
  1051. MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
  1052. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  1053. MODULE_LICENSE("GPL v2");