mt9t112.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * mt9t112 Camera Driver
  3. *
  4. * Copyright (C) 2009 Renesas Solutions Corp.
  5. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  6. *
  7. * Based on ov772x driver, mt9m111 driver,
  8. *
  9. * Copyright (C) 2008 Kuninori Morimoto <morimoto.kuninori@renesas.com>
  10. * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
  11. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  12. * Copyright (C) 2008 Magnus Damm
  13. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2 as
  17. * published by the Free Software Foundation.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/v4l2-mediabus.h>
  25. #include <linux/videodev2.h>
  26. #include <media/mt9t112.h>
  27. #include <media/soc_camera.h>
  28. #include <media/v4l2-clk.h>
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-image-sizes.h>
  31. /* you can check PLL/clock info */
  32. /* #define EXT_CLOCK 24000000 */
  33. /************************************************************************
  34. macro
  35. ************************************************************************/
  36. /*
  37. * frame size
  38. */
  39. #define MAX_WIDTH 2048
  40. #define MAX_HEIGHT 1536
  41. /*
  42. * macro of read/write
  43. */
  44. #define ECHECKER(ret, x) \
  45. do { \
  46. (ret) = (x); \
  47. if ((ret) < 0) \
  48. return (ret); \
  49. } while (0)
  50. #define mt9t112_reg_write(ret, client, a, b) \
  51. ECHECKER(ret, __mt9t112_reg_write(client, a, b))
  52. #define mt9t112_mcu_write(ret, client, a, b) \
  53. ECHECKER(ret, __mt9t112_mcu_write(client, a, b))
  54. #define mt9t112_reg_mask_set(ret, client, a, b, c) \
  55. ECHECKER(ret, __mt9t112_reg_mask_set(client, a, b, c))
  56. #define mt9t112_mcu_mask_set(ret, client, a, b, c) \
  57. ECHECKER(ret, __mt9t112_mcu_mask_set(client, a, b, c))
  58. #define mt9t112_reg_read(ret, client, a) \
  59. ECHECKER(ret, __mt9t112_reg_read(client, a))
  60. /*
  61. * Logical address
  62. */
  63. #define _VAR(id, offset, base) (base | (id & 0x1f) << 10 | (offset & 0x3ff))
  64. #define VAR(id, offset) _VAR(id, offset, 0x0000)
  65. #define VAR8(id, offset) _VAR(id, offset, 0x8000)
  66. /************************************************************************
  67. struct
  68. ************************************************************************/
  69. struct mt9t112_format {
  70. u32 code;
  71. enum v4l2_colorspace colorspace;
  72. u16 fmt;
  73. u16 order;
  74. };
  75. struct mt9t112_priv {
  76. struct v4l2_subdev subdev;
  77. struct mt9t112_camera_info *info;
  78. struct i2c_client *client;
  79. struct v4l2_rect frame;
  80. struct v4l2_clk *clk;
  81. const struct mt9t112_format *format;
  82. int num_formats;
  83. u32 flags;
  84. /* for flags */
  85. #define INIT_DONE (1 << 0)
  86. #define PCLK_RISING (1 << 1)
  87. };
  88. /************************************************************************
  89. supported format
  90. ************************************************************************/
  91. static const struct mt9t112_format mt9t112_cfmts[] = {
  92. {
  93. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  94. .colorspace = V4L2_COLORSPACE_SRGB,
  95. .fmt = 1,
  96. .order = 0,
  97. }, {
  98. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  99. .colorspace = V4L2_COLORSPACE_SRGB,
  100. .fmt = 1,
  101. .order = 1,
  102. }, {
  103. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  104. .colorspace = V4L2_COLORSPACE_SRGB,
  105. .fmt = 1,
  106. .order = 2,
  107. }, {
  108. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  109. .colorspace = V4L2_COLORSPACE_SRGB,
  110. .fmt = 1,
  111. .order = 3,
  112. }, {
  113. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
  114. .colorspace = V4L2_COLORSPACE_SRGB,
  115. .fmt = 8,
  116. .order = 2,
  117. }, {
  118. .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  119. .colorspace = V4L2_COLORSPACE_SRGB,
  120. .fmt = 4,
  121. .order = 2,
  122. },
  123. };
  124. /************************************************************************
  125. general function
  126. ************************************************************************/
  127. static struct mt9t112_priv *to_mt9t112(const struct i2c_client *client)
  128. {
  129. return container_of(i2c_get_clientdata(client),
  130. struct mt9t112_priv,
  131. subdev);
  132. }
  133. static int __mt9t112_reg_read(const struct i2c_client *client, u16 command)
  134. {
  135. struct i2c_msg msg[2];
  136. u8 buf[2];
  137. int ret;
  138. command = swab16(command);
  139. msg[0].addr = client->addr;
  140. msg[0].flags = 0;
  141. msg[0].len = 2;
  142. msg[0].buf = (u8 *)&command;
  143. msg[1].addr = client->addr;
  144. msg[1].flags = I2C_M_RD;
  145. msg[1].len = 2;
  146. msg[1].buf = buf;
  147. /*
  148. * if return value of this function is < 0,
  149. * it mean error.
  150. * else, under 16bit is valid data.
  151. */
  152. ret = i2c_transfer(client->adapter, msg, 2);
  153. if (ret < 0)
  154. return ret;
  155. memcpy(&ret, buf, 2);
  156. return swab16(ret);
  157. }
  158. static int __mt9t112_reg_write(const struct i2c_client *client,
  159. u16 command, u16 data)
  160. {
  161. struct i2c_msg msg;
  162. u8 buf[4];
  163. int ret;
  164. command = swab16(command);
  165. data = swab16(data);
  166. memcpy(buf + 0, &command, 2);
  167. memcpy(buf + 2, &data, 2);
  168. msg.addr = client->addr;
  169. msg.flags = 0;
  170. msg.len = 4;
  171. msg.buf = buf;
  172. /*
  173. * i2c_transfer return message length,
  174. * but this function should return 0 if correct case
  175. */
  176. ret = i2c_transfer(client->adapter, &msg, 1);
  177. if (ret >= 0)
  178. ret = 0;
  179. return ret;
  180. }
  181. static int __mt9t112_reg_mask_set(const struct i2c_client *client,
  182. u16 command,
  183. u16 mask,
  184. u16 set)
  185. {
  186. int val = __mt9t112_reg_read(client, command);
  187. if (val < 0)
  188. return val;
  189. val &= ~mask;
  190. val |= set & mask;
  191. return __mt9t112_reg_write(client, command, val);
  192. }
  193. /* mcu access */
  194. static int __mt9t112_mcu_read(const struct i2c_client *client, u16 command)
  195. {
  196. int ret;
  197. ret = __mt9t112_reg_write(client, 0x098E, command);
  198. if (ret < 0)
  199. return ret;
  200. return __mt9t112_reg_read(client, 0x0990);
  201. }
  202. static int __mt9t112_mcu_write(const struct i2c_client *client,
  203. u16 command, u16 data)
  204. {
  205. int ret;
  206. ret = __mt9t112_reg_write(client, 0x098E, command);
  207. if (ret < 0)
  208. return ret;
  209. return __mt9t112_reg_write(client, 0x0990, data);
  210. }
  211. static int __mt9t112_mcu_mask_set(const struct i2c_client *client,
  212. u16 command,
  213. u16 mask,
  214. u16 set)
  215. {
  216. int val = __mt9t112_mcu_read(client, command);
  217. if (val < 0)
  218. return val;
  219. val &= ~mask;
  220. val |= set & mask;
  221. return __mt9t112_mcu_write(client, command, val);
  222. }
  223. static int mt9t112_reset(const struct i2c_client *client)
  224. {
  225. int ret;
  226. mt9t112_reg_mask_set(ret, client, 0x001a, 0x0001, 0x0001);
  227. msleep(1);
  228. mt9t112_reg_mask_set(ret, client, 0x001a, 0x0001, 0x0000);
  229. return ret;
  230. }
  231. #ifndef EXT_CLOCK
  232. #define CLOCK_INFO(a, b)
  233. #else
  234. #define CLOCK_INFO(a, b) mt9t112_clock_info(a, b)
  235. static int mt9t112_clock_info(const struct i2c_client *client, u32 ext)
  236. {
  237. int m, n, p1, p2, p3, p4, p5, p6, p7;
  238. u32 vco, clk;
  239. char *enable;
  240. ext /= 1000; /* kbyte order */
  241. mt9t112_reg_read(n, client, 0x0012);
  242. p1 = n & 0x000f;
  243. n = n >> 4;
  244. p2 = n & 0x000f;
  245. n = n >> 4;
  246. p3 = n & 0x000f;
  247. mt9t112_reg_read(n, client, 0x002a);
  248. p4 = n & 0x000f;
  249. n = n >> 4;
  250. p5 = n & 0x000f;
  251. n = n >> 4;
  252. p6 = n & 0x000f;
  253. mt9t112_reg_read(n, client, 0x002c);
  254. p7 = n & 0x000f;
  255. mt9t112_reg_read(n, client, 0x0010);
  256. m = n & 0x00ff;
  257. n = (n >> 8) & 0x003f;
  258. enable = ((6000 > ext) || (54000 < ext)) ? "X" : "";
  259. dev_dbg(&client->dev, "EXTCLK : %10u K %s\n", ext, enable);
  260. vco = 2 * m * ext / (n+1);
  261. enable = ((384000 > vco) || (768000 < vco)) ? "X" : "";
  262. dev_dbg(&client->dev, "VCO : %10u K %s\n", vco, enable);
  263. clk = vco / (p1+1) / (p2+1);
  264. enable = (96000 < clk) ? "X" : "";
  265. dev_dbg(&client->dev, "PIXCLK : %10u K %s\n", clk, enable);
  266. clk = vco / (p3+1);
  267. enable = (768000 < clk) ? "X" : "";
  268. dev_dbg(&client->dev, "MIPICLK : %10u K %s\n", clk, enable);
  269. clk = vco / (p6+1);
  270. enable = (96000 < clk) ? "X" : "";
  271. dev_dbg(&client->dev, "MCU CLK : %10u K %s\n", clk, enable);
  272. clk = vco / (p5+1);
  273. enable = (54000 < clk) ? "X" : "";
  274. dev_dbg(&client->dev, "SOC CLK : %10u K %s\n", clk, enable);
  275. clk = vco / (p4+1);
  276. enable = (70000 < clk) ? "X" : "";
  277. dev_dbg(&client->dev, "Sensor CLK : %10u K %s\n", clk, enable);
  278. clk = vco / (p7+1);
  279. dev_dbg(&client->dev, "External sensor : %10u K\n", clk);
  280. clk = ext / (n+1);
  281. enable = ((2000 > clk) || (24000 < clk)) ? "X" : "";
  282. dev_dbg(&client->dev, "PFD : %10u K %s\n", clk, enable);
  283. return 0;
  284. }
  285. #endif
  286. static void mt9t112_frame_check(u32 *width, u32 *height, u32 *left, u32 *top)
  287. {
  288. soc_camera_limit_side(left, width, 0, 0, MAX_WIDTH);
  289. soc_camera_limit_side(top, height, 0, 0, MAX_HEIGHT);
  290. }
  291. static int mt9t112_set_a_frame_size(const struct i2c_client *client,
  292. u16 width,
  293. u16 height)
  294. {
  295. int ret;
  296. u16 wstart = (MAX_WIDTH - width) / 2;
  297. u16 hstart = (MAX_HEIGHT - height) / 2;
  298. /* (Context A) Image Width/Height */
  299. mt9t112_mcu_write(ret, client, VAR(26, 0), width);
  300. mt9t112_mcu_write(ret, client, VAR(26, 2), height);
  301. /* (Context A) Output Width/Height */
  302. mt9t112_mcu_write(ret, client, VAR(18, 43), 8 + width);
  303. mt9t112_mcu_write(ret, client, VAR(18, 45), 8 + height);
  304. /* (Context A) Start Row/Column */
  305. mt9t112_mcu_write(ret, client, VAR(18, 2), 4 + hstart);
  306. mt9t112_mcu_write(ret, client, VAR(18, 4), 4 + wstart);
  307. /* (Context A) End Row/Column */
  308. mt9t112_mcu_write(ret, client, VAR(18, 6), 11 + height + hstart);
  309. mt9t112_mcu_write(ret, client, VAR(18, 8), 11 + width + wstart);
  310. mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x06);
  311. return ret;
  312. }
  313. static int mt9t112_set_pll_dividers(const struct i2c_client *client,
  314. u8 m, u8 n,
  315. u8 p1, u8 p2, u8 p3,
  316. u8 p4, u8 p5, u8 p6,
  317. u8 p7)
  318. {
  319. int ret;
  320. u16 val;
  321. /* N/M */
  322. val = (n << 8) |
  323. (m << 0);
  324. mt9t112_reg_mask_set(ret, client, 0x0010, 0x3fff, val);
  325. /* P1/P2/P3 */
  326. val = ((p3 & 0x0F) << 8) |
  327. ((p2 & 0x0F) << 4) |
  328. ((p1 & 0x0F) << 0);
  329. mt9t112_reg_mask_set(ret, client, 0x0012, 0x0fff, val);
  330. /* P4/P5/P6 */
  331. val = (0x7 << 12) |
  332. ((p6 & 0x0F) << 8) |
  333. ((p5 & 0x0F) << 4) |
  334. ((p4 & 0x0F) << 0);
  335. mt9t112_reg_mask_set(ret, client, 0x002A, 0x7fff, val);
  336. /* P7 */
  337. val = (0x1 << 12) |
  338. ((p7 & 0x0F) << 0);
  339. mt9t112_reg_mask_set(ret, client, 0x002C, 0x100f, val);
  340. return ret;
  341. }
  342. static int mt9t112_init_pll(const struct i2c_client *client)
  343. {
  344. struct mt9t112_priv *priv = to_mt9t112(client);
  345. int data, i, ret;
  346. mt9t112_reg_mask_set(ret, client, 0x0014, 0x003, 0x0001);
  347. /* PLL control: BYPASS PLL = 8517 */
  348. mt9t112_reg_write(ret, client, 0x0014, 0x2145);
  349. /* Replace these registers when new timing parameters are generated */
  350. mt9t112_set_pll_dividers(client,
  351. priv->info->divider.m,
  352. priv->info->divider.n,
  353. priv->info->divider.p1,
  354. priv->info->divider.p2,
  355. priv->info->divider.p3,
  356. priv->info->divider.p4,
  357. priv->info->divider.p5,
  358. priv->info->divider.p6,
  359. priv->info->divider.p7);
  360. /*
  361. * TEST_BYPASS on
  362. * PLL_ENABLE on
  363. * SEL_LOCK_DET on
  364. * TEST_BYPASS off
  365. */
  366. mt9t112_reg_write(ret, client, 0x0014, 0x2525);
  367. mt9t112_reg_write(ret, client, 0x0014, 0x2527);
  368. mt9t112_reg_write(ret, client, 0x0014, 0x3427);
  369. mt9t112_reg_write(ret, client, 0x0014, 0x3027);
  370. mdelay(10);
  371. /*
  372. * PLL_BYPASS off
  373. * Reference clock count
  374. * I2C Master Clock Divider
  375. */
  376. mt9t112_reg_write(ret, client, 0x0014, 0x3046);
  377. mt9t112_reg_write(ret, client, 0x0016, 0x0400); /* JPEG initialization workaround */
  378. mt9t112_reg_write(ret, client, 0x0022, 0x0190);
  379. mt9t112_reg_write(ret, client, 0x3B84, 0x0212);
  380. /* External sensor clock is PLL bypass */
  381. mt9t112_reg_write(ret, client, 0x002E, 0x0500);
  382. mt9t112_reg_mask_set(ret, client, 0x0018, 0x0002, 0x0002);
  383. mt9t112_reg_mask_set(ret, client, 0x3B82, 0x0004, 0x0004);
  384. /* MCU disabled */
  385. mt9t112_reg_mask_set(ret, client, 0x0018, 0x0004, 0x0004);
  386. /* out of standby */
  387. mt9t112_reg_mask_set(ret, client, 0x0018, 0x0001, 0);
  388. mdelay(50);
  389. /*
  390. * Standby Workaround
  391. * Disable Secondary I2C Pads
  392. */
  393. mt9t112_reg_write(ret, client, 0x0614, 0x0001);
  394. mdelay(1);
  395. mt9t112_reg_write(ret, client, 0x0614, 0x0001);
  396. mdelay(1);
  397. mt9t112_reg_write(ret, client, 0x0614, 0x0001);
  398. mdelay(1);
  399. mt9t112_reg_write(ret, client, 0x0614, 0x0001);
  400. mdelay(1);
  401. mt9t112_reg_write(ret, client, 0x0614, 0x0001);
  402. mdelay(1);
  403. mt9t112_reg_write(ret, client, 0x0614, 0x0001);
  404. mdelay(1);
  405. /* poll to verify out of standby. Must Poll this bit */
  406. for (i = 0; i < 100; i++) {
  407. mt9t112_reg_read(data, client, 0x0018);
  408. if (!(0x4000 & data))
  409. break;
  410. mdelay(10);
  411. }
  412. return ret;
  413. }
  414. static int mt9t112_init_setting(const struct i2c_client *client)
  415. {
  416. int ret;
  417. /* Adaptive Output Clock (A) */
  418. mt9t112_mcu_mask_set(ret, client, VAR(26, 160), 0x0040, 0x0000);
  419. /* Read Mode (A) */
  420. mt9t112_mcu_write(ret, client, VAR(18, 12), 0x0024);
  421. /* Fine Correction (A) */
  422. mt9t112_mcu_write(ret, client, VAR(18, 15), 0x00CC);
  423. /* Fine IT Min (A) */
  424. mt9t112_mcu_write(ret, client, VAR(18, 17), 0x01f1);
  425. /* Fine IT Max Margin (A) */
  426. mt9t112_mcu_write(ret, client, VAR(18, 19), 0x00fF);
  427. /* Base Frame Lines (A) */
  428. mt9t112_mcu_write(ret, client, VAR(18, 29), 0x032D);
  429. /* Min Line Length (A) */
  430. mt9t112_mcu_write(ret, client, VAR(18, 31), 0x073a);
  431. /* Line Length (A) */
  432. mt9t112_mcu_write(ret, client, VAR(18, 37), 0x07d0);
  433. /* Adaptive Output Clock (B) */
  434. mt9t112_mcu_mask_set(ret, client, VAR(27, 160), 0x0040, 0x0000);
  435. /* Row Start (B) */
  436. mt9t112_mcu_write(ret, client, VAR(18, 74), 0x004);
  437. /* Column Start (B) */
  438. mt9t112_mcu_write(ret, client, VAR(18, 76), 0x004);
  439. /* Row End (B) */
  440. mt9t112_mcu_write(ret, client, VAR(18, 78), 0x60B);
  441. /* Column End (B) */
  442. mt9t112_mcu_write(ret, client, VAR(18, 80), 0x80B);
  443. /* Fine Correction (B) */
  444. mt9t112_mcu_write(ret, client, VAR(18, 87), 0x008C);
  445. /* Fine IT Min (B) */
  446. mt9t112_mcu_write(ret, client, VAR(18, 89), 0x01F1);
  447. /* Fine IT Max Margin (B) */
  448. mt9t112_mcu_write(ret, client, VAR(18, 91), 0x00FF);
  449. /* Base Frame Lines (B) */
  450. mt9t112_mcu_write(ret, client, VAR(18, 101), 0x0668);
  451. /* Min Line Length (B) */
  452. mt9t112_mcu_write(ret, client, VAR(18, 103), 0x0AF0);
  453. /* Line Length (B) */
  454. mt9t112_mcu_write(ret, client, VAR(18, 109), 0x0AF0);
  455. /*
  456. * Flicker Dectection registers
  457. * This section should be replaced whenever new Timing file is generated
  458. * All the following registers need to be replaced
  459. * Following registers are generated from Register Wizard but user can
  460. * modify them. For detail see auto flicker detection tuning
  461. */
  462. /* FD_FDPERIOD_SELECT */
  463. mt9t112_mcu_write(ret, client, VAR8(8, 5), 0x01);
  464. /* PRI_B_CONFIG_FD_ALGO_RUN */
  465. mt9t112_mcu_write(ret, client, VAR(27, 17), 0x0003);
  466. /* PRI_A_CONFIG_FD_ALGO_RUN */
  467. mt9t112_mcu_write(ret, client, VAR(26, 17), 0x0003);
  468. /*
  469. * AFD range detection tuning registers
  470. */
  471. /* search_f1_50 */
  472. mt9t112_mcu_write(ret, client, VAR8(18, 165), 0x25);
  473. /* search_f2_50 */
  474. mt9t112_mcu_write(ret, client, VAR8(18, 166), 0x28);
  475. /* search_f1_60 */
  476. mt9t112_mcu_write(ret, client, VAR8(18, 167), 0x2C);
  477. /* search_f2_60 */
  478. mt9t112_mcu_write(ret, client, VAR8(18, 168), 0x2F);
  479. /* period_50Hz (A) */
  480. mt9t112_mcu_write(ret, client, VAR8(18, 68), 0xBA);
  481. /* secret register by aptina */
  482. /* period_50Hz (A MSB) */
  483. mt9t112_mcu_write(ret, client, VAR8(18, 303), 0x00);
  484. /* period_60Hz (A) */
  485. mt9t112_mcu_write(ret, client, VAR8(18, 69), 0x9B);
  486. /* secret register by aptina */
  487. /* period_60Hz (A MSB) */
  488. mt9t112_mcu_write(ret, client, VAR8(18, 301), 0x00);
  489. /* period_50Hz (B) */
  490. mt9t112_mcu_write(ret, client, VAR8(18, 140), 0x82);
  491. /* secret register by aptina */
  492. /* period_50Hz (B) MSB */
  493. mt9t112_mcu_write(ret, client, VAR8(18, 304), 0x00);
  494. /* period_60Hz (B) */
  495. mt9t112_mcu_write(ret, client, VAR8(18, 141), 0x6D);
  496. /* secret register by aptina */
  497. /* period_60Hz (B) MSB */
  498. mt9t112_mcu_write(ret, client, VAR8(18, 302), 0x00);
  499. /* FD Mode */
  500. mt9t112_mcu_write(ret, client, VAR8(8, 2), 0x10);
  501. /* Stat_min */
  502. mt9t112_mcu_write(ret, client, VAR8(8, 9), 0x02);
  503. /* Stat_max */
  504. mt9t112_mcu_write(ret, client, VAR8(8, 10), 0x03);
  505. /* Min_amplitude */
  506. mt9t112_mcu_write(ret, client, VAR8(8, 12), 0x0A);
  507. /* RX FIFO Watermark (A) */
  508. mt9t112_mcu_write(ret, client, VAR(18, 70), 0x0014);
  509. /* RX FIFO Watermark (B) */
  510. mt9t112_mcu_write(ret, client, VAR(18, 142), 0x0014);
  511. /* MCLK: 16MHz
  512. * PCLK: 73MHz
  513. * CorePixCLK: 36.5 MHz
  514. */
  515. mt9t112_mcu_write(ret, client, VAR8(18, 0x0044), 133);
  516. mt9t112_mcu_write(ret, client, VAR8(18, 0x0045), 110);
  517. mt9t112_mcu_write(ret, client, VAR8(18, 0x008c), 130);
  518. mt9t112_mcu_write(ret, client, VAR8(18, 0x008d), 108);
  519. mt9t112_mcu_write(ret, client, VAR8(18, 0x00A5), 27);
  520. mt9t112_mcu_write(ret, client, VAR8(18, 0x00a6), 30);
  521. mt9t112_mcu_write(ret, client, VAR8(18, 0x00a7), 32);
  522. mt9t112_mcu_write(ret, client, VAR8(18, 0x00a8), 35);
  523. return ret;
  524. }
  525. static int mt9t112_auto_focus_setting(const struct i2c_client *client)
  526. {
  527. int ret;
  528. mt9t112_mcu_write(ret, client, VAR(12, 13), 0x000F);
  529. mt9t112_mcu_write(ret, client, VAR(12, 23), 0x0F0F);
  530. mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x06);
  531. mt9t112_reg_write(ret, client, 0x0614, 0x0000);
  532. mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x05);
  533. mt9t112_mcu_write(ret, client, VAR8(12, 2), 0x02);
  534. mt9t112_mcu_write(ret, client, VAR(12, 3), 0x0002);
  535. mt9t112_mcu_write(ret, client, VAR(17, 3), 0x8001);
  536. mt9t112_mcu_write(ret, client, VAR(17, 11), 0x0025);
  537. mt9t112_mcu_write(ret, client, VAR(17, 13), 0x0193);
  538. mt9t112_mcu_write(ret, client, VAR8(17, 33), 0x18);
  539. mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x05);
  540. return ret;
  541. }
  542. static int mt9t112_auto_focus_trigger(const struct i2c_client *client)
  543. {
  544. int ret;
  545. mt9t112_mcu_write(ret, client, VAR8(12, 25), 0x01);
  546. return ret;
  547. }
  548. static int mt9t112_init_camera(const struct i2c_client *client)
  549. {
  550. int ret;
  551. ECHECKER(ret, mt9t112_reset(client));
  552. ECHECKER(ret, mt9t112_init_pll(client));
  553. ECHECKER(ret, mt9t112_init_setting(client));
  554. ECHECKER(ret, mt9t112_auto_focus_setting(client));
  555. mt9t112_reg_mask_set(ret, client, 0x0018, 0x0004, 0);
  556. /* Analog setting B */
  557. mt9t112_reg_write(ret, client, 0x3084, 0x2409);
  558. mt9t112_reg_write(ret, client, 0x3092, 0x0A49);
  559. mt9t112_reg_write(ret, client, 0x3094, 0x4949);
  560. mt9t112_reg_write(ret, client, 0x3096, 0x4950);
  561. /*
  562. * Disable adaptive clock
  563. * PRI_A_CONFIG_JPEG_OB_TX_CONTROL_VAR
  564. * PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR
  565. */
  566. mt9t112_mcu_write(ret, client, VAR(26, 160), 0x0A2E);
  567. mt9t112_mcu_write(ret, client, VAR(27, 160), 0x0A2E);
  568. /* Configure STatus in Status_before_length Format and enable header */
  569. /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
  570. mt9t112_mcu_write(ret, client, VAR(27, 144), 0x0CB4);
  571. /* Enable JPEG in context B */
  572. /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
  573. mt9t112_mcu_write(ret, client, VAR8(27, 142), 0x01);
  574. /* Disable Dac_TXLO */
  575. mt9t112_reg_write(ret, client, 0x316C, 0x350F);
  576. /* Set max slew rates */
  577. mt9t112_reg_write(ret, client, 0x1E, 0x777);
  578. return ret;
  579. }
  580. /************************************************************************
  581. v4l2_subdev_core_ops
  582. ************************************************************************/
  583. #ifdef CONFIG_VIDEO_ADV_DEBUG
  584. static int mt9t112_g_register(struct v4l2_subdev *sd,
  585. struct v4l2_dbg_register *reg)
  586. {
  587. struct i2c_client *client = v4l2_get_subdevdata(sd);
  588. int ret;
  589. reg->size = 2;
  590. mt9t112_reg_read(ret, client, reg->reg);
  591. reg->val = (__u64)ret;
  592. return 0;
  593. }
  594. static int mt9t112_s_register(struct v4l2_subdev *sd,
  595. const struct v4l2_dbg_register *reg)
  596. {
  597. struct i2c_client *client = v4l2_get_subdevdata(sd);
  598. int ret;
  599. mt9t112_reg_write(ret, client, reg->reg, reg->val);
  600. return ret;
  601. }
  602. #endif
  603. static int mt9t112_s_power(struct v4l2_subdev *sd, int on)
  604. {
  605. struct i2c_client *client = v4l2_get_subdevdata(sd);
  606. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  607. struct mt9t112_priv *priv = to_mt9t112(client);
  608. return soc_camera_set_power(&client->dev, ssdd, priv->clk, on);
  609. }
  610. static struct v4l2_subdev_core_ops mt9t112_subdev_core_ops = {
  611. #ifdef CONFIG_VIDEO_ADV_DEBUG
  612. .g_register = mt9t112_g_register,
  613. .s_register = mt9t112_s_register,
  614. #endif
  615. .s_power = mt9t112_s_power,
  616. };
  617. /************************************************************************
  618. v4l2_subdev_video_ops
  619. ************************************************************************/
  620. static int mt9t112_s_stream(struct v4l2_subdev *sd, int enable)
  621. {
  622. struct i2c_client *client = v4l2_get_subdevdata(sd);
  623. struct mt9t112_priv *priv = to_mt9t112(client);
  624. int ret = 0;
  625. if (!enable) {
  626. /* FIXME
  627. *
  628. * If user selected large output size,
  629. * and used it long time,
  630. * mt9t112 camera will be very warm.
  631. *
  632. * But current driver can not stop mt9t112 camera.
  633. * So, set small size here to solve this problem.
  634. */
  635. mt9t112_set_a_frame_size(client, VGA_WIDTH, VGA_HEIGHT);
  636. return ret;
  637. }
  638. if (!(priv->flags & INIT_DONE)) {
  639. u16 param = PCLK_RISING & priv->flags ? 0x0001 : 0x0000;
  640. ECHECKER(ret, mt9t112_init_camera(client));
  641. /* Invert PCLK (Data sampled on falling edge of pixclk) */
  642. mt9t112_reg_write(ret, client, 0x3C20, param);
  643. mdelay(5);
  644. priv->flags |= INIT_DONE;
  645. }
  646. mt9t112_mcu_write(ret, client, VAR(26, 7), priv->format->fmt);
  647. mt9t112_mcu_write(ret, client, VAR(26, 9), priv->format->order);
  648. mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x06);
  649. mt9t112_set_a_frame_size(client,
  650. priv->frame.width,
  651. priv->frame.height);
  652. ECHECKER(ret, mt9t112_auto_focus_trigger(client));
  653. dev_dbg(&client->dev, "format : %d\n", priv->format->code);
  654. dev_dbg(&client->dev, "size : %d x %d\n",
  655. priv->frame.width,
  656. priv->frame.height);
  657. CLOCK_INFO(client, EXT_CLOCK);
  658. return ret;
  659. }
  660. static int mt9t112_set_params(struct mt9t112_priv *priv,
  661. const struct v4l2_rect *rect,
  662. u32 code)
  663. {
  664. int i;
  665. /*
  666. * get color format
  667. */
  668. for (i = 0; i < priv->num_formats; i++)
  669. if (mt9t112_cfmts[i].code == code)
  670. break;
  671. if (i == priv->num_formats)
  672. return -EINVAL;
  673. priv->frame = *rect;
  674. /*
  675. * frame size check
  676. */
  677. mt9t112_frame_check(&priv->frame.width, &priv->frame.height,
  678. &priv->frame.left, &priv->frame.top);
  679. priv->format = mt9t112_cfmts + i;
  680. return 0;
  681. }
  682. static int mt9t112_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  683. {
  684. a->bounds.left = 0;
  685. a->bounds.top = 0;
  686. a->bounds.width = MAX_WIDTH;
  687. a->bounds.height = MAX_HEIGHT;
  688. a->defrect.left = 0;
  689. a->defrect.top = 0;
  690. a->defrect.width = VGA_WIDTH;
  691. a->defrect.height = VGA_HEIGHT;
  692. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  693. a->pixelaspect.numerator = 1;
  694. a->pixelaspect.denominator = 1;
  695. return 0;
  696. }
  697. static int mt9t112_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  698. {
  699. struct i2c_client *client = v4l2_get_subdevdata(sd);
  700. struct mt9t112_priv *priv = to_mt9t112(client);
  701. a->c = priv->frame;
  702. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  703. return 0;
  704. }
  705. static int mt9t112_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
  706. {
  707. struct i2c_client *client = v4l2_get_subdevdata(sd);
  708. struct mt9t112_priv *priv = to_mt9t112(client);
  709. const struct v4l2_rect *rect = &a->c;
  710. return mt9t112_set_params(priv, rect, priv->format->code);
  711. }
  712. static int mt9t112_get_fmt(struct v4l2_subdev *sd,
  713. struct v4l2_subdev_pad_config *cfg,
  714. struct v4l2_subdev_format *format)
  715. {
  716. struct v4l2_mbus_framefmt *mf = &format->format;
  717. struct i2c_client *client = v4l2_get_subdevdata(sd);
  718. struct mt9t112_priv *priv = to_mt9t112(client);
  719. if (format->pad)
  720. return -EINVAL;
  721. mf->width = priv->frame.width;
  722. mf->height = priv->frame.height;
  723. mf->colorspace = priv->format->colorspace;
  724. mf->code = priv->format->code;
  725. mf->field = V4L2_FIELD_NONE;
  726. return 0;
  727. }
  728. static int mt9t112_s_fmt(struct v4l2_subdev *sd,
  729. struct v4l2_mbus_framefmt *mf)
  730. {
  731. struct i2c_client *client = v4l2_get_subdevdata(sd);
  732. struct mt9t112_priv *priv = to_mt9t112(client);
  733. struct v4l2_rect rect = {
  734. .width = mf->width,
  735. .height = mf->height,
  736. .left = priv->frame.left,
  737. .top = priv->frame.top,
  738. };
  739. int ret;
  740. ret = mt9t112_set_params(priv, &rect, mf->code);
  741. if (!ret)
  742. mf->colorspace = priv->format->colorspace;
  743. return ret;
  744. }
  745. static int mt9t112_set_fmt(struct v4l2_subdev *sd,
  746. struct v4l2_subdev_pad_config *cfg,
  747. struct v4l2_subdev_format *format)
  748. {
  749. struct v4l2_mbus_framefmt *mf = &format->format;
  750. struct i2c_client *client = v4l2_get_subdevdata(sd);
  751. struct mt9t112_priv *priv = to_mt9t112(client);
  752. unsigned int top, left;
  753. int i;
  754. if (format->pad)
  755. return -EINVAL;
  756. for (i = 0; i < priv->num_formats; i++)
  757. if (mt9t112_cfmts[i].code == mf->code)
  758. break;
  759. if (i == priv->num_formats) {
  760. mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
  761. mf->colorspace = V4L2_COLORSPACE_JPEG;
  762. } else {
  763. mf->colorspace = mt9t112_cfmts[i].colorspace;
  764. }
  765. mt9t112_frame_check(&mf->width, &mf->height, &left, &top);
  766. mf->field = V4L2_FIELD_NONE;
  767. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  768. return mt9t112_s_fmt(sd, mf);
  769. cfg->try_fmt = *mf;
  770. return 0;
  771. }
  772. static int mt9t112_enum_mbus_code(struct v4l2_subdev *sd,
  773. struct v4l2_subdev_pad_config *cfg,
  774. struct v4l2_subdev_mbus_code_enum *code)
  775. {
  776. struct i2c_client *client = v4l2_get_subdevdata(sd);
  777. struct mt9t112_priv *priv = to_mt9t112(client);
  778. if (code->pad || code->index >= priv->num_formats)
  779. return -EINVAL;
  780. code->code = mt9t112_cfmts[code->index].code;
  781. return 0;
  782. }
  783. static int mt9t112_g_mbus_config(struct v4l2_subdev *sd,
  784. struct v4l2_mbus_config *cfg)
  785. {
  786. struct i2c_client *client = v4l2_get_subdevdata(sd);
  787. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  788. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  789. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH |
  790. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
  791. cfg->type = V4L2_MBUS_PARALLEL;
  792. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  793. return 0;
  794. }
  795. static int mt9t112_s_mbus_config(struct v4l2_subdev *sd,
  796. const struct v4l2_mbus_config *cfg)
  797. {
  798. struct i2c_client *client = v4l2_get_subdevdata(sd);
  799. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  800. struct mt9t112_priv *priv = to_mt9t112(client);
  801. if (soc_camera_apply_board_flags(ssdd, cfg) & V4L2_MBUS_PCLK_SAMPLE_RISING)
  802. priv->flags |= PCLK_RISING;
  803. return 0;
  804. }
  805. static struct v4l2_subdev_video_ops mt9t112_subdev_video_ops = {
  806. .s_stream = mt9t112_s_stream,
  807. .cropcap = mt9t112_cropcap,
  808. .g_crop = mt9t112_g_crop,
  809. .s_crop = mt9t112_s_crop,
  810. .g_mbus_config = mt9t112_g_mbus_config,
  811. .s_mbus_config = mt9t112_s_mbus_config,
  812. };
  813. static const struct v4l2_subdev_pad_ops mt9t112_subdev_pad_ops = {
  814. .enum_mbus_code = mt9t112_enum_mbus_code,
  815. .get_fmt = mt9t112_get_fmt,
  816. .set_fmt = mt9t112_set_fmt,
  817. };
  818. /************************************************************************
  819. i2c driver
  820. ************************************************************************/
  821. static struct v4l2_subdev_ops mt9t112_subdev_ops = {
  822. .core = &mt9t112_subdev_core_ops,
  823. .video = &mt9t112_subdev_video_ops,
  824. .pad = &mt9t112_subdev_pad_ops,
  825. };
  826. static int mt9t112_camera_probe(struct i2c_client *client)
  827. {
  828. struct mt9t112_priv *priv = to_mt9t112(client);
  829. const char *devname;
  830. int chipid;
  831. int ret;
  832. ret = mt9t112_s_power(&priv->subdev, 1);
  833. if (ret < 0)
  834. return ret;
  835. /*
  836. * check and show chip ID
  837. */
  838. mt9t112_reg_read(chipid, client, 0x0000);
  839. switch (chipid) {
  840. case 0x2680:
  841. devname = "mt9t111";
  842. priv->num_formats = 1;
  843. break;
  844. case 0x2682:
  845. devname = "mt9t112";
  846. priv->num_formats = ARRAY_SIZE(mt9t112_cfmts);
  847. break;
  848. default:
  849. dev_err(&client->dev, "Product ID error %04x\n", chipid);
  850. ret = -ENODEV;
  851. goto done;
  852. }
  853. dev_info(&client->dev, "%s chip ID %04x\n", devname, chipid);
  854. done:
  855. mt9t112_s_power(&priv->subdev, 0);
  856. return ret;
  857. }
  858. static int mt9t112_probe(struct i2c_client *client,
  859. const struct i2c_device_id *did)
  860. {
  861. struct mt9t112_priv *priv;
  862. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  863. struct v4l2_rect rect = {
  864. .width = VGA_WIDTH,
  865. .height = VGA_HEIGHT,
  866. .left = (MAX_WIDTH - VGA_WIDTH) / 2,
  867. .top = (MAX_HEIGHT - VGA_HEIGHT) / 2,
  868. };
  869. int ret;
  870. if (!ssdd || !ssdd->drv_priv) {
  871. dev_err(&client->dev, "mt9t112: missing platform data!\n");
  872. return -EINVAL;
  873. }
  874. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  875. if (!priv)
  876. return -ENOMEM;
  877. priv->info = ssdd->drv_priv;
  878. v4l2_i2c_subdev_init(&priv->subdev, client, &mt9t112_subdev_ops);
  879. priv->clk = v4l2_clk_get(&client->dev, "mclk");
  880. if (IS_ERR(priv->clk))
  881. return PTR_ERR(priv->clk);
  882. ret = mt9t112_camera_probe(client);
  883. /* Cannot fail: using the default supported pixel code */
  884. if (!ret)
  885. mt9t112_set_params(priv, &rect, MEDIA_BUS_FMT_UYVY8_2X8);
  886. else
  887. v4l2_clk_put(priv->clk);
  888. return ret;
  889. }
  890. static int mt9t112_remove(struct i2c_client *client)
  891. {
  892. struct mt9t112_priv *priv = to_mt9t112(client);
  893. v4l2_clk_put(priv->clk);
  894. return 0;
  895. }
  896. static const struct i2c_device_id mt9t112_id[] = {
  897. { "mt9t112", 0 },
  898. { }
  899. };
  900. MODULE_DEVICE_TABLE(i2c, mt9t112_id);
  901. static struct i2c_driver mt9t112_i2c_driver = {
  902. .driver = {
  903. .name = "mt9t112",
  904. },
  905. .probe = mt9t112_probe,
  906. .remove = mt9t112_remove,
  907. .id_table = mt9t112_id,
  908. };
  909. module_i2c_driver(mt9t112_i2c_driver);
  910. MODULE_DESCRIPTION("SoC Camera driver for mt9t112");
  911. MODULE_AUTHOR("Kuninori Morimoto");
  912. MODULE_LICENSE("GPL v2");