mt9m111.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Driver for MT9M111/MT9M112/MT9M131 CMOS Image Sensor from Micron/Aptina
  3. *
  4. * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/videodev2.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/log2.h>
  14. #include <linux/gpio.h>
  15. #include <linux/delay.h>
  16. #include <linux/v4l2-mediabus.h>
  17. #include <linux/module.h>
  18. #include <media/soc_camera.h>
  19. #include <media/v4l2-common.h>
  20. #include <media/v4l2-ctrls.h>
  21. #include <media/v4l2-chip-ident.h>
  22. /*
  23. * MT9M111, MT9M112 and MT9M131:
  24. * i2c address is 0x48 or 0x5d (depending on SADDR pin)
  25. * The platform has to define i2c_board_info and call i2c_register_board_info()
  26. */
  27. /*
  28. * Sensor core register addresses (0x000..0x0ff)
  29. */
  30. #define MT9M111_CHIP_VERSION 0x000
  31. #define MT9M111_ROW_START 0x001
  32. #define MT9M111_COLUMN_START 0x002
  33. #define MT9M111_WINDOW_HEIGHT 0x003
  34. #define MT9M111_WINDOW_WIDTH 0x004
  35. #define MT9M111_HORIZONTAL_BLANKING_B 0x005
  36. #define MT9M111_VERTICAL_BLANKING_B 0x006
  37. #define MT9M111_HORIZONTAL_BLANKING_A 0x007
  38. #define MT9M111_VERTICAL_BLANKING_A 0x008
  39. #define MT9M111_SHUTTER_WIDTH 0x009
  40. #define MT9M111_ROW_SPEED 0x00a
  41. #define MT9M111_EXTRA_DELAY 0x00b
  42. #define MT9M111_SHUTTER_DELAY 0x00c
  43. #define MT9M111_RESET 0x00d
  44. #define MT9M111_READ_MODE_B 0x020
  45. #define MT9M111_READ_MODE_A 0x021
  46. #define MT9M111_FLASH_CONTROL 0x023
  47. #define MT9M111_GREEN1_GAIN 0x02b
  48. #define MT9M111_BLUE_GAIN 0x02c
  49. #define MT9M111_RED_GAIN 0x02d
  50. #define MT9M111_GREEN2_GAIN 0x02e
  51. #define MT9M111_GLOBAL_GAIN 0x02f
  52. #define MT9M111_CONTEXT_CONTROL 0x0c8
  53. #define MT9M111_PAGE_MAP 0x0f0
  54. #define MT9M111_BYTE_WISE_ADDR 0x0f1
  55. #define MT9M111_RESET_SYNC_CHANGES (1 << 15)
  56. #define MT9M111_RESET_RESTART_BAD_FRAME (1 << 9)
  57. #define MT9M111_RESET_SHOW_BAD_FRAMES (1 << 8)
  58. #define MT9M111_RESET_RESET_SOC (1 << 5)
  59. #define MT9M111_RESET_OUTPUT_DISABLE (1 << 4)
  60. #define MT9M111_RESET_CHIP_ENABLE (1 << 3)
  61. #define MT9M111_RESET_ANALOG_STANDBY (1 << 2)
  62. #define MT9M111_RESET_RESTART_FRAME (1 << 1)
  63. #define MT9M111_RESET_RESET_MODE (1 << 0)
  64. #define MT9M111_RM_FULL_POWER_RD (0 << 10)
  65. #define MT9M111_RM_LOW_POWER_RD (1 << 10)
  66. #define MT9M111_RM_COL_SKIP_4X (1 << 5)
  67. #define MT9M111_RM_ROW_SKIP_4X (1 << 4)
  68. #define MT9M111_RM_COL_SKIP_2X (1 << 3)
  69. #define MT9M111_RM_ROW_SKIP_2X (1 << 2)
  70. #define MT9M111_RMB_MIRROR_COLS (1 << 1)
  71. #define MT9M111_RMB_MIRROR_ROWS (1 << 0)
  72. #define MT9M111_CTXT_CTRL_RESTART (1 << 15)
  73. #define MT9M111_CTXT_CTRL_DEFECTCOR_B (1 << 12)
  74. #define MT9M111_CTXT_CTRL_RESIZE_B (1 << 10)
  75. #define MT9M111_CTXT_CTRL_CTRL2_B (1 << 9)
  76. #define MT9M111_CTXT_CTRL_GAMMA_B (1 << 8)
  77. #define MT9M111_CTXT_CTRL_XENON_EN (1 << 7)
  78. #define MT9M111_CTXT_CTRL_READ_MODE_B (1 << 3)
  79. #define MT9M111_CTXT_CTRL_LED_FLASH_EN (1 << 2)
  80. #define MT9M111_CTXT_CTRL_VBLANK_SEL_B (1 << 1)
  81. #define MT9M111_CTXT_CTRL_HBLANK_SEL_B (1 << 0)
  82. /*
  83. * Colorpipe register addresses (0x100..0x1ff)
  84. */
  85. #define MT9M111_OPER_MODE_CTRL 0x106
  86. #define MT9M111_OUTPUT_FORMAT_CTRL 0x108
  87. #define MT9M111_REDUCER_XZOOM_B 0x1a0
  88. #define MT9M111_REDUCER_XSIZE_B 0x1a1
  89. #define MT9M111_REDUCER_YZOOM_B 0x1a3
  90. #define MT9M111_REDUCER_YSIZE_B 0x1a4
  91. #define MT9M111_REDUCER_XZOOM_A 0x1a6
  92. #define MT9M111_REDUCER_XSIZE_A 0x1a7
  93. #define MT9M111_REDUCER_YZOOM_A 0x1a9
  94. #define MT9M111_REDUCER_YSIZE_A 0x1aa
  95. #define MT9M111_OUTPUT_FORMAT_CTRL2_A 0x13a
  96. #define MT9M111_OUTPUT_FORMAT_CTRL2_B 0x19b
  97. #define MT9M111_OPMODE_AUTOEXPO_EN (1 << 14)
  98. #define MT9M111_OPMODE_AUTOWHITEBAL_EN (1 << 1)
  99. #define MT9M111_OUTFMT_FLIP_BAYER_COL (1 << 9)
  100. #define MT9M111_OUTFMT_FLIP_BAYER_ROW (1 << 8)
  101. #define MT9M111_OUTFMT_PROCESSED_BAYER (1 << 14)
  102. #define MT9M111_OUTFMT_BYPASS_IFP (1 << 10)
  103. #define MT9M111_OUTFMT_INV_PIX_CLOCK (1 << 9)
  104. #define MT9M111_OUTFMT_RGB (1 << 8)
  105. #define MT9M111_OUTFMT_RGB565 (0 << 6)
  106. #define MT9M111_OUTFMT_RGB555 (1 << 6)
  107. #define MT9M111_OUTFMT_RGB444x (2 << 6)
  108. #define MT9M111_OUTFMT_RGBx444 (3 << 6)
  109. #define MT9M111_OUTFMT_TST_RAMP_OFF (0 << 4)
  110. #define MT9M111_OUTFMT_TST_RAMP_COL (1 << 4)
  111. #define MT9M111_OUTFMT_TST_RAMP_ROW (2 << 4)
  112. #define MT9M111_OUTFMT_TST_RAMP_FRAME (3 << 4)
  113. #define MT9M111_OUTFMT_SHIFT_3_UP (1 << 3)
  114. #define MT9M111_OUTFMT_AVG_CHROMA (1 << 2)
  115. #define MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN (1 << 1)
  116. #define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr_RGB_R_B (1 << 0)
  117. /*
  118. * Camera control register addresses (0x200..0x2ff not implemented)
  119. */
  120. #define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg)
  121. #define reg_write(reg, val) mt9m111_reg_write(client, MT9M111_##reg, (val))
  122. #define reg_set(reg, val) mt9m111_reg_set(client, MT9M111_##reg, (val))
  123. #define reg_clear(reg, val) mt9m111_reg_clear(client, MT9M111_##reg, (val))
  124. #define reg_mask(reg, val, mask) mt9m111_reg_mask(client, MT9M111_##reg, \
  125. (val), (mask))
  126. #define MT9M111_MIN_DARK_ROWS 8
  127. #define MT9M111_MIN_DARK_COLS 26
  128. #define MT9M111_MAX_HEIGHT 1024
  129. #define MT9M111_MAX_WIDTH 1280
  130. /* MT9M111 has only one fixed colorspace per pixelcode */
  131. struct mt9m111_datafmt {
  132. enum v4l2_mbus_pixelcode code;
  133. enum v4l2_colorspace colorspace;
  134. };
  135. /* Find a data format by a pixel code in an array */
  136. static const struct mt9m111_datafmt *mt9m111_find_datafmt(
  137. enum v4l2_mbus_pixelcode code, const struct mt9m111_datafmt *fmt,
  138. int n)
  139. {
  140. int i;
  141. for (i = 0; i < n; i++)
  142. if (fmt[i].code == code)
  143. return fmt + i;
  144. return NULL;
  145. }
  146. static const struct mt9m111_datafmt mt9m111_colour_fmts[] = {
  147. {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
  148. {V4L2_MBUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG},
  149. {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
  150. {V4L2_MBUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG},
  151. {V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
  152. {V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE, V4L2_COLORSPACE_SRGB},
  153. {V4L2_MBUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB},
  154. {V4L2_MBUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB},
  155. {V4L2_MBUS_FMT_BGR565_2X8_LE, V4L2_COLORSPACE_SRGB},
  156. {V4L2_MBUS_FMT_BGR565_2X8_BE, V4L2_COLORSPACE_SRGB},
  157. {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
  158. {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
  159. };
  160. enum mt9m111_context {
  161. HIGHPOWER = 0,
  162. LOWPOWER,
  163. };
  164. struct mt9m111 {
  165. struct v4l2_subdev subdev;
  166. struct v4l2_ctrl_handler hdl;
  167. struct v4l2_ctrl *gain;
  168. int model; /* V4L2_IDENT_MT9M111 or V4L2_IDENT_MT9M112 code
  169. * from v4l2-chip-ident.h */
  170. enum mt9m111_context context;
  171. struct v4l2_rect rect;
  172. struct mutex power_lock; /* lock to protect power_count */
  173. int power_count;
  174. const struct mt9m111_datafmt *fmt;
  175. int lastpage; /* PageMap cache value */
  176. unsigned char datawidth;
  177. unsigned int powered:1;
  178. };
  179. static struct mt9m111 *to_mt9m111(const struct i2c_client *client)
  180. {
  181. return container_of(i2c_get_clientdata(client), struct mt9m111, subdev);
  182. }
  183. static int reg_page_map_set(struct i2c_client *client, const u16 reg)
  184. {
  185. int ret;
  186. u16 page;
  187. struct mt9m111 *mt9m111 = to_mt9m111(client);
  188. page = (reg >> 8);
  189. if (page == mt9m111->lastpage)
  190. return 0;
  191. if (page > 2)
  192. return -EINVAL;
  193. ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page));
  194. if (!ret)
  195. mt9m111->lastpage = page;
  196. return ret;
  197. }
  198. static int mt9m111_reg_read(struct i2c_client *client, const u16 reg)
  199. {
  200. int ret;
  201. ret = reg_page_map_set(client, reg);
  202. if (!ret)
  203. ret = swab16(i2c_smbus_read_word_data(client, reg & 0xff));
  204. dev_dbg(&client->dev, "read reg.%03x -> %04x\n", reg, ret);
  205. return ret;
  206. }
  207. static int mt9m111_reg_write(struct i2c_client *client, const u16 reg,
  208. const u16 data)
  209. {
  210. int ret;
  211. ret = reg_page_map_set(client, reg);
  212. if (!ret)
  213. ret = i2c_smbus_write_word_data(client, reg & 0xff,
  214. swab16(data));
  215. dev_dbg(&client->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret);
  216. return ret;
  217. }
  218. static int mt9m111_reg_set(struct i2c_client *client, const u16 reg,
  219. const u16 data)
  220. {
  221. int ret;
  222. ret = mt9m111_reg_read(client, reg);
  223. if (ret >= 0)
  224. ret = mt9m111_reg_write(client, reg, ret | data);
  225. return ret;
  226. }
  227. static int mt9m111_reg_clear(struct i2c_client *client, const u16 reg,
  228. const u16 data)
  229. {
  230. int ret;
  231. ret = mt9m111_reg_read(client, reg);
  232. if (ret >= 0)
  233. ret = mt9m111_reg_write(client, reg, ret & ~data);
  234. return ret;
  235. }
  236. static int mt9m111_reg_mask(struct i2c_client *client, const u16 reg,
  237. const u16 data, const u16 mask)
  238. {
  239. int ret;
  240. ret = mt9m111_reg_read(client, reg);
  241. if (ret >= 0)
  242. ret = mt9m111_reg_write(client, reg, (ret & ~mask) | data);
  243. return ret;
  244. }
  245. static int mt9m111_set_context(struct mt9m111 *mt9m111,
  246. enum mt9m111_context ctxt)
  247. {
  248. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  249. int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B
  250. | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B
  251. | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B
  252. | MT9M111_CTXT_CTRL_VBLANK_SEL_B
  253. | MT9M111_CTXT_CTRL_HBLANK_SEL_B;
  254. int valA = MT9M111_CTXT_CTRL_RESTART;
  255. if (ctxt == HIGHPOWER)
  256. return reg_write(CONTEXT_CONTROL, valB);
  257. else
  258. return reg_write(CONTEXT_CONTROL, valA);
  259. }
  260. static int mt9m111_setup_rect(struct mt9m111 *mt9m111,
  261. struct v4l2_rect *rect)
  262. {
  263. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  264. int ret, is_raw_format;
  265. int width = rect->width;
  266. int height = rect->height;
  267. if (mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
  268. mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE)
  269. is_raw_format = 1;
  270. else
  271. is_raw_format = 0;
  272. ret = reg_write(COLUMN_START, rect->left);
  273. if (!ret)
  274. ret = reg_write(ROW_START, rect->top);
  275. if (is_raw_format) {
  276. if (!ret)
  277. ret = reg_write(WINDOW_WIDTH, width);
  278. if (!ret)
  279. ret = reg_write(WINDOW_HEIGHT, height);
  280. } else {
  281. if (!ret)
  282. ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH);
  283. if (!ret)
  284. ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT);
  285. if (!ret)
  286. ret = reg_write(REDUCER_XSIZE_B, width);
  287. if (!ret)
  288. ret = reg_write(REDUCER_YSIZE_B, height);
  289. if (!ret)
  290. ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH);
  291. if (!ret)
  292. ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT);
  293. if (!ret)
  294. ret = reg_write(REDUCER_XSIZE_A, width);
  295. if (!ret)
  296. ret = reg_write(REDUCER_YSIZE_A, height);
  297. }
  298. return ret;
  299. }
  300. static int mt9m111_enable(struct mt9m111 *mt9m111)
  301. {
  302. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  303. int ret;
  304. ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE);
  305. if (!ret)
  306. mt9m111->powered = 1;
  307. return ret;
  308. }
  309. static int mt9m111_reset(struct mt9m111 *mt9m111)
  310. {
  311. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  312. int ret;
  313. ret = reg_set(RESET, MT9M111_RESET_RESET_MODE);
  314. if (!ret)
  315. ret = reg_set(RESET, MT9M111_RESET_RESET_SOC);
  316. if (!ret)
  317. ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE
  318. | MT9M111_RESET_RESET_SOC);
  319. return ret;
  320. }
  321. static int mt9m111_make_rect(struct mt9m111 *mt9m111,
  322. struct v4l2_rect *rect)
  323. {
  324. if (mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
  325. mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE) {
  326. /* Bayer format - even size lengths */
  327. rect->width = ALIGN(rect->width, 2);
  328. rect->height = ALIGN(rect->height, 2);
  329. /* Let the user play with the starting pixel */
  330. }
  331. /* FIXME: the datasheet doesn't specify minimum sizes */
  332. soc_camera_limit_side(&rect->left, &rect->width,
  333. MT9M111_MIN_DARK_COLS, 2, MT9M111_MAX_WIDTH);
  334. soc_camera_limit_side(&rect->top, &rect->height,
  335. MT9M111_MIN_DARK_ROWS, 2, MT9M111_MAX_HEIGHT);
  336. return mt9m111_setup_rect(mt9m111, rect);
  337. }
  338. static int mt9m111_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  339. {
  340. struct v4l2_rect rect = a->c;
  341. struct i2c_client *client = v4l2_get_subdevdata(sd);
  342. struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
  343. int ret;
  344. dev_dbg(&client->dev, "%s left=%d, top=%d, width=%d, height=%d\n",
  345. __func__, rect.left, rect.top, rect.width, rect.height);
  346. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  347. return -EINVAL;
  348. ret = mt9m111_make_rect(mt9m111, &rect);
  349. if (!ret)
  350. mt9m111->rect = rect;
  351. return ret;
  352. }
  353. static int mt9m111_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  354. {
  355. struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
  356. a->c = mt9m111->rect;
  357. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  358. return 0;
  359. }
  360. static int mt9m111_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  361. {
  362. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  363. return -EINVAL;
  364. a->bounds.left = MT9M111_MIN_DARK_COLS;
  365. a->bounds.top = MT9M111_MIN_DARK_ROWS;
  366. a->bounds.width = MT9M111_MAX_WIDTH;
  367. a->bounds.height = MT9M111_MAX_HEIGHT;
  368. a->defrect = a->bounds;
  369. a->pixelaspect.numerator = 1;
  370. a->pixelaspect.denominator = 1;
  371. return 0;
  372. }
  373. static int mt9m111_g_fmt(struct v4l2_subdev *sd,
  374. struct v4l2_mbus_framefmt *mf)
  375. {
  376. struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
  377. mf->width = mt9m111->rect.width;
  378. mf->height = mt9m111->rect.height;
  379. mf->code = mt9m111->fmt->code;
  380. mf->colorspace = mt9m111->fmt->colorspace;
  381. mf->field = V4L2_FIELD_NONE;
  382. return 0;
  383. }
  384. static int mt9m111_set_pixfmt(struct mt9m111 *mt9m111,
  385. enum v4l2_mbus_pixelcode code)
  386. {
  387. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  388. u16 data_outfmt2, mask_outfmt2 = MT9M111_OUTFMT_PROCESSED_BAYER |
  389. MT9M111_OUTFMT_BYPASS_IFP | MT9M111_OUTFMT_RGB |
  390. MT9M111_OUTFMT_RGB565 | MT9M111_OUTFMT_RGB555 |
  391. MT9M111_OUTFMT_RGB444x | MT9M111_OUTFMT_RGBx444 |
  392. MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN |
  393. MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr_RGB_R_B;
  394. int ret;
  395. switch (code) {
  396. case V4L2_MBUS_FMT_SBGGR8_1X8:
  397. data_outfmt2 = MT9M111_OUTFMT_PROCESSED_BAYER |
  398. MT9M111_OUTFMT_RGB;
  399. break;
  400. case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
  401. data_outfmt2 = MT9M111_OUTFMT_BYPASS_IFP | MT9M111_OUTFMT_RGB;
  402. break;
  403. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  404. data_outfmt2 = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555 |
  405. MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN;
  406. break;
  407. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE:
  408. data_outfmt2 = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555;
  409. break;
  410. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  411. data_outfmt2 = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565 |
  412. MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN;
  413. break;
  414. case V4L2_MBUS_FMT_RGB565_2X8_BE:
  415. data_outfmt2 = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565;
  416. break;
  417. case V4L2_MBUS_FMT_BGR565_2X8_BE:
  418. data_outfmt2 = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565 |
  419. MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr_RGB_R_B;
  420. break;
  421. case V4L2_MBUS_FMT_BGR565_2X8_LE:
  422. data_outfmt2 = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565 |
  423. MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN |
  424. MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr_RGB_R_B;
  425. break;
  426. case V4L2_MBUS_FMT_UYVY8_2X8:
  427. data_outfmt2 = 0;
  428. break;
  429. case V4L2_MBUS_FMT_VYUY8_2X8:
  430. data_outfmt2 = MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr_RGB_R_B;
  431. break;
  432. case V4L2_MBUS_FMT_YUYV8_2X8:
  433. data_outfmt2 = MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN;
  434. break;
  435. case V4L2_MBUS_FMT_YVYU8_2X8:
  436. data_outfmt2 = MT9M111_OUTFMT_SWAP_YCbCr_C_Y_RGB_EVEN |
  437. MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr_RGB_R_B;
  438. break;
  439. default:
  440. dev_err(&client->dev, "Pixel format not handled: %x\n", code);
  441. return -EINVAL;
  442. }
  443. ret = reg_mask(OUTPUT_FORMAT_CTRL2_A, data_outfmt2,
  444. mask_outfmt2);
  445. if (!ret)
  446. ret = reg_mask(OUTPUT_FORMAT_CTRL2_B, data_outfmt2,
  447. mask_outfmt2);
  448. return ret;
  449. }
  450. static int mt9m111_s_fmt(struct v4l2_subdev *sd,
  451. struct v4l2_mbus_framefmt *mf)
  452. {
  453. struct i2c_client *client = v4l2_get_subdevdata(sd);
  454. const struct mt9m111_datafmt *fmt;
  455. struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
  456. struct v4l2_rect rect = {
  457. .left = mt9m111->rect.left,
  458. .top = mt9m111->rect.top,
  459. .width = mf->width,
  460. .height = mf->height,
  461. };
  462. int ret;
  463. fmt = mt9m111_find_datafmt(mf->code, mt9m111_colour_fmts,
  464. ARRAY_SIZE(mt9m111_colour_fmts));
  465. if (!fmt)
  466. return -EINVAL;
  467. dev_dbg(&client->dev,
  468. "%s code=%x left=%d, top=%d, width=%d, height=%d\n", __func__,
  469. mf->code, rect.left, rect.top, rect.width, rect.height);
  470. ret = mt9m111_make_rect(mt9m111, &rect);
  471. if (!ret)
  472. ret = mt9m111_set_pixfmt(mt9m111, mf->code);
  473. if (!ret) {
  474. mt9m111->rect = rect;
  475. mt9m111->fmt = fmt;
  476. mf->colorspace = fmt->colorspace;
  477. }
  478. return ret;
  479. }
  480. static int mt9m111_try_fmt(struct v4l2_subdev *sd,
  481. struct v4l2_mbus_framefmt *mf)
  482. {
  483. struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
  484. const struct mt9m111_datafmt *fmt;
  485. bool bayer = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
  486. mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE;
  487. fmt = mt9m111_find_datafmt(mf->code, mt9m111_colour_fmts,
  488. ARRAY_SIZE(mt9m111_colour_fmts));
  489. if (!fmt) {
  490. fmt = mt9m111->fmt;
  491. mf->code = fmt->code;
  492. }
  493. /*
  494. * With Bayer format enforce even side lengths, but let the user play
  495. * with the starting pixel
  496. */
  497. if (mf->height > MT9M111_MAX_HEIGHT)
  498. mf->height = MT9M111_MAX_HEIGHT;
  499. else if (mf->height < 2)
  500. mf->height = 2;
  501. else if (bayer)
  502. mf->height = ALIGN(mf->height, 2);
  503. if (mf->width > MT9M111_MAX_WIDTH)
  504. mf->width = MT9M111_MAX_WIDTH;
  505. else if (mf->width < 2)
  506. mf->width = 2;
  507. else if (bayer)
  508. mf->width = ALIGN(mf->width, 2);
  509. mf->colorspace = fmt->colorspace;
  510. return 0;
  511. }
  512. static int mt9m111_g_chip_ident(struct v4l2_subdev *sd,
  513. struct v4l2_dbg_chip_ident *id)
  514. {
  515. struct i2c_client *client = v4l2_get_subdevdata(sd);
  516. struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
  517. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  518. return -EINVAL;
  519. if (id->match.addr != client->addr)
  520. return -ENODEV;
  521. id->ident = mt9m111->model;
  522. id->revision = 0;
  523. return 0;
  524. }
  525. #ifdef CONFIG_VIDEO_ADV_DEBUG
  526. static int mt9m111_g_register(struct v4l2_subdev *sd,
  527. struct v4l2_dbg_register *reg)
  528. {
  529. struct i2c_client *client = v4l2_get_subdevdata(sd);
  530. int val;
  531. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
  532. return -EINVAL;
  533. if (reg->match.addr != client->addr)
  534. return -ENODEV;
  535. val = mt9m111_reg_read(client, reg->reg);
  536. reg->size = 2;
  537. reg->val = (u64)val;
  538. if (reg->val > 0xffff)
  539. return -EIO;
  540. return 0;
  541. }
  542. static int mt9m111_s_register(struct v4l2_subdev *sd,
  543. struct v4l2_dbg_register *reg)
  544. {
  545. struct i2c_client *client = v4l2_get_subdevdata(sd);
  546. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
  547. return -EINVAL;
  548. if (reg->match.addr != client->addr)
  549. return -ENODEV;
  550. if (mt9m111_reg_write(client, reg->reg, reg->val) < 0)
  551. return -EIO;
  552. return 0;
  553. }
  554. #endif
  555. static int mt9m111_set_flip(struct mt9m111 *mt9m111, int flip, int mask)
  556. {
  557. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  558. int ret;
  559. if (mt9m111->context == HIGHPOWER) {
  560. if (flip)
  561. ret = reg_set(READ_MODE_B, mask);
  562. else
  563. ret = reg_clear(READ_MODE_B, mask);
  564. } else {
  565. if (flip)
  566. ret = reg_set(READ_MODE_A, mask);
  567. else
  568. ret = reg_clear(READ_MODE_A, mask);
  569. }
  570. return ret;
  571. }
  572. static int mt9m111_get_global_gain(struct mt9m111 *mt9m111)
  573. {
  574. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  575. int data;
  576. data = reg_read(GLOBAL_GAIN);
  577. if (data >= 0)
  578. return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
  579. (1 << ((data >> 9) & 1));
  580. return data;
  581. }
  582. static int mt9m111_set_global_gain(struct mt9m111 *mt9m111, int gain)
  583. {
  584. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  585. u16 val;
  586. if (gain > 63 * 2 * 2)
  587. return -EINVAL;
  588. if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))
  589. val = (1 << 10) | (1 << 9) | (gain / 4);
  590. else if ((gain >= 64) && (gain < 64 * 2))
  591. val = (1 << 9) | (gain / 2);
  592. else
  593. val = gain;
  594. return reg_write(GLOBAL_GAIN, val);
  595. }
  596. static int mt9m111_set_autoexposure(struct mt9m111 *mt9m111, int on)
  597. {
  598. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  599. if (on)
  600. return reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
  601. return reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
  602. }
  603. static int mt9m111_set_autowhitebalance(struct mt9m111 *mt9m111, int on)
  604. {
  605. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  606. if (on)
  607. return reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
  608. return reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
  609. }
  610. static int mt9m111_s_ctrl(struct v4l2_ctrl *ctrl)
  611. {
  612. struct mt9m111 *mt9m111 = container_of(ctrl->handler,
  613. struct mt9m111, hdl);
  614. switch (ctrl->id) {
  615. case V4L2_CID_VFLIP:
  616. return mt9m111_set_flip(mt9m111, ctrl->val,
  617. MT9M111_RMB_MIRROR_ROWS);
  618. case V4L2_CID_HFLIP:
  619. return mt9m111_set_flip(mt9m111, ctrl->val,
  620. MT9M111_RMB_MIRROR_COLS);
  621. case V4L2_CID_GAIN:
  622. return mt9m111_set_global_gain(mt9m111, ctrl->val);
  623. case V4L2_CID_EXPOSURE_AUTO:
  624. return mt9m111_set_autoexposure(mt9m111, ctrl->val);
  625. case V4L2_CID_AUTO_WHITE_BALANCE:
  626. return mt9m111_set_autowhitebalance(mt9m111, ctrl->val);
  627. }
  628. return -EINVAL;
  629. }
  630. static int mt9m111_suspend(struct mt9m111 *mt9m111)
  631. {
  632. v4l2_ctrl_s_ctrl(mt9m111->gain, mt9m111_get_global_gain(mt9m111));
  633. return 0;
  634. }
  635. static void mt9m111_restore_state(struct mt9m111 *mt9m111)
  636. {
  637. mt9m111_set_context(mt9m111, mt9m111->context);
  638. mt9m111_set_pixfmt(mt9m111, mt9m111->fmt->code);
  639. mt9m111_setup_rect(mt9m111, &mt9m111->rect);
  640. v4l2_ctrl_handler_setup(&mt9m111->hdl);
  641. }
  642. static int mt9m111_resume(struct mt9m111 *mt9m111)
  643. {
  644. int ret = 0;
  645. if (mt9m111->powered) {
  646. ret = mt9m111_enable(mt9m111);
  647. if (!ret)
  648. ret = mt9m111_reset(mt9m111);
  649. if (!ret)
  650. mt9m111_restore_state(mt9m111);
  651. }
  652. return ret;
  653. }
  654. static int mt9m111_init(struct mt9m111 *mt9m111)
  655. {
  656. struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
  657. int ret;
  658. mt9m111->context = HIGHPOWER;
  659. ret = mt9m111_enable(mt9m111);
  660. if (!ret)
  661. ret = mt9m111_reset(mt9m111);
  662. if (!ret)
  663. ret = mt9m111_set_context(mt9m111, mt9m111->context);
  664. if (ret)
  665. dev_err(&client->dev, "mt9m111 init failed: %d\n", ret);
  666. return ret;
  667. }
  668. /*
  669. * Interface active, can use i2c. If it fails, it can indeed mean, that
  670. * this wasn't our capture interface, so, we wait for the right one
  671. */
  672. static int mt9m111_video_probe(struct i2c_client *client)
  673. {
  674. struct mt9m111 *mt9m111 = to_mt9m111(client);
  675. s32 data;
  676. int ret;
  677. data = reg_read(CHIP_VERSION);
  678. switch (data) {
  679. case 0x143a: /* MT9M111 or MT9M131 */
  680. mt9m111->model = V4L2_IDENT_MT9M111;
  681. dev_info(&client->dev,
  682. "Detected a MT9M111/MT9M131 chip ID %x\n", data);
  683. break;
  684. case 0x148c: /* MT9M112 */
  685. mt9m111->model = V4L2_IDENT_MT9M112;
  686. dev_info(&client->dev, "Detected a MT9M112 chip ID %x\n", data);
  687. break;
  688. default:
  689. dev_err(&client->dev,
  690. "No MT9M111/MT9M112/MT9M131 chip detected register read %x\n",
  691. data);
  692. return -ENODEV;
  693. }
  694. ret = mt9m111_init(mt9m111);
  695. if (ret)
  696. return ret;
  697. return v4l2_ctrl_handler_setup(&mt9m111->hdl);
  698. }
  699. static int mt9m111_s_power(struct v4l2_subdev *sd, int on)
  700. {
  701. struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
  702. struct i2c_client *client = v4l2_get_subdevdata(sd);
  703. int ret = 0;
  704. mutex_lock(&mt9m111->power_lock);
  705. /*
  706. * If the power count is modified from 0 to != 0 or from != 0 to 0,
  707. * update the power state.
  708. */
  709. if (mt9m111->power_count == !on) {
  710. if (on) {
  711. ret = mt9m111_resume(mt9m111);
  712. if (ret) {
  713. dev_err(&client->dev,
  714. "Failed to resume the sensor: %d\n", ret);
  715. goto out;
  716. }
  717. } else {
  718. mt9m111_suspend(mt9m111);
  719. }
  720. }
  721. /* Update the power count. */
  722. mt9m111->power_count += on ? 1 : -1;
  723. WARN_ON(mt9m111->power_count < 0);
  724. out:
  725. mutex_unlock(&mt9m111->power_lock);
  726. return ret;
  727. }
  728. static const struct v4l2_ctrl_ops mt9m111_ctrl_ops = {
  729. .s_ctrl = mt9m111_s_ctrl,
  730. };
  731. static struct v4l2_subdev_core_ops mt9m111_subdev_core_ops = {
  732. .g_chip_ident = mt9m111_g_chip_ident,
  733. .s_power = mt9m111_s_power,
  734. #ifdef CONFIG_VIDEO_ADV_DEBUG
  735. .g_register = mt9m111_g_register,
  736. .s_register = mt9m111_s_register,
  737. #endif
  738. };
  739. static int mt9m111_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  740. enum v4l2_mbus_pixelcode *code)
  741. {
  742. if (index >= ARRAY_SIZE(mt9m111_colour_fmts))
  743. return -EINVAL;
  744. *code = mt9m111_colour_fmts[index].code;
  745. return 0;
  746. }
  747. static int mt9m111_g_mbus_config(struct v4l2_subdev *sd,
  748. struct v4l2_mbus_config *cfg)
  749. {
  750. struct i2c_client *client = v4l2_get_subdevdata(sd);
  751. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  752. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
  753. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  754. V4L2_MBUS_DATA_ACTIVE_HIGH;
  755. cfg->type = V4L2_MBUS_PARALLEL;
  756. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  757. return 0;
  758. }
  759. static struct v4l2_subdev_video_ops mt9m111_subdev_video_ops = {
  760. .s_mbus_fmt = mt9m111_s_fmt,
  761. .g_mbus_fmt = mt9m111_g_fmt,
  762. .try_mbus_fmt = mt9m111_try_fmt,
  763. .s_crop = mt9m111_s_crop,
  764. .g_crop = mt9m111_g_crop,
  765. .cropcap = mt9m111_cropcap,
  766. .enum_mbus_fmt = mt9m111_enum_fmt,
  767. .g_mbus_config = mt9m111_g_mbus_config,
  768. };
  769. static struct v4l2_subdev_ops mt9m111_subdev_ops = {
  770. .core = &mt9m111_subdev_core_ops,
  771. .video = &mt9m111_subdev_video_ops,
  772. };
  773. static int mt9m111_probe(struct i2c_client *client,
  774. const struct i2c_device_id *did)
  775. {
  776. struct mt9m111 *mt9m111;
  777. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  778. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  779. int ret;
  780. if (!icl) {
  781. dev_err(&client->dev, "mt9m111: driver needs platform data\n");
  782. return -EINVAL;
  783. }
  784. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  785. dev_warn(&adapter->dev,
  786. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  787. return -EIO;
  788. }
  789. mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);
  790. if (!mt9m111)
  791. return -ENOMEM;
  792. v4l2_i2c_subdev_init(&mt9m111->subdev, client, &mt9m111_subdev_ops);
  793. v4l2_ctrl_handler_init(&mt9m111->hdl, 5);
  794. v4l2_ctrl_new_std(&mt9m111->hdl, &mt9m111_ctrl_ops,
  795. V4L2_CID_VFLIP, 0, 1, 1, 0);
  796. v4l2_ctrl_new_std(&mt9m111->hdl, &mt9m111_ctrl_ops,
  797. V4L2_CID_HFLIP, 0, 1, 1, 0);
  798. v4l2_ctrl_new_std(&mt9m111->hdl, &mt9m111_ctrl_ops,
  799. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  800. mt9m111->gain = v4l2_ctrl_new_std(&mt9m111->hdl, &mt9m111_ctrl_ops,
  801. V4L2_CID_GAIN, 0, 63 * 2 * 2, 1, 32);
  802. v4l2_ctrl_new_std_menu(&mt9m111->hdl,
  803. &mt9m111_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  804. V4L2_EXPOSURE_AUTO);
  805. mt9m111->subdev.ctrl_handler = &mt9m111->hdl;
  806. if (mt9m111->hdl.error) {
  807. int err = mt9m111->hdl.error;
  808. kfree(mt9m111);
  809. return err;
  810. }
  811. /* Second stage probe - when a capture adapter is there */
  812. mt9m111->rect.left = MT9M111_MIN_DARK_COLS;
  813. mt9m111->rect.top = MT9M111_MIN_DARK_ROWS;
  814. mt9m111->rect.width = MT9M111_MAX_WIDTH;
  815. mt9m111->rect.height = MT9M111_MAX_HEIGHT;
  816. mt9m111->fmt = &mt9m111_colour_fmts[0];
  817. mt9m111->lastpage = -1;
  818. ret = mt9m111_video_probe(client);
  819. if (ret) {
  820. v4l2_ctrl_handler_free(&mt9m111->hdl);
  821. kfree(mt9m111);
  822. }
  823. return ret;
  824. }
  825. static int mt9m111_remove(struct i2c_client *client)
  826. {
  827. struct mt9m111 *mt9m111 = to_mt9m111(client);
  828. v4l2_device_unregister_subdev(&mt9m111->subdev);
  829. v4l2_ctrl_handler_free(&mt9m111->hdl);
  830. kfree(mt9m111);
  831. return 0;
  832. }
  833. static const struct i2c_device_id mt9m111_id[] = {
  834. { "mt9m111", 0 },
  835. { }
  836. };
  837. MODULE_DEVICE_TABLE(i2c, mt9m111_id);
  838. static struct i2c_driver mt9m111_i2c_driver = {
  839. .driver = {
  840. .name = "mt9m111",
  841. },
  842. .probe = mt9m111_probe,
  843. .remove = mt9m111_remove,
  844. .id_table = mt9m111_id,
  845. };
  846. static int __init mt9m111_mod_init(void)
  847. {
  848. return i2c_add_driver(&mt9m111_i2c_driver);
  849. }
  850. static void __exit mt9m111_mod_exit(void)
  851. {
  852. i2c_del_driver(&mt9m111_i2c_driver);
  853. }
  854. module_init(mt9m111_mod_init);
  855. module_exit(mt9m111_mod_exit);
  856. MODULE_DESCRIPTION("Micron/Aptina MT9M111/MT9M112/MT9M131 Camera driver");
  857. MODULE_AUTHOR("Robert Jarzmik");
  858. MODULE_LICENSE("GPL");