rj54n1cb0c.c 35 KB

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