mt9t112.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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-chip-ident.h>
  29. #include <media/v4l2-common.h>
  30. /* you can check PLL/clock info */
  31. /* #define EXT_CLOCK 24000000 */
  32. /************************************************************************
  33. macro
  34. ************************************************************************/
  35. /*
  36. * frame size
  37. */
  38. #define MAX_WIDTH 2048
  39. #define MAX_HEIGHT 1536
  40. #define VGA_WIDTH 640
  41. #define VGA_HEIGHT 480
  42. /*
  43. * macro of read/write
  44. */
  45. #define ECHECKER(ret, x) \
  46. do { \
  47. (ret) = (x); \
  48. if ((ret) < 0) \
  49. return (ret); \
  50. } while (0)
  51. #define mt9t112_reg_write(ret, client, a, b) \
  52. ECHECKER(ret, __mt9t112_reg_write(client, a, b))
  53. #define mt9t112_mcu_write(ret, client, a, b) \
  54. ECHECKER(ret, __mt9t112_mcu_write(client, a, b))
  55. #define mt9t112_reg_mask_set(ret, client, a, b, c) \
  56. ECHECKER(ret, __mt9t112_reg_mask_set(client, a, b, c))
  57. #define mt9t112_mcu_mask_set(ret, client, a, b, c) \
  58. ECHECKER(ret, __mt9t112_mcu_mask_set(client, a, b, c))
  59. #define mt9t112_reg_read(ret, client, a) \
  60. ECHECKER(ret, __mt9t112_reg_read(client, a))
  61. /*
  62. * Logical address
  63. */
  64. #define _VAR(id, offset, base) (base | (id & 0x1f) << 10 | (offset & 0x3ff))
  65. #define VAR(id, offset) _VAR(id, offset, 0x0000)
  66. #define VAR8(id, offset) _VAR(id, offset, 0x8000)
  67. /************************************************************************
  68. struct
  69. ************************************************************************/
  70. struct mt9t112_format {
  71. enum v4l2_mbus_pixelcode code;
  72. enum v4l2_colorspace colorspace;
  73. u16 fmt;
  74. u16 order;
  75. };
  76. struct mt9t112_priv {
  77. struct v4l2_subdev subdev;
  78. struct mt9t112_camera_info *info;
  79. struct i2c_client *client;
  80. struct v4l2_rect frame;
  81. const struct mt9t112_format *format;
  82. int model;
  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 = V4L2_MBUS_FMT_UYVY8_2X8,
  94. .colorspace = V4L2_COLORSPACE_JPEG,
  95. .fmt = 1,
  96. .order = 0,
  97. }, {
  98. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  99. .colorspace = V4L2_COLORSPACE_JPEG,
  100. .fmt = 1,
  101. .order = 1,
  102. }, {
  103. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  104. .colorspace = V4L2_COLORSPACE_JPEG,
  105. .fmt = 1,
  106. .order = 2,
  107. }, {
  108. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  109. .colorspace = V4L2_COLORSPACE_JPEG,
  110. .fmt = 1,
  111. .order = 3,
  112. }, {
  113. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
  114. .colorspace = V4L2_COLORSPACE_SRGB,
  115. .fmt = 8,
  116. .order = 2,
  117. }, {
  118. .code = V4L2_MBUS_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, 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. static int mt9t112_g_chip_ident(struct v4l2_subdev *sd,
  583. struct v4l2_dbg_chip_ident *id)
  584. {
  585. struct i2c_client *client = v4l2_get_subdevdata(sd);
  586. struct mt9t112_priv *priv = to_mt9t112(client);
  587. id->ident = priv->model;
  588. id->revision = 0;
  589. return 0;
  590. }
  591. #ifdef CONFIG_VIDEO_ADV_DEBUG
  592. static int mt9t112_g_register(struct v4l2_subdev *sd,
  593. struct v4l2_dbg_register *reg)
  594. {
  595. struct i2c_client *client = v4l2_get_subdevdata(sd);
  596. int ret;
  597. reg->size = 2;
  598. mt9t112_reg_read(ret, client, reg->reg);
  599. reg->val = (__u64)ret;
  600. return 0;
  601. }
  602. static int mt9t112_s_register(struct v4l2_subdev *sd,
  603. struct v4l2_dbg_register *reg)
  604. {
  605. struct i2c_client *client = v4l2_get_subdevdata(sd);
  606. int ret;
  607. mt9t112_reg_write(ret, client, reg->reg, reg->val);
  608. return ret;
  609. }
  610. #endif
  611. static struct v4l2_subdev_core_ops mt9t112_subdev_core_ops = {
  612. .g_chip_ident = mt9t112_g_chip_ident,
  613. #ifdef CONFIG_VIDEO_ADV_DEBUG
  614. .g_register = mt9t112_g_register,
  615. .s_register = mt9t112_s_register,
  616. #endif
  617. };
  618. /************************************************************************
  619. v4l2_subdev_video_ops
  620. ************************************************************************/
  621. static int mt9t112_s_stream(struct v4l2_subdev *sd, int enable)
  622. {
  623. struct i2c_client *client = v4l2_get_subdevdata(sd);
  624. struct mt9t112_priv *priv = to_mt9t112(client);
  625. int ret = 0;
  626. if (!enable) {
  627. /* FIXME
  628. *
  629. * If user selected large output size,
  630. * and used it long time,
  631. * mt9t112 camera will be very warm.
  632. *
  633. * But current driver can not stop mt9t112 camera.
  634. * So, set small size here to solve this problem.
  635. */
  636. mt9t112_set_a_frame_size(client, VGA_WIDTH, VGA_HEIGHT);
  637. return ret;
  638. }
  639. if (!(priv->flags & INIT_DONE)) {
  640. u16 param = PCLK_RISING & priv->flags ? 0x0001 : 0x0000;
  641. ECHECKER(ret, mt9t112_init_camera(client));
  642. /* Invert PCLK (Data sampled on falling edge of pixclk) */
  643. mt9t112_reg_write(ret, client, 0x3C20, param);
  644. mdelay(5);
  645. priv->flags |= INIT_DONE;
  646. }
  647. mt9t112_mcu_write(ret, client, VAR(26, 7), priv->format->fmt);
  648. mt9t112_mcu_write(ret, client, VAR(26, 9), priv->format->order);
  649. mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x06);
  650. mt9t112_set_a_frame_size(client,
  651. priv->frame.width,
  652. priv->frame.height);
  653. ECHECKER(ret, mt9t112_auto_focus_trigger(client));
  654. dev_dbg(&client->dev, "format : %d\n", priv->format->code);
  655. dev_dbg(&client->dev, "size : %d x %d\n",
  656. priv->frame.width,
  657. priv->frame.height);
  658. CLOCK_INFO(client, EXT_CLOCK);
  659. return ret;
  660. }
  661. static int mt9t112_set_params(struct mt9t112_priv *priv,
  662. const struct v4l2_rect *rect,
  663. enum v4l2_mbus_pixelcode code)
  664. {
  665. int i;
  666. /*
  667. * get color format
  668. */
  669. for (i = 0; i < ARRAY_SIZE(mt9t112_cfmts); i++)
  670. if (mt9t112_cfmts[i].code == code)
  671. break;
  672. if (i == ARRAY_SIZE(mt9t112_cfmts))
  673. return -EINVAL;
  674. priv->frame = *rect;
  675. /*
  676. * frame size check
  677. */
  678. mt9t112_frame_check(&priv->frame.width, &priv->frame.height,
  679. &priv->frame.left, &priv->frame.top);
  680. priv->format = mt9t112_cfmts + i;
  681. return 0;
  682. }
  683. static int mt9t112_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  684. {
  685. a->bounds.left = 0;
  686. a->bounds.top = 0;
  687. a->bounds.width = MAX_WIDTH;
  688. a->bounds.height = MAX_HEIGHT;
  689. a->defrect.left = 0;
  690. a->defrect.top = 0;
  691. a->defrect.width = VGA_WIDTH;
  692. a->defrect.height = VGA_HEIGHT;
  693. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  694. a->pixelaspect.numerator = 1;
  695. a->pixelaspect.denominator = 1;
  696. return 0;
  697. }
  698. static int mt9t112_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  699. {
  700. struct i2c_client *client = v4l2_get_subdevdata(sd);
  701. struct mt9t112_priv *priv = to_mt9t112(client);
  702. a->c = priv->frame;
  703. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  704. return 0;
  705. }
  706. static int mt9t112_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  707. {
  708. struct i2c_client *client = v4l2_get_subdevdata(sd);
  709. struct mt9t112_priv *priv = to_mt9t112(client);
  710. struct v4l2_rect *rect = &a->c;
  711. return mt9t112_set_params(priv, rect, priv->format->code);
  712. }
  713. static int mt9t112_g_fmt(struct v4l2_subdev *sd,
  714. struct v4l2_mbus_framefmt *mf)
  715. {
  716. struct i2c_client *client = v4l2_get_subdevdata(sd);
  717. struct mt9t112_priv *priv = to_mt9t112(client);
  718. mf->width = priv->frame.width;
  719. mf->height = priv->frame.height;
  720. mf->colorspace = priv->format->colorspace;
  721. mf->code = priv->format->code;
  722. mf->field = V4L2_FIELD_NONE;
  723. return 0;
  724. }
  725. static int mt9t112_s_fmt(struct v4l2_subdev *sd,
  726. struct v4l2_mbus_framefmt *mf)
  727. {
  728. struct i2c_client *client = v4l2_get_subdevdata(sd);
  729. struct mt9t112_priv *priv = to_mt9t112(client);
  730. struct v4l2_rect rect = {
  731. .width = mf->width,
  732. .height = mf->height,
  733. .left = priv->frame.left,
  734. .top = priv->frame.top,
  735. };
  736. int ret;
  737. ret = mt9t112_set_params(priv, &rect, mf->code);
  738. if (!ret)
  739. mf->colorspace = priv->format->colorspace;
  740. return ret;
  741. }
  742. static int mt9t112_try_fmt(struct v4l2_subdev *sd,
  743. struct v4l2_mbus_framefmt *mf)
  744. {
  745. unsigned int top, left;
  746. int i;
  747. for (i = 0; i < ARRAY_SIZE(mt9t112_cfmts); i++)
  748. if (mt9t112_cfmts[i].code == mf->code)
  749. break;
  750. if (i == ARRAY_SIZE(mt9t112_cfmts)) {
  751. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  752. mf->colorspace = V4L2_COLORSPACE_JPEG;
  753. } else {
  754. mf->colorspace = mt9t112_cfmts[i].colorspace;
  755. }
  756. mt9t112_frame_check(&mf->width, &mf->height, &left, &top);
  757. mf->field = V4L2_FIELD_NONE;
  758. return 0;
  759. }
  760. static int mt9t112_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  761. enum v4l2_mbus_pixelcode *code)
  762. {
  763. if (index >= ARRAY_SIZE(mt9t112_cfmts))
  764. return -EINVAL;
  765. *code = mt9t112_cfmts[index].code;
  766. return 0;
  767. }
  768. static int mt9t112_g_mbus_config(struct v4l2_subdev *sd,
  769. struct v4l2_mbus_config *cfg)
  770. {
  771. struct i2c_client *client = v4l2_get_subdevdata(sd);
  772. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  773. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  774. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH |
  775. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
  776. cfg->type = V4L2_MBUS_PARALLEL;
  777. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  778. return 0;
  779. }
  780. static int mt9t112_s_mbus_config(struct v4l2_subdev *sd,
  781. const struct v4l2_mbus_config *cfg)
  782. {
  783. struct i2c_client *client = v4l2_get_subdevdata(sd);
  784. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  785. struct mt9t112_priv *priv = to_mt9t112(client);
  786. if (soc_camera_apply_board_flags(icl, cfg) & V4L2_MBUS_PCLK_SAMPLE_RISING)
  787. priv->flags |= PCLK_RISING;
  788. return 0;
  789. }
  790. static struct v4l2_subdev_video_ops mt9t112_subdev_video_ops = {
  791. .s_stream = mt9t112_s_stream,
  792. .g_mbus_fmt = mt9t112_g_fmt,
  793. .s_mbus_fmt = mt9t112_s_fmt,
  794. .try_mbus_fmt = mt9t112_try_fmt,
  795. .cropcap = mt9t112_cropcap,
  796. .g_crop = mt9t112_g_crop,
  797. .s_crop = mt9t112_s_crop,
  798. .enum_mbus_fmt = mt9t112_enum_fmt,
  799. .g_mbus_config = mt9t112_g_mbus_config,
  800. .s_mbus_config = mt9t112_s_mbus_config,
  801. };
  802. /************************************************************************
  803. i2c driver
  804. ************************************************************************/
  805. static struct v4l2_subdev_ops mt9t112_subdev_ops = {
  806. .core = &mt9t112_subdev_core_ops,
  807. .video = &mt9t112_subdev_video_ops,
  808. };
  809. static int mt9t112_camera_probe(struct i2c_client *client)
  810. {
  811. struct mt9t112_priv *priv = to_mt9t112(client);
  812. const char *devname;
  813. int chipid;
  814. /*
  815. * check and show chip ID
  816. */
  817. mt9t112_reg_read(chipid, client, 0x0000);
  818. switch (chipid) {
  819. case 0x2680:
  820. devname = "mt9t111";
  821. priv->model = V4L2_IDENT_MT9T111;
  822. break;
  823. case 0x2682:
  824. devname = "mt9t112";
  825. priv->model = V4L2_IDENT_MT9T112;
  826. break;
  827. default:
  828. dev_err(&client->dev, "Product ID error %04x\n", chipid);
  829. return -ENODEV;
  830. }
  831. dev_info(&client->dev, "%s chip ID %04x\n", devname, chipid);
  832. return 0;
  833. }
  834. static int mt9t112_probe(struct i2c_client *client,
  835. const struct i2c_device_id *did)
  836. {
  837. struct mt9t112_priv *priv;
  838. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  839. struct v4l2_rect rect = {
  840. .width = VGA_WIDTH,
  841. .height = VGA_HEIGHT,
  842. .left = (MAX_WIDTH - VGA_WIDTH) / 2,
  843. .top = (MAX_HEIGHT - VGA_HEIGHT) / 2,
  844. };
  845. int ret;
  846. if (!icl || !icl->priv) {
  847. dev_err(&client->dev, "mt9t112: missing platform data!\n");
  848. return -EINVAL;
  849. }
  850. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  851. if (!priv)
  852. return -ENOMEM;
  853. priv->info = icl->priv;
  854. v4l2_i2c_subdev_init(&priv->subdev, client, &mt9t112_subdev_ops);
  855. ret = mt9t112_camera_probe(client);
  856. if (ret) {
  857. kfree(priv);
  858. return ret;
  859. }
  860. /* Cannot fail: using the default supported pixel code */
  861. mt9t112_set_params(priv, &rect, V4L2_MBUS_FMT_UYVY8_2X8);
  862. return ret;
  863. }
  864. static int mt9t112_remove(struct i2c_client *client)
  865. {
  866. struct mt9t112_priv *priv = to_mt9t112(client);
  867. kfree(priv);
  868. return 0;
  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. .remove = mt9t112_remove,
  881. .id_table = mt9t112_id,
  882. };
  883. /************************************************************************
  884. module function
  885. ************************************************************************/
  886. static int __init mt9t112_module_init(void)
  887. {
  888. return i2c_add_driver(&mt9t112_i2c_driver);
  889. }
  890. static void __exit mt9t112_module_exit(void)
  891. {
  892. i2c_del_driver(&mt9t112_i2c_driver);
  893. }
  894. module_init(mt9t112_module_init);
  895. module_exit(mt9t112_module_exit);
  896. MODULE_DESCRIPTION("SoC Camera driver for mt9t112");
  897. MODULE_AUTHOR("Kuninori Morimoto");
  898. MODULE_LICENSE("GPL v2");