mt9t112.c 28 KB

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