rj54n1cb0c.c 35 KB

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