mt9v032.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * Driver for MT9V032 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  5. *
  6. * Based on the MT9M001 driver,
  7. *
  8. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/log2.h>
  17. #include <linux/mutex.h>
  18. #include <linux/slab.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/v4l2-mediabus.h>
  21. #include <linux/module.h>
  22. #include <media/mt9v032.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-subdev.h>
  26. #define MT9V032_PIXEL_ARRAY_HEIGHT 492
  27. #define MT9V032_PIXEL_ARRAY_WIDTH 782
  28. #define MT9V032_SYSCLK_FREQ_DEF 26600000
  29. #define MT9V032_CHIP_VERSION 0x00
  30. #define MT9V032_CHIP_ID_REV1 0x1311
  31. #define MT9V032_CHIP_ID_REV3 0x1313
  32. #define MT9V032_COLUMN_START 0x01
  33. #define MT9V032_COLUMN_START_MIN 1
  34. #define MT9V032_COLUMN_START_DEF 1
  35. #define MT9V032_COLUMN_START_MAX 752
  36. #define MT9V032_ROW_START 0x02
  37. #define MT9V032_ROW_START_MIN 4
  38. #define MT9V032_ROW_START_DEF 5
  39. #define MT9V032_ROW_START_MAX 482
  40. #define MT9V032_WINDOW_HEIGHT 0x03
  41. #define MT9V032_WINDOW_HEIGHT_MIN 1
  42. #define MT9V032_WINDOW_HEIGHT_DEF 480
  43. #define MT9V032_WINDOW_HEIGHT_MAX 480
  44. #define MT9V032_WINDOW_WIDTH 0x04
  45. #define MT9V032_WINDOW_WIDTH_MIN 1
  46. #define MT9V032_WINDOW_WIDTH_DEF 752
  47. #define MT9V032_WINDOW_WIDTH_MAX 752
  48. #define MT9V032_HORIZONTAL_BLANKING 0x05
  49. #define MT9V032_HORIZONTAL_BLANKING_MIN 43
  50. #define MT9V032_HORIZONTAL_BLANKING_MAX 1023
  51. #define MT9V032_VERTICAL_BLANKING 0x06
  52. #define MT9V032_VERTICAL_BLANKING_MIN 4
  53. #define MT9V032_VERTICAL_BLANKING_MAX 3000
  54. #define MT9V032_CHIP_CONTROL 0x07
  55. #define MT9V032_CHIP_CONTROL_MASTER_MODE (1 << 3)
  56. #define MT9V032_CHIP_CONTROL_DOUT_ENABLE (1 << 7)
  57. #define MT9V032_CHIP_CONTROL_SEQUENTIAL (1 << 8)
  58. #define MT9V032_SHUTTER_WIDTH1 0x08
  59. #define MT9V032_SHUTTER_WIDTH2 0x09
  60. #define MT9V032_SHUTTER_WIDTH_CONTROL 0x0a
  61. #define MT9V032_TOTAL_SHUTTER_WIDTH 0x0b
  62. #define MT9V032_TOTAL_SHUTTER_WIDTH_MIN 1
  63. #define MT9V032_TOTAL_SHUTTER_WIDTH_DEF 480
  64. #define MT9V032_TOTAL_SHUTTER_WIDTH_MAX 32767
  65. #define MT9V032_RESET 0x0c
  66. #define MT9V032_READ_MODE 0x0d
  67. #define MT9V032_READ_MODE_ROW_BIN_MASK (3 << 0)
  68. #define MT9V032_READ_MODE_ROW_BIN_SHIFT 0
  69. #define MT9V032_READ_MODE_COLUMN_BIN_MASK (3 << 2)
  70. #define MT9V032_READ_MODE_COLUMN_BIN_SHIFT 2
  71. #define MT9V032_READ_MODE_ROW_FLIP (1 << 4)
  72. #define MT9V032_READ_MODE_COLUMN_FLIP (1 << 5)
  73. #define MT9V032_READ_MODE_DARK_COLUMNS (1 << 6)
  74. #define MT9V032_READ_MODE_DARK_ROWS (1 << 7)
  75. #define MT9V032_PIXEL_OPERATION_MODE 0x0f
  76. #define MT9V032_PIXEL_OPERATION_MODE_COLOR (1 << 2)
  77. #define MT9V032_PIXEL_OPERATION_MODE_HDR (1 << 6)
  78. #define MT9V032_ANALOG_GAIN 0x35
  79. #define MT9V032_ANALOG_GAIN_MIN 16
  80. #define MT9V032_ANALOG_GAIN_DEF 16
  81. #define MT9V032_ANALOG_GAIN_MAX 64
  82. #define MT9V032_MAX_ANALOG_GAIN 0x36
  83. #define MT9V032_MAX_ANALOG_GAIN_MAX 127
  84. #define MT9V032_FRAME_DARK_AVERAGE 0x42
  85. #define MT9V032_DARK_AVG_THRESH 0x46
  86. #define MT9V032_DARK_AVG_LOW_THRESH_MASK (255 << 0)
  87. #define MT9V032_DARK_AVG_LOW_THRESH_SHIFT 0
  88. #define MT9V032_DARK_AVG_HIGH_THRESH_MASK (255 << 8)
  89. #define MT9V032_DARK_AVG_HIGH_THRESH_SHIFT 8
  90. #define MT9V032_ROW_NOISE_CORR_CONTROL 0x70
  91. #define MT9V032_ROW_NOISE_CORR_ENABLE (1 << 5)
  92. #define MT9V032_ROW_NOISE_CORR_USE_BLK_AVG (1 << 7)
  93. #define MT9V032_PIXEL_CLOCK 0x74
  94. #define MT9V032_PIXEL_CLOCK_INV_LINE (1 << 0)
  95. #define MT9V032_PIXEL_CLOCK_INV_FRAME (1 << 1)
  96. #define MT9V032_PIXEL_CLOCK_XOR_LINE (1 << 2)
  97. #define MT9V032_PIXEL_CLOCK_CONT_LINE (1 << 3)
  98. #define MT9V032_PIXEL_CLOCK_INV_PXL_CLK (1 << 4)
  99. #define MT9V032_TEST_PATTERN 0x7f
  100. #define MT9V032_TEST_PATTERN_DATA_MASK (1023 << 0)
  101. #define MT9V032_TEST_PATTERN_DATA_SHIFT 0
  102. #define MT9V032_TEST_PATTERN_USE_DATA (1 << 10)
  103. #define MT9V032_TEST_PATTERN_GRAY_MASK (3 << 11)
  104. #define MT9V032_TEST_PATTERN_GRAY_NONE (0 << 11)
  105. #define MT9V032_TEST_PATTERN_GRAY_VERTICAL (1 << 11)
  106. #define MT9V032_TEST_PATTERN_GRAY_HORIZONTAL (2 << 11)
  107. #define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11)
  108. #define MT9V032_TEST_PATTERN_ENABLE (1 << 13)
  109. #define MT9V032_TEST_PATTERN_FLIP (1 << 14)
  110. #define MT9V032_AEC_AGC_ENABLE 0xaf
  111. #define MT9V032_AEC_ENABLE (1 << 0)
  112. #define MT9V032_AGC_ENABLE (1 << 1)
  113. #define MT9V032_THERMAL_INFO 0xc1
  114. struct mt9v032 {
  115. struct v4l2_subdev subdev;
  116. struct media_pad pad;
  117. struct v4l2_mbus_framefmt format;
  118. struct v4l2_rect crop;
  119. struct v4l2_ctrl_handler ctrls;
  120. struct {
  121. struct v4l2_ctrl *link_freq;
  122. struct v4l2_ctrl *pixel_rate;
  123. };
  124. struct mutex power_lock;
  125. int power_count;
  126. struct mt9v032_platform_data *pdata;
  127. u32 sysclk;
  128. u16 chip_control;
  129. u16 aec_agc;
  130. };
  131. static struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
  132. {
  133. return container_of(sd, struct mt9v032, subdev);
  134. }
  135. static int mt9v032_read(struct i2c_client *client, const u8 reg)
  136. {
  137. s32 data = i2c_smbus_read_word_swapped(client, reg);
  138. dev_dbg(&client->dev, "%s: read 0x%04x from 0x%02x\n", __func__,
  139. data, reg);
  140. return data;
  141. }
  142. static int mt9v032_write(struct i2c_client *client, const u8 reg,
  143. const u16 data)
  144. {
  145. dev_dbg(&client->dev, "%s: writing 0x%04x to 0x%02x\n", __func__,
  146. data, reg);
  147. return i2c_smbus_write_word_swapped(client, reg, data);
  148. }
  149. static int mt9v032_set_chip_control(struct mt9v032 *mt9v032, u16 clear, u16 set)
  150. {
  151. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  152. u16 value = (mt9v032->chip_control & ~clear) | set;
  153. int ret;
  154. ret = mt9v032_write(client, MT9V032_CHIP_CONTROL, value);
  155. if (ret < 0)
  156. return ret;
  157. mt9v032->chip_control = value;
  158. return 0;
  159. }
  160. static int
  161. mt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
  162. {
  163. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  164. u16 value = mt9v032->aec_agc;
  165. int ret;
  166. if (enable)
  167. value |= which;
  168. else
  169. value &= ~which;
  170. ret = mt9v032_write(client, MT9V032_AEC_AGC_ENABLE, value);
  171. if (ret < 0)
  172. return ret;
  173. mt9v032->aec_agc = value;
  174. return 0;
  175. }
  176. #define EXT_CLK 25000000
  177. static int mt9v032_power_on(struct mt9v032 *mt9v032)
  178. {
  179. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  180. int ret;
  181. if (mt9v032->pdata->set_clock) {
  182. mt9v032->pdata->set_clock(&mt9v032->subdev, mt9v032->sysclk);
  183. udelay(1);
  184. }
  185. /* Reset the chip and stop data read out */
  186. ret = mt9v032_write(client, MT9V032_RESET, 1);
  187. if (ret < 0)
  188. return ret;
  189. ret = mt9v032_write(client, MT9V032_RESET, 0);
  190. if (ret < 0)
  191. return ret;
  192. return mt9v032_write(client, MT9V032_CHIP_CONTROL, 0);
  193. }
  194. static void mt9v032_power_off(struct mt9v032 *mt9v032)
  195. {
  196. if (mt9v032->pdata->set_clock)
  197. mt9v032->pdata->set_clock(&mt9v032->subdev, 0);
  198. }
  199. static int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
  200. {
  201. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  202. int ret;
  203. if (!on) {
  204. mt9v032_power_off(mt9v032);
  205. return 0;
  206. }
  207. ret = mt9v032_power_on(mt9v032);
  208. if (ret < 0)
  209. return ret;
  210. /* Configure the pixel clock polarity */
  211. if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
  212. ret = mt9v032_write(client, MT9V032_PIXEL_CLOCK,
  213. MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
  214. if (ret < 0)
  215. return ret;
  216. }
  217. /* Disable the noise correction algorithm and restore the controls. */
  218. ret = mt9v032_write(client, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
  219. if (ret < 0)
  220. return ret;
  221. return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
  222. }
  223. /* -----------------------------------------------------------------------------
  224. * V4L2 subdev video operations
  225. */
  226. static struct v4l2_mbus_framefmt *
  227. __mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
  228. unsigned int pad, enum v4l2_subdev_format_whence which)
  229. {
  230. switch (which) {
  231. case V4L2_SUBDEV_FORMAT_TRY:
  232. return v4l2_subdev_get_try_format(fh, pad);
  233. case V4L2_SUBDEV_FORMAT_ACTIVE:
  234. return &mt9v032->format;
  235. default:
  236. return NULL;
  237. }
  238. }
  239. static struct v4l2_rect *
  240. __mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
  241. unsigned int pad, enum v4l2_subdev_format_whence which)
  242. {
  243. switch (which) {
  244. case V4L2_SUBDEV_FORMAT_TRY:
  245. return v4l2_subdev_get_try_crop(fh, pad);
  246. case V4L2_SUBDEV_FORMAT_ACTIVE:
  247. return &mt9v032->crop;
  248. default:
  249. return NULL;
  250. }
  251. }
  252. static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
  253. {
  254. const u16 mode = MT9V032_CHIP_CONTROL_MASTER_MODE
  255. | MT9V032_CHIP_CONTROL_DOUT_ENABLE
  256. | MT9V032_CHIP_CONTROL_SEQUENTIAL;
  257. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  258. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  259. struct v4l2_mbus_framefmt *format = &mt9v032->format;
  260. struct v4l2_rect *crop = &mt9v032->crop;
  261. unsigned int hratio;
  262. unsigned int vratio;
  263. int ret;
  264. if (!enable)
  265. return mt9v032_set_chip_control(mt9v032, mode, 0);
  266. /* Configure the window size and row/column bin */
  267. hratio = DIV_ROUND_CLOSEST(crop->width, format->width);
  268. vratio = DIV_ROUND_CLOSEST(crop->height, format->height);
  269. ret = mt9v032_write(client, MT9V032_READ_MODE,
  270. (hratio - 1) << MT9V032_READ_MODE_ROW_BIN_SHIFT |
  271. (vratio - 1) << MT9V032_READ_MODE_COLUMN_BIN_SHIFT);
  272. if (ret < 0)
  273. return ret;
  274. ret = mt9v032_write(client, MT9V032_COLUMN_START, crop->left);
  275. if (ret < 0)
  276. return ret;
  277. ret = mt9v032_write(client, MT9V032_ROW_START, crop->top);
  278. if (ret < 0)
  279. return ret;
  280. ret = mt9v032_write(client, MT9V032_WINDOW_WIDTH, crop->width);
  281. if (ret < 0)
  282. return ret;
  283. ret = mt9v032_write(client, MT9V032_WINDOW_HEIGHT, crop->height);
  284. if (ret < 0)
  285. return ret;
  286. ret = mt9v032_write(client, MT9V032_HORIZONTAL_BLANKING,
  287. max(43, 660 - crop->width));
  288. if (ret < 0)
  289. return ret;
  290. /* Switch to master "normal" mode */
  291. return mt9v032_set_chip_control(mt9v032, 0, mode);
  292. }
  293. static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
  294. struct v4l2_subdev_fh *fh,
  295. struct v4l2_subdev_mbus_code_enum *code)
  296. {
  297. if (code->index > 0)
  298. return -EINVAL;
  299. code->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  300. return 0;
  301. }
  302. static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
  303. struct v4l2_subdev_fh *fh,
  304. struct v4l2_subdev_frame_size_enum *fse)
  305. {
  306. if (fse->index >= 8 || fse->code != V4L2_MBUS_FMT_SGRBG10_1X10)
  307. return -EINVAL;
  308. fse->min_width = MT9V032_WINDOW_WIDTH_DEF / fse->index;
  309. fse->max_width = fse->min_width;
  310. fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / fse->index;
  311. fse->max_height = fse->min_height;
  312. return 0;
  313. }
  314. static int mt9v032_get_format(struct v4l2_subdev *subdev,
  315. struct v4l2_subdev_fh *fh,
  316. struct v4l2_subdev_format *format)
  317. {
  318. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  319. format->format = *__mt9v032_get_pad_format(mt9v032, fh, format->pad,
  320. format->which);
  321. return 0;
  322. }
  323. static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032,
  324. unsigned int hratio)
  325. {
  326. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  327. int ret;
  328. ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
  329. mt9v032->sysclk / hratio);
  330. if (ret < 0)
  331. dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
  332. }
  333. static int mt9v032_set_format(struct v4l2_subdev *subdev,
  334. struct v4l2_subdev_fh *fh,
  335. struct v4l2_subdev_format *format)
  336. {
  337. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  338. struct v4l2_mbus_framefmt *__format;
  339. struct v4l2_rect *__crop;
  340. unsigned int width;
  341. unsigned int height;
  342. unsigned int hratio;
  343. unsigned int vratio;
  344. __crop = __mt9v032_get_pad_crop(mt9v032, fh, format->pad,
  345. format->which);
  346. /* Clamp the width and height to avoid dividing by zero. */
  347. width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
  348. max(__crop->width / 8, MT9V032_WINDOW_WIDTH_MIN),
  349. __crop->width);
  350. height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
  351. max(__crop->height / 8, MT9V032_WINDOW_HEIGHT_MIN),
  352. __crop->height);
  353. hratio = DIV_ROUND_CLOSEST(__crop->width, width);
  354. vratio = DIV_ROUND_CLOSEST(__crop->height, height);
  355. __format = __mt9v032_get_pad_format(mt9v032, fh, format->pad,
  356. format->which);
  357. __format->width = __crop->width / hratio;
  358. __format->height = __crop->height / vratio;
  359. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  360. mt9v032_configure_pixel_rate(mt9v032, hratio);
  361. format->format = *__format;
  362. return 0;
  363. }
  364. static int mt9v032_get_crop(struct v4l2_subdev *subdev,
  365. struct v4l2_subdev_fh *fh,
  366. struct v4l2_subdev_crop *crop)
  367. {
  368. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  369. crop->rect = *__mt9v032_get_pad_crop(mt9v032, fh, crop->pad,
  370. crop->which);
  371. return 0;
  372. }
  373. static int mt9v032_set_crop(struct v4l2_subdev *subdev,
  374. struct v4l2_subdev_fh *fh,
  375. struct v4l2_subdev_crop *crop)
  376. {
  377. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  378. struct v4l2_mbus_framefmt *__format;
  379. struct v4l2_rect *__crop;
  380. struct v4l2_rect rect;
  381. /* Clamp the crop rectangle boundaries and align them to a non multiple
  382. * of 2 pixels to ensure a GRBG Bayer pattern.
  383. */
  384. rect.left = clamp(ALIGN(crop->rect.left + 1, 2) - 1,
  385. MT9V032_COLUMN_START_MIN,
  386. MT9V032_COLUMN_START_MAX);
  387. rect.top = clamp(ALIGN(crop->rect.top + 1, 2) - 1,
  388. MT9V032_ROW_START_MIN,
  389. MT9V032_ROW_START_MAX);
  390. rect.width = clamp(ALIGN(crop->rect.width, 2),
  391. MT9V032_WINDOW_WIDTH_MIN,
  392. MT9V032_WINDOW_WIDTH_MAX);
  393. rect.height = clamp(ALIGN(crop->rect.height, 2),
  394. MT9V032_WINDOW_HEIGHT_MIN,
  395. MT9V032_WINDOW_HEIGHT_MAX);
  396. rect.width = min(rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
  397. rect.height = min(rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
  398. __crop = __mt9v032_get_pad_crop(mt9v032, fh, crop->pad, crop->which);
  399. if (rect.width != __crop->width || rect.height != __crop->height) {
  400. /* Reset the output image size if the crop rectangle size has
  401. * been modified.
  402. */
  403. __format = __mt9v032_get_pad_format(mt9v032, fh, crop->pad,
  404. crop->which);
  405. __format->width = rect.width;
  406. __format->height = rect.height;
  407. if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  408. mt9v032_configure_pixel_rate(mt9v032, 1);
  409. }
  410. *__crop = rect;
  411. crop->rect = rect;
  412. return 0;
  413. }
  414. /* -----------------------------------------------------------------------------
  415. * V4L2 subdev control operations
  416. */
  417. #define V4L2_CID_TEST_PATTERN (V4L2_CID_USER_BASE | 0x1001)
  418. static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
  419. {
  420. struct mt9v032 *mt9v032 =
  421. container_of(ctrl->handler, struct mt9v032, ctrls);
  422. struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
  423. u32 freq;
  424. u16 data;
  425. switch (ctrl->id) {
  426. case V4L2_CID_AUTOGAIN:
  427. return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
  428. ctrl->val);
  429. case V4L2_CID_GAIN:
  430. return mt9v032_write(client, MT9V032_ANALOG_GAIN, ctrl->val);
  431. case V4L2_CID_EXPOSURE_AUTO:
  432. return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
  433. !ctrl->val);
  434. case V4L2_CID_EXPOSURE:
  435. return mt9v032_write(client, MT9V032_TOTAL_SHUTTER_WIDTH,
  436. ctrl->val);
  437. case V4L2_CID_PIXEL_RATE:
  438. case V4L2_CID_LINK_FREQ:
  439. if (mt9v032->link_freq == NULL)
  440. break;
  441. freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val];
  442. mt9v032->pixel_rate->val64 = freq;
  443. mt9v032->sysclk = freq;
  444. break;
  445. case V4L2_CID_TEST_PATTERN:
  446. switch (ctrl->val) {
  447. case 0:
  448. data = 0;
  449. break;
  450. case 1:
  451. data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
  452. | MT9V032_TEST_PATTERN_ENABLE;
  453. break;
  454. case 2:
  455. data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
  456. | MT9V032_TEST_PATTERN_ENABLE;
  457. break;
  458. case 3:
  459. data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
  460. | MT9V032_TEST_PATTERN_ENABLE;
  461. break;
  462. default:
  463. data = (ctrl->val << MT9V032_TEST_PATTERN_DATA_SHIFT)
  464. | MT9V032_TEST_PATTERN_USE_DATA
  465. | MT9V032_TEST_PATTERN_ENABLE
  466. | MT9V032_TEST_PATTERN_FLIP;
  467. break;
  468. }
  469. return mt9v032_write(client, MT9V032_TEST_PATTERN, data);
  470. }
  471. return 0;
  472. }
  473. static struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
  474. .s_ctrl = mt9v032_s_ctrl,
  475. };
  476. static const struct v4l2_ctrl_config mt9v032_ctrls[] = {
  477. {
  478. .ops = &mt9v032_ctrl_ops,
  479. .id = V4L2_CID_TEST_PATTERN,
  480. .type = V4L2_CTRL_TYPE_INTEGER,
  481. .name = "Test pattern",
  482. .min = 0,
  483. .max = 1023,
  484. .step = 1,
  485. .def = 0,
  486. .flags = 0,
  487. }
  488. };
  489. /* -----------------------------------------------------------------------------
  490. * V4L2 subdev core operations
  491. */
  492. static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
  493. {
  494. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  495. int ret = 0;
  496. mutex_lock(&mt9v032->power_lock);
  497. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  498. * update the power state.
  499. */
  500. if (mt9v032->power_count == !on) {
  501. ret = __mt9v032_set_power(mt9v032, !!on);
  502. if (ret < 0)
  503. goto done;
  504. }
  505. /* Update the power count. */
  506. mt9v032->power_count += on ? 1 : -1;
  507. WARN_ON(mt9v032->power_count < 0);
  508. done:
  509. mutex_unlock(&mt9v032->power_lock);
  510. return ret;
  511. }
  512. /* -----------------------------------------------------------------------------
  513. * V4L2 subdev internal operations
  514. */
  515. static int mt9v032_registered(struct v4l2_subdev *subdev)
  516. {
  517. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  518. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  519. s32 data;
  520. int ret;
  521. dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
  522. client->addr);
  523. ret = mt9v032_power_on(mt9v032);
  524. if (ret < 0) {
  525. dev_err(&client->dev, "MT9V032 power up failed\n");
  526. return ret;
  527. }
  528. /* Read and check the sensor version */
  529. data = mt9v032_read(client, MT9V032_CHIP_VERSION);
  530. if (data != MT9V032_CHIP_ID_REV1 && data != MT9V032_CHIP_ID_REV3) {
  531. dev_err(&client->dev, "MT9V032 not detected, wrong version "
  532. "0x%04x\n", data);
  533. return -ENODEV;
  534. }
  535. mt9v032_power_off(mt9v032);
  536. dev_info(&client->dev, "MT9V032 detected at address 0x%02x\n",
  537. client->addr);
  538. mt9v032_configure_pixel_rate(mt9v032, 1);
  539. return ret;
  540. }
  541. static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  542. {
  543. struct v4l2_mbus_framefmt *format;
  544. struct v4l2_rect *crop;
  545. crop = v4l2_subdev_get_try_crop(fh, 0);
  546. crop->left = MT9V032_COLUMN_START_DEF;
  547. crop->top = MT9V032_ROW_START_DEF;
  548. crop->width = MT9V032_WINDOW_WIDTH_DEF;
  549. crop->height = MT9V032_WINDOW_HEIGHT_DEF;
  550. format = v4l2_subdev_get_try_format(fh, 0);
  551. format->code = V4L2_MBUS_FMT_SGRBG10_1X10;
  552. format->width = MT9V032_WINDOW_WIDTH_DEF;
  553. format->height = MT9V032_WINDOW_HEIGHT_DEF;
  554. format->field = V4L2_FIELD_NONE;
  555. format->colorspace = V4L2_COLORSPACE_SRGB;
  556. return mt9v032_set_power(subdev, 1);
  557. }
  558. static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  559. {
  560. return mt9v032_set_power(subdev, 0);
  561. }
  562. static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
  563. .s_power = mt9v032_set_power,
  564. };
  565. static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
  566. .s_stream = mt9v032_s_stream,
  567. };
  568. static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
  569. .enum_mbus_code = mt9v032_enum_mbus_code,
  570. .enum_frame_size = mt9v032_enum_frame_size,
  571. .get_fmt = mt9v032_get_format,
  572. .set_fmt = mt9v032_set_format,
  573. .get_crop = mt9v032_get_crop,
  574. .set_crop = mt9v032_set_crop,
  575. };
  576. static struct v4l2_subdev_ops mt9v032_subdev_ops = {
  577. .core = &mt9v032_subdev_core_ops,
  578. .video = &mt9v032_subdev_video_ops,
  579. .pad = &mt9v032_subdev_pad_ops,
  580. };
  581. static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
  582. .registered = mt9v032_registered,
  583. .open = mt9v032_open,
  584. .close = mt9v032_close,
  585. };
  586. /* -----------------------------------------------------------------------------
  587. * Driver initialization and probing
  588. */
  589. static int mt9v032_probe(struct i2c_client *client,
  590. const struct i2c_device_id *did)
  591. {
  592. struct mt9v032_platform_data *pdata = client->dev.platform_data;
  593. struct mt9v032 *mt9v032;
  594. unsigned int i;
  595. int ret;
  596. if (!i2c_check_functionality(client->adapter,
  597. I2C_FUNC_SMBUS_WORD_DATA)) {
  598. dev_warn(&client->adapter->dev,
  599. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  600. return -EIO;
  601. }
  602. mt9v032 = kzalloc(sizeof(*mt9v032), GFP_KERNEL);
  603. if (!mt9v032)
  604. return -ENOMEM;
  605. mutex_init(&mt9v032->power_lock);
  606. mt9v032->pdata = pdata;
  607. v4l2_ctrl_handler_init(&mt9v032->ctrls, ARRAY_SIZE(mt9v032_ctrls) + 6);
  608. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  609. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  610. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  611. V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
  612. MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
  613. v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  614. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
  615. V4L2_EXPOSURE_AUTO);
  616. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  617. V4L2_CID_EXPOSURE, MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
  618. MT9V032_TOTAL_SHUTTER_WIDTH_MAX, 1,
  619. MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
  620. mt9v032->pixel_rate =
  621. v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
  622. V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
  623. if (pdata && pdata->link_freqs) {
  624. unsigned int def = 0;
  625. for (i = 0; pdata->link_freqs[i]; ++i) {
  626. if (pdata->link_freqs[i] == pdata->link_def_freq)
  627. def = i;
  628. }
  629. mt9v032->link_freq =
  630. v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
  631. &mt9v032_ctrl_ops,
  632. V4L2_CID_LINK_FREQ, i - 1, def,
  633. pdata->link_freqs);
  634. v4l2_ctrl_cluster(2, &mt9v032->link_freq);
  635. }
  636. for (i = 0; i < ARRAY_SIZE(mt9v032_ctrls); ++i)
  637. v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_ctrls[i], NULL);
  638. mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
  639. if (mt9v032->ctrls.error)
  640. printk(KERN_INFO "%s: control initialization error %d\n",
  641. __func__, mt9v032->ctrls.error);
  642. mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
  643. mt9v032->crop.top = MT9V032_ROW_START_DEF;
  644. mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
  645. mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
  646. mt9v032->format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  647. mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
  648. mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
  649. mt9v032->format.field = V4L2_FIELD_NONE;
  650. mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
  651. mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
  652. mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
  653. v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
  654. mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
  655. mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  656. mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
  657. ret = media_entity_init(&mt9v032->subdev.entity, 1, &mt9v032->pad, 0);
  658. if (ret < 0)
  659. kfree(mt9v032);
  660. return ret;
  661. }
  662. static int mt9v032_remove(struct i2c_client *client)
  663. {
  664. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  665. struct mt9v032 *mt9v032 = to_mt9v032(subdev);
  666. v4l2_device_unregister_subdev(subdev);
  667. media_entity_cleanup(&subdev->entity);
  668. kfree(mt9v032);
  669. return 0;
  670. }
  671. static const struct i2c_device_id mt9v032_id[] = {
  672. { "mt9v032", 0 },
  673. { }
  674. };
  675. MODULE_DEVICE_TABLE(i2c, mt9v032_id);
  676. static struct i2c_driver mt9v032_driver = {
  677. .driver = {
  678. .name = "mt9v032",
  679. },
  680. .probe = mt9v032_probe,
  681. .remove = mt9v032_remove,
  682. .id_table = mt9v032_id,
  683. };
  684. module_i2c_driver(mt9v032_driver);
  685. MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
  686. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  687. MODULE_LICENSE("GPL");