vs6624.c 19 KB

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