mt9v022.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * Driver for MT9V022 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  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/mt9v022.h>
  17. #include <media/soc_camera.h>
  18. #include <media/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_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
  245. {
  246. struct i2c_client *client = v4l2_get_subdevdata(sd);
  247. struct mt9v022 *mt9v022 = to_mt9v022(client);
  248. struct v4l2_rect rect = a->c;
  249. int min_row, min_blank;
  250. int ret;
  251. /* Bayer format - even size lengths */
  252. if (mt9v022->fmts == mt9v022_colour_fmts) {
  253. rect.width = ALIGN(rect.width, 2);
  254. rect.height = ALIGN(rect.height, 2);
  255. /* Let the user play with the starting pixel */
  256. }
  257. soc_camera_limit_side(&rect.left, &rect.width,
  258. MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
  259. soc_camera_limit_side(&rect.top, &rect.height,
  260. MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
  261. /* Like in example app. Contradicts the datasheet though */
  262. ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  263. if (ret >= 0) {
  264. if (ret & 1) /* Autoexposure */
  265. ret = reg_write(client, mt9v022->reg->max_total_shutter_width,
  266. rect.height + mt9v022->y_skip_top + 43);
  267. /*
  268. * If autoexposure is off, there is no need to set
  269. * MT9V022_TOTAL_SHUTTER_WIDTH here. Autoexposure can be off
  270. * only if the user has set exposure manually, using the
  271. * V4L2_CID_EXPOSURE_AUTO with the value V4L2_EXPOSURE_MANUAL.
  272. * In this case the register MT9V022_TOTAL_SHUTTER_WIDTH
  273. * already contains the correct value.
  274. */
  275. }
  276. /* Setup frame format: defaults apart from width and height */
  277. if (!ret)
  278. ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
  279. if (!ret)
  280. ret = reg_write(client, MT9V022_ROW_START, rect.top);
  281. /*
  282. * mt9v022: min total row time is 660 columns, min blanking is 43
  283. * mt9v024: min total row time is 690 columns, min blanking is 61
  284. */
  285. if (is_mt9v024(mt9v022->chip_version)) {
  286. min_row = 690;
  287. min_blank = 61;
  288. } else {
  289. min_row = 660;
  290. min_blank = 43;
  291. }
  292. if (!ret)
  293. ret = v4l2_ctrl_s_ctrl(mt9v022->hblank,
  294. rect.width > min_row - min_blank ?
  295. min_blank : min_row - rect.width);
  296. if (!ret)
  297. ret = v4l2_ctrl_s_ctrl(mt9v022->vblank, 45);
  298. if (!ret)
  299. ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
  300. if (!ret)
  301. ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
  302. rect.height + mt9v022->y_skip_top);
  303. if (ret < 0)
  304. return ret;
  305. dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
  306. mt9v022->rect = rect;
  307. return 0;
  308. }
  309. static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  310. {
  311. struct i2c_client *client = v4l2_get_subdevdata(sd);
  312. struct mt9v022 *mt9v022 = to_mt9v022(client);
  313. a->c = mt9v022->rect;
  314. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  315. return 0;
  316. }
  317. static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  318. {
  319. a->bounds.left = MT9V022_COLUMN_SKIP;
  320. a->bounds.top = MT9V022_ROW_SKIP;
  321. a->bounds.width = MT9V022_MAX_WIDTH;
  322. a->bounds.height = MT9V022_MAX_HEIGHT;
  323. a->defrect = a->bounds;
  324. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  325. a->pixelaspect.numerator = 1;
  326. a->pixelaspect.denominator = 1;
  327. return 0;
  328. }
  329. static int mt9v022_get_fmt(struct v4l2_subdev *sd,
  330. struct v4l2_subdev_pad_config *cfg,
  331. struct v4l2_subdev_format *format)
  332. {
  333. struct v4l2_mbus_framefmt *mf = &format->format;
  334. struct i2c_client *client = v4l2_get_subdevdata(sd);
  335. struct mt9v022 *mt9v022 = to_mt9v022(client);
  336. if (format->pad)
  337. return -EINVAL;
  338. mf->width = mt9v022->rect.width;
  339. mf->height = mt9v022->rect.height;
  340. mf->code = mt9v022->fmt->code;
  341. mf->colorspace = mt9v022->fmt->colorspace;
  342. mf->field = V4L2_FIELD_NONE;
  343. return 0;
  344. }
  345. static int mt9v022_s_fmt(struct v4l2_subdev *sd,
  346. struct v4l2_mbus_framefmt *mf)
  347. {
  348. struct i2c_client *client = v4l2_get_subdevdata(sd);
  349. struct mt9v022 *mt9v022 = to_mt9v022(client);
  350. struct v4l2_crop a = {
  351. .c = {
  352. .left = mt9v022->rect.left,
  353. .top = mt9v022->rect.top,
  354. .width = mf->width,
  355. .height = mf->height,
  356. },
  357. };
  358. int ret;
  359. /*
  360. * The caller provides a supported format, as verified per call to
  361. * .set_fmt(FORMAT_TRY), datawidth is from our supported format list
  362. */
  363. switch (mf->code) {
  364. case MEDIA_BUS_FMT_Y8_1X8:
  365. case MEDIA_BUS_FMT_Y10_1X10:
  366. if (mt9v022->model != MT9V022IX7ATM)
  367. return -EINVAL;
  368. break;
  369. case MEDIA_BUS_FMT_SBGGR8_1X8:
  370. case MEDIA_BUS_FMT_SBGGR10_1X10:
  371. if (mt9v022->model != MT9V022IX7ATC)
  372. return -EINVAL;
  373. break;
  374. default:
  375. return -EINVAL;
  376. }
  377. /* No support for scaling on this camera, just crop. */
  378. ret = mt9v022_s_crop(sd, &a);
  379. if (!ret) {
  380. mf->width = mt9v022->rect.width;
  381. mf->height = mt9v022->rect.height;
  382. mt9v022->fmt = mt9v022_find_datafmt(mf->code,
  383. mt9v022->fmts, mt9v022->num_fmts);
  384. mf->colorspace = mt9v022->fmt->colorspace;
  385. }
  386. return ret;
  387. }
  388. static int mt9v022_set_fmt(struct v4l2_subdev *sd,
  389. struct v4l2_subdev_pad_config *cfg,
  390. struct v4l2_subdev_format *format)
  391. {
  392. struct v4l2_mbus_framefmt *mf = &format->format;
  393. struct i2c_client *client = v4l2_get_subdevdata(sd);
  394. struct mt9v022 *mt9v022 = to_mt9v022(client);
  395. const struct mt9v022_datafmt *fmt;
  396. int align = mf->code == MEDIA_BUS_FMT_SBGGR8_1X8 ||
  397. mf->code == MEDIA_BUS_FMT_SBGGR10_1X10;
  398. if (format->pad)
  399. return -EINVAL;
  400. v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
  401. MT9V022_MAX_WIDTH, align,
  402. &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
  403. MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
  404. fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
  405. mt9v022->num_fmts);
  406. if (!fmt) {
  407. fmt = mt9v022->fmt;
  408. mf->code = fmt->code;
  409. }
  410. mf->colorspace = fmt->colorspace;
  411. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  412. return mt9v022_s_fmt(sd, mf);
  413. cfg->try_fmt = *mf;
  414. return 0;
  415. }
  416. #ifdef CONFIG_VIDEO_ADV_DEBUG
  417. static int mt9v022_g_register(struct v4l2_subdev *sd,
  418. struct v4l2_dbg_register *reg)
  419. {
  420. struct i2c_client *client = v4l2_get_subdevdata(sd);
  421. if (reg->reg > 0xff)
  422. return -EINVAL;
  423. reg->size = 2;
  424. reg->val = reg_read(client, reg->reg);
  425. if (reg->val > 0xffff)
  426. return -EIO;
  427. return 0;
  428. }
  429. static int mt9v022_s_register(struct v4l2_subdev *sd,
  430. const struct v4l2_dbg_register *reg)
  431. {
  432. struct i2c_client *client = v4l2_get_subdevdata(sd);
  433. if (reg->reg > 0xff)
  434. return -EINVAL;
  435. if (reg_write(client, reg->reg, reg->val) < 0)
  436. return -EIO;
  437. return 0;
  438. }
  439. #endif
  440. static int mt9v022_s_power(struct v4l2_subdev *sd, int on)
  441. {
  442. struct i2c_client *client = v4l2_get_subdevdata(sd);
  443. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  444. struct mt9v022 *mt9v022 = to_mt9v022(client);
  445. return soc_camera_set_power(&client->dev, ssdd, mt9v022->clk, on);
  446. }
  447. static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  448. {
  449. struct mt9v022 *mt9v022 = container_of(ctrl->handler,
  450. struct mt9v022, hdl);
  451. struct v4l2_subdev *sd = &mt9v022->subdev;
  452. struct i2c_client *client = v4l2_get_subdevdata(sd);
  453. struct v4l2_ctrl *gain = mt9v022->gain;
  454. struct v4l2_ctrl *exp = mt9v022->exposure;
  455. unsigned long range;
  456. int data;
  457. switch (ctrl->id) {
  458. case V4L2_CID_AUTOGAIN:
  459. data = reg_read(client, MT9V022_ANALOG_GAIN);
  460. if (data < 0)
  461. return -EIO;
  462. range = gain->maximum - gain->minimum;
  463. gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
  464. return 0;
  465. case V4L2_CID_EXPOSURE_AUTO:
  466. data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
  467. if (data < 0)
  468. return -EIO;
  469. range = exp->maximum - exp->minimum;
  470. exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
  471. return 0;
  472. case V4L2_CID_HBLANK:
  473. data = reg_read(client, MT9V022_HORIZONTAL_BLANKING);
  474. if (data < 0)
  475. return -EIO;
  476. ctrl->val = data;
  477. return 0;
  478. case V4L2_CID_VBLANK:
  479. data = reg_read(client, MT9V022_VERTICAL_BLANKING);
  480. if (data < 0)
  481. return -EIO;
  482. ctrl->val = data;
  483. return 0;
  484. }
  485. return -EINVAL;
  486. }
  487. static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
  488. {
  489. struct mt9v022 *mt9v022 = container_of(ctrl->handler,
  490. struct mt9v022, hdl);
  491. struct v4l2_subdev *sd = &mt9v022->subdev;
  492. struct i2c_client *client = v4l2_get_subdevdata(sd);
  493. int data;
  494. switch (ctrl->id) {
  495. case V4L2_CID_VFLIP:
  496. if (ctrl->val)
  497. data = reg_set(client, MT9V022_READ_MODE, 0x10);
  498. else
  499. data = reg_clear(client, MT9V022_READ_MODE, 0x10);
  500. if (data < 0)
  501. return -EIO;
  502. return 0;
  503. case V4L2_CID_HFLIP:
  504. if (ctrl->val)
  505. data = reg_set(client, MT9V022_READ_MODE, 0x20);
  506. else
  507. data = reg_clear(client, MT9V022_READ_MODE, 0x20);
  508. if (data < 0)
  509. return -EIO;
  510. return 0;
  511. case V4L2_CID_AUTOGAIN:
  512. if (ctrl->val) {
  513. if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  514. return -EIO;
  515. } else {
  516. struct v4l2_ctrl *gain = mt9v022->gain;
  517. /* mt9v022 has minimum == default */
  518. unsigned long range = gain->maximum - gain->minimum;
  519. /* Valid values 16 to 64, 32 to 64 must be even. */
  520. unsigned long gain_val = ((gain->val - (s32)gain->minimum) *
  521. 48 + range / 2) / range + 16;
  522. if (gain_val >= 32)
  523. gain_val &= ~1;
  524. /*
  525. * The user wants to set gain manually, hope, she
  526. * knows, what she's doing... Switch AGC off.
  527. */
  528. if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  529. return -EIO;
  530. dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
  531. reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
  532. if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
  533. return -EIO;
  534. }
  535. return 0;
  536. case V4L2_CID_EXPOSURE_AUTO:
  537. if (ctrl->val == V4L2_EXPOSURE_AUTO) {
  538. data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  539. } else {
  540. struct v4l2_ctrl *exp = mt9v022->exposure;
  541. unsigned long range = exp->maximum - exp->minimum;
  542. unsigned long shutter = ((exp->val - (s32)exp->minimum) *
  543. 479 + range / 2) / range + 1;
  544. /*
  545. * The user wants to set shutter width manually, hope,
  546. * she knows, what she's doing... Switch AEC off.
  547. */
  548. data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  549. if (data < 0)
  550. return -EIO;
  551. dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
  552. reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
  553. shutter);
  554. if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
  555. shutter) < 0)
  556. return -EIO;
  557. }
  558. return 0;
  559. case V4L2_CID_HBLANK:
  560. if (reg_write(client, MT9V022_HORIZONTAL_BLANKING,
  561. ctrl->val) < 0)
  562. return -EIO;
  563. return 0;
  564. case V4L2_CID_VBLANK:
  565. if (reg_write(client, MT9V022_VERTICAL_BLANKING,
  566. ctrl->val) < 0)
  567. return -EIO;
  568. return 0;
  569. }
  570. return -EINVAL;
  571. }
  572. /*
  573. * Interface active, can use i2c. If it fails, it can indeed mean, that
  574. * this wasn't our capture interface, so, we wait for the right one
  575. */
  576. static int mt9v022_video_probe(struct i2c_client *client)
  577. {
  578. struct mt9v022 *mt9v022 = to_mt9v022(client);
  579. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  580. s32 data;
  581. int ret;
  582. unsigned long flags;
  583. ret = mt9v022_s_power(&mt9v022->subdev, 1);
  584. if (ret < 0)
  585. return ret;
  586. /* Read out the chip version register */
  587. data = reg_read(client, MT9V022_CHIP_VERSION);
  588. /* must be 0x1311, 0x1313 or 0x1324 */
  589. if (data != 0x1311 && data != 0x1313 && data != 0x1324) {
  590. ret = -ENODEV;
  591. dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
  592. data);
  593. goto ei2c;
  594. }
  595. mt9v022->chip_version = data;
  596. mt9v022->reg = is_mt9v024(data) ? &mt9v024_register :
  597. &mt9v022_register;
  598. /* Soft reset */
  599. ret = reg_write(client, MT9V022_RESET, 1);
  600. if (ret < 0)
  601. goto ei2c;
  602. /* 15 clock cycles */
  603. udelay(200);
  604. if (reg_read(client, MT9V022_RESET)) {
  605. dev_err(&client->dev, "Resetting MT9V022 failed!\n");
  606. if (ret > 0)
  607. ret = -EIO;
  608. goto ei2c;
  609. }
  610. /* Set monochrome or colour sensor type */
  611. if (sensor_type && (!strcmp("colour", sensor_type) ||
  612. !strcmp("color", sensor_type))) {
  613. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  614. mt9v022->model = MT9V022IX7ATC;
  615. mt9v022->fmts = mt9v022_colour_fmts;
  616. } else {
  617. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  618. mt9v022->model = MT9V022IX7ATM;
  619. mt9v022->fmts = mt9v022_monochrome_fmts;
  620. }
  621. if (ret < 0)
  622. goto ei2c;
  623. mt9v022->num_fmts = 0;
  624. /*
  625. * This is a 10bit sensor, so by default we only allow 10bit.
  626. * The platform may support different bus widths due to
  627. * different routing of the data lines.
  628. */
  629. if (ssdd->query_bus_param)
  630. flags = ssdd->query_bus_param(ssdd);
  631. else
  632. flags = SOCAM_DATAWIDTH_10;
  633. if (flags & SOCAM_DATAWIDTH_10)
  634. mt9v022->num_fmts++;
  635. else
  636. mt9v022->fmts++;
  637. if (flags & SOCAM_DATAWIDTH_8)
  638. mt9v022->num_fmts++;
  639. mt9v022->fmt = &mt9v022->fmts[0];
  640. dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  641. data, mt9v022->model == MT9V022IX7ATM ?
  642. "monochrome" : "colour");
  643. ret = mt9v022_init(client);
  644. if (ret < 0)
  645. dev_err(&client->dev, "Failed to initialise the camera\n");
  646. ei2c:
  647. mt9v022_s_power(&mt9v022->subdev, 0);
  648. return ret;
  649. }
  650. static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  651. {
  652. struct i2c_client *client = v4l2_get_subdevdata(sd);
  653. struct mt9v022 *mt9v022 = to_mt9v022(client);
  654. *lines = mt9v022->y_skip_top;
  655. return 0;
  656. }
  657. static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
  658. .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
  659. .s_ctrl = mt9v022_s_ctrl,
  660. };
  661. static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
  662. #ifdef CONFIG_VIDEO_ADV_DEBUG
  663. .g_register = mt9v022_g_register,
  664. .s_register = mt9v022_s_register,
  665. #endif
  666. .s_power = mt9v022_s_power,
  667. };
  668. static int mt9v022_enum_mbus_code(struct v4l2_subdev *sd,
  669. struct v4l2_subdev_pad_config *cfg,
  670. struct v4l2_subdev_mbus_code_enum *code)
  671. {
  672. struct i2c_client *client = v4l2_get_subdevdata(sd);
  673. struct mt9v022 *mt9v022 = to_mt9v022(client);
  674. if (code->pad || code->index >= mt9v022->num_fmts)
  675. return -EINVAL;
  676. code->code = mt9v022->fmts[code->index].code;
  677. return 0;
  678. }
  679. static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
  680. struct v4l2_mbus_config *cfg)
  681. {
  682. struct i2c_client *client = v4l2_get_subdevdata(sd);
  683. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  684. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
  685. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  686. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  687. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  688. V4L2_MBUS_DATA_ACTIVE_HIGH;
  689. cfg->type = V4L2_MBUS_PARALLEL;
  690. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  691. return 0;
  692. }
  693. static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
  694. const struct v4l2_mbus_config *cfg)
  695. {
  696. struct i2c_client *client = v4l2_get_subdevdata(sd);
  697. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  698. struct mt9v022 *mt9v022 = to_mt9v022(client);
  699. unsigned long flags = soc_camera_apply_board_flags(ssdd, cfg);
  700. unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
  701. int ret;
  702. u16 pixclk = 0;
  703. if (ssdd->set_bus_param) {
  704. ret = ssdd->set_bus_param(ssdd, 1 << (bps - 1));
  705. if (ret)
  706. return ret;
  707. } else if (bps != 10) {
  708. /*
  709. * Without board specific bus width settings we only support the
  710. * sensors native bus width
  711. */
  712. return -EINVAL;
  713. }
  714. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  715. pixclk |= 0x10;
  716. if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
  717. pixclk |= 0x1;
  718. if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
  719. pixclk |= 0x2;
  720. ret = reg_write(client, mt9v022->reg->pixclk_fv_lv, pixclk);
  721. if (ret < 0)
  722. return ret;
  723. if (!(flags & V4L2_MBUS_MASTER))
  724. mt9v022->chip_control &= ~0x8;
  725. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  726. if (ret < 0)
  727. return ret;
  728. dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  729. pixclk, mt9v022->chip_control);
  730. return 0;
  731. }
  732. static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
  733. .s_stream = mt9v022_s_stream,
  734. .s_crop = mt9v022_s_crop,
  735. .g_crop = mt9v022_g_crop,
  736. .cropcap = mt9v022_cropcap,
  737. .g_mbus_config = mt9v022_g_mbus_config,
  738. .s_mbus_config = mt9v022_s_mbus_config,
  739. };
  740. static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
  741. .g_skip_top_lines = mt9v022_g_skip_top_lines,
  742. };
  743. static const struct v4l2_subdev_pad_ops mt9v022_subdev_pad_ops = {
  744. .enum_mbus_code = mt9v022_enum_mbus_code,
  745. .get_fmt = mt9v022_get_fmt,
  746. .set_fmt = mt9v022_set_fmt,
  747. };
  748. static struct v4l2_subdev_ops mt9v022_subdev_ops = {
  749. .core = &mt9v022_subdev_core_ops,
  750. .video = &mt9v022_subdev_video_ops,
  751. .sensor = &mt9v022_subdev_sensor_ops,
  752. .pad = &mt9v022_subdev_pad_ops,
  753. };
  754. static int mt9v022_probe(struct i2c_client *client,
  755. const struct i2c_device_id *did)
  756. {
  757. struct mt9v022 *mt9v022;
  758. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  759. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  760. struct mt9v022_platform_data *pdata;
  761. int ret;
  762. if (!ssdd) {
  763. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  764. return -EINVAL;
  765. }
  766. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  767. dev_warn(&adapter->dev,
  768. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  769. return -EIO;
  770. }
  771. mt9v022 = devm_kzalloc(&client->dev, sizeof(struct mt9v022), GFP_KERNEL);
  772. if (!mt9v022)
  773. return -ENOMEM;
  774. pdata = ssdd->drv_priv;
  775. v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
  776. v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
  777. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  778. V4L2_CID_VFLIP, 0, 1, 1, 0);
  779. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  780. V4L2_CID_HFLIP, 0, 1, 1, 0);
  781. mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  782. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  783. mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  784. V4L2_CID_GAIN, 0, 127, 1, 64);
  785. /*
  786. * Simulated autoexposure. If enabled, we calculate shutter width
  787. * ourselves in the driver based on vertical blanking and frame width
  788. */
  789. mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
  790. &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  791. V4L2_EXPOSURE_AUTO);
  792. mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  793. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  794. mt9v022->hblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  795. V4L2_CID_HBLANK, MT9V022_HORIZONTAL_BLANKING_MIN,
  796. MT9V022_HORIZONTAL_BLANKING_MAX, 1,
  797. MT9V022_HORIZONTAL_BLANKING_DEF);
  798. mt9v022->vblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  799. V4L2_CID_VBLANK, MT9V022_VERTICAL_BLANKING_MIN,
  800. MT9V022_VERTICAL_BLANKING_MAX, 1,
  801. MT9V022_VERTICAL_BLANKING_DEF);
  802. mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
  803. if (mt9v022->hdl.error) {
  804. int err = mt9v022->hdl.error;
  805. dev_err(&client->dev, "control initialisation err %d\n", err);
  806. return err;
  807. }
  808. v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
  809. V4L2_EXPOSURE_MANUAL, true);
  810. v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
  811. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  812. /*
  813. * On some platforms the first read out line is corrupted.
  814. * Workaround it by skipping if indicated by platform data.
  815. */
  816. mt9v022->y_skip_top = pdata ? pdata->y_skip_top : 0;
  817. mt9v022->rect.left = MT9V022_COLUMN_SKIP;
  818. mt9v022->rect.top = MT9V022_ROW_SKIP;
  819. mt9v022->rect.width = MT9V022_MAX_WIDTH;
  820. mt9v022->rect.height = MT9V022_MAX_HEIGHT;
  821. mt9v022->clk = v4l2_clk_get(&client->dev, "mclk");
  822. if (IS_ERR(mt9v022->clk)) {
  823. ret = PTR_ERR(mt9v022->clk);
  824. goto eclkget;
  825. }
  826. ret = mt9v022_video_probe(client);
  827. if (ret) {
  828. v4l2_clk_put(mt9v022->clk);
  829. eclkget:
  830. v4l2_ctrl_handler_free(&mt9v022->hdl);
  831. }
  832. return ret;
  833. }
  834. static int mt9v022_remove(struct i2c_client *client)
  835. {
  836. struct mt9v022 *mt9v022 = to_mt9v022(client);
  837. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  838. v4l2_clk_put(mt9v022->clk);
  839. v4l2_device_unregister_subdev(&mt9v022->subdev);
  840. if (ssdd->free_bus)
  841. ssdd->free_bus(ssdd);
  842. v4l2_ctrl_handler_free(&mt9v022->hdl);
  843. return 0;
  844. }
  845. static const struct i2c_device_id mt9v022_id[] = {
  846. { "mt9v022", 0 },
  847. { }
  848. };
  849. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  850. static struct i2c_driver mt9v022_i2c_driver = {
  851. .driver = {
  852. .name = "mt9v022",
  853. },
  854. .probe = mt9v022_probe,
  855. .remove = mt9v022_remove,
  856. .id_table = mt9v022_id,
  857. };
  858. module_i2c_driver(mt9v022_i2c_driver);
  859. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  860. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  861. MODULE_LICENSE("GPL");