mt9t112.c 28 KB

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