mt9m111.c 28 KB

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