mt9t112.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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_UYVY8_2X8,
  98. .colorspace = V4L2_COLORSPACE_JPEG,
  99. .fmt = 1,
  100. .order = 0,
  101. }, {
  102. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  103. .colorspace = V4L2_COLORSPACE_JPEG,
  104. .fmt = 1,
  105. .order = 1,
  106. }, {
  107. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  108. .colorspace = V4L2_COLORSPACE_JPEG,
  109. .fmt = 1,
  110. .order = 2,
  111. }, {
  112. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  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_UYVY8_2X8);
  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_UYVY8_2X8);
  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, unsigned int index,
  780. enum v4l2_mbus_pixelcode *code)
  781. {
  782. if (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. kfree(priv);
  861. }
  862. return ret;
  863. }
  864. static int mt9t112_remove(struct i2c_client *client)
  865. {
  866. struct mt9t112_priv *priv = to_mt9t112(client);
  867. struct soc_camera_device *icd = client->dev.platform_data;
  868. icd->ops = NULL;
  869. kfree(priv);
  870. return 0;
  871. }
  872. static const struct i2c_device_id mt9t112_id[] = {
  873. { "mt9t112", 0 },
  874. { }
  875. };
  876. MODULE_DEVICE_TABLE(i2c, mt9t112_id);
  877. static struct i2c_driver mt9t112_i2c_driver = {
  878. .driver = {
  879. .name = "mt9t112",
  880. },
  881. .probe = mt9t112_probe,
  882. .remove = mt9t112_remove,
  883. .id_table = mt9t112_id,
  884. };
  885. /************************************************************************
  886. module function
  887. ************************************************************************/
  888. static int __init mt9t112_module_init(void)
  889. {
  890. return i2c_add_driver(&mt9t112_i2c_driver);
  891. }
  892. static void __exit mt9t112_module_exit(void)
  893. {
  894. i2c_del_driver(&mt9t112_i2c_driver);
  895. }
  896. module_init(mt9t112_module_init);
  897. module_exit(mt9t112_module_exit);
  898. MODULE_DESCRIPTION("SoC Camera driver for mt9t112");
  899. MODULE_AUTHOR("Kuninori Morimoto");
  900. MODULE_LICENSE("GPL v2");