vs6624.c 19 KB

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