mt9t112.c 28 KB

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