mt9t11x.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  1. /*
  2. * Aptina mt9t11x CMOS Image Sensor driver
  3. *
  4. * This an adaptation of the existing soc_camera/mt9t112.c
  5. * to use the newer v4l2 subdevice framework.
  6. *
  7. * Copyright (C) 2015 Benoit Parrot <bparrot@ti.com>
  8. *
  9. * Register definitions and initial settings based on
  10. * soc_camera/mt9t112.c driver written by Kuninori Morimoto.
  11. * Copyright (C) 2009 Renesas Solutions Corp.
  12. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/gpio.h>
  20. #include <linux/i2c.h>
  21. #include <linux/kernel.h>
  22. #include <linux/media.h>
  23. #include <linux/module.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/ratelimit.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <linux/videodev2.h>
  29. #include <media/media-entity.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-device.h>
  32. #include <media/v4l2-event.h>
  33. #include <media/v4l2-image-sizes.h>
  34. #include <media/v4l2-subdev.h>
  35. #include <media/v4l2-mediabus.h>
  36. #include <media/v4l2-common.h>
  37. #include <media/v4l2-of.h>
  38. static int debug;
  39. module_param(debug, int, 0644);
  40. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  41. #define DRIVER_NAME "mt9t11x"
  42. #define MT9T111_ID 0
  43. #define MT9T112_ID 1
  44. /* you can check PLL/clock info */
  45. #define EXT_CLOCK 32000000
  46. #define MT9T11x_DEF_PIXEL_CLOCK 96000000
  47. #define MT9T11x_DEF_EXT_CLOCK 32000000
  48. #define MT9T11x_MAX_EXT_CLK 54000000
  49. #define MT9T11x_MAX_VCO_CLK 768000000
  50. #define MT9T11x_MAX_PIXEL_CLK 96000000
  51. #define MT9T11x_MAX_MIPI_CLK 768000000
  52. #define MT9T11x_MAX_MCU_CLK 96000000
  53. #define MT9T11x_MAX_SOC_CLK 54000000
  54. #define MT9T11x_MAX_SENSOR_CLK 70000000
  55. #define MT9T11x_MAX_PFD_CLK 24000000
  56. #define MT9T11x_FLAG_VFLIP BIT(2)
  57. #define MT9T11x_FLAG_HFLIP BIT(3)
  58. struct mt9t11x_pll_divider {
  59. u8 m, n;
  60. u8 p1, p2, p3, p4, p5, p6, p7;
  61. };
  62. /*
  63. * mt9t11x camera info
  64. */
  65. struct mt9t11x_camera_info {
  66. u32 flags;
  67. struct mt9t11x_pll_divider divider;
  68. unsigned int mclk;
  69. unsigned int pclk;
  70. struct v4l2_of_endpoint endpoint;
  71. };
  72. /************************************************************************
  73. * macro
  74. ***********************************************************************/
  75. /*
  76. * frame size
  77. */
  78. #define MIN_WIDTH 32
  79. #define MIN_HEIGHT 32
  80. #define MAX_WIDTH 2048
  81. #define MAX_HEIGHT 1536
  82. #define VGA_WIDTH 640
  83. #define VGA_HEIGHT 480
  84. /*
  85. * Logical address
  86. */
  87. #define _VAR(id, offset, base) (base | (id & 0x1f) << 10 | (offset & 0x3ff))
  88. #define VAR(id, offset) _VAR(id, offset, 0x0000)
  89. #define VAR8(id, offset) _VAR(id, offset, 0x8000)
  90. /************************************************************************
  91. * struct
  92. ***********************************************************************/
  93. struct mt9t11x_format {
  94. u32 code;
  95. u32 colorspace;
  96. u16 fmt;
  97. u16 order;
  98. };
  99. struct mt9t11x_framesize {
  100. u16 width;
  101. u16 height;
  102. };
  103. struct mt9t11x_resolution_param {
  104. u16 col_strt;
  105. u16 row_end;
  106. u16 col_end;
  107. u16 read_mode;
  108. u16 fine_cor;
  109. u16 fine_min;
  110. u16 fine_max;
  111. u16 base_lines;
  112. u16 min_lin_len;
  113. u16 line_len;
  114. u16 con_width;
  115. u16 con_height;
  116. u16 s_f1_50;
  117. u16 s_f2_50;
  118. u16 s_f1_60;
  119. u16 s_f2_60;
  120. u16 per_50;
  121. u16 per_50_M;
  122. u16 per_60;
  123. u16 fd_w_height;
  124. u16 tx_water;
  125. u16 max_fd_50;
  126. u16 max_fd_60;
  127. u16 targ_fd;
  128. };
  129. struct mt9t11x_priv {
  130. struct v4l2_subdev subdev;
  131. #if defined(CONFIG_MEDIA_CONTROLLER)
  132. struct media_pad pad;
  133. #endif
  134. struct v4l2_ctrl_handler handler;
  135. struct mt9t11x_camera_info *info;
  136. struct i2c_client *client;
  137. struct v4l2_rect frame;
  138. struct v4l2_clk *clk;
  139. const struct mt9t11x_format *format;
  140. int num_formats;
  141. u32 flags;
  142. /* GPIOs */
  143. struct gpio_desc *reset_gpio;
  144. struct gpio_desc *powerdown_gpio;
  145. struct gpio_desc *oscen_gpio;
  146. struct gpio_desc *bufen_gpio;
  147. struct gpio_desc *camen_gpio;
  148. /* V4L2 Ctrl handle */
  149. struct v4l2_ctrl *hflip;
  150. struct v4l2_ctrl *vflip;
  151. /* Protects the struct fields below */
  152. struct mutex lock;
  153. int streaming;
  154. int power;
  155. struct mt9t11x_resolution_param resolution;
  156. };
  157. /************************************************************************
  158. * supported frame sizes
  159. ***********************************************************************/
  160. static const struct mt9t11x_framesize mt9t11x_framesizes[] = {
  161. {
  162. .width = 2048,
  163. .height = 1536,
  164. }, {
  165. .width = 1920,
  166. .height = 1200,
  167. }, {
  168. .width = 1920,
  169. .height = 1080,
  170. }, {
  171. .width = 1600,
  172. .height = 1200,
  173. }, {
  174. .width = 1440,
  175. .height = 900,
  176. }, {
  177. .width = 1280,
  178. .height = 800,
  179. }, {
  180. .width = 1280,
  181. .height = 720,
  182. }, {
  183. .width = 800,
  184. .height = 600,
  185. }, {
  186. .width = 800,
  187. .height = 480,
  188. }, {
  189. .width = 640,
  190. .height = 480,
  191. }, {
  192. .width = 600,
  193. .height = 400,
  194. }, {
  195. .width = 352,
  196. .height = 288,
  197. }, {
  198. .width = 320,
  199. .height = 240,
  200. }, {
  201. .width = 160,
  202. .height = 120,
  203. },
  204. };
  205. /************************************************************************
  206. * supported format
  207. ***********************************************************************/
  208. static const struct mt9t11x_format mt9t11x_cfmts[] = {
  209. {
  210. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  211. .colorspace = V4L2_COLORSPACE_JPEG,
  212. .fmt = 1,
  213. .order = 0,
  214. }, {
  215. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  216. .colorspace = V4L2_COLORSPACE_JPEG,
  217. .fmt = 1,
  218. .order = 1,
  219. }, {
  220. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  221. .colorspace = V4L2_COLORSPACE_JPEG,
  222. .fmt = 1,
  223. .order = 2,
  224. }, {
  225. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  226. .colorspace = V4L2_COLORSPACE_JPEG,
  227. .fmt = 1,
  228. .order = 3,
  229. }, {
  230. .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
  231. .colorspace = V4L2_COLORSPACE_SRGB,
  232. .fmt = 8,
  233. .order = 2,
  234. }, {
  235. .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  236. .colorspace = V4L2_COLORSPACE_SRGB,
  237. .fmt = 4,
  238. .order = 2,
  239. },
  240. };
  241. /************************************************************************
  242. * general function
  243. ***********************************************************************/
  244. static inline struct mt9t11x_priv *sd_to_mt9t11x(struct v4l2_subdev *sd)
  245. {
  246. return container_of(sd, struct mt9t11x_priv, subdev);
  247. }
  248. static inline struct mt9t11x_priv *to_mt9t11x(const struct i2c_client *client)
  249. {
  250. return sd_to_mt9t11x(i2c_get_clientdata(client));
  251. }
  252. static inline struct mt9t11x_priv *ctrl_to_mt9t11x(struct v4l2_ctrl *ctrl)
  253. {
  254. return container_of(ctrl->handler, struct mt9t11x_priv, handler);
  255. }
  256. static int mt9t11x_reg_read(const struct i2c_client *client, u16 command)
  257. {
  258. struct i2c_msg msg[2];
  259. u8 buf[2];
  260. int ret;
  261. command = swab16(command);
  262. msg[0].addr = client->addr;
  263. msg[0].flags = 0;
  264. msg[0].len = 2;
  265. msg[0].buf = (u8 *)&command;
  266. msg[1].addr = client->addr;
  267. msg[1].flags = I2C_M_RD;
  268. msg[1].len = 2;
  269. msg[1].buf = buf;
  270. /*
  271. * if return value of this function is < 0,
  272. * it mean error.
  273. * else, under 16bit is valid data.
  274. */
  275. ret = i2c_transfer(client->adapter, msg, 2);
  276. if (ret < 0)
  277. return ret;
  278. memcpy(&ret, buf, 2);
  279. return swab16(ret);
  280. }
  281. static int mt9t11x_reg_write(const struct i2c_client *client,
  282. u16 command, u16 data)
  283. {
  284. struct i2c_msg msg;
  285. u8 buf[4];
  286. int ret;
  287. command = swab16(command);
  288. data = swab16(data);
  289. memcpy(buf + 0, &command, 2);
  290. memcpy(buf + 2, &data, 2);
  291. msg.addr = client->addr;
  292. msg.flags = 0;
  293. msg.len = 4;
  294. msg.buf = buf;
  295. /*
  296. * i2c_transfer return message length,
  297. * but this function should return 0 if correct case
  298. */
  299. ret = i2c_transfer(client->adapter, &msg, 1);
  300. if (ret >= 0)
  301. ret = 0;
  302. return ret;
  303. }
  304. static int mt9t11x_reg_mask_set(const struct i2c_client *client,
  305. u16 command,
  306. u16 mask,
  307. u16 set)
  308. {
  309. int val = mt9t11x_reg_read(client, command);
  310. if (val < 0)
  311. return val;
  312. val &= ~mask;
  313. val |= set & mask;
  314. return mt9t11x_reg_write(client, command, val);
  315. }
  316. /* mcu access */
  317. static int mt9t11x_mcu_read(const struct i2c_client *client, u16 command)
  318. {
  319. int ret;
  320. ret = mt9t11x_reg_write(client, 0x098E, command);
  321. if (ret < 0)
  322. return ret;
  323. return mt9t11x_reg_read(client, 0x0990);
  324. }
  325. static int mt9t11x_mcu_write(const struct i2c_client *client,
  326. u16 command, u16 data)
  327. {
  328. int ret;
  329. ret = mt9t11x_reg_write(client, 0x098E, command);
  330. if (ret < 0)
  331. return ret;
  332. return mt9t11x_reg_write(client, 0x0990, data);
  333. }
  334. static int mt9t11x_mcu_mask_set(const struct i2c_client *client,
  335. u16 command,
  336. u16 mask,
  337. u16 set)
  338. {
  339. int val = mt9t11x_mcu_read(client, command);
  340. if (val < 0)
  341. return val;
  342. val &= ~mask;
  343. val |= set & mask;
  344. return mt9t11x_mcu_write(client, command, val);
  345. }
  346. static int mt9t11x_reset(const struct i2c_client *client)
  347. {
  348. int ret;
  349. dev_dbg(&client->dev, "%s:\n", __func__);
  350. ret = mt9t11x_reg_mask_set(client, 0x001a, 0x0001, 0x0001);
  351. if (ret < 0)
  352. return ret;
  353. usleep_range(1000, 2000);
  354. ret = mt9t11x_reg_mask_set(client, 0x001a, 0x0001, 0x0000);
  355. if (ret < 0)
  356. return ret;
  357. return ret;
  358. }
  359. static int mt9t11x_streaming(struct mt9t11x_priv *priv, int on)
  360. {
  361. struct i2c_client *client = priv->client;
  362. int ret, tmp;
  363. dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
  364. on = (on) ? 1 : 0;
  365. ret = mt9t11x_reg_mask_set(client, 0x001a, 0x0200, on << 9);
  366. if (ret < 0)
  367. return ret;
  368. tmp = mt9t11x_reg_read(client, 0x001A);
  369. dev_dbg(&client->dev, "reset_and_misc_control: 0x%04x\n", tmp);
  370. return 0;
  371. }
  372. #define CLOCK_INFO(a, b) \
  373. do { \
  374. if (debug > 1) \
  375. mt9t11x_clock_info(a, b); \
  376. } while (0)
  377. static int mt9t11x_clock_info(const struct i2c_client *client, u32 ext)
  378. {
  379. int m, n, p1, p2, p3, p4, p5, p6, p7;
  380. u32 vco, clk;
  381. char *enable;
  382. ext /= 1000; /* kbyte order */
  383. n = mt9t11x_reg_read(client, 0x0012);
  384. p1 = n & 0x000f;
  385. n = n >> 4;
  386. p2 = n & 0x000f;
  387. n = n >> 4;
  388. p3 = n & 0x000f;
  389. n = mt9t11x_reg_read(client, 0x002a);
  390. p4 = n & 0x000f;
  391. n = n >> 4;
  392. p5 = n & 0x000f;
  393. n = n >> 4;
  394. p6 = n & 0x000f;
  395. n = mt9t11x_reg_read(client, 0x002c);
  396. p7 = n & 0x000f;
  397. n = mt9t11x_reg_read(client, 0x0010);
  398. m = n & 0x00ff;
  399. n = (n >> 8) & 0x003f;
  400. enable = ((ext < 6000) || (ext > 54000)) ? "X" : "";
  401. dev_dbg(&client->dev, "EXTCLK : %10u K %s\n", ext, enable);
  402. vco = 2 * m * ext / (n + 1);
  403. enable = ((vco < 384000) || (vco > 768000)) ? "X" : "";
  404. dev_dbg(&client->dev, "VCO : %10u K %s\n", vco, enable);
  405. clk = vco / (p1 + 1) / (p2 + 1);
  406. enable = (clk > 96000) ? "X" : "";
  407. dev_dbg(&client->dev, "PIXCLK : %10u K %s\n", clk, enable);
  408. clk = vco / (p3 + 1);
  409. enable = (clk > 768000) ? "X" : "";
  410. dev_dbg(&client->dev, "MIPICLK : %10u K %s\n", clk, enable);
  411. clk = vco / (p6 + 1);
  412. enable = (clk > 96000) ? "X" : "";
  413. dev_dbg(&client->dev, "MCU CLK : %10u K %s\n", clk, enable);
  414. clk = vco / (p5 + 1);
  415. enable = (clk > 54000) ? "X" : "";
  416. dev_dbg(&client->dev, "SOC CLK : %10u K %s\n", clk, enable);
  417. clk = vco / (p4 + 1);
  418. enable = (clk > 70000) ? "X" : "";
  419. dev_dbg(&client->dev, "Sensor CLK : %10u K %s\n", clk, enable);
  420. clk = vco / (p7 + 1);
  421. dev_dbg(&client->dev, "External sensor : %10u K\n", clk);
  422. clk = ext / (n + 1);
  423. enable = ((clk < 2000) || (clk > 24000)) ? "X" : "";
  424. dev_dbg(&client->dev, "PFD : %10u K %s\n", clk, enable);
  425. return 0;
  426. }
  427. static void mt9t11x_frame_check(u32 *width, u32 *height, u32 *left, u32 *top)
  428. {
  429. v4l_bound_align_image(width, MIN_WIDTH, MAX_WIDTH, 1,
  430. height, MIN_HEIGHT, MAX_HEIGHT, 1, 0);
  431. *left = 0;
  432. *top = 0;
  433. }
  434. static int mt9t11x_set_a_frame_size(const struct i2c_client *client,
  435. u16 width,
  436. u16 height)
  437. {
  438. int ret;
  439. u16 wstart = (MAX_WIDTH - width) / 2;
  440. u16 hstart = (MAX_HEIGHT - height) / 2;
  441. dev_dbg(&client->dev, "%s:\n", __func__);
  442. /* (Context A) Image Width/Height */
  443. ret = mt9t11x_mcu_write(client, VAR(26, 0), width);
  444. if (ret < 0)
  445. return ret;
  446. ret = mt9t11x_mcu_write(client, VAR(26, 2), height);
  447. if (ret < 0)
  448. return ret;
  449. /* (Context A) Output Width/Height */
  450. ret = mt9t11x_mcu_write(client, VAR(18, 43), 8 + width);
  451. if (ret < 0)
  452. return ret;
  453. ret = mt9t11x_mcu_write(client, VAR(18, 45), 8 + height);
  454. if (ret < 0)
  455. return ret;
  456. /* (Context A) Start Row/Column */
  457. ret = mt9t11x_mcu_write(client, VAR(18, 2), 4 + hstart);
  458. if (ret < 0)
  459. return ret;
  460. ret = mt9t11x_mcu_write(client, VAR(18, 4), 4 + wstart);
  461. if (ret < 0)
  462. return ret;
  463. /* (Context A) End Row/Column */
  464. ret = mt9t11x_mcu_write(client, VAR(18, 6), 11 + height + hstart);
  465. if (ret < 0)
  466. return ret;
  467. ret = mt9t11x_mcu_write(client, VAR(18, 8), 11 + width + wstart);
  468. if (ret < 0)
  469. return ret;
  470. ret = mt9t11x_mcu_write(client, VAR8(1, 0), 0x06);
  471. if (ret < 0)
  472. return ret;
  473. return ret;
  474. }
  475. static int mt9t11x_set_pll_dividers(const struct i2c_client *client,
  476. u8 m, u8 n,
  477. u8 p1, u8 p2, u8 p3,
  478. u8 p4, u8 p5, u8 p6,
  479. u8 p7)
  480. {
  481. int ret;
  482. u16 val;
  483. dev_dbg(&client->dev, "%s:\n", __func__);
  484. /* N/M */
  485. val = (n << 8) |
  486. (m << 0);
  487. ret = mt9t11x_reg_mask_set(client, 0x0010, 0x3fff, val);
  488. if (ret < 0)
  489. return ret;
  490. /* P1/P2/P3 */
  491. val = ((p3 & 0x0F) << 8) |
  492. ((p2 & 0x0F) << 4) |
  493. ((p1 & 0x0F) << 0);
  494. ret = mt9t11x_reg_mask_set(client, 0x0012, 0x0fff, val);
  495. if (ret < 0)
  496. return ret;
  497. /* P4/P5/P6 */
  498. val = (0x7 << 12) |
  499. ((p6 & 0x0F) << 8) |
  500. ((p5 & 0x0F) << 4) |
  501. ((p4 & 0x0F) << 0);
  502. ret = mt9t11x_reg_mask_set(client, 0x002A, 0x7fff, val);
  503. if (ret < 0)
  504. return ret;
  505. /* P7 */
  506. val = (0x1 << 12) |
  507. ((p7 & 0x0F) << 0);
  508. ret = mt9t11x_reg_mask_set(client, 0x002C, 0x100f, val);
  509. if (ret < 0)
  510. return ret;
  511. return ret;
  512. }
  513. static int mt9t11x_set_resolution_params(const struct i2c_client *client)
  514. {
  515. int ret = 1;
  516. struct mt9t11x_priv *priv = to_mt9t11x(client);
  517. struct mt9t11x_resolution_param *resolution = &priv->resolution;
  518. if ((priv->frame.width == 1280) && (priv->frame.height == 720)) {
  519. resolution->col_strt = 0x0004;
  520. resolution->row_end = 0x05AD;
  521. resolution->col_end = 0x050B;
  522. resolution->read_mode = 0x002C;
  523. resolution->fine_cor = 0x008C;
  524. resolution->fine_min = 0x01F1;
  525. resolution->fine_max = 0x00FF;
  526. resolution->base_lines = 0x032D;
  527. resolution->min_lin_len = 0x0378;
  528. resolution->line_len = 0x091C;
  529. resolution->con_width = 0x0508;
  530. resolution->con_height = 0x02D8;
  531. resolution->s_f1_50 = 0x23;
  532. resolution->s_f2_50 = 0x25;
  533. resolution->s_f1_60 = 0x2B;
  534. resolution->s_f2_60 = 0x2D;
  535. resolution->per_50 = 0xDC;
  536. resolution->per_50_M = 0x00;
  537. resolution->per_60 = 0xB7;
  538. resolution->fd_w_height = 0x05;
  539. resolution->tx_water = 0x0210;
  540. resolution->max_fd_50 = 0x0004;
  541. resolution->max_fd_60 = 0x0004;
  542. resolution->targ_fd = 0x0004;
  543. } else if ((priv->frame.width <= 1024) &&
  544. (priv->frame.height <= 768) &&
  545. (priv->frame.width != priv->frame.height)) {
  546. resolution->col_strt = 0x000;
  547. resolution->row_end = 0x60D;
  548. resolution->col_end = 0x80D;
  549. resolution->read_mode = 0x046C;
  550. resolution->fine_cor = 0x00CC;
  551. resolution->fine_min = 0x0381;
  552. resolution->fine_max = 0x024F;
  553. resolution->base_lines = 0x0364;
  554. resolution->min_lin_len = 0x05D0;
  555. resolution->line_len = 0x07AC;
  556. resolution->con_width = 0x0408;
  557. resolution->con_height = 0x0308;
  558. resolution->s_f1_50 = 0x23;
  559. resolution->s_f2_50 = 0x25;
  560. resolution->s_f1_60 = 0x2A;
  561. resolution->s_f2_60 = 0x2C;
  562. resolution->per_50 = 0x05;
  563. resolution->per_50_M = 0x01;
  564. resolution->per_60 = 0xD9;
  565. resolution->fd_w_height = 0x06;
  566. resolution->max_fd_50 = 0x0003;
  567. resolution->max_fd_60 = 0x0004;
  568. resolution->targ_fd = 0x0003;
  569. if ((priv->frame.width == 1024) &&
  570. (priv->frame.height == 768)) {
  571. resolution->tx_water = 0x0218;
  572. } else if ((priv->frame.width == 800) &&
  573. (priv->frame.height == 480)) {
  574. resolution->tx_water = 0x02DA;
  575. } else {
  576. /*
  577. * 640 x 480 but use it with everything else until
  578. * we figure out how to calc it
  579. */
  580. resolution->tx_water = 0x0352;
  581. }
  582. } else {
  583. ret = 0;
  584. }
  585. return ret;
  586. }
  587. static int mt9t11x_pll_setup_pll(const struct i2c_client *client)
  588. {
  589. struct mt9t11x_priv *priv = to_mt9t11x(client);
  590. int data, i, ret;
  591. /* Bypass PLL */
  592. ret = mt9t11x_reg_mask_set(client, 0x14, 1, 1);
  593. if (ret < 0)
  594. return ret;
  595. /* Power-down PLL */
  596. ret = mt9t11x_reg_mask_set(client, 0X14, 2, 0);
  597. if (ret < 0)
  598. return ret;
  599. /* PLL control: BYPASS PLL = 8517 */
  600. ret = mt9t11x_reg_write(client, 0x0014, 0x2145);
  601. if (ret < 0)
  602. return ret;
  603. /* Replace these registers when new timing parameters are generated */
  604. ret = mt9t11x_set_pll_dividers(client,
  605. priv->info->divider.m,
  606. priv->info->divider.n,
  607. priv->info->divider.p1,
  608. priv->info->divider.p2,
  609. priv->info->divider.p3,
  610. priv->info->divider.p4,
  611. priv->info->divider.p5,
  612. priv->info->divider.p6,
  613. priv->info->divider.p7);
  614. if (ret < 0)
  615. return ret;
  616. /* Reset Misc. Control = 536 */
  617. /* make sure parallel interface is not enable at first */
  618. ret = mt9t11x_reg_write(client, 0x001A, 0x018);
  619. if (ret < 0)
  620. return ret;
  621. /* PLL control: TEST_BYPASS on = 9541 */
  622. ret = mt9t11x_reg_write(client, 0x0014, 0x2545);
  623. if (ret < 0)
  624. return ret;
  625. /* PLL control: PLL_ENABLE on = 9543 */
  626. ret = mt9t11x_reg_write(client, 0x0014, 0x2547);
  627. if (ret < 0)
  628. return ret;
  629. /* PLL control: SEL_LOCK_DET on = 9287 */
  630. ret = mt9t11x_reg_write(client, 0x0014, 0x2447);
  631. if (ret < 0)
  632. return ret;
  633. /* PLL control: TEST_BYPASS off = 8263 */
  634. ret = mt9t11x_reg_write(client, 0x0014, 0x2047);
  635. if (ret < 0)
  636. return ret;
  637. /* Wait for the PLL to lock */
  638. for (i = 0; i < 1000; i++) {
  639. data = mt9t11x_reg_read(client, 0x0014);
  640. if (0x8000 & data)
  641. break;
  642. usleep_range(10000, 11000);
  643. }
  644. /* PLL control: PLL_BYPASS off = 8262 */
  645. ret = mt9t11x_reg_write(client, 0x0014, 0x2046);
  646. if (ret < 0)
  647. return ret;
  648. /* Reference clock count for 20 us = 640 */
  649. ret = mt9t11x_reg_write(client, 0x0022, 0x0280);
  650. if (ret < 0)
  651. return ret;
  652. /* Pad Slew Rate = 1911 */
  653. ret = mt9t11x_reg_write(client, 0x001E, 0x0777);
  654. if (ret < 0)
  655. return ret;
  656. /* JPEG Clock = 1024 */
  657. ret = mt9t11x_reg_write(client, 0x0016, 0x0400);
  658. return ret;
  659. }
  660. inline u32 calc_vco(u32 desired, u32 ext, u8 *_m, u8 *_n)
  661. {
  662. u32 m, n;
  663. u32 delta, actual;
  664. long bestdelta = -1;
  665. n = *_n + 1;
  666. if (n == 0)
  667. n = 1;
  668. for (; n <= 64; n++)
  669. for (m = 1; m <= 256; m++) {
  670. actual = ext * 2;
  671. actual *= m;
  672. actual /= n;
  673. delta = (desired - actual);
  674. if (delta < 0)
  675. continue;
  676. if ((delta < bestdelta) || (bestdelta == -1)) {
  677. bestdelta = delta;
  678. *_m = (u8)(m);
  679. *_n = (u8)(n - 1);
  680. }
  681. }
  682. actual = ext * 2 * (*_m);
  683. actual /= (*_n + 1);
  684. return actual;
  685. }
  686. static inline u32 calc_pixclk(u32 desired, u32 vco, u8 *_p1, u8 *_p2)
  687. {
  688. u32 p1, p2;
  689. u32 delta, actual;
  690. long bestdelta = -1;
  691. for (p1 = 1; p1 <= 16; p1++)
  692. for (p2 = 1; p2 <= 16; p2++) {
  693. actual = vco;
  694. actual /= p1;
  695. actual /= p2;
  696. delta = (desired - actual);
  697. if (delta < 0)
  698. continue;
  699. if ((delta < bestdelta) || (bestdelta == -1)) {
  700. bestdelta = delta;
  701. *_p1 = (u8)(p1 - 1);
  702. *_p2 = (u8)(p2 - 1);
  703. }
  704. }
  705. actual = vco / (*_p1 + 1);
  706. actual /= (*_p2 + 1);
  707. return actual;
  708. }
  709. static inline u32 calc_div(u32 desired, u32 src, u8 *_div)
  710. {
  711. u32 div;
  712. if (src > desired) {
  713. div = src / desired;
  714. if ((src % desired) > 0)
  715. div++;
  716. } else {
  717. div = 1;
  718. }
  719. *_div = (u8)(div - 1);
  720. return 0;
  721. }
  722. static unsigned int mt9t11x_pll_calc_params(struct mt9t11x_priv *priv)
  723. {
  724. struct i2c_client *client = priv->client;
  725. struct mt9t11x_camera_info *info = priv->info;
  726. u32 vco;
  727. dev_dbg(&client->dev, "%s:\n", __func__);
  728. calc_div(MT9T11x_MAX_PFD_CLK, info->mclk, &info->divider.n);
  729. vco = calc_vco(MT9T11x_MAX_VCO_CLK, info->mclk, &info->divider.m,
  730. &info->divider.n);
  731. calc_pixclk(info->pclk, vco, &info->divider.p1, &info->divider.p2);
  732. calc_div(MT9T11x_MAX_MIPI_CLK, vco, &info->divider.p3);
  733. calc_div(MT9T11x_MAX_MCU_CLK, vco, &info->divider.p6);
  734. calc_div(MT9T11x_MAX_SOC_CLK, vco, &info->divider.p5);
  735. calc_div(MT9T11x_MAX_SENSOR_CLK, vco, &info->divider.p4);
  736. return 0;
  737. }
  738. static int mt9t11x_sysctl_startup(const struct i2c_client *client)
  739. {
  740. int ret = 0;
  741. /* reset */
  742. mt9t11x_reset(client);
  743. /* Setup PLL */
  744. mt9t11x_pll_setup_pll(client);
  745. return ret;
  746. }
  747. static int mt9t11x_high_speed_overrides(const struct i2c_client *client)
  748. {
  749. int ret;
  750. /*
  751. * Use this section to apply settings that are specific to this
  752. * revision of SOC or for any other specialized settings
  753. * clear the "Output Buffer Enable Adaptive Clock" bit to enable
  754. * the SYSCTL slew rate settings, change this in the variables
  755. * and register
  756. */
  757. /* PRI_A_CONFIG_JPEG_OB_TX_CONTROL_VAR */
  758. ret = mt9t11x_mcu_write(client, VAR(26, 160), 0x082E);
  759. if (ret < 0)
  760. return ret;
  761. /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
  762. ret = mt9t11x_mcu_write(client, VAR(27, 160), 0x082E);
  763. if (ret < 0)
  764. return ret;
  765. /* SEC_A_CONFIG_JPEG_OB_TX_CONTROL_VAR */
  766. ret = mt9t11x_mcu_write(client, VAR(28, 160), 0x082E);
  767. if (ret < 0)
  768. return ret;
  769. /* SEC_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
  770. ret = mt9t11x_mcu_write(client, VAR(29, 160), 0x082E);
  771. if (ret < 0)
  772. return ret;
  773. ret = mt9t11x_reg_mask_set(client, 0x3C52, 0x0040, 0);
  774. if (ret < 0)
  775. return ret;
  776. /* Set correct values for Context B FIFO control */
  777. /* CAM1_CTX_B_RX_FIFO_TRIGGER_MARK */
  778. ret = mt9t11x_mcu_write(client, VAR(18, 142), 32);
  779. if (ret < 0)
  780. return ret;
  781. /* PRI_B_CONFIG_IO_OB_MANUAL_FLAG */
  782. ret = mt9t11x_mcu_write(client, VAR(27, 172), 0);
  783. return ret;
  784. }
  785. static int mt9t11x_go(const struct i2c_client *client)
  786. {
  787. int data, i, ret;
  788. /* release MCU from standby */
  789. ret = mt9t11x_reg_mask_set(client, 0x0018, 0x0001, 0);
  790. if (ret < 0)
  791. return ret;
  792. /* wait for K26A to come out of standby */
  793. for (i = 0; i < 100; i++) {
  794. data = mt9t11x_reg_read(client, 0x0018);
  795. if (!(0x4000 & data))
  796. break;
  797. usleep_range(10000, 11000);
  798. }
  799. return ret;
  800. }
  801. static int mt9t11x_continue(const struct i2c_client *client)
  802. {
  803. int data, i, ret;
  804. /* clear powerup stop bit */
  805. ret = mt9t11x_reg_mask_set(client, 0x0018, 0x0004, 0);
  806. if (ret < 0)
  807. return ret;
  808. /* wait for sequencer to enter preview state */
  809. for (i = 0; i < 100; i++) {
  810. data = mt9t11x_mcu_read(client, VAR8(1, 1));
  811. if (data == 3)
  812. break;
  813. usleep_range(10000, 11000);
  814. }
  815. return ret;
  816. }
  817. static int mt9t11x_mcu_powerup_stop_enable(const struct i2c_client *client)
  818. {
  819. int ret;
  820. /* set powerup stop bit */
  821. ret = mt9t11x_reg_mask_set(client, 0x0018, 0x0004, 0x0004);
  822. return ret;
  823. }
  824. static int mt9t11x_custom_setup(const struct i2c_client *client)
  825. {
  826. struct mt9t11x_priv *priv = to_mt9t11x(client);
  827. struct mt9t11x_resolution_param *resolution = &priv->resolution;
  828. int ret;
  829. /* I2C Master Clock Divider */
  830. ret = mt9t11x_mcu_write(client, VAR(24, 6), 0x0100);
  831. if (ret < 0)
  832. return ret;
  833. /* Output Width (A) */
  834. ret = mt9t11x_mcu_write(client, VAR(26, 0), priv->frame.width);
  835. if (ret < 0)
  836. return ret;
  837. /* Output Height (A) */
  838. ret = mt9t11x_mcu_write(client, VAR(26, 2), priv->frame.height);
  839. if (ret < 0)
  840. return ret;
  841. /* JPEG (A) */
  842. ret = mt9t11x_mcu_write(client, VAR8(26, 142), 0x00);
  843. if (ret < 0)
  844. return ret;
  845. /* Adaptive Output Clock (A) */
  846. ret = mt9t11x_mcu_mask_set(client, VAR(26, 160), 0x0040, 0x0000);
  847. if (ret < 0)
  848. return ret;
  849. /* Row Start (A) */
  850. ret = mt9t11x_mcu_write(client, VAR(18, 2), 0x000);
  851. if (ret < 0)
  852. return ret;
  853. /* Column Start (A) */
  854. ret = mt9t11x_mcu_write(client, VAR(18, 4), resolution->col_strt);
  855. if (ret < 0)
  856. return ret;
  857. /* Row End (A) */
  858. ret = mt9t11x_mcu_write(client, VAR(18, 6), resolution->row_end);
  859. if (ret < 0)
  860. return ret;
  861. /* Column End (A) */
  862. ret = mt9t11x_mcu_write(client, VAR(18, 8), resolution->col_end);
  863. if (ret < 0)
  864. return ret;
  865. /* Row Speed (A) */
  866. ret = mt9t11x_mcu_write(client, VAR(18, 10), 0x0111);
  867. if (ret < 0)
  868. return ret;
  869. /* Read Mode (A) */
  870. ret = mt9t11x_mcu_write(client, VAR(18, 12), resolution->read_mode);
  871. if (ret < 0)
  872. return ret;
  873. /* Fine Correction (A) */
  874. ret = mt9t11x_mcu_write(client, VAR(18, 15), resolution->fine_cor);
  875. if (ret < 0)
  876. return ret;
  877. /* Fine IT Min (A) */
  878. ret = mt9t11x_mcu_write(client, VAR(18, 17), resolution->fine_min);
  879. if (ret < 0)
  880. return ret;
  881. /* Fine IT Max Margin (A) */
  882. ret = mt9t11x_mcu_write(client, VAR(18, 19), resolution->fine_max);
  883. if (ret < 0)
  884. return ret;
  885. /* Base Frame Lines (A) */
  886. ret = mt9t11x_mcu_write(client, VAR(18, 29), resolution->base_lines);
  887. if (ret < 0)
  888. return ret;
  889. /* Min Line Length (A) */
  890. ret = mt9t11x_mcu_write(client, VAR(18, 31), resolution->min_lin_len);
  891. if (ret < 0)
  892. return ret;
  893. /* Line Length (A) */
  894. ret = mt9t11x_mcu_write(client, VAR(18, 37), resolution->line_len);
  895. if (ret < 0)
  896. return ret;
  897. /* Contex Width (A) */
  898. ret = mt9t11x_mcu_write(client, VAR(18, 43), resolution->con_width);
  899. if (ret < 0)
  900. return ret;
  901. /* Context Height (A) */
  902. ret = mt9t11x_mcu_write(client, VAR(18, 45), resolution->con_height);
  903. if (ret < 0)
  904. return ret;
  905. /* Output Width (B) */
  906. ret = mt9t11x_mcu_write(client, VAR(27, 0), 0x0800);
  907. if (ret < 0)
  908. return ret;
  909. /* Output Height (B) */
  910. ret = mt9t11x_mcu_write(client, VAR(27, 2), 0x0600);
  911. if (ret < 0)
  912. return ret;
  913. /* JPEG (B) */
  914. ret = mt9t11x_mcu_write(client, VAR8(27, 142), 0x01);
  915. if (ret < 0)
  916. return ret;
  917. /* Adaptive Output Clock (B) */
  918. ret = mt9t11x_mcu_mask_set(client, VAR(27, 160), 0x0040, 0x0000);
  919. if (ret < 0)
  920. return ret;
  921. /* Row Start (B) */
  922. ret = mt9t11x_mcu_write(client, VAR(18, 74), 0x004);
  923. if (ret < 0)
  924. return ret;
  925. /* Column Start (B) */
  926. ret = mt9t11x_mcu_write(client, VAR(18, 76), 0x004);
  927. if (ret < 0)
  928. return ret;
  929. /* Row End (B) */
  930. ret = mt9t11x_mcu_write(client, VAR(18, 78), 0x60B);
  931. if (ret < 0)
  932. return ret;
  933. /* Column End (B) */
  934. ret = mt9t11x_mcu_write(client, VAR(18, 80), 0x80B);
  935. if (ret < 0)
  936. return ret;
  937. /* Row Speed (B) */
  938. ret = mt9t11x_mcu_write(client, VAR(18, 82), 0x0111);
  939. if (ret < 0)
  940. return ret;
  941. /* Read Mode (B) */
  942. ret = mt9t11x_mcu_write(client, VAR(18, 84), 0x0024);
  943. if (ret < 0)
  944. return ret;
  945. /* Fine Correction (B) */
  946. ret = mt9t11x_mcu_write(client, VAR(18, 87), 0x008C);
  947. if (ret < 0)
  948. return ret;
  949. /* Fine IT Min (B) */
  950. ret = mt9t11x_mcu_write(client, VAR(18, 89), 0x01F1);
  951. if (ret < 0)
  952. return ret;
  953. /* Fine IT Max Margin (B) */
  954. ret = mt9t11x_mcu_write(client, VAR(18, 91), 0x00FF);
  955. if (ret < 0)
  956. return ret;
  957. /* Base Frame Lines (B) */
  958. ret = mt9t11x_mcu_write(client, VAR(18, 101), 0x06AE);
  959. if (ret < 0)
  960. return ret;
  961. /* Min Line Length (B) */
  962. ret = mt9t11x_mcu_write(client, VAR(18, 103), 0x0378);
  963. if (ret < 0)
  964. return ret;
  965. /* Line Length (B) */
  966. ret = mt9t11x_mcu_write(client, VAR(18, 109), 0x0A3A);
  967. if (ret < 0)
  968. return ret;
  969. /* Contex Width (B) */
  970. ret = mt9t11x_mcu_write(client, VAR(18, 115), 0x0808);
  971. if (ret < 0)
  972. return ret;
  973. /* Context Height (B) */
  974. ret = mt9t11x_mcu_write(client, VAR(18, 117), 0x0608);
  975. if (ret < 0)
  976. return ret;
  977. /* search_f1_50 */
  978. ret = mt9t11x_mcu_write(client, VAR8(18, 165), resolution->s_f1_50);
  979. if (ret < 0)
  980. return ret;
  981. /* search_f2_50 */
  982. ret = mt9t11x_mcu_write(client, VAR8(18, 166), resolution->s_f2_50);
  983. if (ret < 0)
  984. return ret;
  985. /* search_f1_60 */
  986. ret = mt9t11x_mcu_write(client, VAR8(18, 167), resolution->s_f1_60);
  987. if (ret < 0)
  988. return ret;
  989. /* search_f2_60 */
  990. ret = mt9t11x_mcu_write(client, VAR8(18, 168), resolution->s_f2_60);
  991. if (ret < 0)
  992. return ret;
  993. /* period_50Hz (A) */
  994. ret = mt9t11x_mcu_write(client, VAR8(18, 68), resolution->per_50);
  995. if (ret < 0)
  996. return ret;
  997. /* period_50Hz (A MSB) */
  998. ret = mt9t11x_mcu_write(client, VAR8(18, 303), resolution->per_50_M);
  999. if (ret < 0)
  1000. return ret;
  1001. /* period_60Hz (A) */
  1002. ret = mt9t11x_mcu_write(client, VAR8(18, 69), resolution->per_60);
  1003. if (ret < 0)
  1004. return ret;
  1005. /* period_60Hz (A MSB) */
  1006. ret = mt9t11x_mcu_write(client, VAR8(18, 301), 0x00);
  1007. if (ret < 0)
  1008. return ret;
  1009. /* period_50Hz (B) */
  1010. ret = mt9t11x_mcu_write(client, VAR8(18, 140), 0xD2);
  1011. if (ret < 0)
  1012. return ret;
  1013. /* period_50Hz (B) MSB */
  1014. ret = mt9t11x_mcu_write(client, VAR8(18, 304), 0x00);
  1015. if (ret < 0)
  1016. return ret;
  1017. /* period_60Hz (B) */
  1018. ret = mt9t11x_mcu_write(client, VAR8(18, 141), 0xAF);
  1019. if (ret < 0)
  1020. return ret;
  1021. /* period_60Hz (B) MSB */
  1022. ret = mt9t11x_mcu_write(client, VAR8(18, 302), 0x00);
  1023. if (ret < 0)
  1024. return ret;
  1025. /* FD Window Height */
  1026. ret = mt9t11x_mcu_write(client, VAR8(14, 37), resolution->fd_w_height);
  1027. if (ret < 0)
  1028. return ret;
  1029. /* Stat_min */
  1030. ret = mt9t11x_mcu_write(client, VAR8(8, 9), 0x02);
  1031. if (ret < 0)
  1032. return ret;
  1033. /* Stat_max */
  1034. ret = mt9t11x_mcu_write(client, VAR8(8, 10), 0x03);
  1035. if (ret < 0)
  1036. return ret;
  1037. /* Min_amplitude */
  1038. ret = mt9t11x_mcu_write(client, VAR8(8, 12), 0x0A);
  1039. if (ret < 0)
  1040. return ret;
  1041. /* RX FIFO Watermark (A) */
  1042. ret = mt9t11x_mcu_write(client, VAR(18, 70), 0x0080);
  1043. if (ret < 0)
  1044. return ret;
  1045. /* TX FIFO Watermark (A) */
  1046. ret = mt9t11x_mcu_write(client, VAR(26, 170), resolution->tx_water);
  1047. if (ret < 0)
  1048. return ret;
  1049. /* Max FD Zone 50 Hz */
  1050. ret = mt9t11x_mcu_write(client, VAR(26, 21), resolution->max_fd_50);
  1051. if (ret < 0)
  1052. return ret;
  1053. /* Max FD Zone 60 Hz */
  1054. ret = mt9t11x_mcu_write(client, VAR(26, 23), resolution->max_fd_60);
  1055. if (ret < 0)
  1056. return ret;
  1057. /* AE Target FD Zone */
  1058. ret = mt9t11x_mcu_write(client, VAR(26, 45), resolution->targ_fd);
  1059. if (ret < 0)
  1060. return ret;
  1061. /* RX FIFO Watermark (B) */
  1062. ret = mt9t11x_mcu_write(client, VAR(18, 142), 0x0080);
  1063. if (ret < 0)
  1064. return ret;
  1065. /* TX FIFO Watermark (B) */
  1066. ret = mt9t11x_mcu_write(client, VAR(27, 170), 0x01D0);
  1067. if (ret < 0)
  1068. return ret;
  1069. /* Refresh Sequencer Mode */
  1070. ret = mt9t11x_mcu_write(client, VAR8(1, 0), 0x06);
  1071. if (ret < 0)
  1072. return ret;
  1073. /* Refresh Sequencer */
  1074. ret = mt9t11x_mcu_write(client, VAR8(1, 0), 0x05);
  1075. if (ret < 0)
  1076. return ret;
  1077. #ifdef TEST_PATTERN
  1078. ret = mt9t11x_mcu_write(client, VAR(24, 3), 0x100);
  1079. if (ret < 0)
  1080. return ret;
  1081. ret = mt9t11x_mcu_write(client, VAR(24, 37), 0x0B);
  1082. #endif
  1083. return ret;
  1084. }
  1085. static int mt9t11x_optimal_power_consumption(const struct i2c_client *client)
  1086. {
  1087. int ret;
  1088. /* Analog setting B */
  1089. ret = mt9t11x_reg_write(client, 0x3084, 0x2409);
  1090. if (ret < 0)
  1091. return ret;
  1092. ret = mt9t11x_reg_write(client, 0x3092, 0x0A49);
  1093. if (ret < 0)
  1094. return ret;
  1095. ret = mt9t11x_reg_write(client, 0x3094, 0x4949);
  1096. if (ret < 0)
  1097. return ret;
  1098. ret = mt9t11x_reg_write(client, 0x3096, 0x4950);
  1099. return ret;
  1100. }
  1101. static int mt9t11x_blooming_row_pattern(const struct i2c_client *client)
  1102. {
  1103. int ret;
  1104. /* Improve high light image quality */
  1105. /* [CAM1_CTX_A_COARSE_ITMIN] */
  1106. ret = mt9t11x_mcu_write(client, VAR(18, 21), 0x0004);
  1107. if (ret < 0)
  1108. return ret;
  1109. /* [CAM1_CTX_B_COARSE_ITMIN] */
  1110. ret = mt9t11x_mcu_write(client, VAR(18, 93), 0x0004);
  1111. return ret;
  1112. }
  1113. static int mt9t11x_set_orientation(const struct i2c_client *client,
  1114. u32 mask, u32 flip)
  1115. {
  1116. int ret;
  1117. flip &= mask;
  1118. dev_dbg(&client->dev, "%s:\n", __func__);
  1119. /* [CAM1_CTX_A_READ_MODE] */
  1120. ret = mt9t11x_mcu_mask_set(client, VAR(18, 12), mask, flip);
  1121. if (ret < 0)
  1122. return ret;
  1123. /* [CAM1_CTX_A_PIXEL_ORDER] */
  1124. ret = mt9t11x_mcu_mask_set(client, VAR8(18, 14), mask, flip);
  1125. if (ret < 0)
  1126. return ret;
  1127. /* [CAM1_CTX_B_READ_MODE] */
  1128. ret = mt9t11x_mcu_mask_set(client, VAR(18, 84), mask, flip);
  1129. if (ret < 0)
  1130. return ret;
  1131. /* [CAM1_CTX_B_PIXEL_ORDER] */
  1132. ret = mt9t11x_mcu_mask_set(client, VAR8(18, 86), mask, flip);
  1133. if (ret < 0)
  1134. return ret;
  1135. /* [SEQ_CMD] */
  1136. ret = mt9t11x_mcu_write(client, VAR8(1, 0), 0x06);
  1137. return ret;
  1138. }
  1139. static int mt9t11x_init_camera_optimized(const struct i2c_client *client)
  1140. {
  1141. int ret;
  1142. dev_dbg(&client->dev, "%s:\n", __func__);
  1143. ret = mt9t11x_sysctl_startup(client);
  1144. if (ret < 0)
  1145. return ret;
  1146. ret = mt9t11x_mcu_powerup_stop_enable(client);
  1147. if (ret < 0)
  1148. return ret;
  1149. ret = mt9t11x_go(client);
  1150. if (ret < 0)
  1151. return ret;
  1152. ret = mt9t11x_custom_setup(client);
  1153. if (ret < 0)
  1154. return ret;
  1155. ret = mt9t11x_high_speed_overrides(client);
  1156. if (ret < 0)
  1157. return ret;
  1158. ret = mt9t11x_optimal_power_consumption(client);
  1159. if (ret < 0)
  1160. return ret;
  1161. ret = mt9t11x_blooming_row_pattern(client);
  1162. if (ret < 0)
  1163. return ret;
  1164. ret = mt9t11x_continue(client);
  1165. return ret;
  1166. }
  1167. static int mt9t11x_init_setting(const struct i2c_client *client)
  1168. {
  1169. struct mt9t11x_priv *priv = to_mt9t11x(client);
  1170. int ret;
  1171. dev_dbg(&client->dev, "%s:\n", __func__);
  1172. /* Adaptive Output Clock (A) */
  1173. ret = mt9t11x_mcu_mask_set(client, VAR(26, 160), 0x0040, 0x0000);
  1174. if (ret < 0)
  1175. return ret;
  1176. /* Read Mode (A) */
  1177. ret = mt9t11x_mcu_write(client, VAR(18, 12), 0x0024);
  1178. if (ret < 0)
  1179. return ret;
  1180. /* Fine Correction (A) */
  1181. ret = mt9t11x_mcu_write(client, VAR(18, 15), 0x00CC);
  1182. if (ret < 0)
  1183. return ret;
  1184. /* Fine IT Min (A) */
  1185. ret = mt9t11x_mcu_write(client, VAR(18, 17), 0x01f1);
  1186. if (ret < 0)
  1187. return ret;
  1188. /* Fine IT Max Margin (A) */
  1189. ret = mt9t11x_mcu_write(client, VAR(18, 19), 0x00fF);
  1190. if (ret < 0)
  1191. return ret;
  1192. /* Base Frame Lines (A) */
  1193. ret = mt9t11x_mcu_write(client, VAR(18, 29), 0x032D);
  1194. if (ret < 0)
  1195. return ret;
  1196. /* Min Line Length (A) */
  1197. ret = mt9t11x_mcu_write(client, VAR(18, 31), 0x073a);
  1198. if (ret < 0)
  1199. return ret;
  1200. /* Line Length (A) */
  1201. ret = mt9t11x_mcu_write(client, VAR(18, 37), 0x07d0);
  1202. if (ret < 0)
  1203. return ret;
  1204. /* Adaptive Output Clock (B) */
  1205. ret = mt9t11x_mcu_mask_set(client, VAR(27, 160), 0x0040, 0x0000);
  1206. if (ret < 0)
  1207. return ret;
  1208. /* Row Start (B) */
  1209. ret = mt9t11x_mcu_write(client, VAR(18, 74), 0x004);
  1210. if (ret < 0)
  1211. return ret;
  1212. /* Column Start (B) */
  1213. ret = mt9t11x_mcu_write(client, VAR(18, 76), 0x004);
  1214. if (ret < 0)
  1215. return ret;
  1216. /* Row End (B) */
  1217. ret = mt9t11x_mcu_write(client, VAR(18, 78), 0x60B);
  1218. if (ret < 0)
  1219. return ret;
  1220. /* Column End (B) */
  1221. ret = mt9t11x_mcu_write(client, VAR(18, 80), 0x80B);
  1222. if (ret < 0)
  1223. return ret;
  1224. /* Fine Correction (B) */
  1225. ret = mt9t11x_mcu_write(client, VAR(18, 87), 0x008C);
  1226. if (ret < 0)
  1227. return ret;
  1228. /* Fine IT Min (B) */
  1229. ret = mt9t11x_mcu_write(client, VAR(18, 89), 0x01F1);
  1230. if (ret < 0)
  1231. return ret;
  1232. /* Fine IT Max Margin (B) */
  1233. ret = mt9t11x_mcu_write(client, VAR(18, 91), 0x00FF);
  1234. if (ret < 0)
  1235. return ret;
  1236. /* Base Frame Lines (B) */
  1237. ret = mt9t11x_mcu_write(client, VAR(18, 101), 0x0668);
  1238. if (ret < 0)
  1239. return ret;
  1240. /* Min Line Length (B) */
  1241. ret = mt9t11x_mcu_write(client, VAR(18, 103), 0x0AF0);
  1242. if (ret < 0)
  1243. return ret;
  1244. /* Line Length (B) */
  1245. ret = mt9t11x_mcu_write(client, VAR(18, 109), 0x0AF0);
  1246. if (ret < 0)
  1247. return ret;
  1248. /*
  1249. * Flicker Dectection registers
  1250. * This section should be replaced whenever new Timing file is
  1251. * generated.
  1252. * All the following registers need to be replaced
  1253. * Following registers are generated from Register Wizard but user can
  1254. * modify them. For detail see auto flicker detection tuning
  1255. */
  1256. /* FD_FDPERIOD_SELECT */
  1257. ret = mt9t11x_mcu_write(client, VAR8(8, 5), 0x01);
  1258. if (ret < 0)
  1259. return ret;
  1260. /* PRI_B_CONFIG_FD_ALGO_RUN */
  1261. ret = mt9t11x_mcu_write(client, VAR(27, 17), 0x0003);
  1262. if (ret < 0)
  1263. return ret;
  1264. /* PRI_A_CONFIG_FD_ALGO_RUN */
  1265. ret = mt9t11x_mcu_write(client, VAR(26, 17), 0x0003);
  1266. if (ret < 0)
  1267. return ret;
  1268. /*
  1269. * AFD range detection tuning registers
  1270. */
  1271. /* search_f1_50 */
  1272. ret = mt9t11x_mcu_write(client, VAR8(18, 165), 0x25);
  1273. if (ret < 0)
  1274. return ret;
  1275. /* search_f2_50 */
  1276. ret = mt9t11x_mcu_write(client, VAR8(18, 166), 0x28);
  1277. if (ret < 0)
  1278. return ret;
  1279. /* search_f1_60 */
  1280. ret = mt9t11x_mcu_write(client, VAR8(18, 167), 0x2C);
  1281. if (ret < 0)
  1282. return ret;
  1283. /* search_f2_60 */
  1284. ret = mt9t11x_mcu_write(client, VAR8(18, 168), 0x2F);
  1285. if (ret < 0)
  1286. return ret;
  1287. /* period_50Hz (A) */
  1288. ret = mt9t11x_mcu_write(client, VAR8(18, 68), 0xBA);
  1289. if (ret < 0)
  1290. return ret;
  1291. /* period_50Hz (A MSB) */
  1292. ret = mt9t11x_mcu_write(client, VAR8(18, 303), 0x00);
  1293. if (ret < 0)
  1294. return ret;
  1295. /* period_60Hz (A) */
  1296. ret = mt9t11x_mcu_write(client, VAR8(18, 69), 0x9B);
  1297. if (ret < 0)
  1298. return ret;
  1299. /* period_60Hz (A MSB) */
  1300. ret = mt9t11x_mcu_write(client, VAR8(18, 301), 0x00);
  1301. if (ret < 0)
  1302. return ret;
  1303. /* period_50Hz (B) */
  1304. ret = mt9t11x_mcu_write(client, VAR8(18, 140), 0x82);
  1305. if (ret < 0)
  1306. return ret;
  1307. /* period_50Hz (B) MSB */
  1308. ret = mt9t11x_mcu_write(client, VAR8(18, 304), 0x00);
  1309. if (ret < 0)
  1310. return ret;
  1311. /* period_60Hz (B) */
  1312. ret = mt9t11x_mcu_write(client, VAR8(18, 141), 0x6D);
  1313. if (ret < 0)
  1314. return ret;
  1315. /* period_60Hz (B) MSB */
  1316. ret = mt9t11x_mcu_write(client, VAR8(18, 302), 0x00);
  1317. if (ret < 0)
  1318. return ret;
  1319. /* FD Mode */
  1320. ret = mt9t11x_mcu_write(client, VAR8(8, 2), 0x10);
  1321. if (ret < 0)
  1322. return ret;
  1323. /* Stat_min */
  1324. ret = mt9t11x_mcu_write(client, VAR8(8, 9), 0x02);
  1325. if (ret < 0)
  1326. return ret;
  1327. /* Stat_max */
  1328. ret = mt9t11x_mcu_write(client, VAR8(8, 10), 0x03);
  1329. if (ret < 0)
  1330. return ret;
  1331. /* Min_amplitude */
  1332. ret = mt9t11x_mcu_write(client, VAR8(8, 12), 0x0A);
  1333. if (ret < 0)
  1334. return ret;
  1335. /* RX FIFO Watermark (A) */
  1336. ret = mt9t11x_mcu_write(client, VAR(18, 70), 0x0014);
  1337. if (ret < 0)
  1338. return ret;
  1339. /* RX FIFO Watermark (B) */
  1340. ret = mt9t11x_mcu_write(client, VAR(18, 142), 0x0014);
  1341. if (ret < 0)
  1342. return ret;
  1343. ret = mt9t11x_set_a_frame_size(client,
  1344. priv->frame.width,
  1345. priv->frame.height);
  1346. return ret;
  1347. }
  1348. static int mt9t11x_init_camera(const struct i2c_client *client)
  1349. {
  1350. int ret;
  1351. dev_dbg(&client->dev, "%s:\n", __func__);
  1352. ret = mt9t11x_sysctl_startup(client);
  1353. if (ret < 0)
  1354. return ret;
  1355. ret = mt9t11x_mcu_powerup_stop_enable(client);
  1356. if (ret < 0)
  1357. return ret;
  1358. ret = mt9t11x_go(client);
  1359. if (ret < 0)
  1360. return ret;
  1361. ret = mt9t11x_init_setting(client);
  1362. if (ret < 0)
  1363. return ret;
  1364. ret = mt9t11x_high_speed_overrides(client);
  1365. if (ret < 0)
  1366. return ret;
  1367. ret = mt9t11x_optimal_power_consumption(client);
  1368. if (ret < 0)
  1369. return ret;
  1370. ret = mt9t11x_blooming_row_pattern(client);
  1371. if (ret < 0)
  1372. return ret;
  1373. ret = mt9t11x_continue(client);
  1374. if (ret < 0)
  1375. return ret;
  1376. return ret;
  1377. }
  1378. /************************************************************************
  1379. * v4l2_subdev_core_ops
  1380. ***********************************************************************/
  1381. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1382. static int mt9t11x_g_register(struct v4l2_subdev *sd,
  1383. struct v4l2_dbg_register *reg)
  1384. {
  1385. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1386. int ret;
  1387. reg->size = 2;
  1388. ret = mt9t11x_reg_read(client, reg->reg);
  1389. reg->val = (__u64)ret;
  1390. return 0;
  1391. }
  1392. static int mt9t11x_s_register(struct v4l2_subdev *sd,
  1393. const struct v4l2_dbg_register *reg)
  1394. {
  1395. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1396. int ret;
  1397. ret = mt9t11x_reg_write(client, reg->reg, reg->val);
  1398. return ret;
  1399. }
  1400. #endif
  1401. static void __mt9t11x_set_power(struct mt9t11x_priv *priv, int on)
  1402. {
  1403. struct i2c_client *client = priv->client;
  1404. dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
  1405. on = (on) ? 1 : 0;
  1406. if (priv->power == on)
  1407. return;
  1408. if (on) {
  1409. if (priv->powerdown_gpio)
  1410. gpiod_set_value_cansleep(priv->powerdown_gpio, 0);
  1411. if (priv->reset_gpio)
  1412. gpiod_set_value_cansleep(priv->reset_gpio, 0);
  1413. usleep_range(25000, 26000);
  1414. } else {
  1415. if (priv->powerdown_gpio)
  1416. gpiod_set_value_cansleep(priv->powerdown_gpio, 1);
  1417. if (priv->reset_gpio)
  1418. gpiod_set_value_cansleep(priv->reset_gpio, 1);
  1419. }
  1420. priv->power = on;
  1421. }
  1422. static int mt9t11x_s_power(struct v4l2_subdev *sd, int on)
  1423. {
  1424. struct mt9t11x_priv *priv = sd_to_mt9t11x(sd);
  1425. struct i2c_client *client = priv->client;
  1426. dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
  1427. mutex_lock(&priv->lock);
  1428. __mt9t11x_set_power(priv, on);
  1429. mutex_unlock(&priv->lock);
  1430. return 0;
  1431. }
  1432. static struct v4l2_subdev_core_ops mt9t11x_subdev_core_ops = {
  1433. .log_status = v4l2_ctrl_subdev_log_status,
  1434. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  1435. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1436. .s_power = mt9t11x_s_power,
  1437. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1438. .g_register = mt9t11x_g_register,
  1439. .s_register = mt9t11x_s_register,
  1440. #endif
  1441. };
  1442. /*
  1443. * V4L2 controls
  1444. */
  1445. static int mt9t11x_s_ctrl(struct v4l2_ctrl *ctrl)
  1446. {
  1447. struct mt9t11x_priv *priv = ctrl_to_mt9t11x(ctrl);
  1448. struct i2c_client *client = priv->client;
  1449. int ret = -EINVAL;
  1450. dev_dbg(&client->dev, "%s: ctrl_id:0x%x (%s), value: %d\n",
  1451. __func__, ctrl->id, ctrl->name, ctrl->val);
  1452. mutex_lock(&priv->lock);
  1453. /*
  1454. * If the device is not powered up now, postpone applying control's
  1455. * value to the hardware, until it is ready to accept commands.
  1456. */
  1457. if (priv->power == 0) {
  1458. mutex_unlock(&priv->lock);
  1459. return 0;
  1460. }
  1461. switch (ctrl->id) {
  1462. case V4L2_CID_VFLIP:
  1463. mt9t11x_set_orientation(client, 0x2, (ctrl->val) ? 2 : 0);
  1464. ret = 0;
  1465. break;
  1466. case V4L2_CID_HFLIP:
  1467. mt9t11x_set_orientation(client, 0x1, (ctrl->val) ? 1 : 0);
  1468. ret = 0;
  1469. break;
  1470. }
  1471. mutex_unlock(&priv->lock);
  1472. return ret;
  1473. }
  1474. static const struct v4l2_ctrl_ops mt9t11x_ctrl_ops = {
  1475. .s_ctrl = mt9t11x_s_ctrl,
  1476. };
  1477. static int mt9t11x_initialize_controls(struct mt9t11x_priv *priv)
  1478. {
  1479. const struct v4l2_ctrl_ops *ops = &mt9t11x_ctrl_ops;
  1480. struct i2c_client *client = priv->client;
  1481. struct v4l2_subdev *sd = &priv->subdev;
  1482. struct v4l2_ctrl_handler *hdl = &priv->handler;
  1483. int ret;
  1484. dev_dbg(&client->dev, "%s:\n", __func__);
  1485. ret = v4l2_ctrl_handler_init(hdl, 2);
  1486. if (ret < 0)
  1487. return ret;
  1488. priv->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  1489. priv->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  1490. sd->ctrl_handler = hdl;
  1491. if (priv->handler.error)
  1492. return priv->handler.error;
  1493. return 0;
  1494. }
  1495. /************************************************************************
  1496. * v4l2_subdev_video_ops
  1497. ***********************************************************************/
  1498. static int mt9t11x_set_params(struct mt9t11x_priv *priv,
  1499. const struct v4l2_rect *rect,
  1500. u32 code)
  1501. {
  1502. int i;
  1503. /*
  1504. * get color format
  1505. */
  1506. for (i = 0; i < priv->num_formats; i++)
  1507. if (mt9t11x_cfmts[i].code == code)
  1508. break;
  1509. if (i == priv->num_formats)
  1510. return -EINVAL;
  1511. priv->frame = *rect;
  1512. /*
  1513. * frame size check
  1514. */
  1515. mt9t11x_frame_check(&priv->frame.width, &priv->frame.height,
  1516. &priv->frame.left, &priv->frame.top);
  1517. priv->format = mt9t11x_cfmts + i;
  1518. return 0;
  1519. }
  1520. static int mt9t11x_s_stream(struct v4l2_subdev *sd, int enable)
  1521. {
  1522. struct mt9t11x_priv *priv = sd_to_mt9t11x(sd);
  1523. struct i2c_client *client = priv->client;
  1524. int ret = 0;
  1525. int optimize = 0;
  1526. struct v4l2_rect rect = {
  1527. .width = priv->frame.width,
  1528. .height = priv->frame.height,
  1529. .left = priv->frame.left,
  1530. .top = priv->frame.top,
  1531. };
  1532. u16 param;
  1533. dev_dbg(&client->dev, "%s: enable: %d\n", __func__, enable);
  1534. mutex_lock(&priv->lock);
  1535. if (priv->streaming == enable) {
  1536. mutex_unlock(&priv->lock);
  1537. return 0;
  1538. }
  1539. if (!enable) {
  1540. /* Stop Streaming Sequence */
  1541. mt9t11x_streaming(priv, false);
  1542. __mt9t11x_set_power(priv, 0);
  1543. priv->streaming = enable;
  1544. mutex_unlock(&priv->lock);
  1545. return 0;
  1546. }
  1547. mt9t11x_set_params(priv, &rect, priv->format->code);
  1548. __mt9t11x_set_power(priv, 1);
  1549. /* fill the structure with new resolution parameters */
  1550. optimize = mt9t11x_set_resolution_params(client);
  1551. if (optimize) {
  1552. ret = mt9t11x_init_camera_optimized(client);
  1553. if (ret < 0)
  1554. return ret;
  1555. } else {
  1556. ret = mt9t11x_init_camera(client);
  1557. if (ret < 0)
  1558. return ret;
  1559. }
  1560. /*
  1561. * By default data is sampled on falling edge of pixclk.
  1562. * Change the default to be rising edge. i.e. Invert PCLK
  1563. */
  1564. param = (priv->info->flags & V4L2_MBUS_PCLK_SAMPLE_RISING) ?
  1565. 0x0001 : 0x0000;
  1566. ret = mt9t11x_reg_write(client, 0x3C20, param);
  1567. if (ret < 0)
  1568. return ret;
  1569. usleep_range(5000, 6000);
  1570. ret = mt9t11x_mcu_write(client, VAR(26, 7), priv->format->fmt);
  1571. if (ret < 0)
  1572. return ret;
  1573. ret = mt9t11x_mcu_write(client, VAR(26, 9), priv->format->order);
  1574. if (ret < 0)
  1575. return ret;
  1576. ret = mt9t11x_mcu_write(client, VAR8(1, 0), 0x06);
  1577. if (ret < 0)
  1578. return ret;
  1579. if (priv->flags & MT9T11x_FLAG_VFLIP)
  1580. v4l2_ctrl_s_ctrl(priv->vflip, 1);
  1581. /* Make sure H/W is consistent with current control settings */
  1582. ret = mt9t11x_set_orientation(client, 0x3,
  1583. (v4l2_ctrl_g_ctrl(priv->vflip) << 1 |
  1584. v4l2_ctrl_g_ctrl(priv->hflip)));
  1585. if (ret < 0)
  1586. return ret;
  1587. dev_dbg(&client->dev, "format : %04x\n", priv->format->code);
  1588. dev_dbg(&client->dev, "size : %d x %d\n",
  1589. priv->frame.width,
  1590. priv->frame.height);
  1591. priv->streaming = enable;
  1592. mt9t11x_streaming(priv, true);
  1593. CLOCK_INFO(client, EXT_CLOCK);
  1594. mutex_unlock(&priv->lock);
  1595. return ret;
  1596. }
  1597. static void mt9t11x_get_default_format(struct mt9t11x_priv *priv,
  1598. struct v4l2_mbus_framefmt *mf)
  1599. {
  1600. struct v4l2_rect rect = {
  1601. .width = VGA_WIDTH,
  1602. .height = VGA_HEIGHT,
  1603. .left = (MAX_WIDTH - VGA_WIDTH) / 2,
  1604. .top = (MAX_HEIGHT - VGA_HEIGHT) / 2,
  1605. };
  1606. mt9t11x_set_params(priv, &rect, MEDIA_BUS_FMT_UYVY8_2X8);
  1607. /* Need fixing */
  1608. mf->width = rect.width;
  1609. mf->height = rect.height;
  1610. mf->colorspace = mt9t11x_cfmts[0].colorspace;
  1611. mf->code = mt9t11x_cfmts[0].code;
  1612. mf->field = V4L2_FIELD_NONE;
  1613. }
  1614. static int mt9t11x_get_fmt(struct v4l2_subdev *sd,
  1615. struct v4l2_subdev_pad_config *cfg,
  1616. struct v4l2_subdev_format *fmt)
  1617. {
  1618. struct mt9t11x_priv *priv = sd_to_mt9t11x(sd);
  1619. struct v4l2_mbus_framefmt *mf;
  1620. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1621. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  1622. mutex_lock(&priv->lock);
  1623. fmt->format = *mf;
  1624. mutex_unlock(&priv->lock);
  1625. return 0;
  1626. }
  1627. mutex_lock(&priv->lock);
  1628. mf = &fmt->format;
  1629. mf->width = priv->frame.width;
  1630. mf->height = priv->frame.height;
  1631. mf->colorspace = priv->format->colorspace;
  1632. mf->code = priv->format->code;
  1633. mf->field = V4L2_FIELD_NONE;
  1634. mutex_unlock(&priv->lock);
  1635. return 0;
  1636. }
  1637. static void __mt9t11x_try_frame_size(struct v4l2_mbus_framefmt *mf)
  1638. {
  1639. const struct mt9t11x_framesize *fsize = &mt9t11x_framesizes[0];
  1640. const struct mt9t11x_framesize *match = NULL;
  1641. int i = ARRAY_SIZE(mt9t11x_framesizes);
  1642. unsigned int min_err = UINT_MAX;
  1643. while (i--) {
  1644. int err = abs(fsize->width - mf->width)
  1645. + abs(fsize->height - mf->height);
  1646. if (err < min_err) {
  1647. min_err = err;
  1648. match = fsize;
  1649. }
  1650. fsize++;
  1651. }
  1652. if (!match)
  1653. match = &mt9t11x_framesizes[0];
  1654. mf->width = match->width;
  1655. mf->height = match->height;
  1656. }
  1657. static int mt9t11x_set_fmt(struct v4l2_subdev *sd,
  1658. struct v4l2_subdev_pad_config *cfg,
  1659. struct v4l2_subdev_format *fmt)
  1660. {
  1661. struct mt9t11x_priv *priv = sd_to_mt9t11x(sd);
  1662. unsigned int index = priv->num_formats;
  1663. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1664. int ret = 0;
  1665. struct v4l2_rect rect;
  1666. __mt9t11x_try_frame_size(mf);
  1667. rect.width = mf->width;
  1668. rect.height = mf->height;
  1669. rect.left = priv->frame.left;
  1670. rect.top = priv->frame.top;
  1671. while (--index >= 0)
  1672. if (mt9t11x_cfmts[index].code == mf->code)
  1673. break;
  1674. if (index < 0)
  1675. return -EINVAL;
  1676. mf->colorspace = mt9t11x_cfmts[index].colorspace;
  1677. mf->code = mt9t11x_cfmts[index].code;
  1678. mf->field = V4L2_FIELD_NONE;
  1679. mutex_lock(&priv->lock);
  1680. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1681. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1682. *mf = fmt->format;
  1683. } else {
  1684. ret = mt9t11x_set_params(priv, &rect, mf->code);
  1685. }
  1686. mutex_unlock(&priv->lock);
  1687. return ret;
  1688. }
  1689. static int mt9t11x_enum_mbus_code(struct v4l2_subdev *sd,
  1690. struct v4l2_subdev_pad_config *cfg,
  1691. struct v4l2_subdev_mbus_code_enum *code)
  1692. {
  1693. struct mt9t11x_priv *priv = sd_to_mt9t11x(sd);
  1694. if (code->index >= priv->num_formats)
  1695. return -EINVAL;
  1696. code->code = mt9t11x_cfmts[code->index].code;
  1697. return 0;
  1698. }
  1699. static int mt9t11x_enum_frame_sizes(struct v4l2_subdev *sd,
  1700. struct v4l2_subdev_pad_config *cfg,
  1701. struct v4l2_subdev_frame_size_enum *fse)
  1702. {
  1703. struct mt9t11x_priv *priv = sd_to_mt9t11x(sd);
  1704. int i = priv->num_formats;
  1705. if (fse->index >= ARRAY_SIZE(mt9t11x_framesizes))
  1706. return -EINVAL;
  1707. while (--i)
  1708. if (mt9t11x_cfmts[i].code == fse->code)
  1709. break;
  1710. fse->code = mt9t11x_cfmts[i].code;
  1711. fse->min_width = mt9t11x_framesizes[fse->index].width;
  1712. fse->max_width = fse->min_width;
  1713. fse->max_height = mt9t11x_framesizes[fse->index].height;
  1714. fse->min_height = fse->max_height;
  1715. return 0;
  1716. }
  1717. static const struct v4l2_subdev_video_ops mt9t11x_subdev_video_ops = {
  1718. .s_stream = mt9t11x_s_stream,
  1719. };
  1720. static const struct v4l2_subdev_pad_ops mt9t11x_subdev_pad_ops = {
  1721. .enum_mbus_code = mt9t11x_enum_mbus_code,
  1722. .enum_frame_size = mt9t11x_enum_frame_sizes,
  1723. .get_fmt = mt9t11x_get_fmt,
  1724. .set_fmt = mt9t11x_set_fmt,
  1725. };
  1726. /************************************************************************
  1727. * i2c driver
  1728. ***********************************************************************/
  1729. static struct v4l2_subdev_ops mt9t11x_subdev_ops = {
  1730. .core = &mt9t11x_subdev_core_ops,
  1731. .video = &mt9t11x_subdev_video_ops,
  1732. .pad = &mt9t11x_subdev_pad_ops,
  1733. };
  1734. /*
  1735. * V4L2 subdev internal operations
  1736. */
  1737. static int mt9t11x_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1738. {
  1739. struct mt9t11x_priv *priv = sd_to_mt9t11x(sd);
  1740. struct i2c_client *client = priv->client;
  1741. struct v4l2_mbus_framefmt *mf;
  1742. dev_dbg(&client->dev, "%s:\n", __func__);
  1743. mf = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  1744. mt9t11x_get_default_format(priv, mf);
  1745. return 0;
  1746. }
  1747. static const struct v4l2_subdev_internal_ops mt9t11x_subdev_internal_ops = {
  1748. .open = mt9t11x_open,
  1749. };
  1750. static int mt9t11x_camera_probe(struct i2c_client *client)
  1751. {
  1752. struct mt9t11x_priv *priv = to_mt9t11x(client);
  1753. const char *devname;
  1754. int chipid;
  1755. int custom_rev;
  1756. int ret = 0;
  1757. __mt9t11x_set_power(priv, 1);
  1758. /*
  1759. * check and show chip ID
  1760. */
  1761. chipid = mt9t11x_reg_read(client, 0x0000);
  1762. switch (chipid) {
  1763. case 0x2680:
  1764. devname = "mt9t111";
  1765. /*
  1766. * Looks like only uyvy is supported
  1767. * so limiting available formats.
  1768. */
  1769. priv->num_formats = 1;
  1770. break;
  1771. case 0x2682:
  1772. devname = "mt9t112";
  1773. priv->num_formats = ARRAY_SIZE(mt9t11x_cfmts);
  1774. break;
  1775. default:
  1776. dev_err(&client->dev, "Product ID error %04x\n", chipid);
  1777. ret = -ENODEV;
  1778. goto done;
  1779. }
  1780. custom_rev = mt9t11x_reg_read(client, 0x31FE);
  1781. dev_info(&client->dev, "%s chip ID %04x rev %04x\n",
  1782. devname, chipid, custom_rev);
  1783. done:
  1784. __mt9t11x_set_power(priv, 0);
  1785. return ret;
  1786. }
  1787. static struct mt9t11x_camera_info *
  1788. mt9t11x_get_pdata(struct i2c_client *client)
  1789. {
  1790. struct mt9t11x_camera_info *pdata;
  1791. struct device_node *endpoint;
  1792. struct v4l2_of_endpoint *v4l2_endpoint;
  1793. dev_dbg(&client->dev, "_get_pdata invoked\n");
  1794. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  1795. return client->dev.platform_data;
  1796. dev_dbg(&client->dev, "_get_pdata: DT Node found\n");
  1797. endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  1798. if (!endpoint)
  1799. return NULL;
  1800. dev_dbg(&client->dev, "_get_pdata: endpoint found\n");
  1801. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  1802. if (!pdata)
  1803. goto done;
  1804. v4l2_endpoint = &pdata->endpoint;
  1805. v4l2_of_parse_endpoint(endpoint, v4l2_endpoint);
  1806. if (of_property_read_u32(endpoint, "input-clock-freq", &pdata->mclk)) {
  1807. dev_err(&client->dev, "input-clock-freq property not found\n");
  1808. goto err_out;
  1809. } else if (pdata->mclk > MT9T11x_MAX_EXT_CLK) {
  1810. dev_err(&client->dev, "input-clock-freq property exceed max value\n");
  1811. goto err_out;
  1812. }
  1813. dev_info(&client->dev, "input-clock-freq: %d\n", pdata->mclk);
  1814. if (of_property_read_u32(endpoint, "pixel-clock-freq", &pdata->pclk)) {
  1815. dev_err(&client->dev, "pixel-clock-freq property not found\n");
  1816. goto err_out;
  1817. } else if (pdata->pclk > MT9T11x_MAX_PIXEL_CLK) {
  1818. dev_err(&client->dev, "pixel-clock-freq property exceed max value\n");
  1819. goto err_out;
  1820. }
  1821. dev_info(&client->dev, "pixel-clock-freq: %d\n", pdata->pclk);
  1822. /* Just copy them for now */
  1823. if (pdata->endpoint.bus_type == V4L2_MBUS_PARALLEL)
  1824. pdata->flags = pdata->endpoint.bus.parallel.flags;
  1825. else
  1826. pdata->flags = 0;
  1827. done:
  1828. of_node_put(endpoint);
  1829. return pdata;
  1830. err_out:
  1831. of_node_put(endpoint);
  1832. kfree(pdata);
  1833. return NULL;
  1834. }
  1835. static int mt9t11x_probe(struct i2c_client *client,
  1836. const struct i2c_device_id *did)
  1837. {
  1838. struct mt9t11x_priv *priv;
  1839. struct v4l2_subdev *sd;
  1840. struct mt9t11x_camera_info *info = mt9t11x_get_pdata(client);
  1841. struct v4l2_mbus_framefmt mf;
  1842. int ret;
  1843. struct gpio_desc *gpio;
  1844. if (!info) {
  1845. dev_err(&client->dev, "mt9t11x: missing platform data!\n");
  1846. return -EINVAL;
  1847. }
  1848. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  1849. if (!priv)
  1850. return -ENOMEM;
  1851. priv->info = info;
  1852. mutex_init(&priv->lock);
  1853. priv->client = client;
  1854. gpio = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_LOW);
  1855. if (IS_ERR(gpio)) {
  1856. if (PTR_ERR(gpio) != -ENOENT)
  1857. return PTR_ERR(gpio);
  1858. gpio = NULL;
  1859. }
  1860. priv->reset_gpio = gpio;
  1861. gpio = devm_gpiod_get(&client->dev, "powerdown", GPIOD_OUT_LOW);
  1862. if (IS_ERR(gpio)) {
  1863. if (PTR_ERR(gpio) != -ENOENT)
  1864. return PTR_ERR(gpio);
  1865. gpio = NULL;
  1866. }
  1867. priv->powerdown_gpio = gpio;
  1868. gpio = devm_gpiod_get(&client->dev, "oscen", GPIOD_OUT_LOW);
  1869. if (IS_ERR(gpio)) {
  1870. if (PTR_ERR(gpio) != -ENOENT)
  1871. return PTR_ERR(gpio);
  1872. gpio = NULL;
  1873. }
  1874. priv->oscen_gpio = gpio;
  1875. gpio = devm_gpiod_get(&client->dev, "bufen", GPIOD_OUT_LOW);
  1876. if (IS_ERR(gpio)) {
  1877. if (PTR_ERR(gpio) != -ENOENT)
  1878. return PTR_ERR(gpio);
  1879. gpio = NULL;
  1880. }
  1881. priv->bufen_gpio = gpio;
  1882. gpio = devm_gpiod_get(&client->dev, "camen", GPIOD_OUT_LOW);
  1883. if (IS_ERR(gpio)) {
  1884. if (PTR_ERR(gpio) != -ENOENT)
  1885. return PTR_ERR(gpio);
  1886. gpio = NULL;
  1887. }
  1888. priv->camen_gpio = gpio;
  1889. /* Do these here for now */
  1890. if (priv->bufen_gpio)
  1891. gpiod_set_value_cansleep(priv->bufen_gpio, 1);
  1892. if (priv->oscen_gpio)
  1893. gpiod_set_value_cansleep(priv->oscen_gpio, 1);
  1894. if (priv->camen_gpio)
  1895. gpiod_set_value_cansleep(priv->camen_gpio, 1);
  1896. sd = &priv->subdev;
  1897. v4l2_i2c_subdev_init(sd, client, &mt9t11x_subdev_ops);
  1898. strlcpy(sd->name, DRIVER_NAME, sizeof(sd->name));
  1899. sd->internal_ops = &mt9t11x_subdev_internal_ops;
  1900. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  1901. V4L2_SUBDEV_FL_HAS_EVENTS;
  1902. #if defined(CONFIG_MEDIA_CONTROLLER)
  1903. priv->pad.flags = MEDIA_PAD_FL_SOURCE;
  1904. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1905. ret = media_entity_pads_init(&sd->entity, 1, &priv->pad);
  1906. if (ret < 0)
  1907. return ret;
  1908. #endif
  1909. ret = mt9t11x_initialize_controls(priv);
  1910. if (ret < 0)
  1911. goto err_me;
  1912. ret = mt9t11x_camera_probe(client);
  1913. if (ret < 0)
  1914. goto err_ctrls;
  1915. mt9t11x_get_default_format(priv, &mf);
  1916. /* Calculate the PLL register value needed */
  1917. mt9t11x_pll_calc_params(priv);
  1918. ret = v4l2_async_register_subdev(sd);
  1919. if (ret)
  1920. goto err_ctrls;
  1921. dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
  1922. return 0;
  1923. err_ctrls:
  1924. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1925. err_me:
  1926. #if defined(CONFIG_MEDIA_CONTROLLER)
  1927. media_entity_cleanup(&sd->entity);
  1928. #endif
  1929. return ret;
  1930. }
  1931. static int mt9t11x_remove(struct i2c_client *client)
  1932. {
  1933. struct mt9t11x_priv *priv = to_mt9t11x(client);
  1934. v4l2_async_unregister_subdev(&priv->subdev);
  1935. v4l2_device_unregister_subdev(&priv->subdev);
  1936. #if defined(CONFIG_MEDIA_CONTROLLER)
  1937. media_entity_cleanup(&priv->subdev.entity);
  1938. #endif
  1939. v4l2_ctrl_handler_free(priv->subdev.ctrl_handler);
  1940. kfree(priv->info);
  1941. kfree(priv);
  1942. return 0;
  1943. }
  1944. static const struct i2c_device_id mt9t11x_id[] = {
  1945. { "mt9t111", 0 },
  1946. { "mt9t112", 0 },
  1947. { /* sentinel */ }
  1948. };
  1949. MODULE_DEVICE_TABLE(i2c, mt9t11x_id);
  1950. #if IS_ENABLED(CONFIG_OF)
  1951. static const struct of_device_id mt9t11x_of_match[] = {
  1952. { .compatible = "aptina,mt9t111", .data = (void *)MT9T111_ID},
  1953. { .compatible = "aptina,mt9t112", .data = (void *)MT9T112_ID},
  1954. { /* sentinel */ },
  1955. };
  1956. MODULE_DEVICE_TABLE(of, mt9t11x_of_match);
  1957. #endif
  1958. static struct i2c_driver mt9t11x_i2c_driver = {
  1959. .driver = {
  1960. .owner = THIS_MODULE,
  1961. .name = DRIVER_NAME,
  1962. .of_match_table = of_match_ptr(mt9t11x_of_match),
  1963. },
  1964. .probe = mt9t11x_probe,
  1965. .remove = mt9t11x_remove,
  1966. .id_table = mt9t11x_id,
  1967. };
  1968. module_i2c_driver(mt9t11x_i2c_driver);
  1969. MODULE_AUTHOR("Benoit Parrot <bparrot@ti.com>");
  1970. MODULE_DESCRIPTION("MT9T11x CMOS Image Sensor driver");
  1971. MODULE_LICENSE("GPL v2");