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