mt9v022.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Driver for MT9V022 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski <[email protected]>
  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/delay.h>
  14. #include <linux/log2.h>
  15. #include <linux/module.h>
  16. #include <media/i2c/mt9v022.h>
  17. #include <media/soc_camera.h>
  18. #include <media/drv-intf/soc_mediabus.h>
  19. #include <media/v4l2-subdev.h>
  20. #include <media/v4l2-clk.h>
  21. #include <media/v4l2-ctrls.h>
  22. /*
  23. * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
  24. * The platform has to define struct i2c_board_info objects and link to them
  25. * from struct soc_camera_host_desc
  26. */
  27. static char *sensor_type;
  28. module_param(sensor_type, charp, S_IRUGO);
  29. MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
  30. /* mt9v022 selected register addresses */
  31. #define MT9V022_CHIP_VERSION 0x00
  32. #define MT9V022_COLUMN_START 0x01
  33. #define MT9V022_ROW_START 0x02
  34. #define MT9V022_WINDOW_HEIGHT 0x03
  35. #define MT9V022_WINDOW_WIDTH 0x04
  36. #define MT9V022_HORIZONTAL_BLANKING 0x05
  37. #define MT9V022_VERTICAL_BLANKING 0x06
  38. #define MT9V022_CHIP_CONTROL 0x07
  39. #define MT9V022_SHUTTER_WIDTH1 0x08
  40. #define MT9V022_SHUTTER_WIDTH2 0x09
  41. #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
  42. #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
  43. #define MT9V022_RESET 0x0c
  44. #define MT9V022_READ_MODE 0x0d
  45. #define MT9V022_MONITOR_MODE 0x0e
  46. #define MT9V022_PIXEL_OPERATION_MODE 0x0f
  47. #define MT9V022_LED_OUT_CONTROL 0x1b
  48. #define MT9V022_ADC_MODE_CONTROL 0x1c
  49. #define MT9V022_REG32 0x20
  50. #define MT9V022_ANALOG_GAIN 0x35
  51. #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
  52. #define MT9V022_PIXCLK_FV_LV 0x74
  53. #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
  54. #define MT9V022_AEC_AGC_ENABLE 0xAF
  55. #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
  56. /* mt9v024 partial list register addresses changes with respect to mt9v022 */
  57. #define MT9V024_PIXCLK_FV_LV 0x72
  58. #define MT9V024_MAX_TOTAL_SHUTTER_WIDTH 0xAD
  59. /* Progressive scan, master, defaults */
  60. #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
  61. #define MT9V022_MAX_WIDTH 752
  62. #define MT9V022_MAX_HEIGHT 480
  63. #define MT9V022_MIN_WIDTH 48
  64. #define MT9V022_MIN_HEIGHT 32
  65. #define MT9V022_COLUMN_SKIP 1
  66. #define MT9V022_ROW_SKIP 4
  67. #define MT9V022_HORIZONTAL_BLANKING_MIN 43
  68. #define MT9V022_HORIZONTAL_BLANKING_MAX 1023
  69. #define MT9V022_HORIZONTAL_BLANKING_DEF 94
  70. #define MT9V022_VERTICAL_BLANKING_MIN 2
  71. #define MT9V022_VERTICAL_BLANKING_MAX 3000
  72. #define MT9V022_VERTICAL_BLANKING_DEF 45
  73. #define is_mt9v022_rev3(id) (id == 0x1313)
  74. #define is_mt9v024(id) (id == 0x1324)
  75. /* MT9V022 has only one fixed colorspace per pixelcode */
  76. struct mt9v022_datafmt {
  77. u32 code;
  78. enum v4l2_colorspace colorspace;
  79. };
  80. /* Find a data format by a pixel code in an array */
  81. static const struct mt9v022_datafmt *mt9v022_find_datafmt(
  82. u32 code, const struct mt9v022_datafmt *fmt,
  83. int n)
  84. {
  85. int i;
  86. for (i = 0; i < n; i++)
  87. if (fmt[i].code == code)
  88. return fmt + i;
  89. return NULL;
  90. }
  91. static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
  92. /*
  93. * Order important: first natively supported,
  94. * second supported with a GPIO extender
  95. */
  96. {MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
  97. {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
  98. };
  99. static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
  100. /* Order important - see above */
  101. {MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
  102. {MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
  103. };
  104. /* only registers with different addresses on different mt9v02x sensors */
  105. struct mt9v02x_register {
  106. u8 max_total_shutter_width;
  107. u8 pixclk_fv_lv;
  108. };
  109. static const struct mt9v02x_register mt9v022_register = {
  110. .max_total_shutter_width = MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
  111. .pixclk_fv_lv = MT9V022_PIXCLK_FV_LV,
  112. };
  113. static const struct mt9v02x_register mt9v024_register = {
  114. .max_total_shutter_width = MT9V024_MAX_TOTAL_SHUTTER_WIDTH,
  115. .pixclk_fv_lv = MT9V024_PIXCLK_FV_LV,
  116. };
  117. enum mt9v022_model {
  118. MT9V022IX7ATM,
  119. MT9V022IX7ATC,
  120. };
  121. struct mt9v022 {
  122. struct v4l2_subdev subdev;
  123. struct v4l2_ctrl_handler hdl;
  124. struct {
  125. /* exposure/auto-exposure cluster */
  126. struct v4l2_ctrl *autoexposure;
  127. struct v4l2_ctrl *exposure;
  128. };
  129. struct {
  130. /* gain/auto-gain cluster */
  131. struct v4l2_ctrl *autogain;
  132. struct v4l2_ctrl *gain;
  133. };
  134. struct v4l2_ctrl *hblank;
  135. struct v4l2_ctrl *vblank;
  136. struct v4l2_rect rect; /* Sensor window */
  137. struct v4l2_clk *clk;
  138. const struct mt9v022_datafmt *fmt;
  139. const struct mt9v022_datafmt *fmts;
  140. const struct mt9v02x_register *reg;
  141. int num_fmts;
  142. enum mt9v022_model model;
  143. u16 chip_control;
  144. u16 chip_version;
  145. unsigned short y_skip_top; /* Lines to skip at the top */
  146. };
  147. static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
  148. {
  149. return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
  150. }
  151. static int reg_read(struct i2c_client *client, const u8 reg)
  152. {
  153. return i2c_smbus_read_word_swapped(client, reg);
  154. }
  155. static int reg_write(struct i2c_client *client, const u8 reg,
  156. const u16 data)
  157. {
  158. return i2c_smbus_write_word_swapped(client, reg, data);
  159. }
  160. static int reg_set(struct i2c_client *client, const u8 reg,
  161. const u16 data)
  162. {
  163. int ret;
  164. ret = reg_read(client, reg);
  165. if (ret < 0)
  166. return ret;
  167. return reg_write(client, reg, ret | data);
  168. }
  169. static int reg_clear(struct i2c_client *client, const u8 reg,
  170. const u16 data)
  171. {
  172. int ret;
  173. ret = reg_read(client, reg);
  174. if (ret < 0)
  175. return ret;
  176. return reg_write(client, reg, ret & ~data);
  177. }
  178. static int mt9v022_init(struct i2c_client *client)
  179. {
  180. struct mt9v022 *mt9v022 = to_mt9v022(client);
  181. int ret;
  182. /*
  183. * Almost the default mode: master, parallel, simultaneous, and an
  184. * undocumented bit 0x200, which is present in table 7, but not in 8,
  185. * plus snapshot mode to disable scan for now
  186. */
  187. mt9v022->chip_control |= 0x10;
  188. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  189. if (!ret)
  190. ret = reg_write(client, MT9V022_READ_MODE, 0x300);
  191. /* All defaults */
  192. if (!ret)
  193. /* AEC, AGC on */
  194. ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
  195. if (!ret)
  196. ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
  197. if (!ret)
  198. ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
  199. if (!ret)
  200. ret = reg_write(client, mt9v022->reg->max_total_shutter_width, 480);
  201. if (!ret)
  202. /* default - auto */
  203. ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
  204. if (!ret)
  205. ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
  206. if (!ret)
  207. return v4l2_ctrl_handler_setup(&mt9v022->hdl);
  208. return ret;
  209. }
  210. static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
  211. {
  212. struct i2c_client *client = v4l2_get_subdevdata(sd);
  213. struct mt9v022 *mt9v022 = to_mt9v022(client);
  214. if (enable) {
  215. /* Switch to master "normal" mode */
  216. mt9v022->chip_control &= ~0x10;
  217. if (is_mt9v022_rev3(mt9v022->chip_version) ||
  218. is_mt9v024(mt9v022->chip_version)) {
  219. /*
  220. * Unset snapshot mode specific settings: clear bit 9
  221. * and bit 2 in reg. 0x20 when in normal mode.
  222. */
  223. if (reg_clear(client, MT9V022_REG32, 0x204))
  224. return -EIO;
  225. }
  226. } else {
  227. /* Switch to snapshot mode */
  228. mt9v022->chip_control |= 0x10;
  229. if (is_mt9v022_rev3(mt9v022->chip_version) ||
  230. is_mt9v024(mt9v022->chip_version)) {
  231. /*
  232. * Required settings for snapshot mode: set bit 9
  233. * (RST enable) and bit 2 (CR enable) in reg. 0x20
  234. * See TechNote TN0960 or TN-09-225.
  235. */
  236. if (reg_set(client, MT9V022_REG32, 0x204))
  237. return -EIO;
  238. }
  239. }
  240. if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
  241. return -EIO;
  242. return 0;
  243. }
  244. static int mt9v022_set_selection(struct v4l2_subdev *sd,
  245. struct v4l2_subdev_pad_config *cfg,
  246. struct v4l2_subdev_selection *sel)
  247. {
  248. struct i2c_client *client = v4l2_get_subdevdata(sd);
  249. struct mt9v022 *mt9v022 = to_mt9v022(client);
  250. struct v4l2_rect rect = sel->r;
  251. int min_row, min_blank;
  252. int ret;
  253. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
  254. sel->target != V4L2_SEL_TGT_CROP)
  255. return -EINVAL;
  256. /* Bayer format - even size lengths */
  257. if (mt9v022->fmts == mt9v022_colour_fmts) {
  258. rect.width = ALIGN(rect.width, 2);
  259. rect.height = ALIGN(rect.height, 2);
  260. /* Let the user play with the starting pixel */
  261. }
  262. soc_camera_limit_side(&rect.left, &rect.width,
  263. MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
  264. soc_camera_limit_side(&rect.top, &rect.height,
  265. MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
  266. /* Like in example app. Contradicts the datasheet though */
  267. ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  268. if (ret >= 0) {
  269. if (ret & 1) /* Autoexposure */
  270. ret = reg_write(client, mt9v022->reg->max_total_shutter_width,
  271. rect.height + mt9v022->y_skip_top + 43);
  272. /*
  273. * If autoexposure is off, there is no need to set
  274. * MT9V022_TOTAL_SHUTTER_WIDTH here. Autoexposure can be off
  275. * only if the user has set exposure manually, using the
  276. * V4L2_CID_EXPOSURE_AUTO with the value V4L2_EXPOSURE_MANUAL.
  277. * In this case the register MT9V022_TOTAL_SHUTTER_WIDTH
  278. * already contains the correct value.
  279. */
  280. }
  281. /* Setup frame format: defaults apart from width and height */
  282. if (!ret)
  283. ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
  284. if (!ret)
  285. ret = reg_write(client, MT9V022_ROW_START, rect.top);
  286. /*
  287. * mt9v022: min total row time is 660 columns, min blanking is 43
  288. * mt9v024: min total row time is 690 columns, min blanking is 61
  289. */
  290. if (is_mt9v024(mt9v022->chip_version)) {
  291. min_row = 690;
  292. min_blank = 61;
  293. } else {
  294. min_row = 660;
  295. min_blank = 43;
  296. }
  297. if (!ret)
  298. ret = v4l2_ctrl_s_ctrl(mt9v022->hblank,
  299. rect.width > min_row - min_blank ?
  300. min_blank : min_row - rect.width);
  301. if (!ret)
  302. ret = v4l2_ctrl_s_ctrl(mt9v022->vblank, 45);
  303. if (!ret)
  304. ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
  305. if (!ret)
  306. ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
  307. rect.height + mt9v022->y_skip_top);
  308. if (ret < 0)
  309. return ret;
  310. dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
  311. mt9v022->rect = rect;
  312. return 0;
  313. }
  314. static int mt9v022_get_selection(struct v4l2_subdev *sd,
  315. struct v4l2_subdev_pad_config *cfg,
  316. struct v4l2_subdev_selection *sel)
  317. {
  318. struct i2c_client *client = v4l2_get_subdevdata(sd);
  319. struct mt9v022 *mt9v022 = to_mt9v022(client);
  320. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  321. return -EINVAL;
  322. switch (sel->target) {
  323. case V4L2_SEL_TGT_CROP_BOUNDS:
  324. case V4L2_SEL_TGT_CROP_DEFAULT:
  325. sel->r.left = MT9V022_COLUMN_SKIP;
  326. sel->r.top = MT9V022_ROW_SKIP;
  327. sel->r.width = MT9V022_MAX_WIDTH;
  328. sel->r.height = MT9V022_MAX_HEIGHT;
  329. return 0;
  330. case V4L2_SEL_TGT_CROP:
  331. sel->r = mt9v022->rect;
  332. return 0;
  333. default:
  334. return -EINVAL;
  335. }
  336. }
  337. static int mt9v022_get_fmt(struct v4l2_subdev *sd,
  338. struct v4l2_subdev_pad_config *cfg,
  339. struct v4l2_subdev_format *format)
  340. {
  341. struct v4l2_mbus_framefmt *mf = &format->format;
  342. struct i2c_client *client = v4l2_get_subdevdata(sd);
  343. struct mt9v022 *mt9v022 = to_mt9v022(client);
  344. if (format->pad)
  345. return -EINVAL;
  346. mf->width = mt9v022->rect.width;
  347. mf->height = mt9v022->rect.height;
  348. mf->code = mt9v022->fmt->code;
  349. mf->colorspace = mt9v022->fmt->colorspace;
  350. mf->field = V4L2_FIELD_NONE;
  351. return 0;
  352. }
  353. static int mt9v022_s_fmt(struct v4l2_subdev *sd,
  354. struct v4l2_mbus_framefmt *mf)
  355. {
  356. struct i2c_client *client = v4l2_get_subdevdata(sd);
  357. struct mt9v022 *mt9v022 = to_mt9v022(client);
  358. struct v4l2_subdev_selection sel = {
  359. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  360. .target = V4L2_SEL_TGT_CROP,
  361. .r.left = mt9v022->rect.left,
  362. .r.top = mt9v022->rect.top,
  363. .r.width = mf->width,
  364. .r.height = mf->height,
  365. };
  366. int ret;
  367. /*
  368. * The caller provides a supported format, as verified per call to
  369. * .set_fmt(FORMAT_TRY), datawidth is from our supported format list
  370. */
  371. switch (mf->code) {
  372. case MEDIA_BUS_FMT_Y8_1X8:
  373. case MEDIA_BUS_FMT_Y10_1X10:
  374. if (mt9v022->model != MT9V022IX7ATM)
  375. return -EINVAL;
  376. break;
  377. case MEDIA_BUS_FMT_SBGGR8_1X8:
  378. case MEDIA_BUS_FMT_SBGGR10_1X10:
  379. if (mt9v022->model != MT9V022IX7ATC)
  380. return -EINVAL;
  381. break;
  382. default:
  383. return -EINVAL;
  384. }
  385. /* No support for scaling on this camera, just crop. */
  386. ret = mt9v022_set_selection(sd, NULL, &sel);
  387. if (!ret) {
  388. mf->width = mt9v022->rect.width;
  389. mf->height = mt9v022->rect.height;
  390. mt9v022->fmt = mt9v022_find_datafmt(mf->code,
  391. mt9v022->fmts, mt9v022->num_fmts);
  392. mf->colorspace = mt9v022->fmt->colorspace;
  393. }
  394. return ret;
  395. }
  396. static int mt9v022_set_fmt(struct v4l2_subdev *sd,
  397. struct v4l2_subdev_pad_config *cfg,
  398. struct v4l2_subdev_format *format)
  399. {
  400. struct v4l2_mbus_framefmt *mf = &format->format;
  401. struct i2c_client *client = v4l2_get_subdevdata(sd);
  402. struct mt9v022 *mt9v022 = to_mt9v022(client);
  403. const struct mt9v022_datafmt *fmt;
  404. int align = mf->code == MEDIA_BUS_FMT_SBGGR8_1X8 ||
  405. mf->code == MEDIA_BUS_FMT_SBGGR10_1X10;
  406. if (format->pad)
  407. return -EINVAL;
  408. v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
  409. MT9V022_MAX_WIDTH, align,
  410. &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
  411. MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
  412. fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
  413. mt9v022->num_fmts);
  414. if (!fmt) {
  415. fmt = mt9v022->fmt;
  416. mf->code = fmt->code;
  417. }
  418. mf->colorspace = fmt->colorspace;
  419. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  420. return mt9v022_s_fmt(sd, mf);
  421. cfg->try_fmt = *mf;
  422. return 0;
  423. }
  424. #ifdef CONFIG_VIDEO_ADV_DEBUG
  425. static int mt9v022_g_register(struct v4l2_subdev *sd,
  426. struct v4l2_dbg_register *reg)
  427. {
  428. struct i2c_client *client = v4l2_get_subdevdata(sd);
  429. if (reg->reg > 0xff)
  430. return -EINVAL;
  431. reg->size = 2;
  432. reg->val = reg_read(client, reg->reg);
  433. if (reg->val > 0xffff)
  434. return -EIO;
  435. return 0;
  436. }
  437. static int mt9v022_s_register(struct v4l2_subdev *sd,
  438. const struct v4l2_dbg_register *reg)
  439. {
  440. struct i2c_client *client = v4l2_get_subdevdata(sd);
  441. if (reg->reg > 0xff)
  442. return -EINVAL;
  443. if (reg_write(client, reg->reg, reg->val) < 0)
  444. return -EIO;
  445. return 0;
  446. }
  447. #endif
  448. static int mt9v022_s_power(struct v4l2_subdev *sd, int on)
  449. {
  450. struct i2c_client *client = v4l2_get_subdevdata(sd);
  451. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  452. struct mt9v022 *mt9v022 = to_mt9v022(client);
  453. return soc_camera_set_power(&client->dev, ssdd, mt9v022->clk, on);
  454. }
  455. static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  456. {
  457. struct mt9v022 *mt9v022 = container_of(ctrl->handler,
  458. struct mt9v022, hdl);
  459. struct v4l2_subdev *sd = &mt9v022->subdev;
  460. struct i2c_client *client = v4l2_get_subdevdata(sd);
  461. struct v4l2_ctrl *gain = mt9v022->gain;
  462. struct v4l2_ctrl *exp = mt9v022->exposure;
  463. unsigned long range;
  464. int data;
  465. switch (ctrl->id) {
  466. case V4L2_CID_AUTOGAIN:
  467. data = reg_read(client, MT9V022_ANALOG_GAIN);
  468. if (data < 0)
  469. return -EIO;
  470. range = gain->maximum - gain->minimum;
  471. gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
  472. return 0;
  473. case V4L2_CID_EXPOSURE_AUTO:
  474. data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
  475. if (data < 0)
  476. return -EIO;
  477. range = exp->maximum - exp->minimum;
  478. exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
  479. return 0;
  480. case V4L2_CID_HBLANK:
  481. data = reg_read(client, MT9V022_HORIZONTAL_BLANKING);
  482. if (data < 0)
  483. return -EIO;
  484. ctrl->val = data;
  485. return 0;
  486. case V4L2_CID_VBLANK:
  487. data = reg_read(client, MT9V022_VERTICAL_BLANKING);
  488. if (data < 0)
  489. return -EIO;
  490. ctrl->val = data;
  491. return 0;
  492. }
  493. return -EINVAL;
  494. }
  495. static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
  496. {
  497. struct mt9v022 *mt9v022 = container_of(ctrl->handler,
  498. struct mt9v022, hdl);
  499. struct v4l2_subdev *sd = &mt9v022->subdev;
  500. struct i2c_client *client = v4l2_get_subdevdata(sd);
  501. int data;
  502. switch (ctrl->id) {
  503. case V4L2_CID_VFLIP:
  504. if (ctrl->val)
  505. data = reg_set(client, MT9V022_READ_MODE, 0x10);
  506. else
  507. data = reg_clear(client, MT9V022_READ_MODE, 0x10);
  508. if (data < 0)
  509. return -EIO;
  510. return 0;
  511. case V4L2_CID_HFLIP:
  512. if (ctrl->val)
  513. data = reg_set(client, MT9V022_READ_MODE, 0x20);
  514. else
  515. data = reg_clear(client, MT9V022_READ_MODE, 0x20);
  516. if (data < 0)
  517. return -EIO;
  518. return 0;
  519. case V4L2_CID_AUTOGAIN:
  520. if (ctrl->val) {
  521. if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  522. return -EIO;
  523. } else {
  524. struct v4l2_ctrl *gain = mt9v022->gain;
  525. /* mt9v022 has minimum == default */
  526. unsigned long range = gain->maximum - gain->minimum;
  527. /* Valid values 16 to 64, 32 to 64 must be even. */
  528. unsigned long gain_val = ((gain->val - (s32)gain->minimum) *
  529. 48 + range / 2) / range + 16;
  530. if (gain_val >= 32)
  531. gain_val &= ~1;
  532. /*
  533. * The user wants to set gain manually, hope, she
  534. * knows, what she's doing... Switch AGC off.
  535. */
  536. if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  537. return -EIO;
  538. dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
  539. reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
  540. if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
  541. return -EIO;
  542. }
  543. return 0;
  544. case V4L2_CID_EXPOSURE_AUTO:
  545. if (ctrl->val == V4L2_EXPOSURE_AUTO) {
  546. data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  547. } else {
  548. struct v4l2_ctrl *exp = mt9v022->exposure;
  549. unsigned long range = exp->maximum - exp->minimum;
  550. unsigned long shutter = ((exp->val - (s32)exp->minimum) *
  551. 479 + range / 2) / range + 1;
  552. /*
  553. * The user wants to set shutter width manually, hope,
  554. * she knows, what she's doing... Switch AEC off.
  555. */
  556. data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  557. if (data < 0)
  558. return -EIO;
  559. dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
  560. reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
  561. shutter);
  562. if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
  563. shutter) < 0)
  564. return -EIO;
  565. }
  566. return 0;
  567. case V4L2_CID_HBLANK:
  568. if (reg_write(client, MT9V022_HORIZONTAL_BLANKING,
  569. ctrl->val) < 0)
  570. return -EIO;
  571. return 0;
  572. case V4L2_CID_VBLANK:
  573. if (reg_write(client, MT9V022_VERTICAL_BLANKING,
  574. ctrl->val) < 0)
  575. return -EIO;
  576. return 0;
  577. }
  578. return -EINVAL;
  579. }
  580. /*
  581. * Interface active, can use i2c. If it fails, it can indeed mean, that
  582. * this wasn't our capture interface, so, we wait for the right one
  583. */
  584. static int mt9v022_video_probe(struct i2c_client *client)
  585. {
  586. struct mt9v022 *mt9v022 = to_mt9v022(client);
  587. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  588. s32 data;
  589. int ret;
  590. unsigned long flags;
  591. ret = mt9v022_s_power(&mt9v022->subdev, 1);
  592. if (ret < 0)
  593. return ret;
  594. /* Read out the chip version register */
  595. data = reg_read(client, MT9V022_CHIP_VERSION);
  596. /* must be 0x1311, 0x1313 or 0x1324 */
  597. if (data != 0x1311 && data != 0x1313 && data != 0x1324) {
  598. ret = -ENODEV;
  599. dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
  600. data);
  601. goto ei2c;
  602. }
  603. mt9v022->chip_version = data;
  604. mt9v022->reg = is_mt9v024(data) ? &mt9v024_register :
  605. &mt9v022_register;
  606. /* Soft reset */
  607. ret = reg_write(client, MT9V022_RESET, 1);
  608. if (ret < 0)
  609. goto ei2c;
  610. /* 15 clock cycles */
  611. udelay(200);
  612. if (reg_read(client, MT9V022_RESET)) {
  613. dev_err(&client->dev, "Resetting MT9V022 failed!\n");
  614. if (ret > 0)
  615. ret = -EIO;
  616. goto ei2c;
  617. }
  618. /* Set monochrome or colour sensor type */
  619. if (sensor_type && (!strcmp("colour", sensor_type) ||
  620. !strcmp("color", sensor_type))) {
  621. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  622. mt9v022->model = MT9V022IX7ATC;
  623. mt9v022->fmts = mt9v022_colour_fmts;
  624. } else {
  625. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  626. mt9v022->model = MT9V022IX7ATM;
  627. mt9v022->fmts = mt9v022_monochrome_fmts;
  628. }
  629. if (ret < 0)
  630. goto ei2c;
  631. mt9v022->num_fmts = 0;
  632. /*
  633. * This is a 10bit sensor, so by default we only allow 10bit.
  634. * The platform may support different bus widths due to
  635. * different routing of the data lines.
  636. */
  637. if (ssdd->query_bus_param)
  638. flags = ssdd->query_bus_param(ssdd);
  639. else
  640. flags = SOCAM_DATAWIDTH_10;
  641. if (flags & SOCAM_DATAWIDTH_10)
  642. mt9v022->num_fmts++;
  643. else
  644. mt9v022->fmts++;
  645. if (flags & SOCAM_DATAWIDTH_8)
  646. mt9v022->num_fmts++;
  647. mt9v022->fmt = &mt9v022->fmts[0];
  648. dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  649. data, mt9v022->model == MT9V022IX7ATM ?
  650. "monochrome" : "colour");
  651. ret = mt9v022_init(client);
  652. if (ret < 0)
  653. dev_err(&client->dev, "Failed to initialise the camera\n");
  654. ei2c:
  655. mt9v022_s_power(&mt9v022->subdev, 0);
  656. return ret;
  657. }
  658. static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  659. {
  660. struct i2c_client *client = v4l2_get_subdevdata(sd);
  661. struct mt9v022 *mt9v022 = to_mt9v022(client);
  662. *lines = mt9v022->y_skip_top;
  663. return 0;
  664. }
  665. static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
  666. .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
  667. .s_ctrl = mt9v022_s_ctrl,
  668. };
  669. static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
  670. #ifdef CONFIG_VIDEO_ADV_DEBUG
  671. .g_register = mt9v022_g_register,
  672. .s_register = mt9v022_s_register,
  673. #endif
  674. .s_power = mt9v022_s_power,
  675. };
  676. static int mt9v022_enum_mbus_code(struct v4l2_subdev *sd,
  677. struct v4l2_subdev_pad_config *cfg,
  678. struct v4l2_subdev_mbus_code_enum *code)
  679. {
  680. struct i2c_client *client = v4l2_get_subdevdata(sd);
  681. struct mt9v022 *mt9v022 = to_mt9v022(client);
  682. if (code->pad || code->index >= mt9v022->num_fmts)
  683. return -EINVAL;
  684. code->code = mt9v022->fmts[code->index].code;
  685. return 0;
  686. }
  687. static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
  688. struct v4l2_mbus_config *cfg)
  689. {
  690. struct i2c_client *client = v4l2_get_subdevdata(sd);
  691. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  692. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
  693. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  694. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  695. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  696. V4L2_MBUS_DATA_ACTIVE_HIGH;
  697. cfg->type = V4L2_MBUS_PARALLEL;
  698. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  699. return 0;
  700. }
  701. static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
  702. const struct v4l2_mbus_config *cfg)
  703. {
  704. struct i2c_client *client = v4l2_get_subdevdata(sd);
  705. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  706. struct mt9v022 *mt9v022 = to_mt9v022(client);
  707. unsigned long flags = soc_camera_apply_board_flags(ssdd, cfg);
  708. unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
  709. int ret;
  710. u16 pixclk = 0;
  711. if (ssdd->set_bus_param) {
  712. ret = ssdd->set_bus_param(ssdd, 1 << (bps - 1));
  713. if (ret)
  714. return ret;
  715. } else if (bps != 10) {
  716. /*
  717. * Without board specific bus width settings we only support the
  718. * sensors native bus width
  719. */
  720. return -EINVAL;
  721. }
  722. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  723. pixclk |= 0x10;
  724. if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
  725. pixclk |= 0x1;
  726. if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
  727. pixclk |= 0x2;
  728. ret = reg_write(client, mt9v022->reg->pixclk_fv_lv, pixclk);
  729. if (ret < 0)
  730. return ret;
  731. if (!(flags & V4L2_MBUS_MASTER))
  732. mt9v022->chip_control &= ~0x8;
  733. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  734. if (ret < 0)
  735. return ret;
  736. dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  737. pixclk, mt9v022->chip_control);
  738. return 0;
  739. }
  740. static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
  741. .s_stream = mt9v022_s_stream,
  742. .g_mbus_config = mt9v022_g_mbus_config,
  743. .s_mbus_config = mt9v022_s_mbus_config,
  744. };
  745. static const struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
  746. .g_skip_top_lines = mt9v022_g_skip_top_lines,
  747. };
  748. static const struct v4l2_subdev_pad_ops mt9v022_subdev_pad_ops = {
  749. .enum_mbus_code = mt9v022_enum_mbus_code,
  750. .get_selection = mt9v022_get_selection,
  751. .set_selection = mt9v022_set_selection,
  752. .get_fmt = mt9v022_get_fmt,
  753. .set_fmt = mt9v022_set_fmt,
  754. };
  755. static struct v4l2_subdev_ops mt9v022_subdev_ops = {
  756. .core = &mt9v022_subdev_core_ops,
  757. .video = &mt9v022_subdev_video_ops,
  758. .sensor = &mt9v022_subdev_sensor_ops,
  759. .pad = &mt9v022_subdev_pad_ops,
  760. };
  761. static int mt9v022_probe(struct i2c_client *client,
  762. const struct i2c_device_id *did)
  763. {
  764. struct mt9v022 *mt9v022;
  765. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  766. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  767. struct mt9v022_platform_data *pdata;
  768. int ret;
  769. if (!ssdd) {
  770. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  771. return -EINVAL;
  772. }
  773. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  774. dev_warn(&adapter->dev,
  775. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  776. return -EIO;
  777. }
  778. mt9v022 = devm_kzalloc(&client->dev, sizeof(struct mt9v022), GFP_KERNEL);
  779. if (!mt9v022)
  780. return -ENOMEM;
  781. pdata = ssdd->drv_priv;
  782. v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
  783. v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
  784. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  785. V4L2_CID_VFLIP, 0, 1, 1, 0);
  786. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  787. V4L2_CID_HFLIP, 0, 1, 1, 0);
  788. mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  789. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  790. mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  791. V4L2_CID_GAIN, 0, 127, 1, 64);
  792. /*
  793. * Simulated autoexposure. If enabled, we calculate shutter width
  794. * ourselves in the driver based on vertical blanking and frame width
  795. */
  796. mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
  797. &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  798. V4L2_EXPOSURE_AUTO);
  799. mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  800. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  801. mt9v022->hblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  802. V4L2_CID_HBLANK, MT9V022_HORIZONTAL_BLANKING_MIN,
  803. MT9V022_HORIZONTAL_BLANKING_MAX, 1,
  804. MT9V022_HORIZONTAL_BLANKING_DEF);
  805. mt9v022->vblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  806. V4L2_CID_VBLANK, MT9V022_VERTICAL_BLANKING_MIN,
  807. MT9V022_VERTICAL_BLANKING_MAX, 1,
  808. MT9V022_VERTICAL_BLANKING_DEF);
  809. mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
  810. if (mt9v022->hdl.error) {
  811. int err = mt9v022->hdl.error;
  812. dev_err(&client->dev, "control initialisation err %d\n", err);
  813. return err;
  814. }
  815. v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
  816. V4L2_EXPOSURE_MANUAL, true);
  817. v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
  818. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  819. /*
  820. * On some platforms the first read out line is corrupted.
  821. * Workaround it by skipping if indicated by platform data.
  822. */
  823. mt9v022->y_skip_top = pdata ? pdata->y_skip_top : 0;
  824. mt9v022->rect.left = MT9V022_COLUMN_SKIP;
  825. mt9v022->rect.top = MT9V022_ROW_SKIP;
  826. mt9v022->rect.width = MT9V022_MAX_WIDTH;
  827. mt9v022->rect.height = MT9V022_MAX_HEIGHT;
  828. mt9v022->clk = v4l2_clk_get(&client->dev, "mclk");
  829. if (IS_ERR(mt9v022->clk)) {
  830. ret = PTR_ERR(mt9v022->clk);
  831. goto eclkget;
  832. }
  833. ret = mt9v022_video_probe(client);
  834. if (ret) {
  835. v4l2_clk_put(mt9v022->clk);
  836. eclkget:
  837. v4l2_ctrl_handler_free(&mt9v022->hdl);
  838. }
  839. return ret;
  840. }
  841. static int mt9v022_remove(struct i2c_client *client)
  842. {
  843. struct mt9v022 *mt9v022 = to_mt9v022(client);
  844. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  845. v4l2_clk_put(mt9v022->clk);
  846. v4l2_device_unregister_subdev(&mt9v022->subdev);
  847. if (ssdd->free_bus)
  848. ssdd->free_bus(ssdd);
  849. v4l2_ctrl_handler_free(&mt9v022->hdl);
  850. return 0;
  851. }
  852. static const struct i2c_device_id mt9v022_id[] = {
  853. { "mt9v022", 0 },
  854. { }
  855. };
  856. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  857. static struct i2c_driver mt9v022_i2c_driver = {
  858. .driver = {
  859. .name = "mt9v022",
  860. },
  861. .probe = mt9v022_probe,
  862. .remove = mt9v022_remove,
  863. .id_table = mt9v022_id,
  864. };
  865. module_i2c_driver(mt9v022_i2c_driver);
  866. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  867. MODULE_AUTHOR("Guennadi Liakhovetski <[email protected]>");
  868. MODULE_LICENSE("GPL");