vs6624.c 21 KB

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