vs6624.c 19 KB

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