mt9m111.c 30 KB

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