rj54n1cb0c.c 30 KB

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