vs6624.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vs6624.c ST VS6624 CMOS image sensor driver
  4. *
  5. * Copyright (c) 2011 Analog Devices Inc.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/gpio.h>
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/videodev2.h>
  16. #include <media/v4l2-ctrls.h>
  17. #include <media/v4l2-device.h>
  18. #include <media/v4l2-mediabus.h>
  19. #include <media/v4l2-image-sizes.h>
  20. #include "vs6624_regs.h"
  21. #define MAX_FRAME_RATE 30
  22. struct vs6624 {
  23. struct v4l2_subdev sd;
  24. struct v4l2_ctrl_handler hdl;
  25. struct v4l2_fract frame_rate;
  26. struct v4l2_mbus_framefmt fmt;
  27. unsigned ce_pin;
  28. };
  29. static const struct vs6624_format {
  30. u32 mbus_code;
  31. enum v4l2_colorspace colorspace;
  32. } vs6624_formats[] = {
  33. {
  34. .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
  35. .colorspace = V4L2_COLORSPACE_JPEG,
  36. },
  37. {
  38. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  39. .colorspace = V4L2_COLORSPACE_JPEG,
  40. },
  41. {
  42. .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  43. .colorspace = V4L2_COLORSPACE_SRGB,
  44. },
  45. };
  46. static const struct v4l2_mbus_framefmt vs6624_default_fmt = {
  47. .width = VGA_WIDTH,
  48. .height = VGA_HEIGHT,
  49. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  50. .field = V4L2_FIELD_NONE,
  51. .colorspace = V4L2_COLORSPACE_JPEG,
  52. };
  53. static const u16 vs6624_p1[] = {
  54. 0x8104, 0x03,
  55. 0x8105, 0x01,
  56. 0xc900, 0x03,
  57. 0xc904, 0x47,
  58. 0xc905, 0x10,
  59. 0xc906, 0x80,
  60. 0xc907, 0x3a,
  61. 0x903a, 0x02,
  62. 0x903b, 0x47,
  63. 0x903c, 0x15,
  64. 0xc908, 0x31,
  65. 0xc909, 0xdc,
  66. 0xc90a, 0x80,
  67. 0xc90b, 0x44,
  68. 0x9044, 0x02,
  69. 0x9045, 0x31,
  70. 0x9046, 0xe2,
  71. 0xc90c, 0x07,
  72. 0xc90d, 0xe0,
  73. 0xc90e, 0x80,
  74. 0xc90f, 0x47,
  75. 0x9047, 0x90,
  76. 0x9048, 0x83,
  77. 0x9049, 0x81,
  78. 0x904a, 0xe0,
  79. 0x904b, 0x60,
  80. 0x904c, 0x08,
  81. 0x904d, 0x90,
  82. 0x904e, 0xc0,
  83. 0x904f, 0x43,
  84. 0x9050, 0x74,
  85. 0x9051, 0x01,
  86. 0x9052, 0xf0,
  87. 0x9053, 0x80,
  88. 0x9054, 0x05,
  89. 0x9055, 0xE4,
  90. 0x9056, 0x90,
  91. 0x9057, 0xc0,
  92. 0x9058, 0x43,
  93. 0x9059, 0xf0,
  94. 0x905a, 0x02,
  95. 0x905b, 0x07,
  96. 0x905c, 0xec,
  97. 0xc910, 0x5d,
  98. 0xc911, 0xca,
  99. 0xc912, 0x80,
  100. 0xc913, 0x5d,
  101. 0x905d, 0xa3,
  102. 0x905e, 0x04,
  103. 0x905f, 0xf0,
  104. 0x9060, 0xa3,
  105. 0x9061, 0x04,
  106. 0x9062, 0xf0,
  107. 0x9063, 0x22,
  108. 0xc914, 0x72,
  109. 0xc915, 0x92,
  110. 0xc916, 0x80,
  111. 0xc917, 0x64,
  112. 0x9064, 0x74,
  113. 0x9065, 0x01,
  114. 0x9066, 0x02,
  115. 0x9067, 0x72,
  116. 0x9068, 0x95,
  117. 0xc918, 0x47,
  118. 0xc919, 0xf2,
  119. 0xc91a, 0x81,
  120. 0xc91b, 0x69,
  121. 0x9169, 0x74,
  122. 0x916a, 0x02,
  123. 0x916b, 0xf0,
  124. 0x916c, 0xec,
  125. 0x916d, 0xb4,
  126. 0x916e, 0x10,
  127. 0x916f, 0x0a,
  128. 0x9170, 0x90,
  129. 0x9171, 0x80,
  130. 0x9172, 0x16,
  131. 0x9173, 0xe0,
  132. 0x9174, 0x70,
  133. 0x9175, 0x04,
  134. 0x9176, 0x90,
  135. 0x9177, 0xd3,
  136. 0x9178, 0xc4,
  137. 0x9179, 0xf0,
  138. 0x917a, 0x22,
  139. 0xc91c, 0x0a,
  140. 0xc91d, 0xbe,
  141. 0xc91e, 0x80,
  142. 0xc91f, 0x73,
  143. 0x9073, 0xfc,
  144. 0x9074, 0xa3,
  145. 0x9075, 0xe0,
  146. 0x9076, 0xf5,
  147. 0x9077, 0x82,
  148. 0x9078, 0x8c,
  149. 0x9079, 0x83,
  150. 0x907a, 0xa3,
  151. 0x907b, 0xa3,
  152. 0x907c, 0xe0,
  153. 0x907d, 0xfc,
  154. 0x907e, 0xa3,
  155. 0x907f, 0xe0,
  156. 0x9080, 0xc3,
  157. 0x9081, 0x9f,
  158. 0x9082, 0xff,
  159. 0x9083, 0xec,
  160. 0x9084, 0x9e,
  161. 0x9085, 0xfe,
  162. 0x9086, 0x02,
  163. 0x9087, 0x0a,
  164. 0x9088, 0xea,
  165. 0xc920, 0x47,
  166. 0xc921, 0x38,
  167. 0xc922, 0x80,
  168. 0xc923, 0x89,
  169. 0x9089, 0xec,
  170. 0x908a, 0xd3,
  171. 0x908b, 0x94,
  172. 0x908c, 0x20,
  173. 0x908d, 0x40,
  174. 0x908e, 0x01,
  175. 0x908f, 0x1c,
  176. 0x9090, 0x90,
  177. 0x9091, 0xd3,
  178. 0x9092, 0xd4,
  179. 0x9093, 0xec,
  180. 0x9094, 0xf0,
  181. 0x9095, 0x02,
  182. 0x9096, 0x47,
  183. 0x9097, 0x3d,
  184. 0xc924, 0x45,
  185. 0xc925, 0xca,
  186. 0xc926, 0x80,
  187. 0xc927, 0x98,
  188. 0x9098, 0x12,
  189. 0x9099, 0x77,
  190. 0x909a, 0xd6,
  191. 0x909b, 0x02,
  192. 0x909c, 0x45,
  193. 0x909d, 0xcd,
  194. 0xc928, 0x20,
  195. 0xc929, 0xd5,
  196. 0xc92a, 0x80,
  197. 0xc92b, 0x9e,
  198. 0x909e, 0x90,
  199. 0x909f, 0x82,
  200. 0x90a0, 0x18,
  201. 0x90a1, 0xe0,
  202. 0x90a2, 0xb4,
  203. 0x90a3, 0x03,
  204. 0x90a4, 0x0e,
  205. 0x90a5, 0x90,
  206. 0x90a6, 0x83,
  207. 0x90a7, 0xbf,
  208. 0x90a8, 0xe0,
  209. 0x90a9, 0x60,
  210. 0x90aa, 0x08,
  211. 0x90ab, 0x90,
  212. 0x90ac, 0x81,
  213. 0x90ad, 0xfc,
  214. 0x90ae, 0xe0,
  215. 0x90af, 0xff,
  216. 0x90b0, 0xc3,
  217. 0x90b1, 0x13,
  218. 0x90b2, 0xf0,
  219. 0x90b3, 0x90,
  220. 0x90b4, 0x81,
  221. 0x90b5, 0xfc,
  222. 0x90b6, 0xe0,
  223. 0x90b7, 0xff,
  224. 0x90b8, 0x02,
  225. 0x90b9, 0x20,
  226. 0x90ba, 0xda,
  227. 0xc92c, 0x70,
  228. 0xc92d, 0xbc,
  229. 0xc92e, 0x80,
  230. 0xc92f, 0xbb,
  231. 0x90bb, 0x90,
  232. 0x90bc, 0x82,
  233. 0x90bd, 0x18,
  234. 0x90be, 0xe0,
  235. 0x90bf, 0xb4,
  236. 0x90c0, 0x03,
  237. 0x90c1, 0x06,
  238. 0x90c2, 0x90,
  239. 0x90c3, 0xc1,
  240. 0x90c4, 0x06,
  241. 0x90c5, 0x74,
  242. 0x90c6, 0x05,
  243. 0x90c7, 0xf0,
  244. 0x90c8, 0x90,
  245. 0x90c9, 0xd3,
  246. 0x90ca, 0xa0,
  247. 0x90cb, 0x02,
  248. 0x90cc, 0x70,
  249. 0x90cd, 0xbf,
  250. 0xc930, 0x72,
  251. 0xc931, 0x21,
  252. 0xc932, 0x81,
  253. 0xc933, 0x3b,
  254. 0x913b, 0x7d,
  255. 0x913c, 0x02,
  256. 0x913d, 0x7f,
  257. 0x913e, 0x7b,
  258. 0x913f, 0x02,
  259. 0x9140, 0x72,
  260. 0x9141, 0x25,
  261. 0xc934, 0x28,
  262. 0xc935, 0xae,
  263. 0xc936, 0x80,
  264. 0xc937, 0xd2,
  265. 0x90d2, 0xf0,
  266. 0x90d3, 0x90,
  267. 0x90d4, 0xd2,
  268. 0x90d5, 0x0a,
  269. 0x90d6, 0x02,
  270. 0x90d7, 0x28,
  271. 0x90d8, 0xb4,
  272. 0xc938, 0x28,
  273. 0xc939, 0xb1,
  274. 0xc93a, 0x80,
  275. 0xc93b, 0xd9,
  276. 0x90d9, 0x90,
  277. 0x90da, 0x83,
  278. 0x90db, 0xba,
  279. 0x90dc, 0xe0,
  280. 0x90dd, 0xff,
  281. 0x90de, 0x90,
  282. 0x90df, 0xd2,
  283. 0x90e0, 0x08,
  284. 0x90e1, 0xe0,
  285. 0x90e2, 0xe4,
  286. 0x90e3, 0xef,
  287. 0x90e4, 0xf0,
  288. 0x90e5, 0xa3,
  289. 0x90e6, 0xe0,
  290. 0x90e7, 0x74,
  291. 0x90e8, 0xff,
  292. 0x90e9, 0xf0,
  293. 0x90ea, 0x90,
  294. 0x90eb, 0xd2,
  295. 0x90ec, 0x0a,
  296. 0x90ed, 0x02,
  297. 0x90ee, 0x28,
  298. 0x90ef, 0xb4,
  299. 0xc93c, 0x29,
  300. 0xc93d, 0x79,
  301. 0xc93e, 0x80,
  302. 0xc93f, 0xf0,
  303. 0x90f0, 0xf0,
  304. 0x90f1, 0x90,
  305. 0x90f2, 0xd2,
  306. 0x90f3, 0x0e,
  307. 0x90f4, 0x02,
  308. 0x90f5, 0x29,
  309. 0x90f6, 0x7f,
  310. 0xc940, 0x29,
  311. 0xc941, 0x7c,
  312. 0xc942, 0x80,
  313. 0xc943, 0xf7,
  314. 0x90f7, 0x90,
  315. 0x90f8, 0x83,
  316. 0x90f9, 0xba,
  317. 0x90fa, 0xe0,
  318. 0x90fb, 0xff,
  319. 0x90fc, 0x90,
  320. 0x90fd, 0xd2,
  321. 0x90fe, 0x0c,
  322. 0x90ff, 0xe0,
  323. 0x9100, 0xe4,
  324. 0x9101, 0xef,
  325. 0x9102, 0xf0,
  326. 0x9103, 0xa3,
  327. 0x9104, 0xe0,
  328. 0x9105, 0x74,
  329. 0x9106, 0xff,
  330. 0x9107, 0xf0,
  331. 0x9108, 0x90,
  332. 0x9109, 0xd2,
  333. 0x910a, 0x0e,
  334. 0x910b, 0x02,
  335. 0x910c, 0x29,
  336. 0x910d, 0x7f,
  337. 0xc944, 0x2a,
  338. 0xc945, 0x42,
  339. 0xc946, 0x81,
  340. 0xc947, 0x0e,
  341. 0x910e, 0xf0,
  342. 0x910f, 0x90,
  343. 0x9110, 0xd2,
  344. 0x9111, 0x12,
  345. 0x9112, 0x02,
  346. 0x9113, 0x2a,
  347. 0x9114, 0x48,
  348. 0xc948, 0x2a,
  349. 0xc949, 0x45,
  350. 0xc94a, 0x81,
  351. 0xc94b, 0x15,
  352. 0x9115, 0x90,
  353. 0x9116, 0x83,
  354. 0x9117, 0xba,
  355. 0x9118, 0xe0,
  356. 0x9119, 0xff,
  357. 0x911a, 0x90,
  358. 0x911b, 0xd2,
  359. 0x911c, 0x10,
  360. 0x911d, 0xe0,
  361. 0x911e, 0xe4,
  362. 0x911f, 0xef,
  363. 0x9120, 0xf0,
  364. 0x9121, 0xa3,
  365. 0x9122, 0xe0,
  366. 0x9123, 0x74,
  367. 0x9124, 0xff,
  368. 0x9125, 0xf0,
  369. 0x9126, 0x90,
  370. 0x9127, 0xd2,
  371. 0x9128, 0x12,
  372. 0x9129, 0x02,
  373. 0x912a, 0x2a,
  374. 0x912b, 0x48,
  375. 0xc900, 0x01,
  376. 0x0000, 0x00,
  377. };
  378. static const u16 vs6624_p2[] = {
  379. 0x806f, 0x01,
  380. 0x058c, 0x01,
  381. 0x0000, 0x00,
  382. };
  383. static const u16 vs6624_run_setup[] = {
  384. 0x1d18, 0x00, /* Enableconstrainedwhitebalance */
  385. VS6624_PEAK_MIN_OUT_G_MSB, 0x3c, /* Damper PeakGain Output MSB */
  386. VS6624_PEAK_MIN_OUT_G_LSB, 0x66, /* Damper PeakGain Output LSB */
  387. VS6624_CM_LOW_THR_MSB, 0x65, /* Damper Low MSB */
  388. VS6624_CM_LOW_THR_LSB, 0xd1, /* Damper Low LSB */
  389. VS6624_CM_HIGH_THR_MSB, 0x66, /* Damper High MSB */
  390. VS6624_CM_HIGH_THR_LSB, 0x62, /* Damper High LSB */
  391. VS6624_CM_MIN_OUT_MSB, 0x00, /* Damper Min output MSB */
  392. VS6624_CM_MIN_OUT_LSB, 0x00, /* Damper Min output LSB */
  393. VS6624_NORA_DISABLE, 0x00, /* Nora fDisable */
  394. VS6624_NORA_USAGE, 0x04, /* Nora usage */
  395. VS6624_NORA_LOW_THR_MSB, 0x63, /* Damper Low MSB Changed 0x63 to 0x65 */
  396. VS6624_NORA_LOW_THR_LSB, 0xd1, /* Damper Low LSB */
  397. VS6624_NORA_HIGH_THR_MSB, 0x68, /* Damper High MSB */
  398. VS6624_NORA_HIGH_THR_LSB, 0xdd, /* Damper High LSB */
  399. VS6624_NORA_MIN_OUT_MSB, 0x3a, /* Damper Min output MSB */
  400. VS6624_NORA_MIN_OUT_LSB, 0x00, /* Damper Min output LSB */
  401. VS6624_F2B_DISABLE, 0x00, /* Disable */
  402. 0x1d8a, 0x30, /* MAXWeightHigh */
  403. 0x1d91, 0x62, /* fpDamperLowThresholdHigh MSB */
  404. 0x1d92, 0x4a, /* fpDamperLowThresholdHigh LSB */
  405. 0x1d95, 0x65, /* fpDamperHighThresholdHigh MSB */
  406. 0x1d96, 0x0e, /* fpDamperHighThresholdHigh LSB */
  407. 0x1da1, 0x3a, /* fpMinimumDamperOutputLow MSB */
  408. 0x1da2, 0xb8, /* fpMinimumDamperOutputLow LSB */
  409. 0x1e08, 0x06, /* MAXWeightLow */
  410. 0x1e0a, 0x0a, /* MAXWeightHigh */
  411. 0x1601, 0x3a, /* Red A MSB */
  412. 0x1602, 0x14, /* Red A LSB */
  413. 0x1605, 0x3b, /* Blue A MSB */
  414. 0x1606, 0x85, /* BLue A LSB */
  415. 0x1609, 0x3b, /* RED B MSB */
  416. 0x160a, 0x85, /* RED B LSB */
  417. 0x160d, 0x3a, /* Blue B MSB */
  418. 0x160e, 0x14, /* Blue B LSB */
  419. 0x1611, 0x30, /* Max Distance from Locus MSB */
  420. 0x1612, 0x8f, /* Max Distance from Locus MSB */
  421. 0x1614, 0x01, /* Enable constrainer */
  422. 0x0000, 0x00,
  423. };
  424. static const u16 vs6624_default[] = {
  425. VS6624_CONTRAST0, 0x84,
  426. VS6624_SATURATION0, 0x75,
  427. VS6624_GAMMA0, 0x11,
  428. VS6624_CONTRAST1, 0x84,
  429. VS6624_SATURATION1, 0x75,
  430. VS6624_GAMMA1, 0x11,
  431. VS6624_MAN_RG, 0x80,
  432. VS6624_MAN_GG, 0x80,
  433. VS6624_MAN_BG, 0x80,
  434. VS6624_WB_MODE, 0x1,
  435. VS6624_EXPO_COMPENSATION, 0xfe,
  436. VS6624_EXPO_METER, 0x0,
  437. VS6624_LIGHT_FREQ, 0x64,
  438. VS6624_PEAK_GAIN, 0xe,
  439. VS6624_PEAK_LOW_THR, 0x28,
  440. VS6624_HMIRROR0, 0x0,
  441. VS6624_VFLIP0, 0x0,
  442. VS6624_ZOOM_HSTEP0_MSB, 0x0,
  443. VS6624_ZOOM_HSTEP0_LSB, 0x1,
  444. VS6624_ZOOM_VSTEP0_MSB, 0x0,
  445. VS6624_ZOOM_VSTEP0_LSB, 0x1,
  446. VS6624_PAN_HSTEP0_MSB, 0x0,
  447. VS6624_PAN_HSTEP0_LSB, 0xf,
  448. VS6624_PAN_VSTEP0_MSB, 0x0,
  449. VS6624_PAN_VSTEP0_LSB, 0xf,
  450. VS6624_SENSOR_MODE, 0x1,
  451. VS6624_SYNC_CODE_SETUP, 0x21,
  452. VS6624_DISABLE_FR_DAMPER, 0x0,
  453. VS6624_FR_DEN, 0x1,
  454. VS6624_FR_NUM_LSB, 0xf,
  455. VS6624_INIT_PIPE_SETUP, 0x0,
  456. VS6624_IMG_FMT0, 0x0,
  457. VS6624_YUV_SETUP, 0x1,
  458. VS6624_IMAGE_SIZE0, 0x2,
  459. 0x0000, 0x00,
  460. };
  461. static inline struct vs6624 *to_vs6624(struct v4l2_subdev *sd)
  462. {
  463. return container_of(sd, struct vs6624, sd);
  464. }
  465. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  466. {
  467. return &container_of(ctrl->handler, struct vs6624, hdl)->sd;
  468. }
  469. #ifdef CONFIG_VIDEO_ADV_DEBUG
  470. static int vs6624_read(struct v4l2_subdev *sd, u16 index)
  471. {
  472. struct i2c_client *client = v4l2_get_subdevdata(sd);
  473. u8 buf[2];
  474. buf[0] = index >> 8;
  475. buf[1] = index;
  476. i2c_master_send(client, buf, 2);
  477. i2c_master_recv(client, buf, 1);
  478. return buf[0];
  479. }
  480. #endif
  481. static int vs6624_write(struct v4l2_subdev *sd, u16 index,
  482. u8 value)
  483. {
  484. struct i2c_client *client = v4l2_get_subdevdata(sd);
  485. u8 buf[3];
  486. buf[0] = index >> 8;
  487. buf[1] = index;
  488. buf[2] = value;
  489. return i2c_master_send(client, buf, 3);
  490. }
  491. static int vs6624_writeregs(struct v4l2_subdev *sd, const u16 *regs)
  492. {
  493. u16 reg;
  494. u8 data;
  495. while (*regs != 0x00) {
  496. reg = *regs++;
  497. data = *regs++;
  498. vs6624_write(sd, reg, data);
  499. }
  500. return 0;
  501. }
  502. static int vs6624_s_ctrl(struct v4l2_ctrl *ctrl)
  503. {
  504. struct v4l2_subdev *sd = to_sd(ctrl);
  505. switch (ctrl->id) {
  506. case V4L2_CID_CONTRAST:
  507. vs6624_write(sd, VS6624_CONTRAST0, ctrl->val);
  508. break;
  509. case V4L2_CID_SATURATION:
  510. vs6624_write(sd, VS6624_SATURATION0, ctrl->val);
  511. break;
  512. case V4L2_CID_HFLIP:
  513. vs6624_write(sd, VS6624_HMIRROR0, ctrl->val);
  514. break;
  515. case V4L2_CID_VFLIP:
  516. vs6624_write(sd, VS6624_VFLIP0, ctrl->val);
  517. break;
  518. default:
  519. return -EINVAL;
  520. }
  521. return 0;
  522. }
  523. static int vs6624_enum_mbus_code(struct v4l2_subdev *sd,
  524. struct v4l2_subdev_pad_config *cfg,
  525. struct v4l2_subdev_mbus_code_enum *code)
  526. {
  527. if (code->pad || code->index >= ARRAY_SIZE(vs6624_formats))
  528. return -EINVAL;
  529. code->code = vs6624_formats[code->index].mbus_code;
  530. return 0;
  531. }
  532. static int vs6624_set_fmt(struct v4l2_subdev *sd,
  533. struct v4l2_subdev_pad_config *cfg,
  534. struct v4l2_subdev_format *format)
  535. {
  536. struct v4l2_mbus_framefmt *fmt = &format->format;
  537. struct vs6624 *sensor = to_vs6624(sd);
  538. int index;
  539. if (format->pad)
  540. return -EINVAL;
  541. for (index = 0; index < ARRAY_SIZE(vs6624_formats); index++)
  542. if (vs6624_formats[index].mbus_code == fmt->code)
  543. break;
  544. if (index >= ARRAY_SIZE(vs6624_formats)) {
  545. /* default to first format */
  546. index = 0;
  547. fmt->code = vs6624_formats[0].mbus_code;
  548. }
  549. /* sensor mode is VGA */
  550. if (fmt->width > VGA_WIDTH)
  551. fmt->width = VGA_WIDTH;
  552. if (fmt->height > VGA_HEIGHT)
  553. fmt->height = VGA_HEIGHT;
  554. fmt->width = fmt->width & (~3);
  555. fmt->height = fmt->height & (~3);
  556. fmt->field = V4L2_FIELD_NONE;
  557. fmt->colorspace = vs6624_formats[index].colorspace;
  558. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  559. cfg->try_fmt = *fmt;
  560. return 0;
  561. }
  562. /* set image format */
  563. switch (fmt->code) {
  564. case MEDIA_BUS_FMT_UYVY8_2X8:
  565. vs6624_write(sd, VS6624_IMG_FMT0, 0x0);
  566. vs6624_write(sd, VS6624_YUV_SETUP, 0x1);
  567. break;
  568. case MEDIA_BUS_FMT_YUYV8_2X8:
  569. vs6624_write(sd, VS6624_IMG_FMT0, 0x0);
  570. vs6624_write(sd, VS6624_YUV_SETUP, 0x3);
  571. break;
  572. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  573. vs6624_write(sd, VS6624_IMG_FMT0, 0x4);
  574. vs6624_write(sd, VS6624_RGB_SETUP, 0x0);
  575. break;
  576. default:
  577. return -EINVAL;
  578. }
  579. /* set image size */
  580. if ((fmt->width == VGA_WIDTH) && (fmt->height == VGA_HEIGHT))
  581. vs6624_write(sd, VS6624_IMAGE_SIZE0, 0x2);
  582. else if ((fmt->width == QVGA_WIDTH) && (fmt->height == QVGA_HEIGHT))
  583. vs6624_write(sd, VS6624_IMAGE_SIZE0, 0x4);
  584. else if ((fmt->width == QQVGA_WIDTH) && (fmt->height == QQVGA_HEIGHT))
  585. vs6624_write(sd, VS6624_IMAGE_SIZE0, 0x6);
  586. else if ((fmt->width == CIF_WIDTH) && (fmt->height == CIF_HEIGHT))
  587. vs6624_write(sd, VS6624_IMAGE_SIZE0, 0x3);
  588. else if ((fmt->width == QCIF_WIDTH) && (fmt->height == QCIF_HEIGHT))
  589. vs6624_write(sd, VS6624_IMAGE_SIZE0, 0x5);
  590. else if ((fmt->width == QQCIF_WIDTH) && (fmt->height == QQCIF_HEIGHT))
  591. vs6624_write(sd, VS6624_IMAGE_SIZE0, 0x7);
  592. else {
  593. vs6624_write(sd, VS6624_IMAGE_SIZE0, 0x8);
  594. vs6624_write(sd, VS6624_MAN_HSIZE0_MSB, fmt->width >> 8);
  595. vs6624_write(sd, VS6624_MAN_HSIZE0_LSB, fmt->width & 0xFF);
  596. vs6624_write(sd, VS6624_MAN_VSIZE0_MSB, fmt->height >> 8);
  597. vs6624_write(sd, VS6624_MAN_VSIZE0_LSB, fmt->height & 0xFF);
  598. vs6624_write(sd, VS6624_CROP_CTRL0, 0x1);
  599. }
  600. sensor->fmt = *fmt;
  601. return 0;
  602. }
  603. static int vs6624_get_fmt(struct v4l2_subdev *sd,
  604. struct v4l2_subdev_pad_config *cfg,
  605. struct v4l2_subdev_format *format)
  606. {
  607. struct vs6624 *sensor = to_vs6624(sd);
  608. if (format->pad)
  609. return -EINVAL;
  610. format->format = sensor->fmt;
  611. return 0;
  612. }
  613. static int vs6624_g_frame_interval(struct v4l2_subdev *sd,
  614. struct v4l2_subdev_frame_interval *ival)
  615. {
  616. struct vs6624 *sensor = to_vs6624(sd);
  617. ival->interval.numerator = sensor->frame_rate.denominator;
  618. ival->interval.denominator = sensor->frame_rate.numerator;
  619. return 0;
  620. }
  621. static int vs6624_s_frame_interval(struct v4l2_subdev *sd,
  622. struct v4l2_subdev_frame_interval *ival)
  623. {
  624. struct vs6624 *sensor = to_vs6624(sd);
  625. struct v4l2_fract *tpf = &ival->interval;
  626. if (tpf->numerator == 0 || tpf->denominator == 0
  627. || (tpf->denominator > tpf->numerator * MAX_FRAME_RATE)) {
  628. /* reset to max frame rate */
  629. tpf->numerator = 1;
  630. tpf->denominator = MAX_FRAME_RATE;
  631. }
  632. sensor->frame_rate.numerator = tpf->denominator;
  633. sensor->frame_rate.denominator = tpf->numerator;
  634. vs6624_write(sd, VS6624_DISABLE_FR_DAMPER, 0x0);
  635. vs6624_write(sd, VS6624_FR_NUM_MSB,
  636. sensor->frame_rate.numerator >> 8);
  637. vs6624_write(sd, VS6624_FR_NUM_LSB,
  638. sensor->frame_rate.numerator & 0xFF);
  639. vs6624_write(sd, VS6624_FR_DEN,
  640. sensor->frame_rate.denominator & 0xFF);
  641. return 0;
  642. }
  643. static int vs6624_s_stream(struct v4l2_subdev *sd, int enable)
  644. {
  645. if (enable)
  646. vs6624_write(sd, VS6624_USER_CMD, 0x2);
  647. else
  648. vs6624_write(sd, VS6624_USER_CMD, 0x4);
  649. udelay(100);
  650. return 0;
  651. }
  652. #ifdef CONFIG_VIDEO_ADV_DEBUG
  653. static int vs6624_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  654. {
  655. reg->val = vs6624_read(sd, reg->reg & 0xffff);
  656. reg->size = 1;
  657. return 0;
  658. }
  659. static int vs6624_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  660. {
  661. vs6624_write(sd, reg->reg & 0xffff, reg->val & 0xff);
  662. return 0;
  663. }
  664. #endif
  665. static const struct v4l2_ctrl_ops vs6624_ctrl_ops = {
  666. .s_ctrl = vs6624_s_ctrl,
  667. };
  668. static const struct v4l2_subdev_core_ops vs6624_core_ops = {
  669. #ifdef CONFIG_VIDEO_ADV_DEBUG
  670. .g_register = vs6624_g_register,
  671. .s_register = vs6624_s_register,
  672. #endif
  673. };
  674. static const struct v4l2_subdev_video_ops vs6624_video_ops = {
  675. .s_frame_interval = vs6624_s_frame_interval,
  676. .g_frame_interval = vs6624_g_frame_interval,
  677. .s_stream = vs6624_s_stream,
  678. };
  679. static const struct v4l2_subdev_pad_ops vs6624_pad_ops = {
  680. .enum_mbus_code = vs6624_enum_mbus_code,
  681. .get_fmt = vs6624_get_fmt,
  682. .set_fmt = vs6624_set_fmt,
  683. };
  684. static const struct v4l2_subdev_ops vs6624_ops = {
  685. .core = &vs6624_core_ops,
  686. .video = &vs6624_video_ops,
  687. .pad = &vs6624_pad_ops,
  688. };
  689. static int vs6624_probe(struct i2c_client *client,
  690. const struct i2c_device_id *id)
  691. {
  692. struct vs6624 *sensor;
  693. struct v4l2_subdev *sd;
  694. struct v4l2_ctrl_handler *hdl;
  695. const unsigned *ce;
  696. int ret;
  697. /* Check if the adapter supports the needed features */
  698. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  699. return -EIO;
  700. ce = client->dev.platform_data;
  701. if (ce == NULL)
  702. return -EINVAL;
  703. ret = devm_gpio_request_one(&client->dev, *ce, GPIOF_OUT_INIT_HIGH,
  704. "VS6624 Chip Enable");
  705. if (ret) {
  706. v4l_err(client, "failed to request GPIO %d\n", *ce);
  707. return ret;
  708. }
  709. /* wait 100ms before any further i2c writes are performed */
  710. msleep(100);
  711. sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
  712. if (sensor == NULL)
  713. return -ENOMEM;
  714. sd = &sensor->sd;
  715. v4l2_i2c_subdev_init(sd, client, &vs6624_ops);
  716. vs6624_writeregs(sd, vs6624_p1);
  717. vs6624_write(sd, VS6624_MICRO_EN, 0x2);
  718. vs6624_write(sd, VS6624_DIO_EN, 0x1);
  719. usleep_range(10000, 11000);
  720. vs6624_writeregs(sd, vs6624_p2);
  721. vs6624_writeregs(sd, vs6624_default);
  722. vs6624_write(sd, VS6624_HSYNC_SETUP, 0xF);
  723. vs6624_writeregs(sd, vs6624_run_setup);
  724. /* set frame rate */
  725. sensor->frame_rate.numerator = MAX_FRAME_RATE;
  726. sensor->frame_rate.denominator = 1;
  727. vs6624_write(sd, VS6624_DISABLE_FR_DAMPER, 0x0);
  728. vs6624_write(sd, VS6624_FR_NUM_MSB,
  729. sensor->frame_rate.numerator >> 8);
  730. vs6624_write(sd, VS6624_FR_NUM_LSB,
  731. sensor->frame_rate.numerator & 0xFF);
  732. vs6624_write(sd, VS6624_FR_DEN,
  733. sensor->frame_rate.denominator & 0xFF);
  734. sensor->fmt = vs6624_default_fmt;
  735. sensor->ce_pin = *ce;
  736. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  737. client->addr << 1, client->adapter->name);
  738. hdl = &sensor->hdl;
  739. v4l2_ctrl_handler_init(hdl, 4);
  740. v4l2_ctrl_new_std(hdl, &vs6624_ctrl_ops,
  741. V4L2_CID_CONTRAST, 0, 0xFF, 1, 0x87);
  742. v4l2_ctrl_new_std(hdl, &vs6624_ctrl_ops,
  743. V4L2_CID_SATURATION, 0, 0xFF, 1, 0x78);
  744. v4l2_ctrl_new_std(hdl, &vs6624_ctrl_ops,
  745. V4L2_CID_HFLIP, 0, 1, 1, 0);
  746. v4l2_ctrl_new_std(hdl, &vs6624_ctrl_ops,
  747. V4L2_CID_VFLIP, 0, 1, 1, 0);
  748. /* hook the control handler into the driver */
  749. sd->ctrl_handler = hdl;
  750. if (hdl->error) {
  751. int err = hdl->error;
  752. v4l2_ctrl_handler_free(hdl);
  753. return err;
  754. }
  755. /* initialize the hardware to the default control values */
  756. ret = v4l2_ctrl_handler_setup(hdl);
  757. if (ret)
  758. v4l2_ctrl_handler_free(hdl);
  759. return ret;
  760. }
  761. static int vs6624_remove(struct i2c_client *client)
  762. {
  763. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  764. v4l2_device_unregister_subdev(sd);
  765. v4l2_ctrl_handler_free(sd->ctrl_handler);
  766. return 0;
  767. }
  768. static const struct i2c_device_id vs6624_id[] = {
  769. {"vs6624", 0},
  770. {},
  771. };
  772. MODULE_DEVICE_TABLE(i2c, vs6624_id);
  773. static struct i2c_driver vs6624_driver = {
  774. .driver = {
  775. .name = "vs6624",
  776. },
  777. .probe = vs6624_probe,
  778. .remove = vs6624_remove,
  779. .id_table = vs6624_id,
  780. };
  781. module_i2c_driver(vs6624_driver);
  782. MODULE_DESCRIPTION("VS6624 sensor driver");
  783. MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
  784. MODULE_LICENSE("GPL v2");